Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
- `Desktop.Platform.System.custom_event/2` — mobile bridge custom events (replaces Hex `Bridge` `[:custom_event, …]` calls)
- `Desktop.Platform.Content.reload/1` and `Desktop.Window.reload/1` — backend-safe webview reload (replaces `:wxWebView.reload/1`)
- Test suite: `mix test.fast`, `xvfb-run mix test.wx`, `mix test.guard` — see `docs/TEST_PLAN.md`
- Compile without OTP `:wx`: conditional `erl_src_paths` and `Desktop.Wx` fallbacks (no `wx.hrl` required)
- `Desktop.Menu` traps parent exits and destroys the taskbar icon on terminate so a window crash cannot leave a duplicate tray icon

- `Desktop.Env` subscriber path/URL payloads (`:open_url`, `:open_file`, `:print_file`) are canonical UTF-8 binaries; wx charlists are normalized at the Env boundary before buffer or delivery

## Changes in 1.5
Expand Down
3 changes: 3 additions & 0 deletions docs/TEST_PLAN.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ L4 App smoke → desktop-example-app (optional, manual/CI nightly)
|----|------|
| T-MENU-01 | Browser adapter menubar nil, set_icon ok |
| T-MENU-02 | Json adapter minimal menubar DOM |
| T-MENU-03 | `GenServer.stop` on Menu calls adapter `set_icon(nil)` (tray destroy) |
| T-MENU-04 | Linked parent crash destroys the taskbar icon via trapped EXIT |
| T-MENU-05 | EXIT from a non-parent process does not destroy the taskbar icon |
| T-FALL-01 | `webview_load` with nil webview does not raise |

### L0 guard — `test/support/guard_boolean_ops.exs` (new)
Expand Down
14 changes: 14 additions & 0 deletions lib/desktop/menu.ex
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ defmodule Desktop.Menu do

@impl true
def init(init_opts) do
Process.flag(:trap_exit, true)
menu_pid = self()
module = Keyword.get(init_opts, :module)
dom = Keyword.get(init_opts, :dom, [])
Expand Down Expand Up @@ -397,6 +398,13 @@ defmodule Desktop.Menu do
end

@impl true
def handle_info({:EXIT, from, reason}, menu) do
case Process.info(self(), :parent) do
{:parent, ^from} -> {:stop, reason, menu}
_ -> {:noreply, menu}
end
end
Comment thread
dominicletz marked this conversation as resolved.
Resolved

def handle_info(event, menu = %{__adapter__: adapter = %{__struct__: adapter_module}})
when is_tuple(event) and elem(event, 0) == :wx do
{:noreply, adapter} = adapter_module.handle_info(event, adapter)
Expand All @@ -408,6 +416,12 @@ defmodule Desktop.Menu do
{:noreply, proxy_handle_info(msg, menu)}
end

@impl true
def terminate(_reason, %{__adapter__: adapter}) do
_ = Adapter.set_icon(adapter, nil)
:ok
end

# Private functions

defp get_adapter_icon(%{__adapter__: adapter}) do
Expand Down
16 changes: 16 additions & 0 deletions lib/desktop/window.ex
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,14 @@ defmodule Desktop.Window do
menu_pid
end

if is_pid(taskbar) do
OnCrash.call(fn reason ->
if reason != :normal do
stop_taskbar(taskbar)
end
end)
end

ui = %Window{
frame: frame,
id: options[:id],
Expand Down Expand Up @@ -781,4 +789,12 @@ defmodule Desktop.Window do
end
|> URI.to_string()
end

defp stop_taskbar(pid) when is_pid(pid) do
if Process.alive?(pid) do
GenServer.stop(pid, :shutdown, 2000)
end
catch
:exit, _ -> :ok
end
end
82 changes: 82 additions & 0 deletions test/desktop/menu_crash_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
defmodule Desktop.MenuCrashTest do
use ExUnit.Case, async: false

alias Desktop.Menu
alias Desktop.Menu.Adapter.Fake

defmodule IconMenu do
use Desktop.Menu, server: false

def mount(menu), do: {:ok, menu}
def handle_event(_event, menu), do: {:noreply, menu}
def handle_info(_msg, menu), do: {:noreply, menu}

def render(assigns) do
~H"""
<menu>
<item onclick="quit">Quit</item>
</menu>
"""
end
end

test "T-MENU-03: GenServer.stop destroys the taskbar icon" do
Process.flag(:trap_exit, true)
{:ok, pid} = start_menu()
assert Process.alive?(pid)

GenServer.stop(pid, :shutdown)
assert_receive {:tray_destroyed, :tray}, 1000
refute Process.alive?(pid)
end

test "T-MENU-04: parent crash destroys the taskbar icon" do
test = self()

parent =
spawn(fn ->
{:ok, pid} = start_menu(notify: test)
send(test, {:menu, pid})

receive do
:crash -> exit(:crash)
end
end)

assert_receive {:menu, menu}, 1000
ref = Process.monitor(menu)

ExUnit.CaptureLog.capture_log(fn ->
send(parent, :crash)
assert_receive {:tray_destroyed, :tray}, 1000
assert_receive {:DOWN, ^ref, :process, ^menu, :crash}, 1000
end)
end

test "T-MENU-05: non-parent EXIT does not destroy the taskbar icon" do
Process.flag(:trap_exit, true)
{:ok, pid} = start_menu()
stranger = spawn(fn -> :ok end)

send(pid, {:EXIT, stranger, :normal})
send(pid, {:EXIT, stranger, :crash})

refute_receive {:tray_destroyed, :tray}, 200
assert Process.alive?(pid)

GenServer.stop(pid, :shutdown)
assert_receive {:tray_destroyed, :tray}, 1000
end

defp start_menu(opts \\ []) do
notify = Keyword.get(opts, :notify, self())

Menu.start_link(
module: IconMenu,
adapter: Fake,
notify: notify,
wx: {:taskbar, :icon},
dom: []
)
end
end
36 changes: 36 additions & 0 deletions test/support/fake_menu_adapter.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
defmodule Desktop.Menu.Adapter.Fake do
@moduledoc false

defstruct [:menu_pid, :notify, :taskbar_icon]

def new(opts) do
tray =
case Keyword.get(opts, :wx) do
{:taskbar, _} -> :tray
_ -> nil
end

%__MODULE__{
menu_pid: Keyword.get(opts, :menu_pid),
notify: Keyword.get(opts, :notify),
taskbar_icon: tray
}
end

def create(adapter, _dom), do: adapter
def update_dom(adapter, _dom), do: adapter
def popup_menu(adapter), do: adapter
def recreate_menu(adapter, _dom), do: adapter
def menubar(_adapter), do: nil
def get_icon(%{taskbar_icon: icon}), do: icon

def set_icon(adapter, nil) do
if is_pid(adapter.notify) and adapter.taskbar_icon != nil do
send(adapter.notify, {:tray_destroyed, adapter.taskbar_icon})
end

{:ok, %{adapter | taskbar_icon: nil}}
end

def set_icon(adapter, icon), do: {:ok, %{adapter | taskbar_icon: icon}}
end
Loading