Forráskód Böngészése

Support window.open()

Johannes Zellner 6 hónapja
szülő
commit
3fecbbee0b
1 módosított fájl, 112 hozzáadás és 11 törlés
  1. 112 11
      src/main.rs

+ 112 - 11
src/main.rs

@@ -20,13 +20,14 @@ use gtk4::glib::prelude::{Cast, IsA, ObjectExt};
 use gtk4::prelude::{GtkWindowExt, NativeExt, WidgetExt};
 use gtk4::{
     Align, Box as GtkBox, Button, Entry, Image, ListBox, Orientation, Overlay, PackType,
-    ScrolledWindow, SelectionMode, WindowControls,
+    PolicyType, ScrolledWindow, SelectionMode, WindowControls,
 };
 use gtk4::gdk::prelude::{DeviceExt, SeatExt, ToplevelExt};
 use gtk4::gdk;
 use webkit6::prelude::*;
 use webkit6::{
-    CookieAcceptPolicy, CookiePersistentStorage, NetworkSession, UserContentInjectedFrames,
+    CookieAcceptPolicy, CookiePersistentStorage, NavigationAction, NavigationPolicyDecision,
+    NavigationType, NetworkSession, PolicyDecisionType, UserContentInjectedFrames,
     UserContentManager, UserScript, UserScriptInjectionTime, WebView,
 };
 
@@ -150,8 +151,78 @@ fn begin_move_from_pointer_on_window(window: &ApplicationWindow) {
     );
 }
 
+/// True when navigation targets the `_blank` browsing context (e.g. `<a target="_blank">`).
+fn navigation_targets_blank_frame(action: &NavigationAction) -> bool {
+    action
+        .frame_name()
+        .as_deref()
+        .is_some_and(|n| n.eq_ignore_ascii_case("_blank"))
+}
+
+/// `window.open()` and `target="_blank"` both go through [`PolicyDecisionType::NewWindowAction`].
+/// Links that target `_blank` load in this view; other new-window requests get a real window via
+/// [`WebViewExt::connect_create`].
+fn wire_webview_window_policy(
+    host_window: &ApplicationWindow,
+    webview: &WebView,
+    network_session: &NetworkSession,
+) {
+    let network_for_create = network_session.clone();
+    let host_window_for_create = host_window.clone();
+
+    webview.connect_decide_policy(move |w, decision, decision_type| {
+        if decision_type != PolicyDecisionType::NewWindowAction {
+            return false;
+        }
+        let Some(nav_decision) = decision.dynamic_cast_ref::<NavigationPolicyDecision>() else {
+            return false;
+        };
+        let Some(action) = nav_decision.navigation_action() else {
+            decision.ignore();
+            return true;
+        };
+
+        if action.navigation_type() == NavigationType::LinkClicked
+            && action.mouse_button() != 2
+            && navigation_targets_blank_frame(&action)
+        {
+            decision.ignore();
+            if let Some(uri) = action.request().and_then(|r| r.uri()) {
+                w.load_uri(&uri);
+            }
+            return true;
+        }
+
+        decision.use_();
+        true
+    });
+
+    webview.connect_create(move |parent, _action| {
+        let app = host_window_for_create.application()?;
+        let window = ApplicationWindow::builder()
+            .application(&app)
+            .title("Appiphany")
+            .default_width(1024)
+            .default_height(768)
+            .build();
+
+        let popup = build_appiphany_webview(&window, &network_for_create, Some(parent));
+        let shell = build_overlay_with_webview(&popup);
+        window.set_content(Some(&shell));
+        window.present();
+        Some(popup.upcast::<gtk4::Widget>())
+    });
+}
+
 /// WebView with injected bridge + script handler; call after `window` exists so moves target it.
-fn build_appiphany_webview(window: &ApplicationWindow, network_session: &NetworkSession) -> WebView {
+///
+/// `related_to` should be the opener when this view is created for `window.open()` so WebKit can
+/// share the rendering process with the parent.
+fn build_appiphany_webview(
+    window: &ApplicationWindow,
+    network_session: &NetworkSession,
+    related_to: Option<&WebView>,
+) -> WebView {
     let ucm = UserContentManager::new();
     if !ucm.register_script_message_handler(WINDOW_DRAG_MESSAGE_HANDLER, None) {
         eprintln!("appiphany: failed to register `{WINDOW_DRAG_MESSAGE_HANDLER}` message handler");
@@ -171,10 +242,17 @@ fn build_appiphany_webview(window: &ApplicationWindow, network_session: &Network
         begin_move_from_pointer_on_window(&win);
     });
 
-    let webview = WebView::builder()
-        .user_content_manager(&ucm)
-        .network_session(network_session)
-        .build();
+    let webview = {
+        let b = WebView::builder()
+            .user_content_manager(&ucm)
+            .network_session(network_session);
+        let b = if let Some(rv) = related_to {
+            b.related_view(rv)
+        } else {
+            b
+        };
+        b.build()
+    };
 
     if let Some(settings) = WebViewExt::settings(&webview) {
         settings.set_user_agent_with_application_details(
@@ -183,6 +261,8 @@ fn build_appiphany_webview(window: &ApplicationWindow, network_session: &Network
         );
     }
 
+    wire_webview_window_policy(window, &webview, network_session);
+
     webview
 }
 
@@ -267,7 +347,7 @@ fn open_web_window(app: &Application, url: &str) {
         .build();
 
     let network_session = network_session_for_app(app);
-    let webview = build_appiphany_webview(&window, &network_session);
+    let webview = build_appiphany_webview(&window, &network_session, None);
     let shell = build_overlay_with_webview(&webview);
     window.set_content(Some(&shell));
     webview.load_uri(url);
@@ -421,12 +501,14 @@ fn build_manager_window(app: &Application) {
     window.set_resizable(true);
     window.set_size_request(420, 320);
 
-    let root = GtkBox::new(Orientation::Vertical, 12);
+    let root = GtkBox::new(Orientation::Vertical, 0);
     root.set_margin_top(12);
     root.set_margin_bottom(12);
     root.set_margin_start(12);
     root.set_margin_end(12);
 
+    let body = GtkBox::new(Orientation::Vertical, 12);
+
     let open_row = GtkBox::new(Orientation::Horizontal, 8);
     let url_entry = Entry::new();
     url_entry.set_hexpand(true);
@@ -436,9 +518,14 @@ fn build_manager_window(app: &Application) {
     open_row.append(&url_entry);
     open_row.append(&open_button);
     open_row.append(&create_button);
+    open_row.set_margin_start(12);
+    open_row.set_margin_end(12);
 
     let launchers_group = PreferencesGroup::builder()
         .title("Installed launchers")
+        // Inset from the ScrolledWindow viewport so `.boxed-list` outlines/shadows are not clipped.
+        .margin_start(12)
+        .margin_end(12)
         .build();
     let list = ListBox::new();
     list.add_css_class("boxed-list");
@@ -449,6 +536,7 @@ fn build_manager_window(app: &Application) {
         .hexpand(true)
         .child(&launchers_group)
         .build();
+    list_scroll.set_policy(PolicyType::Never, PolicyType::Automatic);
     let app_open = app.clone();
     let entry_open = url_entry.clone();
     open_button.connect_clicked(move |_| {
@@ -478,9 +566,22 @@ fn build_manager_window(app: &Application) {
         }
     });
 
-    root.append(&open_row);
-    root.append(&list_scroll);
+    body.append(&open_row);
+    body.append(&list_scroll);
+
+    body.set_hexpand(true);
+    body.set_vexpand(true);
+
+    // Same pattern as GNOME Settings / libadwaita preferences: cap content width and center
+    // so `.boxed-list` borders are not clipped by the scrolled viewport on wide windows.
+    let body_clamp = adw::Clamp::builder()
+        .child(&body)
+        .maximum_size(600)
+        .hexpand(true)
+        .vexpand(true)
+        .build();
 
+    root.append(&body_clamp);
     root.set_hexpand(true);
     root.set_vexpand(true);