From 8a4312a03c87b0bd904d79081f3c25ec3fc0376b Mon Sep 17 00:00:00 2001
From: koekeishiya <aasvi93@hotmail.com>
Date: Tue, 10 Oct 2023 21:19:21 +0200
Subject: [PATCH] #1887 rework automatic window opacity

---
 CHANGELOG.md       |  2 ++
 doc/yabai.1        | 18 --------------
 doc/yabai.asciidoc | 10 --------
 src/event_loop.c   | 62 ++++++++++++++--------------------------------
 src/event_loop.h   |  2 --
 src/event_signal.c |  4 ---
 src/event_signal.h |  4 ---
 7 files changed, 21 insertions(+), 81 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 0488b4af..9b479109 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -11,11 +11,13 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
 ### Changed
 - Fix issue causing window animations to flicker on macOS Ventura and Sonoma [#1879](https://github.com/koekeishiya/yabai/issues/1879)
 - All managed (read: tiled) windows are now automatically placed in the *below* layer. All unmanaged (read: floating) windows will use the default macOS *normal* layer and appear above the tiled layer, replicating the `window_topmost` functionality in a robust way [#1887](https://github.com/koekeishiya/yabai/issues/1887)
+- Automatic window opacity changes will now only apply to focus switches within the same space [#1887](https://github.com/koekeishiya/yabai/issues/1887)
 
 ### Removed
 - Config option `window_topmost` has been removed [#1887](https://github.com/koekeishiya/yabai/issues/1887)
 - Window command `--toggle` option `topmost` has been removed [#1887](https://github.com/koekeishiya/yabai/issues/1887)
 - Window query property `is-topmost` has been removed [#1887](https://github.com/koekeishiya/yabai/issues/1887)
+- Signal `application_activated` and `application_deactivated` has been removed; use `application_front_switched` instead [#1887](https://github.com/koekeishiya/yabai/issues/1887)
 
 ## [5.0.9] - 2023-10-01
 ### Changed
diff --git a/doc/yabai.1 b/doc/yabai.1
index 5c092a74..2f5d1aea 100644
--- a/doc/yabai.1
+++ b/doc/yabai.1
@@ -890,24 +890,6 @@ Triggered when the front\-most application changes.
 Passes two arguments: $YABAI_PROCESS_ID, $YABAI_RECENT_PROCESS_ID
 .RE
 .sp
-\fBapplication_activated\fP
-.RS 4
-Triggered when an application is activated.
-.br
-Eligible for \fBapp\fP filter.
-.br
-Passes one argument: $YABAI_PROCESS_ID
-.RE
-.sp
-\fBapplication_deactivated\fP
-.RS 4
-Triggered when an application is deactivated.
-.br
-Eligible for \fBapp\fP filter.
-.br
-Passes one argument: $YABAI_PROCESS_ID
-.RE
-.sp
 \fBapplication_visible\fP
 .RS 4
 Triggered when an application is unhidden.
diff --git a/doc/yabai.asciidoc b/doc/yabai.asciidoc
index 55be573e..e64c558f 100644
--- a/doc/yabai.asciidoc
+++ b/doc/yabai.asciidoc
@@ -648,16 +648,6 @@ EVENT
     Triggered when the front-most application changes. +
     Passes two arguments: $YABAI_PROCESS_ID, $YABAI_RECENT_PROCESS_ID
 
-*application_activated*::
-    Triggered when an application is activated. +
-    Eligible for *app* filter. +
-    Passes one argument: $YABAI_PROCESS_ID
-
-*application_deactivated*::
-    Triggered when an application is deactivated. +
-    Eligible for *app* filter. +
-    Passes one argument: $YABAI_PROCESS_ID
-
 *application_visible*::
     Triggered when an application is unhidden. +
     Eligible for *app* filter. +
diff --git a/src/event_loop.c b/src/event_loop.c
index 059a9783..1b2c2064 100644
--- a/src/event_loop.c
+++ b/src/event_loop.c
@@ -11,6 +11,12 @@ extern int g_layer_below_window_level;
 
 static void window_did_receive_focus(struct window_manager *wm, struct mouse_state *ms, struct window *window)
 {
+    struct window *focused_window = window_manager_find_window(wm, wm->focused_window_id);
+    if (focused_window && focused_window != window && window_space(focused_window) == window_space(window)) {
+        window_manager_set_window_opacity(wm, focused_window, g_window_manager.normal_window_opacity);
+        border_deactivate(focused_window);
+    }
+
     window_manager_set_window_opacity(wm, window, wm->active_window_opacity);
     border_activate(window);
 
@@ -249,28 +255,20 @@ static EVENT_HANDLER(APPLICATION_FRONT_SWITCHED)
         return;
     }
 
-    event_loop_post(&g_event_loop, APPLICATION_DEACTIVATED, (void *)(intptr_t) g_process_manager.front_pid, 0);
-    event_loop_post(&g_event_loop, APPLICATION_ACTIVATED, (void *)(intptr_t) process->pid, 0);
-
     debug("%s: %s (%d)\n", __FUNCTION__, process->name, process->pid);
     g_process_manager.switch_event_time = GetCurrentEventTime();
     g_process_manager.last_front_pid = g_process_manager.front_pid;
     g_process_manager.front_pid = process->pid;
-
     event_signal_push(SIGNAL_APPLICATION_FRONT_SWITCHED, NULL);
-}
-#pragma clang diagnostic pop
-
-static EVENT_HANDLER(APPLICATION_ACTIVATED)
-{
-    struct application *application = window_manager_find_application(&g_window_manager, (pid_t)(intptr_t) context);
-    if (!application) return;
-
-    debug("%s: %s\n", __FUNCTION__, application->name);
-    event_signal_push(SIGNAL_APPLICATION_ACTIVATED, application);
 
     uint32_t application_focused_window_id = application_focused_window(application);
     if (!application_focused_window_id) {
+        struct window *focused_window = window_manager_find_window(&g_window_manager, g_window_manager.focused_window_id);
+        if (focused_window) {
+            window_manager_set_window_opacity(&g_window_manager, focused_window, g_window_manager.normal_window_opacity);
+            border_deactivate(focused_window);
+        }
+
         g_window_manager.last_window_id = g_window_manager.focused_window_id;
         g_window_manager.focused_window_id = 0;
         g_window_manager.focused_window_psn = application->psn;
@@ -280,6 +278,12 @@ static EVENT_HANDLER(APPLICATION_ACTIVATED)
 
     struct window *window = window_manager_find_window(&g_window_manager, application_focused_window_id);
     if (!window) {
+        struct window *focused_window = window_manager_find_window(&g_window_manager, g_window_manager.focused_window_id);
+        if (focused_window) {
+            window_manager_set_window_opacity(&g_window_manager, focused_window, g_window_manager.normal_window_opacity);
+            border_deactivate(focused_window);
+        }
+
         window_manager_add_lost_focused_event(&g_window_manager, application_focused_window_id);
         return;
     }
@@ -287,29 +291,7 @@ static EVENT_HANDLER(APPLICATION_ACTIVATED)
     window_did_receive_focus(&g_window_manager, &g_mouse_state, window);
     event_signal_push(SIGNAL_WINDOW_FOCUSED, window);
 }
-
-static EVENT_HANDLER(APPLICATION_DEACTIVATED)
-{
-    struct application *application = window_manager_find_application(&g_window_manager, (pid_t)(intptr_t) context);
-    if (!application) return;
-
-    debug("%s: %s\n", __FUNCTION__, application->name);
-    event_signal_push(SIGNAL_APPLICATION_DEACTIVATED, application);
-
-    struct window *focused_window = window_manager_find_window(&g_window_manager, application_focused_window(application));
-    if (!focused_window) return;
-
-    window_manager_set_window_opacity(&g_window_manager, focused_window, g_window_manager.normal_window_opacity);
-    border_deactivate(focused_window);
-
-    if (!window_level_is_standard(focused_window) || !window_is_standard(focused_window)) {
-        struct window *main_window = window_manager_find_window(&g_window_manager, application_main_window(application));
-        if (main_window && main_window != focused_window) {
-            window_manager_set_window_opacity(&g_window_manager, main_window, g_window_manager.normal_window_opacity);
-            border_deactivate(main_window);
-        }
-    }
-}
+#pragma clang diagnostic pop
 
 static EVENT_HANDLER(APPLICATION_VISIBLE)
 {
@@ -515,12 +497,6 @@ static EVENT_HANDLER(WINDOW_FOCUSED)
         return;
     }
 
-    struct window *focused_window = window_manager_find_window(&g_window_manager, g_window_manager.focused_window_id);
-    if (focused_window && focused_window != window) {
-        window_manager_set_window_opacity(&g_window_manager, focused_window, g_window_manager.normal_window_opacity);
-        border_deactivate(focused_window);
-    }
-
     debug("%s: %s %d\n", __FUNCTION__, window->application->name, window->id);
     window_did_receive_focus(&g_window_manager, &g_mouse_state, window);
     event_signal_push(SIGNAL_WINDOW_FOCUSED, window);
diff --git a/src/event_loop.h b/src/event_loop.h
index 2d4b5fa6..aa5dd0eb 100644
--- a/src/event_loop.h
+++ b/src/event_loop.h
@@ -7,8 +7,6 @@
     EVENT_TYPE_ENTRY(APPLICATION_LAUNCHED) \
     EVENT_TYPE_ENTRY(APPLICATION_TERMINATED) \
     EVENT_TYPE_ENTRY(APPLICATION_FRONT_SWITCHED) \
-    EVENT_TYPE_ENTRY(APPLICATION_ACTIVATED) \
-    EVENT_TYPE_ENTRY(APPLICATION_DEACTIVATED) \
     EVENT_TYPE_ENTRY(APPLICATION_VISIBLE) \
     EVENT_TYPE_ENTRY(APPLICATION_HIDDEN) \
     EVENT_TYPE_ENTRY(WINDOW_CREATED) \
diff --git a/src/event_signal.c b/src/event_signal.c
index 66d9f05c..3fcab8fe 100644
--- a/src/event_signal.c
+++ b/src/event_signal.c
@@ -11,8 +11,6 @@ static bool event_signal_filter(struct event_signal *es, struct signal *signal)
     default: return false;
 
     case SIGNAL_APPLICATION_LAUNCHED:
-    case SIGNAL_APPLICATION_ACTIVATED:
-    case SIGNAL_APPLICATION_DEACTIVATED:
     case SIGNAL_APPLICATION_VISIBLE: {
         int regex_match_app = signal->app_regex_exclude ? REGEX_MATCH_YES : REGEX_MATCH_NO;
         return regex_match(signal->app_regex_valid, &signal->app_regex, es->app) == regex_match_app;
@@ -115,8 +113,6 @@ void event_signal_push(enum signal_type type, void *context)
     default: break;
 
     case SIGNAL_APPLICATION_LAUNCHED:
-    case SIGNAL_APPLICATION_ACTIVATED:
-    case SIGNAL_APPLICATION_DEACTIVATED:
     case SIGNAL_APPLICATION_VISIBLE: {
         struct application *application = context;
 
diff --git a/src/event_signal.h b/src/event_signal.h
index 558aa4ea..66d5b485 100644
--- a/src/event_signal.h
+++ b/src/event_signal.h
@@ -8,8 +8,6 @@ enum signal_type
     SIGNAL_APPLICATION_LAUNCHED,
     SIGNAL_APPLICATION_TERMINATED,
     SIGNAL_APPLICATION_FRONT_SWITCHED,
-    SIGNAL_APPLICATION_ACTIVATED,
-    SIGNAL_APPLICATION_DEACTIVATED,
     SIGNAL_APPLICATION_VISIBLE,
     SIGNAL_APPLICATION_HIDDEN,
 
@@ -49,8 +47,6 @@ static const char *signal_type_str[] =
     [SIGNAL_APPLICATION_LAUNCHED]           = "application_launched",
     [SIGNAL_APPLICATION_TERMINATED]         = "application_terminated",
     [SIGNAL_APPLICATION_FRONT_SWITCHED]     = "application_front_switched",
-    [SIGNAL_APPLICATION_ACTIVATED]          = "application_activated",
-    [SIGNAL_APPLICATION_DEACTIVATED]        = "application_deactivated",
     [SIGNAL_APPLICATION_VISIBLE]            = "application_visible",
     [SIGNAL_APPLICATION_HIDDEN]             = "application_hidden",