A tiny friend in your Mac's notch and on your iPhone that keeps an eye on your AI coding agents: Claude Code, Codex, Cursor, Gemini CLI, Antigravity and more. Approve from the notch or your Lock Screen.
Swift ai-agentsanthropicantigravityclaudeclaude-codecodexcursordynamic-islandgemini-cliiosiphonlinuxmacosmacos-appmenubar-appnotchopen-sourceswiftswiftuiwindows
Desktop Mochi: his own window, placed and polled from Rust
Claude64bb85e2d216
diff --git a/windows/src-tauri/capabilities/default.json b/windows/src-tauri/capabilities/default.jsonindex 38b8f5df..c869302e 100644--- a/windows/src-tauri/capabilities/default.json+++ b/windows/src-tauri/capabilities/default.json@@ -2,6 +2,6 @@"$schema": "../gen/schemas/desktop-schema.json","identifier": "default","description": "What Coucou's windows are allowed to do. Everything that touches the OS goes through Coucou's own commands.",- "windows": ["island", "settings"],+ "windows": ["island", "settings", "mochi"],"permissions": ["core:default", "autostart:default"]}diff --git a/windows/src-tauri/src/desktop.rs b/windows/src-tauri/src/desktop.rsnew file mode 100644index 00000000..5b7e6509--- /dev/null+++ b/windows/src-tauri/src/desktop.rs@@ -0,0 +1,1007 @@+// Mochi on the desktop: his own small window, wherever the user dropped him.+// Window side of DesktopMochi.swift, plus the geometry half of+// DesktopMochiLogic.swift (`logic` below, with its tests).+//+// The island page runs his life cycle (src/island/desktop.ts): fly out, back to+// the island for an alert, home. This file owns the window — where it is, which+// part of it takes the mouse, the flights, the drag, and the saved spot.+//+// How the window is placed depends on the system (platform::DesktopMode):+// * Windows: anywhere, in physical desktop pixels. A cursor poll — running+// only while he is awake on the desktop — makes the window click-through+// except over his body, feeds his eyes, and carries him during a drag.+// * Linux X11: the same coordinates; the input region is his body, and the+// page drives the drag.+// * Linux layer-shell: a layer surface on the island's display, placed with+// margins in logical pixels.+// * GNOME on Wayland: off, nothing is created.++use std::sync::atomic::{AtomicU64, Ordering};+use std::sync::{Arc, Mutex};+use std::time::Duration;++use serde::Serialize;+use tauri::{AppHandle, Emitter, Manager, PhysicalPosition, State, WebviewUrl, WebviewWindow, WebviewWindowBuilder};++use crate::island::{self, PollGate};+use crate::platform::{self, DesktopMode, MouseShape};+use crate::settings::{self, DesktopSpot};++pub const LABEL: &str = "mochi";++/// Side of the square window, logical pixels (DesktopMochiLogic.panelSize).+pub const SIZE: f64 = 120.0;+/// Kept between him and the edges of the work area (DesktopMochiLogic.clampMargin).+const MARGIN: f64 = 24.0;+/// To and from the island (DesktopMochi.swift: 0.45 s, ease in-out).+const FLIGHT_MS: u64 = 450;+/// Settling into his spot after a drop (0.25 s, with a little overshoot).+const SNAP_MS: u64 = 250;+const TICK_MS: u64 = 16;++/// Windows never hides his window: it parks it off-screen. tao shows a hidden+/// window again with SW_SHOW, which may take the keyboard focus from whatever+/// the user is typing in each time he flies out; a window created visible+/// (shown without activation) and only ever moved never is. Same as the+/// island, which is never re-shown either.+const PARK: bool = cfg!(windows);+const PARKED: (f64, f64) = (-32000.0, -32000.0);++// ── Pure geometry ─────────────────────────────────────────────────────────────++pub mod logic {+ /// A rectangle, y down, in whatever space the caller works in.+ #[derive(Clone, Copy, Debug, PartialEq)]+ pub struct Rect {+ pub x: f64,+ pub y: f64,+ pub w: f64,+ pub h: f64,+ }++ impl Rect {+ pub fn contains(&self, p: (f64, f64)) -> bool {+ p.0 >= self.x && p.0 < self.x + self.w && p.1 >= self.y && p.1 < self.y + self.h+ }++ pub fn center(&self) -> (f64, f64) {+ (self.x + self.w / 2.0, self.y + self.h / 2.0)+ }+ }++ /// A connected display: its whole area, the part windows may use (no+ /// taskbar), and how many of the space's units make one logical pixel.+ #[derive(Clone, Copy, Debug, PartialEq)]+ pub struct Display {+ pub frame: Rect,+ pub work: Rect,+ pub scale: f64,+ }++ /// Radius of the clickable body, as a fraction of the window side.+ pub const BODY_RADIUS_FRACTION: f64 = 0.24;++ /// Island panel, logical pixels: dropping Mochi on it brings him home.+ pub const HOME_ZONE_W: f64 = crate::island::PANEL_W;+ pub const HOME_ZONE_H: f64 = crate::island::PANEL_H;++ /// Hit test of the round body inside the square window (window-local).+ pub fn is_over_body(local: (f64, f64), size: f64) -> bool {+ let c = size / 2.0;+ let r = size * BODY_RADIUS_FRACTION;+ let (dx, dy) = (local.0 - c, local.1 - c);+ dx * dx + dy * dy <= r * r+ }++ /// Keeps a window of side `size` inside `work`, `margin` from each edge.+ pub fn clamp_origin(origin: (f64, f64), size: f64, work: Rect, margin: f64) -> (f64, f64) {+ (+ origin.0.max(work.x + margin).min(work.x + work.w - size - margin),+ origin.1.max(work.y + margin).min(work.y + work.h - size - margin),+ )+ }++ /// The display whose frame holds `p`, or else the one whose centre is closest.+ pub fn display_near(p: (f64, f64), displays: &[Display]) -> Option<Display> {+ displays.iter().copied().find(|d| d.frame.contains(p)).or_else(|| {+ displays.iter().copied().min_by(|a, b| {+ let da = dist2(p, a.work.center());+ let db = dist2(p, b.work.center());+ da.total_cmp(&db)+ })+ })+ }++ fn dist2(a: (f64, f64), b: (f64, f64)) -> f64 {+ (a.0 - b.0).powi(2) + (a.1 - b.1).powi(2)+ }++ /// Where a window dropped with its top-left at `origin` settles: inside the+ /// work area of the display under its centre (or the nearest one).+ pub fn settle(origin: (f64, f64), displays: &[Display], size: f64, margin: f64) -> (f64, f64) {+ let Some(first) = displays.first() else { return origin };+ let half = size * first.scale / 2.0;+ let Some(d) = display_near((origin.0 + half, origin.1 + half), displays) else {+ return origin;+ };+ clamp_origin(origin, size * d.scale, d.work, margin * d.scale)+ }++ /// A spot saved at an earlier launch, checked against the displays+ /// connected now: `None` when none of them holds its centre any more (the+ /// monitor was unplugged) — he then stays in the island. A spot that is+ /// still on a display is pulled back inside its work area.+ pub fn restore_spot(+ spot: (f64, f64),+ displays: &[Display],+ size: f64,+ margin: f64,+ ) -> Option<(f64, f64)> {+ displays.iter().find_map(|d| {+ let s = size * d.scale;+ let center = (spot.0 + s / 2.0, spot.1 + s / 2.0);+ d.frame+ .contains(center)+ .then(|| clamp_origin(spot, s, d.work, margin * d.scale))+ })+ }++ /// First visit: the bottom-right corner of the work area, like the Mac.+ pub fn default_spot(d: &Display, size: f64, margin: f64) -> (f64, f64) {+ let s = size * d.scale;+ let m = margin * d.scale;+ (d.work.x + d.work.w - s - m, d.work.y + d.work.h - s - m)+ }++ /// The island's panel: centred on the island, from the top of its display.+ pub fn home_zone(island_center_x: f64, island_top: f64, scale: f64) -> Rect {+ Rect {+ x: island_center_x - HOME_ZONE_W * scale / 2.0,+ y: island_top,+ w: HOME_ZONE_W * scale,+ h: HOME_ZONE_H * scale,+ }+ }++ /// Top-left corner of the window when Mochi sits in the island: centred+ /// under the top edge, where the flights start and end.+ pub fn island_spot(island_center_x: f64, island_top: f64, size: f64, scale: f64) -> (f64, f64) {+ (island_center_x - size * scale / 2.0, island_top)+ }++ pub fn ease_in_out(t: f64) -> f64 {+ if t < 0.5 {+ 4.0 * t * t * t+ } else {+ 1.0 - (-2.0 * t + 2.0).powi(3) / 2.0+ }+ }++ /// Ease-out with a small overshoot: the landing squash.+ pub fn ease_out_back(t: f64) -> f64 {+ let c1 = 1.3;+ let c3 = c1 + 1.0;+ 1.0 + c3 * (t - 1.0).powi(3) + c1 * (t - 1.0).powi(2)+ }++ pub fn lerp(a: (f64, f64), b: (f64, f64), t: f64) -> (f64, f64) {+ (a.0 + (b.0 - a.0) * t, a.1 + (b.1 - a.1) * t)+ }+}++use logic::{Display, Rect};++// ── State ─────────────────────────────────────────────────────────────────────++#[derive(Clone, Copy, PartialEq, Eq, Debug, Serialize)]+#[serde(rename_all = "camelCase")]+pub enum Source {+ /// Dragged out of the island.+ Island,+ /// Picked up where he sat on the desktop.+ Desktop,+}++#[derive(Clone, Copy)]+struct Carry {+ /// Cursor minus the window's top-left at pick-up, physical (Poll mode).+ grab: (f64, f64),+ from: Source,+}++#[derive(Default)]+struct Inner {+ /// The window is on screen.+ shown: bool,+ /// Sitting on the desktop, taking pokes (not flying, not carried).+ landed: bool,+ asleep: bool,+ carry: Option<Carry>,+ /// Layer mode: the surface covers the display while it is dragged.+ overlay: bool,+ /// Top-left corner, in the mode's space.+ pos: (f64, f64),+ /// Layer mode: logical size of the display he lives on.+ display: Option<(f64, f64)>,+ /// Poll mode: the click-through state last applied.+ ignoring: Option<bool>,+}++pub struct Desktop {+ pub mode: DesktopMode,+ gate: PollGate,+ inner: Mutex<Inner>,+ /// Bumped by every flight: an older one still running stops moving him.+ flight: AtomicU64,+}++impl Desktop {+ fn new(mode: DesktopMode) -> Self {+ Self { mode, gate: PollGate::new(), inner: Mutex::new(Inner::default()), flight: AtomicU64::new(0) }+ }+}++#[derive(Serialize, Clone)]+#[serde(rename_all = "camelCase")]+struct Dropped {+ from: Source,+ home: bool,+}++#[derive(Serialize, Clone)]+struct Cursor {+ x: f64,+ y: f64,+}++#[derive(Serialize)]+#[serde(rename_all = "camelCase")]+pub struct DesktopInfo {+ /// "poll" | "window" | "layer" | "off".+ mode: &'static str,+ /// He was on the desktop when the app last quit.+ on_desktop: bool,+}++pub fn window(app: &AppHandle) -> Option<WebviewWindow> {+ app.get_webview_window(LABEL)+}++fn body_disc() -> MouseShape {+ MouseShape::Disc { cx: SIZE / 2.0, cy: SIZE / 2.0, r: SIZE * logic::BODY_RADIUS_FRACTION }+}++// ── Setup ─────────────────────────────────────────────────────────────────────++/// Same reasoning as the settings window: created hidden at launch, before the+/// island's webview, then only shown and hidden. Nothing is created where the+/// feature is off.+pub fn setup(app: &AppHandle) {+ let mut mode = platform::desktop_mode();+ if mode != DesktopMode::Off {+ match create_window(app, mode) {+ Some(win) if platform::prepare_desktop_window(&win, mode) => {}+ Some(win) => {+ let _ = win.destroy();+ mode = DesktopMode::Off;+ }+ None => mode = DesktopMode::Off,+ }+ }+ crate::log::line(format!("desktop Mochi: {}", mode.as_str()));+ let desktop = Arc::new(Desktop::new(mode));+ app.manage(desktop.clone());+ // Nothing takes the mouse until he is out.+ refresh(app, &desktop);+ if mode == DesktopMode::Poll {+ spawn_poll(app.clone(), desktop);+ }+}++fn page_url(app: &AppHandle) -> WebviewUrl {+ #[cfg(dev)]+ if let Some(mut base) = app.config().build.dev_url.clone() {+ base.set_path("/mochi.html");+ return WebviewUrl::External(base);+ }+ let _ = app;+ WebviewUrl::App("mochi.html".into())+}++fn create_window(app: &AppHandle, _mode: DesktopMode) -> Option<WebviewWindow> {+ let mut builder = WebviewWindowBuilder::new(app, LABEL, page_url(app))+ .additional_browser_args(crate::BROWSER_ARGS)+ .title("Mochi")+ .inner_size(SIZE, SIZE)+ // GTK won't size a non-resizable window below its natural size (see+ // island::apply_geometry). Windows would grow resize borders instead.+ .resizable(cfg!(target_os = "linux"))+ .decorations(false)+ .transparent(true)+ .shadow(false)+ .always_on_top(true)+ .skip_taskbar(true)+ .visible_on_all_workspaces(true)+ .focused(false)+ .maximizable(false)+ .minimizable(false)+ .closable(false)+ .disable_drag_drop_handler()+ .focusable(false)+ .visible(PARK);+ if PARK {+ builder = builder.position(PARKED.0, PARKED.1);+ }+ let built = builder.build();+ match built {+ Ok(win) => Some(win),+ Err(err) => {+ crate::log::line(format!("desktop Mochi window failed: {err}"));+ None+ }+ }+}++// ── Coordinates ───────────────────────────────────────────────────────────────++/// Connected displays in the mode's space.+fn displays(app: &AppHandle, d: &Desktop) -> Vec<Display> {+ if d.mode == DesktopMode::Layer {+ let (w, h) = d.inner.lock().unwrap().display.unwrap_or((1920.0, 1080.0));+ let frame = Rect { x: 0.0, y: 0.0, w, h };+ return vec![Display { frame, work: frame, scale: 1.0 }];+ }+ let Ok(monitors) = app.available_monitors() else { return Vec::new() };+ monitors+ .iter()+ .map(|m| {+ let (p, s, wa) = (m.position(), m.size(), m.work_area());+ Display {+ frame: Rect { x: p.x as f64, y: p.y as f64, w: s.width as f64, h: s.height as f64 },+ work: Rect {+ x: wa.position.x as f64,+ y: wa.position.y as f64,+ w: wa.size.width as f64,+ h: wa.size.height as f64,+ },+ scale: m.scale_factor(),+ }+ })+ .collect()+}++/// The island's top-centre point and the scale there, in the mode's space.+fn island_anchor(app: &AppHandle, d: &Desktop) -> Option<(f64, f64, f64)> {+ if d.mode == DesktopMode::Layer {+ let (w, _) = d.inner.lock().unwrap().display?;+ return Some((w / 2.0, 0.0, 1.0));+ }+ let win = island::window(app)?;+ let pos = win.outer_position().ok()?;+ let size = win.outer_size().ok()?;+ let scale = win.scale_factor().unwrap_or(1.0);+ Some((pos.x as f64 + size.width as f64 / 2.0, pos.y as f64, scale))+}++/// The window side in the mode's space.+fn side(app: &AppHandle, d: &Desktop) -> f64 {+ if d.mode == DesktopMode::Layer {+ return SIZE;+ }+ SIZE * window(app).and_then(|w| w.scale_factor().ok()).unwrap_or(1.0)+}++/// Asks the GTK main thread for the island's display (layer mode), and waits.+/// Never called from the main thread itself: every caller is a command that+/// runs on Tauri's async pool.+fn refresh_layer_display(app: &AppHandle, d: &Desktop) {+ if d.mode != DesktopMode::Layer {+ return;+ }+ let (Some(island), Some(mochi)) = (island::window(app), window(app)) else { return };+ let (tx, rx) = std::sync::mpsc::channel();+ let _ = app.run_on_main_thread(move || {+ let _ = tx.send(platform::layer_display(&island, &mochi));+ });+ if let Ok(Some(size)) = rx.recv_timeout(Duration::from_secs(1)) {+ d.inner.lock().unwrap().display = Some(size);+ }+}++/// Island-window client coordinates (logical) → the mode's space.+fn from_island_client(app: &AppHandle, d: &Desktop, x: f64, y: f64) -> Option<(f64, f64)> {+ let island = island::window(app)?;+ match d.mode {+ DesktopMode::Layer => {+ // The island is anchored to the top edge and centred.+ let (dw, _) = d.inner.lock().unwrap().display?;+ let scale = island.scale_factor().unwrap_or(1.0);+ let iw = island.inner_size().ok()?.width as f64 / scale;+ Some(((dw - iw) / 2.0 + x, y))+ }+ DesktopMode::Window => {+ let pos = island.outer_position().ok()?;+ let scale = island.scale_factor().unwrap_or(1.0);+ Some((pos.x as f64 + x * scale, pos.y as f64 + y * scale))+ }+ _ => None,+ }+}++// ── Window operations ─────────────────────────────────────────────────────────++fn place(app: &AppHandle, d: &Desktop, pos: (f64, f64)) {+ d.inner.lock().unwrap().pos = pos;+ let Some(win) = window(app) else { return };+ match d.mode {+ DesktopMode::Poll | DesktopMode::Window => {+ let _ = win.set_position(PhysicalPosition::new(pos.0.round() as i32, pos.1.round() as i32));+ }+ DesktopMode::Layer => {+ let _ = app.run_on_main_thread(move || platform::set_layer_margins(&win, pos.0, pos.1));+ }+ DesktopMode::Off => {}+ }+}++fn show(app: &AppHandle, d: &Desktop, at: (f64, f64)) {+ place(app, d, at);+ d.inner.lock().unwrap().shown = true;+ if let Some(win) = window(app) {+ if !PARK {+ let _ = win.show();+ }+ let _ = win.set_always_on_top(true);+ }+ refresh(app, d);+ // The page draws only while he is on screen.+ let _ = app.emit_to(LABEL, "desktop-visible", true);+}++fn hide(app: &AppHandle, d: &Desktop) {+ {+ let mut i = d.inner.lock().unwrap();+ i.shown = false;+ i.landed = false;+ i.carry = None;+ i.asleep = false;+ }+ refresh(app, d);+ if PARK {+ place(app, d, PARKED);+ } else if let Some(win) = window(app) {+ let _ = win.hide();+ }+ let _ = app.emit_to(LABEL, "desktop-visible", false);+}++/// Re-derives the poll gate and what takes the mouse from the current state.+fn refresh(app: &AppHandle, d: &Desktop) {+ let (active, ignore, shape) = {+ let mut i = d.inner.lock().unwrap();+ let active = i.shown && (i.carry.is_some() || (i.landed && !i.asleep));+ // Poll mode: Some(flag) is decided here; None leaves it to the poll.+ let ignore = if !i.shown || (!i.landed && i.carry.is_none()) {+ Some(true)+ } else if let Some(c) = i.carry {+ Some(c.from == Source::Island)+ } else if i.asleep {+ // Asleep, nothing polls: the whole little window takes the mouse+ // and acts as his own wake area — the first move over it wakes+ // him, and the poll gives the rest back right away.+ Some(false)+ } else {+ None+ };+ if ignore.is_none() {+ i.ignoring = None;+ }+ let shape = if !i.shown || (!i.landed && i.carry.is_none()) {+ MouseShape::Empty+ } else if i.overlay {+ MouseShape::Whole+ } else if matches!(i.carry, Some(Carry { from: Source::Island, .. })) {+ MouseShape::Empty+ } else {+ body_disc()+ };+ (active, ignore, shape)+ };+ let Some(win) = window(app) else { return };+ match d.mode {+ DesktopMode::Poll => {+ if let Some(flag) = ignore {+ d.inner.lock().unwrap().ignoring = Some(flag);+ let _ = win.set_ignore_cursor_events(flag);+ }+ d.gate.set_active(active);+ }+ DesktopMode::Window | DesktopMode::Layer => {+ let _ = app.run_on_main_thread(move || platform::set_desktop_shape(&win, shape));+ }+ DesktopMode::Off => {}+ }+}++/// Moves him from where he is to `to`, then lands him. False when another+/// flight took over before the end.+async fn fly(app: &AppHandle, d: &Desktop, to: (f64, f64), ms: u64, ease: fn(f64) -> f64) -> bool {+ let token = d.flight.fetch_add(1, Ordering::SeqCst) + 1;+ let from = d.inner.lock().unwrap().pos;+ let (tx, rx) = tokio::sync::oneshot::channel();+ let app2 = app.clone();+ std::thread::spawn(move || {+ let d = app2.state::<Arc<Desktop>>();+ let steps = (ms / TICK_MS).max(1);+ for k in 1..=steps {+ if d.flight.load(Ordering::SeqCst) != token {+ let _ = tx.send(false);+ return;+ }+ let t = k as f64 / steps as f64;+ place(&app2, &d, logic::lerp(from, to, ease(t)));+ std::thread::sleep(Duration::from_millis(TICK_MS));+ }+ let _ = tx.send(d.flight.load(Ordering::SeqCst) == token);+ });+ rx.await.unwrap_or(false)+}++/// Saves what changed about him in the preferences.+fn remember(app: &AppHandle, change: impl FnOnce(&mut settings::DesktopMochiPref)) {+ let Some(shared) = app.try_state::<crate::Shared>() else { return };+ let snapshot = {+ let mut s = shared.settings.lock().unwrap();+ change(&mut s.desktop_mochi);+ s.clone()+ };+ if let Err(err) = settings::save(&snapshot) {+ crate::log::line(format!("could not save settings: {err}"));+ }+}++fn remember_spot(app: &AppHandle, d: &Desktop, pos: (f64, f64), on_desktop: bool) {+ let space = d.mode.space().to_string();+ remember(app, |p| {+ p.on_desktop = on_desktop;+ p.spot = Some(DesktopSpot { x: pos.0, y: pos.1, space });+ });+}++/// Where he lands when he flies out, or `None` when his spot is on a display+/// that is no longer connected.+fn target_spot(app: &AppHandle, d: &Desktop) -> Option<(f64, f64)> {+ let all = displays(app, d);+ let saved = app.try_state::<crate::Shared>().and_then(|s| s.settings.lock().unwrap().desktop_mochi.spot.clone());+ match saved {+ Some(spot) if spot.space == d.mode.space() => {+ logic::restore_spot((spot.x, spot.y), &all, SIZE, MARGIN)+ }+ // Never placed, or placed under another kind of session: a fresh start+ // in the corner of the island's display.+ _ => {+ let (cx, top, _) = island_anchor(app, d)?;+ let home = logic::display_near((cx, top + 1.0), &all)?;+ Some(logic::default_spot(&home, SIZE, MARGIN))+ }+ }+}++/// The drop, wherever it came from: home if it is over the island's panel,+/// otherwise his new spot.+async fn finish_drop(app: &AppHandle, d: &Desktop, pos: (f64, f64), from: Source) {+ let s = side(app, d);+ let center = (pos.0 + s / 2.0, pos.1 + s / 2.0);+ let home = island_anchor(app, d)+ .map(|(cx, top, scale)| logic::home_zone(cx, top, scale).contains(center))+ .unwrap_or(false);+ {+ let mut i = d.inner.lock().unwrap();+ i.carry = None;+ i.landed = !home;+ }+ if home {+ if from == Source::Island {+ // Back where he came from: he simply reappears in the island.+ hide(app, d);+ } else {+ // The island page flies him home (and forgets the spot).+ refresh(app, d);+ }+ } else {+ let spot = logic::settle(pos, &displays(app, d), SIZE, MARGIN);+ refresh(app, d);+ if spot != pos {+ fly(app, d, spot, SNAP_MS, logic::ease_out_back).await;+ }+ remember_spot(app, d, spot, true);+ }+ let _ = app.emit_to(island::WINDOW_LABEL, "desktop-mochi-dropped", Dropped { from, home });+}++// ── Poll (Windows) ────────────────────────────────────────────────────────────++/// Runs only while he is awake on the desktop or being carried: click-through+/// from the body hit test, the cursor for his eyes, and the drag.+fn spawn_poll(app: AppHandle, d: Arc<Desktop>) {+ std::thread::spawn(move || loop {+ d.gate.wait_until_active();+ let mut last = (f64::MIN, f64::MIN);+ while d.gate.is_active() {+ std::thread::sleep(Duration::from_millis(TICK_MS));+ let Some(win) = window(&app) else { continue };+ let Some((cx, cy)) = platform::cursor_physical() else { continue };+ let carry = d.inner.lock().unwrap().carry;++ if let Some(c) = carry {+ let pos = (cx - c.grab.0, cy - c.grab.1);+ if platform::left_button_down() {+ if pos != d.inner.lock().unwrap().pos {+ place(&app, &d, pos);+ }+ } else {+ // Released: hand the drop to the async pool, where the+ // snap animation may wait.+ d.inner.lock().unwrap().carry = None;+ let app2 = app.clone();+ let d2 = d.clone();+ tauri::async_runtime::spawn(async move { finish_drop(&app2, &d2, pos, c.from).await });+ }+ continue;+ }+ // Between a release and the drop being settled, nothing to decide.+ if !d.inner.lock().unwrap().landed {+ continue;+ }++ let Ok(origin) = win.outer_position() else { continue };+ let scale = win.scale_factor().unwrap_or(1.0);+ let local = ((cx - origin.x as f64) / scale, (cy - origin.y as f64) / scale);+ let accept = logic::is_over_body(local, SIZE);+ {+ let mut i = d.inner.lock().unwrap();+ if i.ignoring != Some(!accept) {+ i.ignoring = Some(!accept);+ let _ = win.set_ignore_cursor_events(!accept);+ }+ }+ if (local.0 - last.0).abs() >= 1.0 || (local.1 - last.1).abs() >= 1.0 {+ last = local;+ let _ = win.emit("desktop-cursor", Cursor { x: local.0, y: local.1 });+ }+ }+ });+}++// ── Commands ──────────────────────────────────────────────────────────────────++#[tauri::command]+pub fn desktop_mochi_info(app: AppHandle, desktop: State<Arc<Desktop>>) -> DesktopInfo {+ let on_desktop = app+ .try_state::<crate::Shared>()+ .map(|s| s.settings.lock().unwrap().desktop_mochi.on_desktop)+ .unwrap_or(false);+ DesktopInfo { mode: desktop.mode.as_str(), on_desktop: on_desktop && desktop.mode != DesktopMode::Off }+}++/// Dragging Mochi out of the island: the window appears under the pointer+/// (`x`, `y`: island-window client coordinates) and follows it — the cursor+/// poll carries it on Windows, `desktop_mochi_carry` elsewhere.+#[tauri::command]+pub async fn desktop_mochi_pick_up(app: AppHandle, x: f64, y: f64) -> bool {+ let d = app.state::<Arc<Desktop>>().inner().clone();+ if d.mode == DesktopMode::Off || window(&app).is_none() || d.inner.lock().unwrap().shown {+ return false;+ }+ refresh_layer_display(&app, &d);+ let s = side(&app, &d);+ let center = match d.mode {+ DesktopMode::Poll => platform::cursor_physical(),+ _ => from_island_client(&app, &d, x, y),+ };+ let Some(center) = center else { return false };+ d.flight.fetch_add(1, Ordering::SeqCst);+ d.inner.lock().unwrap().carry = Some(Carry { grab: (s / 2.0, s / 2.0), from: Source::Island });+ show(&app, &d, (center.0 - s / 2.0, center.1 - s / 2.0));+ true+}++/// Linux: the pointer moved while Mochi is being dragged out of the island.+#[tauri::command]+pub async fn desktop_mochi_carry(app: AppHandle, x: f64, y: f64) {+ let d = app.state::<Arc<Desktop>>().inner().clone();+ if d.mode == DesktopMode::Poll || d.inner.lock().unwrap().carry.is_none() {+ return;+ }+ let s = side(&app, &d);+ if let Some(c) = from_island_client(&app, &d, x, y) {+ place(&app, &d, (c.0 - s / 2.0, c.1 - s / 2.0));+ }+}++/// Linux: the button went up while Mochi was being dragged out of the island.+#[tauri::command]+pub async fn desktop_mochi_carry_end(app: AppHandle, x: f64, y: f64) {+ let d = app.state::<Arc<Desktop>>().inner().clone();+ if d.mode == DesktopMode::Poll || d.inner.lock().unwrap().carry.is_none() {+ return;+ }+ let s = side(&app, &d);+ let pos = from_island_client(&app, &d, x, y)+ .map(|c| (c.0 - s / 2.0, c.1 - s / 2.0))+ .unwrap_or_else(|| d.inner.lock().unwrap().pos);+ finish_drop(&app, &d, pos, Source::Island).await;+}++/// The page saw a drag start on Mochi. Returns his top-left corner in the+/// mode's space, which the page needs to draw him in the overlay (layer mode).+#[tauri::command]+pub async fn desktop_mochi_drag_begin(app: AppHandle) -> Option<(f64, f64)> {+ let d = app.state::<Arc<Desktop>>().inner().clone();+ let pos = {+ let i = d.inner.lock().unwrap();+ if !i.landed || i.carry.is_some() {+ return None;+ }+ i.pos+ };+ // A drag takes over from a snap still settling him.+ d.flight.fetch_add(1, Ordering::SeqCst);+ match d.mode {+ DesktopMode::Poll => {+ let win = window(&app)?;+ let (cx, cy) = platform::cursor_physical()?;+ let origin = win.outer_position().ok()?;+ let grab = (cx - origin.x as f64, cy - origin.y as f64);+ d.inner.lock().unwrap().carry = Some(Carry { grab, from: Source::Desktop });+ refresh(&app, &d);+ }+ DesktopMode::Layer => {+ d.inner.lock().unwrap().overlay = true;+ let win = window(&app)?;+ let _ = app.run_on_main_thread(move || {+ platform::set_layer_overlay(&win, true);+ platform::set_desktop_shape(&win, MouseShape::Whole);+ });+ }+ _ => {}+ }+ Some(pos)+}++/// X11: the page moves the window during its drag (top-left, physical pixels).+#[tauri::command]+pub async fn desktop_mochi_drag_move(app: AppHandle, x: f64, y: f64) {+ let d = app.state::<Arc<Desktop>>().inner().clone();+ if d.mode == DesktopMode::Window && d.inner.lock().unwrap().landed {+ place(&app, &d, (x, y));+ }+}++/// Linux: the page's drag ended with the top-left corner at (`x`, `y`), in the+/// mode's space. On Windows the poll sees the release itself.+#[tauri::command]+pub async fn desktop_mochi_drag_end(app: AppHandle, x: f64, y: f64) {+ let d = app.state::<Arc<Desktop>>().inner().clone();+ if d.mode == DesktopMode::Poll || !d.inner.lock().unwrap().landed {+ return;+ }+ if d.mode == DesktopMode::Layer {+ d.inner.lock().unwrap().overlay = false;+ if let Some(win) = window(&app) {+ let _ = app.run_on_main_thread(move || {+ platform::set_layer_overlay(&win, false);+ platform::set_layer_margins(&win, x, y);+ });+ }+ if let Some(win) = window(&app) {+ let _ = win.set_size(tauri::LogicalSize::new(SIZE, SIZE));+ }+ }+ place(&app, &d, (x, y));+ finish_drop(&app, &d, (x, y), Source::Desktop).await;+}++/// Launch or the end of an alert: from the island to his spot. False when the+/// spot is on a display that is gone — he then stays home, and is forgotten+/// there so the next launch doesn't try again.+#[tauri::command]+pub async fn desktop_mochi_fly_out(app: AppHandle) -> bool {+ let d = app.state::<Arc<Desktop>>().inner().clone();+ if d.mode == DesktopMode::Off || window(&app).is_none() {+ return false;+ }+ refresh_layer_display(&app, &d);+ let Some(target) = target_spot(&app, &d) else {+ crate::log::line("desktop Mochi: his spot is on a display that is gone — he stays home");+ remember(&app, |p| p.on_desktop = false);+ return false;+ };+ let Some((cx, top, scale)) = island_anchor(&app, &d) else { return false };+ let start = logic::island_spot(cx, top, SIZE, scale);+ {+ let mut i = d.inner.lock().unwrap();+ i.landed = false;+ i.carry = None;+ i.asleep = false;+ }+ show(&app, &d, start);+ let _ = app.emit_to(LABEL, "desktop-flight", "out");+ if !fly(&app, &d, target, FLIGHT_MS, logic::ease_in_out).await {+ return false;+ }+ d.inner.lock().unwrap().landed = true;+ refresh(&app, &d);+ remember_spot(&app, &d, target, true);+ true+}++/// To the island, then hidden. `forget` is the user bringing him home (double+/// click, drop on the island); without it he is only away for an alert and+/// flies back out once it is answered.+#[tauri::command]+pub async fn desktop_mochi_fly_home(app: AppHandle, forget: bool) -> bool {+ let d = app.state::<Arc<Desktop>>().inner().clone();+ if !d.inner.lock().unwrap().shown {+ if forget {+ remember(&app, |p| p.on_desktop = false);+ }+ return true;+ }+ {+ let mut i = d.inner.lock().unwrap();+ i.landed = false;+ i.carry = None;+ i.asleep = false;+ }+ refresh(&app, &d);+ let _ = app.emit_to(LABEL, "desktop-flight", "home");+ let done = match island_anchor(&app, &d) {+ Some((cx, top, scale)) => {+ let to = logic::island_spot(cx, top, SIZE, scale);+ fly(&app, &d, to, FLIGHT_MS, logic::ease_in_out).await+ }+ None => true,+ };+ if done {+ hide(&app, &d);+ }+ if forget {+ remember(&app, |p| p.on_desktop = false);+ }+ done+}++/// The page dozed off (or woke up). Asleep, nothing polls.+#[tauri::command]+pub fn desktop_mochi_set_asleep(app: AppHandle, desktop: State<Arc<Desktop>>, asleep: bool) {+ let changed = {+ let mut i = desktop.inner.lock().unwrap();+ let changed = i.asleep != asleep;+ i.asleep = asleep;+ changed+ };+ if changed {+ refresh(&app, &desktop);+ }+}++#[cfg(test)]+mod tests {+ use super::logic::*;++ // Mirrors tests/DesktopMochiTests.swift (isOverBody, clampOrigin), in the+ // y-down coordinates every PC uses.++ #[test]+ fn is_over_body() {+ let s = 120.0;+ let r = s * BODY_RADIUS_FRACTION; // 28.8+ assert!(super::logic::is_over_body((60.0, 60.0), s), "center must be inside body");+ assert!(super::logic::is_over_body((60.0 + r - 0.5, 60.0), s), "inside radius must hit");+ assert!(!super::logic::is_over_body((60.0 + r + 0.5, 60.0), s), "outside radius must miss");+ assert!(!super::logic::is_over_body((0.0, 0.0), s), "corner must miss");+ let diag = r / 2f64.sqrt() - 0.5;+ assert!(super::logic::is_over_body((60.0 + diag, 60.0 + diag), s), "diagonal inside must hit");+ assert!(super::logic::is_over_body((60.0 - diag, 60.0 + diag), s), "every quadrant");+ }++ #[test]+ fn clamp_origin_keeps_him_inside_the_work_area() {+ // A 1440×900 display with a 40 px taskbar at the bottom.+ let work = Rect { x: 0.0, y: 0.0, w: 1440.0, h: 860.0 };+ let (s, m) = (120.0, 24.0);+ assert_eq!(clamp_origin((600.0, 400.0), s, work, m), (600.0, 400.0), "in-bounds origin must be unchanged");+ assert_eq!(clamp_origin((-50.0, 400.0), s, work, m).0, work.x + m, "too-left");+ assert_eq!(clamp_origin((2000.0, 400.0), s, work, m).0, work.x + work.w - s - m, "too-right");+ assert_eq!(clamp_origin((400.0, -50.0), s, work, m).1, work.y + m, "too-high");+ assert_eq!(clamp_origin((400.0, 2000.0), s, work, m).1, work.y + work.h - s - m, "too-low: above the taskbar");+ }++ fn two_displays() -> Vec<Display> {+ // A 1920×1080 laptop at 100 %, and a 4K monitor at 200 % on its right,+ // physical pixels; both with a 48 px taskbar at the bottom.+ let a = Rect { x: 0.0, y: 0.0, w: 1920.0, h: 1080.0 };+ let b = Rect { x: 1920.0, y: -200.0, w: 3840.0, h: 2160.0 };+ vec![+ Display { frame: a, work: Rect { h: 1032.0, ..a }, scale: 1.0 },+ Display { frame: b, work: Rect { h: 2064.0, ..b }, scale: 2.0 },+ ]+ }++ #[test]+ fn a_spot_on_a_connected_display_is_kept() {+ let d = two_displays();+ assert_eq!(restore_spot((800.0, 600.0), &d, 120.0, 24.0), Some((800.0, 600.0)));+ // On the second display, sized in its own scale.+ assert_eq!(restore_spot((3000.0, 1000.0), &d, 120.0, 24.0), Some((3000.0, 1000.0)));+ }++ #[test]+ fn a_spot_on_a_display_that_is_gone_brings_him_home() {+ let d = two_displays();+ // Left of the laptop: a monitor that has been unplugged.+ assert_eq!(restore_spot((-1500.0, 300.0), &d, 120.0, 24.0), None);+ // Nothing connected at all.+ assert_eq!(restore_spot((800.0, 600.0), &[], 120.0, 24.0), None);+ // Only the first display left: the spot on the second is gone.+ assert_eq!(restore_spot((3000.0, 1000.0), &d[..1], 120.0, 24.0), None);+ }++ #[test]+ fn a_spot_that_a_taller_taskbar_now_covers_moves_up() {+ let d = two_displays();+ let (x, y) = restore_spot((800.0, 1000.0), &d, 120.0, 24.0).unwrap();+ assert_eq!(x, 800.0);+ assert_eq!(y, 1032.0 - 120.0 - 24.0);+ // The 200 % display clamps with its own scale.+ let (_, y) = restore_spot((3000.0, 1700.0), &d, 120.0, 24.0).unwrap();+ assert_eq!(y, -200.0 + 2064.0 - 240.0 - 48.0);+ }++ #[test]+ fn a_drop_settles_on_the_display_under_him() {+ let d = two_displays();+ // Dropped against the right edge of the laptop: stays on the laptop.+ assert_eq!(settle((1800.0, 500.0), &d, 120.0, 24.0), (1920.0 - 120.0 - 24.0, 500.0));+ // His centre already over the next display: he moves onto it.+ assert_eq!(settle((1870.0, 500.0), &d, 120.0, 24.0), (1920.0 + 48.0, 500.0));+ // Dropped in the gap above the laptop, next to the taller monitor:+ // nearest display.+ let (x, y) = settle((1000.0, -150.0), &d, 120.0, 24.0);+ assert_eq!((x, y), (1000.0, 24.0));+ }++ #[test]+ fn first_visit_is_the_bottom_right_corner() {+ let d = two_displays();+ assert_eq!(default_spot(&d[0], 120.0, 24.0), (1920.0 - 144.0, 1032.0 - 144.0));+ assert_eq!(default_spot(&d[1], 120.0, 24.0), (1920.0 + 3840.0 - 288.0, -200.0 + 2064.0 - 288.0));+ }++ #[test]+ fn the_island_panel_is_home() {+ // Island centred on a 1920 px display at 150 %.+ let zone = home_zone(960.0, 0.0, 1.5);+ assert!(zone.contains((960.0, 10.0)), "on the island");+ assert!(zone.contains((960.0 - 359.0 * 1.5, 300.0 * 1.5)), "inside the panel");+ assert!(!zone.contains((960.0, 330.0 * 1.5)), "below the panel");+ assert!(!zone.contains((100.0, 10.0)), "top-left corner of the screen");+ }++ #[test]+ fn he_leaves_and_returns_under_the_top_centre() {+ assert_eq!(island_spot(960.0, 0.0, 120.0, 1.0), (900.0, 0.0));+ assert_eq!(island_spot(2880.0, -200.0, 120.0, 2.0), (2760.0, -200.0));+ }++ #[test]+ fn flights_start_and_end_where_asked() {+ for ease in [ease_in_out as fn(f64) -> f64, ease_out_back] {+ assert!(ease(0.0).abs() < 1e-9);+ assert!((ease(1.0) - 1.0).abs() < 1e-9);+ }+ assert!(ease_out_back(0.7) > 1.0, "the landing overshoots a little");+ assert_eq!(lerp((0.0, 10.0), (100.0, 20.0), 0.5), (50.0, 15.0));+ }+}diff --git a/windows/src-tauri/src/island.rs b/windows/src-tauri/src/island.rsindex 92c9cd4c..00536adb 100644--- a/windows/src-tauri/src/island.rs+++ b/windows/src-tauri/src/island.rs@@ -89,14 +89,14 @@ impl PollGate {self.cv.notify_all();}- fn wait_until_active(&self) {+ pub(crate) fn wait_until_active(&self) {let mut guard = self.active.lock().unwrap();while !*guard {guard = self.cv.wait(guard).unwrap();}}- fn is_active(&self) -> bool {+ pub(crate) fn is_active(&self) -> bool {*self.active.lock().unwrap()}}diff --git a/windows/src-tauri/src/lib.rs b/windows/src-tauri/src/lib.rsindex d9ec3353..948d7685 100644--- a/windows/src-tauri/src/lib.rs+++ b/windows/src-tauri/src/lib.rs@@ -1,6 +1,7 @@// Coucou for Windows — app wiring and the commands the island calls.mod claude;+mod desktop;mod files;mod hooks;mod integrations;@@ -65,9 +66,13 @@ fn save_settings(app: AppHandle, shared: State<Shared>, settings: Settings) {let mut current = shared.settings.lock().unwrap();let screen_changed = current.screen != settings.screen;let autostart_changed = current.autostart != settings.autostart;- *current = settings.clone();+ // Where Mochi sits on the desktop is desktop.rs's to say, not a webview's.+ let mut settings = settings.clone();+ settings.desktop_mochi = current.desktop_mochi.clone();+ *current = settings;(screen_changed, autostart_changed)};+ let settings = shared.settings.lock().unwrap().clone();if let Err(err) = settings::save(&settings) {log::line(format!("could not save settings: {err}"));}@@ -301,7 +306,7 @@ fn log_line(message: String) {/// for the *same* arguments as the island (see `additionalBrowserArgs` in/// tauri.conf.json) — a mismatch makes the second window come up blank, with no/// error anywhere.-const BROWSER_ARGS: &str = "--disable-features=msWebOOUI,msPdfOOUI,msSmartScreenProtection --autoplay-policy=no-user-gesture-required";+pub(crate) const BROWSER_ARGS: &str = "--disable-features=msWebOOUI,msPdfOOUI,msSmartScreenProtection --autoplay-policy=no-user-gesture-required";/// In a dev build the pages are served by Vite, so the second window needs the/// absolute dev URL; a bundled build resolves it inside the app bundle.@@ -403,12 +408,24 @@ pub fn run() {open_n8n,open_settings_window,set_paused,+ desktop::desktop_mochi_info,+ desktop::desktop_mochi_pick_up,+ desktop::desktop_mochi_carry,+ desktop::desktop_mochi_carry_end,+ desktop::desktop_mochi_drag_begin,+ desktop::desktop_mochi_drag_move,+ desktop::desktop_mochi_drag_end,+ desktop::desktop_mochi_fly_out,+ desktop::desktop_mochi_fly_home,+ desktop::desktop_mochi_set_asleep,]).setup(move |app| {let handle = app.handle().clone();tray::build(&handle)?;// Before the island: see create_settings_window.create_settings_window(&handle);+ // Same rule for Mochi's desktop window.+ desktop::setup(&handle);if let Some(win) = island::window(&handle) {platform::make_non_activating(&win);diff --git a/windows/src-tauri/src/platform/linux.rs b/windows/src-tauri/src/platform/linux.rsindex e748a336..d4e4581d 100644--- a/windows/src-tauri/src/platform/linux.rs+++ b/windows/src-tauri/src/platform/linux.rs@@ -167,8 +167,12 @@ mod layer {use gtk::ffi::GtkWindow;use std::os::raw::{c_char, c_int};+ pub const LAYER_TOP: c_int = 2;pub const LAYER_OVERLAY: c_int = 3;+ pub const EDGE_LEFT: c_int = 0;+ pub const EDGE_RIGHT: c_int = 1;pub const EDGE_TOP: c_int = 2;+ pub const EDGE_BOTTOM: c_int = 3;pub const KEYBOARD_NONE: c_int = 0;pub const KEYBOARD_ON_DEMAND: c_int = 2;@@ -179,11 +183,18 @@ mod layer {pub fn gtk_layer_set_namespace(window: *mut GtkWindow, name_space: *const c_char);pub fn gtk_layer_set_layer(window: *mut GtkWindow, layer: c_int);pub fn gtk_layer_set_anchor(window: *mut GtkWindow, edge: c_int, anchor: c_int);+ pub fn gtk_layer_set_margin(window: *mut GtkWindow, edge: c_int, margin: c_int);+ pub fn gtk_layer_set_monitor(window: *mut GtkWindow, monitor: *mut gtk::gdk::ffi::GdkMonitor);pub fn gtk_layer_set_exclusive_zone(window: *mut GtkWindow, zone: c_int);pub fn gtk_layer_set_keyboard_mode(window: *mut GtkWindow, mode: c_int);}}+/// COUCOU_LAYER_SHELL=0 is the way out on a compositor where it misbehaves.+fn layer_shell_wanted() -> bool {+ std::env::var("COUCOU_LAYER_SHELL").map(|v| v != "0").unwrap_or(true)+}+/// True once the island window is a layer-shell surface.static LAYER_SURFACE: AtomicBool = AtomicBool::new(false);@@ -210,8 +221,7 @@ pub fn unblock_webview_drops(_app: &AppHandle) {}/// up to the window manager.pub fn make_non_activating(win: &WebviewWindow) {let Ok(gw) = win.gtk_window() else { return };- // COUCOU_LAYER_SHELL=0 is the way out on a compositor where it misbehaves.- let wanted = std::env::var("COUCOU_LAYER_SHELL").map(|v| v != "0").unwrap_or(true);+ let wanted = layer_shell_wanted();let supported = unsafe { layer::gtk_layer_is_supported() } != 0;if !wanted || !supported || gw.is_realized() {let why = if !wanted {@@ -300,6 +310,174 @@ fn apply_input_region(gw: &impl IsA<gtk::Widget>, rect: Region) {}}+// ── Desktop Mochi window ──────────────────────────────────────────────────────+//+// Where the window may go decides everything here:+// * X11 places any window where it is asked: an ordinary always-on-top+// window, moved with set_position;+// * a layer-shell compositor won't move a toplevel, but anchors a layer+// surface wherever its margins say, on the island's display;+// * GNOME on Wayland has neither, so the feature is off there.+// In every case the input region is Mochi's body, so clicks anywhere else go+// to the desktop underneath without any cursor polling.++/// Which of the three applies. Must run on the GTK main thread.+pub fn desktop_mode() -> super::DesktopMode {+ use gtk::glib::prelude::ObjectExt;+ let wayland = gtk::gdk::Display::default()+ .map(|d| d.type_().name().contains("Wayland"))+ .unwrap_or(false);+ let layer = layer_shell_wanted() && unsafe { layer::gtk_layer_is_supported() } != 0;+ if layer {+ super::DesktopMode::Layer+ } else if !wayland {+ super::DesktopMode::Window+ } else {+ super::DesktopMode::Off+ }+}++/// The input shape last asked for the desktop Mochi, re-applied on every map.+static MOCHI_SHAPE: Mutex<super::MouseShape> = Mutex::new(super::MouseShape::Empty);++/// Sets the window up for `mode` before it is ever shown. False means it can't+/// be used (a layer surface can't be made from a window already shown).+pub fn prepare_desktop_window(win: &WebviewWindow, mode: super::DesktopMode) -> bool {+ let Ok(gw) = win.gtk_window() else { return false };+ gw.set_accept_focus(false);+ match mode {+ super::DesktopMode::Layer => {+ if gw.is_realized() {+ crate::log::line("desktop Mochi: window already shown, no layer surface");+ return false;+ }+ gw.set_titlebar(None::<>k::Widget>);+ let ptr = gtk_window_ptr(&gw);+ unsafe {+ layer::gtk_layer_init_for_window(ptr);+ layer::gtk_layer_set_namespace(ptr, c"coucou-mochi".as_ptr());+ // Above windows, below fullscreen video — like a floating panel.+ layer::gtk_layer_set_layer(ptr, layer::LAYER_TOP);+ layer::gtk_layer_set_anchor(ptr, layer::EDGE_TOP, 1);+ layer::gtk_layer_set_anchor(ptr, layer::EDGE_LEFT, 1);+ // -1: margins count from the display's corner, panels or not,+ // the same origin the island uses.+ layer::gtk_layer_set_exclusive_zone(ptr, -1);+ layer::gtk_layer_set_keyboard_mode(ptr, layer::KEYBOARD_NONE);+ }+ // Same first-frame problem as the island (see make_non_activating).+ let remapped = std::cell::Cell::new(false);+ gw.connect_map_event(move |w, _| {+ apply_mouse_shape(w, *MOCHI_SHAPE.lock().unwrap());+ if !remapped.replace(true) {+ let w = w.clone();+ gtk::glib::idle_add_local_once(move || {+ w.hide();+ w.show_all();+ apply_mouse_shape(&w, *MOCHI_SHAPE.lock().unwrap());+ });+ }+ gtk::glib::Propagation::Proceed+ });+ true+ }+ super::DesktopMode::Window => {+ gw.connect_map_event(|w, _| {+ apply_mouse_shape(w, *MOCHI_SHAPE.lock().unwrap());+ gtk::glib::Propagation::Proceed+ });+ true+ }+ _ => false,+ }+}++/// Which part of the desktop Mochi takes the mouse. Main thread.+pub fn set_desktop_shape(win: &WebviewWindow, shape: super::MouseShape) {+ *MOCHI_SHAPE.lock().unwrap() = shape;+ let Ok(gw) = win.gtk_window() else { return };+ apply_mouse_shape(&gw, shape);+}++fn apply_mouse_shape(gw: &impl IsA<gtk::Widget>, shape: super::MouseShape) {+ use gtk::cairo::{RectangleInt, Region};+ if shape == super::MouseShape::Whole {+ gw.input_shape_combine_region(None);+ return;+ }+ let Some(gdk_window) = gw.window() else { return };+ match shape {+ super::MouseShape::Whole => {}+ super::MouseShape::Empty => {+ let region = Region::create_rectangle(&RectangleInt::new(0, 0, 0, 0));+ gdk_window.input_shape_combine_region(®ion, 0, 0);+ }+ super::MouseShape::Disc { cx, cy, r } => {+ // A region is made of rectangles: stack 2 px rows into a disc.+ let region = Region::create_rectangle(&RectangleInt::new(0, 0, 0, 0));+ let mut y = (cy - r).floor();+ while y < cy + r {+ let mid = y + 1.0 - cy;+ let half = (r * r - mid * mid).max(0.0).sqrt();+ let row = RectangleInt::new(+ (cx - half).floor() as i32,+ y as i32,+ (2.0 * half).ceil() as i32,+ 2,+ );+ let _ = region.union_rectangle(&row);+ y += 2.0;+ }+ gdk_window.input_shape_combine_region(®ion, 0, 0);+ }+ }+}++/// Layer surface only: puts the top-left corner at (x, y), logical pixels from+/// the display's corner. Main thread.+pub fn set_layer_margins(win: &WebviewWindow, x: f64, y: f64) {+ let Ok(gw) = win.gtk_window() else { return };+ let ptr = gtk_window_ptr(&gw);+ unsafe {+ layer::gtk_layer_set_margin(ptr, layer::EDGE_LEFT, x.round() as i32);+ layer::gtk_layer_set_margin(ptr, layer::EDGE_TOP, y.round() as i32);+ }+}++/// Layer surface only: `on` stretches it over the whole display — how a drag+/// gets exact pointer positions on Wayland, where a surface that moves under+/// the pointer can't tell where it is — and `off` folds it back to Mochi's+/// size in the corner. Main thread.+pub fn set_layer_overlay(win: &WebviewWindow, on: bool) {+ let Ok(gw) = win.gtk_window() else { return };+ let ptr = gtk_window_ptr(&gw);+ unsafe {+ layer::gtk_layer_set_anchor(ptr, layer::EDGE_RIGHT, on as i32);+ layer::gtk_layer_set_anchor(ptr, layer::EDGE_BOTTOM, on as i32);+ if on {+ layer::gtk_layer_set_margin(ptr, layer::EDGE_LEFT, 0);+ layer::gtk_layer_set_margin(ptr, layer::EDGE_TOP, 0);+ }+ }+}++/// Layer surface only: puts the desktop Mochi on the island's display and+/// returns that display's logical size. Main thread.+pub fn layer_display(island: &WebviewWindow, mochi: &WebviewWindow) -> Option<(f64, f64)> {+ let island_gw = island.gtk_window().ok()?;+ let display = gtk::gdk::Display::default()?;+ let monitor = island_gw+ .window()+ .and_then(|w| display.monitor_at_window(&w))+ .or_else(|| display.primary_monitor())+ .or_else(|| display.monitor(0))?;+ if let Ok(gw) = mochi.gtk_window() {+ unsafe { layer::gtk_layer_set_monitor(gtk_window_ptr(&gw), monitor.to_glib_none().0) };+ }+ let g = monitor.geometry();+ Some((g.width() as f64, g.height() as f64))+}+#[cfg(test)]mod tests {use super::*;diff --git a/windows/src-tauri/src/platform/mod.rs b/windows/src-tauri/src/platform/mod.rsindex 5eb032dc..73ce61f9 100644--- a/windows/src-tauri/src/platform/mod.rs+++ b/windows/src-tauri/src/platform/mod.rs@@ -31,3 +31,55 @@ pub fn home_dir() -> PathBuf {.map(PathBuf::from).unwrap_or_else(|| PathBuf::from("."))}++/// How the desktop Mochi's window can be placed and clicked on this system.+#[derive(Clone, Copy, PartialEq, Eq, Debug)]+pub enum DesktopMode {+ /// Windows: placed anywhere, and a cursor poll drives click-through, the+ /// eyes and the drag, exactly like the island.+ Poll,+ /// Linux on X11: an ordinary window the window manager lets us place; the+ /// input region is the click-through and the page drives the drag.+ Window,+ /// Linux on a layer-shell compositor (KDE, COSMIC, wlroots): a layer+ /// surface anchored to the top-left corner of the island's display, placed+ /// with margins.+ Layer,+ /// Wayland without layer-shell (GNOME): no way to put a window where the+ /// user dropped it, so Mochi stays in the island.+ Off,+}++impl DesktopMode {+ pub fn as_str(self) -> &'static str {+ match self {+ DesktopMode::Poll => "poll",+ DesktopMode::Window => "window",+ DesktopMode::Layer => "layer",+ DesktopMode::Off => "off",+ }+ }++ /// The space a saved spot is measured in. A spot from another space (the+ /// same machine once on X11, once on Wayland) is not trusted.+ pub fn space(self) -> &'static str {+ match self {+ // Physical pixels of the whole desktop, as the OS reports monitors.+ DesktopMode::Poll | DesktopMode::Window => "screen",+ // Logical pixels from the top-left corner of the island's display.+ DesktopMode::Layer | DesktopMode::Off => "layer",+ }+ }+}++/// Which part of the desktop Mochi's window takes the mouse.+#[derive(Clone, Copy, PartialEq, Debug)]+#[cfg_attr(windows, allow(dead_code))]+pub enum MouseShape {+ /// Nothing: every click goes to what is underneath (during a flight).+ Empty,+ /// The whole window (while he is being dragged across the screen).+ Whole,+ /// A disc, in window-logical pixels: Mochi's body.+ Disc { cx: f64, cy: f64, r: f64 },+}diff --git a/windows/src-tauri/src/platform/windows.rs b/windows/src-tauri/src/platform/windows.rsindex 6e1200a8..604ed2dc 100644--- a/windows/src-tauri/src/platform/windows.rs+++ b/windows/src-tauri/src/platform/windows.rs@@ -233,3 +233,31 @@ pub fn set_activating(win: &WebviewWindow, activating: bool) {/// Click-through here is the poll's WS_EX_TRANSPARENT toggle, not a region.pub fn set_input_region(_win: &WebviewWindow, _rect: Option<(f64, f64, f64, f64)>) {}++// ── Desktop Mochi window ──────────────────────────────────────────────────────++/// Windows places a window wherever it is asked, and the cursor poll is there.+pub fn desktop_mode() -> super::DesktopMode {+ super::DesktopMode::Poll+}++/// Same window styles as the island: never takes focus, never in Alt-Tab (it+/// is created without a taskbar button).+pub fn prepare_desktop_window(win: &WebviewWindow, _mode: super::DesktopMode) -> bool {+ make_non_activating(win);+ true+}++/// The cursor poll toggles WS_EX_TRANSPARENT from the body hit test instead.+pub fn set_desktop_shape(_win: &WebviewWindow, _shape: super::MouseShape) {}++/// Layer-shell only (Linux).+pub fn set_layer_margins(_win: &WebviewWindow, _x: f64, _y: f64) {}++/// Layer-shell only (Linux).+pub fn set_layer_overlay(_win: &WebviewWindow, _on: bool) {}++/// Layer-shell only (Linux): the logical size of the display the island is on.+pub fn layer_display(_island: &WebviewWindow, _mochi: &WebviewWindow) -> Option<(f64, f64)> {+ None+}diff --git a/windows/src-tauri/src/settings.rs b/windows/src-tauri/src/settings.rsindex a95abfd7..0cf69499 100644--- a/windows/src-tauri/src/settings.rs+++ b/windows/src-tauri/src/settings.rs@@ -27,6 +27,26 @@ pub struct Settings {/// season), "none" or an outfit id — the Mac's raw values. The island reads/// anything it doesn't know as "auto", so the value is stored as it comes.pub mochi_outfit: String,+ /// Mochi on the desktop: whether he lives there, and his spot. Owned by+ /// the Rust side (desktop.rs) — what a webview sends back is ignored.+ pub desktop_mochi: DesktopMochiPref,+}++#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]+#[serde(rename_all = "camelCase", default)]+pub struct DesktopMochiPref {+ /// He was on the desktop when the app quit: he flies back out at launch.+ pub on_desktop: bool,+ /// Top-left corner of his window where the user last left him.+ pub spot: Option<DesktopSpot>,+}++#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]+pub struct DesktopSpot {+ pub x: f64,+ pub y: f64,+ /// What x and y are measured in (`DesktopMode::space`).+ pub space: String,}fn default_model() -> String {@@ -51,6 +71,7 @@ impl Default for Settings {hooks_installed: false,model: default_model(),mochi_outfit: "auto".into(),+ desktop_mochi: DesktopMochiPref::default(),}}}@@ -312,7 +333,8 @@ mod tests {"autostart": true,"hooksInstalled": true,"model": "some-model",- "mochiOutfit": "witchHat"+ "mochiOutfit": "witchHat",+ "desktopMochi": { "onDesktop": true, "spot": { "x": 1500.5, "y": -300.0, "space": "screen" } }}"#;fn custom() -> Value {@@ -410,6 +432,30 @@ mod tests {assert!(!loaded.sound_enabled);}+ #[test]+ fn a_file_from_before_the_desktop_mochi_keeps_him_in_the_island() {+ let loaded = parse(&custom_with("desktopMochi", None)).unwrap();+ assert_eq!(loaded.desktop_mochi, DesktopMochiPref::default());+ assert!(!loaded.desktop_mochi.on_desktop);+ assert_eq!(loaded.mochi_outfit, "witchHat");+ }++ #[test]+ fn a_half_written_desktop_spot_costs_only_the_spot() {+ let loaded = parse(&custom_with(+ "desktopMochi",+ Some(json!({ "onDesktop": true, "spot": { "x": "left" } })),+ ))+ .unwrap();+ // The whole field falls back, and nothing else does.+ assert_eq!(loaded.desktop_mochi, DesktopMochiPref::default());+ assert_eq!(loaded.model, "some-model");++ let loaded = parse(&custom_with("desktopMochi", Some(json!({ "onDesktop": true })))).unwrap();+ assert!(loaded.desktop_mochi.on_desktop);+ assert_eq!(loaded.desktop_mochi.spot, None);+ }+#[test]fn an_outfit_this_build_does_not_know_is_kept_as_written() {// A newer build may add outfits: the island shows "auto" for it, but@@ -655,6 +701,7 @@ mod tests {"hooksInstalled","model","mochiOutfit",+ "desktopMochi",]);let _ = std::fs::remove_dir_all(&dir);