|
1 | 1 | if Code.ensure_loaded?(Igniter.Mix.Task) do |
2 | | -defmodule Mix.Tasks.Desktop.Install do |
3 | | - @shortdoc "Add Elixir Desktop support to an existing project" |
4 | | - |
5 | | - @moduledoc """ |
6 | | - #{@shortdoc} |
7 | | -
|
8 | | - This mix task adds the minimal glue to launch an Elixir Desktop |
9 | | - main window for your application. |
10 | | -
|
11 | | - To use this task, you'll either need to install Igniter globally, or |
12 | | - manually add the desktop dependency to your project's mix.exs: |
13 | | -
|
14 | | - ``` |
15 | | - {:desktop, "~> 1.0"} |
16 | | - ``` |
17 | | -
|
18 | | - ## Examples |
19 | | -
|
20 | | - Add desktop support to a project: |
21 | | -
|
22 | | - ```bash |
23 | | - mix desktop.install |
24 | | - ``` |
25 | | -
|
26 | | - Create a new project with desktop support: |
27 | | -
|
28 | | - ```bash |
29 | | - mix archive.install hex igniter_new |
| 2 | + defmodule Mix.Tasks.Desktop.Install do |
| 3 | + @shortdoc "Add Elixir Desktop support to an existing project" |
| 4 | + |
| 5 | + @moduledoc """ |
| 6 | + #{@shortdoc} |
| 7 | +
|
| 8 | + This mix task adds the minimal glue to launch an Elixir Desktop |
| 9 | + main window for your application. |
| 10 | +
|
| 11 | + To use this task, you'll either need to install Igniter globally, or |
| 12 | + manually add the desktop dependency to your project's mix.exs: |
| 13 | +
|
| 14 | + ``` |
| 15 | + {:desktop, "~> 1.0"} |
| 16 | + ``` |
| 17 | +
|
| 18 | + ## Examples |
| 19 | +
|
| 20 | + Add desktop support to a project: |
| 21 | +
|
| 22 | + ```bash |
| 23 | + mix desktop.install |
| 24 | + ``` |
| 25 | +
|
| 26 | + Create a new project with desktop support: |
| 27 | +
|
| 28 | + ```bash |
| 29 | + mix archive.install hex igniter_new |
| 30 | +
|
| 31 | + mix igniter.new my_app --install desktop --with phx.new |
| 32 | + ``` |
| 33 | + """ |
| 34 | + |
| 35 | + use Igniter.Mix.Task |
| 36 | + |
| 37 | + @impl Igniter.Mix.Task |
| 38 | + def info(_argv, _composing_task) do |
| 39 | + %Igniter.Mix.Task.Info{ |
| 40 | + # Groups allow for overlapping arguments for tasks by the same author |
| 41 | + # See the generators guide for more. |
| 42 | + group: :desktop |
| 43 | + } |
| 44 | + end |
| 45 | + |
| 46 | + @impl Igniter.Mix.Task |
| 47 | + def igniter(igniter) do |
| 48 | + app = Igniter.Project.Application.app_name(igniter) |
| 49 | + endpoint = Igniter.Libs.Phoenix.web_module_name(igniter, "Endpoint") |
| 50 | + menu = Igniter.Project.Module.module_name(igniter, "Menu") |
| 51 | + menubar = Igniter.Project.Module.module_name(igniter, "MenuBar") |
| 52 | + gettext = Igniter.Libs.Phoenix.web_module_name(igniter, "Gettext") |
| 53 | + main_window = Igniter.Project.Module.module_name(igniter, MainWindow) |
| 54 | + |
| 55 | + igniter |
| 56 | + |> Igniter.compose_task("igniter.add", ["desktop"]) |
| 57 | + |> add_menu_bar(menubar, gettext, endpoint) |
| 58 | + |> add_menu(menu, gettext, app, main_window) |
| 59 | + |> Igniter.Project.Application.add_new_child( |
| 60 | + { |
| 61 | + Desktop.Window, |
| 62 | + {:code, |
| 63 | + quote do |
| 64 | + [ |
| 65 | + app: unquote(app), |
| 66 | + id: unquote(Igniter.Project.Module.module_name(igniter, MainWindow)), |
| 67 | + title: unquote(to_string(app)), |
| 68 | + size: {600, 500}, |
| 69 | + # icon: "icon.png", # TODO: ship an example taskbar icon here |
| 70 | + menubar: unquote(menubar), |
| 71 | + icon_menu: unquote(menu), |
| 72 | + url: &unquote(endpoint).url/0 |
| 73 | + ] |
| 74 | + end} |
| 75 | + }, |
| 76 | + after: [endpoint] |
| 77 | + ) |
| 78 | + |
| 79 | + # TODO: Add runtime_tools observer if available, or optionall add the dependency |
| 80 | + end |
| 81 | + |
| 82 | + defp add_menu_bar(igniter, menubar, gettext, endpoint) do |
| 83 | + Igniter.Project.Module.find_and_update_or_create_module( |
| 84 | + igniter, |
| 85 | + menubar, |
| 86 | + """ |
| 87 | + @moduledoc \""" |
| 88 | + Menu bar that is shown as part of the main window on Windows/Linux. In |
| 89 | + MacOS this menu bar appears at the very top of the screen. |
| 90 | + \""" |
| 91 | + use Gettext, backend: #{inspect(gettext)} |
| 92 | + use Desktop.Menu |
| 93 | + alias Desktop.Window |
| 94 | +
|
| 95 | + @impl true |
| 96 | + def render(assigns) do |
| 97 | + ~H\""" |
| 98 | + <menubar> |
| 99 | + <menu label={gettext "File"}> |
| 100 | + <item onclick="quit">{gettext "Quit"}</item> |
| 101 | + </menu> |
| 102 | + <menu label={gettext "Extra"}> |
| 103 | + <item onclick="browser">{gettext "Open Browser"}</item> |
| 104 | + </menu> |
| 105 | + </menubar> |
| 106 | + \""" |
| 107 | + end |
30 | 108 |
|
31 | | - mix igniter.new my_app --install desktop --with phx.new |
32 | | - ``` |
33 | | - """ |
| 109 | + @impl true |
| 110 | + def handle_event("quit", menu) do |
| 111 | + Window.quit() |
| 112 | + {:noreply, menu} |
| 113 | + end |
34 | 114 |
|
35 | | - use Igniter.Mix.Task |
| 115 | + def handle_event("browser", menu) do |
| 116 | + Window.prepare_url(#{inspect(endpoint)}.url()) |
| 117 | + |> :wx_misc.launchDefaultBrowser() |
36 | 118 |
|
37 | | - @impl Igniter.Mix.Task |
38 | | - def info(_argv, _composing_task) do |
39 | | - %Igniter.Mix.Task.Info{ |
40 | | - # Groups allow for overlapping arguments for tasks by the same author |
41 | | - # See the generators guide for more. |
42 | | - group: :desktop |
43 | | - } |
44 | | - end |
| 119 | + {:noreply, menu} |
| 120 | + end |
45 | 121 |
|
46 | | - @impl Igniter.Mix.Task |
47 | | - def igniter(igniter) do |
48 | | - app = Igniter.Project.Application.app_name(igniter) |
49 | | - endpoint = Igniter.Libs.Phoenix.web_module_name(igniter, "Endpoint") |
50 | | - menu = Igniter.Project.Module.module_name(igniter, "Menu") |
51 | | - menubar = Igniter.Project.Module.module_name(igniter, "MenuBar") |
52 | | - gettext = Igniter.Libs.Phoenix.web_module_name(igniter, "Gettext") |
53 | | - main_window = Igniter.Project.Module.module_name(igniter, MainWindow) |
54 | | - |
55 | | - igniter |
56 | | - |> Igniter.compose_task("igniter.add", ["desktop"]) |
57 | | - |> add_menu_bar(menubar, gettext, endpoint) |
58 | | - |> add_menu(menu, gettext, app, main_window) |
59 | | - |> Igniter.Project.Application.add_new_child( |
60 | | - { |
61 | | - Desktop.Window, |
62 | | - {:code, |
63 | | - quote do |
64 | | - [ |
65 | | - app: unquote(app), |
66 | | - id: unquote(Igniter.Project.Module.module_name(igniter, MainWindow)), |
67 | | - title: unquote(to_string(app)), |
68 | | - size: {600, 500}, |
69 | | - # icon: "icon.png", # TODO: ship an example taskbar icon here |
70 | | - menubar: unquote(menubar), |
71 | | - icon_menu: unquote(menu), |
72 | | - url: &unquote(endpoint).url/0 |
73 | | - ] |
74 | | - end} |
75 | | - }, |
76 | | - after: [endpoint] |
77 | | - ) |
78 | | - |
79 | | - # TODO: Add runtime_tools observer if available, or optionall add the dependency |
80 | | - end |
| 122 | + @impl true |
| 123 | + def handle_info(_, menu) do |
| 124 | + {:noreply, menu} |
| 125 | + end |
81 | 126 |
|
82 | | - defp add_menu_bar(igniter, menubar, gettext, endpoint) do |
83 | | - Igniter.Project.Module.find_and_update_or_create_module( |
84 | | - igniter, |
85 | | - menubar, |
86 | | - """ |
87 | | - @moduledoc \""" |
88 | | - Menu bar that is shown as part of the main window on Windows/Linux. In |
89 | | - MacOS this menu bar appears at the very top of the screen. |
90 | | - \""" |
91 | | - use Gettext, backend: #{inspect(gettext)} |
92 | | - use Desktop.Menu |
93 | | - alias Desktop.Window |
94 | | -
|
95 | | - @impl true |
96 | | - def render(assigns) do |
97 | | - ~H\""" |
98 | | - <menubar> |
99 | | - <menu label={gettext "File"}> |
100 | | - <item onclick="quit">{gettext "Quit"}</item> |
101 | | - </menu> |
102 | | - <menu label={gettext "Extra"}> |
103 | | - <item onclick="browser">{gettext "Open Browser"}</item> |
104 | | - </menu> |
105 | | - </menubar> |
| 127 | + @impl true |
| 128 | + def mount(menu) do |
| 129 | + {:ok, menu} |
| 130 | + end |
| 131 | + """, |
| 132 | + fn zipper -> {:ok, zipper} end |
| 133 | + ) |
| 134 | + end |
| 135 | + |
| 136 | + defp add_menu(igniter, menu, gettext, app, main_window) do |
| 137 | + Igniter.Project.Module.find_and_update_or_create_module( |
| 138 | + igniter, |
| 139 | + menu, |
| 140 | + """ |
| 141 | + @moduledoc \""" |
| 142 | + Menu that is shown when a user clicks on the taskbar icon of the #{app} |
106 | 143 | \""" |
107 | | - end |
108 | | -
|
109 | | - @impl true |
110 | | - def handle_event("quit", menu) do |
111 | | - Window.quit() |
112 | | - {:noreply, menu} |
113 | | - end |
114 | | -
|
115 | | - def handle_event("browser", menu) do |
116 | | - Window.prepare_url(#{inspect(endpoint)}.url()) |
117 | | - |> :wx_misc.launchDefaultBrowser() |
118 | | -
|
119 | | - {:noreply, menu} |
120 | | - end |
121 | | -
|
122 | | - @impl true |
123 | | - def handle_info(_, menu) do |
124 | | - {:noreply, menu} |
125 | | - end |
126 | | -
|
127 | | - @impl true |
128 | | - def mount(menu) do |
129 | | - {:ok, menu} |
130 | | - end |
131 | | - """, |
132 | | - fn zipper -> {:ok, zipper} end |
133 | | - ) |
134 | | - end |
| 144 | + use Gettext, backend: #{inspect(gettext)} |
| 145 | + use Desktop.Menu |
| 146 | +
|
| 147 | + @impl true |
| 148 | + def render(assigns) do |
| 149 | + ~H\""" |
| 150 | + <menu> |
| 151 | + <item onclick="edit">{gettext "Open"}</item> |
| 152 | + <hr/> |
| 153 | + <item onclick="quit">{gettext "Quit"}</item> |
| 154 | + </menu> |
| 155 | + \""" |
| 156 | + end |
135 | 157 |
|
136 | | - defp add_menu(igniter, menu, gettext, app, main_window) do |
137 | | - Igniter.Project.Module.find_and_update_or_create_module( |
138 | | - igniter, |
139 | | - menu, |
140 | | - """ |
141 | | - @moduledoc \""" |
142 | | - Menu that is shown when a user clicks on the taskbar icon of the #{app} |
143 | | - \""" |
144 | | - use Gettext, backend: #{inspect(gettext)} |
145 | | - use Desktop.Menu |
146 | | -
|
147 | | - @impl true |
148 | | - def render(assigns) do |
149 | | - ~H\""" |
150 | | - <menu> |
151 | | - <item onclick="edit">{gettext "Open"}</item> |
152 | | - <hr/> |
153 | | - <item onclick="quit">{gettext "Quit"}</item> |
154 | | - </menu> |
155 | | - \""" |
156 | | - end |
| 158 | + @impl true |
| 159 | + def handle_event(command, menu) do |
| 160 | + case command do |
| 161 | + <<"quit">> -> Desktop.Window.quit() |
| 162 | + <<"edit">> -> Desktop.Window.show(#{inspect(main_window)}) |
| 163 | + end |
| 164 | +
|
| 165 | + {:noreply, menu} |
| 166 | + end |
157 | 167 |
|
158 | | - @impl true |
159 | | - def handle_event(command, menu) do |
160 | | - case command do |
161 | | - <<"quit">> -> Desktop.Window.quit() |
162 | | - <<"edit">> -> Desktop.Window.show(#{inspect(main_window)}) |
| 168 | + @impl true |
| 169 | + def handle_info(_, menu) do |
| 170 | + {:noreply, menu} |
163 | 171 | end |
164 | 172 |
|
165 | | - {:noreply, menu} |
166 | | - end |
167 | | -
|
168 | | - @impl true |
169 | | - def handle_info(_, menu) do |
170 | | - {:noreply, menu} |
171 | | - end |
172 | | -
|
173 | | - @impl true |
174 | | - def mount(menu) do |
175 | | - {:ok, menu} |
176 | | - end |
177 | | - """, |
178 | | - fn zipper -> {:ok, zipper} end |
179 | | - ) |
| 173 | + @impl true |
| 174 | + def mount(menu) do |
| 175 | + {:ok, menu} |
| 176 | + end |
| 177 | + """, |
| 178 | + fn zipper -> {:ok, zipper} end |
| 179 | + ) |
| 180 | + end |
180 | 181 | end |
181 | 182 | end |
182 | | -end |
0 commit comments