diff --git a/src/main/ipc/changes.rs b/src/main/ipc/changes.rs index 7725a5b..e4be80c 100644 --- a/src/main/ipc/changes.rs +++ b/src/main/ipc/changes.rs @@ -7,6 +7,7 @@ use wren::{Browser, Change, ChangeKind, TabFields}; use crate::ipc; +use crate::tabs; use crate::windows::BrowserWindow; /// Tells `window`'s page of the changes to its Browser in front, and @@ -85,7 +86,7 @@ fn apply_batch(window: &BrowserWindow, changes: &[Change]) -> bool { && active && tab.state().is_ok_and(|state| state.in_element) { - let _ = tab.focus(); + tabs::input::give_keyboard(tab); } } _ => {} diff --git a/src/main/ipc/mod.rs b/src/main/ipc/mod.rs index 3c3ae1c..64fe328 100644 --- a/src/main/ipc/mod.rs +++ b/src/main/ipc/mod.rs @@ -48,6 +48,7 @@ use serde::de::DeserializeOwned; use wren::{Browser, MainThread, NativeView, PageCall, WebView}; use crate::app::State; +use crate::tabs::input::give_keyboard; use crate::tabs::is_new_tab; use crate::windows::{BrowserWindow, WindowId}; @@ -230,7 +231,7 @@ fn give_back_keyboard(browser: &Browser) { if let Some(tab) = browser.active_tab() && !is_new_tab(&tab.url()) { - let _ = tab.focus(); + give_keyboard(tab); } } diff --git a/src/main/ipc/window.rs b/src/main/ipc/window.rs index 06b1dcd..b2f5e31 100644 --- a/src/main/ipc/window.rs +++ b/src/main/ipc/window.rs @@ -1,12 +1,13 @@ use std::rc::Rc; -use serde::Serialize; +use serde::{Deserialize, Serialize}; use wren::{MainThread, WebView}; use super::{WindowRef, failed, give_back_keyboard, with_window}; use crate::app::State; use crate::db::SpaceId; use crate::platform::PageWindow; +use crate::tabs::input::{self, InputLock}; use crate::windows::{self, BrowserWindow, Place}; /// The space the page draws for a window on a profile, made up, as no space @@ -56,6 +57,13 @@ fn shown_profile(window: &BrowserWindow) -> Option { } } +/// What `window.lockTabInput` takes. +#[derive(Deserialize)] +struct LockTabInput { + lock: InputLock, + held: bool, +} + pub fn handle(web_view: &WebView, window: &WindowRef, mt: MainThread) { handle_page(web_view, window); @@ -128,6 +136,16 @@ pub fn handle(web_view: &WebView, window: &WindowRef, mt: MainThread) { with_window(&weak, |window| Ok(window.is_blocked())) }); + // Holds a lock on the window's tabs' input while Wren's own UI covers + // them, or lets it go. + let weak = window.clone(); + let _ = web_view.handle("window.lockTabInput", move |call: LockTabInput| { + with_window(&weak, |window| { + input::set_on_window(window, call.lock, call.held); + Ok(()) + }) + }); + // Whether the place in front has an active tab, as the app knows it now. // The page opens the omnibox for a new tab in a window that opens with // none. The tabs it was told can be behind: a window opened for links diff --git a/src/main/platform/window.rs b/src/main/platform/window.rs index 0b4d299..6198577 100644 --- a/src/main/platform/window.rs +++ b/src/main/platform/window.rs @@ -45,7 +45,7 @@ pub enum WindowKind { Panel, } -type CrashWatch = Rc>>>; +type Watch = Rc>>>; /// A window showing one page. Its web view drops first, which takes the page /// out of the window. @@ -53,7 +53,9 @@ pub struct PageWindow { pub web_view: Rc, native: current::window::PageWindow, // What `watch_crash` was given. - crash_watch: CrashWatch, + crash_watch: Watch, + // What `watch_page_gone` was given. + gone_watch: Watch, } impl PageWindow { @@ -81,20 +83,25 @@ impl PageWindow { // so it holds it weakly. let reloaded = Rc::downgrade(&web_view); let crashed = Cell::new(false); - let crash_watch: CrashWatch = Rc::default(); - let watch = crash_watch.clone(); + let loading = Cell::new(false); + let crash_watch: Watch = Rc::default(); + let gone_watch: Watch = Rc::default(); + let (on_crash, on_gone) = (crash_watch.clone(), gone_watch.clone()); web_view.set_changed(Some(Box::new(move |state| { let now_crashed = state.crash != TabCrash::None; - if now_crashed - && !crashed.get() - && let Some(web_view) = reloaded.upgrade() - { + let crash_started = !crashed.replace(now_crashed) && now_crashed; + let load_started = !loading.replace(state.loading) && state.loading; + if crash_started && let Some(web_view) = reloaded.upgrade() { let _ = web_view.reload(); - if let Some(watch) = &*watch.borrow() { + if let Some(watch) = &*on_crash.borrow() { watch(); } } - crashed.set(now_crashed); + if (crash_started || load_started) + && let Some(watch) = &*on_gone.borrow() + { + watch(); + } if state.load_error != 0 || state.crash != TabCrash::None { show(); } @@ -105,6 +112,7 @@ impl PageWindow { web_view, native, crash_watch, + gone_watch, } } @@ -241,6 +249,13 @@ impl PageWindow { *self.crash_watch.borrow_mut() = Some(Box::new(crashed)); } + /// Calls `gone` each time the page goes, with whatever it held: as it + /// crashes, and as another load starts, such as a reload from DevTools + /// or the dev server. + pub fn watch_page_gone(&self, gone: impl Fn() + 'static) { + *self.gone_watch.borrow_mut() = Some(Box::new(gone)); + } + /// Brings the window to the front, and the app with it, as Chrome does /// for a window Chromium wants in front. pub fn bring_forward(&self) { diff --git a/src/main/tabs/README.md b/src/main/tabs/README.md index e2f8335..24ab3a2 100644 --- a/src/main/tabs/README.md +++ b/src/main/tabs/README.md @@ -8,6 +8,7 @@ A part. It is what Wren does to a Browser's tabs, whoever asks, and what each ta - What each tab is, which is a tab of the sidebar's list, a pin's, the placeholder, or one closing for a pin. - Archiving tabs, the pictures of tabs, and making tabs again in another profile. - What follows a change Chromium makes to a Browser's tabs. +- The locks on each tab's input, such as one a window holds on all its tabs while Wren's own UI covers them. ## What goes elsewhere diff --git a/src/main/tabs/input.rs b/src/main/tabs/input.rs new file mode 100644 index 0000000..adc853a --- /dev/null +++ b/src/main/tabs/input.rs @@ -0,0 +1,151 @@ +// Locks on tabs' input. Each tab has its own list of locks, and ignores +// input while any is on it. Each lock has an ID of its own (`InputLock`), so +// letting one go never lifts another held for another reason. +// +// A window holds some locks on all its tabs at once, as a dialog does with +// its scrim over the window. Its tabs have the lock while it holds it, +// those that come to the window too, and a tab that moves to another +// window takes that window's instead. Every lock a window holds is its +// page's, so they all go with the page, should it crash or load again +// under a dialog (`page_gone`). +// +// Wren Runtime refuses the keyboard to a tab that ignores input, so a tab +// given it then (`give_keyboard`) waits, and has it as the window lets go of +// its last lock, if it is still the active tab in front. A dialog's answer +// that makes a tab active, such as a move to another space, so leaves the +// keyboard on that tab and not on the page. + +use std::cell::RefCell; +use std::collections::{HashMap, HashSet}; + +use serde::Deserialize; +use wren::{Browser, BrowserTab}; + +use crate::windows::BrowserWindow; + +/// Why a tab ignores input. Each is held and let go on its own. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Deserialize)] +#[serde(rename_all = "camelCase")] +pub enum InputLock { + /// One of Wren's dialogs is up, its scrim over the window + /// (components/ui/dialog.tsx). + Dialog, +} + +impl InputLock { + // Whether a window holds it on all its tabs, so a tab has it only while + // in that window. + fn is_window_wide(self) -> bool { + match self { + InputLock::Dialog => true, + } + } +} + +/// The locks a window holds on all its tabs. +#[derive(Default)] +pub struct WindowInputLocks(RefCell>); + +thread_local! { + // By tab, whose ID is the app's own whichever Browser it is in. A tab + // with no lock on it has no entry. + static LOCKS: RefCell>> = RefCell::default(); + // Tabs given the keyboard while they ignored input. + static WAITING: RefCell> = RefCell::default(); +} + +/// Gives `tab`'s page the keyboard, or, while it ignores input, once its +/// window lets go of its last lock. +pub fn give_keyboard(tab: BrowserTab) { + if is_locked(tab) { + WAITING.with(|waiting| waiting.borrow_mut().insert(tab)); + } else { + let _ = tab.focus(); + } +} + +/// Holds `lock` on `window`'s tabs, or lets it go. +pub fn set_on_window(window: &BrowserWindow, lock: InputLock, held: bool) { + let was_locked = !window.input_locks.0.borrow().is_empty(); + toggle(&mut window.input_locks.0.borrow_mut(), lock, held); + // Its active tab in front, if it waits for the keyboard. + let waiting = window + .browser() + .and_then(|browser| browser.active_tab()) + .filter(|tab| WAITING.with(|waiting| waiting.borrow().contains(tab))); + for browser in window.browsers() { + for tab in browser.tabs() { + change(tab, |on_tab| toggle(on_tab, lock, held)); + } + } + // A tab that ignores input can still take keys from an input method and + // the Tab key, so the page takes the keyboard from it. + if held && !was_locked { + let _ = window.page.web_view.focus(); + } + if let Some(tab) = waiting.filter(|tab| !is_locked(*tab)) { + let _ = tab.focus(); + } +} + +/// Lets go every lock `window`'s page held, as the page has gone. +pub fn page_gone(window: &BrowserWindow) { + let held: Vec = window.input_locks.0.borrow().iter().copied().collect(); + for lock in held { + set_on_window(window, lock, false); + } +} + +/// Gives `browser`'s tabs the locks `window` holds on all its tabs in place +/// of any window's before, as it comes to the window and as tabs come to +/// it. A tab's own locks stay. +pub fn follow(window: &BrowserWindow, browser: &Browser) { + let of_window = window.input_locks.0.borrow(); + for tab in browser.tabs() { + change(tab, |on_tab| { + on_tab.retain(|lock| !lock.is_window_wide()); + on_tab.extend(of_window.iter().copied()); + }); + } +} + +/// Forgets the locks of tabs that have closed. +pub fn forget_closed() { + LOCKS.with(|locks| locks.borrow_mut().retain(|tab, _| tab.browser().is_some())); + WAITING.with(|waiting| waiting.borrow_mut().retain(|tab| tab.browser().is_some())); +} + +fn is_locked(tab: BrowserTab) -> bool { + LOCKS.with(|locks| locks.borrow().contains_key(&tab)) +} + +fn toggle(locks: &mut HashSet, lock: InputLock, held: bool) { + if held { + locks.insert(lock); + } else { + locks.remove(&lock); + } +} + +// Changes `tab`'s locks. The tab ignores input while it has any, and waits +// for the keyboard no longer once it has none. +fn change(tab: BrowserTab, change: impl FnOnce(&mut HashSet)) { + let (was_locked, locked) = LOCKS.with(|locks| { + let mut locks = locks.borrow_mut(); + let mut on_tab = locks.remove(&tab).unwrap_or_default(); + let was_locked = !on_tab.is_empty(); + change(&mut on_tab); + let locked = !on_tab.is_empty(); + if locked { + locks.insert(tab, on_tab); + } + (was_locked, locked) + }); + if !locked { + WAITING.with(|waiting| waiting.borrow_mut().remove(&tab)); + } + if locked != was_locked { + // It fails only for a tab that has closed. + let _ = tab.set_ignores_input(locked); + } +} diff --git a/src/main/tabs/mod.rs b/src/main/tabs/mod.rs index e83626f..09b8793 100644 --- a/src/main/tabs/mod.rs +++ b/src/main/tabs/mod.rs @@ -9,10 +9,14 @@ // // Tabs brought from another Browser move there if it is of their profile, // and close and are made again if it is of another (recreate.rs). +// +// A tab ignores input while any lock is on it, such as one a window's +// dialog holds on all its tabs (input.rs). pub mod archive; pub mod closes; pub mod groups; +pub mod input; pub mod placeholder; pub mod previews; mod recreate; @@ -39,10 +43,12 @@ use crate::windows::Place; /// (`change`). A placeholder goes, or stays, as the tabs change, and the /// pins' links follow them. A tab left for another has its picture taken, /// and one whose page changes has it go stale. The page is told of the tabs -/// when a pin settled. +/// when a pin settled. Tabs that come to a Browser take on its window's +/// locks on their input. pub fn changed(state: &Rc, browser: &Browser, change: &Change) { closes::settle(&state.analytics); roles::forget_closed(); + input::forget_closed(); items::carry::forget_gone(); placeholder::settle(browser); previews::follow_active(browser); @@ -58,6 +64,11 @@ pub fn changed(state: &Rc, browser: &Browser, change: &Change) { { ipc::send_tabs(&window, browser); } + if change.kind == ChangeKind::Tabs + && let Some(window) = state.windows.of(browser) + { + input::follow(&window, browser); + } } /// Counts a tab whose page has just crashed, if `change` says one did. diff --git a/src/main/windows/mini_wren/mod.rs b/src/main/windows/mini_wren/mod.rs index 383182c..77c4144 100644 --- a/src/main/windows/mini_wren/mod.rs +++ b/src/main/windows/mini_wren/mod.rs @@ -170,7 +170,7 @@ pub fn follow_new_tab_page(state: &State, mt: MainThread, window: &BrowserWindow ipc::send_new_tab_page(window); menus::update_front_menu(state, mt); if !on_new_tab_page && let Some(tab) = browser.active_tab() { - let _ = tab.focus(); + tabs::input::give_keyboard(tab); } } diff --git a/src/main/windows/open.rs b/src/main/windows/open.rs index b34864f..8e499ed 100644 --- a/src/main/windows/open.rs +++ b/src/main/windows/open.rs @@ -17,6 +17,7 @@ use crate::pages::UI; use crate::perf::PageCounts; use crate::platform::{PageWindow, WindowKind, WindowSize}; use crate::spaces; +use crate::tabs; use crate::windows::{self, BrowserWindow, MiniWren, Place, WindowId}; /// A browser window's size when nothing gives it one. @@ -151,6 +152,13 @@ pub(super) fn open_page_window( }); count_crashes(state, &page); let weak = Rc::downgrade(state); + page.watch_page_gone(move || { + let Some(state) = weak.upgrade() else { return }; + if let Some(window) = state.windows.get(id) { + tabs::input::page_gone(&window); + } + }); + let weak = Rc::downgrade(state); page.watch_full_screen(move |full_screen| { let Some(state) = weak.upgrade() else { return }; if let Some(window) = state.windows.get(id) { diff --git a/src/main/windows/switch.rs b/src/main/windows/switch.rs index 6ccad8f..7a5e2c5 100644 --- a/src/main/windows/switch.rs +++ b/src/main/windows/switch.rs @@ -12,6 +12,7 @@ use crate::app::State; use crate::db::SpaceId; use crate::ipc; use crate::spaces; +use crate::tabs; use crate::windows::{self, BrowserWindow, Place, WindowId}; /// Brings `space` forward in window `id`: its Browser if the window has @@ -35,7 +36,7 @@ pub fn show_space(state: &State, id: WindowId, space: SpaceId) -> Result<(), Str Some(browser) => { ipc::send_browser(&window, &browser); if let Some(tab) = browser.active_tab() { - let _ = tab.focus(); + tabs::input::give_keyboard(tab); } } None => ipc::send_no_browser(&window), diff --git a/src/main/windows/window/mod.rs b/src/main/windows/window/mod.rs index f2475f8..ba840f9 100644 --- a/src/main/windows/window/mod.rs +++ b/src/main/windows/window/mod.rs @@ -29,6 +29,7 @@ use crate::ipc::Told; use crate::perf::PageCounts; use crate::platform::PageWindow; use crate::sidebar::hover::SidebarHover; +use crate::tabs::input::{self, WindowInputLocks}; use crate::tabs::placeholder; /// The page's mark on its page info button, which the site's bubbles (page @@ -86,6 +87,8 @@ pub struct BrowserWindow { /// What its page has been told, of its tabs and its downloads /// (ipc/told.rs). pub told: Told, + /// The locks it holds on all its tabs' input (tabs/input.rs). + pub input_locks: WindowInputLocks, places: RefCell>, // A window on a profile has only that place in front, ever. front: RefCell, @@ -138,6 +141,7 @@ impl BrowserWindow { page, sidebar_hover: SidebarHover::default(), told: Told::default(), + input_locks: WindowInputLocks::default(), places: RefCell::default(), front: RefCell::new(place.map_or(Front::Waiting, Front::Place)), framed: Cell::new(framed), @@ -445,6 +449,7 @@ impl BrowserWindow { { self.page.restore_frame(bounds); } + input::follow(self, &browser); self.places.borrow_mut().insert(place, Slot::Open(browser)); } diff --git a/src/renderer/components/ui/dialog.tsx b/src/renderer/components/ui/dialog.tsx index 671a388..37e8836 100644 --- a/src/renderer/components/ui/dialog.tsx +++ b/src/renderer/components/ui/dialog.tsx @@ -20,6 +20,7 @@ import { Button } from "./button"; import { useDialogTurn } from "./dialog-turn"; import { useHoldWhile } from "./hold"; import { Keycap } from "./keycap"; +import { useTabInputLockWhile } from "./tab-input-lock"; import { useWindowBlocked } from "@/state/window"; import { cn } from "@/utils/cn"; import { EASE_LEAVE, EASE_OUT } from "@/utils/motion"; @@ -75,6 +76,8 @@ export function Dialog(props: DialogProps) { const [present, setPresent] = useState(false); const shown = useDialogTurn(props.open || present) && props.open; if (shown && !present) setPresent(true); + // Its scrim covers the tabs from the moment it shows until it has gone. + useTabInputLockWhile("dialog", present); return ( {/* The card is made afresh each time it opens, so nothing of the last diff --git a/src/renderer/components/ui/tab-input-lock.ts b/src/renderer/components/ui/tab-input-lock.ts new file mode 100644 index 0000000..053fcb2 --- /dev/null +++ b/src/renderer/components/ui/tab-input-lock.ts @@ -0,0 +1,19 @@ +// Locks on the input of the window's tabs. What covers them, as a dialog's +// scrim does, holds a lock while it is up, and the tabs ignore input while +// any lock is held. Each lock has an ID of its own, and each holder a hold +// of its own on it, so letting one go never lifts another. A page with no +// tabs under it has nothing to lock, and a hold there does nothing. + +import { createContext, useContext, useEffect } from "react"; +import type { TabInputLock } from "@/ipc"; + +/** Holds `lock` on the window's tabs, and answers what lets it go. */ +export type LockTabInput = (lock: TabInputLock) => () => void; + +export const TabInputLockContext = createContext(() => () => {}); + +/** Holds `lock` on the window's tabs while `held`. */ +export function useTabInputLockWhile(lock: TabInputLock, held: boolean) { + const lockTabInput = useContext(TabInputLockContext); + useEffect(() => (held ? lockTabInput(lock) : undefined), [lockTabInput, lock, held]); +} diff --git a/src/renderer/ipc/window.ts b/src/renderer/ipc/window.ts index 5e4562f..6b20b81 100644 --- a/src/renderer/ipc/window.ts +++ b/src/renderer/ipc/window.ts @@ -1,3 +1,6 @@ +/** Why the window's tabs ignore input, as one of Wren's dialogs covers them. Each is held and let go on its own. */ +export type TabInputLock = "dialog"; + /** The space a window is on, or that it is on none while the user makes one. */ export type WindowSpace = { kind: "space"; id: number } | { kind: "newSpace" }; @@ -55,6 +58,8 @@ export const appWindow = { */ blocked: () => wren.invoke("window.blocked"), onBlocked: (listener: (blocked: boolean) => void) => wren.on("window.blocked", listener), + /** Holds `lock` on the window's tabs, or lets it go. They ignore input while any lock is held. */ + lockTabInput: (lock: TabInputLock, held: boolean) => wren.invoke("window.lockTabInput", { lock, held }), /** Whether the window's space has an active tab, as the app knows it now, not as the page was last told. */ hasActiveTab: () => wren.invoke("window.hasActiveTab"), /** The space the window shows; null while it waits for its first Browser, or shows a profile. */ diff --git a/src/renderer/pages/ui/app.tsx b/src/renderer/pages/ui/app.tsx index aa4e896..c47ee54 100644 --- a/src/renderer/pages/ui/app.tsx +++ b/src/renderer/pages/ui/app.tsx @@ -2,11 +2,13 @@ import { motion, MotionConfig } from "motion/react"; import { type CSSProperties, useEffect, useRef } from "react"; import { hasApp, type SidebarState } from "@/ipc"; import { HoldContext } from "@/components/ui/hold"; +import { TabInputLockContext } from "@/components/ui/tab-input-lock"; import { TooltipProvider } from "@/components/ui/tooltip"; import { ResizeHandle } from "@/components/ui/resize-handle"; import { BrowserProvider, useSidePanel, useTab, useTabShape } from "@/state/browser"; import { DownloadsProvider } from "@/state/downloads"; import { PinsProvider } from "@/state/pins"; +import { lockTabInput } from "@/state/tab-input-lock"; import { SpaceProvider, useSpace } from "@/state/space"; import { useShowWindow, useWindowBlocked, useWindowMiniWren, useWindowTheme } from "@/state/window"; import { useSpaceBlend } from "@/state/space-blend"; @@ -47,24 +49,26 @@ export function App() { - - {miniWren ? ( - - ) : ( - miniWren === null && ( - // Its spaces' pins are read while the sidebar's state is. - - {saved !== undefined && margins !== undefined && addressBar !== undefined && ( - - - - - - )} - - ) - )} - + + + {miniWren ? ( + + ) : ( + miniWren === null && ( + // Its spaces' pins are read while the sidebar's state is. + + {saved !== undefined && margins !== undefined && addressBar !== undefined && ( + + + + + + )} + + ) + )} + + diff --git a/src/renderer/state/tab-input-lock.ts b/src/renderer/state/tab-input-lock.ts new file mode 100644 index 0000000..06c1524 --- /dev/null +++ b/src/renderer/state/tab-input-lock.ts @@ -0,0 +1,29 @@ +// The window's locks on its tabs' input, which the main process holds +// (tabs/input.rs). Several on the page can hold the same lock at once, such +// as a dialog that goes as the next one comes, so the main process is told +// of a lock as its first holder takes it and as its last lets it go. + +import type { LockTabInput } from "@/components/ui/tab-input-lock"; +import { appWindow, hasApp, type TabInputLock } from "@/ipc"; + +const holders = new Map(); + +export const lockTabInput: LockTabInput = (lock) => { + // Outside the app, as in a browser on Vite's dev server, there are no tabs. + if (!hasApp) return () => {}; + const count = holders.get(lock) ?? 0; + holders.set(lock, count + 1); + if (count === 0) void appWindow.lockTabInput(lock, true); + let held = true; + return () => { + if (!held) return; + held = false; + const left = (holders.get(lock) ?? 1) - 1; + if (left > 0) { + holders.set(lock, left); + } else { + holders.delete(lock); + void appWindow.lockTabInput(lock, false); + } + }; +};