@@ -9,6 +9,11 @@ defmodule Desktop.Env do
99
1010 Also it has a global connect() method to allow binding of :wx event callbacks using
1111 this long lived process as reference.
12+
13+ Subscribers (see `subscribe/0`) also receive:
14+
15+ * `{:desktop, :window_activated, window_id}` — a `Desktop.Window` with registered
16+ `id` has become the active frame (user brought the app window to the foreground).
1217 """
1318 alias Desktop.Env
1419 use GenServer
@@ -54,8 +59,15 @@ defmodule Desktop.Env do
5459 send ( pid , e )
5560 end
5661
57- Process . monitor ( pid )
58- { :reply , :ok , % Env { d | subs: [ pid | subs ] , events: [ ] } }
62+ subs =
63+ if pid in subs do
64+ subs
65+ else
66+ Process . monitor ( pid )
67+ [ pid | subs ]
68+ end
69+
70+ { :reply , :ok , % Env { d | subs: subs , events: [ ] } }
5971 end
6072
6173 def handle_call ( :wx_env , _from , d = % Env { wx_env: env } ) do
@@ -112,6 +124,14 @@ defmodule Desktop.Env do
112124 { :noreply , % Env { state | windows: [ window | windows ] } }
113125 end
114126
127+ def handle_cast ( { :notify_subscribers , message } , state = % Env { subs: subs } ) do
128+ for sub <- subs do
129+ send ( sub , message )
130+ end
131+
132+ { :noreply , state }
133+ end
134+
115135 @ impl true
116136 def handle_info ( { :reopen_app , [ ] } , state = % Env { windows: windows } ) do
117137 # Handling MacOS event when the app icon is clicked again
@@ -259,11 +279,22 @@ defmodule Desktop.Env do
259279 * `{:open_file, [filename]}`
260280 * `{:open_url, [filename]}`
261281 * `{:new_file, []}`
282+ * `{:desktop, :window_activated, window_id}` — from `Desktop.Window` when the
283+ frame becomes active (see `Desktop.Env` module doc).
262284 """
263285 def subscribe ( ) do
264286 GenServer . call ( __MODULE__ , { :subscribe , self ( ) } )
265287 end
266288
289+ @ doc """
290+ Delivers a message to all processes that called `subscribe/0`.
291+
292+ Used internally by `Desktop.Window` for lifecycle events (e.g. frame activation).
293+ """
294+ def notify_subscribers ( message ) when is_tuple ( message ) do
295+ GenServer . cast ( __MODULE__ , { :notify_subscribers , message } )
296+ end
297+
267298 defp init_sni ( ) do
268299 # We're protecting the main application from any possible
269300 # side effects of the SNI startup using a monitored (not linked) process:
0 commit comments