fix: menu in macos now with hotkeys

This commit is contained in:
2026-04-25 09:54:33 +02:00
parent 390c67df9b
commit 26d4954724
3 changed files with 96 additions and 10 deletions

View File

@@ -3,18 +3,54 @@ defmodule BDS.UI.Commands do
alias BDS.UI.MenuBar
@menu_shortcuts [
%{id: :new_post, accelerator: "CTRL+N"},
%{id: :import_media, accelerator: "CTRL+I"},
%{id: :save, accelerator: "CTRL+S"},
%{id: :close_tab, accelerator: "CTRL+W", key: "w", primary: true},
%{id: :quit, accelerator: "CTRL+Q"},
%{id: :undo, accelerator: "CTRL+Z"},
%{id: :redo, accelerator: "CTRL+Y"},
%{id: :cut, accelerator: "CTRL+X"},
%{id: :copy, accelerator: "CTRL+C"},
%{id: :paste, accelerator: "CTRL+V"},
%{id: :select_all, accelerator: "CTRL+A"},
%{id: :find, accelerator: "CTRL+F"},
%{id: :replace, accelerator: "CTRL+H"},
%{id: :edit_preferences, accelerator: "CTRL+,"},
%{id: :view_posts, accelerator: "CTRL+1", key: "1", primary: true},
%{id: :view_media, accelerator: "CTRL+2", key: "2", primary: true},
%{id: :toggle_sidebar, accelerator: "CTRL+B", key: "b", primary: true},
%{id: :toggle_panel, accelerator: "CTRL+J", key: "j", primary: true},
%{id: :toggle_assistant_sidebar, accelerator: "CTRL+\\", key: "\\", primary: true},
%{id: :toggle_dev_tools, accelerator: "CTRL+SHIFT+I"},
%{id: :publish_selected, accelerator: "CTRL+SHIFT+P"},
%{id: :preview_post, accelerator: "CTRL+SHIFT+V"},
%{id: :generate_sitemap, accelerator: "CTRL+R"},
%{id: :validate_site, accelerator: "CTRL+SHIFT+L"},
%{id: :upload_site, accelerator: "CTRL+SHIFT+U"}
]
def handle_shortcut(state, shortcut) when is_map(shortcut) do
key = shortcut |> Map.get(:key, Map.get(shortcut, "key", "")) |> String.downcase()
primary = Map.get(shortcut, :meta, false) or Map.get(shortcut, :ctrl, false)
cond do
primary and key == "b" -> MenuBar.execute(state, :toggle_sidebar)
primary and key == "j" -> MenuBar.execute(state, :toggle_panel)
primary and key == "1" -> MenuBar.execute(state, :view_posts)
primary and key == "2" -> MenuBar.execute(state, :view_media)
primary and key == "\\" -> MenuBar.execute(state, :toggle_assistant_sidebar)
primary and key == "w" -> MenuBar.execute(state, :close_tab)
true -> state
case Enum.find(@menu_shortcuts, &shortcut_match?(&1, key, primary)) do
%{id: command_id} -> MenuBar.execute(state, command_id)
nil -> state
end
end
def accelerator_label(command_id) when is_atom(command_id) do
case Enum.find(@menu_shortcuts, &(&1.id == command_id)) do
%{accelerator: accelerator} -> accelerator
nil -> nil
end
end
defp shortcut_match?(%{key: expected_key, primary: expected_primary}, key, primary) do
key == expected_key and primary == expected_primary
end
defp shortcut_match?(_shortcut, _key, _primary), do: false
end