///| fn apps(n : Int) -> Array[@types.AppEntry] { Array::makei(n, i => { id: "app" + i.to_string(), name: ["Files", "Firefox", "Konsole", "Kate", "Gimp", "Foot"][i % 6] + i.to_string(), comment: "", category: "", glyph: "A", }) } ///| fn desk() -> @icondesk.Desk { let d = @icondesk.Desk::new() // 1000 x 600 with a 40 px panel: medium cells (96) -> 10 columns, 5 rows. d.w = 1000 d.h = 600 d.bottom = 40 d } ///| test "icons fill the grid a column at a time" { let d = desk() assert_eq(d.grid(), (10, 5)) let a = apps(20) let cells = d.layout(a) assert_eq(cells.length(), 12) // "12" apps shown by default assert_eq(cells[0], (0, 0)) assert_eq(cells[4], (0, 4)) assert_eq(cells[5], (1, 0)) ignore(d.set_config("count", "All")) assert_eq(d.layout(a).length(), 20) } ///| test "click launches, drag moves and swaps, positions round-trip through the setting" { let d = desk() let a = apps(12) // Cell (0,0) holds app0 at x 20..116, y 20..116. assert_eq(d.app_at(a, 60, 60), "app0") assert_eq(d.app_at(a, 5, 5), "") ignore(d.handle(a, PointerDown({ x: 60, y: 60 }))) assert_eq(d.handle(a, PointerUp({ x: 61, y: 61 })), (true, Launch("app0"))) // Drag app0 onto app5's cell (1,0): they swap. ignore(d.handle(a, PointerDown({ x: 60, y: 60 }))) ignore(d.handle(a, PointerMove({ x: 120, y: 62 }))) assert_true(d.dragging) assert_eq(d.handle(a, PointerUp({ x: 160, y: 62 })), (true, NoAction)) let cells = d.layout(a) assert_eq(cells[0], (1, 0)) assert_eq(cells[5], (0, 0)) // Drag app1 to an empty cell far away. ignore(d.handle(a, PointerDown({ x: 60, y: 150 }))) ignore(d.handle(a, PointerMove({ x: 900, y: 400 }))) ignore(d.handle(a, PointerUp({ x: 900, y: 400 }))) assert_eq(d.layout(a)[1], (9, 3)) // The hidden setting keeps it. let saved = d.config_items().filter(c => c.key == "places")[0] assert_eq(saved.label, "") let d2 = desk() ignore(d2.set_config("places", saved.value)) assert_eq(d2.layout(a), d.layout(a)) // A place off the grid (a smaller screen) falls back to a free cell. d2.w = 400 assert_true(d2.layout(a)[1].0 < 3) } ///| test "keyboard moves the selection and launches" { let d = desk() let a = apps(12) ignore(d.handle(a, KeyDown("ArrowDown"))) assert_eq(d.selected, "app0") ignore(d.handle(a, KeyDown("ArrowDown"))) assert_eq(d.selected, "app1") ignore(d.handle(a, KeyDown("ArrowRight"))) assert_eq(d.selected, "app6") assert_eq(d.handle(a, KeyDown("Enter")), (false, Launch("app6"))) // A letter jumps to the next app with that initial. ignore(d.handle(a, KeyDown("k"))) assert_eq(d.selected, "app8") // Konsole8 (Kate9 comes after it) ignore(d.handle(a, KeyDown("Escape"))) assert_eq(d.selected, "") ignore(d.handle(a, FocusLost)) assert_false(d.focused) } ///| test "tapping with finger-roll drift launches without dragging or moving icons" { let d = desk() let a = apps(12) let before = d.layout(a) // 20 px drift inside cell (0,0): stays below the 32 px medium-cell drag threshold. ignore(d.handle(a, PointerDown({ x: 60, y: 60 }))) ignore(d.handle(a, PointerMove({ x: 80, y: 64 }))) assert_false(d.dragging) assert_eq(d.handle(a, PointerUp({ x: 80, y: 64 })), (true, Launch("app0"))) assert_eq(d.layout(a), before) // 15 px drift starting near the right edge of cell (0,0) and crossing into cell (1,0) still // launches app0 without moving any icon. ignore(d.handle(a, PointerDown({ x: 110, y: 60 }))) ignore(d.handle(a, PointerMove({ x: 125, y: 60 }))) assert_false(d.dragging) assert_eq(d.handle(a, PointerUp({ x: 125, y: 60 })), (true, Launch("app0"))) assert_eq(d.layout(a), before) } ///| test "launching and reordering apps by recency keeps every icon in its cell" { let a = apps(12) // Simulate an app launched from elsewhere (places empty): first-seen order keeps cells stable. let d_unpinned = desk() let expected_by_id : Map[String, (Int, Int)] = {} let initial = d_unpinned.layout(a) for i, app in a { expected_by_id.set(app.id, initial[i]) } let reordered = [a[0], a[1], a[2], a[3], a[4], a[7], a[5], a[6], a[8], a[9], a[10], a[11]] let after_unpinned = d_unpinned.layout(reordered) for i, app in reordered { assert_eq(after_unpinned[i], expected_by_id.get(app.id).unwrap()) } // Launching app7 on the desktop pins existing cells and keeps stability across recents reorder, // off-grid rotation/resize fallback, and reload into a fresh Desk. let d = desk() ignore(d.layout(a)) // app7 is at cell (1, 2): x = 20 + 96 + 40 = 156, y = 20 + 2 * 96 + 40 = 252. ignore(d.handle(a, PointerDown({ x: 156, y: 252 }))) assert_eq(d.handle(a, PointerUp({ x: 156, y: 252 })), (true, Launch("app7"))) let after_launch = d.layout(reordered) for i, app in reordered { assert_eq(after_launch[i], expected_by_id.get(app.id).unwrap()) } // Portrait resize where column >= 1 falls off-grid: off-grid apps fall back in seen order, // not recents order (app5 before app6 before app7). d.w = 220 d.h = 1200 let portrait = d.layout(reordered) let p_by_id : Map[String, (Int, Int)] = {} for i, app in reordered { p_by_id.set(app.id, portrait[i]) } assert_true(p_by_id.get("app5").unwrap().1 < p_by_id.get("app6").unwrap().1) assert_true(p_by_id.get("app6").unwrap().1 < p_by_id.get("app7").unwrap().1) // Saved places round-trip into a fresh Desk with the reordered catalog. let saved = d.config_items().filter(c => c.key == "places")[0] let d2 = desk() ignore(d2.set_config("places", saved.value)) let reloaded = d2.layout(reordered) for i, app in reordered { assert_eq(reloaded[i], expected_by_id.get(app.id).unwrap()) } } ///| test "dragging past threshold and dropping on the same cell cancels cleanly" { let d = desk() let a = apps(12) let before = d.layout(a) let places_val = fn(x : @icondesk.Desk) { x.config_items().filter(c => c.key == "places")[0].value } assert_eq(places_val(d), "") // Move 45 px (> 32 px threshold) while staying inside cell (0,0) (20..116). ignore(d.handle(a, PointerDown({ x: 60, y: 60 }))) ignore(d.handle(a, PointerMove({ x: 105, y: 60 }))) assert_true(d.dragging) assert_eq(d.handle(a, PointerUp({ x: 105, y: 60 })), (true, NoAction)) assert_eq(d.layout(a), before) assert_eq(places_val(d), "") }