Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

WebView improvements #290

Closed
wants to merge 5 commits into from

3 participants

@bratsche
Collaborator

This implements NavigatingToUrl and NavigatedToUrl events and LoadHtmlString() method.

@slluis slluis commented on the diff
Xwt/Xwt/WebView.cs
@@ -59,6 +73,11 @@ public WebView (string url)
Url = url;
}
+ public void LoadHtmlString (string html)
@slluis Owner
slluis added a note

Let's use LoadHtml as method name

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Xwt.WPF/Xwt.WPFBackend/WebViewBackend.cs
@@ -52,6 +52,63 @@ internal WebViewBackend (SWC.WebBrowser browser)
((SWC.WebBrowser)Widget).Navigate (url);
}
}
+
+ public void LoadHtmlString (string html)
+ {
+ ((SWC.WebBrowser)Widget).NavigateToString (html);
+ }
+
+ internal void OnLoadCompleted (object sender, System.Windows.Navigation.NavigationEventArgs e)
+ {
+ var url = e.Uri.AbsoluteUri;
+ EventSink.OnNavigatedToUrl (url);
@slluis Owner
slluis added a note

Call to event sink must be wrapped in a context.InvokeUserCode call. Same in OnNavigating.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@slluis slluis commented on the diff
Xwt/Xwt.Backends/IWebViewBackend.cs
((5 lines not shown))
}
public interface IWebViewEventSink : IWidgetEventSink
{
void OnLoaded ();
+ void OnNavigatingToUrl (string url);
@slluis Owner
slluis added a note

This method should return a "handled" bool. If true, navigation has to be canceled.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@slluis slluis commented on the diff
Xwt/Xwt/WebView.cs
@@ -81,6 +100,18 @@ protected virtual void OnLoaded (EventArgs e)
{
}
+ protected virtual void OnNavigatingToUrl (EventArgs e)
@slluis Owner
slluis added a note

Use WidgetEventArgs, which has a Handled property.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@sevoku
Collaborator

I think we have a working solution with complete navigation features now (#341, #356).

@sevoku sevoku closed this
@bratsche bratsche deleted the bratsche:webevents branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 14, 2014
  1. @bratsche
  2. @bratsche

    [WebView] LoadHtmlString ()

    bratsche authored
Commits on Feb 15, 2014
  1. @bratsche
Commits on Feb 16, 2014
  1. @bratsche
Commits on Feb 18, 2014
  1. @bratsche
This page is out of date. Refresh to see the latest.
View
2  TestApps/GtkOnWindowsTest/GtkOnWindowsTest.csproj
@@ -35,7 +35,7 @@
<Reference Include="pango-sharp, Version=2.4.0.0, Culture=neutral, PublicKeyToken=35e10195dab3c99f">
<SpecificVersion>False</SpecificVersion>
</Reference>
- <Reference Include="Mono.Posix, Version=2.0.0.0, Culture=neutral, PublicKeyToken=0738eb9f132ed756" />
+ <Reference Include="Mono.Posix" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
View
9 TestApps/Samples/Samples/WebViewSample.cs
@@ -34,10 +34,17 @@ public class WebViewSample : VBox
{
public WebViewSample ()
{
- var wb = new WebView ("http://www.xamarin.com");
+ var wb = new WebView ();
wb.WidthRequest = 600;
wb.HeightRequest = 900;
PackStart (wb, true);
+
+ wb.Url = "http://www.xamarin.com";
+
+ wb.NavigatingToUrl += (sender, e) => { Console.WriteLine ("Navigating..."); };
+ wb.NavigatedToUrl += (sender, e) => {
+ Console.WriteLine ("Navigated!");
+ };
}
}
}
View
5 Xwt.Gtk.Mac/WebViewBackend.cs
@@ -47,6 +47,11 @@ public override void Initialize()
Widget.Show ();
}
+ public void LoadHtmlString (string html)
+ {
+ view.MainFrame.LoadHtmlString (html, null);
+ }
+
public string Url {
get { return view.MainFrameUrl; }
set {
View
5 Xwt.Gtk.Windows/WebViewBackend.cs
@@ -67,6 +67,11 @@ public override void Initialize ()
Widget.Show();
}
+ public void LoadHtmlString (string html)
+ {
+ view.DocumentText = html;
+ }
+
public string Url {
get { return url; }
set {
View
39 Xwt.Mac/Xwt.Mac/WebViewBackend.cs
@@ -35,6 +35,10 @@
namespace Xwt.Mac
{
+ class XwtWebFrameDelegate : WebFrameLoadDelegate
+ {
+ }
+
public class WebViewBackend : ViewBackend<MonoMac.WebKit.WebView, IWebViewEventSink>, IWebViewBackend
{
public WebViewBackend ()
@@ -47,10 +51,15 @@ internal WebViewBackend (MacWebView macweb)
}
#region IWebViewBackend implementation
+ public void LoadHtmlString (string html)
+ {
+ ((MacWebView)ViewObject).MainFrame.LoadHtmlString (html, null);
+ }
+
public override void Initialize()
{
base.Initialize ();
- ViewObject = new MacWebView ();
+ ViewObject = new MacWebView (EventSink, ApplicationContext);
}
public string Url {
@@ -64,10 +73,38 @@ public override void Initialize()
class MacWebView : MonoMac.WebKit.WebView, IViewObject
{
+ IWebViewEventSink eventSink;
+ ApplicationContext context;
+ internal EventHandler navigatingToUrl;
+ internal EventHandler navigatedToUrl;
+
public ViewBackend Backend { get; set; }
public NSView View {
get { return this; }
}
+
+ public MacWebView (IWebViewEventSink eventSink, ApplicationContext context) {
+ this.context = context;
+ this.eventSink = eventSink;
+
+ this.StartedProvisionalLoad += (sender, e) => {
+ var nsurl = e.ForFrame.DataSource.Request.MainDocumentURL;
+ var url = nsurl != null ? nsurl.ToString () : null;
+
+ context.InvokeUserCode (delegate {
+ eventSink.OnNavigatingToUrl (url);
+ });
+ };
+
+ this.FinishedLoad += (sender, e) => {
+ var nsurl = e.ForFrame.DataSource.Request.MainDocumentURL;
+ var url = nsurl != null ? nsurl.ToString () : null;
+
+ context.InvokeUserCode (delegate {
+ eventSink.OnNavigatedToUrl (url);
+ });
+ };
+ }
}
}
View
61 Xwt.WPF/Xwt.WPFBackend/WebViewBackend.cs
@@ -52,6 +52,67 @@ internal WebViewBackend (SWC.WebBrowser browser)
((SWC.WebBrowser)Widget).Navigate (url);
}
}
+
+ public void LoadHtmlString (string html)
+ {
+ ((SWC.WebBrowser)Widget).NavigateToString (html);
+ }
+
+ internal void OnLoadCompleted (object sender, System.Windows.Navigation.NavigationEventArgs e)
+ {
+ var url = e.Uri.AbsoluteUri;
+ Context.InvokeUserCode (delegate {
+ EventSink.OnNavigatedToUrl (url);
+ });
+ }
+
+ internal void OnNavigating (object sender, System.Windows.Navigation.NavigatingCancelEventArgs e)
+ {
+ var url = e.Uri.AbsoluteUri;
+ Context.InvokeUserCode (delegate {
+ EventSink.OnNavigatingToUrl (url);
+ });
+ }
+
+ protected new IWebViewEventSink EventSink {
+ get { return (IWebViewEventSink)base.EventSink; }
+ }
+
+ public override void DisableEvent (object eventId)
+ {
+ base.DisableEvent (eventId);
+
+ if (eventId is WebViewEvent)
+ {
+ switch ((WebViewEvent)eventId)
+ {
+ case WebViewEvent.NavigatedToUrl:
+ ((SWC.WebBrowser)Widget).LoadCompleted -= OnLoadCompleted;
+ break;
+ case WebViewEvent.NavigatingToUrl:
+ ((SWC.WebBrowser)Widget).Navigating -= OnNavigating;
+ break;
+ }
+ }
+ }
+
+ public override void EnableEvent (object eventId)
+ {
+ base.EnableEvent (eventId);
+
+ if (eventId is WebViewEvent)
+ {
+ switch ((WebViewEvent)eventId)
+ {
+ case WebViewEvent.NavigatedToUrl:
+ ((SWC.WebBrowser)Widget).LoadCompleted += OnLoadCompleted;
+ break;
+ case WebViewEvent.NavigatingToUrl:
+ ((SWC.WebBrowser)Widget).Navigating += OnNavigating;
+ break;
+ }
+ }
+ }
}
}
View
2  Xwt.sln
@@ -573,7 +573,7 @@ Global
{C1DF0C90-0C4C-4AB5-A45C-A1805587A844} = {83D74DDF-581E-4E2A-AE02-F4047A5B96C7}
EndGlobalSection
GlobalSection(MonoDevelopProperties) = preSolution
- StartupItem = TestApps\GtkOnWindowsTest\GtkOnWindowsTest.csproj
+ StartupItem = TestApps\MacTest\MacTest.csproj
Policies = $0
$0.DotNetNamingPolicy = $1
$1.DirectoryNamespaceAssociation = None
View
7 Xwt/Xwt.Backends/IWebViewBackend.cs
@@ -30,15 +30,20 @@ namespace Xwt.Backends
public interface IWebViewBackend : IWidgetBackend
{
string Url { get; set; }
+ void LoadHtmlString (string html);
}
public interface IWebViewEventSink : IWidgetEventSink
{
void OnLoaded ();
+ void OnNavigatingToUrl (string url);
@slluis Owner
slluis added a note

This method should return a "handled" bool. If true, navigation has to be canceled.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ void OnNavigatedToUrl (string url);
}
public enum WebViewEvent
{
- Loaded = 1
+ Loaded = 1,
+ NavigatingToUrl = 2,
+ NavigatedToUrl = 3
}
}
View
53 Xwt/Xwt/WebView.cs
@@ -35,6 +35,8 @@ namespace Xwt
public class WebView : Widget
{
EventHandler loaded;
+ EventHandler navigatingToUrl;
+ EventHandler navigatedToUrl;
string url;
protected new class WidgetBackendHost : Widget.WidgetBackendHost, IWebViewEventSink
@@ -43,11 +45,23 @@ public void OnLoaded ()
{
((WebView)Parent).OnLoaded (EventArgs.Empty);
}
+
+ public void OnNavigatingToUrl (string url)
+ {
+ ((WebView)Parent).OnNavigatingToUrl (EventArgs.Empty);
+ }
+
+ public void OnNavigatedToUrl (string url)
+ {
+ ((WebView)Parent).OnNavigatedToUrl (EventArgs.Empty);
+ }
}
static WebView ()
{
MapEvent (WebViewEvent.Loaded, typeof(WebView), "OnLoaded");
+ MapEvent (WebViewEvent.NavigatingToUrl, typeof(WebView), "OnNavigatingToUrl");
+ MapEvent (WebViewEvent.NavigatedToUrl, typeof(WebView), "OnNavigatedToUrl");
}
public WebView ()
@@ -59,6 +73,11 @@ public WebView (string url)
Url = url;
}
+ public void LoadHtmlString (string html)
@slluis Owner
slluis added a note

Let's use LoadHtml as method name

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ {
+ Backend.LoadHtmlString (html);
+ }
+
protected override Xwt.Backends.BackendHost CreateBackendHost ()
{
return new WidgetBackendHost ();
@@ -81,6 +100,18 @@ protected virtual void OnLoaded (EventArgs e)
{
}
+ protected virtual void OnNavigatingToUrl (EventArgs e)
@slluis Owner
slluis added a note

Use WidgetEventArgs, which has a Handled property.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ {
+ if (navigatingToUrl != null)
+ navigatingToUrl (this, e);
+ }
+
+ protected virtual void OnNavigatedToUrl (EventArgs e)
+ {
+ if (navigatedToUrl != null)
+ navigatedToUrl (this, e);
+ }
+
public event EventHandler Loaded {
add {
BackendHost.OnBeforeEventAdd (WebViewEvent.Loaded, loaded);
@@ -92,6 +123,28 @@ protected virtual void OnLoaded (EventArgs e)
BackendHost.OnAfterEventRemove (WebViewEvent.Loaded, loaded);
}
}
+
+ public event EventHandler NavigatingToUrl {
+ add {
+ BackendHost.OnBeforeEventAdd (WebViewEvent.NavigatingToUrl, navigatingToUrl);
+ navigatingToUrl += value;
+ }
+ remove {
+ navigatingToUrl -= value;
+ BackendHost.OnAfterEventRemove (WebViewEvent.NavigatingToUrl, navigatingToUrl);
+ }
+ }
+
+ public event EventHandler NavigatedToUrl {
+ add {
+ BackendHost.OnBeforeEventAdd (WebViewEvent.NavigatedToUrl, navigatedToUrl);
+ navigatedToUrl += value;
+ }
+ remove {
+ navigatedToUrl -= value;
+ BackendHost.OnAfterEventRemove (WebViewEvent.NavigatedToUrl, navigatedToUrl);
+ }
+ }
}
}
Something went wrong with that request. Please try again.