///| Desktop modules (wit/desktop.wit, compositor/shell/desktop_module.mbt): a module that fills the /// desktop behind the windows. Needs the sample modules built: scripts/build-modules.sh. ///| A compositor with a first frame drawn and six made-up apps (so icon positions don't depend on /// the machine's .desktop files). fn desk_comp(tag : String, fake_apps? : Bool = true) -> @host.ChandraCompositor { let socket_path = "/tmp/wayland-moon-desk-" + tag + "-" + @sys.getpid().to_string() + ".sock" let comp = @host.ChandraCompositor::new(socket_path, @scene.ThemeTokens::moon_night(), 1280, 720).unwrap() if fake_apps { comp.shell.apps.clear() for i = 0; i < 6; i = i + 1 { comp.shell.apps.push({ id: "app" + i.to_string(), name: ["Files", "Browser", "Terminal", "Editor", "Mail", "Music"][i], comment: "", category: "", glyph: "", }) } comp.shell.apps_dirty = true } ignore(comp.compose_frame()) comp } ///| fn desk_socket(tag : String) -> String { "/tmp/wayland-moon-desk-" + tag + "-" + @sys.getpid().to_string() + ".sock" } ///| Press at (x0, y0), move to (x1, y1) in steps, release there. fn desk_drag(comp : @host.ChandraCompositor, x0 : Int, y0 : Int, x1 : Int, y1 : Int, t : UInt) -> Unit { comp.handle_input(@nested.InputEvent::PointerMove(x0.to_double(), y0.to_double()), t) comp.handle_input(@nested.InputEvent::PointerButton(272U, true), t + 1U) for k = 1; k <= 4; k = k + 1 { let x = x0 + (x1 - x0) * k / 4 let y = y0 + (y1 - y0) * k / 4 comp.handle_input(@nested.InputEvent::PointerMove(x.to_double(), y.to_double()), t + 2U + k.reinterpret_as_uint()) } comp.handle_input(@nested.InputEvent::PointerButton(272U, false), t + 10U) ignore(comp.compose_frame()) } ///| The value of the desktop module's setting `key` ("" when none). fn desk_setting(comp : @host.ChandraCompositor, key : String) -> String { for it in comp.shell.slot_config("desktop") { if it.key == key { return it.value } } "" } ///| Whether the desktop module currently draws a focused selection (an outlined icon cell). fn desk_selection_outlined(comp : @host.ChandraCompositor) -> Bool { comp.shell.slot_ops("desktop").iter().any(op => op is StrokeRect(_)) } ///| test "Settings > Desktop lists the desktop modules, and only the desktop takes them" { let comp = desk_comp("choose") let mods = comp.shell.desktop_modules() assert_true(mods.contains(("icon-desktop", "Icon Desktop"))) assert_true(mods.contains(("starfield", "Live Starfield"))) // A desktop module is not a panel module, and the other way round. assert_true(comp.shell.load_path("build/components/starfield.wasm") is None) assert_false(comp.shell.set_desktop("launcher")) assert_eq(comp.shell.desktop_choice(), "clock-card") assert_true(comp.shell.desktop_module() is None) // Choose one: it is loaded under the "desktop" slot (chandra-ctl list shows it last). assert_true(comp.shell.set_desktop("icon-desktop")) assert_eq(comp.shell.desktop_choice(), "icon-desktop") let last = comp.shell.loaded_modules().last().unwrap() assert_eq((last.0, last.1), ("desktop", "icon-desktop")) // Another replaces it; the wallpaper alone unloads it. assert_true(comp.shell.set_desktop("starfield")) assert_eq(comp.shell.desktop_module().unwrap().module_id, "starfield") assert_true(comp.shell.set_desktop("wallpaper")) assert_true(comp.shell.desktop_module() is None) // The built-in clock card can only be dragged while it is on the desktop. assert_false(comp.shell.desk_hit(1280 - 100, 60)) assert_true(comp.shell.set_desktop("clock-card")) assert_true(comp.shell.desk_hit(1280 - 100, 60)) } ///| test "the empty desktop's pointer and keys reach the desktop module" { let comp = desk_comp("input") assert_true(comp.shell.set_desktop("icon-desktop")) ignore(comp.compose_frame()) // Grid cells are 96 px from (20, 20): app0 at (0,0), app1 below it. click_at(comp, 900, 300, 10U) // empty desktop, no icon there assert_true(comp.shell.desktop_has_focus()) assert_false(desk_selection_outlined(comp)) // Arrow keys move the selection (drawn outlined while the desktop has the keyboard). comp.handle_input(@nested.InputEvent::Key(108U, true), 20U) // Down comp.handle_input(@nested.InputEvent::Key(108U, false), 21U) ignore(comp.compose_frame()) assert_true(desk_selection_outlined(comp)) // Drag app0 to the cell at column 5, row 2: the arrangement is saved as the hidden setting. desk_drag(comp, 68, 68, 20 + 5 * 96 + 40, 20 + 2 * 96 + 40, 30U) assert_true(desk_setting(comp, "places").contains("app0=5,2")) assert_eq(comp.shell.module_setting("icon-desktop", "places"), Some(desk_setting(comp, "places"))) // The wheel over the empty desktop goes to the module too (the icon desktop ignores it). comp.handle_input(@nested.InputEvent::PointerMove(900.0, 300.0), 50U) comp.handle_input(@nested.InputEvent::Scroll(0U, 10.0, 1), 51U) assert_true(comp.shell.desktop_has_focus()) } ///| test "windows and the panel win over the desktop module" { let comp = desk_comp("hit") assert_true(comp.shell.set_desktop("icon-desktop")) ignore(comp.compose_frame()) // The panel: a click on a panel module doesn't reach the desktop. let p = comp.shell.panel_rect click_at(comp, p.x + p.w / 2, p.y + p.h / 2, 10U) assert_false(comp.shell.desktop_has_focus()) assert_false(comp.shell.desktop_grabbing()) // A right-click on the panel opens the panel's menu, not the desktop's. click_at(comp, p.x + p.w - 3, p.y + p.h / 2, 20U, btn=273U) assert_true(comp.shell.menu_title != "Icon Desktop") comp.shell.key_down("Escape") // A window over the desktop (tiled: it fills the work area) takes clicks and keys. let (_c, _shm, _surf, _xdg, _tl, _deco) = open_and_configure_client(comp, desk_socket("hit"), "Win", "test.win") ignore(comp.compose_frame()) click_at(comp, 68, 68, 30U) // where app0's icon is, under the window assert_false(comp.shell.desktop_has_focus()) assert_false(comp.shell.desktop_grabbing()) assert_eq(desk_setting(comp, "places"), "") // No desktop menu over the window either. click_at(comp, 68, 68, 40U, btn=273U) assert_false(comp.shell.menu_open && comp.shell.menu_title == "Icon Desktop") } ///| test "a press on the desktop keeps the pointer until the release, even over a window" { let comp = desk_comp("grab") assert_true(comp.shell.set_desktop("icon-desktop")) ignore(comp.compose_frame()) comp.handle_input(@nested.InputEvent::PointerMove(68.0, 68.0), 10U) comp.handle_input(@nested.InputEvent::PointerButton(272U, true), 11U) assert_true(comp.shell.desktop_grabbing()) comp.handle_input(@nested.InputEvent::PointerMove(1200.0, 600.0), 12U) comp.handle_input(@nested.InputEvent::PointerButton(272U, false), 13U) assert_false(comp.shell.desktop_grabbing()) } ///| test "right-click on the empty desktop opens the desktop menu" { let comp = desk_comp("menu") // No module: just System settings (on the Desktop tab). click_at(comp, 600, 300, 10U, btn=273U) assert_true(comp.shell.menu_open) assert_eq(comp.shell.menu_title, "Desktop") assert_eq(comp.shell.menu_rows.map(r => r.label), ["System settings..."]) comp.shell.key_down("Escape") assert_true(comp.shell.set_desktop("starfield")) click_at(comp, 600, 300, 20U, btn=273U) assert_eq(comp.shell.menu_title, "Live Starfield") assert_eq(comp.shell.menu_rows.map(r => r.label), ["Module settings...", "System settings..."]) // It opens at the pointer, not above the panel. let m = comp.shell.menu_rect assert_eq((m.x, m.y), (600, 300)) // Module settings: the module's own choices; picking one applies it. click_at(comp, m.x + 20, m.y + 26 + 14, 30U) assert_eq(comp.shell.menu_title, "Live Starfield settings") assert_true(comp.shell.menu_rows.map(r => r.label).contains("Still")) comp.shell.set_module_setting("desktop", "speed", "Fast") assert_eq(desk_setting(comp, "speed"), "Fast") ignore(@sys.write_file_bytes("_build/desktop_menu.png", @renderer.encode_png(comp.compose_frame()))) comp.shell.key_down("Escape") // System settings opens the settings module on its Desktop tab, listing the choices. click_at(comp, 600, 300, 40U, btn=273U) let m = comp.shell.menu_rect click_at(comp, m.x + 20, m.y + 26 + 28 + 14, 50U) let slot = comp.shell.slot_of("session-settings") let texts = comp.shell.slot_popup_texts(slot).map(t => t.0) assert_true(texts.contains("On the desktop")) assert_true(texts.contains("Live Starfield")) assert_true(texts.contains("Icon Desktop")) assert_true(texts.contains("Clock card")) } ///| test "the desktop module is drawn at the output's size, and again after a resize" { let comp = desk_comp("resize") assert_true(comp.shell.set_desktop("starfield")) let f = comp.compose_frame() assert_eq(comp.shell.desktop_module().unwrap().rect.w, 1280) // It replaces the wallpaper: deep space in the corner, darker than any wallpaper's. let dark = fn(px : @scene.Rgba?) { match px { Some(c) => c.r.to_int() + c.g.to_int() + c.b.to_int() < 120 None => false } } assert_true(dark(f.pixel(3, 3))) comp.resize(1600, 900) let f = comp.compose_frame() assert_eq(f.width, 1600) let d = comp.shell.desktop_module().unwrap() assert_eq((d.rect.w, d.rect.h), (1600, 900)) assert_true(dark(f.pixel(1595, 5))) match comp.shell.slot_ops("desktop")[0] { FillRect(r) => assert_eq(r.rect.w, 1600U) _ => fail("the starfield starts with its background") } } ///| test "a still desktop module lets the main loop sleep; an animated one sets its pace" { let comp = desk_comp("idle") assert_true(comp.shell.set_desktop("icon-desktop")) ignore(comp.compose_frame()) ignore(comp.compose_frame()) // The icon desktop never animates: nothing keeps the loop awake, frames are unchanged. assert_false(comp.shell.desktop_animating()) assert_false(comp.has_timed_work()) assert_eq(comp.wake_timeout(980), 980) ignore(comp.compose_frame()) assert_false(comp.frame_changed) // The starfield asks for a frame every 33 ms: the idle sleep is cut to that, and each due // frame is a changed one. assert_true(comp.shell.set_desktop("starfield")) ignore(comp.compose_frame()) assert_true(comp.shell.desktop_animating()) let t = comp.wake_timeout(980) assert_true(t >= 0 && t <= 33) @sys.sleep_ms(40) ignore(comp.compose_frame()) assert_true(comp.frame_changed) // Still: it stops asking, and the loop sleeps until the next second again. comp.shell.set_module_setting("desktop", "motion", "Still") @sys.sleep_ms(40) ignore(comp.compose_frame()) ignore(comp.compose_frame()) assert_false(comp.shell.desktop_animating()) assert_eq(comp.wake_timeout(980), 980) ignore(comp.compose_frame()) assert_false(comp.frame_changed) // Moving again, but a window covering the desktop pauses it. comp.shell.set_module_setting("desktop", "motion", "Moving") assert_true(comp.shell.desktop_animating()) let (_c, _shm, _surf, _xdg, _tl, _deco) = open_and_configure_client(comp, desk_socket("idle"), "Cover", "test.cover") ignore(comp.compose_frame()) assert_eq(comp.wake_timeout(980), 980) } ///| test "reload brings the desktop module back from disk with its settings" { let comp = desk_comp("reload") assert_true(comp.shell.set_desktop("starfield")) comp.shell.set_module_setting("desktop", "speed", "Fast") let (lines, ok) = comp.shell.reload_modules("starfield") assert_true(ok) assert_eq(lines, ["reloaded desktop starfield"]) assert_eq(desk_setting(comp, "speed"), "Fast") // `reload` with no name reloads it along with the panel; `reload desktop` names it. let (all, ok) = comp.shell.reload_modules("") assert_true(ok) assert_true(all.contains("reloaded desktop starfield")) assert_eq(comp.shell.reload_modules("desktop").0, ["reloaded desktop starfield"]) assert_eq(comp.shell.desktop_module().unwrap().module_id, "starfield") } ///| test "desktop screenshots: icons, starfield, the Desktop tab" { let comp = desk_comp("shots", fake_apps=false) assert_true(comp.shell.set_desktop("icon-desktop")) comp.shell.set_module_setting("desktop", "count", "All") ignore(comp.compose_frame()) // Hover one icon and select another with the keyboard. click_at(comp, 900, 300, 10U) comp.handle_input(@nested.InputEvent::Key(106U, true), 20U) // Right: selects the first comp.handle_input(@nested.InputEvent::Key(108U, true), 21U) // Down comp.handle_input(@nested.InputEvent::PointerMove(68.0, 68.0), 22U) ignore(@sys.write_file_bytes("_build/desktop_icons.png", @renderer.encode_png(comp.compose_frame()))) assert_true(comp.shell.set_desktop("starfield")) comp.shell.set_module_setting("desktop", "speed", "Normal") comp.handle_input(@nested.InputEvent::PointerMove(900.0, 250.0), 30U) for _ in 0..<6 { @sys.sleep_ms(34) ignore(comp.compose_frame()) } ignore(@sys.write_file_bytes("_build/desktop_starfield.png", @renderer.encode_png(comp.compose_frame()))) // Warp pulse: a click streaks the stars. click_at(comp, 900, 250, 40U) for _ in 0..<3 { @sys.sleep_ms(34) ignore(comp.compose_frame()) } ignore(@sys.write_file_bytes("_build/desktop_warp.png", @renderer.encode_png(comp.compose_frame()))) assert_true(comp.shell.open_settings(Desktop)) for _ in 0..<10 { @sys.sleep_ms(40) // the popup slides in ignore(comp.compose_frame()) } ignore(@sys.write_file_bytes("_build/desktop_settings.png", @renderer.encode_png(comp.compose_frame()))) } ///| test "mobile tap on desktop icon with finger drift launches without moving any icon" { let socket_path = "/tmp/wayland-moon-desk-mobile-" + @sys.getpid().to_string() + ".sock" let comp = @host.ChandraCompositor::new(socket_path, @scene.ThemeTokens::moon_night(), 540, 1170).unwrap() comp.shell.apps.clear() for i = 0; i < 6; i = i + 1 { let id = "app" + i.to_string() comp.shell.apps.push({ id, name: ["Files", "Browser", "Terminal", "Editor", "Mail", "Music"][i], comment: "", category: "", glyph: "", }) comp.shell.exec.set(id, "true") } comp.shell.apps_dirty = true assert_true(comp.shell.set_desktop("icon-desktop")) ignore(comp.compose_frame()) // At 540x1170 with 96px medium cells and 20px margin, app5 is at column 0, row 5: center (68, 548). // Tap with an 18 px touch-drift between press and release: must launch app5 and keep every icon in place. comp.handle_input(@nested.InputEvent::PointerMove(68.0, 548.0), 10U) comp.handle_input(@nested.InputEvent::PointerButton(272U, true), 11U) comp.handle_input(@nested.InputEvent::PointerMove(86.0, 548.0), 12U) comp.handle_input(@nested.InputEvent::PointerButton(272U, false), 13U) ignore(comp.compose_frame()) assert_eq(comp.shell.recents[0], "app5") assert_eq(comp.shell.apps[0].id, "app5") let places = desk_setting(comp, "places") assert_true(places.contains("app0=0,0")) assert_true(places.contains("app5=0,5")) // A deliberate drag (> 50 px) to column 1, row 0 (center 164, 68) still moves app5 there. desk_drag(comp, 68, 548, 164, 68, 30U) assert_true(desk_setting(comp, "places").contains("app5=1,0")) comp.close() }