Skip to content

armgit / Pake

Public Imported from GitHub
0

Turn any website into a lightweight desktop app with one command, on macOS, Windows and Linux.

fix(window): keep a page's popup on the screen, over its opener

Tw931a09719aa563
diff --git a/src-tauri/src/app/setup.rs b/src-tauri/src/app/setup.rs
index 4d6d861c..d4630829 100644
--- a/src-tauri/src/app/setup.rs
+++ b/src-tauri/src/app/setup.rs
@@ -206,7 +206,7 @@ fn menu_bar_popover_position(
}
#[cfg(any(target_os = "macos", target_os = "windows", test))]
-fn clamp_to_work_area(
+pub(crate) fn clamp_to_work_area(
raw: (f64, f64),
win_size: (f64, f64),
work_pos: (f64, f64),
diff --git a/src-tauri/src/app/window.rs b/src-tauri/src/app/window.rs
index d8d2c501..b12462a3 100644
--- a/src-tauri/src/app/window.rs
+++ b/src-tauri/src/app/window.rs
@@ -520,9 +520,12 @@ fn open_requested_window(
app: &AppHandle,
config: &PakeConfig,
tauri_config: &Config,
+ opener: &str,
target_url: Url,
features: NewWindowFeatures,
) -> tauri::Result<WebviewWindow> {
+ #[cfg(not(any(target_os = "macos", target_os = "windows")))]
+ let _ = opener;
let state = app.state::<MultiWindowState>();
let label = state.next_popup_label();
let window = build_window(
@@ -542,11 +545,135 @@ fn open_requested_window(
let _ = window.set_title(host);
}
reapply_window_icon(&window);
+ #[cfg(any(target_os = "macos", target_os = "windows"))]
+ keep_popup_on_screen(app, opener, &window);
let _ = window.set_focus();
Ok(window)
}
+/// A window frame in physical pixels: x, y, width, height.
+#[cfg(any(target_os = "macos", target_os = "windows", test))]
+type Frame = (f64, f64, f64, f64);
+
+/// A page places its popup from its own window's frame, and Notion's Google
+/// sign-in reads that frame as zero in a macOS app: it asked for (-305, 800)
+/// points, most of the window off the screen. A popup that does not sit wholly
+/// on one monitor's work area goes over its opener instead.
+#[cfg(any(target_os = "macos", target_os = "windows"))]
+fn keep_popup_on_screen(app: &AppHandle, opener: &str, popup: &WebviewWindow) {
+ fn frame(position: tauri::PhysicalPosition<i32>, size: tauri::PhysicalSize<u32>) -> Frame {
+ (
+ f64::from(position.x),
+ f64::from(position.y),
+ f64::from(size.width),
+ f64::from(size.height),
+ )
+ }
+ let (Ok(position), Ok(size)) = (popup.outer_position(), popup.outer_size()) else {
+ return;
+ };
+ let work_areas: Vec<Frame> = app
+ .available_monitors()
+ .unwrap_or_default()
+ .iter()
+ .map(|monitor| frame(monitor.work_area().position, monitor.work_area().size))
+ .collect();
+ let Some(opener) = app.get_webview_window(opener) else {
+ return;
+ };
+ let (Ok(opener_position), Ok(opener_size)) = (opener.outer_position(), opener.outer_size())
+ else {
+ return;
+ };
+ let Some(monitor) = opener
+ .current_monitor()
+ .ok()
+ .flatten()
+ .or_else(|| app.primary_monitor().ok().flatten())
+ else {
+ return;
+ };
+ let work = monitor.work_area();
+ if let Some((x, y)) = popup_position_on_screen(
+ frame(position, size),
+ frame(opener_position, opener_size),
+ &work_areas,
+ frame(work.position, work.size),
+ ) {
+ let _ = popup.set_position(tauri::PhysicalPosition::new(x, y));
+ }
+}
+
+/// Where a popup goes when it is not wholly on any work area: centred over its
+/// opener and kept inside the opener's work area. `None` leaves it where the
+/// page put it.
+#[cfg(any(target_os = "macos", target_os = "windows", test))]
+fn popup_position_on_screen(
+ popup: Frame,
+ opener: Frame,
+ work_areas: &[Frame],
+ opener_work: Frame,
+) -> Option<(i32, i32)> {
+ let (x, y, width, height) = popup;
+ let on_screen = work_areas.iter().any(|&(wx, wy, ww, wh)| {
+ x >= wx && y >= wy && x + width <= wx + ww && y + height <= wy + wh
+ });
+ if on_screen {
+ return None;
+ }
+ let (ox, oy, ow, oh) = opener;
+ Some(crate::app::setup::clamp_to_work_area(
+ (ox + (ow - width) / 2.0, oy + (oh - height) / 2.0),
+ (width, height),
+ (opener_work.0, opener_work.1),
+ (opener_work.2, opener_work.3),
+ ))
+}
+
+#[cfg(test)]
+mod popup_position_tests {
+ use super::popup_position_on_screen;
+
+ const SCREEN: (f64, f64, f64, f64) = (0.0, 76.0, 3456.0, 2158.0);
+ const OPENER: (f64, f64, f64, f64) = (528.0, 142.0, 2400.0, 1616.0);
+
+ #[test]
+ fn a_popup_mostly_off_the_screen_goes_over_its_opener() {
+ // Notion's Google sign-in on a 2x Mac, 2026-12-04: (-305, 800) points.
+ let popup = (-610.0, 1600.0, 1220.0, 1456.0);
+ assert_eq!(
+ popup_position_on_screen(popup, OPENER, &[SCREEN], SCREEN),
+ Some((1118, 222))
+ );
+ }
+
+ #[test]
+ fn a_popup_on_the_screen_stays_where_the_page_put_it() {
+ let popup = (1138.0, 250.0, 1180.0, 1716.0);
+ assert_eq!(
+ popup_position_on_screen(popup, OPENER, &[SCREEN], SCREEN),
+ None
+ );
+ // Wholly on a second monitor to the right counts as on the screen.
+ let second = (3456.0, 0.0, 1920.0, 1080.0);
+ let there = (3600.0, 100.0, 800.0, 600.0);
+ assert_eq!(
+ popup_position_on_screen(there, OPENER, &[SCREEN, second], SCREEN),
+ None
+ );
+ }
+
+ #[test]
+ fn a_popup_larger_than_the_work_area_starts_at_its_corner() {
+ let popup = (-100.0, -100.0, 4000.0, 3000.0);
+ assert_eq!(
+ popup_position_on_screen(popup, OPENER, &[SCREEN], SCREEN),
+ Some((0, 76))
+ );
+ }
+}
+
/// Open a multi-window clone of the home app. The window is built hidden and
/// revealed on its first real page load (see `lib.rs` on_page_load) so Cmd+N
/// does not flash an empty shell the way the main window used to.
@@ -1047,6 +1174,7 @@ fn build_window(
let app_handle = app.clone();
let popup_config = config.clone();
let popup_tauri_config = tauri_config.clone();
+ let opener = label.to_string();
window_builder = window_builder.on_new_window(move |target_url, features| {
// Even without --new-window, two-stage popups need a real webview
// with our download delegate, never a proxy for the main window.
@@ -1061,6 +1189,7 @@ fn build_window(
&app_handle,
&popup_config,
&popup_tauri_config,
+ &opener,
target_url,
features,
) {