Skip to content

Commit 3c6fe53

Browse files
committed
Added load_url(pid, url)
1 parent a846e3d commit 3c6fe53

2 files changed

Lines changed: 31 additions & 0 deletions

File tree

lib/desktop/fallback.ex

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,14 @@ defmodule Desktop.Fallback do
123123
call(:wxWebView, :getCurrentURL, [webview])
124124
end
125125

126+
def webview_load(%Desktop.Window{webview: nil}, url) do
127+
OS.launch_default_browser(url)
128+
end
129+
130+
def webview_load(%Desktop.Window{webview: webview}, url) do
131+
call(:wxWebView, :loadURL, [webview, url])
132+
end
133+
126134
def webview_show(%Desktop.Window{webview: nil}, url, _) do
127135
OS.launch_default_browser(url)
128136
end

lib/desktop/window.ex

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,22 @@ defmodule Desktop.Window do
234234
GenServer.call(pid, :url)
235235
end
236236

237+
@doc """
238+
Loads the given url into the Window.
239+
240+
* `pid` - The pid or atom of the Window
241+
* `url` - The url to load. If none is provided, the last url will be used.
242+
243+
## Examples
244+
245+
iex> Desktop.Window.load_url(pid, "http://localhost:1234/main")
246+
:ok
247+
248+
"""
249+
def load_url(pid, url) do
250+
GenServer.cast(pid, {:load_url, url})
251+
end
252+
237253
@doc """
238254
Show the Window if not visible with the given url.
239255
@@ -623,6 +639,13 @@ defmodule Desktop.Window do
623639
{:noreply, %Window{ui | last_url: new_url}}
624640
end
625641

642+
def handle_cast({:load_url, url}, ui = %Window{home_url: home, last_url: last}) do
643+
new_url = prepare_url(url || last || home)
644+
Logger.info("Loading #{new_url}")
645+
Fallback.webview_load(ui, new_url)
646+
{:noreply, %Window{ui | last_url: new_url}}
647+
end
648+
626649
def handle_cast(:hide, ui = %Window{frame: frame}) do
627650
if frame do
628651
:wxWindow.hide(frame)

0 commit comments

Comments
 (0)