Skip to content

Commit

Permalink
Merge pull request #2781 from rmartin16/log-errors-in-callback
Browse files Browse the repository at this point in the history
Show error dialog if WebView fails to load
  • Loading branch information
freakboy3742 authored Aug 23, 2024
2 parents 5adb61a + 4ae9b7e commit 0a14beb
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
1 change: 1 addition & 0 deletions changes/2779.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The WebView and MapView widgets now log an error if initialization fails.
13 changes: 10 additions & 3 deletions winforms/src/toga_winforms/widgets/mapview.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class MapView(Widget):
def create(self):
self.native = WebView2()
# WebView has a 2 phase startup; the widget needs to be initialized,
# the the content needs to be loaded. We can't actually use the widget
# the content needs to be loaded. We can't actually use the widget
# until the content is fully loaded.
self.native.CoreWebView2InitializationCompleted += WeakrefCallable(
self.winforms_initialization_completed
Expand Down Expand Up @@ -141,11 +141,18 @@ def winforms_initialization_completed(self, sender, args):
WinForms.MessageBoxIcon.Error,
)
webbrowser.open(
"https://developer.microsoft.com/en-us/microsoft-edge/webview2/#download-section"
"https://developer.microsoft.com/en-us/microsoft-edge/webview2/#download"
)

else: # pragma: nocover
raise RuntimeError(args.InitializationException)
WinForms.MessageBox.Show(
"A critical error has occurred and functionality may be impaired.\n\n"
"The WebView2 initialization failed with an exception:\n\n"
f"{args.InitializationException}",
"Error",
WinForms.MessageBoxButtons.OK,
WinForms.MessageBoxIcon.Error,
)

def winforms_navigation_completed(self, sender, args):
# The Toga API allows you to invoke methods on the MapView as soon
Expand Down
4 changes: 2 additions & 2 deletions winforms/src/toga_winforms/widgets/slider.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ def create(self):

# Unlike Scroll, ValueChanged also fires when the value is changed
# programmatically, such as via the testbed probe.
self.native.ValueChanged += WeakrefCallable(self.winforms_value_chaned)
self.native.ValueChanged += WeakrefCallable(self.winforms_value_changed)
self.native.MouseDown += WeakrefCallable(self.winforms_mouse_down)
self.native.MouseUp += WeakrefCallable(self.winforms_mouse_up)

def winforms_value_chaned(self, sender, event):
def winforms_value_changed(self, sender, event):
self.on_change()

def winforms_mouse_down(self, sender, event):
Expand Down
13 changes: 11 additions & 2 deletions winforms/src/toga_winforms/widgets/webview.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,15 @@ def winforms_initialization_completed(self, sender, args):
self.default_user_agent = settings.UserAgent

debug = True
settings.AreBrowserAcceleratorKeysEnabled = debug
settings.AreDefaultContextMenusEnabled = debug
settings.AreDefaultScriptDialogsEnabled = True
settings.AreDevToolsEnabled = debug
settings.IsBuiltInErrorPageEnabled = True
settings.IsScriptEnabled = True
settings.IsWebMessageEnabled = True
settings.IsStatusBarEnabled = debug
settings.IsSwipeNavigationEnabled = False
settings.IsZoomControlEnabled = True

for task in self.pending_tasks:
Expand All @@ -108,11 +110,18 @@ def winforms_initialization_completed(self, sender, args):
WinForms.MessageBoxIcon.Error,
)
webbrowser.open(
"https://developer.microsoft.com/en-us/microsoft-edge/webview2/#download-section"
"https://developer.microsoft.com/en-us/microsoft-edge/webview2/#download"
)

else: # pragma: nocover
raise RuntimeError(args.InitializationException)
WinForms.MessageBox.Show(
"A critical error has occurred and functionality may be impaired.\n\n"
"The WebView2 initialization failed with an exception:\n\n"
f"{args.InitializationException}",
"Error",
WinForms.MessageBoxButtons.OK,
WinForms.MessageBoxIcon.Error,
)

def winforms_navigation_completed(self, sender, args):
self.interface.on_webview_load()
Expand Down

0 comments on commit 0a14beb

Please sign in to comment.