fix: shutdown moved to standard functionality
This commit is contained in:
@@ -6,16 +6,24 @@ defmodule BDS.Desktop.MainWindow do
|
||||
alias Desktop.Window
|
||||
|
||||
@window_id __MODULE__
|
||||
@server_name BDS.Desktop.MainWindow.Watcher
|
||||
@persist_interval_ms 1_000
|
||||
@default_size {1280, 780}
|
||||
@default_min_size {800, 600}
|
||||
@state_file "window-state.json"
|
||||
|
||||
def start_link(_opts) do
|
||||
GenServer.start_link(__MODULE__, :ok)
|
||||
GenServer.start_link(__MODULE__, :ok, name: @server_name)
|
||||
end
|
||||
|
||||
def window_id, do: @window_id
|
||||
def server_name, do: @server_name
|
||||
|
||||
def persist_now(timeout \\ 100) do
|
||||
GenServer.call(@server_name, :persist_bounds_now, timeout)
|
||||
catch
|
||||
:exit, _reason -> :ok
|
||||
end
|
||||
|
||||
def window_options(extra_opts \\ []) do
|
||||
desktop_config = Application.get_env(:bds, :desktop, [])
|
||||
@@ -90,6 +98,11 @@ defmodule BDS.Desktop.MainWindow do
|
||||
{:noreply, %{state | last_bounds: next_bounds}}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_call(:persist_bounds_now, _from, state) do
|
||||
{:reply, :ok, persist_current_bounds(state)}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def terminate(_reason, %{last_bounds: last_bounds}) do
|
||||
if bounds = last_bounds do
|
||||
@@ -103,6 +116,16 @@ defmodule BDS.Desktop.MainWindow do
|
||||
Process.send_after(self(), :persist_bounds, @persist_interval_ms)
|
||||
end
|
||||
|
||||
defp persist_current_bounds(%{frame: frame} = state) do
|
||||
next_bounds = current_bounds(frame) || state.last_bounds
|
||||
|
||||
if next_bounds do
|
||||
_ = persist_bounds(next_bounds)
|
||||
end
|
||||
|
||||
%{state | last_bounds: next_bounds}
|
||||
end
|
||||
|
||||
defp apply_restored_bounds(frame) do
|
||||
case restore_bounds() do
|
||||
%{x: x, y: y, width: width, height: height} ->
|
||||
@@ -127,23 +150,30 @@ defmodule BDS.Desktop.MainWindow do
|
||||
defp current_bounds(nil), do: nil
|
||||
|
||||
defp current_bounds(frame) do
|
||||
with_wx_env(fn ->
|
||||
cond do
|
||||
not :wxWindow.isShown(frame) ->
|
||||
nil
|
||||
try do
|
||||
with_wx_env(fn ->
|
||||
cond do
|
||||
not :wxWindow.isShown(frame) ->
|
||||
nil
|
||||
|
||||
:wxTopLevelWindow.isFullScreen(frame) ->
|
||||
nil
|
||||
:wxTopLevelWindow.isFullScreen(frame) ->
|
||||
nil
|
||||
|
||||
:wxTopLevelWindow.isMaximized(frame) ->
|
||||
nil
|
||||
:wxTopLevelWindow.isMaximized(frame) ->
|
||||
nil
|
||||
|
||||
true ->
|
||||
{x, y} = :wxWindow.getPosition(frame)
|
||||
{width, height} = :wxWindow.getSize(frame)
|
||||
%{x: x, y: y, width: width, height: height}
|
||||
end
|
||||
end)
|
||||
true ->
|
||||
{x, y} = :wxWindow.getPosition(frame)
|
||||
{width, height} = :wxWindow.getSize(frame)
|
||||
%{x: x, y: y, width: width, height: height}
|
||||
end
|
||||
end)
|
||||
rescue
|
||||
ErlangError -> nil
|
||||
FunctionClauseError -> nil
|
||||
catch
|
||||
:exit, _reason -> nil
|
||||
end
|
||||
end
|
||||
|
||||
defp with_wx_env(fun) do
|
||||
|
||||
@@ -4,8 +4,6 @@ defmodule BDS.Desktop.Shutdown do
|
||||
alias BDS.Desktop.MainWindow
|
||||
alias Desktop.Window
|
||||
|
||||
@stop_delay_ms 100
|
||||
|
||||
@spec install_handlers(term()) :: :ok
|
||||
def install_handlers(frame) do
|
||||
:wx.set_env(Desktop.Env.wx_env())
|
||||
@@ -17,13 +15,6 @@ defmodule BDS.Desktop.Shutdown do
|
||||
userData: self()
|
||||
)
|
||||
|
||||
_ = :wxFrame.disconnect(frame, :command_menu_selected, id: Desktop.Wx.wxID_EXIT())
|
||||
|
||||
:wxFrame.connect(frame, :command_menu_selected,
|
||||
id: Desktop.Wx.wxID_EXIT(),
|
||||
callback: &__MODULE__.command_menu_selected/2
|
||||
)
|
||||
|
||||
:ok
|
||||
rescue
|
||||
_error -> :ok
|
||||
@@ -51,42 +42,16 @@ defmodule BDS.Desktop.Shutdown do
|
||||
request_quit()
|
||||
end
|
||||
|
||||
@spec command_menu_selected(tuple(), term()) :: :ok
|
||||
def command_menu_selected(_event, _command_event) do
|
||||
request_quit()
|
||||
end
|
||||
|
||||
defp start_shutdown_task do
|
||||
Task.start(fn ->
|
||||
close_main_window()
|
||||
Process.sleep(@stop_delay_ms)
|
||||
System.stop(0)
|
||||
MainWindow.persist_now()
|
||||
quit_module().quit()
|
||||
end)
|
||||
|
||||
:ok
|
||||
end
|
||||
|
||||
defp close_main_window do
|
||||
with frame when not is_nil(frame) <- main_frame() do
|
||||
:wx.set_env(Desktop.Env.wx_env())
|
||||
|
||||
if :wxWindow.isShown(frame) do
|
||||
:wxWindow.hide(frame)
|
||||
end
|
||||
|
||||
:wxWindow.destroy(frame)
|
||||
else
|
||||
_other -> :ok
|
||||
end
|
||||
rescue
|
||||
_error -> :ok
|
||||
catch
|
||||
:exit, _reason -> :ok
|
||||
end
|
||||
|
||||
defp main_frame do
|
||||
Window.frame(MainWindow.window_id())
|
||||
catch
|
||||
:exit, _reason -> nil
|
||||
defp quit_module do
|
||||
Application.get_env(:bds, :desktop_window_quit_module, Window)
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user