Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/main/ipc/changes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}
_ => {}
Expand Down
3 changes: 2 additions & 1 deletion src/main/ipc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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);
}
}

Expand Down
20 changes: 19 additions & 1 deletion src/main/ipc/window.rs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -56,6 +57,13 @@ fn shown_profile(window: &BrowserWindow) -> Option<ShownProfile> {
}
}

/// 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);

Expand Down Expand Up @@ -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
Expand Down
35 changes: 25 additions & 10 deletions src/main/platform/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,17 @@ pub enum WindowKind {
Panel,
}

type CrashWatch = Rc<RefCell<Option<Box<dyn Fn()>>>>;
type Watch = Rc<RefCell<Option<Box<dyn Fn()>>>>;

/// A window showing one page. Its web view drops first, which takes the page
/// out of the window.
pub struct PageWindow {
pub web_view: Rc<WebView>,
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 {
Expand Down Expand Up @@ -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();
}
Expand All @@ -105,6 +112,7 @@ impl PageWindow {
web_view,
native,
crash_watch,
gone_watch,
}
}

Expand Down Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions src/main/tabs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
151 changes: 151 additions & 0 deletions src/main/tabs/input.rs
Original file line number Diff line number Diff line change
@@ -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<HashSet<InputLock>>);

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<HashMap<BrowserTab, HashSet<InputLock>>> = RefCell::default();
// Tabs given the keyboard while they ignored input.
static WAITING: RefCell<HashSet<BrowserTab>> = 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<InputLock> = 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<InputLock>, 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<InputLock>)) {
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);
}
}
13 changes: 12 additions & 1 deletion src/main/tabs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<State>, 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);
Expand All @@ -58,6 +64,11 @@ pub fn changed(state: &Rc<State>, 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.
Expand Down
2 changes: 1 addition & 1 deletion src/main/windows/mini_wren/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
8 changes: 8 additions & 0 deletions src/main/windows/open.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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) {
Expand Down
Loading
Loading