mirror of
https://github.com/livebook-dev/livebook.git
synced 2025-11-09 21:51:42 +08:00
Fix macOS menu bar (#1057)
This commit is contained in:
parent
e45f22c328
commit
cb7f51726a
2 changed files with 38 additions and 31 deletions
|
|
@ -21,6 +21,7 @@ defmodule WxDemo.Window do
|
||||||
|
|
||||||
# https://github.com/erlang/otp/blob/OTP-24.1.2/lib/wx/include/wx.hrl#L1314
|
# https://github.com/erlang/otp/blob/OTP-24.1.2/lib/wx/include/wx.hrl#L1314
|
||||||
@wx_id_exit 5006
|
@wx_id_exit 5006
|
||||||
|
@wx_id_osx_hide 5250
|
||||||
|
|
||||||
def start_link(_) do
|
def start_link(_) do
|
||||||
{:wx_ref, _, _, pid} = :wx_object.start_link(__MODULE__, [], [])
|
{:wx_ref, _, _, pid} = :wx_object.start_link(__MODULE__, [], [])
|
||||||
|
|
@ -45,19 +46,19 @@ defmodule WxDemo.Window do
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
def init(_) do
|
def init(_) do
|
||||||
title = "WxDemo"
|
app_name = "WxDemo"
|
||||||
|
|
||||||
true = Process.register(self(), __MODULE__)
|
true = Process.register(self(), __MODULE__)
|
||||||
os = os()
|
os = os()
|
||||||
wx = :wx.new()
|
wx = :wx.new()
|
||||||
frame = :wxFrame.new(wx, -1, title, size: {100, 100})
|
frame = :wxFrame.new(wx, -1, app_name, size: {100, 100})
|
||||||
|
|
||||||
if os == :macos do
|
if os == :macos do
|
||||||
fixup_macos_menubar(frame, title)
|
fixup_macos_menubar(frame, app_name)
|
||||||
end
|
end
|
||||||
|
|
||||||
:wxFrame.show(frame)
|
:wxFrame.show(frame)
|
||||||
:wxFrame.connect(frame, :command_menu_selected)
|
:wxFrame.connect(frame, :command_menu_selected, skip: true)
|
||||||
:wxFrame.connect(frame, :close_window, skip: true)
|
:wxFrame.connect(frame, :close_window, skip: true)
|
||||||
|
|
||||||
case os do
|
case os do
|
||||||
|
|
@ -84,6 +85,12 @@ defmodule WxDemo.Window do
|
||||||
{:stop, :shutdown, state}
|
{:stop, :shutdown, state}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# ignore other menu events
|
||||||
|
@impl true
|
||||||
|
def handle_event({:wx, _, _, _, {:wxCommand, :command_menu_selected, _, _, _}}, state) do
|
||||||
|
{:noreply, state}
|
||||||
|
end
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
def handle_info(event, state) do
|
def handle_info(event, state) do
|
||||||
show_dialog(state, inspect(event))
|
show_dialog(state, inspect(event))
|
||||||
|
|
@ -97,22 +104,21 @@ defmodule WxDemo.Window do
|
||||||
|> :wxDialog.showModal()
|
|> :wxDialog.showModal()
|
||||||
end
|
end
|
||||||
|
|
||||||
defp fixup_macos_menubar(frame, title) do
|
# 1. WxeApp attaches event handler to "Quit" menu item that does nothing (to not accidentally bring
|
||||||
|
# down the VM). Let's create a fresh menu bar without that caveat.
|
||||||
|
# 2. Fix app name
|
||||||
|
defp fixup_macos_menubar(frame, app_name) do
|
||||||
menubar = :wxMenuBar.new()
|
menubar = :wxMenuBar.new()
|
||||||
# :wxMenuBar.setAutoWindowMenu(false)
|
|
||||||
:wxFrame.setMenuBar(frame, menubar)
|
:wxFrame.setMenuBar(frame, menubar)
|
||||||
|
|
||||||
# App Menu
|
|
||||||
menu = :wxMenuBar.oSXGetAppleMenu(menubar)
|
menu = :wxMenuBar.oSXGetAppleMenu(menubar)
|
||||||
|
|
||||||
# Remove all items except for quit
|
menu
|
||||||
for item <- :wxMenu.getMenuItems(menu) do
|
|> :wxMenu.findItem(@wx_id_osx_hide)
|
||||||
if :wxMenuItem.getId(item) == @wx_id_exit do
|
|> :wxMenuItem.setItemLabel("Hide #{app_name}\tCtrl+H")
|
||||||
:wxMenuItem.setText(item, "Quit #{title}\tCtrl+Q")
|
|
||||||
else
|
menu
|
||||||
:wxMenu.delete(menu, item)
|
|> :wxMenu.findItem(@wx_id_exit)
|
||||||
end
|
|> :wxMenuItem.setItemLabel("Quit #{app_name}\tCtrl+Q")
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
defp os() do
|
defp os() do
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@ if Mix.target() == :app do
|
||||||
|
|
||||||
# https://github.com/erlang/otp/blob/OTP-24.1.2/lib/wx/include/wx.hrl#L1314
|
# https://github.com/erlang/otp/blob/OTP-24.1.2/lib/wx/include/wx.hrl#L1314
|
||||||
@wx_id_exit 5006
|
@wx_id_exit 5006
|
||||||
|
@wx_id_osx_hide 5250
|
||||||
|
|
||||||
def start_link(_) do
|
def start_link(_) do
|
||||||
{:wx_ref, _, _, pid} = :wx_object.start_link(__MODULE__, [], [])
|
{:wx_ref, _, _, pid} = :wx_object.start_link(__MODULE__, [], [])
|
||||||
|
|
@ -30,7 +31,7 @@ if Mix.target() == :app do
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
def init(_) do
|
def init(_) do
|
||||||
title = "Livebook"
|
app_name = "Livebook"
|
||||||
|
|
||||||
true = Process.register(self(), __MODULE__)
|
true = Process.register(self(), __MODULE__)
|
||||||
os = os()
|
os = os()
|
||||||
|
|
@ -39,14 +40,14 @@ if Mix.target() == :app do
|
||||||
size = if os == :macos, do: {0, 0}, else: {100, 100}
|
size = if os == :macos, do: {0, 0}, else: {100, 100}
|
||||||
|
|
||||||
wx = :wx.new()
|
wx = :wx.new()
|
||||||
frame = :wxFrame.new(wx, -1, title, size: size)
|
frame = :wxFrame.new(wx, -1, app_name, size: size)
|
||||||
|
|
||||||
if os == :macos do
|
if os == :macos do
|
||||||
fixup_macos_menubar(frame, title)
|
fixup_macos_menubar(frame, app_name)
|
||||||
end
|
end
|
||||||
|
|
||||||
:wxFrame.show(frame)
|
:wxFrame.show(frame)
|
||||||
:wxFrame.connect(frame, :command_menu_selected)
|
:wxFrame.connect(frame, :command_menu_selected, skip: true)
|
||||||
:wxFrame.connect(frame, :close_window, skip: true)
|
:wxFrame.connect(frame, :close_window, skip: true)
|
||||||
|
|
||||||
case os do
|
case os do
|
||||||
|
|
@ -115,21 +116,21 @@ if Mix.target() == :app do
|
||||||
{:noreply, state}
|
{:noreply, state}
|
||||||
end
|
end
|
||||||
|
|
||||||
defp fixup_macos_menubar(frame, title) do
|
# 1. WxeApp attaches event handler to "Quit" menu item that does nothing (to not accidentally bring
|
||||||
|
# down the VM). Let's create a fresh menu bar without that caveat.
|
||||||
|
# 2. Fix app name
|
||||||
|
defp fixup_macos_menubar(frame, app_name) do
|
||||||
menubar = :wxMenuBar.new()
|
menubar = :wxMenuBar.new()
|
||||||
:wxFrame.setMenuBar(frame, menubar)
|
:wxFrame.setMenuBar(frame, menubar)
|
||||||
|
|
||||||
# App Menu
|
|
||||||
menu = :wxMenuBar.oSXGetAppleMenu(menubar)
|
menu = :wxMenuBar.oSXGetAppleMenu(menubar)
|
||||||
|
|
||||||
# Remove all items except for quit
|
menu
|
||||||
for item <- :wxMenu.getMenuItems(menu) do
|
|> :wxMenu.findItem(@wx_id_osx_hide)
|
||||||
if :wxMenuItem.getId(item) == @wx_id_exit do
|
|> :wxMenuItem.setItemLabel("Hide #{app_name}\tCtrl+H")
|
||||||
:wxMenuItem.setText(item, "Quit #{title}\tCtrl+Q")
|
|
||||||
else
|
menu
|
||||||
:wxMenu.delete(menu, item)
|
|> :wxMenu.findItem(@wx_id_exit)
|
||||||
end
|
|> :wxMenuItem.setItemLabel("Quit #{app_name}\tCtrl+Q")
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
defp os() do
|
defp os() do
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue