From 84f712ed1decba54aa06e052e85f43d2ba55dcb5 Mon Sep 17 00:00:00 2001 From: Dominic Letz Date: Sun, 2 Aug 2026 13:02:06 +0800 Subject: [PATCH] Allow configurable menu adapters for third-party backends. Honor config :desktop, :menu_adapter so packages like desktop_webview can plug in without hardcoding Wx/Json/DBus/Browser. Co-authored-by: Cursor --- AGENTS.md | 3 ++- guides/faq.md | 8 ++++++++ lib/desktop/menu.ex | 5 +---- lib/desktop/platform/menu.ex | 5 ++++- 4 files changed, 15 insertions(+), 6 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index d36a715..b9ac89a 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -44,7 +44,8 @@ All commands that load the `:wx` application (compile, test, iex) need a display | `Desktop.Backend.Json` | `:mobile_target` compile config or `OS.mobile?/0` — JSON bridge via `BRIDGE_PORT` | | `Desktop.Backend.Browser` | `NO_WX=1` or `:wx` unavailable | -Override with `config :desktop, :backend, :wx | :json | :browser | :auto`. +Override with `config :desktop, :backend, :wx | :json | :browser | :auto` or a custom module. +Custom backends may set `config :desktop, :menu_adapter, Some.Menu.Adapter` (used by `desktop_webview`). ### Platform abstraction (do not regress) diff --git a/guides/faq.md b/guides/faq.md index 0b11c53..e77adee 100644 --- a/guides/faq.md +++ b/guides/faq.md @@ -34,6 +34,14 @@ For a custom implementation, set the backend to a module that implements the `De config :desktop, :backend, MyApp.DesktopBackend ``` +Third-party backends that ship their own menu adapter (for example [`desktop_webview`](https://github.com/elixir-desktop/webview)) should also set: + +```elixir +config :desktop, :menu_adapter, DesktopWebview.Menu.Adapter +``` + +`Desktop.Platform.Menu.adapter/1` prefers `config :desktop, :menu_adapter` when present, then falls back to Wx / Json / DBus / Browser selection. + Restart the app after changing backend config — the router reads `Application.get_env(:desktop, :backend, :auto)` at runtime. ### Environment variables diff --git a/lib/desktop/menu.ex b/lib/desktop/menu.ex index e0b6f41..a5041c7 100644 --- a/lib/desktop/menu.ex +++ b/lib/desktop/menu.ex @@ -291,14 +291,11 @@ defmodule Desktop.Menu do adapter_module = case Keyword.get(init_opts, :adapter) do - mod when mod in [Adapter.Wx, Adapter.DBus, Adapter.Json, Adapter.Browser] -> + mod when is_atom(mod) and not is_nil(mod) -> mod nil -> Desktop.Platform.Menu.adapter(init_opts) - - _ -> - Desktop.Platform.Menu.adapter(init_opts) end adapter_opts = diff --git a/lib/desktop/platform/menu.ex b/lib/desktop/platform/menu.ex index 688c40a..20c07a8 100644 --- a/lib/desktop/platform/menu.ex +++ b/lib/desktop/platform/menu.ex @@ -11,7 +11,10 @@ defmodule Desktop.Platform.Menu do caps = Desktop.Platform.capabilities() cond do - Keyword.get(opts, :adapter) in [Adapter.Wx, Adapter.DBus, Adapter.Json, Adapter.Browser] -> + mod = Application.get_env(:desktop, :menu_adapter) -> + mod + + Keyword.get(opts, :adapter) -> Keyword.get(opts, :adapter) Keyword.get(opts, :sni) != nil and Desktop.Platform.backend() == Desktop.Backend.Wx ->