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: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ All notable changes to Tarn are documented in this file. The format is based on

### Added

- A video playing with sound moves into Picture in Picture when you switch to another tab, and
back into its page when you return; a window you opened yourself in that tab stays.
Settings › Tabs turns it off. macOS (WebKit tabs) and Windows.
- Agent control: turn on Settings › AI › Let AI agents control Tarn and add Tarn to Claude Code or
Codex (`tarn --mcp`), and the agent can browse for you in a window of its own that keeps nothing.
You allow the agent (look only, or act), each site it uses and each click on a page holding a
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,10 @@ Trackpad (Settings › Tabs › Trackpad gestures): swipe left or right over the
spaces; swipe over the address field to step through tabs; swipe right over the toolbar to show the
sidebar. The mouse's back and forward buttons always navigate.

A video playing with sound moves into Picture in Picture when you switch to another tab, and back
into its page when you return (Settings › Tabs › Picture in Picture when switching tabs; macOS
WebKit tabs and Windows).

</details>

<details>
Expand Down
3 changes: 2 additions & 1 deletion crates/app/src/browser/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,8 +404,9 @@ impl Browser {
link: Option<Option<Url>>,
cx: &mut Context<Self>,
) {
if self.model.tab(key).is_some() && self.views.remove(&key).is_some() {
if self.model.tab(key).is_some() && self.views.contains_key(&key) {
self.forget_media(key);
self.views.remove(&key);
// The old view's last events are dropped; the new one says when its load starts.
let page = self.pages.entry(key).or_default();
page.loading = false;
Expand Down
2 changes: 1 addition & 1 deletion crates/app/src/capture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ impl Browser {
fn picture_in_picture(&mut self, cx: &mut Context<Self>) {
let (Some(key), Some(view)) = (self.model.active, self.active_view()) else { return };
let (weak, (tx, mut rx)) = (Rc::downgrade(&view), mpsc::unbounded());
view.picture_in_picture(move |result| drop(tx.unbounded_send(result)));
view.picture_in_picture(engine::page_scripts::PipMode::Toggle, move |result| drop(tx.unbounded_send(result)));
cx.spawn(async move |this, cx| {
let notice = match rx.next().await.as_deref() {
Some("none") => "This page has no video",
Expand Down
1 change: 1 addition & 0 deletions crates/app/src/content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ pub(crate) fn banner(text: impl Into<SharedString>) -> gpui::Div {
pub fn render(b: &mut Browser, window: &mut Window, cx: &mut Context<Browser>) -> AnyElement {
b.settle_motion(window, cx);
b.settle_split();
b.settle_screen(cx);
if b.capture_overlay.as_ref().is_some_and(|o| b.model.active != Some(o.key)) {
b.capture_overlay = None;
}
Expand Down
76 changes: 75 additions & 1 deletion crates/app/src/mini_player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::rc::Rc;

use browser_core::media::{Control, POLL};
use browser_core::page_ops::MediaState;
use engine::page_scripts::PipMode;
use futures::StreamExt;
use futures::channel::mpsc;
use gpui::{AnyElement, Context, InteractiveElement, ParentElement, SharedString, StatefulInteractiveElement, Styled, Task, Window, div, px};
Expand All @@ -18,6 +19,7 @@ use crate::browser::{Browser, TabKey};
#[derive(Default)]
pub struct MiniPlayer {
pub(crate) player: browser_core::media::Player,
auto_pip: browser_core::media::AutoPip,
poll: Option<Task<()>>,
}

Expand Down Expand Up @@ -59,9 +61,81 @@ impl Browser {
}));
}

/// Drops `key` from the mini-player once its view is gone.
/// Drops `key` from the mini-player once its view is gone; a video Tarn moved into Picture in
/// Picture comes back out, since the tab closes or shows in another window.
pub(crate) fn forget_media(&mut self, key: TabKey) {
self.mini_player.player.forget(key);
if self.mini_player.auto_pip.take(key) {
self.exit_pip(key);
}
}

/// The tabs on screen: the active tab, or every pane of its split, and a tab still fading out.
fn screen_tabs(&self) -> Vec<TabKey> {
let active = self.model.active;
let mut tabs = active.and_then(|k| self.splits.of(k)).map_or_else(|| active.into_iter().collect(), |s| s.panes().to_vec());
let fading = self.page_fade.as_ref().and_then(|(view, _)| self.views.iter().find(|(_, v)| Rc::ptr_eq(v, view)));
tabs.extend(fading.map(|(k, _)| *k).filter(|k| !tabs.contains(k)));
tabs
}

/// Called on every render, after whatever changed the tabs on screen: a playing video of a tab
/// that left moves into Picture in Picture, and one Tarn moved there comes back with its tab.
pub(crate) fn settle_screen(&mut self, cx: &mut Context<Self>) {
let (left, back) = self.mini_player.auto_pip.screen(self.screen_tabs());
for key in left {
self.leave_for_pip(key, cx);
}
for key in back {
self.exit_pip(key);
}
}

/// Asks the engine first whether the page plays (on WebKit its own playback state), so a page
/// that plays nothing gets no script, and with it no gesture, when its tab leaves the screen.
fn leave_for_pip(&mut self, key: TabKey, cx: &mut Context<Self>) {
let Some(view) = self.views.get(&key).filter(|v| self.may_pip(key, v)) else { return };
let (weak, (tx, mut rx)) = (Rc::downgrade(view), mpsc::unbounded());
view.media_playing(move |playing| drop(tx.unbounded_send(playing)));
cx.spawn(async move |this, cx| {
if rx.next().await != Some(true) {
return;
}
this.update(cx, |b, cx| {
let Some(view) = weak.upgrade().filter(|v| b.same_view(key, &weak) && !b.screen_tabs().contains(&key) && b.may_pip(key, v)) else { return };
let (tx, mut rx) = mpsc::unbounded();
view.picture_in_picture(PipMode::Enter, move |result| drop(tx.unbounded_send(result)));
cx.spawn(async move |this, cx| {
let Some(result) = rx.next().await else { return };
this.update(cx, |b, _| {
if !b.same_view(key, &weak) {
// The view moved to another window or was replaced meanwhile.
if let Some(view) = weak.upgrade().filter(|_| result == "pip") {
view.picture_in_picture(PipMode::Exit, |_| {});
}
} else if b.mini_player.auto_pip.answered(key, &result, b.screen_tabs().contains(&key)) {
b.exit_pip(key);
}
})
.ok();
})
.detach();
})
.ok();
})
.detach();
}

/// The switch is on, the tab is not muted in Tarn, and no script of Tarn's may run under Lockdown.
fn may_pip(&self, key: TabKey, view: &engine::WebView) -> bool {
let url = view.url().map(|u| u.to_string()).unwrap_or_default();
self.pref(crate::prefs::Pref::AutoPictureInPicture) && !view.is_muted() && self.level_for_tab(key, &url) != shield::ShieldLevel::Lockdown
}

fn exit_pip(&self, key: TabKey) {
if let Some(view) = self.views.get(&key) {
view.picture_in_picture(PipMode::Exit, |_| {});
}
}

fn press_media(&mut self, control: Control, cx: &mut Context<Self>) {
Expand Down
7 changes: 6 additions & 1 deletion crates/app/src/prefs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@ pub enum Pref {
AgentControl,
AgentProfile,
AgentTabs,
AutoPictureInPicture,
}

impl Pref {
pub const ALL: [Pref; 29] = [
pub const ALL: [Pref; 30] = [
Pref::CompactToolbar,
Pref::AutoHideSidebar,
Pref::Gestures,
Expand Down Expand Up @@ -70,6 +71,7 @@ impl Pref {
Pref::AgentControl,
Pref::AgentProfile,
Pref::AgentTabs,
Pref::AutoPictureInPicture,
];

pub(crate) fn key(self) -> &'static str {
Expand Down Expand Up @@ -103,6 +105,7 @@ impl Pref {
Pref::AgentControl => browser_core::agent::SETTING,
Pref::AgentProfile => browser_core::agent::SETTING_PROFILE,
Pref::AgentTabs => browser_core::agent::SETTING_TABS,
Pref::AutoPictureInPicture => browser_core::prefs::AUTO_PICTURE_IN_PICTURE.key,
}
}

Expand All @@ -124,6 +127,7 @@ impl Pref {
| Pref::LocalhostFromApps
| Pref::Passkeys
| Pref::FeatureHints
| Pref::AutoPictureInPicture
)
}

Expand Down Expand Up @@ -513,6 +517,7 @@ mod tests {
(Pref::TearOffTemporary, prefs::TEAR_OFF_TEMPORARY),
(Pref::FeatureHints, prefs::FEATURE_HINTS),
(Pref::CompactDensity, prefs::COMPACT),
(Pref::AutoPictureInPicture, prefs::AUTO_PICTURE_IN_PICTURE),
] {
assert_eq!((pref.key(), pref.default()), (switch.key, switch.default), "{pref:?}");
}
Expand Down
8 changes: 8 additions & 0 deletions crates/app/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1127,6 +1127,14 @@ impl SettingsWindow {
},
s.prefs.get(Pref::Gestures),
cx,
))
.child(self.pref_row(
false,
Pref::AutoPictureInPicture,
"Picture in Picture when switching tabs",
"A playing video with sound keeps playing in a floating window while you are in another tab, and goes back into its page when you return.",
s.prefs.get(Pref::AutoPictureInPicture),
cx,
)),
)
.into_any_element()
Expand Down
59 changes: 59 additions & 0 deletions crates/browser-core/src/media.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//! ask `page_ops::MediaState` of a tab once the engine says it plays, and again every `POLL` only
//! while the shown media plays, so nothing ticks while nothing plays.

use std::collections::HashSet;
use std::time::Duration;

use engine::page_scripts::MediaCommand;
Expand Down Expand Up @@ -111,6 +112,41 @@ impl Player {
}
}

/// The tabs whose video Tarn moved into Picture in Picture when they left the screen, so only
/// those come back out when the tab returns; one the user moved there stays.
#[derive(Clone, Debug, Default)]
pub struct AutoPip {
entered: HashSet<TabKey>,
screen: Vec<TabKey>,
}

impl AutoPip {
/// The tabs on screen are now `now`: answers those that left it, whose playing video should
/// move into Picture in Picture, and those Tarn moved there that are back, whose video
/// should come out.
pub fn screen(&mut self, now: Vec<TabKey>) -> (Vec<TabKey>, Vec<TabKey>) {
let left = self.screen.iter().copied().filter(|k| !now.contains(k)).collect();
let back = now.iter().copied().filter(|k| self.entered.remove(k)).collect();
self.screen = now;
(left, back)
}

/// `key`'s page answered `result` to `PipMode::Enter`; true when its video should come back out
/// at once, because the tab is `visible` again before the answer arrived.
pub fn answered(&mut self, key: TabKey, result: &str, visible: bool) -> bool {
let entered = result == "pip";
if entered && !visible {
self.entered.insert(key);
}
entered && visible
}

/// True once for a tab Tarn moved into Picture in Picture: on its return, or when it goes.
pub fn take(&mut self, key: TabKey) -> bool {
self.entered.remove(&key)
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -150,6 +186,29 @@ mod tests {
assert!(player.forget(key(2)) && player.tab().is_none());
}

#[test]
fn tabs_leave_and_return_once_each() {
let mut pip = AutoPip::default();
assert_eq!(pip.screen(vec![key(1), key(2)]), (vec![], vec![]));
assert_eq!(pip.screen(vec![key(1), key(2)]), (vec![], vec![]), "nothing changed");
assert_eq!(pip.screen(vec![key(3)]), (vec![key(1), key(2)], vec![]), "a whole split leaves");
pip.answered(key(1), "pip", false);
assert_eq!(pip.screen(vec![key(1)]), (vec![key(3)], vec![key(1)]));
assert_eq!(pip.screen(vec![key(1), key(2)]), (vec![], vec![]), "back out only once");
}

#[test]
fn only_video_tarn_moved_comes_back_out() {
let mut pip = AutoPip::default();
assert!(!pip.answered(key(1), "pip", false));
assert!(!pip.answered(key(2), "open", false), "the user's own window is not Tarn's");
assert!(!pip.answered(key(3), "refused", false));
assert!(pip.take(key(1)) && !pip.take(key(1)), "back out once");
assert!(!pip.take(key(2)) && !pip.take(key(3)));
assert!(pip.answered(key(4), "pip", true), "back on screen before the answer: out at once");
assert!(!pip.take(key(4)));
}

#[test]
fn the_visible_tab_shows_no_mini_player() {
let mut player = Player::default();
Expand Down
2 changes: 2 additions & 0 deletions crates/browser-core/src/prefs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ pub const REDUCE_MOTION: Switch = Switch { key: "reduce_motion", default: false
pub const NEW_TAB_START_PAGE: Switch = Switch { key: "new_tab_start_page", default: false };
pub const CLOSE_SELECTS_PREVIOUS: Switch = Switch { key: "close_selects_previous", default: false };
pub const TEAR_OFF_TEMPORARY: Switch = Switch { key: "tear_off_temporary", default: true };
/// A playing video moves into Picture in Picture when its tab leaves the screen, and back after.
pub const AUTO_PICTURE_IN_PICTURE: Switch = Switch { key: "auto_picture_in_picture", default: true };
/// Settings' switch for the toasts that name a command's shortcut (`hints`), on every client.
pub const FEATURE_HINTS: Switch = Switch { key: "feature_hints", default: true };
/// Settings' Appearance switch for compact density: tighter sidebar and tab strip rows on every client.
Expand Down
23 changes: 21 additions & 2 deletions crates/engine/src/chromium/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2295,8 +2295,27 @@ impl WebView {
}
}

pub fn picture_in_picture(&self, done: impl Fn(String) + 'static) {
self.run_isolated_as_gesture(crate::page_scripts::PICTURE_IN_PICTURE, true, done);
/// Only entering needs the user's gesture, which the page shares: the automatic `Enter` lends
/// it only once a check without one found a video to take, and `Exit` lends none. The automatic
/// modes run no script of Tarn's under Lockdown.
pub fn picture_in_picture(&self, mode: crate::page_scripts::PipMode, done: impl Fn(String) + 'static) {
use crate::page_scripts::{PipMode, picture_in_picture_ready_script, picture_in_picture_script};
if mode != PipMode::Toggle && self.lockdown_view() {
return done("refused".into());
}
if mode != PipMode::Enter {
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
return self.run_isolated_as_gesture(&picture_in_picture_script(mode), mode == PipMode::Toggle, done);
}
let (inner, done) = (Rc::downgrade(&self.0), Rc::new(done));
self.run_isolated(&picture_in_picture_ready_script(), move |answer| match (answer.as_str(), inner.upgrade()) {
// Back on screen before the check answered: no gesture for a video that stays inline.
("ready", Some(inner)) if inner.visible.get() => done("none".into()),
("ready", Some(inner)) => {
let done = done.clone();
WebView(inner).run_isolated_as_gesture(&picture_in_picture_script(PipMode::Enter), true, move |a| done(a));
}
_ => done(answer),
});
}

/// The visible page as PNG, scaled to `width` points when given.
Expand Down
Loading
Loading