Skip to content

Commit

Permalink
Implement navigation event callback handler on gtk
Browse files Browse the repository at this point in the history
  • Loading branch information
atlanticaccent committed Mar 9, 2022
1 parent 8a5df2f commit 0500e74
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions src/webview/webkitgtk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ use glib::signal::Inhibit;
use gtk::prelude::*;
use webkit2gtk::{
traits::*, UserContentInjectedFrames, UserScript, UserScriptInjectionTime, WebView,
WebViewBuilder,
WebViewBuilder, PolicyDecisionType, NavigationPolicyDecision,
};
use webkit2gtk_sys::{
webkit_get_major_version, webkit_get_micro_version, webkit_get_minor_version,
webkit_get_major_version, webkit_get_micro_version, webkit_get_minor_version, webkit_policy_decision_ignore, webkit_policy_decision_use,
};

use web_context::WebContextExt;
Expand Down Expand Up @@ -198,6 +198,31 @@ impl InnerWebView {
Inhibit(false)
});

if let Some(nav_handler) = attributes.navigation_handler {
webview.connect_decide_policy(move |_webview, policy_decision, policy_type| {
if let PolicyDecisionType::NavigationAction = policy_type {
if let Some(policy) = policy_decision.dynamic_cast_ref::<NavigationPolicyDecision>() {
if let Some(nav_action) = policy.navigation_action() {
if let Some(uri_req) = nav_action.request() {
if let Some(uri) = uri_req.uri() {
let cancel = nav_handler(uri.to_string());
let pointer = policy_decision.as_ptr();
unsafe {
if cancel {
webkit_policy_decision_ignore(pointer)
} else {
webkit_policy_decision_use(pointer)
}
}
}
}
}
}
}
false
});
}

// Gtk application window can only contain one widget at a time.
// In tao, we add a GtkBox to pack menu bar. So we check if
// there's a box widget here.
Expand Down

0 comments on commit 0500e74

Please sign in to comment.