Skip to content

Commit bdc5c31

Browse files
committed
Glue up enough installer to launch a desktop window to the app
Bootstrapping needs to be documented, and will be easier once the installer is included in a published release of elixir-desktop. For now, add the {:desktop, path: to_dev_directory} dependency manually and then run `mix desktop.install` in the target project.
1 parent 8188cc2 commit bdc5c31

1 file changed

Lines changed: 82 additions & 7 deletions

File tree

lib/mix/tasks/desktop.install.ex

Lines changed: 82 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,28 +43,103 @@ defmodule Mix.Tasks.Desktop.Install do
4343
def igniter(igniter) do
4444
app = Igniter.Project.Application.app_name(igniter)
4545
endpoint = Igniter.Libs.Phoenix.web_module_name(igniter, "Endpoint")
46+
menu = Igniter.Project.Module.module_name(igniter, "Menu")
47+
menubar = Igniter.Project.Module.module_name(igniter, "MenuBar")
48+
gettext = Igniter.Libs.Phoenix.web_module_name(igniter, "Gettext")
49+
main_window = Igniter.Project.Module.module_name(igniter, MainWindow)
4650

4751
igniter
4852
|> Igniter.compose_task("igniter.add", ["desktop"])
53+
|> Igniter.Project.Module.create_module(menubar, """
54+
@moduledoc """
55+
Menu bar that is shown as part of the main window on Windows/Linux. In
56+
MacOS this menu bar appears at the very top of the screen.
57+
\"""
58+
use Gettext, backend: #{gettext}
59+
use Desktop.Menu
60+
alias Desktop.Window
61+
62+
def render(assigns) do
63+
~H"""
64+
<menubar>
65+
<menu label={gettext "File"}>
66+
<item onclick="quit">{gettext "Quit"}</item>
67+
</menu>
68+
<menu label={gettext "Extra"}>
69+
<item onclick="observer">{gettext "Show Observer"}</item>
70+
<item onclick="browser">{gettext "Open Browser"}</item>
71+
</menu>
72+
</menubar>
73+
\"""
74+
end
75+
76+
def handle_event("quit", menu) do
77+
Window.quit()
78+
{:noreply, menu}
79+
end
80+
81+
def handle_event("observer", menu) do
82+
:observer.start()
83+
{:noreply, menu}
84+
end
85+
86+
def handle_event("browser", menu) do
87+
Window.prepare_url(#{endpoint}.url())
88+
|> :wx_misc.launchDefaultBrowser()
89+
90+
{:noreply, menu}
91+
end
92+
93+
def mount(menu) do
94+
{:ok, menu}
95+
end
96+
""")
97+
|> Igniter.Project.Module.create_module(menu, """
98+
@moduledoc """
99+
Menu that is shown when a user clicks on the taskbar icon of the #{app}
100+
\"""
101+
use Gettext, backend: #{gettext}
102+
use Desktop.Menu
103+
104+
def render(assigns) do
105+
~H"""
106+
<menu>
107+
<item onclick="edit">{gettext "Open"}</item>
108+
<hr/>
109+
<item onclick="quit">{gettext "Quit"}</item>
110+
</menu>
111+
\"""
112+
end
113+
114+
def handle_event(command, menu) do
115+
case command do
116+
<<"quit">> -> Desktop.Window.quit()
117+
<<"edit">> -> Desktop.Window.show(#{main_window})
118+
end
119+
120+
{:noreply, menu}
121+
end
122+
123+
def mount(menu) do
124+
{:ok, menu}
125+
end
126+
""")
49127
|> Igniter.Project.Application.add_new_child(
50128
{
51129
Desktop.Window,
52130
[
53131
app: app,
54132
id: Igniter.Project.Module.module_name(igniter, MainWindow),
55-
# FIXME: configurable
56133
title: to_string(app),
57134
size: {600, 500},
58-
icon: "icon.png",
59-
menubar: Igniter.Project.Module.module_name(igniter, MenuBar),
60-
icon_menu: Igniter.Project.Module.module_name(igniter, Menu),
61-
url: {endpoint, :url, []}
135+
# icon: "icon.png", # TODO: ship an example taskbar icon here
136+
menubar: menubar,
137+
icon_menu: menu,
138+
url: fn -> apply(endpoint, :url, []) end
62139
]
63140
},
64141
after: [endpoint]
65142
)
66-
67143
# TODO: detect and warn if the project assumes pgsql
68-
# TODO: create MyApp.MenuBar
69144
end
70145
end

0 commit comments

Comments
 (0)