Skip to content

Commit

Permalink
#503 add command to explicitly modify window opacity
Browse files Browse the repository at this point in the history
  • Loading branch information
koekeishiya committed Jun 13, 2020
1 parent 2e419dc commit 22fcc31
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 20 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
## [Unreleased]
### Added
- Re-introduce a more efficient window border system [#565](https://github.com/koekeishiya/yabai/issues/565)
- New command *window --opacity* to explicitly set the opacity of a window [#503](https://github.com/koekeishiya/yabai/issues/503)

### Changed
- Re-construct application switched and window created events in the correct order when the window is moved through a rule upon creation [#564](https://github.com/koekeishiya/yabai/issues/564)
Expand Down
21 changes: 14 additions & 7 deletions doc/yabai.1
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
.\" Title: yabai
.\" Author: [see the "AUTHOR(S)" section]
.\" Generator: Asciidoctor 2.0.10
.\" Date: 2020-06-12
.\" Date: 2020-06-13
.\" Manual: Yabai Manual
.\" Source: Yabai
.\" Language: English
.\"
.TH "YABAI" "1" "2020-06-12" "Yabai" "Yabai Manual"
.TH "YABAI" "1" "2020-06-13" "Yabai" "Yabai Manual"
.ie \n(.g .ds Aq \(aq
.el .ds Aq '
.ss \n[.ss] 0
Expand Down Expand Up @@ -414,6 +414,13 @@ Toggle the given property of the selected window.
Set the stacking layer of the selected window.
.RE
.sp
\fB\-\-opacity\fP \fI<floating point number>\fP
.RS 4
Set the opacity of the selected window. The window will no longer be eligible for automatic change in opacity upon focus change.
.br
Specify the value \fI0.0\fP to reset back to full opacity OR have it be automatically managed through focus changes.
.RE
.sp
\fB\-\-display\fP \fI<DISPLAY_SEL>\fP
.RS 4
Send the selected window to the given display.
Expand Down Expand Up @@ -527,11 +534,6 @@ Send window to display. If \fI^\fP is present, follow focus.
Send window to space. If \fI^\fP is present, follow focus.
.RE
.sp
\fBopacity=\fI<FLOAT_SEL>\fP\fP
.RS 4
Set window opacity.
.RE
.sp
\fBmanage=\fI<BOOL_SEL>\fP\fP
.RS 4
Window should be managed (tile vs float).
Expand All @@ -547,6 +549,11 @@ Window appears on all spaces.
Window is ordered within the given stacking layer.
.RE
.sp
\fBopacity=\fI<FLOAT_SEL>\fP\fP
.RS 4
Set window opacity. The window will no longer be eligible for automatic change in opacity upon focus change.
.RE
.sp
\fBborder=<BOOL_SEL>\(aq\fP
.RS 4
Window should draw border when \fIconfig window_border on\fP is specified.
Expand Down
10 changes: 7 additions & 3 deletions doc/yabai.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,10 @@ COMMAND
*--layer* '<LAYER>'::
Set the stacking layer of the selected window.

*--opacity* '<floating point number>'::
Set the opacity of the selected window. The window will no longer be eligible for automatic change in opacity upon focus change. +
Specify the value '0.0' to reset back to full opacity OR have it be automatically managed through focus changes.

*--display* '<DISPLAY_SEL>'::
Send the selected window to the given display.

Expand Down Expand Up @@ -394,9 +398,6 @@ ARGUMENT
*space='[^]<SPACE_SEL>'*::
Send window to space. If '^' is present, follow focus.

*opacity='<FLOAT_SEL>'*::
Set window opacity.

*manage='<BOOL_SEL>'*::
Window should be managed (tile vs float).

Expand All @@ -406,6 +407,9 @@ ARGUMENT
*layer='<LAYER>'*::
Window is ordered within the given stacking layer.

*opacity='<FLOAT_SEL>'*::
Set window opacity. The window will no longer be eligible for automatic change in opacity upon focus change.

*border=<BOOL_SEL>'*::
Window should draw border when 'config window_border on' is specified.

Expand Down
12 changes: 11 additions & 1 deletion src/message.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ extern bool g_verbose;
#define COMMAND_WINDOW_DEMIN "--deminimize"
#define COMMAND_WINDOW_CLOSE "--close"
#define COMMAND_WINDOW_LAYER "--layer"
#define COMMAND_WINDOW_OPACITY "--opacity"
#define COMMAND_WINDOW_TOGGLE "--toggle"
#define COMMAND_WINDOW_DISPLAY "--display"
#define COMMAND_WINDOW_SPACE "--space"
Expand Down Expand Up @@ -1601,6 +1602,15 @@ static void handle_domain_window(FILE *rsp, struct token domain, char *message)
} else {
daemon_fail(rsp, "unknown value '%.*s' given to command '%.*s' for domain '%.*s'\n", value.length, value.text, command.length, command.text, domain.length, domain.text);
}
} else if (token_equals(command, COMMAND_WINDOW_OPACITY)) {
float opacity;
struct token value = get_token(&message);
if ((sscanf(value.text, "%f", &opacity) == 1) && in_range_ii(opacity, 0.0f, 1.0f)) {
acting_window->opacity = opacity;
window_manager_set_opacity(&g_window_manager, acting_window, opacity);
} else {
daemon_fail(rsp, "unknown value '%.*s' given to command '%.*s' for domain '%.*s'\n", value.length, value.text, command.length, command.text, domain.length, domain.text);
}
} else if (token_equals(command, COMMAND_WINDOW_TOGGLE)) {
struct token value = get_token(&message);
if (token_equals(value, ARGUMENT_WINDOW_TOGGLE_FLOAT)) {
Expand Down Expand Up @@ -1907,7 +1917,7 @@ static void handle_domain_rule(FILE *rsp, struct token domain, char *message)
} else if (string_equals(key, ARGUMENT_RULE_KEY_ALPHA)) {
if (exclusion) unsupported_exclusion = key;

if (sscanf(value, "%f", &rule.alpha) != 1) {
if ((sscanf(value, "%f", &rule.alpha) != 1) || (!in_range_ei(rule.alpha, 0.0f, 1.0f))) {
daemon_fail(rsp, "invalid value '%s' for key '%s'\n", value, key);
did_parse = false;
}
Expand Down
10 changes: 10 additions & 0 deletions src/window.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ void window_serialize(FILE *rsp, struct window *window)
bool is_minimized = window_is_minimized(window);
bool visible = !is_minimized && (sticky || space_is_visible(sid));
bool border = window->border.id ? 1 : 0;
float opacity = window_opacity(window);

CFStringRef cfrole = window_role(window);
if (cfrole) {
Expand Down Expand Up @@ -163,6 +164,7 @@ void window_serialize(FILE *rsp, struct window *window)
"\t\"sticky\":%d,\n"
"\t\"minimized\":%d,\n"
"\t\"topmost\":%d,\n"
"\t\"opacity\":%.4f,\n"
"\t\"shadow\":%d,\n"
"\t\"border\":%d,\n"
"\t\"zoom-parent\":%d,\n"
Expand All @@ -189,6 +191,7 @@ void window_serialize(FILE *rsp, struct window *window)
sticky,
is_minimized,
is_topmost,
opacity,
window->has_shadow,
border,
zoom_parent,
Expand Down Expand Up @@ -329,6 +332,13 @@ bool window_is_topmost(struct window *window)
return is_topmost;
}

float window_opacity(struct window *window)
{
float alpha = 0.0f;
SLSGetWindowAlpha(g_connection, window->id, &alpha);
return alpha;
}

int window_level(struct window *window)
{
int level = 0;
Expand Down
4 changes: 3 additions & 1 deletion src/window.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
extern int SLSMainConnectionID(void);
extern CGError SLSGetWindowBounds(int cid, uint32_t wid, CGRect *frame);
extern CGError SLSGetWindowLevel(int cid, uint32_t wid, int *level);
extern CGError SLSGetWindowAlpha(int cid, uint32_t wid, float *alpha);
extern CGError SLSSetWindowResolution(int cid, uint32_t wid, float resolution);
extern CGError SLSCopyWindowProperty(int cid, uint32_t wid, CFStringRef property, CFTypeRef *value);
extern CFStringRef SLSCopyManagedDisplayForWindow(int cid, uint32_t wid);
Expand Down Expand Up @@ -49,7 +50,7 @@ struct window
bool is_fullscreen;
bool is_minimized;
bool is_floating;
float rule_alpha;
float opacity;
bool rule_manage;
bool rule_fullscreen;
struct border border;
Expand All @@ -63,6 +64,7 @@ void window_serialize(FILE *rsp, struct window *window);
char *window_title(struct window *window);
CGRect window_ax_frame(struct window *window);
CGRect window_frame(struct window *window);
float window_opacity(struct window *window);
int window_level(struct window *window);
CFStringRef window_role(struct window *window);
CFStringRef window_subrole(struct window *window);
Expand Down
31 changes: 23 additions & 8 deletions src/window_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ void window_manager_apply_rule_to_window(struct space_manager *sm, struct window
}

if (in_range_ei(rule->alpha, 0.0f, 1.0f)) {
window_manager_set_window_opacity(wm, window, rule->alpha);
window->rule_alpha = rule->alpha;
window->opacity = rule->alpha;
window_manager_set_opacity(wm, window, rule->alpha);
}

if (rule->manage == RULE_PROP_ON) {
Expand Down Expand Up @@ -500,15 +500,19 @@ void window_manager_set_purify_mode(struct window_manager *wm, enum purify_mode
}
}

void window_manager_set_window_opacity(struct window_manager *wm, struct window *window, float opacity)
void window_manager_set_opacity(struct window_manager *wm, struct window *window, float opacity)
{
if (!wm->enable_window_opacity) return;
if (window->rule_alpha != 0.0f) return;
if ((!window_is_standard(window)) && (!window_is_dialog(window))) return;

int sockfd;
char message[MAXLEN];

if (opacity == 0.0f) {
if (wm->enable_window_opacity) {
opacity = window->id == wm->focused_window_id ? wm->active_window_opacity : wm->normal_window_opacity;
} else {
opacity = 1.0f;
}
}

if (socket_connect_un(&sockfd, g_sa_socket_file)) {
snprintf(message, sizeof(message), "window_alpha_fade %d %f %f", window->id, opacity, wm->window_opacity_duration);
socket_write(sockfd, message);
Expand All @@ -517,6 +521,15 @@ void window_manager_set_window_opacity(struct window_manager *wm, struct window
socket_close(sockfd);
}

void window_manager_set_window_opacity(struct window_manager *wm, struct window *window, float opacity)
{
if (!wm->enable_window_opacity) return;
if (window->opacity != 0.0f) return;
if ((!window_is_standard(window)) && (!window_is_dialog(window))) return;

window_manager_set_opacity(wm, window, opacity);
}

void window_manager_set_active_window_opacity(struct window_manager *wm, float opacity)
{
wm->active_window_opacity = opacity;
Expand All @@ -532,9 +545,11 @@ void window_manager_set_normal_window_opacity(struct window_manager *wm, float o
while (bucket) {
if (bucket->value) {
struct window *window = bucket->value;
if (window->id != wm->focused_window_id) window_manager_set_window_opacity(wm, window, wm->normal_window_opacity);
if (window->id == wm->focused_window_id) goto next;
window_manager_set_window_opacity(wm, window, wm->normal_window_opacity);
}

next:
bucket = bucket->next;
}
}
Expand Down
1 change: 1 addition & 0 deletions src/window_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ enum window_op_error window_manager_resize_window_relative(struct window_manager
void window_manager_set_purify_mode(struct window_manager *wm, enum purify_mode mode);
void window_manager_set_active_window_opacity(struct window_manager *wm, float opacity);
void window_manager_set_normal_window_opacity(struct window_manager *wm, float opacity);
void window_manager_set_opacity(struct window_manager *wm, struct window *window, float opacity);
void window_manager_set_window_opacity(struct window_manager *wm, struct window *window, float opacity);
void window_manager_set_window_border_enabled(struct window_manager *wm, bool enabled);
void window_manager_set_window_border_width(struct window_manager *wm, int width);
Expand Down

0 comments on commit 22fcc31

Please sign in to comment.