diff --git a/.github/workflows/update.yml b/.github/workflows/update.yml new file mode 100644 index 000000000..be10934e6 --- /dev/null +++ b/.github/workflows/update.yml @@ -0,0 +1,36 @@ +name: App updates + +on: + workflow_dispatch: + push: + branches: [main] + paths: ['crates/update/**', 'Cargo.toml', 'Cargo.lock', 'rustfmt.toml', 'clippy.toml', '.github/workflows/update.yml'] + pull_request: + paths: ['crates/update/**', 'Cargo.toml', 'Cargo.lock', 'rustfmt.toml', 'clippy.toml', '.github/workflows/update.yml'] + +permissions: + contents: read + +jobs: + update: + timeout-minutes: 15 + strategy: + fail-fast: false + matrix: + os: [ubuntu-latest, windows-latest, macos-latest] + runs-on: ${{ matrix.os }} + steps: + - uses: actions/checkout@v4 + with: + # The token isn't needed after checkout; cargo runs build scripts and proc macros from + # dependencies, so don't leave it in .git/config for that third-party code to read. + persist-credentials: false + - uses: dtolnay/rust-toolchain@b725eda525362e7cf34af9a7243cac1e3f01a7ec # stable + with: + components: rustfmt, clippy + - run: cargo fmt --check + # Default (no egui) and the egui feature both build and lint cleanly. + - run: cargo clippy -p craft-update --all-targets --locked -- -D warnings + - run: cargo clippy -p craft-update --all-targets --locked --features egui -- -D warnings + - run: cargo test -p craft-update --locked -- --nocapture + - run: cargo test -p craft-update --locked --features egui -- --nocapture diff --git a/Cargo.lock b/Cargo.lock index 192a05c0a..8c3e4764e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -922,7 +922,7 @@ name = "craft-settings" version = "0.1.0" dependencies = [ "craft-atomic", - "dirs", + "dirs 7.0.0", "serde", "tempfile", "toml_edit", @@ -945,11 +945,38 @@ dependencies = [ "serde_json", ] +[[package]] +name = "craft-update" +version = "0.1.0" +dependencies = [ + "chrono", + "dirs 6.0.0", + "dunce", + "egui", + "flate2", + "getrandom 0.3.4", + "log", + "plist", + "semver", + "serde", + "serde_json", + "sha2", + "tar", + "tempfile", + "ureq", + "verifysign", + "winreg", + "winsafe", + "zip", +] + [[package]] name = "craft-window" version = "0.1.0" dependencies = [ "egui", + "objc2 0.6.5", + "objc2-app-kit 0.3.2", "wayland-client", ] @@ -1068,6 +1095,12 @@ dependencies = [ "syn 2.0.119", ] +[[package]] +name = "deranged" +version = "0.5.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9e9de72ce2ad1f90dc62fa25f0f430ef85eb4b0d8fa0be4f30373bc40a21d28e" + [[package]] name = "digest" version = "0.10.7" @@ -1078,6 +1111,15 @@ dependencies = [ "crypto-common", ] +[[package]] +name = "dirs" +version = "6.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3e8aa94d75141228480295a7d0e7feb620b1a5ad9f12bc40be62411e38cce4e" +dependencies = [ + "dirs-sys", +] + [[package]] name = "dirs" version = "7.0.0" @@ -1750,6 +1792,18 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "getrandom" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "899def5c37c4fd7b2664648c28120ecec138e4d395b459e5ca34f9cce2dd77fd" +dependencies = [ + "cfg-if", + "libc", + "r-efi 5.3.0", + "wasip2", +] + [[package]] name = "getrandom" version = "0.4.3" @@ -1759,7 +1813,7 @@ dependencies = [ "cfg-if", "js-sys", "libc", - "r-efi", + "r-efi 6.0.0", "rand_core 0.10.1", "wasm-bindgen", ] @@ -2953,6 +3007,12 @@ dependencies = [ "windows-sys 0.61.2", ] +[[package]] +name = "num-conv" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "521739c6d2bac4aa25192232afe6841231376b2b26d4d9fae5ecf8ca5772e441" + [[package]] name = "num-traits" version = "0.2.19" @@ -3533,6 +3593,19 @@ version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b4596b6d070b27117e987119b4dac604f3c58cfb0b191112e24771b2faeac1a6" +[[package]] +name = "plist" +version = "1.10.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "327a4cb2ad49dc48adfdc9da89e22c64d488b508c67a044d71ba729e4521a285" +dependencies = [ + "base64 0.23.1", + "indexmap", + "quick-xml 0.42.0", + "serde", + "time", +] + [[package]] name = "png" version = "0.17.16" @@ -3626,6 +3699,12 @@ dependencies = [ "zerovec", ] +[[package]] +name = "powerfmt" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4a6394b9e965e73d0a289ee54f589087e2c676aedf60885baf52c76b771e4958" + [[package]] name = "presser" version = "0.3.1" @@ -3727,6 +3806,15 @@ dependencies = [ "memchr", ] +[[package]] +name = "quick-xml" +version = "0.42.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41b1177fdf999d2321d3fb46ff47159d9c1fb9ad66a4879f8c50a0b504615e9b" +dependencies = [ + "memchr", +] + [[package]] name = "quinn" version = "0.11.12" @@ -3793,6 +3881,12 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "r-efi" +version = "5.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f" + [[package]] name = "r-efi" version = "6.0.0" @@ -4113,12 +4207,14 @@ dependencies = [ [[package]] name = "rustls" -version = "0.23.45" +version = "0.23.46" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0d41d731c7d2f962d1ccc364cec258de3c0e93b38c2fb3ba97ac74513048d634" +checksum = "48e13bd8c0e9365c43cfa5c9e8f9ad49d3c8444926c9aac819e0e4dc503c8fdf" dependencies = [ "aws-lc-rs", + "log", "once_cell", + "ring", "rustls-pki-types", "rustls-webpki", "subtle", @@ -4292,6 +4388,10 @@ name = "semver" version = "1.0.28" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8a7852d02fc848982e0c167ef163aaff9cd91dc640ba85e263cb1ce46fae51cd" +dependencies = [ + "serde", + "serde_core", +] [[package]] name = "send_wrapper" @@ -4982,6 +5082,36 @@ dependencies = [ "syn 3.0.6", ] +[[package]] +name = "time" +version = "0.3.55" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cdb87b95ec50ddfa440816d227a17b2ccbdda963a316a727fda0fc4334f7d134" +dependencies = [ + "deranged", + "num-conv", + "powerfmt", + "serde_core", + "time-core", + "time-macros", +] + +[[package]] +name = "time-core" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9e1c906769ad99c88eaa54e728060edef082f8e358ff32030cb7c7d315e81109" + +[[package]] +name = "time-macros" +version = "0.2.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7e689342a48d2ea927c87ea50cabf8594854bf940e9310208848d680d668ed85" +dependencies = [ + "num-conv", + "time-core", +] + [[package]] name = "tiny-skia" version = "0.11.4" @@ -5247,6 +5377,12 @@ dependencies = [ "rustc-hash 2.1.3", ] +[[package]] +name = "typed-path" +version = "0.12.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "54ec2755506471f54baf5e4bb5499d8e510cc1caf271270e778a736bd2d7336c" + [[package]] name = "typenum" version = "1.20.1" @@ -5345,6 +5481,35 @@ version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1" +[[package]] +name = "ureq" +version = "3.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cde93ff05758a36a851a398a772b0b79a82fbe3c186a46f2696baaeaf84c966b" +dependencies = [ + "base64 0.23.1", + "flate2", + "log", + "percent-encoding", + "rustls", + "rustls-pki-types", + "rustls-platform-verifier", + "ureq-proto", + "utf8-zero", +] + +[[package]] +name = "ureq-proto" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "841583fc36089d7d59419568ae157f43a5a03a2e5a81ba87c77d1596ef46e922" +dependencies = [ + "base64 0.23.1", + "http", + "httparse", + "log", +] + [[package]] name = "url" version = "2.5.8" @@ -5385,6 +5550,12 @@ version = "1.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c8232dd3cdaed5356e0f716d285e4b40b932ac434100fe9b7e0e8e935b9e6246" +[[package]] +name = "utf8-zero" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8c0a043c9540bae7c578c88f91dda8bd82e59ae27c21baca69c8b191aaf5a6e" + [[package]] name = "utf8_iter" version = "1.0.4" @@ -5476,6 +5647,16 @@ dependencies = [ "vello_common 0.2.0", ] +[[package]] +name = "verifysign" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6acfcc42a8d44f340d001193d97ef56bf33210b51ad760ae8308c97d5241ca51" +dependencies = [ + "core-foundation 0.10.1", + "windows-sys 0.61.2", +] + [[package]] name = "version_check" version = "0.9.5" @@ -5516,6 +5697,15 @@ version = "0.11.1+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b" +[[package]] +name = "wasip2" +version = "1.0.4+wasi-0.2.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b67efb37e106e55ce722a510d6b5f9c17f083e5fc79afc2badeb12cc313d9487" +dependencies = [ + "wit-bindgen", +] + [[package]] name = "wasm-bindgen" version = "0.2.129" @@ -5643,7 +5833,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "338e30461b3a2b67d70eb30a6d89f8e0c93a833e07d2ae89085cd070c4a00ac0" dependencies = [ "proc-macro2", - "quick-xml", + "quick-xml 0.41.0", "quote", ] @@ -6171,12 +6361,28 @@ dependencies = [ "xml", ] +[[package]] +name = "winreg" +version = "0.55.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb5a765337c50e9ec252c2069be9bf91c7df47afb103b642ba3a53bf8101be97" +dependencies = [ + "cfg-if", + "windows-sys 0.59.0", +] + [[package]] name = "winsafe" version = "0.0.29" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9ef0ffc427f045c0cc9ebffd6f4f91153dcd2a1547b5c3c29ee3f541e16e95c6" +[[package]] +name = "wit-bindgen" +version = "0.57.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ebf944e87a7c253233ad6766e082e3cd714b5d03812acc24c318f549614536e" + [[package]] name = "write-fonts" version = "0.48.1" @@ -6517,6 +6723,20 @@ dependencies = [ "syn 3.0.6", ] +[[package]] +name = "zip" +version = "8.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2d04a6b5381502aa6087c94c669499eb1602eb9c5e8198e534de571f7154809b" +dependencies = [ + "crc32fast", + "flate2", + "indexmap", + "memchr", + "typed-path", + "zopfli", +] + [[package]] name = "zlib-rs" version = "0.6.8" @@ -6529,6 +6749,18 @@ version = "1.0.23" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "29666d0abbfad1e3dc4dcf6144730dd3a3ab225bbbdac83319345b1b44ccfc1b" +[[package]] +name = "zopfli" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aaf7fc5d30c28483d93805c4a5e12b05bbb52407fa67c5f8bd552374cd01fb11" +dependencies = [ + "bumpalo", + "crc32fast", + "log", + "simd-adler32", +] + [[package]] name = "zune-core" version = "0.5.3" diff --git a/Cargo.toml b/Cargo.toml index d89872482..0feef09b2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -36,6 +36,7 @@ craft-print = { path = "crates/print" } craft-quit = { path = "crates/quit" } craft-settings = { path = "crates/settings" } craft-ui = { path = "crates/ui" } +craft-update = { path = "crates/update" } craft-window = { path = "crates/window" } egui = { version = "0.37", default-features = false } rayon = "1" diff --git a/crates/ui/src/docking/chrome.rs b/crates/ui/src/docking/chrome.rs index 6ac7466b5..41440f308 100644 --- a/crates/ui/src/docking/chrome.rs +++ b/crates/ui/src/docking/chrome.rs @@ -32,6 +32,8 @@ pub struct ChromeStyle { pub text: Color32, pub weak_text: Color32, pub accent: Color32, + /// Fill of a selected or hovered control, such as an icon rail's open item. + pub active_background: Color32, pub font: FontId, /// Mirror the title bar: buttons at the left, the title at the right. pub right_to_left: bool, @@ -39,7 +41,23 @@ pub struct ChromeStyle { impl ChromeStyle { pub fn from_ui(ui: &Ui) -> Self { - Self::from_dock(&DockStyle::from_ui(ui)) + Self::from_visuals(ui.visuals()) + } + + /// The colors [`Self::from_dock`] takes from [`DockStyle::from_visuals`], without building + /// the dock style's labels. + pub fn from_visuals(visuals: &egui::Visuals) -> Self { + Self { + background: visuals.window_fill, + stroke: visuals.window_stroke, + title_background: visuals.extreme_bg_color, + text: visuals.text_color(), + weak_text: visuals.weak_text_color(), + accent: visuals.selection.stroke.color, + active_background: visuals.widgets.active.bg_fill, + font: FontId::proportional(12.0), + right_to_left: false, + } } pub fn from_dock(style: &DockStyle) -> Self { @@ -50,6 +68,7 @@ impl ChromeStyle { text: style.text, weak_text: style.inactive_text, accent: style.accent, + active_background: style.active_background, font: style.font.clone(), right_to_left: style.right_to_left, } diff --git a/crates/ui/src/docking/command.rs b/crates/ui/src/docking/command.rs index 99825f1d3..7db19eb40 100644 --- a/crates/ui/src/docking/command.rs +++ b/crates/ui/src/docking/command.rs @@ -340,7 +340,24 @@ impl DockCommand

{ /// [`super::float_space`] of the area that draws `workspace`. Given `x`, `y`, `width` and /// `height` replace the matching values, and a group or column that is already a whole /// floating window keeps its rect. + /// + /// An outer edge dock wraps the part between the docks. [`Self::to_actions_with`] also + /// knows which panels are protected, so it wraps the canvas column where a drag would. pub fn to_actions(&self, workspace: &Workspace

, right_to_left: bool, space: &FloatSpace

) -> Result>, DockError> { + self.to_actions_with(workspace, right_to_left, space, |_| Permissions::default()) + } + + /// [`Self::to_actions`] with the `permissions` the actions are applied with. An outer edge + /// dock wraps what a drag to that edge would: the canvas column where the area narrows its + /// edge band to it, else the part between the docks. `space` says whether the area draws + /// with [`super::DockStyle::side_edge_zones`], which never narrows. + pub fn to_actions_with( + &self, + workspace: &Workspace

, + right_to_left: bool, + space: &FloatSpace

, + permissions: impl Fn(&P) -> Permissions, + ) -> Result>, DockError> { let layout = &workspace.layout; let zoned = Zoned { layout, right_to_left }; let shown = |panel: &P| if layout.contains(panel) { Ok(()) } else { Err(DockError::MissingPanel) }; @@ -418,12 +435,13 @@ impl DockCommand

{ if before.is_some() { return Err(DockError::InvalidPlacement); } - let anchor = layout - .panels() - .into_iter() - .find(|p| !members.contains(p) && matches!(layout.place(p), Some(Place::Dock { .. } | Place::Node(_)))) - .cloned() - .ok_or(DockError::MissingTarget)?; + // The model narrows the edge on a protected anchor, as `Workspace::apply` checks it. + let protected = |p: &P| { + let policy = if workspace.locked { permissions(p).locked() } else { permissions(p) }; + policy.is_protected() + }; + let narrowed = !space.side_edge_zones && layout.edge_narrows(zone, &protected); + let anchor = layout.edge_anchor(narrowed, &protected, &|p| members.contains(p)).ok_or(DockError::MissingTarget)?; return one(unit.to(layout, panel, Target::At { anchor, placement: Placement::Edge(zone) })?); } if !matches!(layout.place(panel), Some(Place::Floating { .. })) { @@ -504,7 +522,7 @@ impl DockCommand

{ /// location, and a panel never laid out opens as [`Action::Open`] without an anchor does /// ([`Workspace::set_visible`]); Lock sets [`Workspace::locked`]. Application-owned commands /// ([`Self::is_app_owned`]) return [`DockError::NotAllowed`] and change nothing. - /// `space` is where `window.panel.float` floats, as for [`Self::to_actions`]. + /// `space` is where `window.panel.float` floats, as for [`Self::to_actions_with`]. pub fn apply( &self, workspace: &mut Workspace

, @@ -526,7 +544,7 @@ impl DockCommand

{ // Shown, then moved to the edge, all or nothing. let mut shown = workspace.clone(); shown.set_visible(panel.clone(), true, None, &permissions)?; - let closed = shown.apply_all(self.to_actions(&shown, right_to_left, space)?, permissions)?; + let closed = shown.apply_all(self.to_actions_with(&shown, right_to_left, space, &permissions)?, &permissions)?; *workspace = shown; return Ok(closed); } @@ -534,7 +552,7 @@ impl DockCommand

{ workspace.locked = locked.unwrap_or(!workspace.locked); return Ok(Vec::new()); } - let actions = self.to_actions(workspace, right_to_left, space)?; + let actions = self.to_actions_with(workspace, right_to_left, space, &permissions)?; workspace.apply_all(actions, permissions) } } diff --git a/crates/ui/src/docking/command_tests.rs b/crates/ui/src/docking/command_tests.rs index 78d406b10..c6ac3f1d7 100644 --- a/crates/ui/src/docking/command_tests.rs +++ b/crates/ui/src/docking/command_tests.rs @@ -45,7 +45,7 @@ fn parse(params: Value) -> Result, serde_json::Error> { /// The actions `params` makes on a fresh workspace. fn actions(params: Value) -> Vec> { let command = parse(params.clone()).unwrap_or_else(|e| panic!("{params}: {e}")); - command.to_actions(&workspace(), false, &space()).unwrap_or_else(|e| panic!("{params}: {e}")) + command.to_actions_with(&workspace(), false, &space(), permissions).unwrap_or_else(|e| panic!("{params}: {e}")) } /// Apply `params` to a fresh workspace; return it and the panels closed. @@ -294,7 +294,7 @@ fn dock_commands_parse_every_documented_shape_and_map_to_atomic_actions() { fn side_zones_map_to_the_placement_the_anchor_container_takes_in_either_direction() { let command = |zone: &str, anchor: &str| parse(json!({"command": "window.panel.move", "panel": "history", "anchor": anchor, "zone": zone})).unwrap(); - let placed = |zone: &str, anchor: &str, rtl: bool| match command(zone, anchor).to_actions(&workspace(), rtl, &space()) { + let placed = |zone: &str, anchor: &str, rtl: bool| match command(zone, anchor).to_actions_with(&workspace(), rtl, &space(), permissions) { Ok(actions) => match actions.as_slice() { [Action::Move { placement, .. }] => Ok(placement.clone()), other => panic!("{other:?}"), @@ -338,7 +338,7 @@ fn float_without_a_rect_takes_the_drag_size_inside_the_dock_area() { let d = testing::DockDriver::new(workspace().layout, Sized).with_area(area); let space = float_space::

(&d.ctx, d.id).expect("the area records its float space"); assert_eq!(space.area, area); - let float = |params: Value| parse(params).unwrap().to_actions(&workspace(), false, &space).unwrap(); + let float = |params: Value| parse(params).unwrap().to_actions_with(&workspace(), false, &space, permissions).unwrap(); let panel = float(json!({"command": "window.panel.float", "panel": "layers"})); // The floating_size width, and an empty body's least fitted height under its 150 points. @@ -359,19 +359,25 @@ fn float_without_a_rect_takes_the_drag_size_inside_the_dock_area() { // Without recorded sizes a unit floats at the layout's size, shrunk into a small area. let small = FloatSpace::new(egui::Rect::from_min_size(egui::pos2(0.0, 30.0), egui::vec2(200.0, 200.0))); - let applied = parse(json!({"command": "window.panel.float", "panel": "layers", "group": true})).unwrap().to_actions(&workspace(), false, &small); + let applied = parse(json!({"command": "window.panel.float", "panel": "layers", "group": true})).unwrap().to_actions_with( + &workspace(), + false, + &small, + permissions, + ); assert_eq!(applied, Ok(vec![Action::MoveGroup { panel: s("layers"), to: Target::Float { rect: Some([0.0, 30.0, 200.0, 200.0]) } }])); // A host seeds the size of a panel the area never drew; a recorded size still wins. let seeded = FloatSpace::new(area).with_panel_size(s("layers"), egui::vec2(180.0, 240.0)); - let applied = parse(json!({"command": "window.panel.float", "panel": "layers"})).unwrap().to_actions(&workspace(), false, &seeded); + let applied = + parse(json!({"command": "window.panel.float", "panel": "layers"})).unwrap().to_actions_with(&workspace(), false, &seeded, permissions); assert_eq!(applied, Ok(vec![Action::Float { panel: s("layers"), rect: [24.0, 54.0, 180.0, 240.0] }])); let recorded = space.clone().with_panel_size(s("layers"), egui::vec2(180.0, 240.0)); assert_eq!(float_with(&recorded, "layers"), vec![Action::Float { panel: s("layers"), rect: [24.0, 54.0, 200.0, 120.0] }]); } fn float_with(space: &FloatSpace

, panel: &str) -> Vec> { - parse(json!({"command": "window.panel.float", "panel": panel})).unwrap().to_actions(&workspace(), false, space).unwrap() + parse(json!({"command": "window.panel.float", "panel": panel})).unwrap().to_actions_with(&workspace(), false, space, permissions).unwrap() } #[test] @@ -411,6 +417,35 @@ fn dock_with_a_side_zone_and_no_anchor_makes_an_outer_edge_frame() { rejected(json!({"command": "window.panel.dock", "panel": "color", "zone": "left", "before": null}), DockError::InvalidPlacement); } +/// The frame beside `panel`'s own: what an edge split with `panel` wrapped. +fn wrapped(layout: &Layout

, panel: &str) -> Option> { + let Some(dock_model::Place::Node(path)) = layout.place(&s(panel)) else { return None }; + let (last, parent) = path.split_last()?; + layout.root.as_ref().and_then(|root| dock_model::node_at(root, &[parent, &[!last]].concat())).cloned() +} + +#[test] +fn an_outer_edge_dock_wraps_the_canvas_column_whatever_the_tree_order() { + let tabs = |panel: &str| Node::Tabs { panels: vec![s(panel)], active: 0 }; + let side = + |first, second| Node::Split { axis: SplitAxis::Horizontal, size: SplitSize::Ratio(0.5), first: Box::new(first), second: Box::new(second) }; + let canvas_first = side(tabs("canvas"), side(tabs("layers"), tabs("history"))); + let canvas_later = side(side(tabs("layers"), tabs("canvas")), tabs("history")); + for root in [canvas_first, canvas_later] { + let layout = Layout { root: Some(root), floating: vec![Floating::single(vec![s("color")], 0, [300.0, 200.0, 200.0, 150.0])] }; + let command = parse(json!({"command": "window.panel.dock", "panel": "color", "zone": "top"})).unwrap(); + let mut workspace = Workspace::new(layout.clone()); + command.apply(&mut workspace, false, &space(), permissions).unwrap(); + assert_eq!(wrapped(&workspace.layout, "color"), Some(tabs("canvas")), "{:?}", workspace.layout); + // An area drawn with side edge zones never narrows its edge to the canvas column. + let mut workspace = Workspace::new(layout.clone()); + command.apply(&mut workspace, false, &space().with_side_edge_zones(true), permissions).unwrap(); + let mut whole = layout; + whole.floating.clear(); + assert_eq!(wrapped(&workspace.layout, "color"), whole.root, "{:?}", workspace.layout); + } +} + #[test] fn dock_to_the_side_a_panel_already_leads_the_last_column_of() { // Layers leads the dock's only column: it moves out to a new column after it. diff --git a/crates/ui/src/docking/dock_model.rs b/crates/ui/src/docking/dock_model.rs index 9abd4082d..0c3185009 100644 --- a/crates/ui/src/docking/dock_model.rs +++ b/crates/ui/src/docking/dock_model.rs @@ -326,6 +326,35 @@ pub(super) fn split_frame

(root: &mut Node

, path: &[bool], new: Node

, zo true } +/// [`split_frame`] with the sizes of [`Placement::SplitSized`]: the new split takes `size`, and +/// the nearest enclosing split whose divider runs along the frame's `zone` side takes `outer`. +/// False when there is no such split, or a dock is beyond it. +pub(super) fn split_frame_sized

(root: &mut Node

, path: &[bool], new: Node

, zone: Zone, size: SplitSize, outer: SplitSize) -> bool { + let axis = if matches!(zone, Zone::Top | Zone::Bottom) { SplitAxis::Vertical } else { SplitAxis::Horizontal }; + // A divider on the frame's start side (left or top) belongs to a split holding the frame + // in its second child. + let start = matches!(zone, Zone::Left | Zone::Top); + let enclosing = (0..path.len()).rev().find(|&depth| { + path.get(depth) == Some(&start) && matches!(node_at(root, &path[..depth]), Some(Node::Split { axis: own, .. }) if *own == axis) + }); + let Some(depth) = enclosing else { return false }; + let Some(Node::Split { size: own, first, second, .. }) = node_at_mut(root, &path[..depth]) else { return false }; + if zone == Zone::Center || matches!(**first, Node::Dock { .. }) || matches!(**second, Node::Dock { .. }) { + return false; + } + *own = outer; + if !split_frame(root, path, new, zone) { + return false; + } + match node_at_mut(root, path) { + Some(Node::Split { size: own, .. }) => { + *own = size; + true + } + _ => false, + } +} + /// Insert `new` at `index` and select the member at `index + selected`. fn splice

(panels: &mut Vec

, active: &mut usize, index: usize, new: Vec

, selected: usize) { let index = index.min(panels.len()); @@ -335,6 +364,66 @@ fn splice

(panels: &mut Vec

, active: &mut usize, index: usize, new: Vec

, } impl Layout

{ + /// The layout without its sizes and rectangles: which panels are where, and their state. + /// A run of splits along one axis nests one way (`a | (b | c)`), since nesting it the + /// other way only moves sizes between its members. + pub(super) fn shape(&self) -> Layout

{ + fn run<'a, P>(node: &'a Node

, axis: SplitAxis, out: &mut Vec<&'a Node

>) { + match node { + Node::Split { axis: own, first, second, .. } if *own == axis => { + run(first, axis, out); + run(second, axis, out); + } + node => out.push(node), + } + } + fn node(n: &Node

) -> Node

{ + match n { + Node::Split { axis, .. } => { + let mut members = Vec::new(); + run(n, *axis, &mut members); + let mut shaped = members.into_iter().rev().map(node); + let last = shaped.next().unwrap_or(Node::Tabs { panels: Vec::new(), active: 0 }); + shaped.fold(last, |second, first| Node::Split { + axis: *axis, + size: SplitSize::Ratio(0.5), + first: Box::new(first), + second: Box::new(second), + }) + } + Node::Tabs { panels, active } => Node::Tabs { panels: panels.clone(), active: *active }, + Node::Stack { entries } => Node::Stack { entries: entries.iter().map(|e| super::StackEntry { height: None, ..e.clone() }).collect() }, + Node::Dock { side, columns } => Node::Dock { + side: *side, + columns: columns + .iter() + .map(|c| Column { + groups: c.groups.iter().map(|g| Group { height: None, ..g.clone() }).collect(), + width: 0.0, + iconic: c.iconic, + iconic_width: None, + }) + .collect(), + }, + } + } + Layout { + root: self.root.as_ref().map(node), + floating: self + .floating + .iter() + .map(|w| Floating { root: node(&w.root), rect: [0.0; 4], native: None, monitor: None, monitor_size: None, ..w.clone() }) + .collect(), + } + } + + /// Whether `other` arranges the panels as this layout does: equal apart from split sizes, + /// column widths, group heights and window rectangles. A drop that leads from one to the + /// other changes nothing the user would see as a move. + pub(super) fn same_arrangement(&self, other: &Self) -> bool { + self.shape() == other.shape() + } + pub(super) fn place(&self, panel: &P) -> Option { for (w, window) in self.floating.iter().enumerate() { for (leaf, path) in leaves(&window.root) { @@ -454,18 +543,32 @@ impl Layout

{ None } - /// The node an edge split wraps: the root, or the part between the docks. - pub(in crate::docking) fn edge_path(&self) -> Vec { + /// The node an edge split at `zone` wraps: the root, or the part between the docks. Where + /// neither side of a side-by-side split is a dock and only one holds a `canvas` panel, + /// that side: the canvas column, without the side frames. A top (bottom) edge also + /// descends a split of rows into its first (last) row when only that row holds a `canvas` + /// panel, so a full-width row below (above) the canvas keeps its edge. + pub(in crate::docking) fn edge_path(&self, zone: Zone, canvas: &dyn Fn(&P) -> bool) -> Vec { + let holds = |node: &Node

| tree_panels(node).into_iter().any(canvas); let mut path = Vec::new(); let mut node = self.root.as_ref(); - while let Some(Node::Split { axis: SplitAxis::Horizontal, first, second, .. }) = node { + while let Some(Node::Split { axis, first, second, .. }) = node { if path.len() > MAX_DEPTH { break; } - let second_side = match (&**first, &**second) { - (Node::Dock { .. }, _) => true, - (_, Node::Dock { .. }) => false, - _ => break, + let second_side = match (axis, &**first, &**second) { + (SplitAxis::Horizontal, Node::Dock { .. }, _) => true, + (SplitAxis::Horizontal, _, Node::Dock { .. }) => false, + (SplitAxis::Vertical, ..) => match (zone, holds(first), holds(second)) { + (Zone::Top, true, false) => false, + (Zone::Bottom, false, true) => true, + _ => break, + }, + _ => match (holds(first), holds(second)) { + (true, false) => false, + (false, true) => true, + _ => break, + }, }; path.push(second_side); node = Some(if second_side { second } else { first }); @@ -473,6 +576,22 @@ impl Layout

{ path } + /// Whether an edge drop at `zone` that narrows by `canvas` wraps less than one that does + /// not: the edge area is narrower than the part between the docks. + pub(in crate::docking) fn edge_narrows(&self, zone: Zone, canvas: &dyn Fn(&P) -> bool) -> bool { + self.edge_path(zone, canvas) != self.edge_path(zone, &|_| false) + } + + /// The docked panel, other than `skip`, that a [`Placement::Edge`] anchors on so the drop + /// wraps the edge area it shows: a `protected` one where that area is narrowed to the + /// canvas column (`narrowed`), else one that is not protected, since a protected anchor + /// narrows the model's edge. Any docked panel when every one is protected. + pub(in crate::docking) fn edge_anchor(&self, narrowed: bool, protected: &dyn Fn(&P) -> bool, skip: &dyn Fn(&P) -> bool) -> Option

{ + let docked: Vec<&P> = + self.panels().into_iter().filter(|p| !skip(p) && matches!(self.place(p), Some(Place::Dock { .. } | Place::Node(_)))).collect(); + docked.iter().find(|p| protected(p) == narrowed).or(docked.first()).map(|p| (*p).clone()) + } + pub(super) fn columns_mut(&mut self, path: &[bool]) -> Option<&mut Vec>> { match self.root.as_mut().and_then(|root| node_at_mut(root, path)) { Some(Node::Dock { columns, .. }) => Some(columns), @@ -666,7 +785,8 @@ impl Layout

{ } /// Insert groups taken by [`Self::take_unit`] (or a new single-panel group). - pub(super) fn insert_moving(&mut self, moving: Moving

, to: Target

) -> Result<(), DockError> { + /// `canvas` is the protected-panel test an outer edge drop narrows by ([`Self::edge_path`]). + pub(super) fn insert_moving(&mut self, moving: Moving

, to: Target

, canvas: &dyn Fn(&P) -> bool) -> Result<(), DockError> { if moving.groups.is_empty() || moving.groups.iter().any(|g| g.panels.is_empty()) { return Err(DockError::MissingPanel); } @@ -732,10 +852,22 @@ impl Layout

{ let root = self.tree_mut(window).ok_or(DockError::MissingTarget)?; if split_frame(root, path, new, zone) { Ok(()) } else { Err(DockError::MissingTarget) } } + Placement::SplitSized { zone, size, outer } => { + let (window, path) = place.tree_path().ok_or(DockError::InvalidPlacement)?; + let new = match moving.node { + Some(node) => node, + None => { + let (panels, active) = flatten(moving.groups); + Node::Tabs { panels, active } + } + }; + let root = self.tree_mut(window).ok_or(DockError::MissingTarget)?; + if split_frame_sized(root, path, new, zone, size, outer) { Ok(()) } else { Err(DockError::InvalidPlacement) } + } // A floating window is a tree: a group above or below splits the anchor's group. Placement::Group { below, .. } if matches!(place, Place::Floating { .. }) => { let zone = if below { Zone::Bottom } else { Zone::Top }; - self.insert_moving(moving, Target::At { anchor, placement: Placement::Split(zone) }) + self.insert_moving(moving, Target::At { anchor, placement: Placement::Split(zone) }, canvas) } Placement::Group { below, height } => { if !valid_height(height) { @@ -757,7 +889,8 @@ impl Layout

{ if zone == Zone::Center || matches!(place, Place::Floating { .. }) { return Err(DockError::InvalidPlacement); } - let path = self.edge_path(); + // A protected anchor narrows the edge to the canvas column. + let path = if canvas(&anchor) { self.edge_path(zone, canvas) } else { self.edge_path(zone, &|_| false) }; let node = self.root.as_mut().and_then(|root| node_at_mut(root, &path)).ok_or(DockError::MissingTarget)?; if matches!(node, Node::Dock { .. }) { return Err(DockError::InvalidPlacement); diff --git a/crates/ui/src/docking/drop_tests.rs b/crates/ui/src/docking/drop_tests.rs index 8a7fe6f33..efe44c5d4 100644 --- a/crates/ui/src/docking/drop_tests.rs +++ b/crates/ui/src/docking/drop_tests.rs @@ -195,3 +195,312 @@ fn the_root_edge_band_is_28_pt_and_previews_a_line_along_the_layouts_edge() { let out = d.drag(tab(&d, 3), pos2(x, 7.0)); assert!(matches!(&out.actions[..], [Action::Move { panel: 3, placement: Placement::Edge(Zone::Top), .. }]), "{:?}", out.actions); } + +fn tabs(panels: &[u32]) -> Node { + Node::Tabs { panels: panels.to_vec(), active: 0 } +} + +fn split(axis: SplitAxis, size: SplitSize, first: Node, second: Node) -> Node { + Node::Split { axis, size, first: Box::new(first), second: Box::new(second) } +} + +/// Side frame `[1]`, canvas 0 and side frame `[2, 3]` side by side, 200, 400 and 200 pt wide. +fn canvas_columns() -> Layout { + let sides = split(SplitAxis::Horizontal, SplitSize::FixedSecond(200.0), tabs(&[0]), Node::Tabs { panels: vec![2, 3], active: 1 }); + Layout { root: Some(split(SplitAxis::Horizontal, SplitSize::FixedFirst(200.0), tabs(&[1]), sides)), floating: Vec::new() } +} + +fn canvas_driver(canvas: Permissions, style: impl Fn(&mut DockStyle) + 'static) -> Driver { + DockDriver::new(canvas_columns(), Bodies { rules: [(0, canvas)].into(), ..Bodies::default() }).with_style(style) +} + +fn moves(out: &DockOutput) -> bool { + out.actions.iter().any(|a| matches!(a, Action::Move { .. } | Action::MovePanel { .. })) +} + +#[test] +fn between_side_frames_the_outer_edge_band_covers_only_the_canvas_column() { + let d = canvas_driver(Permissions::PROTECTED, |_| {}); + let (canvas, _) = group(&d, 0); + let (side, _) = group(&d, 1); + let at = pos2(canvas.center().x, 590.0); + let mut d = canvas_driver(Permissions::PROTECTED, |_| {}); + d.hold(tab(&d, 3), at); + assert!(d.filled(line(canvas, Zone::Bottom)), "a line along the canvas column's bottom"); + assert!(!d.filled(Rect::from_x_y_ranges(0.0..=800.0, 596.0..=600.0)), "no line along the window's bottom"); + let out = d.release(at); + assert_eq!(out.actions, vec![Action::Move { panel: 3, anchor: 0, placement: Placement::Edge(Zone::Bottom) }]); + // The side frames keep their full height. + let column = split(SplitAxis::Vertical, SplitSize::Ratio(0.75), tabs(&[0]), tabs(&[3])); + let sides = split(SplitAxis::Horizontal, SplitSize::FixedSecond(200.0), column, tabs(&[2])); + assert_eq!(d.layout.root, Some(split(SplitAxis::Horizontal, SplitSize::FixedFirst(200.0), tabs(&[1]), sides))); + // Under a side frame, its own bottom third. + let mut d = canvas_driver(Permissions::PROTECTED, |_| {}); + let out = d.drag(tab(&d, 3), pos2(side.center().x, 590.0)); + assert_eq!(out.actions, vec![Action::Move { panel: 3, anchor: 1, placement: Placement::Split(Zone::Bottom) }]); +} + +#[test] +fn between_docks_the_outer_edge_band_covers_the_part_between_them() { + let dock = |side, panels: &[u32]| Node::Dock { side, columns: vec![Column::new(vec![Group::new(panels.to_vec(), 0)], 200.0)] }; + let middle = split(SplitAxis::Vertical, SplitSize::Ratio(0.7), tabs(&[0]), tabs(&[4])); + let inner = split(SplitAxis::Horizontal, SplitSize::FixedSecond(200.0), middle.clone(), dock(Side::End, &[2, 3])); + let layout = + Layout { root: Some(split(SplitAxis::Horizontal, SplitSize::FixedFirst(200.0), dock(Side::Start, &[1]), inner)), floating: Vec::new() }; + let driver = || DockDriver::new(layout.clone(), Bodies { rules: [(0, Permissions::PROTECTED)].into(), ..Bodies::default() }); + let d = driver(); + let (canvas, _) = group(&d, 0); + let (timeline, _) = group(&d, 4); + let area = Rect::from_x_y_ranges(canvas.x_range(), (canvas.top() - 26.0)..=timeline.bottom()); + let at = pos2(canvas.center().x, 590.0); + let mut d = driver(); + d.hold(tab(&d, 3), at); + assert!(d.filled(line(area, Zone::Bottom)), "a line along the bottom between the docks"); + let out = d.release(at); + assert!(matches!(&out.actions[..], [Action::Move { panel: 3, placement: Placement::Edge(Zone::Bottom), .. }]), "{:?}", out.actions); + let Some(Node::Split { second, .. }) = &d.layout.root else { panic!("{:?}", d.layout) }; + let Node::Split { first, .. } = &**second else { panic!("{:?}", d.layout) }; + assert_eq!(**first, split(SplitAxis::Vertical, SplitSize::Ratio(0.75), middle, tabs(&[3]))); +} + +/// The frame beside `panel`'s own: what an edge split with `panel` wrapped. +fn wrapped(layout: &Layout, panel: u32) -> Option> { + let Some(dock_model::Place::Node(path)) = layout.place(&panel) else { return None }; + let (last, parent) = path.split_last()?; + layout.root.as_ref().and_then(|root| dock_model::node_at(root, &[parent, &[!last]].concat())).cloned() +} + +#[test] +fn over_a_full_width_row_the_top_edge_band_covers_only_the_canvas_column_above_it() { + // SoundCraft's Edit window: side frame 1, canvas 0 and side frame [2, 3] over a full-width + // editor 4. + let Some(upper) = canvas_columns().root else { panic!() }; + let root = split(SplitAxis::Vertical, SplitSize::Ratio(0.7), upper.clone(), tabs(&[4])); + let layout = Layout { root: Some(root), floating: Vec::new() }; + let driver = || DockDriver::new(layout.clone(), Bodies { rules: [(0, Permissions::PROTECTED)].into(), ..Bodies::default() }); + let d = driver(); + let (canvas, _) = group(&d, 0); + let at = pos2(canvas.center().x, 5.0); + let mut d = driver(); + d.hold(tab(&d, 3), at); + assert!(d.filled(Rect::from_x_y_ranges(canvas.x_range(), 0.0..=4.0)), "a line along the canvas column's top"); + assert!(!d.filled(Rect::from_x_y_ranges(0.0..=800.0, 0.0..=4.0)), "no line along the window's top"); + let out = d.release(at); + assert_eq!(out.actions, vec![Action::Move { panel: 3, anchor: 0, placement: Placement::Edge(Zone::Top) }]); + assert_eq!(wrapped(&d.layout, 3), Some(tabs(&[0])), "{:?}", d.layout); +} + +#[test] +fn an_outer_edge_drop_wraps_the_area_it_previews_when_the_app_applies_other_permissions() { + // Frame [2, 3] and canvas 0 beside frame 1, which is protected only while drawing (PDFCraft's + // bare tools mode): drawn, the edge band is not narrowed to the canvas. + let inner = split(SplitAxis::Horizontal, SplitSize::Ratio(0.5), Node::Tabs { panels: vec![2, 3], active: 1 }, tabs(&[0])); + let layout = Layout { root: Some(split(SplitAxis::Horizontal, SplitSize::Ratio(0.7), inner, tabs(&[1]))), floating: Vec::new() }; + let drawn = [(0, Permissions::PROTECTED), (1, Permissions::PROTECTED)]; + let driver = || DockDriver::new(layout.clone(), Bodies { rules: drawn.into(), ..Bodies::default() }); + let d = driver(); + let (canvas, _) = group(&d, 0); + let at = pos2(canvas.center().x, 5.0); + let mut d = driver(); + d.hold(tab(&d, 3), at); + assert!(d.filled(Rect::from_x_y_ranges(0.0..=800.0, 0.0..=4.0)), "a line along the window's top"); + let out = d.release(at); + let mut applied = layout.clone(); + let apply = |p: &u32| if *p == 0 { Permissions::PROTECTED } else { Permissions::default() }; + applied.apply_all(out.actions.clone(), apply).unwrap_or_else(|e| panic!("{e}: {:?}", out.actions)); + let mut rest = layout; + rest.apply(Action::Close { panel: 3 }).unwrap(); + assert_eq!(wrapped(&applied, 3), rest.root, "{:?}", out.actions); +} + +#[test] +fn drop_edges_limit_the_outer_edges_and_the_canvas_sides() { + let left_right = |style: &mut DockStyle| style.drop_edges = Sides::LEFT_RIGHT; + let d = canvas_driver(Permissions::PROTECTED, left_right); + // The canvas has no tab strip: its body is its frame. + let (canvas, _) = group(&d, 0); + let (side, _) = group(&d, 1); + // Without top and bottom, no outer edge takes a drop: left and right are docks here. + for at in [pos2(canvas.center().x, 590.0), pos2(canvas.left() + 5.0, 300.0), pos2(canvas.right() - 5.0, 300.0)] { + let mut d = canvas_driver(Permissions::PROTECTED, left_right); + d.hold(tab(&d, 3), at); + assert!(!d.filled(line(canvas, Zone::Bottom)), "{at:?}"); + let out = d.release(at); + assert!(!moves(&out), "{at:?}: {:?}", out.actions); + } + // A side frame keeps all four sides. + for (zone, y) in [(Zone::Bottom, 590.0), (Zone::Top, side.top() + side.height() * 0.2)] { + let mut d = canvas_driver(Permissions::PROTECTED, left_right); + let out = d.drag(tab(&d, 3), pos2(side.center().x, y)); + assert_eq!(out.actions, vec![Action::Move { panel: 3, anchor: 1, placement: Placement::Split(zone) }], "{zone:?}"); + } + // No drop edges: no outer edge takes a drop. + for at in [pos2(canvas.center().x, 590.0), pos2(canvas.center().x, 5.0), pos2(canvas.left() + 5.0, 300.0), pos2(canvas.right() - 5.0, 300.0)] { + let mut d = canvas_driver(Permissions::PROTECTED, |style| style.drop_edges = Sides::NONE); + let out = d.drag(tab(&d, 3), at); + assert!(!moves(&out), "{at:?}: {:?}", out.actions); + } +} + +#[test] +fn a_canvas_that_takes_splits_takes_them_only_on_its_drop_edges() { + let beside = Permissions { accepts_splits: true, ..Permissions::PROTECTED }; + let d = canvas_driver(beside, |_| {}); + let (body, _) = group(&d, 0); + // Nearer the bottom than the left, and right of center nearer the right than the top. + let low = pos2(body.left() + body.width() * 0.3, body.bottom() - 40.0); + let high = pos2(body.right() - body.width() * 0.3, body.top() + 40.0); + for (edges, zones) in [ + (DockStyle::default().drop_edges, [Some(Zone::Bottom), Some(Zone::Top)]), + (Sides::LEFT_RIGHT, [Some(Zone::Left), Some(Zone::Right)]), + (Sides::NONE, [None, None]), + ] { + for (at, zone) in [low, high].into_iter().zip(zones) { + let mut d = canvas_driver(beside, move |style| style.drop_edges = edges); + let out = d.drag(tab(&d, 3), at); + match zone { + Some(zone) => assert_eq!(out.actions, vec![Action::Move { panel: 3, anchor: 0, placement: Placement::Split(zone) }], "{edges:?}"), + None => assert!(!moves(&out), "{edges:?}: {:?}", out.actions), + } + } + } +} + +/// Whether the last frame painted a drop preview: an accent line, outline or fill. +fn previews(d: &Driver) -> bool { + let accent = accent(); + d.shapes().iter().any(|clipped| match &clipped.shape { + egui::Shape::Rect(shape) => shape.fill == accent || shape.fill == accent.gamma_multiply(0.08) || shape.stroke.color == accent, + egui::Shape::Path(path) => path.fill != Color32::TRANSPARENT && path.fill == accent.gamma_multiply(0.3), + _ => false, + }) +} + +fn ratio_split(axis: SplitAxis, ratio: f32, first: Node, second: Node) -> Node { + Node::Split { axis, size: SplitSize::Ratio(ratio), first: Box::new(first), second: Box::new(second) } +} + +#[test] +fn a_drop_that_changes_only_sizes_shows_nothing_and_does_nothing() { + // CADCraft's sidebar: Layers (2) alone above Properties (3) and a third group (4), beside + // the canvas (1); and the same with the column as one two-group split. + let nested = ratio_split(SplitAxis::Vertical, 0.3, tabs(&[2]), ratio_split(SplitAxis::Vertical, 0.5, tabs(&[3]), tabs(&[4]))); + let flat = ratio_split(SplitAxis::Vertical, 0.3, tabs(&[2]), tabs(&[3])); + for column in [nested, flat] { + let layout = Layout { root: Some(ratio_split(SplitAxis::Horizontal, 0.6, tabs(&[1]), column)), floating: Vec::new() }; + let start = || DockDriver::new(layout.clone(), Bodies::default()); + let d = start(); + let (body, rect) = group(&d, 3); + let top = pos2(body.center().x, body.top() + body.height() * 0.2); + // Properties' bottom third moves Layers, with a line. + let mut d = start(); + d.hold(tab(&d, 2), pos2(body.center().x, body.bottom() - body.height() * 0.2)); + assert!(previews(&d) && d.filled(line(rect, Zone::Bottom))); + // Its top third would put Layers back where it is. + let mut d = start(); + d.hold(tab(&d, 2), top); + assert!(!previews(&d), "no line, outline or fill"); + let out = d.release(top); + assert!(out.actions.is_empty(), "{:?}", out.actions); + assert_eq!(d.layout, layout); + } + // A lone frame's tab on the layout edge its frame already holds. + let layout = Layout { root: Some(ratio_split(SplitAxis::Vertical, 0.75, tabs(&[1]), tabs(&[4]))), floating: Vec::new() }; + let mut d = DockDriver::new(layout.clone(), Bodies::default()); + let at = pos2(400.0, 590.0); + d.hold(tab(&d, 4), at); + assert!(!previews(&d)); + let out = d.release(at); + assert!(out.actions.is_empty(), "{:?}", out.actions); + assert_eq!(d.layout, layout); +} + +#[test] +fn a_dock_drop_that_changes_only_heights_shows_nothing_and_does_nothing() { + // A dock column of a sized lone group (2) above another (3), beside the canvas (1). + let column = Column::new(vec![Group { height: Some(150.0), ..Group::new(vec![2], 0) }, Group::new(vec![3], 0)], 240.0); + let dock = Node::Dock { side: Side::End, columns: vec![column] }; + let root = Node::Split { axis: SplitAxis::Horizontal, size: SplitSize::FixedSecond(240.0), first: Box::new(tabs(&[1])), second: Box::new(dock) }; + let layout = Layout { root: Some(root), floating: Vec::new() }; + let start = || DockDriver::new(layout.clone(), Bodies::default()); + let d = start(); + let (body, rect) = group(&d, 3); + let (upper, _) = group(&d, 2); + // Below the lower group moves the upper one, with a line. + let mut d = start(); + d.hold(tab(&d, 2), pos2(body.center().x, body.bottom() - body.height() * 0.2)); + assert!(previews(&d)); + // The lower group's top third and the seam between them put the group back where it is. + for at in [pos2(body.center().x, body.top() + body.height() * 0.2), pos2(body.center().x, (upper.bottom() + rect.top()) * 0.5)] { + let mut d = start(); + d.hold(tab(&d, 2), at); + assert!(!previews(&d), "{at:?}: no line"); + let out = d.release(at); + assert!(out.actions.is_empty(), "{at:?}: {:?}", out.actions); + assert_eq!(d.layout, layout); + } +} + +#[test] +fn a_header_drag_of_a_windows_only_group_looks_through_its_window() { + // Group 4 floats alone over frame 1's left part. + let mut layout = frames(); + layout.floating.push(Floating::single(vec![4], 0, [40.0, 150.0, 300.0, 260.0])); + let start = || DockDriver::new(layout.clone(), Bodies::default()); + let d = start(); + let (body, rect) = group(&d, 1); + let window = Rect::from_min_size(pos2(40.0, 150.0), vec2(300.0, 260.0)); + let at = pos2(body.left() + body.width() * 0.25, body.center().y); + assert!(window.contains(at)); + let mut d = start(); + let bar = d.hit(&Hit::DragBar(4)).unwrap().center(); + d.hold(bar, at); + assert!(d.filled(line(rect, Zone::Left)), "frame 1's left third, behind the window"); + let out = d.release(at); + assert_eq!(out.actions, vec![Action::MoveGroup { panel: 4, to: Target::At { anchor: 1, placement: Placement::Split(Zone::Left) } }]); +} + +#[test] +fn a_header_drag_in_a_window_of_two_groups_still_offers_the_other_group() { + let mut layout = frames(); + let tree = ratio_split(SplitAxis::Vertical, 0.5, tabs(&[4]), tabs(&[5])); + layout.floating.push(Floating::new(tree, [40.0, 100.0, 300.0, 400.0])); + let start = || DockDriver::new(layout.clone(), Bodies::default()); + let d = start(); + let (body, rect) = group(&d, 5); + let mut d = start(); + let bar = d.hit(&Hit::DragBar(4)).unwrap().center(); + d.hold(bar, body.center()); + assert!(d.outlined(rect.shrink(1.0)), "group 5 joins"); + let out = d.release(body.center()); + assert!( + matches!( + &out.actions[..], + [Action::MoveGroup { panel: 4, to: Target::At { anchor: 5, placement: Placement::Tab { .. } | Placement::Split(Zone::Center) } }] + ), + "{:?}", + out.actions + ); +} + +#[test] +fn a_drop_beside_a_frame_too_narrow_to_halve_takes_room_from_its_neighbour() { + // Frame 2 is 60 pt wide beside frame 1; group 4 floats over frame 1. + let root = + Node::Split { axis: SplitAxis::Horizontal, size: SplitSize::FixedSecond(60.0), first: Box::new(tabs(&[1])), second: Box::new(tabs(&[2])) }; + let layout = Layout { root: Some(root), floating: vec![Floating::single(vec![4], 0, [100.0, 100.0, 200.0, 200.0])] }; + let mut d = DockDriver::new(layout, Bodies::default()); + let (body, rect) = group(&d, 2); + let at = pos2(body.left() + 8.0, body.center().y); + d.hold(tab(&d, 4), at); + assert!(d.filled(line(rect, Zone::Left)), "a line along frame 2's left edge"); + let out = d.release(at); + assert!( + matches!(&out.actions[..], [Action::Move { panel: 4, anchor: 2, placement: Placement::SplitSized { zone: Zone::Left, .. } }]), + "{:?}", + out.actions + ); + d.settle(); + assert!((group(&d, 2).1.width() - rect.width()).abs() < 1.0, "frame 2 keeps its width"); + assert!(group(&d, 4).1.width() >= 40.0); +} diff --git a/crates/ui/src/docking/group_tests.rs b/crates/ui/src/docking/group_tests.rs index 46be48c4a..665e8d734 100644 --- a/crates/ui/src/docking/group_tests.rs +++ b/crates/ui/src/docking/group_tests.rs @@ -526,7 +526,8 @@ fn release_over_own_position_is_a_noop() { #[test] fn floating_border_and_drag_bar_move_and_cancel_as_a_group() { for name in ["drag-border", "drag-bar"] { - let layout = Layout { root: Some(tabs(&[1], 0)), floating: vec![Floating::single(vec![5, 6], 1, [80.0, 70.0, 300.0, 220.0])] }; + // Nothing docked: a header drag of a window's only group looks through the window. + let layout = Layout { root: None, floating: vec![Floating::single(vec![5, 6], 1, [80.0, 70.0, 300.0, 220.0])] }; let mut d = Driver::new(layout.clone()); let from = d.rect(name, 5).center(); d.hold(from, from + vec2(50.0, 40.0)); @@ -547,7 +548,7 @@ fn floating_border_and_drag_bar_move_and_cancel_as_a_group() { /// Moving a floating window by eight fractional pointer steps lands within one /// point of the accumulated 80.3 × 60.7 pt offset, without step-rounding drift. fn floating_border_movement_does_not_accumulate_fractional_step_rounding() { - let layout = Layout { root: Some(tabs(&[1], 0)), floating: vec![Floating::single(vec![5, 6], 1, [80.0, 70.0, 300.0, 220.0])] }; + let layout = Layout { root: None, floating: vec![Floating::single(vec![5, 6], 1, [80.0, 70.0, 300.0, 220.0])] }; let mut d = Driver::new(layout); let from = d.rect("drag-border", 5).center(); d.step(vec![Event::PointerMoved(from), button(from, true)]); diff --git a/crates/ui/src/docking/mod.rs b/crates/ui/src/docking/mod.rs index f960c3e63..9b0273d9d 100644 --- a/crates/ui/src/docking/mod.rs +++ b/crates/ui/src/docking/mod.rs @@ -35,8 +35,8 @@ pub use surface::{ WindowFiles, WindowKind, begin_drag, cancel_drag, float_space, is_dragging, route_app_drag, }; pub use targets::{ - DropProposal, DropTarget, SIDE_EDGE_ZONE_DEPTH, ZoneRule, body_zone, drop_zone, resolve_drop, resolve_drop_by, tab_insertion_marker, zone_rect, - zone_shape, + DropProposal, DropTarget, SIDE_EDGE_ZONE_DEPTH, Sides, ZoneRule, body_zone, drop_zone, resolve_drop, resolve_drop_by, tab_insertion_marker, + zone_rect, zone_shape, }; pub use workspace::Workspace; diff --git a/crates/ui/src/docking/model.rs b/crates/ui/src/docking/model.rs index be2ae3a37..11d0f3e55 100644 --- a/crates/ui/src/docking/model.rs +++ b/crates/ui/src/docking/model.rs @@ -1,7 +1,8 @@ use std::{collections::HashSet, fmt, hash::Hash}; use super::dock_model::{ - Column, ColumnLocation, Group, GroupLocation, Moving, PanelState, Place, Side, Target, Unit, node_at, split_frame, tree_panels, valid_column, + Column, ColumnLocation, Group, GroupLocation, Moving, PanelState, Place, Side, Target, Unit, node_at, split_frame, split_frame_sized, + tree_panels, valid_column, }; use crate::layout::{SplitAxis, SplitSize}; @@ -61,15 +62,16 @@ pub struct StackEntry

{ /// Serde reads the v1 shape `{panels, active, rect}` as one group, and a stack of groups /// `{groups, rect}` as a vertical split of them, sized by their heights. A window with one /// group (not minimized, no native rect) writes the v1 shape, so older builds can read files -/// that use no newer feature. A native window also writes its `id`; any other window gets a -/// fresh one on load. +/// that use no newer feature. Every window writes its `id`; a window read without one gets a +/// fresh one. /// /// Equality ignores `id`: two windows with the same contents and place are the same layout. #[derive(Clone, Debug)] pub struct Floating

{ /// Names the window across panel moves, and its OS window with feature `native-windows`. - /// Unique within a layout: [`Self::new`] and loading draw fresh ids, [`Self::with_id`] - /// sets one, and merging windows keeps the receiving window's. + /// Unique within a layout: [`Self::new`] draws a fresh id, [`Self::with_id`] sets one, + /// loading keeps the saved id (or draws one when it is missing or taken), and merging + /// windows keeps the receiving window's. pub id: u64, /// `Split`, `Tabs` and `Stack` nodes; a `Dock` is invalid here. pub root: Node

, @@ -181,6 +183,7 @@ mod floating_serde { #[derive(Serialize)] struct V1<'a, P> { + id: u64, panels: &'a [P], active: usize, rect: [f32; 4], @@ -188,8 +191,7 @@ mod floating_serde { #[derive(Serialize)] struct Tree<'a, P> { - #[serde(skip_serializing_if = "Option::is_none")] - id: Option, + id: u64, root: &'a Node

, rect: [f32; 4], #[serde(skip_serializing_if = "Option::is_none")] @@ -206,10 +208,10 @@ mod floating_serde { fn serialize(&self, serializer: S) -> Result { match &self.root { Node::Tabs { panels, active } if !self.minimized && self.native.is_none() => { - V1 { panels, active: *active, rect: self.rect }.serialize(serializer) + V1 { id: self.id, panels, active: *active, rect: self.rect }.serialize(serializer) } root => Tree { - id: self.native.map(|_| self.id), + id: self.id, root, rect: self.rect, native: self.native, @@ -305,6 +307,13 @@ pub enum Placement

{ /// Center inserts after the anchor; sides split the anchor's frame into two halves, and /// every other frame keeps its size. Sides apply to `Tabs` and `Stack` nodes only. Split(Zone), + /// A side [`Placement::Split`] that gives the new frame its own room instead of half the + /// anchor's frame: the new split takes `size`, and `outer` replaces the size of the nearest + /// enclosing split whose divider runs along the anchor frame's `zone` side, so the frames + /// beyond that divider give the room. The renderer offers it beside a frame too small to + /// halve. `Center`, and an anchor without such a divider or with a dock beyond it, are + /// invalid. + SplitSized { zone: Zone, size: SplitSize, outer: SplitSize }, /// A new group above or below the anchor's group in a dock column. On an anchor in a /// floating window, a split of the anchor's group above or below. /// `height` is the inserted group's preferred height ([`super::Group::height`]); `None` @@ -318,7 +327,11 @@ pub enum Placement

{ Column { after: bool }, /// A new frame along one edge of the docked layout, a quarter of its width or height: the /// root is split, or in a layout with docks, the part between them. The anchor names any - /// docked panel. `Center` is invalid. + /// docked panel. A protected anchor ([`Permissions::is_protected`], applied through + /// [`Layout::apply_with_permissions`]) narrows the part to the canvas column: where neither + /// side of a side-by-side split is a dock and only one holds a protected panel, that side; + /// for `Top` (`Bottom`), also the first (last) row of a split of rows when only that row + /// holds one. `Center` is invalid. Edge(Zone), } @@ -819,14 +832,14 @@ impl Layout

{ let window_open = location.anchor.as_ref().is_some_and(|anchor| proposed.floating.iter().any(|w| w.contains(anchor))); if let Some(rect) = location.floating.filter(|_| !window_open) { // A window that closed as a whole reopens at its last rect. - proposed.insert_moving(moving(), Target::Float { rect: Some(rect) })?; + proposed.insert_moving(moving(), Target::Float { rect: Some(rect) }, &|_| false)?; } else if let Some(anchor) = &location.anchor && matches!(location.placement, Placement::Group { .. } | Placement::Column { .. }) { if !proposed.contains(anchor) { return Err(DockError::MissingTarget); } - proposed.insert_moving(moving(), Target::At { anchor: anchor.clone(), placement: location.placement.clone() })?; + proposed.insert_moving(moving(), Target::At { anchor: anchor.clone(), placement: location.placement.clone() }, &|_| false)?; } else if let Some(column) = state.and_then(|s| s.column).filter(|_| location.anchor.is_none()) { // Alone in its dock: back at that edge, as the outermost column if a dock exists. match proposed.dock_path(column.side) { @@ -835,7 +848,7 @@ impl Layout

{ let Moving { groups, .. } = moving(); columns.insert(0, Column { groups, width: column.width, iconic: column.iconic, iconic_width: column.iconic_width }); } - None => proposed.insert_moving(moving(), Target::NewDock { side: column.side })?, + None => proposed.insert_moving(moving(), Target::NewDock { side: column.side }, &|_| false)?, } } else if let Some(anchor) = &location.anchor { if !proposed.contains(anchor) { @@ -862,7 +875,7 @@ impl Layout

{ Placement::Tab { before: Some(before) } if !proposed.same_group(before, anchor) => Placement::Tab { before: None }, placement => placement.clone(), }; - proposed.apply_inner(Action::OpenAt { panel: panel.clone(), anchor: anchor.clone(), placement })?; + proposed.apply_inner(Action::OpenAt { panel: panel.clone(), anchor: anchor.clone(), placement }, &|_| false)?; if let Some(size) = location.size { proposed.trees_mut().any(|root| restore_parent_size(root, &panel, size)); } @@ -899,7 +912,7 @@ impl Layout

{ .find(|p| matches!(self.place(p), Some(Place::Dock { .. } | Place::Node(_)))) .cloned() .ok_or(DockError::MissingTarget)?; - proposed.apply_inner(Action::OpenAt { panel: panel.clone(), anchor, placement: Placement::Edge(Zone::Right) })?; + proposed.apply_inner(Action::OpenAt { panel: panel.clone(), anchor, placement: Placement::Edge(Zone::Right) }, &|_| false)?; if let Some(root) = proposed.root.as_mut() { restore_parent_size(root, &panel, SplitSize::Ratio(0.25)); } @@ -987,7 +1000,7 @@ impl Layout

{ } let closed = self.closed_by(&action)?; let mut proposed = self.clone(); - proposed.apply_inner(action)?; + proposed.apply_inner(action, &|p| permissions(p).is_protected())?; proposed.validate()?; *self = proposed; Ok(closed) @@ -1017,7 +1030,8 @@ impl Layout

{ }) } - fn apply_inner(&mut self, action: Action

) -> Result<(), DockError> { + /// `canvas` is the protected-panel test an outer edge drop narrows by. + fn apply_inner(&mut self, action: Action

, canvas: &dyn Fn(&P) -> bool) -> Result<(), DockError> { match action { Action::Move { panel, anchor, placement } => { if panel == anchor { @@ -1036,7 +1050,7 @@ impl Layout

{ } } let entry = self.take(&panel).ok_or(DockError::MissingPanel)?; - self.insert(entry, &anchor, placement) + self.insert(entry, &anchor, placement, canvas) } Action::Float { panel, rect } => { if !valid_rect(rect) { @@ -1070,11 +1084,11 @@ impl Layout

{ } Action::Open { panel, anchor } => { if self.contains(&panel) { - return self.apply_inner(Action::Activate { panel }); + return self.apply_inner(Action::Activate { panel }, canvas); } let entry = StackEntry { panel, open: true, height: None }; if let Some(anchor) = anchor { - self.insert(entry, &anchor, Placement::Tab { before: None }) + self.insert(entry, &anchor, Placement::Tab { before: None }, canvas) } else if self.root.is_none() { self.root = Some(Node::Tabs { panels: vec![entry.panel], active: 0 }); Ok(()) @@ -1084,14 +1098,14 @@ impl Layout

{ } Action::OpenAt { panel, anchor, placement } => { if self.contains(&panel) { - return self.apply_inner(Action::Move { panel, anchor, placement }); + return self.apply_inner(Action::Move { panel, anchor, placement }, canvas); } if let Placement::Tab { before: Some(before) } = &placement && !self.same_group(before, &anchor) { return Err(DockError::MissingTarget); } - self.insert(StackEntry { panel, open: true, height: None }, &anchor, placement) + self.insert(StackEntry { panel, open: true, height: None }, &anchor, placement, canvas) } Action::MoveFloating { panel, rect } => { if !valid_rect(rect) { @@ -1142,13 +1156,13 @@ impl Layout

{ return Ok(()); } let moving = self.take_unit(panel, unit)?; - self.insert_moving(moving, to.clone()) + self.insert_moving(moving, to.clone(), canvas) } Action::MovePanel { panel, to } => match to { - Target::At { anchor, placement } => self.apply_inner(Action::Move { panel, anchor, placement }), + Target::At { anchor, placement } => self.apply_inner(Action::Move { panel, anchor, placement }, canvas), to => { let entry = self.take(&panel).ok_or(DockError::MissingPanel)?; - self.insert_moving(Moving::group(Group::new(vec![entry.panel], 0)), to) + self.insert_moving(Moving::group(Group::new(vec![entry.panel], 0)), to, canvas) } }, Action::CloseGroup { ref panel } | Action::CloseWindow { ref panel } => self.take_unit(panel, unit_of(&action)).map(|_| ()), @@ -1285,16 +1299,18 @@ impl Layout

{ if let Some((panel, _)) = groups.clone().find(|(_, place)| side(place) == Some(Side::End)).or_else(|| groups.clone().next()) { return Some(((*panel).clone(), Placement::Tab { before: None })); } - docked.first().map(|(panel, _)| ((*panel).clone(), Placement::Edge(Zone::Right))) + // A canvas anchor would narrow the edge to the canvas column. + let edge = docked.iter().find(|(p, _)| !permissions(p).is_protected()).or(docked.first()); + edge.map(|(panel, _)| ((*panel).clone(), Placement::Edge(Zone::Right))) } - fn insert(&mut self, entry: StackEntry

, anchor: &P, placement: Placement

) -> Result<(), DockError> { + fn insert(&mut self, entry: StackEntry

, anchor: &P, placement: Placement

, canvas: &dyn Fn(&P) -> bool) -> Result<(), DockError> { let place = self.place(anchor).ok_or(DockError::MissingTarget)?; let tree = place.tree_path().map(|(window, path)| (window, path.to_vec())); let Some((window, path)) = tree.filter(|_| !matches!(placement, Placement::Group { .. } | Placement::Column { .. } | Placement::Edge(_))) else { let group = Group::new(vec![entry.panel], 0); - return self.insert_moving(Moving::group(group), Target::At { anchor: anchor.clone(), placement }); + return self.insert_moving(Moving::group(group), Target::At { anchor: anchor.clone(), placement }, canvas); }; let root = self.tree_mut(window).ok_or(DockError::MissingTarget)?; if let Placement::Split(zone) = placement @@ -1303,6 +1319,10 @@ impl Layout

{ let new = Node::Tabs { panels: vec![entry.panel], active: 0 }; return if split_frame(root, &path, new, zone) { Ok(()) } else { Err(DockError::MissingTarget) }; } + if let Placement::SplitSized { zone, size, outer } = placement { + let new = Node::Tabs { panels: vec![entry.panel], active: 0 }; + return if split_frame_sized(root, &path, new, zone, size, outer) { Ok(()) } else { Err(DockError::InvalidPlacement) }; + } if insert_node(root, &entry, anchor, &placement) { Ok(()) } else { Err(DockError::MissingTarget) } } } @@ -1312,7 +1332,7 @@ impl Layout

{ fn placement_allowed

(placement: &Placement

, anchor: Permissions) -> bool { match placement { Placement::Tab { .. } | Placement::Split(Zone::Center) => anchor.accepts_tabs, - Placement::Split(_) => anchor.accepts_splits, + Placement::Split(_) | Placement::SplitSized { .. } => anchor.accepts_splits, _ => true, } } diff --git a/crates/ui/src/docking/model_tests.rs b/crates/ui/src/docking/model_tests.rs index 9327fd4d3..6bf2553da 100644 --- a/crates/ui/src/docking/model_tests.rs +++ b/crates/ui/src/docking/model_tests.rs @@ -576,18 +576,32 @@ mod serde_tests { fn single_plain_floating_window_writes_the_v1_shape() { let rect = [1.0, 2.0, 300.0, 200.0]; let plain = Floating::single(vec![4u32, 5], 1, rect); - assert_eq!(serde_json::to_value(&plain).unwrap(), json!({"panels": [4, 5], "active": 1, "rect": rect})); + assert_eq!(serde_json::to_value(&plain).unwrap(), json!({"id": plain.id, "panels": [4, 5], "active": 1, "rect": rect})); let minimized = Floating { minimized: true, ..Floating::single(vec![4u32], 0, rect) }; let stacked = Floating::stack(vec![g(&[4], 0), g(&[5], 0)], rect).unwrap(); let native = Floating { native: Some(rect), ..Floating::single(vec![4u32], 0, rect) }; for window in [minimized, stacked, native] { let value = serde_json::to_value(&window).unwrap(); assert!(value.get("root").is_some() && value.get("panels").is_none(), "{value}"); + assert_eq!(value["id"], json!(window.id), "{value}"); assert_eq!(serde_json::from_value::>(value).unwrap(), window); } assert!(serde_json::from_value::>(json!({"rect": rect})).is_err()); } + #[test] + fn in_window_windows_keep_their_ids_through_a_save() { + let rect = [1.0, 2.0, 300.0, 200.0]; + let layout = Layout { + root: None, + floating: vec![Floating::single(vec![4u32], 0, rect).with_id(7), Floating::stack(vec![g(&[5], 0), g(&[6], 0)], rect).unwrap().with_id(8)], + }; + let saved = serde_json::to_string(&layout).unwrap(); + let loaded: Layout = serde_json::from_str(&saved).unwrap(); + assert_eq!(loaded, layout); + assert_eq!(loaded.floating.iter().map(|w| w.id).collect::>(), [7, 8]); + } + #[test] fn v2_round_trips() { let mut workspace = Workspace::new(docked()); @@ -668,8 +682,13 @@ mod serde_tests { } let layout: super::Layout = serde_json::from_value(layout_value.clone()).unwrap_or_else(|e| panic!("{app}{pointer}: {e}")); layout.validate().unwrap_or_else(|e| panic!("{app}{pointer}: {e}")); - // Nothing new is in use, so the layout writes back exactly as read. - assert_eq!(serde_json::to_value(&layout).unwrap(), layout_value, "{app}{pointer}"); + // Nothing new is in use, so the layout writes back exactly as read, plus the + // fresh id of each floating window. + let mut written = serde_json::to_value(&layout).unwrap(); + for (window, value) in layout.floating.iter().zip(written["floating"].as_array_mut().unwrap()) { + assert_eq!(value.as_object_mut().unwrap().remove("id"), Some(json!(window.id)), "{app}{pointer}"); + } + assert_eq!(written, layout_value, "{app}{pointer}"); } } } @@ -754,3 +773,23 @@ fn edge_placement_splits_the_part_between_the_docks_or_the_root() { assert_eq!(frames.apply(Action::Move { panel: 2, anchor: 1, placement: Placement::Edge(Zone::Center) }), Err(DockError::InvalidPlacement)); assert_eq!(frames, before); } + +#[test] +fn a_sized_split_sets_the_new_split_and_the_enclosing_one_beside_the_anchor() { + // 1 | (2 | 3): 4 goes left of 3, and the split between 2 and 3 gives the room. + let layout = Layout { + root: Some(hsplit(SplitSize::Ratio(0.5), tabs(&[1], 0), hsplit(SplitSize::FixedSecond(60.0), tabs(&[2], 0), tabs(&[3], 0)))), + floating: vec![Floating::single(vec![4], 0, [0.0, 0.0, 100.0, 100.0])], + }; + let sized = |zone| Placement::SplitSized { zone, size: SplitSize::FixedFirst(100.0), outer: SplitSize::FixedSecond(164.0) }; + let mut moved = layout.clone(); + moved.apply(Action::Move { panel: 4, anchor: 3, placement: sized(Zone::Left) }).unwrap(); + let inner = hsplit(SplitSize::FixedSecond(164.0), tabs(&[2], 0), hsplit(SplitSize::FixedFirst(100.0), tabs(&[4], 0), tabs(&[3], 0))); + assert_eq!(moved.root, Some(hsplit(SplitSize::Ratio(0.5), tabs(&[1], 0), inner))); + // 1 has no divider on its left, and a center is no side. + for (anchor, zone) in [(1, Zone::Left), (3, Zone::Center)] { + let mut moved = layout.clone(); + assert_eq!(moved.apply(Action::Move { panel: 4, anchor, placement: sized(zone) }), Err(DockError::InvalidPlacement), "{anchor} {zone:?}"); + assert_eq!(moved, layout); + } +} diff --git a/crates/ui/src/docking/native_tests.rs b/crates/ui/src/docking/native_tests.rs index 4b69e1f40..d141bd27f 100644 --- a/crates/ui/src/docking/native_tests.rs +++ b/crates/ui/src/docking/native_tests.rs @@ -3,7 +3,9 @@ use egui::{Color32, Event, Key, Modifiers, PointerButton, Pos2, Rect, ViewportCommand, epaint::ClippedShape, pos2, vec2}; -use super::render::{APPKIT_CORNER, DragPointer, MACOS_CLOSE, WindowDrag, appkit_resize_areas, close_rects, floats_shown, resize_bands, wayland}; +use super::render::{ + APPKIT_CORNER, DragPointer, MACOS_CLOSE, WindowDrag, appkit_resize_areas, close_rects, drag_opacity, floats_shown, resize_bands, wayland, +}; use super::testing::DockDriver; use super::{Action, DockContent, Floating, Hit, Layout, Monitor, Node, Permissions, Placement, Target, WindowKind, Workspace}; use crate::layout::{SplitAxis, SplitSize}; @@ -606,8 +608,9 @@ fn a_native_window_saves_its_id_place_and_monitor() { let copies: Layout = serde_json::from_str(&serde_json::to_string(&copies).unwrap()).unwrap(); assert_eq!(copies.floating[0].id, layout.floating[0].id); assert_ne!(copies.floating[1].id, copies.floating[0].id); - // An in-window window writes no id, as older builds did. - assert!(!serde_json::to_string(&Floating::single(vec![1u32], 0, [0.0, 0.0, 9.0, 9.0])).unwrap().contains("id")); + // An in-window window writes its id too. + let in_window = Floating::single(vec![1u32], 0, [0.0, 0.0, 9.0, 9.0]).with_id(3); + assert!(serde_json::to_string(&in_window).unwrap().contains(r#""id":3"#)); } /// Height of the bar at the top of a native window. @@ -2403,3 +2406,17 @@ fn repeated_discards_deliver_the_release_and_drop_once() { assert!(!egui::DragAndDrop::has_any_payload(&d.ctx)); assert!(super::render::held_events(&d.ctx, egui::ViewportId::ROOT).is_empty()); } + +#[test] +fn a_native_window_fades_only_while_its_drag_can_dock() { + let drag = |docks| WindowDrag { window: 1, grab: vec2(10.0, 10.0), to: pos2(100.0, 100.0), start: pos2(100.0, 100.0), docks }; + assert_eq!(drag_opacity(None, false, true), 1.0); + // A bar drag that only moves the window stays opaque. + assert_eq!(drag_opacity(Some(&drag(false)), false, false), 1.0); + assert_eq!(drag_opacity(Some(&drag(false)), false, true), 0.75); + // A drag by the window's only tab or group docks either way. + assert_eq!(drag_opacity(Some(&drag(true)), false, false), 0.75); + // With Cmd/Ctrl held the drag only moves the window. + assert_eq!(drag_opacity(Some(&drag(false)), true, true), 1.0); + assert_eq!(drag_opacity(Some(&drag(true)), true, false), 1.0); +} diff --git a/crates/ui/src/docking/render/dock.rs b/crates/ui/src/docking/render/dock.rs index 90914ee77..a04c46b46 100644 --- a/crates/ui/src/docking/render/dock.rs +++ b/crates/ui/src/docking/render/dock.rs @@ -10,7 +10,7 @@ use egui::{Context, Key, Pos2, Rect, Sense, Stroke, StrokeKind, Ui, UiBuilder, p use super::{GroupKind, Renderer, RowItem, row_drag, row_range, stripless}; use crate::docking::{ - Action, Column, DockEvent, DockHeights, DockStyle, Group, GroupDrag, GroupGesture, Node, Placement, Side, Target, Zone, + Action, Column, DockEvent, DockHeights, DockStyle, DropTarget, Group, GroupDrag, GroupGesture, Node, Placement, Side, Target, Zone, dock_model::Unit, targets::{ DockDrop, DockTarget, SIDE_EDGE_ZONE_DEPTH, STRIP_REACH, body_zone, column_edge, drop_zone, horizontal_line, resolve_dock, usable, @@ -636,18 +636,25 @@ impl Renderer<'_, P> { /// takes tabs: the strip joins the group, and border bands reach into it only /// [`STRIP_REACH`] from their lines. Not with [`DockStyle::side_edge_zones`]. pub(super) fn in_tab_strip(&self, pointer: Pos2) -> bool { + self.in_tab_strip_past(pointer, &[]) + } + + /// [`Self::in_tab_strip`] past the groups holding any of `through`. + fn in_tab_strip_past(&self, pointer: Pos2, through: &[P]) -> bool { + let own = |t: &DropTarget

| t.tabs.iter().any(|(p, _)| through.contains(p)); !self.style.side_edge_zones - && self.targets.iter().rev().find(|t| usable(t.rect) && t.rect.contains(pointer)).is_some_and(|t| { + && self.targets.iter().rev().find(|t| usable(t.rect) && t.rect.contains(pointer) && !own(t)).is_some_and(|t| { t.accepts_tabs && t.tab_strip.map(|strip| strip.intersect(t.rect)).is_some_and(|strip| usable(strip) && strip.contains(pointer)) }) } /// The dock target at `pointer`: a band at a border, else a side zone of a dock group's /// body ([`SIDE_EDGE_ZONE_DEPTH`] deep with [`DockStyle::side_edge_zones`]). The middle of a - /// group that takes tabs is none: the group's own target joins it. - fn dock_drop(&self, pointer: Pos2) -> Option> { + /// group that takes tabs is none: the group's own target joins it. The groups holding + /// `through` (a floating window the drag looks through) hide no strip. + fn dock_drop(&self, pointer: Pos2, through: &[P]) -> Option> { let mut candidates = self.dock_candidates(); - let strip = self.in_tab_strip(pointer); + let strip = self.in_tab_strip_past(pointer, through); if strip { candidates.retain(|t| t.line.distance_to_pos(pointer) <= STRIP_REACH); } @@ -670,11 +677,11 @@ impl Renderer<'_, P> { /// The dock target a whole group or column drag would take at `pointer`, with the action /// and its line. A target that leaves the layout unchanged is a no-op: `None` action. pub(super) fn dock_group_proposal(&self, drag: &GroupDrag

, pointer: Pos2) -> Option<(Option>, Rect)> { - let moving = (drag.gesture == GroupGesture::Window).then_some(&drag.panel); - if self.dock_covered(pointer, moving) { + let through = self.looks_through(drag); + if self.dock_covered(pointer, through.then_some(&drag.panel)) { return None; } - let drop = self.dock_drop(pointer)?; + let drop = self.dock_drop(pointer, if through { &drag.members } else { &[] })?; let action = group_action(drag, drop.target.to()); self.checked(action).map(|action| (action, drop.line)) } @@ -686,7 +693,7 @@ impl Renderer<'_, P> { if self.dock_covered(pointer, None) { return None; } - let drop = self.dock_drop(pointer)?; + let drop = self.dock_drop(pointer, &[])?; let action = match drop.target.to() { // A target named by the dragged panel is its own group or column: another panel // there names the same one. With none, the drop leaves the panel where it is. @@ -707,11 +714,12 @@ impl Renderer<'_, P> { self.checked(action).map(|action| (action, drop.line)) } - /// `Some(None)` when the action is valid but changes nothing, `None` when it is invalid. + /// `Some(None)` when the action is valid but changes nothing apart from sizes + /// ([`crate::docking::Layout::same_arrangement`]), `None` when it is invalid. fn checked(&self, action: Action

) -> Option>> { let mut candidate = self.layout.clone(); candidate.apply_with_permissions(action.clone(), |p| self.permissions(p)).ok()?; - Some((candidate != *self.layout).then_some(action)) + Some((!candidate.same_arrangement(self.layout)).then_some(action)) } /// The line a drop beside a group, column, dock or the layout's edge previews with. diff --git a/crates/ui/src/docking/render/frame.rs b/crates/ui/src/docking/render/frame.rs index 74ba98a0a..e1b7c4297 100644 --- a/crates/ui/src/docking/render/frame.rs +++ b/crates/ui/src/docking/render/frame.rs @@ -7,8 +7,8 @@ use egui::{Context, Pos2, Rect, pos2, vec2}; use super::{GroupKind, Renderer, floating::WindowHit, group::TabGroup}; use crate::docking::{ - Action, Column, DockContent, DockEvent, Floating, Group, HidePanels, Layout, Node, Zone, - dock_model::{Place, node_at}, + Action, Column, DockContent, DockEvent, Floating, Group, HidePanels, Layout, Node, Placement, Target, Zone, + dock_model::{Place, node_at, tree_panels}, }; /// How far below the pointer the float hint sits, clear of the cursor. @@ -20,22 +20,52 @@ pub(in crate::docking) struct FrameState

{ paths: Option, Vec>>, /// The same for each floating window's splits, by the window's first shown panel. window_paths: HashMap, Vec>>, - /// The view node whose rect is the outer edge area; none while a frame is maximized. - edge_path: Option>, - pub(super) edge: Option, + /// The outer edge areas of top, bottom, left and right drops; none while a frame is + /// maximized. + edges: Option<[EdgeArea

; 4]>, + /// The panels the view leaves out. + hidden: Vec

, pub(super) maximized: Option

, /// Floating windows as drawn, back to front. pub(super) windows: Vec>, } -/// The layout as shown: a maximized frame alone, and hidden panels left out. +/// The view node an outer edge drop wraps ([`Layout::edge_path`]), and its rect once drawn. +struct EdgeArea

{ + path: Vec, + /// The node is narrower than the part between the docks: the canvas column. + narrowed: bool, + panels: Vec

, + rect: Option, +} + +impl EdgeArea

{ + fn of(view: &Layout

, zone: Zone, canvas: &dyn Fn(&P) -> bool) -> Self { + let path = view.edge_path(zone, canvas); + let panels = + view.root.as_ref().and_then(|root| node_at(root, &path)).map_or(Vec::new(), |node| tree_panels(node).into_iter().cloned().collect()); + Self { narrowed: view.edge_narrows(zone, canvas), path, panels, rect: None } + } +} + +const EDGES: [Zone; 4] = [Zone::Top, Zone::Bottom, Zone::Left, Zone::Right]; + +fn edge_areas(view: &Layout

, canvas: &dyn Fn(&P) -> bool) -> [EdgeArea

; 4] { + EDGES.map(|zone| EdgeArea::of(view, zone, canvas)) +} + +/// The layout as shown: a maximized frame alone, and hidden panels left out. `narrow` narrows +/// the outer edge area to the canvas column ([`Layout::edge_path`]). pub(in crate::docking) fn view<'a, P: Clone + Eq + Hash>( layout: &'a Layout

, maximized: Option<&P>, hide: HidePanels<'_, P>, content: &dyn DockContent

, + narrow: bool, ) -> (Cow<'a, Layout

>, FrameState

) { let protected = |p: &P| content.permissions(p).is_protected(); + // The edge area narrows to the canvas column without side edge zones. + let canvas = |p: &P| narrow && protected(p); let hidden = |p: &P| match hide { HidePanels::None => false, HidePanels::All => !protected(p), @@ -50,11 +80,12 @@ pub(in crate::docking) fn view<'a, P: Clone + Eq + Hash>( } Place::Floating { .. } => None, }); - let mut state = FrameState { paths: None, window_paths: HashMap::new(), edge_path: None, edge: None, maximized: None, windows: Vec::new() }; + let mut state = FrameState { paths: None, window_paths: HashMap::new(), edges: None, hidden: Vec::new(), maximized: None, windows: Vec::new() }; if maximized.is_none() && matches!(hide, HidePanels::None) { - state.edge_path = Some(layout.edge_path()); + state.edges = Some(edge_areas(layout, &canvas)); return (Cow::Borrowed(layout), state); } + state.hidden = layout.panels().into_iter().filter(|p| hidden(p)).cloned().collect(); let mut paths = HashMap::new(); let view = match maximized { Some((panel, path, node)) => { @@ -77,7 +108,7 @@ pub(in crate::docking) fn view<'a, P: Clone + Eq + Hash>( } }; if state.maximized.is_none() { - state.edge_path = Some(view.edge_path()); + state.edges = Some(edge_areas(&view, &canvas)); } state.paths = Some(paths); (Cow::Owned(view), state) @@ -152,8 +183,11 @@ fn prune_group(group: &Group

, hidden: &dyn Fn(&P) -> bo impl Renderer<'_, P> { pub(super) fn note_edge(&mut self, path: &[bool], rect: Rect) { - if self.tree.is_none() && self.frame.edge_path.as_deref() == Some(path) { - self.frame.edge = Some(rect); + if self.tree.is_some() { + return; + } + for area in self.frame.edges.iter_mut().flatten().filter(|area| area.path == path) { + area.rect = Some(rect); } } @@ -166,32 +200,40 @@ impl Renderer<'_, P> { paths.and_then(|paths| paths.get(path).cloned()).unwrap_or_else(|| path.to_vec()) } - /// The outer edge band under the pointer (P8): top and bottom, and left and right with + /// The outer edge band under the pointer (P8): the top and bottom of + /// [`crate::docking::DockStyle::drop_edges`], all four with /// [`crate::docking::DockStyle::side_edge_zones`]. Returns a docked anchor that is not - /// moving, the zone and the preview: a line along the layout's whole edge, or with side edge - /// zones the quarter the new frame takes. Without them, on a tab strip that takes tabs, the band reaches - /// only [`crate::docking::targets::STRIP_REACH`] from the line. + /// moving, the zone and the preview: a line along the edge area's whole edge, or with side + /// edge zones the quarter the new frame takes. Without them, on a tab strip that takes tabs, + /// the band reaches only [`crate::docking::targets::STRIP_REACH`] from the line. pub(super) fn edge_proposal(&self, members: &[P], pointer: Pos2) -> Option<(P, Zone, Rect)> { - let area = self.frame.edge.filter(|area| area.contains(pointer))?; + let sides = self.style.outer_edges(); + let (distance, zone, area) = EDGES + .into_iter() + .filter(|zone| sides.contains(*zone)) + .filter_map(|zone| { + let area = self.edge_area(zone)?.rect.filter(|area| area.contains(pointer))?; + let distance = match zone { + Zone::Top => pointer.y - area.top(), + Zone::Bottom => area.bottom() - pointer.y, + Zone::Left => pointer.x - area.left(), + Zone::Right | Zone::Center => area.right() - pointer.x, + }; + Some((distance, zone, area)) + }) + .min_by(|a, b| a.0.total_cmp(&b.0))?; let band = if self.style.side_edge_zones { crate::docking::targets::SIDE_EDGE_BAND } else { self.style.edge_band }; - let band = band.min(area.width() / 4.0).min(area.height() / 4.0); - let line = |zone| crate::docking::targets::edge_line(area, zone, self.style.drop_line_width); - let mut edges = vec![(pointer.y - area.top(), Zone::Top), (area.bottom() - pointer.y, Zone::Bottom)]; - if self.style.side_edge_zones { - edges.extend([(pointer.x - area.left(), Zone::Left), (area.right() - pointer.x, Zone::Right)]); - } - let (distance, zone) = edges.into_iter().min_by(|a, b| a.0.total_cmp(&b.0))?; - if distance > band { + if distance > band.min(area.width() / 4.0).min(area.height() / 4.0) { return None; } + let line = crate::docking::targets::edge_line(area, zone, self.style.drop_line_width); let reach = |line: Rect| line.distance_to_pos(pointer) <= crate::docking::targets::STRIP_REACH; - if !self.style.side_edge_zones && self.in_tab_strip(pointer) && !line(zone).is_some_and(reach) { + if !self.style.side_edge_zones && self.in_tab_strip(pointer) && !line.is_some_and(reach) { return None; } - let anchor = - self.layout.panels().into_iter().find(|p| !members.contains(p) && !matches!(self.place(p), Some(Place::Floating { .. }) | None))?.clone(); + let anchor = self.edge_anchor(members, zone)?; if !self.style.side_edge_zones { - return Some((anchor, zone, line(zone)?)); + return Some((anchor, zone, line?)); } let size = area.size() * 0.25; let preview = match zone { @@ -203,6 +245,48 @@ impl Renderer<'_, P> { Some((anchor, zone, preview)) } + fn edge_area(&self, zone: Zone) -> Option<&EdgeArea

> { + self.frame.edges.as_ref()?.get(EDGES.iter().position(|edge| *edge == zone)?) + } + + /// The docked anchor, not one of `members`, of an outer edge drop at `zone` that wraps the + /// edge area drawn: protected where that area is the canvas column, as the model narrows on + /// a protected anchor, else not. The drawn area and the model's can still differ (hidden + /// panels, a canvas test that differs from these permissions), so the drop is tried: an + /// anchor whose drop wraps other panels than the area shows gives way to one that is not + /// protected, and then to no edge target. + fn edge_anchor(&self, members: &[P], zone: Zone) -> Option

{ + let area = self.edge_area(zone)?; + let protected = |p: &P| self.permissions(p).is_protected(); + let skip = |p: &P| members.contains(p); + let lead = members.first()?; + let wraps_area = |anchor: &P| { + let to = Target::At { anchor: anchor.clone(), placement: Placement::Edge(zone) }; + let action = match members { + [_] => Action::Move { panel: lead.clone(), anchor: anchor.clone(), placement: Placement::Edge(zone) }, + _ => Action::MoveGroup { panel: lead.clone(), to }, + }; + let mut candidate = self.layout.clone(); + if candidate.apply_with_permissions(action, |p| self.permissions(p)).is_err() { + // Left to the model's check. + return true; + } + let Some(Place::Node(path)) = candidate.place(lead) else { return false }; + let Some((last, parent)) = path.split_last() else { return false }; + let beside = [parent, &[!last]].concat(); + let Some(wrapped) = candidate.root.as_ref().and_then(|root| node_at(root, &beside)) else { return false }; + let shown = |p: &&P| !self.frame.hidden.contains(p) && !members.contains(p); + let wrapped: Vec<&P> = tree_panels(wrapped).into_iter().filter(shown).collect(); + let drawn: Vec<&P> = area.panels.iter().filter(shown).collect(); + wrapped.len() == drawn.len() && wrapped.iter().all(|p| drawn.contains(p)) + }; + let anchor = self.layout.edge_anchor(area.narrowed, &protected, &skip)?; + if wraps_area(&anchor) { + return Some(anchor); + } + self.layout.edge_anchor(false, &protected, &skip).filter(|other| *other != anchor && !protected(other) && wraps_area(other)) + } + /// Under the pointer while a drag offers a docking target: which modifier floats the drop /// instead. With native windows, the window under the pointer draws it, beside its preview. pub(super) fn paint_float_hint(&self, ctx: &Context, pointer: Pos2) { diff --git a/crates/ui/src/docking/render/group.rs b/crates/ui/src/docking/render/group.rs index c5f26e89d..1ddf3bca9 100644 --- a/crates/ui/src/docking/render/group.rs +++ b/crates/ui/src/docking/render/group.rs @@ -394,7 +394,7 @@ impl Renderer<'_, P> { }); let response = if elided { response.on_hover_text(&label) } else { response }; self.register_interaction(ui.ctx(), response.id); - if response.is_pointer_button_down_on() || response.clicked() { + if response.is_pointer_button_down_on() || response.clicked_by(egui::PointerButton::Primary) { ui.ctx().data_mut(|d| d.insert_temp(tab_id.with("pointer-focus"), true)); } // The click that expands a minimized group is not the first half of a double-click. diff --git a/crates/ui/src/docking/render/mod.rs b/crates/ui/src/docking/render/mod.rs index 273e15bb4..79a6836bb 100644 --- a/crates/ui/src/docking/render/mod.rs +++ b/crates/ui/src/docking/render/mod.rs @@ -21,7 +21,7 @@ pub(in crate::docking) use native::WindowDrag; #[cfg(all(test, feature = "native-windows"))] pub(in crate::docking) use { cross::DragPointer, - native::{APPKIT_CORNER, MACOS_CLOSE, Session, appkit_resize_areas, close_rects, floats_shown, resize_bands, session, wayland}, + native::{APPKIT_CORNER, MACOS_CLOSE, Session, appkit_resize_areas, close_rects, drag_opacity, floats_shown, resize_bands, session, wayland}, }; #[cfg(all(test, feature = "native-windows"))] diff --git a/crates/ui/src/docking/render/native.rs b/crates/ui/src/docking/render/native.rs index 12ef37a8e..9c96401dd 100644 --- a/crates/ui/src/docking/render/native.rs +++ b/crates/ui/src/docking/render/native.rs @@ -88,10 +88,22 @@ pub(in crate::docking) const MACOS_CLOSE: CloseLayout = pub(in crate::docking) const OTHER_CLOSE: CloseLayout = CloseLayout { glyph_top: 4.5, glyph_trailing: 11.5, glyph_size: 9.0, hit_left: 4.5, hit_right: 4.5, hit_up: 4.5, hit_down: 4.5 }; -/// Opacity of a native window while its bar or only tab drags it: the window stays above the -/// main window, so the drop preview under the pointer shows only through it. -#[cfg(target_os = "macos")] -const DRAG_OPACITY: f64 = 0.5; +/// Opacity of a native window while its bar or only tab drags it somewhere it can dock: the +/// window stays above the main window, so the drop preview under the pointer shows only +/// through it. +#[cfg(any(target_os = "macos", test))] +const DRAG_OPACITY: f64 = 0.75; + +/// Opacity of the window `drag` moves: faded while the drag can dock (its only tab or group, or +/// a bar with [`crate::docking::DockStyle::title_bar_docks`]), opaque for a plain move and while +/// Ctrl/Cmd keeps the drop floating (`float_only`). +#[cfg(any(target_os = "macos", test))] +pub(in crate::docking) fn drag_opacity(drag: Option<&WindowDrag>, float_only: bool, title_bar_docks: bool) -> f64 { + match drag { + Some(drag) if !float_only && (drag.docks || title_bar_docks) => DRAG_OPACITY, + _ => 1.0, + } +} /// Sets the opacity of the AppKit windows titled `title`, never one titled `main` (the main /// window's title). egui has no viewport command for a window's opacity, and a transparent @@ -580,9 +592,12 @@ impl Renderer<'_, P> { #[cfg(target_os = "macos")] { let main = ctx.input(|i| i.raw.viewports.get(&ViewportId::ROOT).and_then(|v| v.title.clone())); - let dragged = ctx.data(|d| d.get_temp::(drag_key)).and_then(|drag| self.layout.floating.iter().find(|w| w.id == drag.window)); - let faded = dragged.and_then(|w| crate::docking::dock_model::first_active(&w.root)).is_some_and(|p| self.label_of(p) == title); - set_window_opacity(&title, main.as_deref(), if faded { DRAG_OPACITY } else { 1.0 }); + let drag = ctx.data(|d| d.get_temp::(drag_key)); + let dragged = drag.as_ref().and_then(|drag| self.layout.floating.iter().find(|w| w.id == drag.window)); + let this = dragged.and_then(|w| crate::docking::dock_model::first_active(&w.root)).is_some_and(|p| self.label_of(p) == title); + let float_only = drag.is_some_and(|drag| self.cross.pointer.is_some_and(|p| p.window == Some(drag.window) && p.command)); + let opacity = if this { drag_opacity(drag.as_ref(), float_only, self.style.title_bar_docks) } else { 1.0 }; + set_window_opacity(&title, main.as_deref(), opacity); } if let Some(placed) = report.placed.filter(|_| !moving) { let waiting = place.pending.is_some_and(|until| now < until); diff --git a/crates/ui/src/docking/render/preview.rs b/crates/ui/src/docking/render/preview.rs index 2defec03c..f29e77f33 100644 --- a/crates/ui/src/docking/render/preview.rs +++ b/crates/ui/src/docking/render/preview.rs @@ -8,9 +8,9 @@ use crate::docking::{ Action, Drag, DropProposal, DropRequest, DropTarget, GroupDrag, GroupGesture, Layout, Node, Placement, Target, Zone, cancel_drag, dock_model::Place, surface::TAB_PADDING, - targets::{SIDE_EDGE_ZONE_DEPTH, resolve_drop_by}, + targets::{SIDE_EDGE_ZONE_DEPTH, resolve_drop_on}, }; -use crate::layout::SplitAxis; +use crate::layout::{SplitAxis, SplitSize}; /// Ctrl (Windows, Linux) or Cmd (macOS) held: the drop floats and no target is offered. fn float_only(ctx: &Context) -> bool { @@ -127,24 +127,35 @@ impl Renderer<'_, P> { } /// The valid target under the pointer for a whole group. A target holding the group - /// itself is a no-op proposal; a target the model would reject is no target. A window - /// moving live looks through itself and does not split frames or, moved by its bar, join - /// a body's center. + /// itself is a no-op proposal, and so is a drop that would change only sizes; a target + /// the model would reject is no target. A drag that takes a whole floating window along + /// looks through it ([`Self::looks_through`]). A window moving live does not split frames + /// or, moved by its bar, join a body's center. pub(super) fn group_proposal(&self, drag: &GroupDrag

, pointer: Pos2) -> Option> { - let window = drag.gesture == GroupGesture::Window; - if let Some(proposal) = self.extra_proposal(&drag.members, pointer, window) { - return self.valid_group(drag, proposal); + if let Some(proposal) = self.extra_proposal(&drag.members, pointer, self.looks_through(drag)) { + return self.valid_group(drag, self.settled(proposal, |to| group_action(drag, to))); } let target = self.group_target(drag, pointer)?; self.group_proposal_at(drag, pointer, target) } - /// The group under the pointer that a group drag addresses. A window moving live looks - /// through itself. + /// Whether a group drag looks through the floating window it comes from: the window's + /// title bar moves it, or the drag holds the window's only group and so takes the window + /// along. The window then covers nothing behind it. + pub(super) fn looks_through(&self, drag: &GroupDrag

) -> bool { + match drag.gesture { + GroupGesture::Window => true, + GroupGesture::Group => self.whole_window(&drag.panel), + GroupGesture::Column => false, + } + } + + /// The group under the pointer that a group drag addresses, past the drag's own groups + /// when it looks through its window. fn group_target(&self, drag: &GroupDrag

, pointer: Pos2) -> Option<&DropTarget

> { - let window = drag.gesture == GroupGesture::Window; + let through = self.looks_through(drag); let own = |t: &DropTarget

| t.tabs.iter().any(|(p, _)| drag.members.contains(p)); - self.targets.iter().rev().find(|t| crate::docking::targets::usable(t.rect) && t.rect.contains(pointer) && !(window && own(t))) + self.targets.iter().rev().find(|t| crate::docking::targets::usable(t.rect) && t.rect.contains(pointer) && !(through && own(t))) } /// [`Self::group_proposal`] on `target` alone. @@ -171,7 +182,7 @@ impl Renderer<'_, P> { let (edges, joins) = (false, false); let split = match proposal.placement { Placement::Split(Zone::Center) => !joins, - Placement::Split(_) => !edges, + Placement::Split(_) | Placement::SplitSized { .. } => !edges, _ => false, }; if window && !into_window && split { @@ -186,19 +197,130 @@ impl Renderer<'_, P> { (proposal.noop || self.layout.clone().apply_with_permissions(group_action(drag, to), |p| self.permissions(p)).is_ok()).then_some(proposal) } - /// [`crate::docking::resolve_drop_by`] with [`crate::docking::DockStyle::zone_rule`], where a split that would leave + /// [`crate::docking::resolve_drop_by`] with [`crate::docking::DockStyle::zone_rule`] and the + /// canvas sides of [`crate::docking::DockStyle::drop_edges`], where a split that would leave /// a group without room becomes a join of the target group, or no target when the group - /// takes no tabs. + /// takes no tabs. A drop that would change only sizes is a no-op ([`Self::settled`]). fn resolve_roomy(&self, panel: &P, pointer: Pos2, targets: &[DropTarget

], action: impl Fn(Target

) -> Action

) -> Option> { - let proposal = resolve_drop_by(panel, pointer, targets, self.style.zone_rule())?; + let proposal = resolve_drop_on(panel, pointer, targets, self.style.zone_rule(), self.style.drop_sides())?; if !matches!(proposal.placement, Placement::Split(zone) if zone != Zone::Center) { - return Some(proposal); + return Some(self.settled(proposal, action)); } if self.leaves_room(action(Target::At { anchor: proposal.anchor.clone(), placement: proposal.placement.clone() })) { - return Some(proposal); + return Some(self.settled(proposal, action)); + } + if let Some(placement) = self.sized_split(panel, &proposal, &action) + && self.leaves_room(action(Target::At { anchor: proposal.anchor.clone(), placement: placement.clone() })) + { + return Some(self.settled(DropProposal { placement, ..proposal }, action)); } let target = targets.iter().rev().find(|t| crate::docking::targets::usable(t.rect) && t.rect.contains(pointer))?; - resolve_drop_by(panel, pointer, std::slice::from_ref(&DropTarget { allow_splits: false, ..target.clone() }), self.style.zone_rule()) + let proposal = resolve_drop_on( + panel, + pointer, + std::slice::from_ref(&DropTarget { allow_splits: false, ..target.clone() }), + self.style.zone_rule(), + self.style.drop_sides(), + )?; + Some(self.settled(proposal, action)) + } + + /// The side split of `proposal`'s frame when halving it leaves no room + /// ([`Placement::SplitSized`]): the frame keeps its size, and the frames beyond the divider + /// on that side give the new frame its minimum (with [`DROP_MARGIN`]). A floating panel + /// keeps its size along the split instead, and one in a dock column its column's width or + /// its group's height, when that fits. `None` without such a divider, or when those frames + /// cannot give the new frame its minimum. + fn sized_split(&self, panel: &P, proposal: &DropProposal

, action: &impl Fn(Target

) -> Action

) -> Option> { + let Placement::Split(zone) = proposal.placement else { return None }; + let axis = if matches!(zone, Zone::Top | Zone::Bottom) { SplitAxis::Vertical } else { SplitAxis::Horizontal }; + let along = |v: Vec2| if axis == SplitAxis::Horizontal { v.x } else { v.y }; + // The divider on the frame's start side (left or top) belongs to a split holding the + // frame in its second child, as `split_frame_sized` finds it. + let start = matches!(zone, Zone::Left | Zone::Top); + let anchor = &proposal.anchor; + let now = self.drawn_groups(self.layout); + let frame = along(now.iter().find(|(node, _)| holds(node, anchor))?.1.size()); + let mut candidate = self.layout.clone(); + let halves = action(Target::At { anchor: anchor.clone(), placement: proposal.placement.clone() }); + candidate.apply_with_permissions(halves, |p| self.permissions(p)).ok()?; + let (root, area, path) = match candidate.place(anchor)? { + Place::Node(path) => (candidate.root.as_ref()?, self.workspace, path), + Place::Floating { window, path } => { + let window = candidate.floating.get(window)?; + (&window.root, self.window_body(window), path) + } + Place::Dock { .. } => return None, + }; + // The splits from the root down to the new one, as drawn with halves. + let sizer = self.sizer(); + let (_, split_path) = path.split_last()?; + let mut chain = vec![(root, area)]; + for &second in split_path { + let &(node, rect) = chain.last()?; + let [a, b] = sizer.children(node, rect)?; + chain.push(if second { b } else { a }); + } + let &(Node::Split { first, second, .. }, halved) = chain.last()? else { return None }; + let new = if start { first } else { second }; + let depth = (0..split_path.len()).rev().find(|&d| { + split_path.get(d) == Some(&start) && chain.get(d).is_some_and(|(n, _)| matches!(n, Node::Split { axis: own, .. } if *own == axis)) + })?; + let &(outer, outer_rect) = chain.get(depth)?; + let Node::Split { size: outer_size, first: a, second: b, .. } = outer else { return None }; + if matches!(**a, Node::Dock { .. }) || matches!(**b, Node::Dock { .. }) { + return None; + } + let [a, b] = sizer.children(outer, outer_rect)?; + let far = if start { a } else { b }; + // The far side's edge at the divider is its end edge when the divider is on the start + // side of the frame. + let room = sizer.travel(far.0, far.1, axis, start).0 - DROP_MARGIN; + let least = along(super::node_limits(new, self.style, &*self.content).min) + DROP_MARGIN; + let gap = self.style.gap; + // A split frame's width is its share of the room, not one of its own. + let saved = match self.place(panel) { + Some(Place::Floating { .. }) => now.iter().find(|(node, _)| holds(node, panel)).map(|(_, rect)| along(rect.size())), + Some(place @ Place::Dock { .. }) if axis == SplitAxis::Horizontal => self.layout.column(place).map(|column| column.width), + Some(place @ Place::Dock { .. }) => self.layout.group(place).and_then(|group| group.height), + _ => None, + }; + let most = along(halved.size()) + room - frame - gap; + if least > most { + return None; + } + let size = saved.filter(|width| (least..=most).contains(width)).unwrap_or(least); + // The anchor's side of the outer split grows by what the halves did not take already. + let grow = frame + gap + size - along(halved.size()); + let first = along(a.1.size()) + if start { -grow } else { grow }; + let outer = super::sizes::resized(*outer_size, first, along(a.1.size()) + along(b.1.size()))?; + let size = super::sizes::resized(SplitSize::Ratio(0.5), if start { size } else { frame }, frame + size)?; + Some(Placement::SplitSized { zone, size, outer }) + } + + /// Every group of `layout`'s trees as drawn, with its rect. + fn drawn_groups<'l>(&self, layout: &'l Layout

) -> Vec<(&'l Node

, Rect)> { + let sizer = self.sizer(); + let mut out = Vec::new(); + for (root, area) in self.trees(layout) { + let mut drawn = Vec::new(); + sizer.groups(root, area, &mut drawn); + out.extend(drawn.into_iter().map(|d| (d.node, d.rect))); + } + out + } + + /// `proposal`, marked a no-op when `action` at it would leave every panel where it is, + /// changing only sizes ([`Layout::same_arrangement`]): a lone group dropped beside the + /// group it already borders, or a tab on the edge its frame already holds. Such a drop + /// shows no target and changes nothing. + fn settled(&self, mut proposal: DropProposal

, action: impl Fn(Target

) -> Action

) -> DropProposal

{ + if !proposal.noop { + let mut candidate = self.layout.clone(); + let to = Target::At { anchor: proposal.anchor.clone(), placement: proposal.placement.clone() }; + proposal.noop = candidate.apply_with_permissions(action(to), |p| self.permissions(p)).is_ok() && candidate.same_arrangement(self.layout); + } + proposal } /// Whether `action`, a drop that splits a group or the outer edge, leaves room: every group @@ -238,8 +360,9 @@ impl Renderer<'_, P> { } /// Release of a group drag: join or split at the target, or float at the ghost with the - /// group's size. Over its own docked position nothing changes; a floating group released - /// over itself moves there. + /// group's size. Over its own docked position, or where the drop would change only sizes, + /// nothing changes; a floating group released over itself or a window's title bar moves + /// there. fn drop_group(&mut self, drag: &GroupDrag

, pointer: Pos2, float_only: bool) { if pointer.distance(drag.origin) < 2.0 { return; @@ -255,7 +378,8 @@ impl Renderer<'_, P> { self.output.actions.push(group_action(drag, to)); return; } - if proposal.is_some() && !floating { + // A no-op anchored on another group would change only sizes. + if proposal.is_some_and(|p| !floating || !drag.members.contains(&p.anchor)) { return; } if !float_only && !self.style.float_on_empty_drop { @@ -311,7 +435,7 @@ impl Renderer<'_, P> { if zones { self.paint_marker(&painter, &proposal); } else { - let split = matches!(proposal.placement, Placement::Split(zone) | Placement::Edge(zone) if zone != Zone::Center); + let split = matches!(proposal.placement, Placement::Split(zone) | Placement::SplitSized { zone, .. } | Placement::Edge(zone) if zone != Zone::Center); self.paint_preview(ctx, &painter, &proposal, split); } if drag.gesture != GroupGesture::Window { @@ -409,12 +533,14 @@ impl Renderer<'_, P> { /// The group target a single tab drag would take at `pointer`. pub(super) fn tab_proposal(&self, panel: &P, pointer: Pos2) -> Option> { - self.extra_proposal(std::slice::from_ref(panel), pointer, false).or_else(|| { - self.resolve_roomy(panel, pointer, &self.targets, |to| match to { - Target::At { anchor, placement } => Action::Move { panel: panel.clone(), anchor, placement }, - to => Action::MovePanel { panel: panel.clone(), to }, - }) - }) + let action = |to| match to { + Target::At { anchor, placement } => Action::Move { panel: panel.clone(), anchor, placement }, + to => Action::MovePanel { panel: panel.clone(), to }, + }; + match self.extra_proposal(std::slice::from_ref(panel), pointer, false) { + Some(proposal) => Some(self.settled(proposal, action)), + None => self.resolve_roomy(panel, pointer, &self.targets, action), + } } /// The preview of a tab drag's group target (with @@ -449,7 +575,7 @@ impl Renderer<'_, P> { /// region the drop takes, filled for a tab drag (`filled`) or a split. fn paint_preview(&self, ctx: &Context, painter: &egui::Painter, proposal: &DropProposal

, filled: bool) { if crate::docking::targets::usable(proposal.preview) { - let beside = matches!(proposal.placement, Placement::Split(zone) | Placement::Edge(zone) if zone != Zone::Center); + let beside = matches!(proposal.placement, Placement::Split(zone) | Placement::SplitSized { zone, .. } | Placement::Edge(zone) if zone != Zone::Center); if self.style.side_edge_zones { drop_preview(painter, proposal.preview.shrink(1.0), self.style.accent, filled); } else if beside { @@ -493,8 +619,7 @@ impl Renderer<'_, P> { /// [`Self::paint_zones`] for a group drag. pub(super) fn paint_group_zones(&self, painter: &egui::Painter, drag: &GroupDrag

, pointer: Pos2, proposal: Option<&DropProposal

>) -> bool { - let window = drag.gesture == GroupGesture::Window; - if !self.style.side_edge_zones || self.extra_proposal(&drag.members, pointer, window).is_some() { + if !self.style.side_edge_zones || self.extra_proposal(&drag.members, pointer, self.looks_through(drag)).is_some() { return false; } let Some(target) = self.group_target(drag, pointer) else { return false }; @@ -516,7 +641,7 @@ impl Renderer<'_, P> { use crate::docking::targets::{header_height, usable, zone_shape}; let zone_of = |proposal: &DropProposal

| match proposal.placement { _ if proposal.noop => None, - Placement::Split(zone) => Some(zone), + Placement::Split(zone) | Placement::SplitSized { zone, .. } => Some(zone), Placement::Tab { .. } => Some(Zone::Center), _ => None, }; @@ -680,6 +805,15 @@ impl Renderer<'_, P> { } } +/// Whether the drawn group `node` holds `panel`. +fn holds(node: &Node

, panel: &P) -> bool { + match node { + Node::Tabs { panels, .. } => panels.contains(panel), + Node::Stack { entries } => entries.iter().any(|e| &e.panel == panel), + Node::Split { .. } | Node::Dock { .. } => false, + } +} + /// The panel that names a drawn group: its first panel; none for a dock or a split. fn lead

(node: &Node

) -> Option<&P> { match node { diff --git a/crates/ui/src/docking/render/sizes.rs b/crates/ui/src/docking/render/sizes.rs index b1c9fd493..415ecf8dc 100644 --- a/crates/ui/src/docking/render/sizes.rs +++ b/crates/ui/src/docking/render/sizes.rs @@ -312,16 +312,22 @@ impl

Sizer<'_, P> { /// The size of a split at `path` whose first child is `first` of `available` points, in the /// split's own mode `size`: a fixed side keeps its points, a ratio stays a ratio. fn push_size(path: &[bool], size: SplitSize, first: f32, available: f32, out: &mut Vec<(Vec, SplitSize)>) { + if let Some(size) = resized(size, first, available) { + out.push((path.to_vec(), size)); + } +} + +/// `size` in its own mode for a first child of `first` out of `available` points. +pub(in crate::docking) fn resized(size: SplitSize, first: f32, available: f32) -> Option { if available <= 0.0 || !available.is_finite() || !first.is_finite() { - return; + return None; } let first = first.clamp(0.0, available); - let size = match size { + Some(match size { SplitSize::Ratio(_) => SplitSize::Ratio((first / available).clamp(0.0, 1.0)), SplitSize::FixedFirst(_) => SplitSize::FixedFirst(first), SplitSize::FixedSecond(_) => SplitSize::FixedSecond(available - first), - }; - out.push((path.to_vec(), size)); + }) } /// One group of a flat row, such as the panels of a stack or the groups of a dock column: diff --git a/crates/ui/src/docking/split_tests.rs b/crates/ui/src/docking/split_tests.rs index 8a07c80fa..fd3070c28 100644 --- a/crates/ui/src/docking/split_tests.rs +++ b/crates/ui/src/docking/split_tests.rs @@ -194,10 +194,10 @@ fn groups_have_one_small_minimum_and_no_maximum() { assert_eq!(group(&d, TOOLS).width(), 40.0, "and shrinks to the minimum, no further"); // At its minimum, with Project left of it at its own, it has no room for two halves and - // takes a drop on its edge as a tab. + // takes a drop on its left edge as a tab. let tools = group(&d, TOOLS); assert_eq!(group(&d, PROJECT[0]).width(), 40.0); - let out = d.drag(tab(&d, INFO), pos2(tools.right() - 4.0, tools.center().y + 30.0)); + let out = d.drag(tab(&d, INFO), pos2(tools.left() + 4.0, tools.center().y + 30.0)); assert!( matches!(&out.actions[..], [Action::Move { panel: INFO, anchor: TOOLS, placement: Placement::Split(Zone::Center) | Placement::Tab { .. } }]), "{:?}", @@ -359,6 +359,83 @@ fn a_drop_beside_a_narrow_group_splits_that_group_only() { assert_eq!((group(&d, PROJECT[0]), group(&d, TIMELINE), group(&d, METERS)), (project, timeline, meters)); } +/// Properties and a narrow Tools frame side by side, Tools `tools` wide in an area `width` +/// wide, with Info floating at the left. +fn beside_tools(width: f32, tools: f32) -> Driver { + let root = Node::Split { + axis: SplitAxis::Horizontal, + size: SplitSize::FixedSecond(tools), + first: Box::new(tabs(&PROPERTIES)), + second: Box::new(tabs(&[TOOLS])), + }; + let layout = Layout { root: Some(root), floating: vec![Floating::single(vec![INFO], 0, [width + 100.0, 100.0, 280.0, 200.0])] }; + let mut d = driver(layout, Film::default()); + d.area = Rect::from_min_size(Pos2::ZERO, vec2(width, SCREEN.y)); + d.settle(); + d +} + +#[test] +fn a_drop_beside_a_frame_too_narrow_to_halve_takes_room_from_its_neighbour() { + let mut d = beside_tools(1000.0, 60.0); + let (tools, properties, info) = (group(&d, TOOLS), group(&d, PROPERTIES[0]), group(&d, INFO)); + let left = zone_shape(tools, 26.0, 40.0, Zone::Left).unwrap(); + let at = pos2(tools.left() + 4.0, tools.center().y + 13.0); + d.hold(tab(&d, INFO), at); + assert!(d.filled_polygon(&left), "Tools' left zone is a target"); + let out = d.release(at); + assert!( + matches!(&out.actions[..], [Action::Move { panel: INFO, anchor: TOOLS, placement: Placement::SplitSized { zone: Zone::Left, .. } }]), + "{:?}", + out.actions + ); + d.settle(); + let (new_tools, new_info, new_properties) = (group(&d, TOOLS), group(&d, INFO), group(&d, PROPERTIES[0])); + assert!((new_tools.width() - tools.width()).abs() < 1.0, "Tools keeps its width: {new_tools:?}"); + assert!((new_info.width() - info.width()).abs() < 1.0, "Info keeps its floating width: {new_info:?}"); + assert!(new_info.right() < new_tools.left() && new_properties.right() < new_info.left()); + assert!(new_properties.width() < properties.width() - 200.0, "Properties gave the room"); +} + +#[test] +fn a_docked_panel_dropped_beside_a_frame_too_narrow_to_halve_takes_its_minimum_width() { + // Info docked below Properties, as wide as it, beside a narrow Tools frame. + let root = Node::Split { + axis: SplitAxis::Horizontal, + size: SplitSize::FixedSecond(60.0), + first: Box::new(split(SplitAxis::Vertical, 0.6, tabs(&PROPERTIES), tabs(&[INFO]))), + second: Box::new(tabs(&[TOOLS])), + }; + let mut d = driver(Layout { root: Some(root), floating: Vec::new() }, Film::default()); + d.area = Rect::from_min_size(Pos2::ZERO, vec2(1000.0, SCREEN.y)); + d.settle(); + let (tools, properties) = (group(&d, TOOLS), group(&d, PROPERTIES[0])); + let out = d.drag(tab(&d, INFO), pos2(tools.left() + 4.0, tools.center().y + 13.0)); + assert!( + matches!(&out.actions[..], [Action::Move { panel: INFO, anchor: TOOLS, placement: Placement::SplitSized { zone: Zone::Left, .. } }]), + "{:?}", + out.actions + ); + d.settle(); + let (new_tools, new_info, new_properties) = (group(&d, TOOLS), group(&d, INFO), group(&d, PROPERTIES[0])); + assert!((new_tools.width() - tools.width()).abs() < 1.0, "Tools keeps its width: {new_tools:?}"); + assert!(new_info.width() < 60.0, "Info takes its minimum, not its docked width: {new_info:?}"); + assert!(new_properties.width() > properties.width() - 60.0, "Properties gives only that: {new_properties:?}"); +} + +#[test] +fn a_drop_beside_a_narrow_frame_joins_it_when_no_neighbour_has_room() { + let mut d = beside_tools(130.0, 60.0); + let tools = group(&d, TOOLS); + let at = pos2(tools.left() + 4.0, tools.center().y + 13.0); + let out = d.drag(tab(&d, INFO), at); + assert!( + matches!(&out.actions[..], [Action::Move { panel: INFO, anchor: TOOLS, placement: Placement::Tab { .. } | Placement::Split(Zone::Center) }]), + "{:?}", + out.actions + ); +} + #[test] fn every_divider_travels_after_drops() { let mut d = film(); diff --git a/crates/ui/src/docking/surface.rs b/crates/ui/src/docking/surface.rs index 0b036b2f5..6b86c67b8 100644 --- a/crates/ui/src/docking/surface.rs +++ b/crates/ui/src/docking/surface.rs @@ -64,6 +64,12 @@ pub struct DockStyle { /// bottom edges add a full-width row, a group's zones are [`Self::side_zone_fraction`] of its /// body, and every preview is a line or a tab outline. pub side_edge_zones: bool, + /// Which sides of a group that takes no tabs (a canvas), and of the layout's outer edge, take + /// a drop (all four). Without [`Self::side_edge_zones`] the outer left and right edges are + /// new docks, so only top and bottom apply there. A dock-column app whose palettes dock only + /// at the left and right sets [`crate::docking::Sides::NONE`] with [`Self::new_dock_zones`] + /// on. Not used with [`Self::side_edge_zones`]. Groups that take tabs keep all four sides. + pub drop_edges: super::Sides, /// The share of a group's body, below the tab strip, that means "beside" on each side: of /// its width from the left and right edges, of its height from the top and bottom edges /// (one third, at most one half). The middle joins the group as a tab. Not used with @@ -190,6 +196,7 @@ impl DockStyle { title_height: super::chrome::TITLE_HEIGHT, edge_band: 28.0, side_edge_zones: false, + drop_edges: super::Sides::ALL, side_zone_fraction: 1.0 / 3.0, drop_line_width: 4.0, new_dock_zones: true, @@ -227,6 +234,19 @@ impl DockStyle { } } + /// The canvas sides [`Self::drop_edges`] gives in this profile: all four with + /// [`Self::side_edge_zones`]. + pub(super) fn drop_sides(&self) -> super::Sides { + if self.side_edge_zones { super::Sides::ALL } else { self.drop_edges } + } + + /// The outer edges that take a drop: all four with [`Self::side_edge_zones`], else the top + /// and bottom of [`Self::drop_edges`]. + pub(super) fn outer_edges(&self) -> super::Sides { + let sides = self.drop_sides(); + if self.side_edge_zones { sides } else { super::Sides { left: false, right: false, ..sides } } + } + /// How a group's body divides into drop zones: [`crate::docking::SIDE_EDGE_ZONE_DEPTH`] deep /// with [`Self::side_edge_zones`], else [`Self::side_zone_fraction`] of it, with /// [`Self::drop_line_width`] previews. Pass it to [`crate::docking::resolve_drop_by`]. @@ -343,9 +363,9 @@ const FLOAT_INSET: f32 = 24.0; #[cfg_attr(not(feature = "serde"), allow(dead_code))] pub(super) const DEFAULT_FLOAT: Vec2 = Vec2::new(260.0, 320.0); -/// Where a docking command floats a panel, group or column when it gives no rect (see -/// `DockCommand::to_actions`, feature `serde`). [`DockArea`] records one each frame; read it -/// with [`float_space`]. +/// Where a docking command floats a panel, group or column when it gives no rect, and how the +/// area draws its outer edge (see `DockCommand::to_actions`, feature `serde`). [`DockArea`] +/// records one each frame; read it with [`float_space`]. #[derive(Clone, Debug, PartialEq)] pub struct FloatSpace

{ /// The rect the area drew in. A floated unit goes inside it, 24 points from its top-left @@ -358,13 +378,23 @@ pub struct FloatSpace

{ /// The height each panel's content used when it was last drawn, which fits a floated group /// to its content; `None` for content that filled its body. pub(super) contents: Vec<(P, Option)>, + /// The area draws with [`DockStyle::side_edge_zones`]: an outer edge drop wraps the part + /// between the docks, never only the canvas column. + pub(super) side_edge_zones: bool, } impl FloatSpace

{ /// An area with no recorded sizes: commands float a panel at 260x320, and a group or /// column at its dock column's width (else 260) by 320. pub fn new(area: Rect) -> Self { - Self { area, panels: Vec::new(), groups: Vec::new(), columns: Vec::new(), contents: Vec::new() } + Self { area, panels: Vec::new(), groups: Vec::new(), columns: Vec::new(), contents: Vec::new(), side_edge_zones: false } + } + + /// For an area drawn with [`DockStyle::side_edge_zones`] when the area has not drawn yet + /// (a headless run): an outer edge command then wraps what that profile's edge band wraps. + pub fn with_side_edge_zones(mut self, on: bool) -> Self { + self.side_edge_zones = on; + self } /// Adds the size a command floats `panel` at alone when the area has not drawn it, such as @@ -476,54 +506,19 @@ fn cancel_drag_if_replaced }) else { return; }; - let now = shape(layout); - if now == shape(&last.layout) { + let now = layout.shape(); + if now == last.layout.shape() { return; } let mut applied = last.layout; for action in last.actions { let _ = applied.apply(action); } - if now != shape(&applied) { + if now != applied.shape() { cancel_drag::

(ctx, area); } } -/// `layout` without its sizes and rectangles: which panels are where, and their state. -fn shape(layout: &Layout

) -> Layout

{ - use super::{Column, Floating, Group, Node, StackEntry}; - use crate::layout::SplitSize; - fn node(n: &Node

) -> Node

{ - match n { - Node::Split { axis, first, second, .. } => { - Node::Split { axis: *axis, size: SplitSize::Ratio(0.5), first: Box::new(node(first)), second: Box::new(node(second)) } - } - Node::Tabs { panels, active } => Node::Tabs { panels: panels.clone(), active: *active }, - Node::Stack { entries } => Node::Stack { entries: entries.iter().map(|e| StackEntry { height: None, ..e.clone() }).collect() }, - Node::Dock { side, columns } => Node::Dock { - side: *side, - columns: columns - .iter() - .map(|c| Column { - groups: c.groups.iter().map(|g| Group { height: None, ..g.clone() }).collect(), - width: 0.0, - iconic: c.iconic, - iconic_width: None, - }) - .collect(), - }, - } - } - Layout { - root: layout.root.as_ref().map(node), - floating: layout - .floating - .iter() - .map(|w| Floating { root: node(&w.root), rect: [0.0; 4], native: None, monitor: None, monitor_size: None, ..w.clone() }) - .collect(), - } -} - /// Keep what [`cancel_drag_if_replaced`] compares the next frame against, while a drag of this /// area goes on. fn record_drag_frame( @@ -860,6 +855,10 @@ pub trait DockContent

{ 0.0 } /// Draw the group header into `ui.max_rect()`, sized by [`Self::group_header_width`]. + /// The header's widgets take their own input, so a drag on them moves nothing. For a widget + /// that should drag (a ≡ button, say), call [`begin_drag`] with the id given to + /// [`DockArea::new`], `active` and the widget's rect when its response reports + /// `drag_started()`; the panel then moves as its tab would. fn group_header(&mut self, _ui: &mut Ui, _panels: &[P], _active: &P, _floating: bool) {} } @@ -988,6 +987,8 @@ impl<'a, P: Clone + Eq + Hash + Debug + Send + Sync + 'static> DockArea<'a, P> { /// one it is on, and a restored layout puts each window back on its monitor, or on the /// main window's when that monitor is gone. egui 0.36 reports no monitors; without a list /// windows store the monitor size egui reports and restore onto the main window's monitor. + /// eframe 0.36 keeps winit's event loop, and with it `available_monitors`, to itself, so an + /// eframe application builds the list from the platform's own display API. pub fn monitors(mut self, monitors: Vec) -> Self { self.monitors = monitors; self @@ -1177,7 +1178,7 @@ impl<'a, P: Clone + Eq + Hash + Debug + Send + Sync + 'static> DockArea<'a, P> { ui.ctx().data_mut(|d| d.insert_temp(blocked_key, self.input_blocked)); cancel_drag_if_replaced(ui.ctx(), self.id, layout); release_moved_tab_focus(ui.ctx(), self.id, layout); - let (view, frame) = super::render::view(layout, self.maximized.as_ref(), self.hide, &*content); + let (view, frame) = super::render::view(layout, self.maximized.as_ref(), self.hide, &*content, !style.side_edge_zones); let places = layout.places(); // A panel keeps the content height it last drew while hidden or minimized. let contents: Vec<(P, Option)> = float_space::

(ui.ctx(), self.id) @@ -1201,7 +1202,7 @@ impl<'a, P: Clone + Eq + Hash + Debug + Send + Sync + 'static> DockArea<'a, P> { output: DockOutput::default(), frame, iconic: super::render::Iconic::new(self.auto_collapse), - float_space: FloatSpace { contents, ..FloatSpace::new(rect) }, + float_space: FloatSpace { contents, side_edge_zones: style.side_edge_zones, ..FloatSpace::new(rect) }, places, contents: content_index, dividers: Vec::new(), diff --git a/crates/ui/src/docking/targets.rs b/crates/ui/src/docking/targets.rs index c9ac64a71..0752cbb67 100644 --- a/crates/ui/src/docking/targets.rs +++ b/crates/ui/src/docking/targets.rs @@ -23,7 +23,8 @@ pub struct DropProposal

{ pub placement: Placement

, pub preview: Rect, pub insertion: Option, - /// The pointer addresses the tab's current insertion slot. + /// The pointer addresses the tab's current insertion slot. The renderer also marks a drop + /// that would leave every panel where it is, changing only sizes. pub noop: bool, } @@ -78,6 +79,12 @@ pub fn drop_zone(rect: Rect, tab_height: f32, depth: f32, pointer: Pos2) -> Opti /// or less leaves only the center; more than one half acts as one half. Outside, non-finite, /// empty and inverted rectangles have no target. pub fn body_zone(rect: Rect, tab_height: f32, fraction: f32, center: bool, pointer: Pos2) -> Option { + body_zone_on(rect, tab_height, fraction, center, Sides::ALL, pointer) +} + +/// [`body_zone`] where a body without `center` maps only to the nearest of `sides`, and to no +/// target without any. +pub(super) fn body_zone_on(rect: Rect, tab_height: f32, fraction: f32, center: bool, sides: Sides, pointer: Pos2) -> Option { if !usable(rect) || !pointer.is_finite() || !rect.contains(pointer) || !tab_height.is_finite() || tab_height < 0.0 { return None; } @@ -87,13 +94,16 @@ pub fn body_zone(rect: Rect, tab_height: f32, fraction: f32, center: bool, point } let fraction = fraction.min(0.5); let (w, h) = (body.width(), body.height()); - let sides = [ + let allowed = |zone: &Zone| center || sides.contains(*zone); + let (distance, zone) = [ ((pointer.x - body.left()) / w, Zone::Left), ((body.right() - pointer.x) / w, Zone::Right), ((pointer.y - body.top()) / h, Zone::Top), ((body.bottom() - pointer.y) / h, Zone::Bottom), - ]; - let (distance, zone) = sides.into_iter().min_by(|a, b| a.0.total_cmp(&b.0))?; + ] + .into_iter() + .filter(|(_, zone)| allowed(zone)) + .min_by(|a, b| a.0.total_cmp(&b.0))?; Some(if center && distance > fraction { Zone::Center } else { zone }) } @@ -216,6 +226,34 @@ pub fn resolve_drop(panel: &P, pointer: Pos2, targets: &[DropTarg resolve_drop_by(panel, pointer, targets, ZoneRule::Depth(zone_depth)) } +/// A set of sides: of the layout's outer edge, or of a group's body +/// ([`crate::docking::DockStyle::drop_edges`]). +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct Sides { + pub left: bool, + pub right: bool, + pub top: bool, + pub bottom: bool, +} + +impl Sides { + pub const ALL: Self = Self { left: true, right: true, top: true, bottom: true }; + pub const NONE: Self = Self { left: false, right: false, top: false, bottom: false }; + pub const LEFT_RIGHT: Self = Self { left: true, right: true, top: false, bottom: false }; + pub const TOP_BOTTOM: Self = Self { left: false, right: false, top: true, bottom: true }; + + /// Whether `zone`'s side is in the set; never the center. + pub fn contains(self, zone: Zone) -> bool { + match zone { + Zone::Left => self.left, + Zone::Right => self.right, + Zone::Top => self.top, + Zone::Bottom => self.bottom, + Zone::Center => false, + } + } +} + /// How [`resolve_drop_by`] divides a group's body. #[derive(Clone, Copy, Debug, PartialEq)] pub enum ZoneRule { @@ -229,6 +267,18 @@ pub enum ZoneRule { /// [`resolve_drop`] with the body zones of `rule`: [`crate::docking::DockStyle::zone_rule`] /// gives the ones the dock area uses. pub fn resolve_drop_by(panel: &P, pointer: Pos2, targets: &[DropTarget

], rule: ZoneRule) -> Option> { + resolve_drop_on(panel, pointer, targets, rule, Sides::ALL) +} + +/// [`resolve_drop_by`] where a group that takes no tabs splits only on `sides` with +/// [`ZoneRule::Fraction`]. +pub(super) fn resolve_drop_on( + panel: &P, + pointer: Pos2, + targets: &[DropTarget

], + rule: ZoneRule, + sides: Sides, +) -> Option> { if !pointer.is_finite() || targets.len() > super::MAX_PANELS { return None; } @@ -270,7 +320,7 @@ pub fn resolve_drop_by(panel: &P, pointer: Pos2, targets: &[DropT let zone = match rule { _ if !target.allow_splits => Zone::Center, ZoneRule::Depth(depth) => drop_zone(target.rect, header, depth, pointer)?, - ZoneRule::Fraction { fraction, .. } => body_zone(target.rect, header, fraction, target.accepts_tabs, pointer)?, + ZoneRule::Fraction { fraction, .. } => body_zone_on(target.rect, header, fraction, target.accepts_tabs, sides, pointer)?, }; if zone == Zone::Center && target.accepts_tabs && target.tabs.iter().any(|(p, _)| p == panel) { return Some(DropProposal { diff --git a/crates/ui/src/docking/trial_tests.rs b/crates/ui/src/docking/trial_tests.rs index da55949f4..949d93108 100644 --- a/crates/ui/src/docking/trial_tests.rs +++ b/crates/ui/src/docking/trial_tests.rs @@ -221,6 +221,14 @@ fn a_clicked_tab_shows_no_focus_outline_but_tab_key_focus_does() { assert_eq!(d.ctx.memory(|m| m.focused()), Some(id)); d.step(vec![]); assert!(outlined(&d)); + + // Space and Enter activate the tab without counting as a pointer click. + for activate in [egui::Key::Space, egui::Key::Enter] { + let out = d.step(vec![key(activate)]); + assert!(out.actions.contains(&Action::Activate { panel: 3 }), "{:?}", out.actions); + d.step(vec![]); + assert!(outlined(&d), "{activate:?} hid the focus outline"); + } } #[test] diff --git a/crates/ui/src/docking/workspace.rs b/crates/ui/src/docking/workspace.rs index 183af38d3..c3711a50c 100644 --- a/crates/ui/src/docking/workspace.rs +++ b/crates/ui/src/docking/workspace.rs @@ -312,6 +312,13 @@ fn reversed(hidden: &Location

, location: &Location

, panel: Placement::Split(Zone::Bottom) => (Placement::Split(Zone::Top), location.size), Placement::Group { below, .. } => (Placement::Group { below: !below, height: None }, None), Placement::Column { after } => (Placement::Column { after: !after }, None), + Placement::SplitSized { zone, .. } => match zone { + Zone::Left => (Placement::Split(Zone::Right), location.size), + Zone::Right => (Placement::Split(Zone::Left), location.size), + Zone::Top => (Placement::Split(Zone::Bottom), location.size), + Zone::Bottom => (Placement::Split(Zone::Top), location.size), + Zone::Center => return None, + }, Placement::Edge(_) => return None, }; Some(Location { floating: None, anchor: Some(panel.clone()), placement, size, sibling: Vec::new(), ..hidden.clone() }) diff --git a/crates/ui/src/panes/mod.rs b/crates/ui/src/panes/mod.rs index f0eb66765..07365378d 100644 --- a/crates/ui/src/panes/mod.rs +++ b/crates/ui/src/panes/mod.rs @@ -5,13 +5,16 @@ //! `CentralPanel`; the document never becomes a panel. The app owns which panes are open and //! where they are: the host takes the open list and each pane's [`PaneState`] every frame and //! returns [`PaneRequest`]s for the app to run through its own commands. Docking chrome -//! ([`crate::docking::chrome`]) draws the floating frames and title bars. +//! ([`crate::docking::chrome`]) draws the floating frames and title bars. [`Rail`] draws an icon +//! strip at one edge that opens one pane at a time. use std::fmt::Debug; use std::hash::Hash; use std::ops::RangeInclusive; -use egui::{Context, Frame, Id, Key, LayerId, Margin, Modifiers, Order, Pos2, Rect, Response, Sense, Ui, UiBuilder, Vec2, pos2, vec2}; +use egui::{ + Context, Frame, Id, Key, LayerId, Margin, Modifiers, Order, Pos2, Rect, Response, Role, Sense, Ui, UiBuilder, Vec2, WidgetInfo, pos2, vec2, +}; use crate::docking::PanelLimits; use crate::docking::chrome::{self, ChromeStyle, FloatingFrame, MoveStep, TitleBar}; @@ -20,6 +23,10 @@ use crate::layout::{ResizeHandle, ResizeHandleStyle, SplitAxis}; #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; +mod rail; + +pub use rail::{RAIL_BUTTON, RAIL_WIDTH, Rail, RailItem, RailOutput, RailRequest}; + /// A title dragged this far out of a docked pane floats it; a pane dropped this close to the /// host's left or right edge docks there. pub const DOCK_SNAP: f32 = 32.0; @@ -136,6 +143,13 @@ pub trait PaneContent

{ fn closable(&self, _pane: &P) -> bool { true } + /// `false`: a docked pane draws no title bar (no title, ▾ menu, × or title drag), for bodies + /// that draw their own header. The body fills the pane, the resize edge stays and F6 still + /// reaches the pane. Close then comes only from the app. A floating pane always draws its + /// title bar, which moves it. + fn title_bar(&self, _pane: &P) -> bool { + true + } /// App items, shown above the Move, Float and Close items of the ▾ menu. fn menu(&mut self, _ui: &mut Ui, _pane: &P) {} } @@ -205,6 +219,7 @@ struct DragOut { canceled: bool, } +#[derive(Clone)] struct Shown

{ pane: P, id: Id, @@ -224,6 +239,7 @@ fn memory_id(id: Id) -> Id { /// Move focus to the next pane (the previous one with `reverse`) at the host's next frame. For /// apps that route F6 through their own commands; see [`PaneHost::f6`]. +/// Pass the same explicit [`Id`] used for the host; strings require [`Id::unique`]. pub fn focus_next(ctx: &Context, id: impl Into, reverse: bool) { let key = memory_id(id.into()); ctx.data_mut(|data| data.get_temp_mut_or_default::(key).cycle = Some(reverse)); @@ -231,6 +247,8 @@ pub fn focus_next(ctx: &Context, id: impl Into, reverse: bool) { } impl PaneHost { + /// Creates a host with a stable global ID. Use [`Id::unique`] to turn a string or tuple + /// into an ID, and reuse that ID for [`focus_next`]. pub fn new(id: impl Into) -> Self { Self { id: id.into(), @@ -349,20 +367,9 @@ impl PaneHost { states(pane).or_else(|| ctx.data(|data| data.get_temp::(id.with("state")))).unwrap_or_else(|| content.default_state(pane)); shown.push(Shown { pane: pane.clone(), id, state }); } - if self.policy == SidePolicy::OneSlot { - let mut taken = Vec::new(); - for index in (0..shown.len()).rev() { - if let Place::Docked(edge) = shown[index].state.place - && content.closable(&shown[index].pane) - { - if taken.contains(&edge) { - shown.remove(index); - } else { - taken.push(edge); - } - } - } - } + // Before the one-slot filter: a drop can reveal a pane this frame hides. + let open_panes = shown.clone(); + self.one_slot(&mut shown, content); let previous = memory.active.filter(|active| shown.iter().any(|s| s.id == *active)); memory.active = previous; @@ -391,12 +398,13 @@ impl PaneHost { let mut regions = Vec::new(); for edge in [Edge::Start, Edge::End, Edge::Bottom] { for s in shown.iter().filter(|s| s.state.place == Place::Docked(edge)) { - let rect = self.docked(ui, s, edge, content, &style, focus_target == Some(s.id), bounds, &mut memory, &preview, &mut requests); + let rect = + self.docked(ui, &open_panes, s, edge, content, &style, focus_target == Some(s.id), bounds, &mut memory, &preview, &mut requests); regions.push(Region { pane: s.id, rect, floating: None }); } } for s in shown.iter().filter(|s| s.state.place == Place::Floating) { - let (rect, layer) = self.floating(&ctx, s, content, &style, focus_target == Some(s.id), bounds, &mut requests); + let (rect, layer) = self.floating(&ctx, &open_panes, s, content, &style, focus_target == Some(s.id), bounds, &preview, &mut requests); regions.push(Region { pane: s.id, rect, floating: Some(layer) }); } @@ -461,10 +469,90 @@ impl PaneHost { (min, max.min(self.max_fraction * extent).max(min)) } + /// With [`SidePolicy::OneSlot`], drop every closable docked pane but the last on its edge. + fn one_slot>(&self, shown: &mut Vec>, content: &C) { + if self.policy != SidePolicy::OneSlot { + return; + } + let mut taken = Vec::new(); + for index in (0..shown.len()).rev() { + if let Place::Docked(edge) = shown[index].state.place + && content.closable(&shown[index].pane) + { + if taken.contains(&edge) { + shown.remove(index); + } else { + taken.push(edge); + } + } + } + } + + /// The width (height at the bottom) of a pane docked at `edge`: its size, else its default, + /// within its limits. + fn docked_size>(&self, content: &C, s: &Shown

, edge: Edge, bounds: Rect) -> f32 { + let extent = if self.side(edge) == Side::Bottom { bounds.height() } else { bounds.width() }; + let (min, max) = self.limits(content, &s.pane, extent); + let default = content.default_state(&s.pane).size; + s.state.size.filter(|v| v.is_finite()).or(default.filter(|v| v.is_finite())).unwrap_or(min).clamp(min, max) + } + + /// The rect `pane` takes on the next frame once it is docked at the side `edge`. Side panels + /// are laid out as [`Self::show`] draws them: `Start` panes, then `End` panes, each in `open` + /// order from the outside in and no wider than the space left. `None` when + /// [`SidePolicy::OneSlot`] would hide the pane there. + fn landing>(&self, shown: &[Shown

], pane: Id, edge: Edge, content: &C, bounds: Rect) -> Option { + let mut after: Vec> = shown + .iter() + .map(|s| { + let place = if s.id == pane { Place::Docked(edge) } else { s.state.place }; + Shown { pane: s.pane.clone(), id: s.id, state: PaneState { place, ..s.state } } + }) + .collect(); + self.one_slot(&mut after, content); + let mut free = bounds; + for side_edge in [Edge::Start, Edge::End] { + for s in after.iter().filter(|s| s.state.place == Place::Docked(side_edge)) { + let size = self.docked_size(content, s, side_edge, bounds).min(free.width()).max(0.0); + let rect = if self.side(side_edge) == Side::Left { + let rect = Rect::from_x_y_ranges(free.left()..=free.left() + size, free.y_range()); + free.min.x = rect.right(); + rect + } else { + let rect = Rect::from_x_y_ranges(free.right() - size..=free.right(), free.y_range()); + free.max.x = rect.left(); + rect + }; + if s.id == pane { + return Some(rect); + } + } + } + None + } + + /// Paints where `pane` lands when it is dropped docked at the side `edge`. + #[allow(clippy::too_many_arguments)] + fn preview_dock>( + &self, + preview: &egui::Painter, + style: &ChromeStyle, + shown: &[Shown

], + pane: Id, + edge: Edge, + content: &C, + bounds: Rect, + ) { + if let Some(rect) = self.landing(shown, pane, edge, content, bounds) { + chrome::drop_preview(preview, rect.shrink(1.0), style.accent, true); + } + } + #[allow(clippy::too_many_arguments)] fn docked>( &self, ui: &mut Ui, + shown: &[Shown

], s: &Shown

, edge: Edge, content: &mut C, @@ -478,8 +566,10 @@ impl PaneHost { let side = self.side(edge); let extent = if side == Side::Bottom { bounds.height() } else { bounds.width() }; let (min, max) = self.limits(content, &s.pane, extent); - let default = content.default_state(&s.pane).size; - let size = s.state.size.filter(|v| v.is_finite()).or(default.filter(|v| v.is_finite())).unwrap_or(min).clamp(min, max); + // A side pane takes no more than the width the panes outside it leave: egui would place + // a wider one partly beyond the host's edge. + let room = if side == Side::Bottom { f32::INFINITY } else { ui.available_rect_before_wrap().width().max(0.0) }; + let size = self.docked_size(content, s, edge, bounds).min(room); let panel_id = s.id.with("panel"); let panel = match side { Side::Left => egui::Panel::left(panel_id), @@ -487,26 +577,45 @@ impl PaneHost { Side::Bottom => egui::Panel::bottom(panel_id), }; let frame = self.frame.unwrap_or_else(|| Frame::NONE.fill(ui.visuals().panel_fill).stroke(ui.visuals().widgets.noninteractive.bg_stroke)); - let shown = panel.exact_size(size).resizable(false).frame(frame).show(ui, |ui| { + let title_bar = content.title_bar(&s.pane); + let drawn = panel.exact_size(size).resizable(false).frame(frame).show(ui, |ui| { let top = ui.max_rect(); - let title = Rect::from_min_size(top.min, vec2(top.width(), TITLE_BAR_HEIGHT)); - ui.allocate_rect(title, Sense::hover()); - let bar = self.chrome(ui, title, s, content, style, focus, bounds, requests); + let (bar, body_top) = if title_bar { + let title = Rect::from_min_size(top.min, vec2(top.width(), TITLE_BAR_HEIGHT)); + ui.allocate_rect(title, Sense::hover()); + (Some(self.chrome(ui, title, s, content, style, focus, bounds, requests)), title.bottom()) + } else { + (None, top.top()) + }; // The pane reserves exactly its size and clips its body. egui cuts a panel wider // than its exact size at its outer edge, so a wide body would shift a right or // bottom pane's reserved rect inwards and the document would draw over the pane. - let body = Rect::from_min_max(pos2(top.left(), title.bottom()), top.max); + let body = Rect::from_min_max(pos2(top.left(), body_top), top.max); + // Without a title bar, F6 focuses the body itself; Tab goes on into its widgets, + // which register after it. + let stop = (!title_bar).then(|| { + let stop = ui.interact(body, s.id.with("body"), Sense::focusable_noninteractive()); + let title = content.title(&s.pane); + stop.widget_info(|| WidgetInfo::labeled(Role::Pane, true, &title)); + if focus { + stop.request_focus(); + } + stop + }); let mut child = ui.new_child(UiBuilder::new().max_rect(body).layout(*ui.layout())); child.set_clip_rect(body.intersect(ui.clip_rect())); Frame::NONE.inner_margin(self.margin).show(&mut child, |ui| { ui.set_min_width(ui.available_width()); content.body(ui, &s.pane); }); + if stop.as_ref().is_some_and(Response::has_focus) { + ui.painter().rect_stroke(body.shrink(1.0), 0.0, egui::Stroke::new(1.0, style.accent), egui::StrokeKind::Inside); + } ui.allocate_rect(body, Sense::hover()); bar }); - let rect = shown.response.rect; - let bar = shown.inner; + let rect = drawn.response.rect; + let bar = drawn.inner; let (axis, strip) = match side { Side::Left => (SplitAxis::Horizontal, Rect::from_x_y_ranges(rect.right() - 2.0..=rect.right() + 2.0, rect.y_range())), @@ -514,7 +623,13 @@ impl PaneHost { Side::Bottom => (SplitAxis::Vertical, Rect::from_x_y_ranges(rect.x_range(), rect.top() - 2.0..=rect.top() + 2.0)), }; let handle_style = ResizeHandleStyle { hovered: style.accent.gamma_multiply(0.5), dragged: style.accent, focused: style.accent }; - let resize = ResizeHandle::new(s.id.with("resize"), axis, strip, &self.labels.resize, handle_style).show(ui); + // The edge's hit strip reaches over the document, whose widgets register after the host + // and would take presses there on the host's layer. A sublayer sits directly above it: + // over the document, under floating windows. + let edge_layer = LayerId::new(ui.layer_id().order, s.id.with("resize-layer")); + ui.ctx().set_sublayer(ui.layer_id(), edge_layer); + let mut edge_ui = ui.new_child(UiBuilder::new().layer_id(edge_layer)); + let resize = ResizeHandle::new(s.id.with("resize"), axis, strip, &self.labels.resize, handle_style).show(&mut edge_ui); let next = if resize.response.dragged() { resize.response.interact_pointer_pos().map(|p| match side { Side::Left => p.x - rect.left(), @@ -533,29 +648,26 @@ impl PaneHost { requests.push(PaneRequest::Resize(s.pane.clone(), next)); } - if content.movable(&s.pane) { - // Docked at the host's left or right edge, the pane gets the width the side limits - // give its size: a bottom pane's height, not its width. - let (side_min, side_max) = self.limits(content, &s.pane, bounds.width()); - let side_width = - s.state.size.filter(|v| v.is_finite()).or(default.filter(|v| v.is_finite())).unwrap_or(side_min).clamp(side_min, side_max); - self.drag_out(ui.ctx(), &bar, s, edge, rect, side_width, bounds, memory, preview, style, requests); + if content.movable(&s.pane) + && let Some(bar) = &bar + { + self.drag_out(ui.ctx(), bar, shown, s, edge, rect, content, bounds, memory, preview, style, requests); } rect } /// A docked pane's title dragged beyond [`DOCK_SNAP`] of the pane: dropped near the host's - /// left or right edge it docks there `side_width` wide, elsewhere it floats under the - /// pointer. + /// left or right edge it docks there, elsewhere it floats under the pointer. #[allow(clippy::too_many_arguments)] - fn drag_out( + fn drag_out>( &self, ctx: &Context, bar: &Response, + shown: &[Shown

], s: &Shown

, edge: Edge, rect: Rect, - side_width: f32, + content: &C, bounds: Rect, memory: &mut Memory, preview: &egui::Painter, @@ -598,23 +710,23 @@ impl PaneHost { if drag.canceled || !outside { return; } - let shape = match target.map(|t| self.side(t)) { - Some(Side::Left) => Rect::from_x_y_ranges(bounds.left()..=bounds.left() + side_width, bounds.y_range()), - Some(_) => Rect::from_x_y_ranges(bounds.right() - side_width..=bounds.right(), bounds.y_range()), - None => float, - }; - chrome::drop_preview(preview, shape.shrink(1.0), style.accent, true); + match target { + Some(target) => self.preview_dock(preview, style, shown, s.id, target, content, bounds), + None => chrome::drop_preview(preview, float.shrink(1.0), style.accent, true), + } } #[allow(clippy::too_many_arguments)] fn floating>( &self, ctx: &Context, + panes: &[Shown

], s: &Shown

, content: &mut C, style: &ChromeStyle, focus: bool, bounds: Rect, + preview: &egui::Painter, requests: &mut Vec>, ) -> (Rect, LayerId) { let (min_width, _) = self.limits(content, &s.pane, bounds.width()); @@ -641,7 +753,13 @@ impl PaneHost { requests.push(PaneRequest::FloatRect(s.pane.clone(), rect_array(resized))); } if content.movable(&s.pane) { - match chrome::live_move(ctx, &shown.inner, shown.rect, bounds) { + let step = chrome::live_move(ctx, &shown.inner, shown.rect, bounds); + if let Some(MoveStep::Moving(_)) = step + && let Some(edge) = ctx.pointer_latest_pos().and_then(|pointer| self.snap(pointer, bounds)) + { + self.preview_dock(preview, style, panes, s.id, edge, content, bounds); + } + match step { Some(MoveStep::Moving(next) | MoveStep::Canceled(next)) if next != shown.rect => { requests.push(PaneRequest::FloatRect(s.pane.clone(), rect_array(next))); } @@ -737,5 +855,7 @@ pub fn float_rect(saved: Option<[f32; 4]>, bounds: Rect, min: Vec2) -> Rect { Rect::from_min_size(pos, size) } +#[cfg(test)] +mod rail_tests; #[cfg(test)] mod tests; diff --git a/crates/ui/src/panes/rail.rs b/crates/ui/src/panes/rail.rs new file mode 100644 index 000000000..c6756e7be --- /dev/null +++ b/crates/ui/src/panes/rail.rs @@ -0,0 +1,299 @@ +//! An icon rail: a vertical strip of icons, always shown at one edge, that opens one task pane at +//! a time beside it. +//! +//! The rail stores nothing. The app passes its items and the pane it has open every frame, shows +//! that pane with [`super::PaneHost`] ([`super::SidePolicy::OneSlot`], not movable), and runs the +//! [`RailRequest`] a click returns. + +use std::fmt::Debug; +use std::hash::Hash; + +use egui::{ + Align, EventFilter, Frame, Id, ImageSource, Key, Layout, Margin, Modifiers, Rect, Response, Role, Sense, Stroke, StrokeKind, Ui, WidgetInfo, vec2, +}; + +use super::Edge; +use crate::docking::chrome::ChromeStyle; + +/// Default width of the rail. +pub const RAIL_WIDTH: f32 = 48.0; +/// Default width and height of one item. +pub const RAIL_BUTTON: f32 = 34.0; +/// Room between two items. +const SPACING: f32 = 4.0; +/// Room between an item's edge and its icon. +const ICON_INSET: f32 = 8.0; +/// Radius of the marker dot. +const MARKER_RADIUS: f32 = 3.0; +/// Largest width or item size the rail accepts; larger or non-finite values fall back. +const MAX_SIZE: f32 = 4096.0; + +/// One icon of the rail, standing for the pane it opens. +#[derive(Clone, Debug)] +pub struct RailItem

{ + pub pane: P, + /// Painted tinted with the chrome's text colors, like docking's panel icons. + pub icon: ImageSource<'static>, + /// Tooltip and accessible name. Translate it in the app. + pub label: String, + /// `false`: drawn disabled, not focusable, and a click asks for nothing. + pub enabled: bool, + /// A small dot telling that the pane has content (comments exist, the document has + /// bookmarks). Drawn only while the pane is not open. + pub marker: bool, + /// The item's accessible description while the marker shows ("Has comments"). Translate it + /// in the app. + pub marker_label: Option, +} + +impl

RailItem

{ + /// An enabled item without a marker. + pub fn new(pane: P, icon: ImageSource<'static>, label: impl Into) -> Self { + Self { pane, icon, label: label.into(), enabled: true, marker: false, marker_label: None } + } + + pub fn enabled(mut self, enabled: bool) -> Self { + self.enabled = enabled; + self + } + + pub fn marker(mut self, marker: bool) -> Self { + self.marker = marker; + self + } + + pub fn marker_label(mut self, label: impl Into) -> Self { + self.marker_label = Some(label.into()); + self + } +} + +/// What a click on an item asks the app to do. +#[derive(Clone, Debug, PartialEq)] +#[non_exhaustive] +pub enum RailRequest

{ + /// Open this pane in place of the open one. + Open(P), + /// Close this pane, which is the open one. + Close(P), +} + +/// What [`Rail::show`] returns. +#[derive(Clone, Debug, PartialEq)] +#[non_exhaustive] +pub struct RailOutput

{ + /// The click of this frame, if any. + pub request: Option>, + /// The item holding keyboard focus. + pub focused: Option

, + /// Where the rail was drawn. + pub rect: Rect, +} + +/// Draws the icon rail. Build it every frame. +pub struct Rail { + id: Id, + edge: Edge, + rtl: bool, + width: f32, + button: f32, + frame: Option, +} + +impl Rail { + /// Creates a rail with a stable global ID; use [`Id::unique`] for string or tuple keys. + pub fn new(id: impl Into) -> Self { + Self { id: id.into(), edge: Edge::End, rtl: false, width: RAIL_WIDTH, button: RAIL_BUTTON, frame: None } + } + + /// The edge the rail sits at. Default [`Edge::End`]. The rail is vertical, so + /// [`Edge::Bottom`] draws it at the end edge. + pub fn edge(mut self, edge: Edge) -> Self { + self.edge = edge; + self + } + + /// Right-to-left interface: `Start` is the right edge, and the marker dot sits at the + /// item's top-left corner. + pub fn rtl(mut self, rtl: bool) -> Self { + self.rtl = rtl; + self + } + + /// Width of the rail. Default [`RAIL_WIDTH`]. + pub fn width(mut self, width: f32) -> Self { + self.width = if width.is_finite() { width.clamp(0.0, MAX_SIZE) } else { RAIL_WIDTH }; + self + } + + /// Width and height of one item. Default [`RAIL_BUTTON`]. The icon is 16 points smaller. + pub fn button_size(mut self, size: f32) -> Self { + self.button = if size.is_finite() { size.clamp(0.0, MAX_SIZE) } else { RAIL_BUTTON }; + self + } + + /// Frame of the rail. Default: the panel fill and an 8 pt margin above and below. The panel's + /// separator line draws the inner edge. + pub fn frame(mut self, frame: Frame) -> Self { + self.frame = Some(frame); + self + } + + /// The widget id of `pane`'s item, to give it keyboard focus (`Memory::request_focus`), for + /// example when the app's F6 cycle reaches the rail. + pub fn item_id(&self, pane: &P) -> Id { + self.id.with(("rail-item", pane)) + } + + fn left(&self) -> bool { + matches!((self.edge, self.rtl), (Edge::Start, false) | (Edge::End | Edge::Bottom, true)) + } + + /// Draw the rail as the outermost panel on its edge: call it before [`super::PaneHost::show`] + /// (and after the app's top and bottom bars), so the open pane sits between the document and + /// the rail. `items` are drawn top-down and scroll in a short window; `foot` draws below + /// them, always visible (page navigation, zoom), and may draw nothing. `open` is the pane + /// the app has open. + pub fn show(self, ui: &mut Ui, items: &[RailItem

], open: Option<&P>, foot: impl FnOnce(&mut Ui)) -> RailOutput

{ + let style = ChromeStyle::from_ui(ui); + let panel_id = self.id.with("rail-panel"); + let panel = if self.left() { egui::Panel::left(panel_id) } else { egui::Panel::right(panel_id) }; + let frame = self.frame.unwrap_or_else(|| Frame::NONE.fill(ui.visuals().panel_fill).inner_margin(Margin::symmetric(0, 8))); + let shown = panel.exact_size(self.width).resizable(false).frame(frame).show(ui, |ui| { + // The foot is a panel of its own, so that in a short window the items scroll instead + // of running into it. + egui::Panel::bottom(self.id.with("rail-foot")).frame(Frame::NONE).show_separator_line(false).resizable(false).show(ui, |ui| { + ui.with_layout(Layout::top_down(Align::Center), foot); + }); + egui::ScrollArea::vertical() + .id_salt(self.id.with("rail-items")) + .auto_shrink([false, false]) + .show(ui, |ui| { + ui.with_layout(Layout::top_down(Align::Center), |ui| { + ui.spacing_mut().item_spacing.y = SPACING; + self.items(ui, items, open, &style) + }) + .inner + }) + .inner + }); + let (request, focused) = shown.inner; + RailOutput { request, focused, rect: shown.response.rect } + } + + fn items( + &self, + ui: &mut Ui, + items: &[RailItem

], + open: Option<&P>, + style: &ChromeStyle, + ) -> (Option>, Option

) { + let mut request = None; + let mut focused = None; + let open = open.map(|pane| self.item_id(pane)); + let mut drawn: Vec = Vec::new(); + // Focus order for Up and Down: the enabled items with their place in `drawn`. + let mut enabled: Vec<(usize, Id, &P)> = Vec::new(); + let mut focus_index = None; + // An item disabled while focused, by its place. egui drops a disabled widget's focus as + // it draws it, so this is read before. + let mut lost_focus = None; + for item in items { + let id = self.item_id(&item.pane); + // A pane listed twice would give two widgets one id. + if drawn.contains(&id) { + continue; + } + let place = drawn.len(); + drawn.push(id); + let selected = open == Some(id); + let had_focus = ui.memory(|m| m.has_focus(id)); + let response = ui + .add_enabled_ui(item.enabled, |ui| { + let (rect, _) = ui.allocate_exact_size(vec2(self.button, self.button), Sense::hover()); + let response = ui.interact(rect, id, Sense::click()); + response.widget_info(|| WidgetInfo::selected(Role::Button, ui.is_enabled(), selected, &item.label)); + if item.marker + && !selected + && let Some(label) = &item.marker_label + { + ui.ctx().accesskit_node_builder(id, |node| node.set_description(label.as_str())); + } + self.paint(ui, &response, item, selected, style); + response + }) + .inner; + let response = if item.enabled { response.on_hover_text(&item.label) } else { response.on_disabled_hover_text(&item.label) }; + if !item.enabled { + if had_focus { + lost_focus = Some(place); + } + continue; + } + if response.gained_focus() { + response.scroll_to_me(None); + } + if response.has_focus() { + focus_index = Some(enabled.len()); + focused = Some(item.pane.clone()); + } + enabled.push((place, id, &item.pane)); + if response.clicked() { + request = Some(if selected { RailRequest::Close(item.pane.clone()) } else { RailRequest::Open(item.pane.clone()) }); + } + } + if let Some(place) = lost_focus + && focus_index.is_none() + { + // Focus moves to the nearest enabled item below, else above, else leaves the rail. + let next = enabled.iter().find(|(p, ..)| *p > place).or_else(|| enabled.iter().rev().find(|(p, ..)| *p < place)); + if let Some(&(_, next, pane)) = next { + ui.memory_mut(|m| m.request_focus(next)); + focused = Some(pane.clone()); + } + } + if let Some(index) = focus_index + && let Some(&(_, id, _)) = enabled.get(index) + { + // Up and Down move between the items, never out of the rail by egui's geometric focus. + ui.memory_mut(|m| m.set_focus_lock_filter(id, EventFilter { vertical_arrows: true, ..Default::default() })); + let next = ui.input_mut(|input| { + if input.consume_key(Modifiers::NONE, Key::ArrowDown) { + Some(index.checked_add(1)) + } else if input.consume_key(Modifiers::NONE, Key::ArrowUp) { + Some(index.checked_sub(1)) + } else { + None + } + }); + if let Some(&(_, next, _)) = next.flatten().and_then(|i| enabled.get(i)) { + ui.memory_mut(|m| m.request_focus(next)); + } + } + (request, focused) + } + + fn paint

(&self, ui: &Ui, response: &Response, item: &RailItem

, selected: bool, style: &ChromeStyle) { + let rect = response.rect; + if !rect.is_positive() { + return; + } + let painter = ui.painter(); + let hovered = response.hovered() && ui.is_enabled(); + if selected || hovered { + painter.rect_filled(rect.shrink(1.0), 4.0, style.active_background); + } + if response.has_focus() { + painter.rect_stroke(rect.shrink(1.0), 4.0, Stroke::new(1.0, style.accent), StrokeKind::Inside); + } + let tint = if selected || hovered { style.text } else { style.weak_text }; + let icon = rect.shrink(ICON_INSET); + if icon.is_positive() { + egui::Image::new(item.icon.clone()).tint(tint).paint_at(ui, icon); + } + if item.marker && !selected { + let corner = if self.rtl { rect.left_top() + vec2(6.0, 6.0) } else { rect.right_top() + vec2(-6.0, 6.0) }; + painter.circle_filled(corner, MARKER_RADIUS, style.accent); + } + } +} diff --git a/crates/ui/src/panes/rail_tests.rs b/crates/ui/src/panes/rail_tests.rs new file mode 100644 index 000000000..2fa6ce770 --- /dev/null +++ b/crates/ui/src/panes/rail_tests.rs @@ -0,0 +1,412 @@ +//! Behavior tests for the icon rail, driven by egui pointer and key events. The harness's root ui +//! has an 8 pt margin, so in an 800 pt wide window the rail and panes span x 8..792. + +use egui::accesskit::{Role, Toggled}; +use egui::load::Bytes; +use egui::{Event, ImageSource, Key, Modifiers, PointerButton, Pos2, Rect, Ui, vec2}; +use egui_kittest::{Harness, kittest::NodeT, kittest::Queryable}; + +use super::*; + +const L: f32 = 8.0; +const R: f32 = 792.0; + +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +enum Tool { + Comments, + Bookmarks, + Pages, + Layers, + Files, + Tags, +} + +const TOOLS: [Tool; 6] = [Tool::Comments, Tool::Bookmarks, Tool::Pages, Tool::Layers, Tool::Files, Tool::Tags]; + +/// A tiny valid icon for the rail's images. The tests install the SVG loader below; without a +/// loadable image, egui paints its failed-image placeholder ("⚠️"), which the harness's missing +/// glyph policy rejects. +const ICON_SVG: &[u8] = br##""##; + +fn items() -> Vec> { + TOOLS + .iter() + .map(|t| { + let icon = ImageSource::Bytes { uri: format!("bytes://rail-test/{t:?}.svg").into(), bytes: Bytes::Static(ICON_SVG) }; + RailItem::new(*t, icon, format!("{t:?}")) + }) + .collect() +} + +/// The one-slot task pane the rail opens, at the rail's edge. +struct Content { + edge: Edge, +} + +impl PaneContent for Content { + fn title(&self, pane: &Tool) -> String { + format!("{pane:?} pane") + } + fn body(&mut self, ui: &mut Ui, pane: &Tool) { + ui.label(format!("{pane:?} body")); + } + fn default_state(&self, _: &Tool) -> PaneState { + PaneState::docked(self.edge, 250.0) + } + fn movable(&self, _: &Tool) -> bool { + false + } +} + +/// The app side: which pane is open, and every request the rail sent. +struct App { + items: Vec>, + open: Option, + log: Vec>, + edge: Edge, + rtl: bool, + /// Show the open pane with a `PaneHost` after the rail. + pane: bool, + foot: bool, + out: Option>, + /// `PaneOutput::focused` of the pane host. + pane_focused: Option, + document: Rect, +} + +impl App { + fn new() -> Self { + Self { + items: items(), + open: None, + log: Vec::new(), + edge: Edge::End, + rtl: false, + pane: false, + foot: false, + out: None, + pane_focused: None, + document: Rect::NOTHING, + } + } + + fn frame(&mut self, ui: &mut Ui) { + let foot = self.foot; + let out = Rail::new(Id::unique("rail")).edge(self.edge).rtl(self.rtl).show(ui, &self.items, self.open.as_ref(), |ui| { + if foot { + let _ = ui.button("Foot"); + } + }); + if self.pane { + let open: Vec = self.open.into_iter().collect(); + let panes = PaneHost::new(Id::unique("panes")).rtl(self.rtl).policy(SidePolicy::OneSlot).show( + ui, + &open, + |_| None, + &mut Content { edge: self.edge }, + ); + self.pane_focused = panes.focused; + } + self.document = egui::CentralPanel::default().show(ui, |ui| ui.label("Document")).response.rect; + match &out.request { + Some(RailRequest::Open(p)) => self.open = Some(*p), + Some(RailRequest::Close(_)) => self.open = None, + None => {} + } + self.log.extend(out.request.clone()); + self.out = Some(out); + } + + fn out(&self) -> &RailOutput { + self.out.as_ref().unwrap() + } +} + +fn harness(app: App, height: f32) -> Harness<'static, App> { + let mut h = Harness::builder() + .with_context_setup(egui_extras::install_image_loaders) + .with_size(vec2(800.0, height)) + .build_ui_state(|ui, app: &mut App| app.frame(ui), app); + h.input_mut().focused = true; + h.run_steps(2); + h +} + +fn item(h: &Harness<'_, App>, tool: Tool) -> Rect { + h.ctx.read_response(Rail::new(Id::unique("rail")).item_id(&tool)).unwrap().rect +} + +fn click(h: &mut Harness<'_, App>, at: Pos2) { + let button = |pressed| Event::PointerButton { pos: at, button: PointerButton::Primary, pressed, modifiers: Modifiers::NONE }; + h.input_mut().events.extend([Event::PointerMoved(at), button(true)]); + h.run_steps(1); + h.input_mut().events.push(button(false)); + h.run_steps(2); +} + +fn click_item(h: &mut Harness<'_, App>, tool: Tool) { + let at = item(h, tool).center(); + click(h, at); +} + +fn key(h: &mut Harness<'_, App>, key: Key) { + h.input_mut().events.push(Event::Key { key, physical_key: None, pressed: true, repeat: false, modifiers: Modifiers::NONE }); + h.run_steps(1); + h.input_mut().events.push(Event::Key { key, physical_key: None, pressed: false, repeat: false, modifiers: Modifiers::NONE }); + h.run_steps(2); +} + +fn focused(h: &Harness<'_, App>) -> Option { + h.state().out().focused +} + +#[test] +fn a_click_opens_the_open_item_closes_and_another_swaps() { + let mut h = harness(App::new(), 600.0); + click_item(&mut h, Tool::Comments); + assert_eq!(h.state().log, [RailRequest::Open(Tool::Comments)]); + assert_eq!(h.state().open, Some(Tool::Comments)); + click_item(&mut h, Tool::Comments); + assert_eq!(h.state().log[1..], [RailRequest::Close(Tool::Comments)]); + assert_eq!(h.state().open, None); + click_item(&mut h, Tool::Comments); + click_item(&mut h, Tool::Pages); + assert_eq!(h.state().log[2..], [RailRequest::Open(Tool::Comments), RailRequest::Open(Tool::Pages)]); + assert_eq!(h.state().open, Some(Tool::Pages)); +} + +#[test] +fn a_disabled_item_sends_nothing_and_the_marker_shows_only_while_closed() { + let mut app = App::new(); + app.items[1].enabled = false; + app.items[0].marker = true; + app.items[0].marker_label = Some("Has comments".into()); + app.items[3] = app.items[3].clone().marker(true).marker_label("Has layers"); + app.open = Some(Tool::Comments); + let mut h = harness(app, 600.0); + click_item(&mut h, Tool::Bookmarks); + assert!(h.state().log.is_empty()); + assert_eq!(h.state().open, Some(Tool::Comments)); + + let markers = |h: &Harness<'_, App>| -> Vec { + h.output() + .shapes + .iter() + .filter_map(|s| match &s.shape { + egui::Shape::Circle(circle) if circle.radius == 3.0 => Some(circle.center), + _ => None, + }) + .collect() + }; + let shown = markers(&h); + assert_eq!(shown.len(), 1, "{shown:?}"); + assert!(item(&h, Tool::Layers).contains(shown[0])); + assert!(shown[0].x > item(&h, Tool::Layers).center().x, "the marker sits at the top-right corner"); + let description = |h: &Harness<'_, App>, label: &str| h.get_by_label(label).accesskit_node().description(); + assert_eq!(description(&h, "Layers").as_deref(), Some("Has layers")); + // egui 0.37 uses tooltip text as the default accessible description. + assert_eq!(description(&h, "Comments").as_deref(), Some("Comments"), "no marker description while the pane is open"); + + // Closed, Comments shows its marker too. + click_item(&mut h, Tool::Comments); + assert_eq!(h.state().open, None); + let shown = markers(&h); + assert_eq!(shown.len(), 2, "{shown:?}"); + assert!(shown.iter().any(|p| item(&h, Tool::Comments).contains(*p))); + h.run_steps(1); + // Hovering Comments shows a tooltip with its label; move away before reading by label. + h.input_mut().events.push(Event::PointerMoved(Pos2::new(400.0, 300.0))); + h.run_steps(2); + assert_eq!(description(&h, "Comments").as_deref(), Some("Has comments")); + + let mut app = App::new(); + app.rtl = true; + app.items[3].marker = true; + let h = harness(app, 600.0); + let shown = markers(&h); + assert_eq!(shown.len(), 1, "{shown:?}"); + assert!(shown[0].x < item(&h, Tool::Layers).center().x, "right-to-left: the marker sits at the top-left corner"); +} + +#[test] +fn the_rail_is_outermost_and_the_open_pane_sits_between_it_and_the_document() { + for (edge, rtl, left) in [(Edge::End, false, false), (Edge::End, true, true), (Edge::Start, false, true), (Edge::Start, true, false)] { + let mut app = App::new(); + app.edge = edge; + app.rtl = rtl; + app.pane = true; + app.open = Some(Tool::Comments); + let h = harness(app, 600.0); + let rail = h.state().out().rect; + let pane_id = Id::unique("panes").with(("pane", &Tool::Comments)); + // The resize handle lies on the pane's inner edge; the pane is 250 wide. + let inner = h.ctx.read_response(pane_id.with("resize")).unwrap().rect.center().x; + let pane = if left { + Rect::from_x_y_ranges(inner - 250.0..=inner, rail.y_range()) + } else { + Rect::from_x_y_ranges(inner..=inner + 250.0, rail.y_range()) + }; + let title = h.ctx.read_response(pane_id.with("title")).unwrap().rect; + let document = h.state().document; + let case = format!("{edge:?} rtl {rtl}: rail {rail:?}, pane {pane:?}, title {title:?}, document {document:?}"); + assert_eq!(rail.width(), RAIL_WIDTH, "{case}"); + assert!(pane.x_range().contains(title.left()) && pane.x_range().contains(title.right()), "{case}"); + if left { + assert_eq!(rail.left(), L, "{case}"); + assert_eq!(pane.left(), rail.right(), "{case}"); + assert!(document.left() >= pane.right(), "{case}"); + } else { + assert_eq!(rail.right(), R, "{case}"); + assert_eq!(pane.right(), rail.left(), "{case}"); + assert!(document.right() <= pane.left(), "{case}"); + } + assert!(rail.x_range().contains(item(&h, Tool::Comments).center().x), "{case}"); + } +} + +#[test] +fn in_a_short_window_the_items_scroll_and_the_foot_stays_visible() { + let mut app = App::new(); + app.foot = true; + let mut h = harness(app, 200.0); + let foot = h.get_by_label("Foot").rect(); + assert!(foot.bottom() <= 192.0, "{foot:?}"); + assert!(item(&h, Tool::Comments).top() < 30.0); + assert!(item(&h, Tool::Tags).bottom() > foot.top(), "the last item starts out of view"); + + let over = item(&h, Tool::Comments).center(); + h.input_mut().events.extend([ + Event::PointerMoved(over), + Event::MouseWheel { + source: egui::MouseWheelSource::Unknown, + unit: egui::MouseWheelUnit::Point, + delta: vec2(0.0, -500.0), + phase: egui::TouchPhase::Move, + modifiers: Modifiers::NONE, + }, + ]); + h.run_steps(3); + let tags = item(&h, Tool::Tags); + assert!(tags.bottom() <= h.get_by_label("Foot").rect().top(), "{tags:?} above the foot after scrolling"); + assert_eq!(h.get_by_label("Foot").rect(), foot); +} + +#[test] +fn items_are_named_toggle_buttons_and_the_keyboard_moves_between_and_activates_them() { + let mut app = App::new(); + app.items[1].enabled = false; + app.open = Some(Tool::Comments); + let mut h = harness(app, 600.0); + let comments = h.get_by_label("Comments").accesskit_node(); + assert_eq!(comments.role(), Role::Button); + assert_eq!(comments.toggled(), Some(Toggled::True)); + assert!(!comments.is_disabled()); + let pages = h.get_by_label("Pages").accesskit_node(); + assert_eq!(pages.role(), Role::Button); + assert_eq!(pages.toggled(), Some(Toggled::False)); + assert!(h.get_by_label("Bookmarks").accesskit_node().is_disabled()); + + h.ctx.memory_mut(|m| m.request_focus(Rail::new(Id::unique("rail")).item_id(&Tool::Comments))); + h.run_steps(2); + assert_eq!(focused(&h), Some(Tool::Comments)); + key(&mut h, Key::ArrowUp); + assert_eq!(focused(&h), Some(Tool::Comments), "Up on the first item stays"); + key(&mut h, Key::ArrowDown); + assert_eq!(focused(&h), Some(Tool::Pages), "Down skips the disabled item"); + key(&mut h, Key::Enter); + assert_eq!(h.state().log, [RailRequest::Open(Tool::Pages)]); + key(&mut h, Key::Space); + assert_eq!(h.state().log[1..], [RailRequest::Close(Tool::Pages)]); + key(&mut h, Key::ArrowUp); + assert_eq!(focused(&h), Some(Tool::Comments)); + for _ in 0..TOOLS.len() { + key(&mut h, Key::ArrowDown); + } + assert_eq!(focused(&h), Some(Tool::Tags), "Down on the last item stays"); +} + +#[test] +fn an_empty_rail_draws_and_bad_sizes_fall_back() { + let mut app = App::new(); + app.items.clear(); + let h = harness(app, 600.0); + let rail = h.state().out().rect; + assert_eq!((rail.right(), rail.width()), (R, RAIL_WIDTH)); + assert_eq!(h.state().out().request, None); + + let mut h = Harness::builder().with_context_setup(egui_extras::install_image_loaders).with_size(vec2(400.0, 300.0)).build_ui(|ui| { + let out = Rail::new(Id::unique("bad")).width(f32::NAN).button_size(f32::INFINITY).show(ui, &items(), None, |_| {}); + assert_eq!(out.rect.width(), RAIL_WIDTH); + }); + h.run_steps(2); +} + +#[test] +fn a_disabled_item_takes_no_tab_focus_and_a_focused_item_disabled_hands_focus_on() { + let mut app = App::new(); + app.items[1].enabled = false; + let mut h = harness(app, 600.0); + let focused_id = |h: &Harness<'_, App>| h.ctx.memory(|m| m.focused()); + let id = |tool: Tool| Rail::new(Id::unique("rail")).item_id(&tool); + h.ctx.memory_mut(|m| m.request_focus(id(Tool::Comments))); + h.run_steps(2); + key(&mut h, Key::Tab); + assert_eq!(focused_id(&h), Some(id(Tool::Pages)), "Tab skips the disabled item"); + + // Pages is disabled while focused: focus goes to the next enabled item. + h.state_mut().items[2].enabled = false; + h.run_steps(2); + assert_eq!(focused_id(&h), Some(id(Tool::Layers))); + assert_eq!(focused(&h), Some(Tool::Layers)); + + // The last item disabled while focused: focus goes to the enabled one above. + h.ctx.memory_mut(|m| m.request_focus(id(Tool::Tags))); + h.run_steps(2); + h.state_mut().items[5].enabled = false; + h.run_steps(2); + assert_eq!(focused_id(&h), Some(id(Tool::Files))); + + // Nothing left enabled: focus leaves the rail. + for item in &mut h.state_mut().items { + item.enabled = false; + } + h.run_steps(2); + assert_eq!((focused_id(&h), focused(&h)), (None, None)); +} + +#[test] +fn a_press_on_the_rail_clears_the_pane_hosts_focused_pane() { + let mut app = App::new(); + app.pane = true; + app.open = Some(Tool::Comments); + let mut h = harness(app, 600.0); + let body = h.get_by_label("Comments body").rect().center(); + click(&mut h, body); + assert_eq!(h.state().pane_focused, Some(Tool::Comments)); + // Below the items: the rail, not a pane. + let rail = h.state().out().rect; + click(&mut h, Pos2::new(rail.center().x, rail.bottom() - 40.0)); + assert_eq!(h.state().pane_focused, None); + assert!(h.state().log.is_empty()); + assert_eq!(h.state().open, Some(Tool::Comments)); +} + +#[test] +fn a_bottom_edge_draws_the_rail_at_the_end() { + let mut app = App::new(); + app.edge = Edge::Bottom; + let h = harness(app, 600.0); + let rail = h.state().out().rect; + assert_eq!((rail.right(), rail.height()), (R, 584.0), "{rail:?}"); +} + +#[test] +fn chrome_style_from_visuals_matches_the_dock_style_it_skips() { + use crate::docking::{DockStyle, chrome::ChromeStyle}; + for visuals in [egui::Visuals::light(), egui::Visuals::dark()] { + let direct = ChromeStyle::from_visuals(&visuals); + let through_dock = ChromeStyle::from_dock(&DockStyle::from_visuals(&visuals)); + assert_eq!(format!("{direct:?}"), format!("{through_dock:?}")); + } +} diff --git a/crates/ui/src/panes/tests.rs b/crates/ui/src/panes/tests.rs index 2be008ac8..f39e207d4 100644 --- a/crates/ui/src/panes/tests.rs +++ b/crates/ui/src/panes/tests.rs @@ -4,7 +4,7 @@ use std::collections::HashMap; use egui::{Event, Id, Key, Modifiers, PointerButton, Pos2, Rect, Ui, pos2, vec2}; -use egui_kittest::{Harness, kittest::Queryable}; +use egui_kittest::{Harness, kittest::NodeT, kittest::Queryable}; use super::*; @@ -43,6 +43,30 @@ impl PaneContent for Content { } } +/// [`Content`] with the title bar on or off. +struct Titled(bool); + +impl PaneContent for Titled { + fn title(&self, pane: &Pane) -> String { + Content.title(pane) + } + fn body(&mut self, ui: &mut Ui, pane: &Pane) { + Content.body(ui, pane); + } + fn default_state(&self, pane: &Pane) -> PaneState { + Content.default_state(pane) + } + fn movable(&self, pane: &Pane) -> bool { + Content.movable(pane) + } + fn closable(&self, pane: &Pane) -> bool { + Content.closable(pane) + } + fn title_bar(&self, _: &Pane) -> bool { + self.0 + } +} + /// The app side: open list, persisted states and every request it received. struct App { open: Vec, @@ -52,13 +76,28 @@ struct App { to_document: usize, rtl: bool, policy: SidePolicy, + title_bar: bool, + /// The document is one drag-sensing widget, like a canvas. + drag_document: bool, + document_drags: usize, } impl App { /// Create an app with `open` panes, empty state and log, no focused pane, no /// document-focus requests, left-to-right layout and column side policy. fn new(open: &[Pane]) -> Self { - Self { open: open.to_vec(), states: HashMap::new(), log: Vec::new(), focused: None, to_document: 0, rtl: false, policy: SidePolicy::Columns } + Self { + open: open.to_vec(), + states: HashMap::new(), + log: Vec::new(), + focused: None, + to_document: 0, + rtl: false, + policy: SidePolicy::Columns, + title_bar: true, + drag_document: false, + document_drags: 0, + } } /// Render `PaneHost` with the app's open panes, policy and RTL flag, then a central document @@ -66,8 +105,25 @@ impl App { /// document-focus transitions. fn frame(&mut self, ui: &mut Ui) { let states = self.states.clone(); - let out = PaneHost::new(Id::unique("panes")).rtl(self.rtl).policy(self.policy).show(ui, &self.open, |p| states.get(p).copied(), &mut Content); - egui::CentralPanel::default().show(ui, |ui| ui.label("Document")); + let out = PaneHost::new(Id::unique("panes")).rtl(self.rtl).policy(self.policy).show( + ui, + &self.open, + |p| states.get(p).copied(), + &mut Titled(self.title_bar), + ); + let drag_document = self.drag_document; + // A canvas fills the panel, without the default frame's margin. + let panel = if drag_document { egui::CentralPanel::default().frame(Frame::NONE) } else { egui::CentralPanel::default() }; + let dragged = panel + .show(ui, |ui| { + if drag_document { + ui.interact(ui.max_rect(), Id::unique("document-drag"), Sense::click_and_drag()).dragged() + } else { + ui.label("Document").dragged() + } + }) + .inner; + self.document_drags += usize::from(dragged); for request in &out.requests { match request { PaneRequest::Close(p) => self.open.retain(|o| o != p), @@ -143,6 +199,26 @@ fn drag(h: &mut Harness<'_, App>, from: Pos2, to: Pos2) { h.run_steps(2); } +/// The rect a docked pane's panel takes. egui 0.37 keys a panel's persisted `PanelState` by the +/// scope of the `Ui` it is shown in and the panel's id salt, not by the id alone. +fn panel(h: &Harness<'_, App>, pane: Pane) -> Rect { + egui::containers::panel::PanelState::load(&h.ctx, h.ui_id().with(pane_id(pane).with("panel"))).unwrap().outer_rect +} + +/// The drop previews painted in the last frame: 2 pt accent outlines with a 15% accent fill. +fn previews(h: &Harness<'_, App>) -> Vec { + h.output() + .shapes + .iter() + .filter_map(|clipped| match &clipped.shape { + egui::Shape::Rect(shape) if shape.stroke.width == 2.0 && shape.fill == shape.stroke.color.gamma_multiply(0.15) => { + Some(shape.rect.expand(1.0)) + } + _ => None, + }) + .collect() +} + fn key(h: &mut Harness<'_, App>, key: Key, modifiers: Modifiers) { send(h, vec![Event::Key { key, physical_key: None, pressed: true, repeat: false, modifiers }]); h.run_steps(1); @@ -160,6 +236,79 @@ fn last_resize(app: &App, pane: Pane) -> Option { }) } +#[test] +fn a_docked_pane_without_a_title_bar_fills_the_pane_and_still_resizes() { + for title_bar in [true, false] { + let mut app = App::new(&[Pane::Nav]); + app.title_bar = title_bar; + let mut h = harness(app); + let chrome = [ + rect(&h, pane_id(Pane::Nav).with("title")).is_some(), + h.query_by_label("Move Nav pane").is_some(), + h.query_by_label("Pane options").is_some(), + h.query_by_label("Close").is_some(), + ]; + assert_eq!(chrome, [title_bar; 4], "title bar {title_bar}: title, its node, ▾ and ×"); + // The body's first line sits under the body margin, below the title bar when there is one. + let body = h.get_by_label("Nav body").rect(); + let top = if title_bar { L + TITLE_BAR_HEIGHT + 8.0 } else { L + 8.0 }; + assert!((body.top() - top).abs() < 2.0, "title bar {title_bar}: body {body:?}"); + + // Without the bar, the body is the pane's accessible region, named by its title. + let regions = h.query_all_by_label("Nav").filter(|n| n.accesskit_node().role() == egui::accesskit::Role::Pane).count(); + assert_eq!(regions, usize::from(!title_bar), "title bar {title_bar}"); + assert_eq!(h.get_by_label("Resize pane").accesskit_node().role(), egui::accesskit::Role::Splitter); + + drag(&mut h, pos2(L + 250.0, 300.0), pos2(L + 300.0, 300.0)); + assert_eq!(last_resize(h.state(), Pane::Nav), Some(300.0), "title bar {title_bar}"); + assert_eq!(edge(&h, Pane::Nav), L + 300.0); + } +} + +#[test] +fn f6_reaches_a_pane_without_a_title_bar() { + let mut app = App::new(&[Pane::Nav, Pane::Comments]); + app.title_bar = false; + let mut h = harness(app); + key(&mut h, Key::F6, Modifiers::NONE); + assert_eq!(h.state().focused, Some(Pane::Nav)); + assert_eq!(h.ctx.memory(|m| m.focused()), Some(pane_id(Pane::Nav).with("body")), "F6 focuses the body"); + key(&mut h, Key::F6, Modifiers::NONE); + assert_eq!(h.state().focused, Some(Pane::Comments)); + key(&mut h, Key::F6, Modifiers::NONE); + assert_eq!((h.state().focused, h.state().to_document), (None, 1)); +} + +#[test] +fn the_resize_edge_works_on_both_sides_of_the_line_over_a_dragging_document() { + for offset in [-3.5, 3.5] { + let mut app = App::new(&[Pane::Nav]); + app.drag_document = true; + let mut h = harness(app); + // The line is at L + 250; positive offsets lie over the document. + drag(&mut h, pos2(L + 250.0 + offset, 300.0), pos2(L + 300.0 + offset, 300.0)); + // The edge follows the pointer. + let size = last_resize(h.state(), Pane::Nav); + assert!(size.is_some_and(|size| (size - (300.0 + offset)).abs() <= 1.0), "offset {offset}: {size:?}"); + assert_eq!(h.state().document_drags, 0, "offset {offset}: the document took the drag"); + } + // A floating pane over the edge keeps its presses: the edge stays under floating windows. + let mut app = App::new(&[Pane::Nav, Pane::Styles]); + app.states.insert(Pane::Styles, PaneState { place: Place::Floating, size: None, float_rect: Some([L + 150.0, 100.0, 220.0, 300.0]) }); + let mut h = harness(app); + h.run_steps(2); + drag(&mut h, pos2(L + 250.0, 300.0), pos2(L + 300.0, 300.0)); + assert_eq!(last_resize(h.state(), Pane::Nav), None, "the edge under a floating pane took the drag"); + + // Away from the edge, the document still drags. + let mut app = App::new(&[Pane::Nav]); + app.drag_document = true; + let mut h = harness(app); + drag(&mut h, pos2(L + 400.0, 300.0), pos2(L + 450.0, 300.0)); + assert!(h.state().document_drags > 0); + assert_eq!(last_resize(h.state(), Pane::Nav), None); +} + #[test] fn columns_sit_side_by_side_resize_and_move_to_end() { let mut h = harness(App::new(&[Pane::Nav, Pane::Styles])); @@ -230,6 +379,86 @@ fn title_dragged_out_floats_and_a_drop_near_an_edge_docks() { assert_eq!(h.state().states[&Pane::Styles].float_rect.map(|r| [r[2], r[3]]), Some([270.0, 480.0])); } +#[test] +fn a_floating_pane_near_a_side_edge_previews_the_column_it_docks_in() { + let mut app = App::new(&[Pane::Nav, Pane::Styles]); + app.states.insert(Pane::Styles, PaneState { place: Place::Floating, size: Some(300.0), float_rect: Some([300.0, 100.0, 270.0, 400.0]) }); + let mut h = harness(app); + let from = title(&h, Pane::Styles).center(); + hold(&mut h, from, pos2(400.0, 300.0)); + assert!(previews(&h).is_empty(), "away from the edges"); + let to = pos2(L + 10.0, 300.0); + send(&mut h, vec![Event::PointerMoved(to)]); + let landing = Rect::from_x_y_ranges(L + 250.0..=L + 550.0, 8.0..=592.0); + assert_eq!(previews(&h), vec![landing], "beside Nav, 300 pt wide"); + send(&mut h, vec![button(to, false)]); + h.run_steps(2); + assert_eq!(h.state().place(Pane::Styles), Place::Docked(Edge::Start)); + assert_eq!(panel(&h, Pane::Styles), landing); +} + +#[test] +fn a_docked_pane_dragged_across_previews_the_column_it_lands_in() { + let mut h = harness(App::new(&[Pane::Nav, Pane::Comments])); + let from = title(&h, Pane::Comments).center(); + let to = pos2(L + 10.0, 300.0); + hold(&mut h, from, to); + let landing = Rect::from_x_y_ranges(L + 250.0..=L + 500.0, 8.0..=592.0); + assert_eq!(previews(&h), vec![landing], "right of Nav, which comes first in open"); + send(&mut h, vec![button(to, false)]); + h.run_steps(2); + assert_eq!(h.state().place(Pane::Comments), Place::Docked(Edge::Start)); + assert_eq!(panel(&h, Pane::Comments), landing); + + // Right to left: Start is the right edge, and Nav stays outermost there. + let mut app = App::new(&[Pane::Nav, Pane::Comments]); + app.rtl = true; + let mut h = harness(app); + let from = title(&h, Pane::Comments).center(); + let to = pos2(R - 10.0, 300.0); + hold(&mut h, from, to); + let landing = Rect::from_x_y_ranges(R - 500.0..=R - 250.0, 8.0..=592.0); + assert_eq!(previews(&h), vec![landing]); + send(&mut h, vec![button(to, false)]); + h.run_steps(2); + assert_eq!(panel(&h, Pane::Comments), landing); +} + +#[test] +fn one_slot_previews_a_pane_beside_the_context_pane() { + let mut app = App::new(&[Pane::Pivot, Pane::Styles]); + app.policy = SidePolicy::OneSlot; + let mut h = harness(app); + let from = title(&h, Pane::Styles).center(); + let to = pos2(R - 10.0, 300.0); + hold(&mut h, from, to); + let landing = Rect::from_x_y_ranges(R - 500.0..=R - 250.0, 8.0..=592.0); + assert_eq!(previews(&h), vec![landing], "inside the Pivot pane, which comes first in open"); + send(&mut h, vec![button(to, false)]); + h.run_steps(2); + assert_eq!(panel(&h, Pane::Styles), landing); +} + +#[test] +fn one_slot_previews_the_room_a_pane_it_reveals_leaves() { + // Nav and Styles share the start slot, so only Styles shows. Moved to the end, it reveals + // Nav, which takes 500 of the host's 784 pt. + let mut app = App::new(&[Pane::Nav, Pane::Styles]); + app.policy = SidePolicy::OneSlot; + for pane in [Pane::Nav, Pane::Styles] { + app.states.insert(pane, PaneState::docked(Edge::Start, 500.0)); + } + let mut h = harness(app); + let from = title(&h, Pane::Styles).center(); + let to = pos2(R - 10.0, 300.0); + hold(&mut h, from, to); + let landing = Rect::from_x_y_ranges(L + 500.0..=R, 8.0..=592.0); + assert_eq!(previews(&h), vec![landing]); + send(&mut h, vec![button(to, false)]); + h.run_steps(2); + assert_eq!(panel(&h, Pane::Styles), landing); +} + #[test] fn escape_and_focus_loss_restore_a_floating_pane_during_a_move() { let mut app = App::new(&[Pane::Styles]); diff --git a/crates/ui/tests/docking_workflows.rs b/crates/ui/tests/docking_workflows.rs index 26f50d895..8303a7eda 100644 --- a/crates/ui/tests/docking_workflows.rs +++ b/crates/ui/tests/docking_workflows.rs @@ -425,7 +425,8 @@ mod v1_fixtures { let float: DockCommand = serde_json::from_value(json!({"command": "window.panel.float", "panel": bars[0], "column": true})).unwrap(); let space = craft_ui::docking::float_space(&d.ctx, d.id).unwrap(); - let actions = float.to_actions(&craft_ui::docking::Workspace::new(d.layout.clone()), false, &space).unwrap(); + let actions = + float.to_actions_with(&craft_ui::docking::Workspace::new(d.layout.clone()), false, &space, |p| d.content.permissions(p)).unwrap(); d.layout.apply_all(actions, |p| d.content.permissions(p)).unwrap_or_else(|e| panic!("{at}: {e}")); d.settle(); assert!(start_columns(&d.layout).is_empty(), "{at}: floated"); diff --git a/crates/update/Cargo.toml b/crates/update/Cargo.toml new file mode 100644 index 000000000..353e8f333 --- /dev/null +++ b/crates/update/Cargo.toml @@ -0,0 +1,46 @@ +[package] +name = "craft-update" +description = "Release checking, downloading, installing and self-updating for the Craft App Suite and ArtCraft Launcher" +version.workspace = true +edition.workspace = true +license.workspace = true +rust-version.workspace = true +repository.workspace = true + +[lints] +workspace = true + +[features] +default = [] +egui = ["dep:egui"] +local-mirror = [] + +[dependencies] +egui = { workspace = true, optional = true } +serde = { workspace = true } +serde_json = { workspace = true } +log = "0.4" +dirs = "6" +getrandom = "0.3" +tempfile = "3" +sha2 = "0.10" +ureq = { version = "3.4", default-features = false, features = ["rustls-no-provider", "_ring", "gzip", "platform-verifier"] } +chrono = { workspace = true } +semver = { version = "1", features = ["serde"] } + +[dev-dependencies] +flate2 = "1" + +[target.'cfg(unix)'.dependencies] +flate2 = "1" +tar = { version = "0.4", default-features = false } + +[target.'cfg(target_os = "macos")'.dependencies] +plist = "1" + +[target.'cfg(target_os = "windows")'.dependencies] +winreg = "0.55" +dunce = "1" +zip = { version = "8", default-features = false, features = ["deflate"] } +winsafe = { version = "0.0.29", features = ["kernel", "version"] } +verifysign = "1" diff --git a/crates/update/src/app.rs b/crates/update/src/app.rs new file mode 100644 index 000000000..c5e0ce25c --- /dev/null +++ b/crates/update/src/app.rs @@ -0,0 +1,75 @@ +//! Which app an installer or an update works on. + +/// A Craft App, as its releases and installers know it. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct App { + /// Machine name: repo, binary, package and asset stem (`photocraft`). + pub id: &'static str, + /// Display name (`PhotoCraft`). + pub name: &'static str, + /// What it's for, in a few words (English). Written into the menu entries installers add. + pub tagline: &'static str, + /// GitHub `owner/repo`. Releases are only ever downloaded from this repository. + pub repo: &'static str, + /// Older names its release assets may carry (PdfCraft shipped as PrintCraft until 0.2.1). + pub former_ids: &'static [&'static str], + /// Older display names its installers registered (PdfCraft's MSI said PrintCraft until 0.2.1). + pub former_names: &'static [&'static str], + /// Its 128 px PNG icon, for the menu entry written beside an AppImage. `None`: no icon. + pub icon_png: Option<&'static [u8]>, +} + +impl App { + /// An app with no former names and no icon. + pub const fn new(id: &'static str, name: &'static str, tagline: &'static str, repo: &'static str) -> Self { + Self { id, name, tagline, repo, former_ids: &[], former_names: &[], icon_png: None } + } + + /// `ai.storyteller.`: macOS bundle id, Flatpak id and Linux desktop-file id. + pub fn bundle_id(&self) -> String { + format!("ai.storyteller.{}", self.id) + } + + pub fn releases_url(&self) -> String { + format!("https://github.com/{}/releases", self.repo) + } + + pub fn repo_url(&self) -> String { + format!("https://github.com/{}", self.repo) + } + + /// The app's page on getartcraft.com. + pub fn website(&self) -> String { + format!("https://getartcraft.com/apps/{}", self.id) + } + + /// Asset stems this app's releases may use, current first. + pub fn stems(&self) -> impl Iterator + '_ { + std::iter::once(self.id).chain(self.former_ids.iter().copied()) + } + + /// Display names this app's installers may have registered, current first. + pub fn names(&self) -> impl Iterator + '_ { + std::iter::once(self.name).chain(self.former_names.iter().copied()) + } + + /// Looks up an app in the Craft App Suite catalog by its id. + pub fn by_id(id: &str) -> Option<&'static App> { + crate::suite::by_id(id) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn former_names_follow_the_current_one() { + let pdf = + App { former_ids: &["printcraft"], former_names: &["PrintCraft"], ..App::new("pdfcraft", "PdfCraft", "PDFs", "storytold/pdfcraft") }; + assert_eq!(pdf.stems().collect::>(), ["pdfcraft", "printcraft"]); + assert_eq!(pdf.names().collect::>(), ["PdfCraft", "PrintCraft"]); + assert_eq!(pdf.bundle_id(), "ai.storyteller.pdfcraft"); + assert_eq!(pdf.releases_url(), "https://github.com/storytold/pdfcraft/releases"); + } +} diff --git a/crates/update/src/desktop_entry.rs b/crates/update/src/desktop_entry.rs new file mode 100644 index 000000000..d6639bef9 --- /dev/null +++ b/crates/update/src/desktop_entry.rs @@ -0,0 +1,186 @@ +//! Menu entries (freedesktop `.desktop` files) for AppImages the launcher installs. +//! +//! An AppImage installs nothing by itself, so without an entry the app is missing from the +//! desktop's menu and, on Wayland, the dock can't match the window's app id to an icon. The +//! launcher writes `/applications/ai.storyteller..desktop` (the id each app's window +//! reports) and marks it as its own, so it only ever replaces or removes entries it wrote. +//! +//! The text is built by pure functions, tested on every platform; only Linux writes the files. + +#![cfg_attr(not(target_os = "linux"), allow(dead_code))] + +use std::path::Path; + +use crate::App; + +/// The key that marks an entry as written by the launcher. +pub const MARKER: &str = "X-ArtCraft-Launcher=true"; + +/// The menu categories and the window class each app's own `.desktop` file declares. +pub fn meta(app: &App) -> (&'static str, Option<&'static str>) { + match app.id { + "photocraft" => ("Graphics;2DGraphics;RasterGraphics;Photography;", Some("photocraft")), + "pdfcraft" => ("Office;Viewer;", Some("pdfcraft")), + "lightcraft" => ("Graphics;2DGraphics;RasterGraphics;Photography;", None), + "filmcraft" => ("AudioVideo;Video;AudioVideoEditing;", None), + "designcraft" => ("Graphics;Publishing;", None), + "vectorcraft" => ("Graphics;2DGraphics;VectorGraphics;", None), + "effectcraft" => ("AudioVideo;Video;Graphics;2DGraphics;", None), + "wordcraft" => ("Office;WordProcessor;", None), + "deckcraft" => ("Office;Presentation;", None), + "gridcraft" => ("Office;Spreadsheet;", None), + "cadcraft" => ("Graphics;Engineering;VectorGraphics;", None), + "soundcraft" => ("AudioVideo;Audio;Midi;Mixer;Sequencer;Recorder;", None), + "figcraft" => ("Graphics;2DGraphics;VectorGraphics;X-Design;", Some("figcraft")), + _ => ("Graphics;", None), + } +} + +/// `path` as one quoted argument of an `Exec=` key, per the Desktop Entry spec: inside double +/// quotes `"`, `` ` ``, `$` and `\` take a backslash, then the string-value rule doubles every +/// backslash, and `%` (field codes) is written `%%`. `None` for paths that can't be written +/// safely (not UTF-8, holding a control character such as a line break, or holding `=`, which the +/// spec forbids in the executable name or path even when quoted). +pub fn exec_quote(path: &Path) -> Option { + let s = path.to_str()?; + if s.chars().any(|c| c.is_control() || c == '=') { + return None; + } + let mut out = String::with_capacity(s.len() + 2); + out.push('"'); + for c in s.chars() { + match c { + '"' | '`' | '$' => { + out.push_str("\\\\"); + out.push(c); + } + '\\' => out.push_str("\\\\\\\\"), + '%' => out.push_str("%%"), + c => out.push(c), + } + } + out.push('"'); + Some(out) +} + +/// A localestring/string value: backslashes and line breaks escaped. +fn value(s: &str) -> String { + s.replace('\\', "\\\\").replace('\n', "\\n").replace('\r', "\\r").replace('\t', "\\t") +} + +/// The entry for `app` installed as the AppImage `appimage`; `icon` when its icon is installed +/// under the app's id. `None` when the path can't be put in an `Exec=` line. +pub fn render(app: &App, appimage: &Path, icon: bool) -> Option { + let exec = exec_quote(appimage)?; + let (categories, wm_class) = meta(app); + let mut text = String::from("[Desktop Entry]\nType=Application\n"); + text.push_str(&format!("Name={}\n", value(app.name))); + text.push_str(&format!("Comment={}\n", value(app.tagline))); + text.push_str(&format!("Exec={exec} %F\n")); + if icon { + text.push_str(&format!("Icon={}\n", app.bundle_id())); + } + text.push_str("Terminal=false\n"); + text.push_str(&format!("Categories={categories}\n")); + text.push_str(&format!("StartupWMClass={}\n", wm_class.map_or_else(|| app.bundle_id(), str::to_string))); + text.push_str(MARKER); + text.push('\n'); + Some(text) +} + +/// Whether the launcher wrote this entry. +pub fn is_ours(text: &str) -> bool { + text.lines().any(|l| l.trim() == MARKER) +} + +/// The program an entry's `Exec=` runs: its first argument, unquoted. Reads the launcher's own +/// quoting and the plain `Exec="" %F` the apps' AppImages write for themselves. +pub fn exec_program(text: &str) -> Option { + let line = text.lines().find_map(|l| l.strip_prefix("Exec="))?; + // String-value escapes first, then the quoting rule. + let mut unescaped = String::with_capacity(line.len()); + let mut chars = line.chars(); + while let Some(c) = chars.next() { + if c == '\\' { + match chars.next() { + Some('s') => unescaped.push(' '), + Some('n') => unescaped.push('\n'), + Some('t') => unescaped.push('\t'), + Some('r') => unescaped.push('\r'), + Some(other) => unescaped.push(other), + None => unescaped.push('\\'), + } + } else { + unescaped.push(c); + } + } + let mut out = String::new(); + let mut chars = unescaped.trim_start().chars(); + if unescaped.trim_start().starts_with('"') { + chars.next(); + loop { + match chars.next()? { + '"' => break, + '\\' => out.push(chars.next()?), + c => out.push(c), + } + } + } else { + out.extend(chars.take_while(|c| !c.is_whitespace())); + } + Some(out.replace("%%", "%")) +} + +#[cfg(test)] +mod tests { + use std::path::PathBuf; + + use super::*; + + fn app(id: &str) -> &'static App { + App::by_id(id).unwrap() + } + + #[test] + fn writes_a_complete_entry() { + let text = render(app("photocraft"), Path::new("/home/ana/Applications/photocraft-0.6.0-linux-x86_64.AppImage"), true).unwrap(); + let expected = format!( + "[Desktop Entry]\nType=Application\nName=PhotoCraft\nComment={}\n\ + Exec=\"/home/ana/Applications/photocraft-0.6.0-linux-x86_64.AppImage\" %F\n\ + Icon=ai.storyteller.photocraft\nTerminal=false\nCategories=Graphics;2DGraphics;RasterGraphics;Photography;\n\ + StartupWMClass=photocraft\nX-ArtCraft-Launcher=true\n", + app("photocraft").tagline + ); + assert_eq!(text, expected); + assert!(is_ours(&text)); + let no_icon = render(app("wordcraft"), Path::new("/x/w.AppImage"), false).unwrap(); + assert!(!no_icon.contains("Icon=")); + assert!(no_icon.contains("StartupWMClass=ai.storyteller.wordcraft\n")); + assert!(no_icon.contains("Categories=Office;WordProcessor;\n")); + } + + #[test] + fn quotes_exec_paths_per_the_spec() { + assert_eq!(exec_quote(Path::new("/home/ana/My Apps/p.AppImage")).unwrap(), "\"/home/ana/My Apps/p.AppImage\""); + assert_eq!(exec_quote(Path::new("/a/\"q\" $HOME `x`.AppImage")).unwrap(), r#""/a/\\"q\\" \\$HOME \\`x\\`.AppImage""#); + assert_eq!(exec_quote(Path::new(r"/a\b/100%.AppImage")).unwrap(), r#""/a\\\\b/100%%.AppImage""#); + assert_eq!(exec_quote(Path::new("/a/line\nbreak.AppImage")), None); + // The spec forbids `=` in the executable name or path, even quoted. + assert_eq!(exec_quote(Path::new("/opt/artcraft=beta/app")), None); + assert!(render(app("photocraft"), Path::new("/a/line\nbreak.AppImage"), true).is_none()); + } + + #[test] + fn reads_back_the_program_it_wrote_and_the_apps_own_entries() { + for p in ["/home/ana/My Apps/p.AppImage", "/a/\"q\" $HOME `x`.AppImage", r"/a\b/100%.AppImage", "/plain.AppImage"] { + let text = render(app("gridcraft"), Path::new(p), false).unwrap(); + assert_eq!(exec_program(&text).map(PathBuf::from), Some(PathBuf::from(p)), "{p}"); + } + // PhotoCraft's AppRun writes its own entry, unmarked. + let theirs = "[Desktop Entry]\nName=PhotoCraft\nExec=\"/home/ana/Applications/photocraft-0.5.0-linux-x86_64.AppImage\" %F\n"; + assert_eq!(exec_program(theirs).as_deref(), Some("/home/ana/Applications/photocraft-0.5.0-linux-x86_64.AppImage")); + assert!(!is_ours(theirs)); + assert_eq!(exec_program("[Desktop Entry]\nExec=photocraft %F\n").as_deref(), Some("photocraft")); + assert_eq!(exec_program("[Desktop Entry]\nName=x\n"), None); + } +} diff --git a/crates/update/src/detect.rs b/crates/update/src/detect.rs new file mode 100644 index 000000000..840245a18 --- /dev/null +++ b/crates/update/src/detect.rs @@ -0,0 +1,777 @@ +//! Finding Craft Apps that are already installed, however they got there. +//! +//! - macOS: `.app` in `/Applications` or `~/Applications`, read from its `Info.plist`. +//! - Windows: the Windows Installer's uninstall entries (`Learning Machines LLC`), and portable +//! copies the launcher unpacked. +//! - Linux: AppImages in `~/Applications` (where the launcher and AppImageLauncher put them), +//! `~/AppImages` (Gear Lever) and `~/.local/bin`, Debian/RPM packages, Flatpaks, and tarball +//! installs. +//! - FreeBSD: tarball installs and `/usr/local/bin/`. + +use std::path::{Path, PathBuf}; + +use crate::{App, InstallSource, Installed, version}; + +use crate::manifest::Manifest; + +/// One look at the system, shared by every app's detection (package lists are read once). +#[derive(Default)] +pub struct Snapshot { + #[allow(dead_code)] + packages: Vec, +} + +impl Snapshot { + pub fn take() -> Self { + #[cfg(target_os = "linux")] + { + Self { packages: linux::packages() } + } + #[cfg(not(target_os = "linux"))] + { + Self::default() + } + } +} + +/// What's installed of `app`, best source first. +pub fn detect(app: &App, manifest: &Manifest, snapshot: &Snapshot) -> Option { + let _ = snapshot; + if let Some(found) = from_manifest(app, manifest) { + return Some(found); + } + #[cfg(target_os = "macos")] + return macos::detect(app); + #[cfg(windows)] + return windows::detect(app); + #[cfg(target_os = "linux")] + return linux::detect(app, snapshot); + #[cfg(target_os = "freebsd")] + return freebsd::detect(app); + #[allow(unreachable_code)] + None +} + +fn from_manifest(app: &App, manifest: &Manifest) -> Option { + let rec = manifest.apps.get(app.id)?; + if !rec.path.exists() { + return None; + } + // On macOS the bundle itself is the truth (the app may have updated itself). + #[cfg(target_os = "macos")] + if rec.kind == crate::PackageKind::Dmg { + return macos::read_bundle(app, &rec.path); + } + Some(Installed { + version: version::parse(&rec.version), + source: InstallSource::Launcher, + location: rec.path.display().to_string(), + package: None, + }) +} + +/// Whether a file name is an AppImage of the app with asset stems `stems`, and the version its +/// name gives. Matches only the names a release AppImage ends up with: the release's own +/// (`photocraft-0.6.0-linux-x86_64.AppImage`) and the bare app name Gear Lever renames it to +/// (`photocraft.appimage`, version unknown). The extension's case doesn't matter. +#[cfg_attr(not(target_os = "linux"), allow(dead_code))] +pub(crate) fn appimage_version(name: &str, stems: &[&str]) -> Option> { + let split = name.len().checked_sub(".appimage".len())?; + let (base, ext) = (name.get(..split)?, name.get(split..)?); + if !ext.eq_ignore_ascii_case(".appimage") { + return None; + } + if stems.iter().any(|s| base.eq_ignore_ascii_case(s)) { + return Some(None); + } + let parsed = crate::platform::parse_asset_name(&format!("{base}.AppImage"), stems)?; + (parsed.kind == crate::PackageKind::AppImage && parsed.token.starts_with("linux-")).then_some(Some(parsed.version)) +} + +/// Who our Windows Installer packages name as their publisher. +#[cfg_attr(not(windows), allow(dead_code))] +const PUBLISHER: &str = "Learning Machines LLC"; + +/// Whether an uninstall entry's `DisplayName` and `Publisher` are `app`'s, under any name its +/// installers have registered. +#[cfg_attr(not(windows), allow(dead_code))] +fn is_installer_entry(app: &App, display_name: &str, publisher: &str) -> bool { + publisher == PUBLISHER && app.names().any(|n| n == display_name) +} + +/// Where a Windows install of `app` may keep its exe, in the order to try: the entry's +/// `InstallLocation`, then `` under each Program Files root for each name the app has had, +/// and in each folder `.exe` for each stem, current first. Doesn't touch the disk. +#[cfg_attr(not(windows), allow(dead_code))] +fn exe_candidates(app: &App, location: Option<&Path>, program_files: &[PathBuf]) -> Vec { + let in_program_files = program_files.iter().flat_map(|root| app.names().map(move |n| root.join(n))); + let dirs = location.map(Path::to_path_buf).into_iter().chain(in_program_files); + dirs.flat_map(|d| app.stems().map(move |s| d.join(format!("{s}.exe")))).collect() +} + +/// Version from ` --version` output. Binaries print the machine id (`photocraft 0.7.0`), +/// the display name (`SoundCraft 0.5.0`), or a former id (`printcraft 0.2.1` for PdfCraft). +#[cfg_attr(not(any(target_os = "linux", target_os = "freebsd")), allow(dead_code))] +fn parse_cli_version(line: &str, app: &App) -> Option { + let line = line.trim(); + for prefix in app.stems().chain(std::iter::once(app.name)) { + if let Some(rest) = strip_cli_prefix(line, prefix) + && let Some(tok) = rest.split_whitespace().next() + && let Some(v) = version::parse(tok) + { + return Some(v); + } + } + version::parse_package(line) +} + +fn strip_cli_prefix<'a>(line: &'a str, prefix: &str) -> Option<&'a str> { + let (head, rest) = line.split_at_checked(prefix.len())?; + if !head.eq_ignore_ascii_case(prefix) { + return None; + } + match rest.chars().next() { + None => Some(rest), + Some(c) if c.is_ascii_whitespace() => Some(rest), + _ => None, + } +} + +#[cfg(target_os = "macos")] +pub mod macos { + use std::path::{Path, PathBuf}; + + use super::{App, InstallSource, Installed, version}; + + /// Where a `.app` might be: the system and the user's Applications folders. + pub fn candidates(app: &App) -> Vec { + let bundle = format!("{}.app", app.name); + let mut out = vec![Path::new("/Applications").join(&bundle)]; + if let Some(home) = dirs::home_dir() { + out.push(home.join("Applications").join(&bundle)); + } + out + } + + pub fn detect(app: &App) -> Option { + candidates(app).iter().find_map(|p| read_bundle(app, p)) + } + + /// Reads a bundle's `Info.plist`; `None` unless it's really this app. + pub fn read_bundle(app: &App, path: &Path) -> Option { + let plist = plist::Value::from_file(path.join("Contents/Info.plist")).ok()?; + let dict = plist.as_dictionary()?; + if dict.get("CFBundleIdentifier")?.as_string()? != app.bundle_id() { + return None; + } + // `Version` carries the full version including a pre-release suffix; + // CFBundleShortVersionString only has the numbers. + let full_key = format!("{}Version", app.name); + let text = + [full_key.as_str(), "CFBundleShortVersionString", "CFBundleVersion"].iter().find_map(|k| dict.get(k).and_then(plist::Value::as_string)); + Some(Installed { version: text.and_then(version::parse), source: InstallSource::MacApp, location: path.display().to_string(), package: None }) + } +} + +#[cfg(windows)] +pub mod windows { + use std::path::PathBuf; + + use winreg::RegKey; + use winreg::enums::{HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE, KEY_READ, KEY_WOW64_32KEY, KEY_WOW64_64KEY}; + + use super::{App, InstallSource, Installed, exe_candidates, is_installer_entry, version}; + + const UNINSTALL: &str = r"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"; + + /// Reads the ProductVersion string from a program's VERSIONINFO, in the first language that + /// has one, without running the program or any other process. + pub fn executable_version(path: &std::path::Path) -> Option { + let path = dunce::canonicalize(path).ok()?; + let info = winsafe::HVERSIONINFO::GetFileVersionInfo(path.to_str()?).ok()?; + let listed = info.langs_and_cps().map(<[_]>::to_vec).unwrap_or_default(); + let product = string_tables(&listed).into_iter().find_map(|table| info.str_val(table, "ProductVersion").ok())?; + version::parse(product.trim()) + } + + /// The string tables to look in: the languages the file lists, then U.S. English in UTF-16, + /// Windows-1252 and no code page, as .NET's `FileVersionInfo` also tries when a file lists none + /// or its listed one has no strings. + pub(crate) fn string_tables(listed: &[(winsafe::LANGID, winsafe::co::CP)]) -> Vec<(winsafe::LANGID, winsafe::co::CP)> { + use winsafe::{LANGID, co}; + let us = LANGID::new(co::LANG::ENGLISH, co::SUBLANG::ENGLISH_US); + listed.iter().copied().chain([(us, co::CP::UTF16), (us, co::CP::WINDOWS_1252), (us, co::CP::ACP)]).collect() + } + + /// An MSI install found in the registry. + pub struct MsiInstall { + pub product_code: String, + pub version: Option, + pub exe: Option, + } + + /// Every Windows Installer entry for `app`, newest first. + pub fn msi_installs(app: &App) -> Vec { + let mut out = Vec::new(); + for (hive, flags) in [(HKEY_LOCAL_MACHINE, KEY_WOW64_64KEY), (HKEY_LOCAL_MACHINE, KEY_WOW64_32KEY), (HKEY_CURRENT_USER, 0)] { + let Ok(root) = RegKey::predef(hive).open_subkey_with_flags(UNINSTALL, KEY_READ | flags) else { continue }; + for code in root.enum_keys().flatten() { + let Ok(key) = root.open_subkey_with_flags(&code, KEY_READ | flags) else { continue }; + let name: String = key.get_value("DisplayName").unwrap_or_default(); + let publisher: String = key.get_value("Publisher").unwrap_or_default(); + if !is_installer_entry(app, &name, &publisher) || out.iter().any(|m: &MsiInstall| m.product_code == code) { + continue; + } + let version = key.get_value::("DisplayVersion").ok().and_then(|v| version::parse(&v)); + let location: String = key.get_value("InstallLocation").unwrap_or_default(); + let exe = exe_path(app, (!location.is_empty()).then(|| PathBuf::from(location)).or_else(|| install_dir(app))); + out.push(MsiInstall { product_code: code, version, exe }); + } + } + out.sort_by(|a, b| match (&b.version, &a.version) { + (Some(x), Some(y)) => version::compare(x, y), + (Some(_), None) => std::cmp::Ordering::Greater, + (None, Some(_)) => std::cmp::Ordering::Less, + (None, None) => std::cmp::Ordering::Equal, + }); + out + } + + /// The folder the app's MSI remembers for its next upgrade, which is where it went when the + /// launcher chose the folder. + fn install_dir(app: &App) -> Option { + [KEY_WOW64_64KEY, KEY_WOW64_32KEY].into_iter().find_map(|flags| { + let key = RegKey::predef(HKEY_LOCAL_MACHINE).open_subkey_with_flags(format!(r"SOFTWARE\{}", app.name), KEY_READ | flags).ok()?; + key.get_value::("InstallDir").ok().filter(|d| !d.is_empty()).map(PathBuf::from) + }) + } + + /// The app's exe: from `App Paths`, the install folder, or Program Files. + fn exe_path(app: &App, location: Option) -> Option { + for stem in app.stems() { + let app_paths = format!(r"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\{stem}.exe"); + for flags in [KEY_WOW64_64KEY, KEY_WOW64_32KEY] { + if let Ok(key) = RegKey::predef(HKEY_LOCAL_MACHINE).open_subkey_with_flags(&app_paths, KEY_READ | flags) + && let Ok(path) = key.get_value::("") + { + let p = PathBuf::from(path.trim_matches('"')); + if p.exists() { + return Some(p); + } + } + } + } + let mut roots = Vec::new(); + for var in ["ProgramW6432", "ProgramFiles", "ProgramFiles(x86)"] { + if let Some(pf) = std::env::var_os(var) { + roots.push(PathBuf::from(pf)); + } + } + exe_candidates(app, location.as_deref(), &roots).into_iter().find(|p| p.exists()) + } + + pub fn detect(app: &App) -> Option { + let m = msi_installs(app).into_iter().next()?; + Some(Installed { + version: m.version, + source: InstallSource::Msi, + location: m.exe.map_or_else(|| app.name.to_string(), |p| p.display().to_string()), + package: Some(m.product_code), + }) + } +} + +#[cfg(target_os = "linux")] +pub mod linux { + use std::path::{Path, PathBuf}; + use std::time::Duration; + + use super::{App, InstallSource, Installed, Snapshot, appimage_version, parse_cli_version, version}; + use crate::packages::{self, Package}; + use crate::process::capture; + + /// Installed Craft App packages from every package manager on this system (dpkg, rpm, + /// pacman, Flatpak). Read once per detection pass. + pub fn packages() -> Vec { + let ours = |name: &str| { + crate::suite::SUITE.iter().any(|a| { + [InstallSource::Deb, InstallSource::Rpm, InstallSource::Pacman, InstallSource::Flatpak] + .iter() + .any(|m| crate::packages::candidate_names(a, *m).iter().any(|n| n == name)) + }) + }; + let t = Duration::from_secs(10); + let mut out = Vec::new(); + let mut add = |manager, list: Vec<(String, String)>| { + out.extend(list.into_iter().filter(|(n, _)| ours(n)).map(|(name, version)| Package { manager, name, version })); + }; + if let Some(list) = capture("dpkg-query", &["-W", "-f", "${Package}|${db:Status-Status}|${Version}\n"], t) { + add(InstallSource::Deb, packages::parse_dpkg(&list)); + } + if let Some(list) = capture("rpm", &["-qa", "--qf", "%{NAME}|%{VERSION}\n"], t) { + add(InstallSource::Rpm, packages::parse_rpm(&list)); + } + // Only run `pacman` where its database exists: elsewhere `pacman` on PATH may be an unrelated program + // (the Pac-Man game in /usr/games on Debian-family systems). + if has_pacman_db() + && let Some(list) = capture("pacman", &["-Q"], t) + { + add(InstallSource::Pacman, packages::parse_pacman(&list)); + } + let arch = format!("--arch={}", std::env::consts::ARCH); + if let Some(list) = crate::process::capture_flatpak(&["list", "--app", &arch, "--columns=application,version"], t) { + let mut flatpaks = packages::parse_flatpak_list(&list); + // Bundles built from a release tarball often list no version: ask Flatpak directly. + for (id, version) in flatpaks.iter_mut().filter(|(id, v)| v.is_empty() && id.starts_with("ai.storyteller.")) { + if let Some(v) = crate::process::capture_flatpak(&["info", &arch, id], t).as_deref().and_then(packages::parse_flatpak_info) { + *version = v; + } + } + add(InstallSource::Flatpak, flatpaks); + } + out + } + + /// The local package database of Arch's pacman. + pub(crate) const PACMAN_DB: &str = "/var/lib/pacman/local"; + + /// True when `db` is a directory, i.e. this system really uses pacman. + pub(crate) fn pacman_db_present(db: &Path) -> bool { + db.is_dir() + } + + /// True when the system's default pacman database directory exists. + pub(crate) fn has_pacman_db() -> bool { + pacman_db_present(Path::new(PACMAN_DB)) + } + + /// Where a package put the app's binary: on `PATH`, else `/usr/bin/`. + fn binary_on_path(id: &str) -> String { + std::env::var_os("PATH") + .and_then(|path| std::env::split_paths(&path).map(|d| d.join(id)).find(|p| p.is_file())) + .unwrap_or_else(|| PathBuf::from("/usr/bin").join(id)) + .display() + .to_string() + } + + /// `~/Applications`, where the launcher puts AppImages (the AppImageLauncher convention). + pub fn appimage_dir() -> Option { + dirs::home_dir().map(|h| h.join("Applications")) + } + + /// Every folder searched for AppImages, the launcher's own first: `~/Applications`, + /// `~/AppImages` (Gear Lever's default) and `~/.local/bin`. + pub fn appimage_dirs() -> Vec { + let Some(home) = dirs::home_dir() else { return Vec::new() }; + vec![home.join("Applications"), home.join("AppImages"), home.join(".local").join("bin")] + } + + /// AppImages of `app` in `dirs`, newest first (unknown versions last; earlier folders win + /// ties). + pub fn appimages_in(app: &App, dirs: &[PathBuf]) -> Vec<(PathBuf, Option)> { + let stems: Vec<&str> = app.stems().collect(); + let mut out: Vec<(PathBuf, Option)> = Vec::new(); + for dir in dirs { + let Ok(entries) = std::fs::read_dir(dir) else { continue }; + let mut found: Vec<(PathBuf, Option)> = entries + .flatten() + .filter_map(|e| { + let name = e.file_name().into_string().ok()?; + let version = appimage_version(&name, &stems)?; + let path = e.path(); + path.is_file().then(|| (path, version.as_deref().and_then(version::parse))) + }) + .collect(); + found.sort_by(|a, b| a.0.cmp(&b.0)); + out.extend(found); + } + // Stable sort: equal versions keep the folder order above. + out.sort_by(|a, b| match (&b.1, &a.1) { + (Some(x), Some(y)) => version::compare(x, y), + (Some(_), None) => std::cmp::Ordering::Greater, + (None, Some(_)) => std::cmp::Ordering::Less, + (None, None) => std::cmp::Ordering::Equal, + }); + out + } + + /// AppImages of `app` in any of the usual folders, newest first. + pub fn appimages(app: &App) -> Vec<(PathBuf, Option)> { + appimages_in(app, &appimage_dirs()) + } + + pub fn detect(app: &App, snapshot: &Snapshot) -> Option { + if let Some((path, version)) = appimages(app).into_iter().next() { + return Some(Installed { version, source: InstallSource::AppImage, location: path.display().to_string(), package: None }); + } + let order = [InstallSource::Deb, InstallSource::Rpm, InstallSource::Pacman, InstallSource::Flatpak]; + if let Some(p) = packages::find(app, &snapshot.packages, &order) { + let location = if p.manager == InstallSource::Flatpak { app.bundle_id() } else { binary_on_path(app.id) }; + return Some(Installed { version: packages::version_of(p), source: p.manager, location, package: Some(p.name.clone()) }); + } + detect_system_install(app) + } + + /// When the package managers don't report the app (a query timed out or was locked, an + /// unlisted package name, or a sandbox that can't see them) but its binary is there. It is + /// reported as a system binary: the launcher can open it but leaves updating and removing it + /// to whatever installed it, since it can't tell which package manager that was. + fn detect_system_install(app: &App) -> Option { + let bin = find_binary(app.id)?; + Some(Installed { version: binary_version(&bin, app), source: InstallSource::Binary, location: bin.display().to_string(), package: None }) + } + + fn find_binary(id: &str) -> Option { + let on_path = std::env::var_os("PATH").and_then(|path| std::env::split_paths(&path).map(|d| d.join(id)).find(|p| p.is_file())); + let usual = ["/usr/bin", "/usr/local/bin"].into_iter().map(PathBuf::from).chain(dirs::home_dir().map(|h| h.join(".local/bin"))); + on_path.or_else(|| usual.map(|d| d.join(id)).find(|p| p.is_file())) + } + + pub(crate) fn binary_version(bin: &Path, app: &App) -> Option { + let out = crate::process::capture(bin.to_str()?, &["--version"], Duration::from_secs(5))?; + parse_cli_version(out.trim(), app) + } +} + +#[cfg(target_os = "freebsd")] +pub mod freebsd { + use std::path::Path; + use std::time::Duration; + + use super::{App, InstallSource, Installed, parse_cli_version, version}; + + pub fn detect(app: &App) -> Option { + let bin = Path::new("/usr/local/bin").join(app.id); + if !bin.exists() { + return None; + } + Some(Installed { version: binary_version(&bin, app), source: InstallSource::Binary, location: bin.display().to_string(), package: None }) + } + + /// ` --version` prints ` 0.5.0 (sha, date)`. + fn binary_version(bin: &Path, app: &App) -> Option { + let out = crate::process::capture(bin.to_str()?, &["--version"], Duration::from_secs(5))?; + parse_cli_version(out.trim(), app) + } +} + +#[cfg(all(test, target_os = "linux"))] +mod linux_tests { + use super::*; + + #[test] + fn ours_filter_matches_rpm_packages() { + let ours = |name: &str| { + crate::suite::SUITE.iter().any(|a| { + [InstallSource::Deb, InstallSource::Rpm, InstallSource::Pacman, InstallSource::Flatpak] + .iter() + .any(|m| crate::packages::candidate_names(a, *m).iter().any(|n| n == name)) + }) + }; + assert!(ours("photocraft")); + assert!(ours("ai.storyteller.photocraft")); + assert!(!ours("unrelated-package")); + } + + #[test] + fn pacman_is_only_queried_where_its_database_exists() { + let root = tempfile::tempdir().unwrap(); + assert!(!linux::pacman_db_present(&root.path().join("local"))); + std::fs::create_dir(root.path().join("local")).unwrap(); + assert!(linux::pacman_db_present(&root.path().join("local"))); + } + + /// `linux::binary_version` on a script the test just wrote. Another test thread that forks + /// while the script is still open for writing holds that descriptor until it execs, and running + /// the script in that window fails with ETXTBSY ("Text file busy"), so a miss is retried. + fn binary_version_of_new_script(bin: &Path, app: &App) -> Option { + for _ in 0..20 { + if let Some(v) = linux::binary_version(bin, app) { + return Some(v); + } + std::thread::sleep(std::time::Duration::from_millis(50)); + } + None + } + + #[test] + fn reads_binary_version_from_cli() { + let photo = App::by_id("photocraft").unwrap(); + let temp_dir = tempfile::tempdir().unwrap(); + let bin = temp_dir.path().join("photocraft"); + std::fs::write(&bin, "#!/bin/sh\necho 'photocraft 0.6.0 (0c72d9542, 2026-10-10)'\n").unwrap(); + use std::os::unix::fs::PermissionsExt; + std::fs::set_permissions(&bin, std::fs::Permissions::from_mode(0o755)).unwrap(); + let ver = binary_version_of_new_script(&bin, photo); + assert_eq!(ver.unwrap().to_string(), "0.6.0"); + } + + #[test] + fn reads_binary_version_when_the_cli_uses_the_display_name_or_a_former_id() { + let temp_dir = tempfile::tempdir().unwrap(); + use std::os::unix::fs::PermissionsExt; + let write = |name: &str, script: &str| { + let bin = temp_dir.path().join(name); + std::fs::write(&bin, script).unwrap(); + std::fs::set_permissions(&bin, std::fs::Permissions::from_mode(0o755)).unwrap(); + bin + }; + let sound = write("soundcraft", "#!/bin/sh\necho 'SoundCraft 0.5.0'\n"); + assert_eq!(binary_version_of_new_script(&sound, App::by_id("soundcraft").unwrap()).unwrap().to_string(), "0.5.0"); + let pdf = write("pdfcraft", "#!/bin/sh\necho 'printcraft 0.2.1'\n"); + assert_eq!(binary_version_of_new_script(&pdf, App::by_id("pdfcraft").unwrap()).unwrap().to_string(), "0.2.1"); + } +} + +#[cfg(test)] +mod tests { + use crate::SUITE; + + use super::*; + + /// A file without VERSIONINFO, or one that isn't there, has no version. + #[cfg(windows)] + #[test] + fn a_file_without_version_info_has_no_version() { + let dir = tempfile::tempdir().unwrap(); + let text = dir.path().join("photocraft.exe"); + std::fs::write(&text, "not a program").unwrap(); + assert_eq!(windows::executable_version(&text), None); + assert_eq!(windows::executable_version(&dir.path().join("missing.exe")), None); + } + + /// Without a listed language, the version is still looked for in the string tables .NET's + /// `FileVersionInfo` falls back to (040904B0, 040904E4, 04090000), after any listed one. + #[cfg(windows)] + #[test] + fn the_version_is_looked_for_in_the_usual_string_tables_too() { + use winsafe::{LANGID, co}; + let key = |(lang, cp): (LANGID, co::CP)| format!("{:04x}{:04x}", u16::from(lang), u16::from(cp)); + let keys = |listed: &[(LANGID, co::CP)]| windows::string_tables(listed).into_iter().map(key).collect::>(); + assert_eq!(keys(&[]), ["040904b0", "040904e4", "04090000"]); + let german = (LANGID::new(co::LANG::GERMAN, co::SUBLANG::GERMAN), co::CP::UTF16); + assert_eq!(keys(&[german]), ["040704b0", "040904b0", "040904e4", "04090000"]); + } + + /// A program whose VERSIONINFO has a ProductVersion in the U.S. English UTF-16 table but no + /// `\VarFileInfo\Translation` entry still has a version. + #[cfg(windows)] + #[test] + fn a_version_without_a_translation_entry_is_found() { + let dir = tempfile::tempdir().unwrap(); + let exe = dir.path().join("photocraft.exe"); + std::fs::write(&exe, resource_only_program("1.2.3")).unwrap(); + assert_eq!(windows::executable_version(&exe), version::parse("1.2.3")); + } + + /// A VERSIONINFO block node: length, value length, type, the key, then the value and the + /// children, each aligned to 4 bytes. + #[cfg(windows)] + fn version_node(key: &str, value: &[u8], value_length: u16, text: bool, children: &[Vec]) -> Vec { + let align = |bytes: &mut Vec| bytes.resize(bytes.len().next_multiple_of(4), 0); + let mut node = vec![0, 0]; + node.extend(value_length.to_le_bytes()); + node.extend(u16::from(text).to_le_bytes()); + node.extend(key.encode_utf16().chain([0]).flat_map(u16::to_le_bytes)); + align(&mut node); + node.extend(value); + for child in children { + align(&mut node); + node.extend(child); + } + let length = u16::try_from(node.len()).unwrap(); + node[..2].copy_from_slice(&length.to_le_bytes()); + node + } + + /// A 64-bit program file holding only a version resource (`ProductVersion` = `product`, in + /// the 040904B0 string table) and no `\VarFileInfo\Translation` entry. + #[cfg(windows)] + fn resource_only_program(product: &str) -> Vec { + let put = |bytes: &mut Vec, at: usize, value: &[u8]| bytes[at..at + value.len()].copy_from_slice(value); + let text: Vec = product.encode_utf16().chain([0]).flat_map(u16::to_le_bytes).collect(); + let string = version_node("ProductVersion", &text, u16::try_from(product.len() + 1).unwrap(), true, &[]); + let table = version_node("040904b0", &[], 0, true, &[string]); + let strings = version_node("StringFileInfo", &[], 0, true, &[table]); + let mut fixed = vec![0u8; 52]; + put(&mut fixed, 0, &0xFEEF_04BDu32.to_le_bytes()); + put(&mut fixed, 4, &0x0001_0000u32.to_le_bytes()); + let info = version_node("VS_VERSION_INFO", &fixed, 52, false, &[strings]); + + // The resource section: type 16 (RT_VERSION) > id 1 > language 0x0409 > the data. + const RVA: u32 = 0x1000; + let mut rsrc = vec![0u8; 0x58]; + for (dir, id, next) in [(0x00, 16u32, 0x8000_0018u32), (0x18, 1, 0x8000_0030), (0x30, 0x0409, 0x48)] { + put(&mut rsrc, dir + 14, &1u16.to_le_bytes()); + put(&mut rsrc, dir + 16, &id.to_le_bytes()); + put(&mut rsrc, dir + 20, &next.to_le_bytes()); + } + put(&mut rsrc, 0x48, &(RVA + 0x58).to_le_bytes()); + put(&mut rsrc, 0x4C, &u32::try_from(info.len()).unwrap().to_le_bytes()); + rsrc.extend(&info); + let raw = u32::try_from(rsrc.len().next_multiple_of(0x200)).unwrap(); + let rsrc_len = u32::try_from(rsrc.len()).unwrap(); + + let mut file = vec![0u8; 0x200]; + put(&mut file, 0, b"MZ"); + put(&mut file, 0x3C, &0x40u32.to_le_bytes()); + put(&mut file, 0x40, b"PE\0\0"); + // COFF header: x64, one section, a 240-byte optional header, an executable DLL image. + put(&mut file, 0x44, &0x8664u16.to_le_bytes()); + put(&mut file, 0x46, &1u16.to_le_bytes()); + put(&mut file, 0x54, &240u16.to_le_bytes()); + put(&mut file, 0x56, &0x2022u16.to_le_bytes()); + // PE32+ optional header. + let opt = 0x58; + put(&mut file, opt, &0x20Bu16.to_le_bytes()); + put(&mut file, opt + 8, &raw.to_le_bytes()); + put(&mut file, opt + 24, &0x1_4000_0000u64.to_le_bytes()); + put(&mut file, opt + 32, &0x1000u32.to_le_bytes()); + put(&mut file, opt + 36, &0x200u32.to_le_bytes()); + put(&mut file, opt + 40, &6u16.to_le_bytes()); + put(&mut file, opt + 48, &6u16.to_le_bytes()); + put(&mut file, opt + 56, &(RVA + rsrc_len.next_multiple_of(0x1000)).to_le_bytes()); + put(&mut file, opt + 60, &0x200u32.to_le_bytes()); + put(&mut file, opt + 68, &2u16.to_le_bytes()); + put(&mut file, opt + 72, &0x10_0000u64.to_le_bytes()); + put(&mut file, opt + 80, &0x1000u64.to_le_bytes()); + put(&mut file, opt + 88, &0x10_0000u64.to_le_bytes()); + put(&mut file, opt + 96, &0x1000u64.to_le_bytes()); + put(&mut file, opt + 108, &16u32.to_le_bytes()); + put(&mut file, opt + 112 + 2 * 8, &RVA.to_le_bytes()); + put(&mut file, opt + 112 + 2 * 8 + 4, &rsrc_len.to_le_bytes()); + // The .rsrc section header. + let section = opt + 240; + put(&mut file, section, b".rsrc\0\0\0"); + put(&mut file, section + 8, &rsrc_len.to_le_bytes()); + put(&mut file, section + 12, &RVA.to_le_bytes()); + put(&mut file, section + 16, &raw.to_le_bytes()); + put(&mut file, section + 20, &0x200u32.to_le_bytes()); + put(&mut file, section + 36, &0x4000_0040u32.to_le_bytes()); + rsrc.resize(usize::try_from(raw).unwrap(), 0); + file.extend(rsrc); + file + } + + #[test] + fn recognises_appimage_names_conservatively() { + let pdf = ["pdfcraft", "printcraft"]; + let v = |n: &str| appimage_version(n, &pdf); + assert_eq!(v("pdfcraft-0.3.0-linux-x86_64.AppImage"), Some(Some("0.3.0".into()))); + assert_eq!(v("printcraft-0.2.1-linux-aarch64.appimage"), Some(Some("0.2.1".into()))); + assert_eq!(v("pdfcraft-0.3.0-rc.1-linux-x86_64.AppImage"), Some(Some("0.3.0-rc.1".into()))); + // Gear Lever's rename, and a plain rename by hand. + assert_eq!(v("pdfcraft.appimage"), Some(None)); + assert_eq!(v("PdfCraft.AppImage"), Some(None)); + for other in [ + "pdfcraft-0.3.0-linux-x86_64.AppImage.zsync", + "pdfcraft-0.3.0-linux-x86_64.deb", + "pdfcraft-0.3.0-windows-x64.AppImage", + "pdfcraft-cli-0.3.0-linux-x86_64.AppImage", + "photocraft-0.6.0-linux-x86_64.AppImage", + "mypdfcraft.AppImage", + "pdfcraft-latest.AppImage", + ".appimage", + "x", + ] { + assert_eq!(v(other), None, "{other}"); + } + } + + #[test] + fn parse_cli_version_accepts_id_display_name_and_former_id() { + let photo = App::by_id("photocraft").unwrap(); + let sound = App::by_id("soundcraft").unwrap(); + let pdf = App::by_id("pdfcraft").unwrap(); + assert_eq!(parse_cli_version("photocraft 0.7.0 (dev build)", photo).unwrap().to_string(), "0.7.0"); + assert_eq!(parse_cli_version("SoundCraft 0.5.0", sound).unwrap().to_string(), "0.5.0"); + assert_eq!(parse_cli_version("printcraft 0.2.1", pdf).unwrap().to_string(), "0.2.1"); + assert_eq!(parse_cli_version("PdfCraft 0.6.0", pdf).unwrap().to_string(), "0.6.0"); + assert!(strip_cli_prefix("photocraft-cli 1.0.0", "photocraft").is_none()); + } + + #[cfg(target_os = "linux")] + #[test] + fn finds_appimages_in_every_folder_newest_first() { + let app = App::by_id("photocraft").unwrap(); + let root = tempfile::tempdir().unwrap(); + let (apps, gear, bin) = (root.path().join("Applications"), root.path().join("AppImages"), root.path().join("bin")); + for d in [&apps, &gear, &bin] { + std::fs::create_dir_all(d).unwrap(); + } + std::fs::write(apps.join("photocraft-0.5.0-linux-x86_64.AppImage"), b"").unwrap(); + std::fs::write(apps.join("wordcraft-0.3.0-linux-x86_64.AppImage"), b"").unwrap(); + std::fs::write(gear.join("photocraft.appimage"), b"").unwrap(); + std::fs::write(bin.join("photocraft-0.6.0-linux-x86_64.AppImage"), b"").unwrap(); + std::fs::create_dir(bin.join("photocraft.AppImage")).unwrap(); + let found = linux::appimages_in(app, &[apps.clone(), gear.clone(), bin.clone(), root.path().join("missing")]); + let names: Vec<_> = found.iter().map(|(p, v)| (p.clone(), v.as_ref().map(ToString::to_string))).collect(); + assert_eq!( + names, + [ + (bin.join("photocraft-0.6.0-linux-x86_64.AppImage"), Some("0.6.0".to_string())), + (apps.join("photocraft-0.5.0-linux-x86_64.AppImage"), Some("0.5.0".to_string())), + (gear.join("photocraft.appimage"), None), + ] + ); + } + + const LEARNING_MACHINES: &str = "Learning Machines LLC"; + + fn roots() -> Vec { + vec![PathBuf::from(r"C:\Program Files"), PathBuf::from(r"C:\Program Files (x86)")] + } + + #[test] + fn pdfcraft_matches_its_former_printcraft_installer_name() { + let pdf = App::by_id("pdfcraft").unwrap(); + assert!(is_installer_entry(pdf, "PrintCraft", LEARNING_MACHINES), "a PrintCraft entry from {LEARNING_MACHINES} should be PdfCraft's"); + } + + #[test] + fn pdfcraft_still_matches_its_own_installer_name() { + let pdf = App::by_id("pdfcraft").unwrap(); + assert!(is_installer_entry(pdf, "PdfCraft", LEARNING_MACHINES)); + } + + #[test] + fn printcraft_entries_match_nothing_else() { + let pdf = App::by_id("pdfcraft").unwrap(); + assert!(!is_installer_entry(pdf, "PrintCraft", "Some Other Publisher")); + assert!(!is_installer_entry(pdf, "PhotoCraft", LEARNING_MACHINES)); + for a in SUITE.iter().filter(|a| a.id != "pdfcraft") { + assert!(!is_installer_entry(a, "PrintCraft", LEARNING_MACHINES), "PrintCraft matched {}", a.name); + } + } + + #[test] + fn pdfcraft_exe_is_sought_under_every_name_it_has_shipped_as() { + let pdf = App::by_id("pdfcraft").unwrap(); + let location = PathBuf::from(r"D:\Apps\PrintCraft"); + let roots = roots(); + let mut want = vec![location.join("pdfcraft.exe"), location.join("printcraft.exe")]; + for root in &roots { + for dir in ["PdfCraft", "PrintCraft"] { + want.push(root.join(dir).join("pdfcraft.exe")); + want.push(root.join(dir).join("printcraft.exe")); + } + } + assert_eq!(exe_candidates(pdf, Some(&location), &roots), want); + } + + #[test] + fn apps_without_former_names_match_and_find_their_exe_as_before() { + let roots = roots(); + let location = PathBuf::from(r"D:\Apps\Somewhere"); + for a in SUITE.iter().filter(|a| a.former_ids.is_empty() && a.former_names.is_empty()) { + assert!(is_installer_entry(a, a.name, LEARNING_MACHINES), "{}", a.name); + assert!(!is_installer_entry(a, a.name, "Some Other Publisher"), "{}", a.name); + assert!(!is_installer_entry(a, &a.name.to_lowercase(), LEARNING_MACHINES), "{}", a.name); + let exe = format!("{}.exe", a.id); + let in_program_files: Vec = roots.iter().map(|r| r.join(a.name).join(&exe)).collect(); + assert_eq!(exe_candidates(a, None, &roots), in_program_files); + let with_location: Vec = std::iter::once(location.join(&exe)).chain(in_program_files).collect(); + assert_eq!(exe_candidates(a, Some(&location), &roots), with_location); + } + } +} diff --git a/crates/update/src/dialog.rs b/crates/update/src/dialog.rs new file mode 100644 index 000000000..e43c38ae1 --- /dev/null +++ b/crates/update/src/dialog.rs @@ -0,0 +1,394 @@ +//! Startup update prompt dialog for egui frontends. + +use egui::{Align, Align2, Color32, Layout, RichText, Vec2}; + +use crate::app::App; +use crate::release::Release; + +/// What the user chose in the startup update prompt. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum StartupPromptResponse { + /// Dialog is still open and waiting for user input. + Pending, + /// User chose to update and restart now. + UpdateAndRestart, + /// User chose to download manually via browser. + Download, + /// User dismissed the dialog for now. + Later, +} + +/// State for the startup update prompt dialog. +#[derive(Clone, Debug)] +pub struct StartupPrompt { + pub app: &'static App, + pub release: Release, + pub running_version: String, + pub is_self_updating: bool, + pub open: bool, + pub status_message: Option, + pub is_installing: bool, +} + +impl StartupPrompt { + /// Creates a new prompt dialog state for the given app and newly discovered release. + pub fn new(app: &'static App, release: Release, running_version: impl Into) -> Self { + let is_self_updating = crate::self_update::is_self_updating(app); + Self { app, release, running_version: running_version.into(), is_self_updating, open: true, status_message: None, is_installing: false } + } + + /// Renders the modal window on the given egui context. + pub fn show(&mut self, ctx: &egui::Context) -> StartupPromptResponse { + if !self.open { + return StartupPromptResponse::Later; + } + + let mut response = StartupPromptResponse::Pending; + + // Modal dimmed background overlay; it senses clicks so they don't reach widgets underneath. + let screen_rect = ctx.content_rect(); + egui::Area::new(egui::Id::unique("craft_update_backdrop")).order(egui::Order::Middle).fixed_pos(screen_rect.min).show(ctx, |ui| { + let (rect, _) = ui.allocate_exact_size(screen_rect.size(), egui::Sense::click_and_drag()); + ui.painter().rect_filled(rect, 0.0, Color32::from_black_alpha(150)); + }); + + if ctx.input_mut(|i| i.consume_key(egui::Modifiers::NONE, egui::Key::Escape)) { + response = StartupPromptResponse::Later; + } + + let is_dark = ctx.theme() == egui::Theme::Dark; + let card_bg = if is_dark { Color32::from_rgb(26, 26, 28) } else { Color32::from_rgb(255, 255, 255) }; + let border_color = if is_dark { Color32::from_rgb(52, 52, 56) } else { Color32::from_rgb(215, 215, 220) }; + let text_primary = if is_dark { Color32::from_rgb(245, 245, 248) } else { Color32::from_rgb(20, 20, 25) }; + let text_muted = if is_dark { Color32::from_rgb(150, 150, 160) } else { Color32::from_rgb(105, 105, 115) }; + let accent_color = Color32::from_rgb(52, 120, 246); + + egui::Area::new(egui::Id::unique("craft_update.startup_prompt")) + .anchor(Align2::CENTER_CENTER, Vec2::ZERO) + .order(egui::Order::Foreground) + .show(ctx, |ui| { + egui::Frame::new() + .fill(card_bg) + .stroke(egui::Stroke::new(1.0, border_color)) + .corner_radius(egui::CornerRadius::ZERO) + .inner_margin(egui::Margin::symmetric(20, 18)) + .show(ui, |ui| { + ui.set_width(400.0); + ui.spacing_mut().item_spacing = Vec2::new(8.0, 6.0); + + // Header row: App badge + title + close + ui.horizontal(|ui| { + let (badge_rect, _) = ui.allocate_exact_size(Vec2::new(34.0, 34.0), egui::Sense::hover()); + ui.painter().rect_filled(badge_rect, egui::CornerRadius::ZERO, accent_color); + let initial = self.app.name.chars().next().map(|c| c.to_string()).unwrap_or_else(|| "A".to_string()); + ui.painter().text(badge_rect.center(), Align2::CENTER_CENTER, initial, egui::FontId::monospace(18.0), Color32::WHITE); + + ui.add_space(4.0); + + ui.vertical(|ui| { + ui.spacing_mut().item_spacing.y = 2.0; + ui.horizontal(|ui| { + ui.label(RichText::new(self.app.name).size(15.0).strong().color(text_primary)); + ui.label(RichText::new("UPDATE").size(10.0).monospace().color(accent_color)); + }); + let version_pill = format!("v{} → {}", self.running_version, self.release.version_label()); + ui.label(RichText::new(version_pill).size(11.0).monospace().color(text_muted)); + }); + + ui.with_layout(Layout::right_to_left(Align::Min), |ui| { + let x_btn = egui::Button::new(RichText::new("✕").size(12.0).color(text_muted)).frame(false); + if ui.add(x_btn).clicked() { + response = StartupPromptResponse::Later; + } + }); + }); + + ui.add_space(8.0); + + if let Some(msg) = &self.status_message { + let alert_bg = if is_dark { Color32::from_rgb(50, 30, 20) } else { Color32::from_rgb(255, 242, 235) }; + let alert_color = Color32::from_rgb(225, 95, 30); + egui::Frame::new() + .fill(alert_bg) + .stroke(egui::Stroke::new(1.0, alert_color)) + .corner_radius(egui::CornerRadius::ZERO) + .inner_margin(egui::Margin::symmetric(10, 6)) + .show(ui, |ui| { + ui.set_width(ui.available_width()); + ui.label(RichText::new(msg).size(12.0).color(alert_color)); + }); + ui.add_space(4.0); + } + + if self.is_installing { + ui.horizontal(|ui| { + ui.spinner(); + ui.label(RichText::new("Downloading and installing update…").size(12.0).color(text_primary)); + }); + } else { + let desc = + format!("{} {} is available with the latest features and bug fixes.", self.app.name, self.release.version_label()); + ui.label(RichText::new(desc).size(12.5).color(text_muted)); + } + + ui.add_space(10.0); + let (hairline_rect, _) = ui.allocate_exact_size(Vec2::new(ui.available_width(), 1.0), egui::Sense::hover()); + ui.painter().hline(hairline_rect.x_range(), hairline_rect.top(), egui::Stroke::new(1.0, border_color)); + ui.add_space(8.0); + + // Footer actions + ui.horizontal(|ui| { + let what_new = egui::Hyperlink::from_label_and_url( + RichText::new("What's new").size(11.5).color(accent_color), + &self.release.html_url, + ); + ui.add(what_new); + + ui.with_layout(Layout::right_to_left(Align::Center), |ui| { + if self.is_installing { + let dismiss_btn = egui::Button::new(RichText::new("DISMISS").size(10.5).monospace().color(text_muted)) + .stroke(egui::Stroke::new(1.0, border_color)) + .corner_radius(egui::CornerRadius::ZERO) + .min_size(Vec2::new(72.0, 26.0)); + if ui.add(dismiss_btn).clicked() { + response = StartupPromptResponse::Later; + } + } else { + if self.is_self_updating { + let update_btn = egui::Button::new( + RichText::new("UPDATE AND RESTART").size(10.5).strong().monospace().color(Color32::WHITE), + ) + .fill(accent_color) + .corner_radius(egui::CornerRadius::ZERO) + .min_size(Vec2::new(140.0, 26.0)); + if ui.add(update_btn).clicked() { + response = StartupPromptResponse::UpdateAndRestart; + } + } else { + let dl_btn = + egui::Button::new(RichText::new("DOWNLOAD").size(10.5).strong().monospace().color(Color32::WHITE)) + .fill(accent_color) + .corner_radius(egui::CornerRadius::ZERO) + .min_size(Vec2::new(90.0, 26.0)); + if ui.add(dl_btn).clicked() { + response = StartupPromptResponse::Download; + } + } + + let later_btn = egui::Button::new(RichText::new("LATER").size(10.5).monospace().color(text_primary)) + .stroke(egui::Stroke::new(1.0, border_color)) + .corner_radius(egui::CornerRadius::ZERO) + .min_size(Vec2::new(65.0, 26.0)); + if ui.add(later_btn).clicked() { + response = StartupPromptResponse::Later; + } + } + }); + }); + }); + }); + + if response == StartupPromptResponse::Later { + self.open = false; + } + + response + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::suite::PHOTOCRAFT; + use egui::{Context, Event, FullOutput, Key, Modifiers, RawInput, Rect}; + + const DARK_CARD: Color32 = Color32::from_rgb(26, 26, 28); + const DARK_STATUS: Color32 = Color32::from_rgb(50, 30, 20); + + fn screen() -> Rect { + Rect::from_min_size(egui::Pos2::ZERO, egui::vec2(640.0, 480.0)) + } + + /// A dark, animation-free context so a single frame paints final (unfaded) colors. + fn dark_context() -> Context { + let ctx = Context::default(); + ctx.set_theme(egui::ThemePreference::Dark); + ctx.global_style_mut(|style| style.animation_time = 0.0); + ctx + } + + /// A prompt in a known installation mode, without probing the host for a real install. + fn prompt_for(is_self_updating: bool) -> StartupPrompt { + let release = Release { version: crate::version::parse("0.2.0"), tag: "v0.2.0".to_string(), ..Release::default() }; + StartupPrompt { + app: &PHOTOCRAFT, + release, + running_version: "0.1.0".to_string(), + is_self_updating, + open: true, + status_message: None, + is_installing: false, + } + } + + fn frame(ctx: &Context, prompt: &mut StartupPrompt, events: Vec) -> (StartupPromptResponse, FullOutput) { + let mut response = StartupPromptResponse::Pending; + let mut output = ctx.run_ui(RawInput { screen_rect: Some(screen()), events, ..RawInput::default() }, |ui| { + response = prompt.show(ui.ctx()); + }); + output.textures_delta.clear(); // Headless test: no renderer consumes the font atlas. + (response, output) + } + + fn collect_text(shape: &egui::Shape, out: &mut Vec) { + match shape { + egui::Shape::Vec(shapes) => { + for shape in shapes { + collect_text(shape, out); + } + } + egui::Shape::Text(text) => out.push(text.galley.text().to_owned()), + _ => {} + } + } + + /// Every string the frame actually painted, one per line. + fn painted_text(output: &FullOutput) -> String { + let mut texts = Vec::new(); + for clipped in &output.shapes { + collect_text(&clipped.shape, &mut texts); + } + texts.join("\n") + } + + /// The rectangle fills painted this frame, so the card and alert surfaces can be told apart. + fn rect_fills(output: &FullOutput) -> Vec<(Rect, Color32)> { + fn collect(shape: &egui::Shape, out: &mut Vec<(Rect, Color32)>) { + match shape { + egui::Shape::Vec(shapes) => { + for shape in shapes { + collect(shape, out); + } + } + egui::Shape::Rect(rect) => out.push((rect.rect, rect.fill)), + _ => {} + } + } + let mut fills = Vec::new(); + for clipped in &output.shapes { + collect(&clipped.shape, &mut fills); + } + fills + } + + #[test] + fn creates_startup_prompt_dialog() { + let release = Release { version: crate::version::parse("0.2.0"), tag: "v0.2.0".to_string(), ..Release::default() }; + let prompt = StartupPrompt::new(&PHOTOCRAFT, release, "0.1.0"); + assert_eq!(prompt.app.name, "PhotoCraft"); + assert_eq!(prompt.running_version, "0.1.0"); + assert!(prompt.open); + } + + #[test] + fn download_prompt_paints_card_actions_and_stays_pending() { + let ctx = dark_context(); + let mut prompt = prompt_for(false); + // A new Area first measures itself invisibly; inspect the following visible frame. + let (sizing, _) = frame(&ctx, &mut prompt, Vec::new()); + assert_eq!(sizing, StartupPromptResponse::Pending); + let (response, output) = frame(&ctx, &mut prompt, Vec::new()); + assert_eq!(response, StartupPromptResponse::Pending); + assert!(prompt.open); + let painted = painted_text(&output); + for expected in [ + "PhotoCraft", + "UPDATE", + "v0.1.0 → 0.2.0", + "PhotoCraft 0.2.0 is available with the latest features and bug fixes.", + "What's new", + "DOWNLOAD", + "LATER", + ] { + assert!(painted.contains(expected), "missing {expected:?} in painted text {painted:?}"); + } + assert!(!painted.contains("UPDATE AND RESTART"), "download branch must not offer the self-update button: {painted:?}"); + let fills = rect_fills(&output); + let backdrop = fills.iter().any(|(rect, fill)| *fill == Color32::from_black_alpha(150) && rect.size() == screen().size()); + assert!(backdrop, "modal backdrop missing: {fills:?}"); + let card = fills.iter().any(|(_, fill)| *fill == DARK_CARD); + assert!(card, "dialog card missing: {fills:?}"); + } + + #[test] + fn self_updating_prompt_offers_update_and_restart() { + let ctx = dark_context(); + let mut prompt = prompt_for(true); + // A new Area first measures itself invisibly; inspect the following visible frame. + let (sizing, _) = frame(&ctx, &mut prompt, Vec::new()); + assert_eq!(sizing, StartupPromptResponse::Pending); + let (response, output) = frame(&ctx, &mut prompt, Vec::new()); + assert_eq!(response, StartupPromptResponse::Pending); + assert!(prompt.open); + let painted = painted_text(&output); + assert!(painted.contains("UPDATE AND RESTART"), "self-update button missing: {painted:?}"); + assert!(painted.contains("LATER"), "dismiss button missing: {painted:?}"); + assert!(!painted.contains("DOWNLOAD"), "self-updating copy must not offer the manual download button: {painted:?}"); + let card = rect_fills(&output).iter().any(|(_, fill)| *fill == DARK_CARD); + assert!(card, "dialog card missing"); + } + + #[test] + fn installing_prompt_paints_status_and_dismiss() { + let ctx = dark_context(); + let mut prompt = prompt_for(true); + prompt.is_installing = true; + prompt.status_message = Some("Couldn't reach GitHub".to_string()); + // A new Area first measures itself invisibly; inspect the following visible frame. + let (sizing, _) = frame(&ctx, &mut prompt, Vec::new()); + assert_eq!(sizing, StartupPromptResponse::Pending); + let (response, output) = frame(&ctx, &mut prompt, Vec::new()); + assert_eq!(response, StartupPromptResponse::Pending); + assert!(prompt.open); + let painted = painted_text(&output); + for expected in ["PhotoCraft", "Downloading and installing update…", "Couldn't reach GitHub", "DISMISS", "What's new"] { + assert!(painted.contains(expected), "missing {expected:?} in painted text {painted:?}"); + } + for absent in ["LATER", "DOWNLOAD", "UPDATE AND RESTART"] { + assert!(!painted.contains(absent), "unexpected {absent:?} while installing: {painted:?}"); + } + let fills = rect_fills(&output); + let alert = fills.iter().any(|(_, fill)| *fill == DARK_STATUS); + assert!(alert, "status alert frame missing: {fills:?}"); + } + + #[test] + fn escape_dismisses_open_prompt() { + let ctx = dark_context(); + let mut prompt = prompt_for(false); + let (first, _) = frame(&ctx, &mut prompt, Vec::new()); + assert_eq!(first, StartupPromptResponse::Pending); + assert!(prompt.open); + let escape = Event::Key { key: Key::Escape, physical_key: None, pressed: true, repeat: false, modifiers: Modifiers::NONE }; + let (dismissed, dismissed_output) = frame(&ctx, &mut prompt, vec![escape]); + assert_eq!(dismissed, StartupPromptResponse::Later); + assert!(!prompt.open); + // The dismissing frame still paints the dialog; the closed state applies from the next frame on. + assert!(!painted_text(&dismissed_output).is_empty(), "dismissing frame should have painted the dialog"); + let (later, closed_output) = frame(&ctx, &mut prompt, Vec::new()); + assert_eq!(later, StartupPromptResponse::Later); + assert!(closed_output.shapes.is_empty(), "closed prompt painted {} shapes", closed_output.shapes.len()); + } + + #[test] + fn closed_prompt_returns_later_without_painting() { + let ctx = dark_context(); + let mut prompt = prompt_for(false); + prompt.open = false; + let (response, output) = frame(&ctx, &mut prompt, Vec::new()); + assert_eq!(response, StartupPromptResponse::Later); + assert!(!prompt.open); + assert!(output.shapes.is_empty(), "closed prompt painted {} shapes", output.shapes.len()); + assert!(painted_text(&output).is_empty()); + } +} diff --git a/crates/update/src/distro.rs b/crates/update/src/distro.rs new file mode 100644 index 000000000..890805226 --- /dev/null +++ b/crates/update/src/distro.rs @@ -0,0 +1,108 @@ +//! Linux distributions: reading `os-release` and choosing the package formats that suit them. +//! +//! The distro decides the system package format, not which tools happen to be on `PATH`: Fedora +//! can have `apt-get` and `dpkg` installed (for building Debian images), and an `.rpm` system +//! must never be handed to apt. + +use crate::platform::PackageKind; + +/// The parts of `/etc/os-release` that say which distro family this is. +#[derive(Clone, Debug, Default, PartialEq, Eq)] +pub struct OsRelease { + /// `ID`, lower-cased (`fedora`, `ubuntu`, `opensuse-tumbleweed`, …). Empty when missing. + pub id: String, + /// `ID_LIKE`, lower-cased and split on spaces (`["rhel", "centos", "fedora"]`). + pub id_like: Vec, +} + +/// Reads `os-release` text (`KEY=value` lines, values optionally quoted). +pub fn parse_os_release(text: &str) -> OsRelease { + let mut out = OsRelease::default(); + for line in text.lines() { + let line = line.trim(); + if line.starts_with('#') { + continue; + } + let Some((key, value)) = line.split_once('=') else { continue }; + let value = unquote(value.trim()).to_ascii_lowercase(); + match key.trim() { + "ID" => out.id = value, + "ID_LIKE" => out.id_like = value.split_whitespace().map(str::to_string).collect(), + _ => {} + } + } + out +} + +fn unquote(v: &str) -> &str { + for q in ['"', '\''] { + if let Some(inner) = v.strip_prefix(q).and_then(|v| v.strip_suffix(q)) { + return inner; + } + } + v +} + +/// The distro's own package format: `.deb` for the Debian family, `.rpm` for the Fedora, Red Hat +/// and SUSE families, `.pkg.tar.zst` for the Arch Linux family, `None` for anything else +/// (unknown distros), which gets AppImages. +pub fn native_package(os: &OsRelease) -> Option { + std::iter::once(&os.id).chain(&os.id_like).find_map(|id| family(id)) +} + +fn family(id: &str) -> Option { + match id { + "debian" | "ubuntu" => Some(PackageKind::Deb), + "fedora" | "rhel" | "centos" | "suse" | "sles" => Some(PackageKind::Rpm), + _ if id.starts_with("opensuse") => Some(PackageKind::Rpm), + "arch" | "manjaro" | "endeavouros" | "garuda" | "cachyos" | "artix" => Some(PackageKind::ArchPkg), + _ => None, + } +} + +#[cfg(test)] +mod tests { + use super::*; + + const FEDORA: &str = r#"NAME="Fedora Linux" +VERSION="40 (Workstation Edition)" +ID=fedora +VERSION_ID=40 +PRETTY_NAME="Fedora Linux 40 (Workstation Edition)" +"#; + const UBUNTU: &str = r#"PRETTY_NAME="Ubuntu 24.04.1 LTS" +NAME="Ubuntu" +VERSION_ID="24.04" +ID=ubuntu +ID_LIKE=debian +"#; + const MINT: &str = "NAME=\"Linux Mint\"\nID=linuxmint\nID_LIKE=\"ubuntu debian\"\n"; + const ARCH: &str = "NAME=\"Arch Linux\"\nPRETTY_NAME=\"Arch Linux\"\nID=arch\nBUILD_ID=rolling\n"; + const TUMBLEWEED: &str = "NAME=\"openSUSE Tumbleweed\"\n# a comment\nID=\"opensuse-tumbleweed\"\nID_LIKE=\"opensuse suse\"\n"; + const ROCKY: &str = "NAME=\"Rocky Linux\"\nID=\"rocky\"\nID_LIKE=\"rhel centos fedora\"\n"; + const UNKNOWN: &str = "NAME=\"Some OS\"\nID=someos\n"; + + fn native(text: &str) -> Option { + native_package(&parse_os_release(text)) + } + + #[test] + fn os_release_is_parsed() { + assert_eq!(parse_os_release(FEDORA), OsRelease { id: "fedora".into(), id_like: vec![] }); + assert_eq!(parse_os_release(MINT), OsRelease { id: "linuxmint".into(), id_like: vec!["ubuntu".into(), "debian".into()] }); + assert_eq!(parse_os_release("ID='opensuse-leap'\n").id, "opensuse-leap"); + assert_eq!(parse_os_release(""), OsRelease::default()); + } + + #[test] + fn the_distro_family_picks_the_package_format() { + assert_eq!(native(FEDORA), Some(PackageKind::Rpm)); + assert_eq!(native(ROCKY), Some(PackageKind::Rpm)); + assert_eq!(native(TUMBLEWEED), Some(PackageKind::Rpm)); + assert_eq!(native(UBUNTU), Some(PackageKind::Deb)); + assert_eq!(native(MINT), Some(PackageKind::Deb)); + assert_eq!(native(ARCH), Some(PackageKind::ArchPkg)); + assert_eq!(native(UNKNOWN), None); + assert_eq!(native(""), None); + } +} diff --git a/crates/update/src/http.rs b/crates/update/src/http.rs new file mode 100644 index 000000000..963eed1e5 --- /dev/null +++ b/crates/update/src/http.rs @@ -0,0 +1,1124 @@ +//! HTTPS requests: small JSON/text answers, redirect probes, and verified downloads. + +use std::io::{Read, Write}; +use std::path::Path; +use std::sync::atomic::{AtomicU8, Ordering}; + +use crate::protocol::{CANCELLED, PAUSED}; +use std::time::Duration; + +use sha2::{Digest, Sha256}; + +/// Largest JSON or text answer we read. +const MAX_SMALL: u64 = 4 << 20; + +/// What a GET of a small resource produced. +#[derive(Debug, PartialEq, Eq)] +pub enum Fetched { + Body { + text: String, + etag: Option, + }, + /// `If-None-Match` matched: the saved copy is current. + NotModified, + NotFound, + /// GitHub's API refused: out of quota until `reset` (Unix seconds), if it said. + RateLimited { + reset: Option, + }, +} + +#[derive(Clone)] +pub struct Http { + small: ureq::Agent, + /// ArtCraft's release feed: a short timeout, so a slow server never holds up a check. + feed: ureq::Agent, + no_redirect: ureq::Agent, + download: ureq::Agent, + direct: ureq::Agent, + direct_download: ureq::Agent, + token: Option, +} + +/// Why ArtCraft's release feed couldn't be read. +#[derive(Debug)] +pub struct FeedError { + pub why: String, + /// The server answered HTTP 429 Too Many Requests. + pub throttled: bool, + /// The server's `Retry-After`, in seconds. + pub retry_after: Option, +} + +/// `http://:` and an optional path: the host must be exactly 127.0.0.1, +/// localhost or [::1] (no `user@` in front of another host), with a numeric port. +pub fn is_loopback(url: &str) -> bool { + let Some(rest) = url.strip_prefix("http://") else { return false }; + let authority = rest.split(['/', '?', '#']).next().unwrap_or_default(); + let Some((host, port)) = authority.rsplit_once(':') else { return false }; + matches!(host, "127.0.0.1" | "localhost" | "[::1]") && !port.is_empty() && port.parse::().is_ok() +} + +impl Http { + /// `user_agent` names the product and its version (`PhotoCraft/0.9.0`) and nothing about the + /// person using it. + pub fn new(user_agent: &str) -> Result { + // The OS checks each certificate chain itself: Windows fetches roots it hasn't stored yet + // and trusts what antivirus HTTPS scanning installs. Reading the store ourselves missed + // both ("invalid peer certificate: UnknownIssuer"). + let tls = || ureq::tls::TlsConfig::builder().root_certs(ureq::tls::RootCerts::PlatformVerifier).build(); + // Chosen once: the proxy environment variables, else the OS proxy setting. + let proxy = crate::proxy::pick(); + let small = ureq::Agent::config_builder() + .tls_config(tls()) + .proxy(proxy.clone()) + .timeout_global(Some(Duration::from_secs(20))) + .http_status_as_error(false) + .user_agent(user_agent) + .build() + .new_agent(); + let feed = ureq::Agent::config_builder() + .tls_config(tls()) + .proxy(proxy.clone()) + .timeout_connect(Some(Duration::from_secs(5))) + .timeout_global(Some(Duration::from_secs(10))) + .http_status_as_error(false) + .user_agent(user_agent) + .build() + .new_agent(); + let no_redirect = ureq::Agent::config_builder() + .tls_config(tls()) + .proxy(proxy.clone()) + .timeout_global(Some(Duration::from_secs(20))) + .http_status_as_error(false) + .max_redirects(0) + .max_redirects_will_error(false) + .user_agent(user_agent) + .build() + .new_agent(); + let download = ureq::Agent::config_builder() + .tls_config(tls()) + .proxy(proxy.clone()) + .timeout_connect(Some(Duration::from_secs(20))) + .timeout_recv_response(Some(Duration::from_secs(60))) + // ureq has no idle timeout: cap each attempt instead, and resume where it stopped. + .timeout_recv_body(Some(ATTEMPT_BODY_TIME)) + .http_status_as_error(false) + .user_agent(user_agent) + .build() + .new_agent(); + let direct = ureq::Agent::config_builder() + .proxy(None) + .tls_config(tls()) + .timeout_connect(Some(Duration::from_secs(5))) + .timeout_global(Some(Duration::from_secs(20))) + .http_status_as_error(false) + .user_agent(user_agent) + .build() + .new_agent(); + let direct_download = ureq::Agent::config_builder() + .proxy(None) + .tls_config(tls()) + .timeout_connect(Some(Duration::from_secs(20))) + .timeout_recv_response(Some(Duration::from_secs(60))) + .timeout_recv_body(Some(ATTEMPT_BODY_TIME)) + .http_status_as_error(false) + .user_agent(user_agent) + .build() + .new_agent(); + Ok(Self { small, feed, no_redirect, download, direct, direct_download, token: None }) + } + + /// An optional token raises GitHub's limit from 60 to 5000 requests an hour. Only ever sent + /// to api.github.com. Blank means none. + #[must_use] + pub fn with_github_token(mut self, token: Option) -> Self { + self.token = token.map(|t| t.trim().to_string()).filter(|t| !t.is_empty()); + self + } + + /// GET a GitHub API resource, revalidating with `etag` when given. + pub fn get_api(&self, url: &str, etag: Option<&str>) -> Result { + let mut req = self.small.get(url).header("Accept", "application/vnd.github+json").header("X-GitHub-Api-Version", "2022-11-28"); + if let Some(tag) = etag { + req = req.header("If-None-Match", tag); + } + if let Some(token) = &self.token + && url.starts_with("https://api.github.com/") + { + req = req.header("Authorization", &format!("Bearer {token}")); + } + let mut resp = req.call().map_err(|e| format!("couldn't reach GitHub ({e})"))?; + let header = |name: &str| resp.headers().get(name).and_then(|v| v.to_str().ok()).map(str::to_string); + let status = resp.status().as_u16(); + match status { + 200 => { + let etag = header("etag"); + let text = read_text_capped(&mut resp, MAX_SMALL).map_err(|e| format!("unreadable answer from GitHub ({e})"))?; + Ok(Fetched::Body { text, etag }) + } + 304 => Ok(Fetched::NotModified), + 404 => Ok(Fetched::NotFound), + 403 | 429 => { + let remaining = header("x-ratelimit-remaining"); + let reset = header("x-ratelimit-reset").and_then(|r| r.trim().parse::().ok()); + let retry_after = header("retry-after").and_then(|r| r.trim().parse::().ok()); + match refused_until(status, remaining.as_deref(), reset, retry_after, crate::now()) { + Some(reset) => Ok(Fetched::RateLimited { reset }), + None => Err(format!("GitHub refused the request (HTTP {status})")), + } + } + s => Err(format!("GitHub answered HTTP {s}")), + } + } + + /// ArtCraft's release feed, or why it couldn't be read. Never takes more than ~10 seconds. + pub fn get_feed(&self, url: &str) -> Result { + let failed = |why: String| FeedError { why, throttled: false, retry_after: None }; + let agent = if is_loopback(url) { &self.direct } else { &self.feed }; + let mut resp = + agent.get(url).header("Accept", "application/json").call().map_err(|e| failed(format!("couldn't reach {} ({e})", host(url))))?; + // Seconds only; an HTTP date falls back to our own backoff. + let retry_after = resp.headers().get("retry-after").and_then(|v| v.to_str().ok()).and_then(|r| r.trim().parse::().ok()); + match resp.status().as_u16() { + 200 => read_text_capped(&mut resp, MAX_SMALL).map_err(|e| failed(format!("unreadable answer ({e})"))), + s => Err(FeedError { why: format!("{} answered HTTP {s}", host(url)), throttled: s == 429, retry_after }), + } + } + + /// GET a small text file (following redirects). + pub fn get_text(&self, url: &str) -> Result, String> { + let agent = if is_loopback(url) { &self.direct } else { &self.small }; + let mut resp = agent.get(url).call().map_err(|e| format!("couldn't reach {} ({e})", host(url)))?; + match resp.status().as_u16() { + 200 => read_text_capped(&mut resp, MAX_SMALL).map(Some).map_err(|e| format!("unreadable answer ({e})")), + 404 => Ok(None), + s => Err(format!("{} answered HTTP {s}", host(url))), + } + } + + /// GET a small binary file (following redirects). `Ok(None)` for a 404. A file of more than + /// `max` bytes is refused: by its announced length before a byte is read, and by a hard limit + /// on the body when the length wasn't announced or was wrong. + pub fn get_bytes(&self, url: &str, max: usize) -> Result>, String> { + let agent = if is_loopback(url) { &self.direct } else { &self.small }; + let mut resp = agent.get(url).call().map_err(|e| format!("couldn't reach {} ({e})", host(url)))?; + match resp.status().as_u16() { + 200 => { + let announced = resp.headers().get("content-length").and_then(|v| v.to_str().ok()).and_then(|v| v.parse::().ok()); + let too_big = || format!("{} sent more than {max} bytes", host(url)); + if announced.is_some_and(|n| n > max as u64) { + return Err(too_big()); + } + let body = read_bytes_capped(&mut resp, max) + .map_err(|e| if e.contains("larger than") { too_big() } else { format!("unreadable answer ({e})") })?; + Ok(Some(body)) + } + 404 => Ok(None), + s => Err(format!("{} answered HTTP {s}", host(url))), + } + } + + /// Where `url` redirects to, without following it. + pub fn redirect_target(&self, url: &str) -> Result, String> { + let resp = self.no_redirect.head(url).call().map_err(|e| format!("couldn't reach {} ({e})", host(url)))?; + match resp.status().as_u16() { + 300..=399 => Ok(resp.headers().get("location").and_then(|v| v.to_str().ok()).map(str::to_string)), + 404 => Ok(None), + s => Err(format!("{} answered HTTP {s}", host(url))), + } + } + + /// Downloads `url` to `dest`, checking it against the supplied SHA-256 checksum (and always + /// against `size`, when known). The caller must provide a published checksum. + /// + /// Tolerant by design: an interrupted or stalled transfer resumes where it stopped (HTTP range + /// requests) with backoff; a paused download, or one left by an earlier run, continues from + /// its partial file; a file that fails its checksum is fetched once more from scratch before + /// giving up. When the connection stays down, the download ends paused (`PAUSED`), never + /// losing what it has. `stop` pauses or cancels it between reads. + pub fn download(&self, url: &str, dest: &Path, size: u64, sha256: &str, stop: &Stop, progress: &mut dyn FnMut(Transfer)) -> Result<(), String> { + if sha256.len() != 64 || !sha256.bytes().all(|b| b.is_ascii_hexdigit()) { + return Err("the release publishes no valid checksum for this file, so it wasn't downloaded".into()); + } + if let Some(dir) = dest.parent() { + std::fs::create_dir_all(dir).map_err(|e| format!("couldn't create {} ({e})", dir.display()))?; + } + // A finished copy from an earlier attempt that never got installed. Marking it as just + // used, before checking it, keeps `clean_downloads` away from it while it's installed. + if dest.is_file() && touch(dest) && matches_published(dest, sha256) { + progress(Transfer::Bytes { done: size, total: size }); + return Ok(()); + } + // The partial file is named after the release file and its size; if it still turns out + // to belong to something else, the checksum catches it and the download starts over. + let part = part_path(dest, size); + let mut fresh_start_used = false; + loop { + if let Err(e) = self.fetch_resuming(url, &part, size, stop, progress) { + if e == CANCELLED { + let _ = std::fs::remove_file(&part); + } else if e != PAUSED { + log::warn!("download of {url} failed: {e}"); + } + return Err(e); + } + if matches_published(&part, sha256) { + if let Err(e) = rename_retrying(&part, dest, stop) { + if e == CANCELLED { + let _ = std::fs::remove_file(&part); + } + return Err(e); + } + return Ok(()); + } + let _ = std::fs::remove_file(&part); + if fresh_start_used { + return Err("the download doesn't match its published checksum, so it wasn't installed".into()); + } + // A resumed file can be spliced wrongly by a misbehaving proxy: try once from zero. + log::warn!("checksum mismatch for {url}; downloading again from the start"); + fresh_start_used = true; + } + } + + /// Fetches `url` into `part`, resuming from whatever `part` already holds. Retries while the + /// transfer keeps making progress; after several attempts in a row get nowhere, it stops and + /// reports `PAUSED` so the user can resume later. + fn fetch_resuming(&self, url: &str, part: &Path, size: u64, stop: &Stop, progress: &mut dyn FnMut(Transfer)) -> Result<(), String> { + let mut fruitless = 0u32; + loop { + stop.check()?; + let have = std::fs::metadata(part).map(|m| m.len()).unwrap_or(0); + if fruitless >= MAX_FRUITLESS_ATTEMPTS { + log::warn!("{url}: no progress after {fruitless} attempts; pausing"); + return Err(PAUSED.into()); + } + if fruitless > 0 { + progress(Transfer::Reconnecting { done: have, total: size }); + // 1 s, 2 s, 4 s … 30 s, staying responsive to Pause and Cancel. + let wait = Duration::from_secs((1u64 << (fruitless - 1).min(5)).min(30)); + let until = std::time::Instant::now() + wait; + while std::time::Instant::now() < until { + stop.check()?; + std::thread::sleep(Duration::from_millis(100)); + } + } + if size > 0 && have == size { + return Ok(()); + } + if size > 0 && have > size { + std::fs::remove_file(part).map_err(|e| format!("couldn't save the download ({e})"))?; + continue; + } + match self.attempt(url, part, have, size, stop, progress) { + Ok(()) => return Ok(()), + Err(AttemptError::Stopped(e)) => return Err(e), + Err(AttemptError::Fatal(e)) => return Err(e), + Err(AttemptError::Retry { error, progressed }) => { + log::info!("{url}: {error}; retrying"); + fruitless = if progressed { 1 } else { fruitless + 1 }; + } + } + } + } + + /// One request: appends to `part` from byte `have`. + fn attempt(&self, url: &str, part: &Path, have: u64, size: u64, stop: &Stop, progress: &mut dyn FnMut(Transfer)) -> Result<(), AttemptError> { + let retry = |error: String, progressed: bool| AttemptError::Retry { error, progressed }; + // Binaries are already compressed; ask for the bytes exactly as published. + let agent = if is_loopback(url) { &self.direct_download } else { &self.download }; + let mut req = agent.get(url).header("Accept-Encoding", "identity"); + if have > 0 { + req = req.header("Range", &format!("bytes={have}-")); + } + let mut resp = match req.call() { + Ok(r) => r, + Err(e) => return Err(retry(format!("couldn't connect: {e}"), false)), + }; + let status = resp.status().as_u16(); + let header = |name: &str| resp.headers().get(name).and_then(|v| v.to_str().ok()).map(str::to_string); + let (start, total) = match status { + 200 => (0, header("content-length").and_then(|v| v.parse::().ok()).filter(|n| *n > 0).unwrap_or(size)), + 206 => match header("content-range").as_deref().and_then(parse_content_range) { + Some((start, Some(total))) => (start, total), + Some((start, None)) => (start, size), + None => return Err(retry("the server sent an unreadable partial response".into(), false)), + }, + // The range is past the end: the part file is already complete (or wrong; the + // checksum decides). + 416 if have > 0 => return Ok(()), + 404 | 410 => return Err(AttemptError::Fatal("the file is no longer on the release page".into())), + 401 | 403 => return Err(retry(format!("the server refused the download (HTTP {status})"), false)), + s => return Err(retry(format!("HTTP {s}"), false)), + }; + if size > 0 && total != size { + return Err(AttemptError::Fatal(format!("the server offered {total} bytes, but the release lists {size}"))); + } + if let Some(enc) = header("content-encoding") + && !enc.eq_ignore_ascii_case("identity") + { + return Err(AttemptError::Fatal(format!("unexpected Content-Encoding '{enc}' on binary artifact download"))); + } + let limit = if total > 0 { total } else { MAX_DOWNLOAD }; + let appends = status == 206 && start == have; + if appends && have > limit { + return Err(AttemptError::Fatal(format!("the download exceeds the permitted size of {limit} bytes"))); + } + // Open the part file at the right place: a 200 restarts it, a 206 continues it. + let mut file = match (status, start) { + (206, s) if s == have => std::fs::OpenOptions::new().append(true).open(part), + _ => std::fs::File::create(part), + } + .map_err(|e| AttemptError::Fatal(format!("couldn't save the download ({e})")))?; + let mut done = if status == 206 && start == have { have } else { 0 }; + // We check the length and checksum ourselves; the reader's limit is only a backstop + // against an endless body. (ureq errors once the limit is *reached*, so leave headroom.) + let remaining = limit.saturating_sub(done); + let mut reader = resp.body_mut().with_config().limit(remaining.saturating_add(1 << 20)).reader(); + let mut buf = vec![0u8; 256 * 1024]; + let at_start = done; + progress(Transfer::Bytes { done, total }); + loop { + stop.check().map_err(AttemptError::Stopped)?; + let n = match reader.read(&mut buf) { + Ok(n) => n, + Err(e) => { + let _ = file.sync_all(); + return Err(retry(format!("the connection dropped: {e}"), done > at_start)); + } + }; + if n == 0 { + break; + } + let next_done = done.saturating_add(n as u64); + if next_done > limit { + return Err(AttemptError::Fatal(format!("the server sent more than the permitted size of {limit} bytes"))); + } + let chunk = buf.get(..n).ok_or_else(|| AttemptError::Fatal("read past the buffer".into()))?; + file.write_all(chunk).map_err(|e| AttemptError::Fatal(format!("couldn't save the download ({e}). Is the disk full?")))?; + done = next_done; + progress(Transfer::Bytes { done, total }); + } + file.sync_all().map_err(|e| AttemptError::Fatal(format!("couldn't save the download ({e})")))?; + if total > 0 && done < total { + return Err(retry(format!("the connection closed early ({done} of {total} bytes)"), done > at_start)); + } + Ok(()) + } +} + +/// Attempts in a row without any new bytes before pausing (about two minutes of backoff). +const MAX_FRUITLESS_ATTEMPTS: u32 = 8; +/// The longest one request may spend receiving; the next request resumes. +const ATTEMPT_BODY_TIME: Duration = Duration::from_secs(5 * 60); +/// Backstop when the server doesn't say how big the file is. +const MAX_DOWNLOAD: u64 = 16 << 30; + +/// What a download reports as it goes. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Transfer { + Bytes { + done: u64, + total: u64, + }, + /// The connection dropped; waiting before trying again. + Reconnecting { + done: u64, + total: u64, + }, +} + +/// Asks a running download to pause (keeping its partial file) or cancel (deleting it). +#[derive(Debug, Default)] +pub struct Stop(AtomicU8); + +impl Stop { + const RUN: u8 = 0; + const PAUSE: u8 = 1; + const CANCEL: u8 = 2; + + pub fn pause(&self) { + let _ = self.0.compare_exchange(Self::RUN, Self::PAUSE, Ordering::SeqCst, Ordering::SeqCst); + } + + pub fn cancel(&self) { + self.0.store(Self::CANCEL, Ordering::SeqCst); + } + + /// `Err(PAUSED | CANCELLED)` once asked to stop. + pub fn check(&self) -> Result<(), String> { + match self.0.load(Ordering::SeqCst) { + Self::PAUSE => Err(PAUSED.into()), + Self::CANCEL => Err(CANCELLED.into()), + _ => Ok(()), + } + } +} + +/// Whether a file on disk is the published one: its SHA-256 matches. +fn matches_published(path: &Path, sha256: &str) -> bool { + sha256_file(path).is_ok_and(|h| h.eq_ignore_ascii_case(sha256)) +} + +/// How much of a download is already on disk (a paused or interrupted one). +pub fn partial_bytes(dest: &Path, size: u64) -> Option { + std::fs::metadata(part_path(dest, size)).ok().map(|m| m.len()).filter(|n| *n > 0) +} + +/// Renames `from` to `to`, retrying for a few seconds while Windows reports the file busy: an +/// antivirus scanner often holds a just-written installer open, and the rename then fails with +/// "Access denied" (os error 5) or a sharing violation (os error 32). `stop` pauses or cancels +/// it between tries. +fn rename_retrying(from: &Path, to: &Path, stop: &Stop) -> Result<(), String> { + let mut wait = Duration::from_millis(100); + loop { + stop.check()?; + match std::fs::rename(from, to) { + Err(e) if cfg!(windows) && matches!(e.raw_os_error(), Some(5 | 32)) && wait <= Duration::from_secs(4) => { + log::info!("{} is busy ({e}); retrying the rename", to.display()); + std::thread::sleep(wait); + wait = wait.saturating_mul(2); + } + result => return result.map_err(|e| format!("couldn't save the download ({e})")), + } + } +} + +/// Sets a file's modification time to now. +fn touch(path: &Path) -> bool { + std::fs::File::options().write(true).open(path).and_then(|f| f.set_modified(std::time::SystemTime::now())).is_ok() +} + +/// Whether a finished download is waiting at `dest`. Judged by size alone: `download` checks it +/// against the published checksum before anything uses it. +pub fn finished(dest: &Path, size: u64) -> bool { + std::fs::metadata(dest).is_ok_and(|m| m.is_file() && m.len() == size) +} + +/// Removes leftovers from the download folder: partial files in an old naming scheme, paused +/// downloads untouched for `partial_max_age`, and finished ones (kept after a failed install) +/// untouched for `finished_max_age` (they're for releases long since replaced, or given up on). +/// Files written within the last minute are always kept, so a running download is never touched. +pub fn clean_downloads(dir: &Path, partial_max_age: Duration, finished_max_age: Duration) { + let Ok(entries) = std::fs::read_dir(dir) else { return }; + let now = std::time::SystemTime::now(); + for entry in entries.flatten() { + let path = entry.path(); + let Ok(meta) = entry.metadata() else { continue }; + if !meta.is_file() { + continue; + } + let age = meta.modified().ok().and_then(|m| now.duration_since(m).ok()).unwrap_or_default(); + if age < Duration::from_secs(60) { + continue; + } + let name = entry.file_name().to_string_lossy().into_owned(); + // Current partial files are `..part`. + let current_part = name + .strip_suffix(".part") + .and_then(|stem| stem.rsplit_once('.')) + .is_some_and(|(_, size)| !size.is_empty() && size.bytes().all(|b| b.is_ascii_digit())); + let legacy_part = name.ends_with(".part") && !current_part; + let max_age = if name.ends_with(".part") { partial_max_age } else { finished_max_age }; + if legacy_part || age > max_age { + log::info!("removing stale download {}", path.display()); + let _ = std::fs::remove_file(&path); + } + } +} + +/// Throws away a paused download, and a finished one kept for another try at installing. +pub fn discard(dest: &Path, size: u64) { + let _ = std::fs::remove_file(part_path(dest, size)); + let _ = std::fs::remove_file(dest); +} + +enum AttemptError { + /// Paused or cancelled. + Stopped(String), + /// Won't get better by trying again. + Fatal(String), + Retry { + error: String, + progressed: bool, + }, +} + +/// `..part`: where an unfinished download of `dest` is kept. +fn part_path(dest: &Path, size: u64) -> std::path::PathBuf { + let tag = size.to_string(); + let mut name = dest.file_name().map(std::ffi::OsStr::to_os_string).unwrap_or_default(); + name.push(format!(".{tag}.part")); + dest.with_file_name(name) +} + +/// `bytes 100-199/1000` → (100, Some(1000)); `bytes 100-199/*` → (100, None). +fn parse_content_range(v: &str) -> Option<(u64, Option)> { + let rest = v.trim().strip_prefix("bytes ")?; + let (range, total) = rest.split_once('/')?; + let start = range.split_once('-')?.0.trim().parse().ok()?; + let total = if total.trim() == "*" { None } else { Some(total.trim().parse().ok()?) }; + Some((start, total)) +} + +pub fn hex(bytes: &[u8]) -> String { + use std::fmt::Write as _; + bytes.iter().fold(String::with_capacity(bytes.len() * 2), |mut s, b| { + let _ = write!(s, "{b:02x}"); + s + }) +} + +/// SHA-256 of a file on disk. +pub fn sha256_file(path: &Path) -> Result { + let mut file = std::fs::File::open(path).map_err(|e| format!("couldn't read {} ({e})", path.display()))?; + let mut hasher = Sha256::new(); + std::io::copy(&mut file, &mut hasher).map_err(|e| format!("couldn't read {} ({e})", path.display()))?; + Ok(hex(&hasher.finalize())) +} + +/// Whether a 403 or 429 from GitHub's API means "slow down", and until when (Unix seconds, when +/// it says). `None`: refused for some other reason. +/// +/// GitHub sends `x-ratelimit-reset` with every answer: it's when the hourly quota refills, which +/// only matters once `x-ratelimit-remaining` is 0. A secondary limit says how long to wait in +/// `Retry-After` (seconds) and leaves the hourly quota alone; without either, GitHub's advice is +/// to wait a minute. +fn refused_until(status: u16, remaining: Option<&str>, reset: Option, retry_after: Option, now: i64) -> Option> { + let retry = retry_after.map(|secs| now.saturating_add(secs.max(0))); + if remaining.map(str::trim) == Some("0") { + // `None` sorts first: whichever of the two is known, and the later if both are. + return Some(reset.max(retry)); + } + if retry.is_some() { + return Some(retry); + } + (status == 429).then_some(Some(now.saturating_add(60))) +} + +/// The host of `url` for error messages, or all of it when it has no scheme. +fn host(url: &str) -> &str { + url.split_once("://").and_then(|(_, rest)| rest.split('/').next()).unwrap_or(url) +} + +/// Reads a text body, rejecting one whose *decoded* size exceeds `max`. +/// +/// ureq's own `limit` guards the *compressed* stream: it sits below the transparent gzip decoder, +/// so a few hundred wire bytes can still expand far past the cap. So we cap the fully decoded +/// reader with `take` instead, one byte past the cap, and refuse an oversized answer outright — +/// before it is parsed — instead of truncating it into something that merely fails to parse +/// (CWE-409). Invalid UTF-8 stays an error, as before. +fn read_text_capped(resp: &mut ureq::http::Response, max: u64) -> Result { + let mut reader = resp.body_mut().with_config().reader().take(max.saturating_add(1)); + let mut text = String::new(); + reader.read_to_string(&mut text).map_err(|e| e.to_string())?; + if text.len() as u64 > max { + return Err(format!("the answer is larger than {max} bytes")); + } + Ok(text) +} + +/// Reads a binary body, rejecting one whose decoded size exceeds `max` (CWE-409). +fn read_bytes_capped(resp: &mut ureq::http::Response, max: usize) -> Result, String> { + let mut reader = resp.body_mut().with_config().reader().take((max as u64).saturating_add(1)); + let mut bytes = Vec::new(); + reader.read_to_end(&mut bytes).map_err(|e| e.to_string())?; + if bytes.len() > max { + return Err(format!("the answer is larger than {max} bytes")); + } + Ok(bytes) +} + +#[cfg(test)] +pub(crate) mod tests { + use super::*; + + const DAYS_30: Duration = Duration::from_secs(30 * 86_400); + const DAYS_7: Duration = Duration::from_secs(7 * 86_400); + + #[test] + fn host_is_what_follows_the_scheme() { + assert_eq!(host("https://api.github.com/repos/storytold/wordcraft"), "api.github.com"); + assert_eq!(host("http://127.0.0.1:8080"), "127.0.0.1:8080"); + assert_eq!(host("not a url"), "not a url"); + } + + #[test] + fn cleaning_keeps_current_partial_files_and_removes_leftovers() { + let dir = tempfile::tempdir().unwrap(); + let old = std::time::SystemTime::now() - Duration::from_secs(3600); + let make = |name: &str| { + let p = dir.path().join(name); + std::fs::write(&p, b"x").unwrap(); + std::fs::File::options().write(true).open(&p).unwrap().set_modified(old).unwrap(); + p + }; + let keep = make("photocraft-0.5.0-macos-universal.dmg.150000.part"); + let legacy = make("photocraft-0.5.0-macos-universal.part"); + let finished = make("photocraft-0.5.0-macos-universal.dmg"); + let fresh = dir.path().join("x.part"); + std::fs::write(&fresh, b"x").unwrap(); + clean_downloads(dir.path(), DAYS_30, DAYS_7); + assert!(keep.exists() && fresh.exists() && finished.exists()); + assert!(!legacy.exists()); + // A finished file goes sooner than a paused one. + clean_downloads(dir.path(), DAYS_30, Duration::from_secs(60)); + assert!(keep.exists() && !finished.exists()); + clean_downloads(dir.path(), Duration::from_secs(60), DAYS_7); + assert!(!keep.exists()); + } + + #[test] + fn a_kept_file_that_still_matches_is_not_fetched_again() { + let (body, sha) = body(); + let len = body.len() as u64; + let (url, requests) = serve(body.clone(), vec![Reply::Full, Reply::Full]); + let dir = tempfile::tempdir().unwrap(); + let dest = dir.path().join("file.bin"); + let http = Http::new(crate::TEST_AGENT).unwrap(); + http.download(&url, &dest, len, &sha, &Stop::default(), &mut |_| {}).unwrap(); + assert!(finished(&dest, len) && !finished(&dest, 0)); + // Old enough to be cleaned up, were it not about to be used. + let old = std::time::SystemTime::now() - Duration::from_secs(40 * 86_400); + std::fs::File::options().write(true).open(&dest).unwrap().set_modified(old).unwrap(); + let mut last = (0, 0); + let again = http.download(&url, &dest, len, &sha, &Stop::default(), &mut |t| { + if let Transfer::Bytes { done, total } = t { + last = (done, total); + } + }); + assert_eq!(again, Ok(())); + assert_eq!(last, (len, len)); + assert_eq!(requests.lock().unwrap().len(), 1); + // A cleanup while the installer runs leaves the reused file alone. + clean_downloads(dir.path(), DAYS_30, DAYS_7); + assert_eq!(std::fs::read(&dest).unwrap(), body); + discard(&dest, len); + assert!(!finished(&dest, len)); + } + + #[test] + fn a_kept_file_that_no_longer_matches_is_fetched_again() { + let (body, sha) = body(); + let len = body.len() as u64; + let (url, requests) = serve(body.clone(), vec![Reply::Full, Reply::Full]); + let dir = tempfile::tempdir().unwrap(); + let dest = dir.path().join("file.bin"); + let http = Http::new(crate::TEST_AGENT).unwrap(); + http.download(&url, &dest, len, &sha, &Stop::default(), &mut |_| {}).unwrap(); + // The same size, so only the checksum can tell. + std::fs::write(&dest, vec![b'x'; body.len()]).unwrap(); + http.download(&url, &dest, len, &sha, &Stop::default(), &mut |_| {}).unwrap(); + assert_eq!(std::fs::read(&dest).unwrap(), body); + assert_eq!(requests.lock().unwrap().len(), 2); + } + + #[test] + fn reads_content_ranges() { + assert_eq!(parse_content_range("bytes 100-199/1000"), Some((100, Some(1000)))); + assert_eq!(parse_content_range("bytes 5-9/*"), Some((5, None))); + assert_eq!(parse_content_range("items 1-2/3"), None); + assert_eq!(parse_content_range("bytes x-9/10"), None); + } + + use std::io::{BufRead, BufReader}; + use std::net::TcpListener; + use std::sync::{Arc, Mutex}; + + /// How the fake server answers one request. + #[derive(Clone, Copy, Debug)] + pub(crate) enum Reply { + /// The whole file. + Full, + /// Promise the whole file, send `n` bytes, hang up. + DropAfter(usize), + /// Honour `Range` with a 206. + Ranged, + /// The right length, wrong bytes. + Corrupt, + /// 404. + Missing, + /// The whole body, already a gzip stream, announced as `Content-Encoding: gzip`. + Gzipped, + } + + /// Serves `body` over plain HTTP following `script`, one reply per connection; records the + /// `Range` header each request carried. + pub(crate) fn serve(body: Vec, script: Vec) -> (String, Arc>>>) { + let listener = TcpListener::bind("127.0.0.1:0").unwrap(); + let url = format!("http://{}/file.bin", listener.local_addr().unwrap()); + let ranges = Arc::new(Mutex::new(Vec::new())); + let seen = ranges.clone(); + std::thread::spawn(move || { + for reply in script { + let Ok((mut stream, _)) = listener.accept() else { return }; + let mut range = None; + let mut reader = BufReader::new(stream.try_clone().unwrap()); + loop { + let mut line = String::new(); + if reader.read_line(&mut line).unwrap_or(0) == 0 || line == "\r\n" { + break; + } + if let Some(v) = line.to_ascii_lowercase().strip_prefix("range: bytes=") { + range = v.trim().trim_end_matches('-').parse::().ok(); + } + } + seen.lock().unwrap().push(range.map(|r| r.to_string())); + let len = body.len(); + let _ = match reply { + Reply::Full => { + write!(stream, "HTTP/1.1 200 OK\r\nContent-Length: {len}\r\nConnection: close\r\n\r\n").and_then(|()| stream.write_all(&body)) + } + Reply::DropAfter(n) => write!(stream, "HTTP/1.1 200 OK\r\nContent-Length: {len}\r\nConnection: close\r\n\r\n") + .and_then(|()| stream.write_all(&body[..n])), + Reply::Corrupt => write!(stream, "HTTP/1.1 200 OK\r\nContent-Length: {len}\r\nConnection: close\r\n\r\n") + .and_then(|()| stream.write_all(&vec![b'x'; len])), + Reply::Missing => write!(stream, "HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\nConnection: close\r\n\r\n"), + Reply::Gzipped => { + write!(stream, "HTTP/1.1 200 OK\r\nContent-Length: {len}\r\nContent-Encoding: gzip\r\nConnection: close\r\n\r\n") + .and_then(|()| stream.write_all(&body)) + } + Reply::Ranged => { + let from = range.unwrap_or(0); + write!( + stream, + "HTTP/1.1 206 Partial Content\r\nContent-Length: {}\r\nContent-Range: bytes {from}-{}/{len}\r\nConnection: close\r\n\r\n", + len - from, + len - 1 + ) + .and_then(|()| stream.write_all(&body[from..])) + } + }; + } + }); + (url, ranges) + } + + pub(crate) fn body() -> (Vec, String) { + let body: Vec = (0..700_000u32).map(|i| (i % 251) as u8).collect(); + let sha = hex(&Sha256::digest(&body)); + (body, sha) + } + + fn run(script: Vec) -> (Result, String>, Vec>) { + let (body, sha) = body(); + let (url, ranges) = serve(body.clone(), script); + let dir = tempfile::tempdir().unwrap(); + let dest = dir.path().join("file.bin"); + let http = Http::new(crate::TEST_AGENT).unwrap(); + let mut last = (0, 0); + let result = http.download(&url, &dest, body.len() as u64, &sha, &Stop::default(), &mut |t| { + if let Transfer::Bytes { done, total } = t { + last = (done, total); + } + }); + let result = result.map(|()| std::fs::read(&dest).unwrap()); + if result.is_ok() { + assert_eq!(last, (body.len() as u64, body.len() as u64)); + } + let seen = ranges.lock().unwrap().clone(); + (result, seen) + } + + #[test] + fn small_binary_files_are_read_whole_and_a_404_is_none() { + let (body, _) = body(); + let http = Http::new(crate::TEST_AGENT).unwrap(); + let (url, _) = serve(body.clone(), vec![Reply::Full]); + assert_eq!(http.get_bytes(&url, body.len()).unwrap(), Some(body.clone()), "a body of exactly the cap is fine"); + let (url, _) = serve(body, vec![Reply::Missing]); + assert_eq!(http.get_bytes(&url, 1 << 20).unwrap(), None); + } + + #[test] + fn a_file_over_the_cap_is_refused() { + let (body, _) = body(); + let http = Http::new(crate::TEST_AGENT).unwrap(); + let (url, _) = serve(body.clone(), vec![Reply::Full]); + let err = http.get_bytes(&url, body.len() - 1).unwrap_err(); + assert!(err.contains("more than"), "{err}"); + } + + #[test] + fn a_complete_body_of_exactly_the_published_size_succeeds() { + // The bug this guards: a reader limit equal to the size failed on the final read. + let (result, _) = run(vec![Reply::Full]); + assert_eq!(result.unwrap(), body().0); + } + + #[test] + fn a_dropped_connection_resumes_where_it_stopped() { + let (result, ranges) = run(vec![Reply::DropAfter(300_000), Reply::Ranged]); + assert_eq!(result.unwrap(), body().0); + assert_eq!(ranges, [None, Some("300000".to_string())]); + } + + #[test] + fn a_server_that_ignores_ranges_starts_over() { + let (result, _) = run(vec![Reply::DropAfter(123_456), Reply::Full]); + assert_eq!(result.unwrap(), body().0); + } + + #[test] + fn corrupt_data_is_fetched_again_and_never_kept() { + let (result, _) = run(vec![Reply::Corrupt, Reply::Full]); + assert_eq!(result.unwrap(), body().0); + let (result, _) = run(vec![Reply::Corrupt, Reply::Corrupt]); + assert!(result.unwrap_err().contains("checksum")); + } + + #[test] + fn pausing_keeps_the_partial_file_and_resuming_continues_it() { + let (body, sha) = body(); + let len = body.len() as u64; + let (url, ranges) = serve(body.clone(), vec![Reply::Full, Reply::Ranged]); + let dir = tempfile::tempdir().unwrap(); + let dest = dir.path().join("file.bin"); + let http = Http::new(crate::TEST_AGENT).unwrap(); + let stop = Stop::default(); + // Pause as soon as the first bytes arrive. + let err = http + .download(&url, &dest, len, &sha, &stop, &mut |t| { + if matches!(t, Transfer::Bytes { done, .. } if done > 0) { + stop.pause(); + } + }) + .unwrap_err(); + assert_eq!(err, PAUSED); + let kept = partial_bytes(&dest, len).unwrap(); + assert!(kept > 0 && kept < len, "{kept}"); + http.download(&url, &dest, len, &sha, &Stop::default(), &mut |_| {}).unwrap(); + assert_eq!(std::fs::read(&dest).unwrap(), body); + assert_eq!(ranges.lock().unwrap()[1], Some(kept.to_string())); + } + + #[test] + fn cancelling_throws_the_partial_file_away() { + let (body, sha) = body(); + let len = body.len() as u64; + let (url, _) = serve(body, vec![Reply::Full]); + let dir = tempfile::tempdir().unwrap(); + let dest = dir.path().join("file.bin"); + let stop = Stop::default(); + let err = Http::new(crate::TEST_AGENT) + .unwrap() + .download(&url, &dest, len, &sha, &stop, &mut |t| { + if matches!(t, Transfer::Bytes { done, .. } if done > 0) { + stop.cancel(); + } + }) + .unwrap_err(); + assert_eq!(err, CANCELLED); + assert_eq!(partial_bytes(&dest, len), None); + } + + #[test] + fn a_missing_file_fails_at_once() { + let (result, ranges) = run(vec![Reply::Missing, Reply::Full]); + assert!(result.unwrap_err().contains("no longer")); + assert_eq!(ranges.len(), 1); + } + + #[test] + fn a_partial_file_from_an_earlier_run_is_continued() { + let (body, sha) = body(); + let (url, ranges) = serve(body.clone(), vec![Reply::Ranged]); + let dir = tempfile::tempdir().unwrap(); + let dest = dir.path().join("file.bin"); + std::fs::write(part_path(&dest, body.len() as u64), &body[..500_000]).unwrap(); + assert_eq!(partial_bytes(&dest, body.len() as u64), Some(500_000)); + Http::new(crate::TEST_AGENT).unwrap().download(&url, &dest, body.len() as u64, &sha, &Stop::default(), &mut |_| {}).unwrap(); + assert_eq!(std::fs::read(&dest).unwrap(), body); + assert_eq!(*ranges.lock().unwrap(), [Some("500000".to_string())]); + assert!(!part_path(&dest, body.len() as u64).exists()); + } + + #[test] + fn an_oversized_partial_file_is_restarted_from_zero() { + let (body, sha) = body(); + let (url, ranges) = serve(body.clone(), vec![Reply::Full]); + let dir = tempfile::tempdir().unwrap(); + let dest = dir.path().join("file.bin"); + let size = body.len() as u64; + std::fs::write(part_path(&dest, size), vec![b'x'; body.len() + 1]).unwrap(); + Http::new(crate::TEST_AGENT).unwrap().download(&url, &dest, size, &sha, &Stop::default(), &mut |_| {}).unwrap(); + assert_eq!(std::fs::read(dest).unwrap(), body); + assert_eq!(*ranges.lock().unwrap(), [None]); + } + + #[test] + fn an_oversized_partial_file_that_cannot_be_removed_reports_an_error() { + let dir = tempfile::tempdir().unwrap(); + let part = part_path(&dir.path().join("file.bin"), 1); + // A directory cannot be removed as a file on Unix. On Windows, hold a file open + // without delete sharing: metadata remains readable, but removing it is refused. + #[cfg(unix)] + std::fs::create_dir(&part).unwrap(); + #[cfg(windows)] + let _locked_partial = { + use std::os::windows::fs::OpenOptionsExt; + std::fs::write(&part, b"too large").unwrap(); + // FILE_SHARE_READ | FILE_SHARE_WRITE, without FILE_SHARE_DELETE. + std::fs::File::options().read(true).share_mode(0x00000003).open(&part).unwrap() + }; + assert!(std::fs::metadata(&part).unwrap().len() > 1); + assert!(std::fs::remove_file(&part).is_err()); + let http = Http::new(crate::TEST_AGENT).unwrap(); + let stop = Stop::default(); + // Bound the regression: the old retry loop spins until Cancel, without a request. + let (finished, done) = std::sync::mpsc::channel(); + let error = std::thread::scope(|scope| { + let watchdog_stop = &stop; + scope.spawn(move || { + if done.recv_timeout(Duration::from_secs(10)).is_err() { + watchdog_stop.cancel(); + } + }); + let result = http.fetch_resuming("http://127.0.0.1:9/file.bin", &part, 1, &stop, &mut |_| {}); + let _ = finished.send(()); + result.unwrap_err() + }); + assert!(error.contains("couldn't save the download"), "{error}"); + assert!(part.exists(), "a blocked partial file must be left alone"); + } + + #[test] + fn an_oversized_feed_answer_is_refused_before_parsing() { + // More than MAX_SMALL (4 MiB) of decoded text: rejected outright, not truncated and parsed. + let big = vec![b' '; (MAX_SMALL as usize) + 1024]; + let (url, _) = serve(big, vec![Reply::Full]); + let http = Http::new(crate::TEST_AGENT).unwrap(); + let err = http.get_feed(&url).unwrap_err(); + assert!(err.why.contains("larger than"), "{}", err.why); + // A small answer still comes back intact. + let (url, _) = serve(b"{\"ok\":true}".to_vec(), vec![Reply::Full]); + assert_eq!(http.get_feed(&url).unwrap(), "{\"ok\":true}"); + } + + #[test] + fn a_gzip_bomb_answer_is_refused_before_parsing() { + // A few hundred bytes on the wire that decode to more than MAX_SMALL. ureq's own limit + // sits below the transparent gzip decoder, so only a cap on the decoded stream (CWE-409) + // refuses this; a wire-byte limit would hand the parser truncated JSON. + let big = vec![b'x'; (MAX_SMALL as usize) + 1024]; + let gz = gzip(&big); + assert!(gz.len() * 4 < MAX_SMALL as usize, "no longer a bomb: {} compressed bytes", gz.len()); + let (url, _) = serve(gz, vec![Reply::Gzipped]); + let http = Http::new(crate::TEST_AGENT).unwrap(); + let err = http.get_text(&url).unwrap_err(); + assert!(err.contains("larger than"), "{err}"); + // A small gzip answer still decodes intact. + let (url, _) = serve(gzip(b"{\"ok\":true}"), vec![Reply::Gzipped]); + assert_eq!(http.get_text(&url).unwrap().unwrap(), "{\"ok\":true}"); + } + + /// `data` as a real gzip stream (what `Reply::Gzipped` serves). + fn gzip(data: &[u8]) -> Vec { + let mut enc = flate2::write::GzEncoder::new(Vec::new(), flate2::Compression::default()); + enc.write_all(data).unwrap(); + enc.finish().unwrap() + } + + /// Live: a real Craft App release asset (`cargo test -p artcraft-launcher-native -- --ignored`). + #[test] + #[ignore = "needs network access"] + fn downloads_a_real_release_asset() { + let http = Http::new(crate::TEST_AGENT).unwrap(); + let Fetched::Body { text, .. } = http.get_api("https://api.github.com/repos/storytold/wordcraft/releases/latest", None).unwrap() else { + panic!("no release") + }; + let release = crate::release::parse_github_release(&text, "storytold/wordcraft").unwrap(); + let sums_url = &release.sums_asset().unwrap().url; + let sums = crate::release::parse_sha256sums(&http.get_text(sums_url).unwrap().unwrap()); + let asset = release.assets.iter().find(|a| a.name.ends_with("freebsd-x86_64.tar.gz")).unwrap(); + let sha = asset.sha256.clone().or_else(|| sums.iter().find(|(_, n)| *n == asset.name).map(|(h, _)| h.clone())).unwrap(); + let dir = tempfile::tempdir().unwrap(); + let dest = dir.path().join(&asset.name); + http.download(&asset.url, &dest, asset.size, &sha, &Stop::default(), &mut |_| {}).unwrap(); + assert_eq!(std::fs::metadata(&dest).unwrap().len(), asset.size); + } + + /// Answers one request with `head` (status line and headers) and an empty body. + fn answer_once(head: String) -> String { + let listener = TcpListener::bind("127.0.0.1:0").unwrap(); + let url = format!("http://{}/repos/storytold/photocraft/releases/latest", listener.local_addr().unwrap()); + std::thread::spawn(move || { + let Ok((mut stream, _)) = listener.accept() else { return }; + let mut reader = BufReader::new(stream.try_clone().unwrap()); + loop { + let mut line = String::new(); + if reader.read_line(&mut line).unwrap_or(0) == 0 || line == "\r\n" { + break; + } + } + let _ = write!(stream, "{head}Content-Length: 0\r\nConnection: close\r\n\r\n"); + }); + url + } + + #[test] + fn a_secondary_limit_waits_for_retry_after_not_the_hourly_reset() { + // GitHub's secondary limit: quota left, an hourly reset 50 minutes away, and "try again + // in a minute". That's a minute, not 50 minutes of "the hourly limit was reached". + let now = crate::now(); + let url = answer_once(format!( + "HTTP/1.1 403 Forbidden\r\nx-ratelimit-remaining: 41\r\nx-ratelimit-reset: {}\r\nretry-after: 60\r\n", + now + 50 * 60 + )); + let Fetched::RateLimited { reset: Some(until) } = Http::new(crate::TEST_AGENT).unwrap().get_api(&url, None).unwrap() else { + panic!("not rate limited") + }; + assert!((now + 60..now + 120).contains(&until), "limited for {} s", until - now); + } + + #[test] + fn an_empty_hourly_quota_waits_for_the_reset() { + let now = crate::now(); + let reset = now + 31 * 60; + let url = answer_once(format!("HTTP/1.1 403 Forbidden\r\nx-ratelimit-remaining: 0\r\nx-ratelimit-reset: {reset}\r\n")); + assert_eq!(Http::new(crate::TEST_AGENT).unwrap().get_api(&url, None).unwrap(), Fetched::RateLimited { reset: Some(reset) }); + } + + #[test] + fn github_refusals_are_read_the_way_github_documents_them() { + const NOW: i64 = 1_791_500_000; + // Out of quota: until the reset, or Retry-After if that's later. + assert_eq!(refused_until(403, Some("0"), Some(NOW + 1800), None, NOW), Some(Some(NOW + 1800))); + assert_eq!(refused_until(403, Some("0"), Some(NOW + 30), Some(90), NOW), Some(Some(NOW + 90))); + assert_eq!(refused_until(403, Some("0"), None, None, NOW), Some(None)); + // A secondary limit: Retry-After, never the hourly reset. + assert_eq!(refused_until(429, Some("12"), Some(NOW + 1800), Some(30), NOW), Some(Some(NOW + 30))); + // A 429 that doesn't say: a minute. + assert_eq!(refused_until(429, Some("12"), Some(NOW + 1800), None, NOW), Some(Some(NOW + 60))); + // A plain 403 (blocked, private, …) isn't a rate limit. + assert_eq!(refused_until(403, Some("12"), Some(NOW + 1800), None, NOW), None); + assert_eq!(refused_until(403, None, None, None, NOW), None); + } + + #[test] + fn partial_files_are_named_after_the_file_and_size() { + assert_eq!(part_path(Path::new("/d/photocraft.dmg"), 9), Path::new("/d/photocraft.dmg.9.part")); + } + + #[test] + fn get_bytes_rejects_oversized_payload() { + let listener = TcpListener::bind("127.0.0.1:0").unwrap(); + let port = listener.local_addr().unwrap().port(); + std::thread::spawn(move || { + let Ok((mut stream, _)) = listener.accept() else { return }; + let mut line = String::new(); + let mut reader = BufReader::new(stream.try_clone().unwrap()); + while reader.read_line(&mut line).unwrap_or(0) > 0 && line != "\r\n" { + line.clear(); + } + let body = vec![b'A'; 200]; + let _ = write!(stream, "HTTP/1.1 200 OK\r\nContent-Length: 200\r\nConnection: close\r\n\r\n"); + let _ = stream.write_all(&body); + }); + let http = Http::new(crate::TEST_AGENT).unwrap(); + let result = http.get_bytes(&format!("http://127.0.0.1:{port}/test.bin"), 100); + assert!(result.is_err()); + assert!(result.unwrap_err().contains("more than 100 bytes")); + } +} diff --git a/crates/update/src/install.rs b/crates/update/src/install.rs new file mode 100644 index 000000000..a92379f63 --- /dev/null +++ b/crates/update/src/install.rs @@ -0,0 +1,2870 @@ +//! Installing, updating, removing and starting Craft Apps. +//! +//! Apps go where their platform keeps applications, installed the platform's own way: +//! +//! - macOS: the signed `.app` from the release DMG into `/Applications`. +//! - Windows: the release MSI (Windows Installer) into `C:\Program Files\`. +//! - Linux: the distro's own package (`.deb` through apt on the Debian family, `.rpm` through +//! dnf/zypper/rpm on the Fedora, Red Hat and SUSE families, read from `os-release`) with the +//! system's polkit password prompt; otherwise a Flatpak, otherwise an AppImage in +//! `~/Applications` (the AppImage convention), with a menu entry and icon in +//! `~/.local/share` so it shows up like any other app. A Flatpak or AppImage launcher prefers +//! its own format. +//! - FreeBSD: the release's `/usr/local` tree, with a polkit prompt (or `~/.local` without one). +//! - Linux, inside the Flatpak launcher with no usable host Flatpak (none installed, or +//! `flatpak-spawn` not allowed): the release tarball, unpacked into the launcher's own folder +//! (`/apps/`) and run inside the same sandbox with `HOME` pointed at a `home/` +//! folder beside it, so the app's settings stay in the sandbox too. +//! +//! Every download is checked against its published SHA-256 before anything is unpacked, a +//! running app is never replaced, and a new copy is fully in place before the old one goes. + +use std::path::{Path, PathBuf}; + +use crate::protocol::{CANCELLED, INSTALL_CANCELLED, InstallLocations, JobPhase}; +use crate::release::{self, Asset, Release}; +use crate::{App, InstallSource, Installed, PackageKind, version}; + +use crate::http::{Http, Stop, Transfer}; +use crate::manifest::Record; +use crate::paths::Paths; +use crate::process; + +/// What an install needs from the backend. +pub struct Job<'a> { + pub http: &'a Http, + pub paths: &'a Paths, + pub stop: &'a Stop, + pub phase: &'a mut dyn FnMut(JobPhase), + pub install_locations: &'a InstallLocations, + /// Write over whatever [`occupied`] found. + pub replace: bool, +} + +/// Downloads and installs `asset` of `release`. Returns a record when the launcher owns the +/// result (portable ZIP, AppImage, macOS bundle, tarball); MSI installs are found in the registry. +/// `previous` is the launcher's record of the copy being replaced, if it installed it. +pub fn install( + app: &App, + release: &Release, + asset: &Asset, + kind: PackageKind, + existing: Option<&Installed>, + previous: Option<&Record>, + job: &mut Job<'_>, +) -> Result, String> { + install_at(app, release, asset, kind, existing, previous, job, None) +} + +type RecordCommit<'a> = Option<&'a mut dyn FnMut(&Record) -> Result<(), String>>; + +#[allow(clippy::too_many_arguments)] +pub fn install_at( + app: &App, + release: &Release, + asset: &Asset, + kind: PackageKind, + existing: Option<&Installed>, + previous: Option<&Record>, + job: &mut Job<'_>, + commit: RecordCommit<'_>, +) -> Result, String> { + #[cfg(windows)] + let mut commit = commit; + #[cfg(not(windows))] + let _ = commit; + #[allow(unused_variables)] + let destination = destination(app, kind, existing, previous, job.install_locations)?; + if let Some(inst) = existing + && inst.source.launcher_can_update() + && process::is_running(Path::new(&inst.location)) + { + return Err(format!("{} is running. Quit it, then try again.", app.name)); + } + bind_artifact_version(app, release, asset)?; + if !job.replace + && let Some(inst) = existing + && let (Some(rel_v), Some(inst_v)) = (release.version.as_ref(), inst.version.as_ref()) + && version::is_newer(inst_v, rel_v) + { + return Err(format!("Cannot update {} to {}: installed version {} is newer", app.name, release.version_label(), inst_v)); + } + log::info!("{}: starting {} to {} ({})", app.name, if existing.is_some() { "update" } else { "install" }, release.version_label(), kind.label()); + let file = download(release, asset, job)?; + log::info!("{}: download ready ({}), running installer...", app.name, file.display()); + // Past this point the install can't be interrupted halfway. + (job.phase)(JobPhase::Installing); + #[allow(unused_variables)] + let version = release.version_label(); + let result = match kind { + #[cfg(target_os = "macos")] + PackageKind::Dmg => { + macos::install_dmg(app, &file, existing, destination.as_deref(), job.replace, job.paths, &macos::Expect::release(release)) + .map(|path| Some(record(&version, kind, path, Vec::new(), false))) + } + #[cfg(windows)] + PackageKind::Msi => windows::run_msi(&file, destination.as_deref(), job.stop, &mut *job.phase).map(|_| None), + #[cfg(windows)] + PackageKind::PortableZip => { + crate::portable::install(app, &file, &version, previous, job.paths, destination.as_deref(), job.replace, &mut |record| { + if release.version.is_some() && crate::detect::windows::executable_version(&record.path).as_ref() != release.version.as_ref() { + return Err("The portable executable does not match the selected version. The installation was rolled back.".into()); + } + match commit.as_deref_mut() { + Some(commit) => commit(record), + None => Ok(()), + } + }) + .map(Some) + } + #[cfg(target_os = "linux")] + PackageKind::Deb | PackageKind::Rpm | PackageKind::ArchPkg | PackageKind::Flatpak => { + linux::install_package(kind, &file, job.stop, &mut *job.phase).map(|_| None) + } + #[cfg(target_os = "linux")] + PackageKind::AppImage => { + linux::install_appimage(&file, &asset.name, destination.as_deref(), previous, job.replace).map(|(path, replaced)| { + let files = linux::integrate(app, &path, &replaced, previous); + Some(record(&version, kind, path, files, false)) + }) + } + #[cfg(target_os = "freebsd")] + PackageKind::TarGz => freebsd::install_tree(app, &file, job.paths).map(|(bin, files, root)| Some(record(&version, kind, bin, files, root))), + #[cfg(target_os = "linux")] + PackageKind::TarGz => linux::install_tree(app, &file, job.paths).map(|path| Some(record(&version, kind, path, Vec::new(), false))), + other => Err(format!("{} isn't supported on this system", other.label())), + }; + match &result { + Ok(_) => log::info!("{}: successfully installed/updated to {}", app.name, release.version_label()), + Err(e) if e == CANCELLED => log::info!("{}: installation cancelled", app.name), + Err(e) => log::error!("{}: installation failed: {e}", app.name), + } + // A failed install keeps the download: the next try checks it against its published checksum + // again and skips the transfer. + if result.is_ok() { + let _ = std::fs::remove_file(&file); + } + // The system's own prompt was dismissed: the download itself wasn't cancelled. + result.map_err(|e| if e == CANCELLED { INSTALL_CANCELLED.to_string() } else { e }) +} + +/// Rejects an artifact whose own identity disagrees with the release that authorized it. +/// +/// A feed or GitHub converter supplies the release version, the asset filename, and the download +/// URL independently. The downgrade guard and the persisted record both trust `release.version`, +/// so a controller could advertise a new version while pointing at an older official artifact — +/// bypassing the guard and recording a false installed version (finding: version not bound to the +/// release). We require the version encoded in the asset filename and the release tag embedded in +/// the download URL to agree with the release version before any bytes are fetched. +pub(crate) fn bind_artifact_version(app: &App, release: &Release, asset: &Asset) -> Result<(), String> { + let mismatch = || format!("{}'s download does not match the advertised version {}.", app.name, release.version_label()); + let release_version = release.version.as_ref(); + + // The version in the asset filename (`---.`), when it carries one. + let stems: Vec<&str> = app.stems().collect(); + if let Some(release_version) = release_version + && let Some(parsed) = crate::platform::parse_asset_name(&asset.name, &stems) + && version::parse(&parsed.version).as_ref() != Some(release_version) + { + return Err(mismatch()); + } + + // The release tag and filename embedded in a release download URL (`.../releases/download//`). + if let Some(rest) = asset.url.split("/releases/download/").nth(1) { + let mut parts = rest.split('/'); + let tag = parts.next().unwrap_or(""); + let url_filename = parts.next().unwrap_or(""); + if tag.is_empty() + || release_version + .map_or(tag != release.tag, |release_version| tag != release.tag && version::parse(tag).as_ref() != Some(release_version)) + { + return Err(mismatch()); + } + if !url_filename.is_empty() && url_filename != asset.name { + return Err(format!("{}'s download URL filename does not match selected asset {}.", app.name, asset.name)); + } + } + Ok(()) +} + +/// What a new install would write over: something already at its destination that the launcher +/// didn't put there. +pub fn occupied( + app: &App, + #[cfg_attr(not(target_os = "linux"), allow(unused_variables))] asset: &Asset, + kind: PackageKind, + existing: Option<&Installed>, + previous: Option<&Record>, + locations: &InstallLocations, + paths: &Paths, +) -> Result, String> { + if existing.is_some() || previous.is_some() { + return Ok(None); + } + let folder = destination(app, kind, None, None, locations)?; + let target = match kind { + PackageKind::Msi => folder, + PackageKind::PortableZip => Some(folder.unwrap_or_else(|| paths.apps()).join(app.id)), + PackageKind::Dmg => folder.map(|f| f.join(format!("{}.app", app.name))), + #[cfg(target_os = "linux")] + PackageKind::AppImage => folder.or_else(crate::detect::linux::appimage_dir).map(|f| f.join(&asset.name)), + _ => None, + }; + Ok(target.filter(|t| std::fs::symlink_metadata(t).is_ok())) +} + +/// The folder a new copy goes in: beside the launcher's copy it replaces, else the user's choice, +/// else the platform default (`None`). macOS updates find their bundle's folder themselves. +fn destination( + app: &App, + kind: PackageKind, + existing: Option<&Installed>, + previous: Option<&Record>, + locations: &InstallLocations, +) -> Result, String> { + if kind == PackageKind::PortableZip && previous.is_some_and(|record| record.kind == PackageKind::PortableZip) { + return Ok(None); + } + if let Some(inst) = existing { + let exe = Path::new(&inst.location); + return Ok(match kind { + PackageKind::AppImage => previous.filter(|r| r.kind == kind).and_then(|r| r.path.parent()).map(Path::to_path_buf), + PackageKind::Msi => exe.parent().filter(|_| exe.is_absolute()).map(Path::to_path_buf), + _ => None, + }); + } + let Some(folder) = locations.folder(app.id) else { return Ok(None) }; + if !matches!(kind, PackageKind::Dmg | PackageKind::AppImage | PackageKind::Msi | PackageKind::PortableZip) || process::in_sandbox() { + return Err("custom install folders aren't supported on this system".into()); + } + let parent = selected_folder(folder, kind != PackageKind::Msi)?; + Ok(Some(if kind == PackageKind::Msi { parent.join(app.name) } else { parent })) +} + +fn selected_folder(folder: &str, writable: bool) -> Result { + let path = if folder == "~" { + dirs::home_dir().ok_or("no home folder")? + } else if let Some(relative) = folder.strip_prefix("~/") { + dirs::home_dir().ok_or("no home folder")?.join(relative) + } else { + PathBuf::from(folder) + }; + let invalid = |reason: &str| format!("couldn't use install folder {}: {reason}", path.display()); + if folder.chars().any(|c| c.is_control() || (cfg!(windows) && c == '"')) { + return Err(invalid("the path contains invalid characters")); + } + if !path.is_absolute() { + return Err(invalid("choose an absolute path")); + } + if !path.is_dir() { + return Err(invalid("the folder is unavailable. Create it or reconnect its drive first")); + } + if writable { + let probe = path.join(format!( + ".artcraft-launcher-write-test-{}-{}", + std::process::id(), + std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map_or(0, |d| d.as_nanos()) + )); + let output = std::fs::OpenOptions::new().write(true).create_new(true).open(&probe).map_err(|e| invalid(&e.to_string()))?; + drop(output); + std::fs::remove_file(&probe).map_err(|e| invalid(&e.to_string()))?; + } + Ok(path) +} + +#[cfg(any(windows, test))] +fn msi_folder_argument(path: &Path) -> Result { + let value = path.to_str().ok_or("unusual characters in a path")?; + if value.chars().any(|c| c == '"' || c.is_control()) { + return Err(format!("couldn't use install folder {value}: the path contains invalid characters")); + } + Ok(format!("INSTALLFOLDER=\"{value}\"")) +} + +/// Where `asset` is downloaded to (and its partial file kept while unfinished). +pub fn download_path(paths: &Paths, asset: &Asset) -> PathBuf { + paths.downloads().join(&asset.name) +} + +#[allow(dead_code)] +fn record(version: &str, kind: PackageKind, path: PathBuf, files: Vec, needs_root: bool) -> Record { + Record { version: version.to_string(), kind, path, installed_at: crate::now(), files, needs_root } +} + +/// The package format to install `app` with: the format of the copy that's there (an update +/// stays a `.deb` update), else the user's choice, else the best this system supports. +/// +/// Returns an empty `Vec` when `app` is already installed outside the launcher (such as an AUR +/// or repository Arch package) and must not be updated or replaced by the launcher. +pub fn choose_kinds(app: &App, existing: Option<&Installed>, record: Option<&Record>, preferred: &[PackageKind]) -> Vec { + let available = available_kinds(); + let can_pacman = available.contains(&PackageKind::ArchPkg) || can_pacman(); + choose_kinds_with(app, existing, record, preferred, &available, can_pacman) +} + +pub(crate) fn choose_kinds_with( + app: &App, + existing: Option<&Installed>, + record: Option<&Record>, + preferred: &[PackageKind], + available: &[PackageKind], + can_pacman: bool, +) -> Vec { + if let Some(inst) = existing { + let kind = match inst.source { + InstallSource::Launcher => record.map(|r| r.kind), + InstallSource::MacApp => Some(PackageKind::Dmg), + InstallSource::Msi => Some(PackageKind::Msi), + InstallSource::Deb => Some(PackageKind::Deb), + InstallSource::Rpm => Some(PackageKind::Rpm), + // The release's own Arch package is updated like a .deb. AUR builds and repository + // packages are updated outside the launcher (see `managed_elsewhere`); the launcher + // opens and removes them but never replaces them. + InstallSource::Pacman if is_release_arch_package(app, inst) => Some(PackageKind::ArchPkg), + InstallSource::Pacman => return Vec::new(), + InstallSource::Flatpak => Some(PackageKind::Flatpak), + InstallSource::AppImage => Some(PackageKind::AppImage), + InstallSource::Binary => Some(PackageKind::TarGz), + }; + if let Some(k) = kind.filter(|k| available.contains(k) || (*k == PackageKind::ArchPkg && can_pacman)) { + return vec![k]; + } + } + let mut kinds: Vec = preferred.iter().copied().filter(|k| available.contains(k)).collect(); + for k in available { + if !kinds.contains(k) { + kinds.push(*k); + } + } + kinds +} + +/// Whether this system has a working pacman installation and pacman database. +fn can_pacman() -> bool { + #[cfg(target_os = "linux")] + return find_in(&SYSTEM_BIN_DIRS, std::ffi::OsStr::new("pacman")).is_some() && crate::detect::linux::has_pacman_db(); + #[cfg(not(target_os = "linux"))] + return false; +} + +/// Whether a pacman install is the release's own Arch package rather than an AUR build. The +/// release names it by the bundle id (`ai.storyteller.`), which no AUR package uses +/// (they are ``, `-bin` and `-git`), so an AUR helper never mistakes it +/// for one of its own and rebuilds it from source. +pub fn is_release_arch_package(app: &App, installed: &Installed) -> bool { + installed.source == InstallSource::Pacman && installed.package.as_deref() == Some(&app.bundle_id()) +} + +/// Package formats this machine can install, best first. +pub fn available_kinds() -> Vec { + #[cfg(target_os = "macos")] + return vec![PackageKind::Dmg]; + #[cfg(windows)] + return vec![PackageKind::Msi, PackageKind::PortableZip]; + #[cfg(target_os = "linux")] + return linux::available_kinds(); + #[cfg(target_os = "freebsd")] + return vec![PackageKind::TarGz]; + #[allow(unreachable_code)] + Vec::new() +} + +/// Fetches `asset` into the download folder, checked against its published SHA-256: the feed's +/// or GitHub's per-file digest, else the release's `SHA256SUMS.txt`. A release with neither is +/// refused before anything is fetched: a file nothing vouches for is never installed (#90). +pub(crate) fn download(release: &Release, asset: &Asset, job: &mut Job<'_>) -> Result { + if !release::valid_file_name(&asset.name) { + return Err("the release has an unexpected file name".into()); + } + let sha = match &asset.sha256 { + Some(s) => Some(s.clone()), + None => published_checksum(release, asset, job.http)?, + }; + let Some(sha) = sha else { + return Err(format!( + "the release publishes no checksum for {}, so it can't be verified and wasn't installed. Try again later, or report it on GitHub.", + asset.name + )); + }; + let dest = download_path(job.paths, asset); + let phase = &mut *job.phase; + job.http.download(&asset.url, &dest, asset.size, &sha, job.stop, &mut |t| match t { + Transfer::Bytes { done, total } => phase(JobPhase::Downloading { done, total }), + Transfer::Reconnecting { done, total } => phase(JobPhase::Reconnecting { done, total }), + })?; + (job.phase)(JobPhase::Verifying); + Ok(dest) +} + +/// The file's line in the release's `SHA256SUMS.txt`. `Ok(None)` only when the release really +/// publishes no checksum for it; a list that exists but can't be fetched is an error, so a +/// network hiccup never downgrades the check. +fn published_checksum(release: &Release, asset: &Asset, http: &Http) -> Result, String> { + let Some(sums) = release.sums_asset() else { return Ok(None) }; + let Some(text) = http.get_text(&sums.url).map_err(|e| format!("couldn't fetch the release's checksums ({e}). Try again"))? else { + return Ok(None); + }; + Ok(release::parse_sha256sums(&text).into_iter().find(|(_, n)| *n == asset.name).map(|(h, _)| h)) +} + +/// When `installed` is owned by something the launcher doesn't drive, what to tell the user +/// instead of installing a second copy. Arch packages other than the release's own are removed +/// through pacman like any system package, but updated by whatever they came from: a system +/// update for a repository package (Omarchy's, Chaotic-AUR, ...), the user's AUR helper (which +/// builds as the user and can't run behind the password prompt) for anything else. +pub fn managed_elsewhere(app: &App, installed: &Installed, uninstall: bool) -> Option { + managed_elsewhere_with(app, installed, uninstall, pacman_repo) +} + +fn managed_elsewhere_with(app: &App, installed: &Installed, uninstall: bool, repo_fn: impl Fn(&str) -> Option) -> Option { + match installed.source { + InstallSource::Pacman if !uninstall && !is_release_arch_package(app, installed) => { + let pkg = installed.package.clone().unwrap_or_else(|| app.id.to_string()); + Some(pacman_update_hint(app, &pkg, repo_fn(&pkg).as_deref())) + } + InstallSource::Binary => Some(format!("{} was installed outside the launcher. Update or remove it the way it was installed.", app.name)), + _ => None, + } +} + +/// How to update an Arch package the launcher doesn't own: `repo` is the sync repository it +/// comes from, or `None` for a foreign (AUR) package. +fn pacman_update_hint(app: &App, pkg: &str, repo: Option<&str>) -> String { + let helper = ["paru", "yay"].into_iter().find(|h| which(h).is_some()); + let cmd = match helper { + Some(h) => format!("{h} -S {pkg}"), + None => format!("your AUR helper (for example: yay -S {pkg})"), + }; + if let Some(repo) = repo { + return format!("{} was installed from the {repo} repository or the AUR ({pkg}). Update it with {cmd} or sudo pacman -Syu", app.name); + } + format!("{} was installed from the AUR ({pkg}). Update it with {cmd}", app.name) +} + +/// The sync repository (`extra`, `omarchy`, ...) that carries the installed package `pkg`, or +/// `None` when no configured repository does (an AUR or locally built package) or pacman can't +/// be asked. +fn pacman_repo(pkg: &str) -> Option { + #[cfg(target_os = "linux")] + if !crate::detect::linux::has_pacman_db() { + return None; + } + let pacman = which("pacman")?; + let t = std::time::Duration::from_secs(5); + // For packages carried by a configured sync repository, ask pacman -Si which repo carries it. + // pacman translates its field names; the C locale keeps "Repository" parseable. + let mut si = process::command(&pacman); + si.args(["-Si", "--", pkg]).env("LC_ALL", "C"); + let out = process::run_command(si, "pacman", t).ok()?; + parse_pacman_repo(&out) +} + +/// The `Repository` field of `pacman -Si` output (the first one, when several repos carry it: +/// that is the one pacman installs from). +fn parse_pacman_repo(si: &str) -> Option { + si.lines().find_map(|l| { + let (key, value) = l.split_once(':')?; + (key.trim() == "Repository").then(|| value.trim().to_string()).filter(|r| !r.is_empty()) + }) +} + +/// Removes an installed copy. `phase` hears when the removal has to wait for another install; +/// `stop` cancels it while it waits. +pub fn uninstall( + app: &App, + installed: &Installed, + record: Option<&Record>, + paths: &Paths, + stop: &Stop, + phase: &mut dyn FnMut(JobPhase), +) -> Result<(), String> { + let _ = (record, stop, &phase, paths); + let path = PathBuf::from(&installed.location); + if let Some(msg) = managed_elsewhere(app, installed, true) { + return Err(msg); + } + if matches!(installed.source, InstallSource::MacApp | InstallSource::Msi | InstallSource::AppImage | InstallSource::Launcher) + && process::is_running(&path) + { + return Err(format!("{} is running. Quit it, then try again.", app.name)); + } + match installed.source { + #[cfg(windows)] + InstallSource::Launcher if record.is_some_and(|r| r.kind == PackageKind::PortableZip) => { + crate::portable::remove(app, record.ok_or("the launcher has no record of these files")?) + } + #[cfg(windows)] + InstallSource::Msi => windows::uninstall_msi(app, stop, phase), + #[cfg(target_os = "macos")] + InstallSource::MacApp | InstallSource::Launcher => macos::move_to_trash(&path), + #[cfg(target_os = "linux")] + InstallSource::Deb | InstallSource::Rpm | InstallSource::Pacman | InstallSource::Flatpak => { + linux::remove_package(app, installed, stop, phase) + } + #[cfg(target_os = "freebsd")] + InstallSource::Launcher => match record { + Some(r) => freebsd::remove_files(&r.files, r.needs_root), + None => Err("the launcher has no record of these files".into()), + }, + #[cfg(target_os = "linux")] + InstallSource::AppImage => remove_appimage(app, &path, record), + // A tarball the launcher unpacked into its own folder: take the tree out and leave the + // app's settings (`home/`) alone, so a reinstall finds its projects where they were. + #[cfg(target_os = "linux")] + InstallSource::Launcher if record.is_some_and(|r| r.kind == PackageKind::TarGz) => linux::remove_tree(app, paths), + #[cfg(target_os = "linux")] + InstallSource::Launcher => remove_appimage(app, &path, record), + other => Err(format!("{} can't be removed from here", other.label())), + } +} + +/// Deletes an AppImage, the menu entry and icon the launcher wrote for it, and the menu entry +/// the app may have written for itself on first run. +#[cfg(target_os = "linux")] +fn remove_appimage(app: &App, path: &Path, record: Option<&Record>) -> Result<(), String> { + if path.extension().is_none_or(|e| !e.eq_ignore_ascii_case("AppImage")) || !path.is_file() { + return Err(format!("{} isn't an AppImage, so it was left alone", path.display())); + } + // AppImages found elsewhere (Gear Lever's ~/AppImages, ~/.local/bin) are the user's own: + // remove only the launcher's folder or a copy the launcher recorded installing. + let ours = + path.parent().is_some_and(|dir| crate::detect::linux::appimage_dir().is_some_and(|own| own == dir)) || record.is_some_and(|r| r.path == path); + if !ours { + return Err(format!("{} wasn't installed by the launcher. Remove it the way it was added.", path.display())); + } + std::fs::remove_file(path).map_err(|e| format!("couldn't remove {} ({e})", path.display()))?; + linux::unintegrate(app, path, record); + Ok(()) +} + +/// Starts an installed app. +pub fn launch(app: &App, installed: &Installed, paths: &Paths) -> Result<(), String> { + let _ = paths; + let path = Path::new(&installed.location); + match installed.source { + InstallSource::Flatpak => { + let mut cmd = process::flatpak_command(); + cmd.args(["run", &app.bundle_id()]); + process::spawn_detached(cmd) + } + InstallSource::MacApp => process::open(path.as_os_str()), + // A tarball the launcher unpacked into its own folder: run it here, against its own + // home, rather than on the host through `flatpak-spawn` (which is what `launch_binary` + // would do inside the sandbox). Its settings then land beside it instead of in the + // launcher's or the host's home folder. + #[cfg(target_os = "linux")] + InstallSource::Launcher if path.starts_with(paths.apps()) => { + let home = paths.app_home(app.id); + prepare_home(&home); + let mut cmd = process::command(path); + for (name, value) in home_env(&home) { + cmd.env(name, value); + } + if let Some(dir) = path.parent() { + cmd.current_dir(dir); + } + process::spawn_detached(cmd) + } + _ if cfg!(target_os = "macos") && path.extension().is_some_and(|e| e == "app") => process::open(path.as_os_str()), + _ => { + if !path.exists() { + return Err(format!("{} isn't where it was installed ({})", app.name, path.display())); + } + process::launch_binary(path, &[]) + } + } + .map_err(|e| format!("Couldn't open {}: {e}", app.name)) +} + +/// The environment a Craft App unpacked by the launcher runs with: its own `HOME` and the XDG +/// base directories inside it. Apps read their settings from these, so everything they keep +/// (settings, projects, caches) stays in that one folder. +#[cfg(target_os = "linux")] +fn home_env(home: &Path) -> [(&'static str, PathBuf); 5] { + [ + ("HOME", home.to_path_buf()), + ("XDG_CONFIG_HOME", home.join(".config")), + ("XDG_DATA_HOME", home.join(".local/share")), + ("XDG_CACHE_HOME", home.join(".cache")), + ("XDG_STATE_HOME", home.join(".local/state")), + ] +} + +/// Creates the folders an app unpacked by the launcher expects to find, and links the host's +/// `~/.vst3` and `~/.clap` (SoundCraft's plugin folders) in when they exist, so an app running +/// inside the sandbox still sees the plugins installed on the host. Every failure here is +/// ignored: the app is worth starting even if its home is not quite ready. +#[cfg(target_os = "linux")] +fn prepare_home(home: &Path) { + for (_, dir) in home_env(home).iter().skip(1) { + let _ = std::fs::create_dir_all(dir); + } + let Some(real) = dirs::home_dir() else { return }; + if real == home { + return; + } + for name in [".vst3", ".clap"] { + let link = home.join(name); + if link.symlink_metadata().is_ok() { + continue; + } + if real.join(name).is_dir() { + let _ = std::os::unix::fs::symlink(real.join(name), link); + } + } +} + +/// A new folder of our own inside `dir`, removed when dropped: a predictable name there may be +/// the user's. +#[cfg(any(target_os = "linux", target_os = "macos"))] +fn staging_dir(dir: &Path) -> Result { + tempfile::Builder::new() + .prefix(".artcraft-launcher-") + .tempdir_in(dir) + .map_err(|e| format!("couldn't create a staging folder in {} ({e})", dir.display())) +} + +/// Moves `staged` into place at `dest`, keeping the old copy until the new one is there. +#[allow(dead_code)] +fn swap_into_place(staged: &Path, dest: &Path) -> Result<(), String> { + swap_into_place_via(staged, dest, |from, to| std::fs::rename(from, to)) +} + +fn swap_into_place_via(staged: &Path, dest: &Path, rename: impl Fn(&Path, &Path) -> std::io::Result<()>) -> Result<(), String> { + // Reserve a directory of our own beside the destination. A predictable sibling such as + // `app.old` may be a user's backup, and must never be removed or overwritten. + let backup = if dest.exists() { + let parent = dest.parent().ok_or("the installed app has no parent folder")?; + let backup = tempfile::Builder::new() + .prefix(".artcraft-launcher-backup-") + .tempdir_in(parent) + .map_err(|e| format!("couldn't create a backup folder in {} ({e})", parent.display()))?; + rename(dest, &backup.path().join("old")).map_err(|e| format!("couldn't move the old copy aside ({e})"))?; + Some(backup) + } else { + None + }; + if let Err(e) = rename(staged, dest) { + if let Some(backup) = backup + && let Err(restore) = rename(&backup.path().join("old"), dest) + { + // A failed rollback must not let TempDir's cleanup delete the only old copy. + let saved = backup.keep().join("old"); + return Err(format!( + "couldn't put the new copy in place ({e}); couldn't restore the old copy ({restore}); it was kept at {}", + saved.display() + )); + } + return Err(format!("couldn't put the new copy in place ({e})")); + } + Ok(()) +} + +/// After unpacking an archive into `dir`: its single top-level folder, if it has one. +#[allow(dead_code)] +fn single_top_folder(dir: &Path) -> Result { + let entries: Vec<_> = std::fs::read_dir(dir).map_err(|e| e.to_string())?.flatten().collect(); + match entries.as_slice() { + [only] if only.file_type().is_ok_and(|ft| ft.is_dir() && !ft.is_symlink()) => Ok(only.path()), + _ => Ok(dir.to_path_buf()), + } +} + +/// The app binary inside an unpacked archive: `bin/`, `` or `.exe`. +#[allow(dead_code)] +fn find_binary(root: &Path, app: &App) -> Option { + let exe = if cfg!(windows) { format!("{}.exe", app.id) } else { app.id.to_string() }; + [root.join("bin").join(&exe), root.join(&exe)].into_iter().find(|p| p.is_file()) +} + +/// Finds a program on `PATH`. +#[allow(dead_code)] +fn which(name: &str) -> Option { + let path = std::env::var_os("PATH")?; + std::env::split_paths(&path).map(|d| d.join(name)).find(|p| p.is_file()) +} + +/// The root-owned directories that programs run as root come from. `pkexec` looks a bare program +/// name up on the caller's `PATH`, so it is always handed an absolute path from one of these. +#[cfg(target_os = "linux")] +const SYSTEM_BIN_DIRS: [&str; 4] = ["/usr/bin", "/bin", "/usr/sbin", "/sbin"]; +/// FreeBSD's packages, polkit's `pkexec` among them, install under `/usr/local`. +#[cfg(target_os = "freebsd")] +const SYSTEM_BIN_DIRS: [&str; 6] = ["/usr/bin", "/bin", "/usr/sbin", "/sbin", "/usr/local/bin", "/usr/local/sbin"]; + +/// `name` in the first of `dirs` that has it. +#[cfg(any(target_os = "linux", target_os = "freebsd"))] +fn find_in(dirs: &[impl AsRef], name: &std::ffi::OsStr) -> Option { + dirs.iter().map(|d| d.as_ref().join(name)).find(|p| p.is_file()) +} + +/// `args` with its program as an absolute path: a bare name is looked up in `dirs`. +#[cfg(any(target_os = "linux", target_os = "freebsd"))] +fn with_absolute_program(dirs: &[impl AsRef], args: &[&std::ffi::OsStr]) -> Result, String> { + let (program, rest) = args.split_first().ok_or("there is no command to run")?; + let path = Path::new(program); + let program = if path.is_absolute() { + path.to_path_buf() + } else if path.file_name() == Some(*program) { + find_in(dirs, program).ok_or_else(|| format!("{} isn't installed", path.display()))? + } else { + return Err(format!("{} isn't a program name", path.display())); + }; + Ok(std::iter::once(program.into_os_string()).chain(rest.iter().map(|a| a.to_os_string())).collect()) +} + +/// polkit's `pkexec`, from the same directories as the programs it runs. +#[cfg(any(target_os = "linux", target_os = "freebsd"))] +fn pkexec() -> Option { + find_in(&SYSTEM_BIN_DIRS, std::ffi::OsStr::new("pkexec")) +} + +/// Runs a command as root through polkit (`pkexec`), which shows the desktop's password prompt. +#[cfg(any(target_os = "linux", target_os = "freebsd"))] +fn run_as_root(args: &[&std::ffi::OsStr]) -> Result<(), String> { + let args = with_absolute_program(&SYSTEM_BIN_DIRS, args)?; + let pkexec = pkexec().ok_or("installing system-wide needs polkit (pkexec), which isn't installed")?; + let status = process::command(pkexec).args(&args).status().map_err(|e| format!("couldn't ask for permission ({e})"))?; + root_outcome(status.code()) +} + +/// What `pkexec`'s exit code says about the command it ran. +#[cfg(any(target_os = "linux", target_os = "freebsd"))] +fn root_outcome(code: Option) -> Result<(), String> { + match code { + Some(0) => Ok(()), + // pkexec: 126 = the password prompt was dismissed, 127 = not authorized. + Some(126 | 127) => Err("cancelled".into()), + Some(c) => Err(format!("the system installer stopped with error {c}")), + None => Err("the system installer was interrupted".into()), + } +} + +/// How the error of an installer call shared by several apps ends (see `linux::shared_outcome`). +const SHARED_CALL: &str = "apps together. Try them one at a time."; + +/// Whether an install failed in one installer call shared with other apps (Linux packages +/// installed together). Such a call can fail after it has installed some of its apps; any other +/// failure is the app's own. +pub fn failed_in_shared_call(error: &str) -> bool { + cfg!(target_os = "linux") && error.ends_with(SHARED_CALL) +} + +/// The system's installer runs one job at a time; the others wait their turn here. +#[cfg(any(target_os = "linux", windows))] +static INSTALLER: crate::queue::Queue = crate::queue::Queue::new(); + +/// For [`INSTALLER`]: reports [`JobPhase::Waiting`] while a job waits, then `working` again. +#[cfg(any(target_os = "linux", windows))] +fn turn<'a>(phase: &'a mut dyn FnMut(JobPhase), working: JobPhase) -> impl FnMut(bool) + 'a { + move |waiting| phase(if waiting { JobPhase::Waiting } else { working }) +} + +/// For [`INSTALLER`]: whether the user cancelled the job while it waits. +#[cfg(any(target_os = "linux", windows))] +fn cancelled(stop: &Stop) -> impl Fn() -> bool + '_ { + || stop.check().is_err_and(|e| e == crate::protocol::CANCELLED) +} + +#[cfg(target_os = "linux")] +pub(crate) mod linux { + use std::ffi::{OsStr, OsString}; + use std::os::unix::fs::PermissionsExt; + + use crate::distro; + use crate::protocol::CANCELLED; + + use super::{ + App, INSTALLER, InstallSource, Installed, JobPhase, PackageKind, Path, PathBuf, Paths, Record, SHARED_CALL, SYSTEM_BIN_DIRS, Stop, cancelled, + find_in, pkexec, process, run_as_root, swap_into_place, tree, turn, + }; + + /// apt waits this long for another package tool (a software centre, automatic updates) to + /// let go of its lock, instead of failing at once. + const APT_WAITS: &str = "DPkg::Lock::Timeout=60"; + + /// A package tool that root commands can run, from the same directories `run_as_root` uses. + fn system_tool(name: &str) -> Option { + find_in(&SYSTEM_BIN_DIRS, OsStr::new(name)) + } + + /// Where commands that need root go: the installer's queue, then `pkexec`. + pub struct Root<'a> { + pub queue: &'a crate::queue::Queue, + pub run: &'a (dyn Fn(&[&OsStr]) -> Result<(), String> + Sync), + } + + const SYSTEM: Root<'static> = Root { queue: &INSTALLER, run: &run_as_root }; + + /// Runs `command target` as root. Jobs waiting for the same command go into one call + /// (`apt-get install a.deb b.deb`), so the password is asked for once. + fn as_root( + root: &Root<'_>, + command: &[&str], + target: &OsStr, + stop: &Stop, + phase: &mut dyn FnMut(JobPhase), + working: JobPhase, + ) -> Result<(), String> { + let command = command.iter().map(OsString::from).collect(); + root.queue.batched(command, target.to_os_string(), &cancelled(stop), &mut turn(phase, working), &|command, targets| { + let args: Vec<&OsStr> = command.iter().chain(targets).map(OsString::as_os_str).collect(); + shared_outcome((root.run)(&args), targets.len()) + }) + } + + /// What one call's result means for each of the `count` apps it handled: a failure can't be + /// pinned on one of them, and trying each again by itself would ask for the password each time. + pub fn shared_outcome(result: Result<(), String>, count: usize) -> Result<(), String> { + match result { + Err(e) if count > 1 && e != CANCELLED => Err(format!("{e} while handling {count} {SHARED_CALL}")), + other => other, + } + } + + /// The command that installs (or upgrades) a downloaded system package. + pub fn install_command(kind: PackageKind) -> Option<&'static [&'static str]> { + match kind { + // apt resolves dependencies and records the package like any other. + PackageKind::Deb => Some(&["apt-get", "-o", APT_WAITS, "install", "-y", "--allow-downgrades"]), + PackageKind::Rpm if system_tool("dnf").is_some() => Some(&["dnf", "install", "-y"]), + PackageKind::Rpm if system_tool("zypper").is_some() => Some(&["zypper", "--non-interactive", "install", "--allow-unsigned-rpm"]), + PackageKind::Rpm => Some(&["rpm", "-U", "--replacepkgs"]), + PackageKind::ArchPkg => Some(&["pacman", "-U", "--noconfirm"]), + _ => None, + } + } + + /// Fedora Atomic (Silverblue, Kinoite, Bazzite…): rpm-ostree manages the root filesystem and + /// `dnf install` of a local package is refused (Bazzite wraps `/usr/bin/dnf` to say so). + fn immutable_ostree() -> bool { + Path::new("/run/ostree-booted").exists() + } + + /// What this system can install, ordered with the best default first. + /// + /// The default package format aligns with how the user installed the launcher itself: + /// - Running as a Flatpak: prefers Flatpak (per-user, desktop integration). When the host + /// has no usable Flatpak (none installed, or `flatpak-spawn` refused) it prefers the + /// release tarball instead, which the launcher unpacks and runs inside the sandbox; an + /// AppImage cannot mount there and a distro package needs root, so both are dead ends. + /// - Running as an AppImage: prefers AppImage. + /// - Otherwise (system install): prefers the distro's own package format (`.deb` on the + /// Debian family, `.rpm` on the Fedora, Red Hat and SUSE families, `.pkg.tar.zst` on Arch, + /// read from `os-release` rather than from whichever tools are on `PATH`), then Flatpak if + /// available, then AppImage. + pub fn available_kinds() -> Vec { + let flatpak = process::is_flatpak_available(); + let is_flatpak = process::in_sandbox(); + let is_appimage = std::env::var_os("APPIMAGE").is_some(); + let root = pkexec().is_some(); + let os = ["/etc/os-release", "/usr/lib/os-release"].iter().find_map(|p| std::fs::read_to_string(p).ok()).unwrap_or_default(); + let native = distro::native_package(&distro::parse_os_release(&os)); + // An ostree system can't take .deb or .rpm installs, whatever os-release says. + let root = root && !immutable_ostree(); + let can_deb = root && native == Some(PackageKind::Deb) && system_tool("apt-get").is_some() && system_tool("dpkg").is_some(); + let can_rpm = root && native == Some(PackageKind::Rpm) && system_tool("rpm").is_some(); + let can_arch = root && native == Some(PackageKind::ArchPkg) && system_tool("pacman").is_some() && crate::detect::linux::has_pacman_db(); + order_available_kinds(is_flatpak, is_appimage, flatpak, can_deb, can_rpm, can_arch) + } + + pub(crate) fn order_available_kinds( + is_flatpak: bool, + is_appimage: bool, + flatpak_available: bool, + can_deb: bool, + can_rpm: bool, + can_arch: bool, + ) -> Vec { + let mut kinds = Vec::new(); + if is_flatpak && flatpak_available { + kinds.push(PackageKind::Flatpak); + } else if is_flatpak { + kinds.push(PackageKind::TarGz); + } else if is_appimage { + kinds.push(PackageKind::AppImage); + } + + if can_deb { + kinds.push(PackageKind::Deb); + } + if can_rpm { + kinds.push(PackageKind::Rpm); + } + if can_arch { + kinds.push(PackageKind::ArchPkg); + } + + if flatpak_available && !kinds.contains(&PackageKind::Flatpak) { + kinds.push(PackageKind::Flatpak); + } + // The tarball runs inside the sandbox, so it is only ever offered there. + if is_flatpak && !kinds.contains(&PackageKind::TarGz) { + kinds.push(PackageKind::TarGz); + } + if !kinds.contains(&PackageKind::AppImage) { + kinds.push(PackageKind::AppImage); + } + kinds + } + + /// Installs (or upgrades) a downloaded package with the system's own tools. + pub fn install_package(kind: PackageKind, file: &Path, stop: &Stop, phase: &mut dyn FnMut(JobPhase)) -> Result<(), String> { + if kind == PackageKind::Deb && system_tool("apt-get").is_none() { + return Err("this system has no apt-get to install a Debian package with".into()); + } + if kind == PackageKind::ArchPkg && system_tool("pacman").is_none() { + return Err("this system has no pacman to install an Arch package with".into()); + } + install_package_via(&SYSTEM, kind, file, stop, phase) + } + + pub fn install_package_via(root: &Root<'_>, kind: PackageKind, file: &Path, stop: &Stop, phase: &mut dyn FnMut(JobPhase)) -> Result<(), String> { + let f = file.as_os_str(); + if let Some(command) = install_command(kind) { + return as_root(root, command, f, stop, phase, JobPhase::Installing); + } + match kind { + PackageKind::Flatpak => { + let install_one = || { + log::info!("executing flatpak install --reinstall --bundle for {}", file.display()); + let output = process::flatpak_command() + .args(["install", "--user", "-y", "--noninteractive", "--reinstall", "--bundle"]) + .arg(f) + .output() + .map_err(|e| format!("couldn't start Flatpak ({e})"))?; + if output.status.success() { + log::info!("flatpak install succeeded for {}", file.display()); + Ok(()) + } else { + let stderr = String::from_utf8_lossy(&output.stderr); + let err = stderr.trim(); + let err = if err.is_empty() { String::from_utf8_lossy(&output.stdout).trim().to_string() } else { err.to_string() }; + if err.is_empty() { + Err("Flatpak couldn't install the bundle (it needs the Flathub remote for its runtime)".into()) + } else { + let clean_err = err.strip_prefix("error: ").or_else(|| err.strip_prefix("Error: ")).unwrap_or(&err); + Err(format!("Flatpak couldn't install the bundle: {clean_err}")) + } + } + }; + root.queue.alone(&cancelled(stop), &mut turn(phase, JobPhase::Installing), install_one).unwrap_or(Err(CANCELLED.into())) + } + other => Err(format!("{} isn't a system package", other.label())), + } + } + + pub fn remove_package(app: &App, installed: &Installed, stop: &Stop, phase: &mut dyn FnMut(JobPhase)) -> Result<(), String> { + let mut remove = |command: &[&str], package: &str| as_root(&SYSTEM, command, OsStr::new(package), stop, phase, JobPhase::Removing); + match installed.source { + InstallSource::Deb if system_tool("apt-get").is_some() => remove(&["apt-get", "-o", APT_WAITS, "remove", "-y"], app.id), + InstallSource::Deb => remove(&["dpkg", "-r"], app.id), + InstallSource::Rpm => { + if system_tool("dnf").is_some() { + remove(&["dnf", "remove", "-y"], app.id) + } else { + remove(&["rpm", "-e"], app.id) + } + } + // The package detected (photocraft, photocraft-bin or photocraft-git). + InstallSource::Pacman => remove(&["pacman", "-R", "--noconfirm"], installed.package.as_deref().unwrap_or(app.id)), + InstallSource::Flatpak => { + let id = app.bundle_id(); + let user = process::flatpak_command().args(["uninstall", "--user", "-y", "--noninteractive", &id]).status(); + if user.is_ok_and(|s| s.success()) { + return Ok(()); + } + if process::in_sandbox() { + let system = process::flatpak_command().args(["uninstall", "--system", "-y", "--noninteractive", &id]).status(); + if system.is_ok_and(|s| s.success()) { + return Ok(()); + } + return Err(format!("couldn't uninstall {id}")); + } + remove(&["flatpak", "uninstall", "--system", "-y", "--noninteractive"], &id) + } + other => Err(format!("{} isn't a system package", other.label())), + } + } + + pub fn install_appimage( + file: &Path, + name: &str, + destination: Option<&Path>, + previous: Option<&Record>, + replace: bool, + ) -> Result<(PathBuf, Vec), String> { + let dir = match destination { + Some(dir) => dir.to_path_buf(), + None => { + let dir = crate::detect::linux::appimage_dir().ok_or("no home folder")?; + std::fs::create_dir_all(&dir).map_err(|e| format!("couldn't create {} ({e})", dir.display()))?; + dir + } + }; + let dest = dir.join(name); + let ours = previous.is_some_and(|r| r.kind == PackageKind::AppImage && r.path == dest); + if std::fs::symlink_metadata(&dest).is_ok_and(|m| m.is_dir() || !(replace || ours && m.is_file())) { + return Err(format!("{} already exists and wasn't installed by ArtCraft Launcher", dest.display())); + } + let staging = super::staging_dir(&dir)?; + let partial = staging.path().join(name); + std::fs::copy(file, &partial).map_err(|e| format!("couldn't copy the AppImage ({e})"))?; + // The AppImage is the program the user just installed and must be executable; 0o755 is + // not group/world-writable. + std::fs::set_permissions(&partial, std::fs::Permissions::from_mode(0o755)).map_err(|e| e.to_string())?; // NOSONAR + std::fs::rename(&partial, &dest).map_err(|e| format!("couldn't put the AppImage in place ({e})"))?; + let mut removed = Vec::new(); + if let Some(old) = previous.filter(|r| r.kind == PackageKind::AppImage && r.path != dest) + && std::fs::remove_file(&old.path).is_ok() + { + removed.push(old.path.clone()); + } + Ok((dest, removed)) + } + + /// Unpacks a release tarball (`bin/`, `share/…`) into `/apps/`, a folder + /// only the launcher owns, and returns the app's binary. This is the fallback for the + /// Flatpak launcher when the host has no usable Flatpak: the app then runs inside the + /// sandbox instead of on the host. `bin` and `share` are swapped in one at a time, so an + /// update leaves the app's own `home/` beside them untouched. + pub fn install_tree(app: &App, file: &Path, paths: &Paths) -> Result { + let staged = paths.cache.join(format!("{}.new", app.id)); + let result = unpack_tree(app, file, &staged, paths); + let _ = std::fs::remove_dir_all(&staged); + result + } + + fn unpack_tree(app: &App, file: &Path, staged: &Path, paths: &Paths) -> Result { + let tree = tree::unpack(app, file, staged)?; + // Verify tree root is strictly within staged and is not a symlink (CWE-59). + let rel_tree = tree.strip_prefix(staged).map_err(|_| "staging layout escapes staging".to_string())?; + let mut check_root = staged.to_path_buf(); + for part in rel_tree.components() { + check_root.push(part); + let meta = std::fs::symlink_metadata(&check_root).map_err(|e| format!("couldn't check {} ({e})", check_root.display()))?; + if meta.file_type().is_symlink() { + return Err("unpacked tree root is a symlink".into()); + } + } + // Validate the staged binary and set its mode *before* either swap: a rejected update + // (e.g. a symlinked app binary) must never disturb the installed tree it replaces. + let staged_bin = tree.join("bin").join(app.id); + make_runnable_within(&tree, &staged_bin)?; + let root = paths.apps().join(app.id); + std::fs::create_dir_all(&root).map_err(|e| format!("couldn't create {} ({e})", root.display()))?; + for dir in ["bin", "share"] { + if tree.join(dir).is_dir() { + swap_into_place(&tree.join(dir), &root.join(dir))?; + } + } + Ok(root.join("bin").join(app.id)) + } + + /// Makes `bin` executable (0755) without ever following an archive-controlled symlink out of + /// the installation tree. + /// + /// `bin` comes from an untrusted tarball. If it, or one of its ancestors inside `root`, is a + /// symlink, a plain `std::fs::set_permissions(bin, …)` would follow the link and change the + /// mode of an existing file *outside* the tree (CWE-59). We reject any symlinked component + /// before changing permissions, so the chmod can only ever reach a regular file that lives at + /// the verified path inside the launcher's own apps folder. + fn make_runnable_within(root: &Path, bin: &Path) -> Result<(), String> { + let root_meta = std::fs::symlink_metadata(root).map_err(|e| format!("couldn't check {} ({e})", root.display()))?; + if root_meta.file_type().is_symlink() { + return Err("the install tree root is a symlink".into()); + } + // No component between `root` and `bin` may be a symlink. + let rel = bin.strip_prefix(root).map_err(|_| "the app binary isn't inside the install tree".to_string())?; + let mut here = root.to_path_buf(); + for part in rel.components() { + here.push(part); + let meta = std::fs::symlink_metadata(&here).map_err(|e| format!("couldn't check {} ({e})", here.display()))?; + if meta.file_type().is_symlink() { + return Err("the download's app binary escapes the install tree through a symlink".into()); + } + } + // The final component is a verified non-symlink regular file inside the launcher's own + // apps folder, so changing its mode cannot reach anything outside the tree. + if !here.metadata().map_err(|e| e.to_string())?.file_type().is_file() { + return Err("the download's app binary isn't a regular file".into()); + } + // Verified non-symlink regular file inside the launcher's apps folder; the app binary must + // be executable and 0o755 is not group/world-writable. + std::fs::set_permissions(bin, std::fs::Permissions::from_mode(0o755)).map_err(|e| format!("couldn't make {} runnable ({e})", bin.display())) // NOSONAR + } + + /// Removes a tree the launcher unpacked in [`install_tree`], keeping the app's `home/`: the + /// settings and projects in it are the user's, not the installer's. Only the two folders the + /// launcher put there are removed, and only inside the launcher's own apps folder. + pub fn remove_tree(app: &App, paths: &Paths) -> Result<(), String> { + let root = paths.apps().join(app.id); + let mut removed = false; + for dir in ["bin", "share"] { + let path = root.join(dir); + if path.starts_with(paths.apps()) && path.is_dir() { + std::fs::remove_dir_all(&path).map_err(|e| format!("couldn't remove {} ({e})", path.display()))?; + removed = true; + } + } + if removed { Ok(()) } else { Err(format!("{} isn't installed in this folder", app.name)) } + } + + /// Where menu entries and icons go: `$XDG_DATA_HOME`, else `~/.local/share`. Inside the + /// launcher's own Flatpak that variable points into the sandbox, so the host's is used. + pub fn host_data_dir() -> Option { + if Path::new("/.flatpak-info").exists() { + if let Some(dir) = std::env::var_os("HOST_XDG_DATA_HOME").map(PathBuf::from).filter(|p| p.is_absolute()) { + return Some(dir); + } + return dirs::home_dir().map(|h| h.join(".local").join("share")); + } + dirs::data_dir() + } + + /// The app's 128 px icon, as bundled in `assets/app-icons`. + fn icon_png(app: &App) -> Option<&'static [u8]> { + app.icon_png + } + + fn desktop_path(data: &Path, app: &App) -> PathBuf { + data.join("applications").join(format!("{}.desktop", app.bundle_id())) + } + + fn icon_path(data: &Path, app: &App) -> PathBuf { + data.join("icons/hicolor/128x128/apps").join(format!("{}.png", app.bundle_id())) + } + + /// Adds `appimage` to the desktop's menu. Best effort: a failure here never fails the + /// install. Returns the files written, for the record. + pub fn integrate(app: &App, appimage: &Path, replaced: &[PathBuf], previous: Option<&Record>) -> Vec { + match host_data_dir() { + Some(data) => integrate_into(&data, app, appimage, replaced, previous), + None => Vec::new(), + } + } + + /// [`integrate`] into the data folder `data`. Only writes over files the launcher wrote + /// before: its own marked menu entry, an entry pointing at a copy this install replaced, and + /// an icon listed in `previous`. + pub(crate) fn integrate_into(data: &Path, app: &App, appimage: &Path, replaced: &[PathBuf], previous: Option<&Record>) -> Vec { + let ours_before = |p: &Path| previous.is_some_and(|r| r.files.iter().any(|f| f == p)); + let mut written = Vec::new(); + let icon = icon_path(data, app); + let has_icon = match icon_png(app) { + Some(png) if !icon.exists() || ours_before(&icon) => match crate::paths::write_atomic(&icon, png) { + Ok(()) => { + written.push(icon.clone()); + true + } + Err(e) => { + log::warn!("{}: couldn't install the menu icon: {e}", app.name); + false + } + }, + // Someone else's icon under the app's id (the app's own AppImage puts one there). + _ => icon.is_file(), + }; + let Some(text) = crate::desktop_entry::render(app, appimage, has_icon) else { + log::warn!("{}: {} can't be written into a menu entry; skipping it", app.name, appimage.display()); + return written; + }; + let desktop = desktop_path(data, app); + let replace = match std::fs::read_to_string(&desktop) { + Ok(old) => { + crate::desktop_entry::is_ours(&old) + || crate::desktop_entry::exec_program(&old) + .is_some_and(|p| Path::new(&p) == appimage || replaced.iter().any(|r| r == Path::new(&p))) + } + Err(_) => !desktop.exists(), + }; + if !replace { + log::info!("{}: leaving the existing menu entry {} alone", app.name, desktop.display()); + return written; + } + match crate::paths::write_atomic(&desktop, text.as_bytes()) { + Ok(()) => written.push(desktop), + Err(e) => log::warn!("{}: couldn't add the menu entry: {e}", app.name), + } + written + } + + /// Takes away what [`integrate`] added for the removed AppImage `appimage`. + pub fn unintegrate(app: &App, appimage: &Path, record: Option<&Record>) { + if let Some(data) = host_data_dir() { + unintegrate_from(&data, app, appimage, record); + } + } + + /// [`unintegrate`] from the data folder `data`: the menu entry when it runs the removed copy, + /// or when the launcher wrote it and what it runs is gone; the icon only when the + /// launcher's record lists it. + pub(crate) fn unintegrate_from(data: &Path, app: &App, appimage: &Path, record: Option<&Record>) { + let desktop = desktop_path(data, app); + if let Ok(text) = std::fs::read_to_string(&desktop) { + let program = crate::desktop_entry::exec_program(&text).map(PathBuf::from); + let runs_removed = program.as_deref() == Some(appimage); + let ours_and_dangling = crate::desktop_entry::is_ours(&text) && program.is_none_or(|p| !p.exists()); + if runs_removed || ours_and_dangling { + let _ = std::fs::remove_file(&desktop); + } + } + let icon = icon_path(data, app); + if record.is_some_and(|r| r.files.contains(&icon)) { + let _ = std::fs::remove_file(&icon); + } + } +} + +#[cfg(target_os = "macos")] +pub mod macos { + use std::time::Duration; + + use crate::version::Version; + + use super::{App, InstallSource, Installed, Path, PathBuf, Paths, Release, process, staging_dir, swap_into_place}; + + /// `/Applications` when we can write there, else `~/Applications`. + fn applications_dir(existing: Option<&Installed>, destination: Option<&Path>) -> Result { + if let Some(parent) = + existing.filter(|i| i.source == InstallSource::MacApp).and_then(|i| Path::new(&i.location).parent().map(Path::to_path_buf)) + { + return Ok(parent); + } + if let Some(dir) = destination { + return Ok(dir.to_path_buf()); + } + let system = PathBuf::from("/Applications"); + let probe = system.join(".artcraft-launcher-write-test"); + if std::fs::write(&probe, b"").is_ok() { + let _ = std::fs::remove_file(&probe); + return Ok(system); + } + let user = dirs::home_dir().ok_or("no home folder")?.join("Applications"); + std::fs::create_dir_all(&user).map_err(|e| format!("couldn't create {} ({e})", user.display()))?; + Ok(user) + } + + /// The Apple Developer team that signs every Craft App and ArtCraft Launcher (Learning + /// Machines LLC): the macOS side of the Windows publisher check. + pub const TEAM_ID: &str = "DJ6XS33FX8"; + + /// What a new copy must prove before it replaces anything. The checksum shows the download is + /// the file the release lists; these show the app inside is the one the release promises. + pub struct Expect<'a> { + /// The team whose Developer ID signature the bundle must carry. `None` only in tests, + /// which build unsigned bundles. + pub team: Option<&'a str>, + /// The release's version, which the bundle must report. `None` when the tag isn't one. + pub version: Option<&'a Version>, + } + + impl<'a> Expect<'a> { + pub fn release(release: &'a Release) -> Self { + Self { team: Some(TEAM_ID), version: release.version.as_ref() } + } + } + + /// Held while this process asks diskimages-helper for something (an attach here; the tests' + /// `hdiutil create` too), since it turns concurrent requests away. + pub(crate) static DISK_IMAGES: std::sync::Mutex<()> = std::sync::Mutex::new(()); + + /// Apple's tools, by full path: a launcher started from Finder or at login gets a minimal + /// `PATH`, and nothing earlier on it should stand in for these. + const HDIUTIL: &str = "/usr/bin/hdiutil"; + const DITTO: &str = "/usr/bin/ditto"; + const CODESIGN: &str = "/usr/bin/codesign"; + + pub fn install_dmg( + app: &App, + dmg: &Path, + existing: Option<&Installed>, + destination: Option<&Path>, + replace: bool, + paths: &Paths, + expect: &Expect<'_>, + ) -> Result { + // A new, empty mount point for every install. A fixed one can still hold an image from an + // attach that was interrupted or timed out, and hdiutil won't mount another over it. + let attempt = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map(|d| d.as_nanos()).unwrap_or_default(); + let mount = paths.cache.join(format!("mount-{}-{}-{attempt}", app.id, std::process::id())); + std::fs::create_dir_all(&mount).map_err(|e| format!("couldn't create a folder to open the disk image in ({e})"))?; + let (Some(dmg_s), Some(mount_s)) = (dmg.to_str(), mount.to_str()) else { + let _ = std::fs::remove_dir(&mount); + return Err("unusual characters in the cache folder's path".into()); + }; + // diskimages-helper refuses an attach while it is busy with another image ("Resource + // temporarily unavailable"): attach one image at a time (several apps may be updating at + // once), and wait a little and try again when something outside the launcher holds it. + // The download's SHA-256 is already checked, so hdiutil's own checksum pass (-noverify) + // would only add minutes on a large image. + let attached = { + let _one_at_a_time = DISK_IMAGES.lock().unwrap_or_else(std::sync::PoisonError::into_inner); + let attach = || { + process::run( + HDIUTIL, + &["attach", "-nobrowse", "-readonly", "-noautoopen", "-noverify", "-mountpoint", mount_s, dmg_s], + Duration::from_secs(300), + ) + }; + let mut attached = attach(); + for wait in [1, 2, 4, 8] { + if attached.is_ok() { + break; + } + let _ = detach(&mount); + std::thread::sleep(Duration::from_secs(wait)); + attached = attach(); + } + attached + }; + if let Err(reason) = attached { + let _ = std::fs::remove_dir(&mount); + return Err(format!("couldn't open the disk image: {reason}")); + } + let result = copy_app(app, &mount, existing, destination, replace, expect); + let _ = detach(&mount); + let _ = std::fs::remove_dir(&mount); + result + } + + fn copy_app( + app: &App, + mount: &Path, + existing: Option<&Installed>, + destination: Option<&Path>, + replace: bool, + expect: &Expect<'_>, + ) -> Result { + let bundle = format!("{}.app", app.name); + let src = mount.join(&bundle); + crate::detect::macos::read_bundle(app, &src).ok_or("the disk image doesn't contain the app")?; + let dir = applications_dir(existing, destination)?; + let dest = existing.filter(|i| i.source == InstallSource::MacApp).map_or_else(|| dir.join(&bundle), |i| PathBuf::from(&i.location)); + if !replace + && std::fs::symlink_metadata(&dest).is_ok() + && (std::fs::symlink_metadata(&dest).is_ok_and(|m| m.file_type().is_symlink()) || crate::detect::macos::read_bundle(app, &dest).is_none()) + { + return Err(format!("{} already exists and wasn't installed by ArtCraft Launcher", dest.display())); + } + let staging = super::staging_dir(&dir)?; + let staged = staging.path().join(&bundle); + let (Some(src_s), Some(staged_s)) = (src.to_str(), staged.to_str()) else { return Err("unusual characters in a path".into()) }; + // ditto keeps the code signature and symlinks intact. The disk image's volume adds Finder + // metadata (com.apple.FinderInfo) to the files; leave it and the extended attributes + // behind, or `codesign --verify --strict` rejects the bundle as carrying "detritus". A + // bundle's signature lives in its files, not in extended attributes. + process::run(DITTO, &["--norsrc", "--noextattr", "--noacl", src_s, staged_s], Duration::from_secs(600)) + .map_err(|reason| format!("couldn't copy the app: {reason}"))?; + let checked = crate::detect::macos::read_bundle(app, &staged) + .ok_or_else(|| "the copied app is incomplete".to_string()) + .and_then(|found| check(&staged, &found, expect)); + checked?; + swap_into_place(&staged, &dest)?; + Ok(dest) + } + + /// Checks a copied bundle against what its release promises, before it replaces anything. + fn check(bundle: &Path, found: &Installed, expect: &Expect<'_>) -> Result<(), String> { + if let Some(team) = expect.team + && !signed_by(bundle, team) + { + return Err("the app isn't signed by its developer, so it wasn't installed".into()); + } + if let Some(want) = expect.version + && !found.version.as_ref().is_some_and(|v| same_release(v, want)) + { + return Err(format!("the app inside isn't version {want}, so it wasn't installed")); + } + Ok(()) + } + + /// Whether `bundle` has a valid Developer ID signature from `team`, nested code included. The + /// requirement pins the signer, so a valid ad-hoc or other developer's signature fails it. + /// Unlike a Gatekeeper assessment it needs no network. + pub fn signed_by(bundle: &Path, team: &str) -> bool { + let Some(path) = bundle.to_str() else { return false }; + if team.is_empty() || !team.bytes().all(|b| b.is_ascii_alphanumeric()) { + return false; + } + let requirement = format!("=anchor apple generic and certificate leaf[subject.OU] = \"{team}\""); + process::capture(CODESIGN, &["--verify", "--deep", "-R", &requirement, path], Duration::from_secs(120)).is_some() + } + + /// Whether a bundle's version is the release's. A bundle that reports only the numbers + /// (`CFBundleShortVersionString`) matches a pre-release with the same numbers. + fn same_release(found: &Version, release: &Version) -> bool { + (found.major, found.minor, found.patch) == (release.major, release.minor, release.patch) && (found.pre.is_empty() || found.pre == release.pre) + } + + fn detach(mount: &Path) -> Option { + let m = mount.to_str()?; + if !mount.join(".").exists() { + return None; + } + process::capture(HDIUTIL, &["detach", m, "-quiet"], Duration::from_secs(60)) + .or_else(|| process::capture(HDIUTIL, &["detach", m, "-force", "-quiet"], Duration::from_secs(60))) + } + + /// Moves an app to the user's Trash (so it can be put back). + pub fn move_to_trash(path: &Path) -> Result<(), String> { + let trash = dirs::home_dir().ok_or("no home folder")?.join(".Trash"); + let name = path.file_name().ok_or("not an app")?; + let mut dest = trash.join(name); + if dest.exists() { + dest = trash.join(format!("{} {}", name.to_string_lossy(), crate::now())); + } + std::fs::rename(path, &dest).map_err(|e| format!("couldn't move {} to Trash ({e}). Remove it in Finder instead.", path.display())) + } +} + +/// FreeBSD: the release tarball holds a `/usr/local`-style tree (`bin/`, +/// `share/applications`, icons). Older releases had the files flat in one folder; those are laid +/// out the same way first. +#[cfg(unix)] +#[allow(dead_code)] +mod tree { + use super::{App, Path, PathBuf, single_top_folder}; + + /// Unpacks `file` and returns a staging folder laid out like `/usr/local`. + pub fn unpack(app: &App, file: &Path, staging: &Path) -> Result { + let _ = std::fs::remove_dir_all(staging); + std::fs::create_dir_all(staging).map_err(|e| format!("couldn't create {} ({e})", staging.display()))?; + let gz = std::fs::File::open(file).map_err(|e| e.to_string())?; + let mut archive = tar::Archive::new(flate2::read::GzDecoder::new(gz)); + archive.set_preserve_permissions(true); + // Bound entry count and cumulative unpacked bytes to prevent decompression bombs (CWE-409). + const MAX_ENTRIES: usize = 50_000; + const MAX_UNPACKED_BYTES: u64 = 2 * 1024 * 1024 * 1024; + let mut entry_count = 0; + let mut total_unpacked = 0u64; + let mut directories = Vec::new(); + let entries = archive.entries().map_err(|e| format!("couldn't read archive entries ({e})"))?; + for entry in entries { + let mut entry = entry.map_err(|e| format!("invalid tar entry ({e})"))?; + entry_count += 1; + if entry_count > MAX_ENTRIES { + return Err("archive has too many entries".into()); + } + let size = entry.size(); + total_unpacked = total_unpacked.saturating_add(size); + if total_unpacked > MAX_UNPACKED_BYTES { + return Err("archive exceeds maximum unpacked size".into()); + } + if entry.header().entry_type().is_dir() { + directories.push(entry); + } else { + entry.unpack_in(staging).map_err(|e| format!("couldn't unpack archive entry ({e})"))?; + } + } + // Delay directory entries until child entries are written so restrictive modes do not break child extraction. + directories.sort_by(|a, b| b.path_bytes().cmp(&a.path_bytes())); + for mut dir in directories { + dir.unpack_in(staging).map_err(|e| format!("couldn't unpack archive directory ({e})"))?; + } + let top = single_top_folder(staging)?; + if top.join("bin").join(app.id).is_file() { + return Ok(top); + } + // Flat layout: put each file where /usr/local keeps it. + // Ensure tree is a fresh directory and not an archive-created symlink or directory (CWE-59). + let tree = staging.join("tree"); + if let Ok(m) = std::fs::symlink_metadata(&tree) { + if m.file_type().is_symlink() || !m.is_dir() { + let _ = std::fs::remove_file(&tree); + } else { + let _ = std::fs::remove_dir_all(&tree); + } + } + std::fs::create_dir_all(&tree).map_err(|e| e.to_string())?; + let id = app.bundle_id(); + for (from, to) in [ + (top.join(app.id), tree.join("bin").join(app.id)), + (top.join(format!("{}-cli", app.id)), tree.join("bin").join(format!("{}-cli", app.id))), + (top.join(format!("{id}.desktop")), tree.join("share/applications").join(format!("{id}.desktop"))), + (top.join(format!("{id}.png")), tree.join("share/pixmaps").join(format!("{id}.png"))), + ] { + if std::fs::symlink_metadata(&from).is_ok_and(|m| m.file_type().is_file()) { + if let Some(dir) = to.parent() { + let rel = dir.strip_prefix(&tree).map_err(|_| "destination escapes staging tree".to_string())?; + let mut check = tree.clone(); + for part in rel.components() { + check.push(part); + if let Ok(m) = std::fs::symlink_metadata(&check) + && m.file_type().is_symlink() + { + return Err("destination parent directory is a symlink".into()); + } + } + std::fs::create_dir_all(dir).map_err(|e| e.to_string())?; + } + std::fs::rename(&from, &to).map_err(|e| e.to_string())?; + } + } + if !tree.join("bin").join(app.id).is_file() { + return Err("the download doesn't contain the app".into()); + } + Ok(tree) + } + + /// Every file under `root`, relative to it. Does not descend through symlinks (CWE-59, CWE-400). + pub fn files(root: &Path) -> Result, String> { + let mut out = Vec::new(); + let mut stack = vec![root.to_path_buf()]; + let mut visited = 0; + const MAX_FILES: usize = 50_000; + while let Some(dir) = stack.pop() { + let entries = std::fs::read_dir(&dir).map_err(|e| format!("couldn't read directory {}: {e}", dir.display()))?; + for entry in entries { + let entry = entry.map_err(|e| format!("couldn't read directory entry: {e}"))?; + visited += 1; + if visited > MAX_FILES { + return Err("directory tree exceeds maximum file limit of 50,000".into()); + } + let ft = entry.file_type().map_err(|e| format!("couldn't read file type: {e}"))?; + let p = entry.path(); + if ft.is_dir() && !ft.is_symlink() { + stack.push(p); + } else { + let rel = p.strip_prefix(root).map_err(|e| e.to_string())?; + out.push(rel.to_path_buf()); + } + } + } + out.sort(); + Ok(out) + } +} + +#[cfg(target_os = "freebsd")] +mod freebsd { + use std::ffi::OsStr; + + use super::{App, Path, PathBuf, Paths, pkexec, process, run_as_root, tree}; + + /// Copies the release's tree into `/usr/local` (asking for permission through polkit), or + /// into `~/.local` when polkit isn't installed. Returns the binary, every file installed, + /// and whether removing them needs root. + pub fn install_tree(app: &App, file: &Path, paths: &Paths) -> Result<(PathBuf, Vec, bool), String> { + let staging = paths.cache.join(format!("{}.new", app.id)); + let result = install_from(app, file, &staging); + let _ = std::fs::remove_dir_all(&staging); + result + } + + fn install_from(app: &App, file: &Path, staging: &Path) -> Result<(PathBuf, Vec, bool), String> { + let tree = tree::unpack(app, file, staging)?; + let rel = tree::files(&tree)?; + let mut source = tree.into_os_string(); + source.push("/."); + if pkexec().is_some() { + let prefix = PathBuf::from("/usr/local"); + run_as_root(&[OsStr::new("/bin/cp"), OsStr::new("-R"), &source, prefix.as_os_str()])?; + return Ok((prefix.join("bin").join(app.id), rel.iter().map(|r| prefix.join(r)).collect(), true)); + } + let prefix = dirs::home_dir().ok_or("no home folder")?.join(".local"); + std::fs::create_dir_all(&prefix).map_err(|e| e.to_string())?; + let status = process::command("/bin/cp").arg("-R").arg(&source).arg(&prefix).status().map_err(|e| e.to_string())?; + if !status.success() { + return Err("couldn't copy the app into ~/.local".into()); + } + // ~/.local/bin is often not on PATH for the desktop: point the menu entry at the binary. + let bin = prefix.join("bin").join(app.id); + let desktop = prefix.join("share/applications").join(format!("{}.desktop", app.bundle_id())); + if let Ok(text) = std::fs::read_to_string(&desktop) { + let exec = format!("Exec=\"{}\"", bin.display()); + let fixed: String = text + .lines() + .map(|l| { + if let Some(rest) = l.strip_prefix("Exec=").and_then(|r| r.strip_prefix(app.id)) { + format!("{exec}{rest}\n") + } else { + format!("{l}\n") + } + }) + .collect(); + let _ = std::fs::write(&desktop, fixed); + } + Ok((bin, rel.iter().map(|r| prefix.join(r)).collect(), false)) + } + + /// Removes the files a tree install put down. + pub fn remove_files(files: &[PathBuf], needs_root: bool) -> Result<(), String> { + if files.is_empty() { + return Err("the launcher has no record of these files".into()); + } + let prefix = if needs_root { PathBuf::from("/usr/local") } else { dirs::home_dir().ok_or("no home folder")?.join(".local") }; + let mut safe_files = Vec::new(); + for f in files { + // Refuse paths that climb or escape install prefix (CWE-59). + if !f.starts_with(&prefix) || f.components().any(|c| matches!(c, std::path::Component::ParentDir)) { + return Err(format!("refusing to remove file outside install prefix: {}", f.display())); + } + let mut has_symlink_ancestor = false; + if let Ok(rel) = f.strip_prefix(&prefix) { + let mut check = prefix.clone(); + for part in rel.components() { + if let Ok(meta) = std::fs::symlink_metadata(&check) + && meta.file_type().is_symlink() + { + has_symlink_ancestor = true; + break; + } + check.push(part); + } + } + if !has_symlink_ancestor { + safe_files.push(f); + } + } + if safe_files.is_empty() { + return Ok(()); + } + if needs_root { + let mut args: Vec<&OsStr> = vec![OsStr::new("/bin/rm"), OsStr::new("-f"), OsStr::new("--")]; + args.extend(safe_files.iter().map(|f| f.as_os_str())); + return run_as_root(&args); + } + for f in safe_files { + let _ = std::fs::remove_file(f); + } + Ok(()) + } +} + +/// Builds the Windows Installer command separately so its package argument can be checked +/// without starting an installer. +#[cfg_attr(not(windows), allow(dead_code))] +fn msi_command(program: &Path, msi: &Path) -> Result { + let msi = msi_path(msi)?; + let mut command = process::command(program); + // A verbose log beside the package: Windows Installer's own errors (1603 above all) say + // nothing without it. + command.arg("/i").arg(&msi).args(["/passive", "/norestart", "/l*v"]).arg(msi_log(&msi)); + Ok(command) +} + +/// Where `msi_command` asks Windows Installer to write its log. +#[cfg_attr(not(windows), allow(dead_code))] +fn msi_log(msi: &Path) -> PathBuf { + msi.with_extension("log") +} + +/// Windows Installer's storage APIs can fail on paths through junctions or symlinks (#338). +/// Keep ordinary drive/UNC syntax when possible: Windows canonicalization alone introduces +/// a `\\?\` prefix that older APIs also cannot handle. +#[cfg_attr(not(windows), allow(dead_code))] +pub(crate) fn msi_path(msi: &Path) -> Result { + #[cfg(windows)] + let resolved = dunce::canonicalize(msi); + #[cfg(not(windows))] + let resolved = std::fs::canonicalize(msi); + resolved.map_err(|e| format!("couldn't resolve the Windows Installer package {} ({e})", msi.display())) +} + +#[cfg(windows)] +pub mod windows { + use crate::protocol::CANCELLED; + + use super::{App, INSTALLER, JobPhase, Path, PathBuf, Stop, cancelled, msi_command, msi_folder_argument, msi_log, process, turn}; + + fn msiexec() -> PathBuf { + std::env::var_os("SystemRoot").map_or_else(|| PathBuf::from(r"C:\Windows"), PathBuf::from).join(r"System32\msiexec.exe") + } + + fn explain(code: Option) -> Result<(), String> { + match code { + Some(0 | 3010 | 1641) => Ok(()), + Some(1602) => Err("cancelled".into()), + Some(1618) => Err("another installation is in progress. Wait for it to finish, then try again.".into()), + Some(1925 | 1303) => Err("Windows didn't allow the install (administrator permission is needed)".into()), + Some(c) => Err(format!("the Windows Installer stopped with error {c}")), + None => Err("the Windows Installer was interrupted".into()), + } + } + + /// Runs an MSI with a progress window and no restart. Windows asks for permission itself. + pub fn run_msi(msi: &Path, destination: Option<&Path>, stop: &Stop, phase: &mut dyn FnMut(JobPhase)) -> Result<(), String> { + in_turn(stop, phase, JobPhase::Installing, || { + use std::os::windows::process::CommandExt; + let mut command = msi_command(&msiexec(), msi)?; + if let Some(dir) = destination { + command.raw_arg(msi_folder_argument(dir)?); + } + let status = command.status().map_err(|e| format!("couldn't start the Windows Installer ({e})"))?; + explain(status.code()).map_err(|e| match status.code() { + Some(1602) | None => e, + _ => format!("{e}. Its log is at {}", msi_log(msi).display()), + }) + }) + } + + /// Runs the launcher's own new MSI for a self-update, with the same progress window and no + /// restart as [`run_msi`], and waits for it. + pub fn reinstall(msi: &Path) -> Result<(), String> { + let status = msi_command(&msiexec(), msi)?.status().map_err(|e| format!("couldn't start the Windows Installer ({e})"))?; + explain(status.code()) + } + + /// Runs `work` once no other Windows Installer job of ours is running (a second one would + /// stop with error 1618). Cancelled while it waits, it never runs. + fn in_turn(stop: &Stop, phase: &mut dyn FnMut(JobPhase), working: JobPhase, work: impl FnOnce() -> Result<(), String>) -> Result<(), String> { + INSTALLER.alone(&cancelled(stop), &mut turn(phase, working), work).unwrap_or_else(|| Err(CANCELLED.into())) + } + + pub fn uninstall_msi(app: &App, stop: &Stop, phase: &mut dyn FnMut(JobPhase)) -> Result<(), String> { + let installs = crate::detect::windows::msi_installs(app); + if installs.is_empty() { + return Err(format!("{} isn't registered with the Windows Installer", app.name)); + } + in_turn(stop, phase, JobPhase::Removing, || { + for m in installs { + let status = process::command(msiexec()) + .args(["/x", &m.product_code, "/passive", "/norestart"]) + .status() + .map_err(|e| format!("couldn't start the Windows Installer ({e})"))?; + explain(status.code())?; + } + Ok(()) + }) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[cfg(unix)] + #[test] + fn msi_install_uses_the_package_target_instead_of_a_symlinked_cache() { + let dir = tempfile::tempdir().unwrap(); + let cache = dir.path().join("persistent cache"); + std::fs::create_dir(&cache).unwrap(); + let file = cache.join("PhotoCraft.msi"); + std::fs::write(&file, b"test package").unwrap(); + let link = dir.path().join("current cache"); + std::os::unix::fs::symlink(&cache, &link).unwrap(); + let command = msi_command(Path::new("msiexec.exe"), &link.join("PhotoCraft.msi")).unwrap(); + let args: Vec<_> = command.get_args().collect(); + assert_eq!( + args, + [ + std::ffi::OsStr::new("/i"), + file.canonicalize().unwrap().as_os_str(), + std::ffi::OsStr::new("/passive"), + std::ffi::OsStr::new("/norestart"), + std::ffi::OsStr::new("/l*v"), + file.canonicalize().unwrap().with_extension("log").as_os_str() + ] + ); + } + + #[test] + fn an_unavailable_msi_is_refused_before_starting_the_installer() { + let dir = tempfile::tempdir().unwrap(); + let missing = dir.path().join("missing.msi"); + let error = msi_command(Path::new("msiexec.exe"), &missing).unwrap_err(); + assert!(error.contains("couldn't resolve the Windows Installer package"), "{error}"); + } + + #[cfg(windows)] + #[test] + fn a_short_local_msi_path_keeps_ordinary_windows_syntax() { + let dir = tempfile::tempdir().unwrap(); + let file = dir.path().join("package.msi"); + std::fs::write(&file, b"test package").unwrap(); + let resolved = msi_path(&file).unwrap(); + assert!(!resolved.as_os_str().to_string_lossy().starts_with(r"\\?\"), "{resolved:?}"); + assert_eq!(std::fs::read(resolved).unwrap(), b"test package"); + } + + #[test] + fn selected_folders_must_exist_and_leave_no_probe_files() { + let dir = tempfile::tempdir().unwrap(); + assert_eq!(selected_folder(dir.path().to_str().unwrap(), true).unwrap(), dir.path()); + assert_eq!(std::fs::read_dir(dir.path()).unwrap().count(), 0); + #[cfg(unix)] + { + let quoted = dir.path().join("Craft \"Apps\""); + std::fs::create_dir("ed).unwrap(); + assert_eq!(selected_folder(quoted.to_str().unwrap(), true).unwrap(), quoted); + } + let missing = dir.path().join("unavailable/Apps"); + assert!(selected_folder(missing.to_str().unwrap(), true).is_err()); + assert!(!missing.exists()); + assert!(selected_folder("relative/folder", true).is_err()); + assert!(selected_folder("bad\nfolder", true).is_err()); + let app = App::by_id("photocraft").unwrap(); + let locations = InstallLocations { root: Some(dir.path().display().to_string()), ..Default::default() }; + assert_eq!(destination(app, PackageKind::Msi, None, None, &locations).unwrap(), Some(dir.path().join("PhotoCraft"))); + assert_eq!(destination(app, PackageKind::PortableZip, None, None, &locations).unwrap(), Some(dir.path().to_path_buf())); + let old = record("0.5.0", PackageKind::PortableZip, dir.path().join("PhotoCraft/photocraft.exe"), vec![], false); + let unavailable = InstallLocations { root: Some(dir.path().join("missing").display().to_string()), ..Default::default() }; + assert_eq!(destination(app, PackageKind::PortableZip, None, Some(&old), &unavailable).unwrap(), None); + let msi = Installed { + version: None, + source: InstallSource::Msi, + location: dir.path().join("MSI/PhotoCraft/photocraft.exe").display().to_string(), + package: None, + }; + assert_eq!(destination(app, PackageKind::Msi, Some(&msi), Some(&old), &unavailable).unwrap(), Some(dir.path().join("MSI/PhotoCraft"))); + assert!(destination(app, PackageKind::TarGz, None, None, &locations).unwrap_err().contains("aren't supported")); + } + + #[test] + fn updates_stay_beside_the_launchers_copy_and_new_installs_use_the_chosen_folder() { + let dir = tempfile::tempdir().unwrap(); + let app = App::by_id("photocraft").unwrap(); + let path = dir.path().join("old.AppImage"); + let rec = record("0.5.0", PackageKind::AppImage, path.clone(), vec![], false); + let launcher = Installed { version: None, source: InstallSource::Launcher, location: path.display().to_string(), package: None }; + let users = Installed { version: None, source: InstallSource::AppImage, location: "/home/me/AppImages/p.AppImage".into(), package: None }; + let chosen = dir.path().join("New apps"); + std::fs::create_dir(&chosen).unwrap(); + let mut locations = InstallLocations { root: Some(chosen.display().to_string()), ..Default::default() }; + assert_eq!(destination(app, PackageKind::AppImage, Some(&launcher), Some(&rec), &locations).unwrap(), Some(dir.path().into())); + assert_eq!(destination(app, PackageKind::AppImage, Some(&users), None, &locations).unwrap(), None); + if !process::in_sandbox() { + assert_eq!(destination(app, PackageKind::AppImage, None, None, &locations).unwrap(), Some(chosen.clone())); + locations.per_app.insert(app.id.into(), dir.path().display().to_string()); + assert_eq!(destination(app, PackageKind::Dmg, None, None, &locations).unwrap(), Some(dir.path().into())); + } + } + + #[test] + fn msi_folder_assignment_preserves_spaces_unicode_and_root_separators() { + for (value, argument) in [ + (r"D:\", r#"INSTALLFOLDER="D:\""#), + (r"D:\Craft Apps\PhotoCraft", r#"INSTALLFOLDER="D:\Craft Apps\PhotoCraft""#), + (r"\\server\apps\绘画", r#"INSTALLFOLDER="\\server\apps\绘画""#), + (r"D:\Craft Apps\", r#"INSTALLFOLDER="D:\Craft Apps\""#), + ] { + assert_eq!(msi_folder_argument(Path::new(value)).unwrap(), argument); + } + assert!(msi_folder_argument(Path::new("D:\\bad\" TRANSFORMS=evil")).is_err()); + assert!(msi_folder_argument(Path::new("D:\\bad\nfolder")).is_err()); + } + + #[cfg(target_os = "linux")] + #[test] + fn appimage_updates_replace_only_the_recorded_file_in_the_same_folder() { + let dir = tempfile::tempdir().unwrap(); + let folder = dir.path().join("Craft Apps"); + std::fs::create_dir(&folder).unwrap(); + let download = dir.path().join("download"); + std::fs::write(&download, "first").unwrap(); + let unrelated = folder.join("photocraft-unrelated.AppImage"); + std::fs::write(&unrelated, "user copy").unwrap(); + let (old, removed) = linux::install_appimage(&download, "photocraft-0.5.0.AppImage", Some(&folder), None, false).unwrap(); + assert!(removed.is_empty()); + assert_eq!(std::fs::read(&old).unwrap(), b"first"); + let rec = record("0.5.0", PackageKind::AppImage, old.clone(), vec![], false); + std::fs::write(&download, "updated").unwrap(); + let (new, removed) = linux::install_appimage(&download, "photocraft-0.6.0.AppImage", Some(&folder), Some(&rec), false).unwrap(); + assert_eq!(new, folder.join("photocraft-0.6.0.AppImage")); + assert_eq!(std::fs::read(&new).unwrap(), b"updated"); + assert_eq!(removed.as_slice(), std::slice::from_ref(&old)); + assert!(!old.exists()); + assert_eq!(std::fs::read(&unrelated).unwrap(), b"user copy"); + assert!(linux::install_appimage(&download, "photocraft-unrelated.AppImage", Some(&folder), Some(&rec), false).is_err()); + assert_eq!(std::fs::read(&unrelated).unwrap(), b"user copy"); + let new_rec = record("0.6.0", PackageKind::AppImage, new.clone(), vec![], false); + assert!(linux::install_appimage(&download, "photocraft-0.6.0.AppImage", Some(&folder), Some(&new_rec), false).is_ok()); + assert_eq!(std::fs::read_dir(&folder).unwrap().count(), 2); + let link = folder.join("linked.AppImage"); + std::os::unix::fs::symlink(&unrelated, &link).unwrap(); + let link_rec = record("0.6.0", PackageKind::AppImage, link, vec![], false); + assert!(linux::install_appimage(&download, "linked.AppImage", Some(&folder), Some(&link_rec), false).is_err()); + assert_eq!(std::fs::read(&unrelated).unwrap(), b"user copy"); + } + + #[cfg(target_os = "linux")] + #[test] + fn an_appimage_writes_over_a_users_file_only_when_asked_to() { + let dir = tempfile::tempdir().unwrap(); + let download = dir.path().join("download"); + std::fs::write(&download, "new").unwrap(); + let folder = dir.path().join("Apps"); + std::fs::create_dir(&folder).unwrap(); + let theirs = folder.join("photocraft.AppImage"); + let personal = dir.path().join("personal.txt"); + std::fs::write(&theirs, "user copy").unwrap(); + std::fs::write(&personal, "user file").unwrap(); + std::os::unix::fs::symlink(&personal, folder.join(".photocraft.AppImage.partial")).unwrap(); + assert!(linux::install_appimage(&download, "photocraft.AppImage", Some(&folder), None, false).is_err()); + assert_eq!(std::fs::read(&theirs).unwrap(), b"user copy"); + linux::install_appimage(&download, "photocraft.AppImage", Some(&folder), None, true).unwrap(); + assert_eq!(std::fs::read(&theirs).unwrap(), b"new"); + assert_eq!(std::fs::read(&personal).unwrap(), b"user file"); + assert_eq!(std::fs::read_dir(&folder).unwrap().count(), 2, "staging was left behind"); + } + + #[test] + fn a_new_install_reports_what_it_would_write_over() { + let dir = tempfile::tempdir().unwrap(); + let app = App::by_id("photocraft").unwrap(); + let paths = Paths::under(dir.path()); + let locations = InstallLocations { root: Some(dir.path().display().to_string()), ..Default::default() }; + let asset = Asset { name: "photocraft.zip".into(), ..Default::default() }; + let occupied = |previous| occupied(app, &asset, PackageKind::PortableZip, None, previous, &locations, &paths).unwrap(); + assert_eq!(occupied(None), None); + std::fs::create_dir(dir.path().join("photocraft")).unwrap(); + assert_eq!(occupied(None), Some(dir.path().join("photocraft"))); + let old = record("0.5.0", PackageKind::PortableZip, dir.path().join("photocraft/photocraft.exe"), vec![], false); + assert_eq!(occupied(Some(&old)), None); + } + + #[test] + fn swap_keeps_the_old_copy_until_the_new_one_is_in_place() { + let dir = tempfile::tempdir().unwrap(); + let dest = dir.path().join("app"); + let staged = dir.path().join("app.new"); + std::fs::create_dir(&dest).unwrap(); + std::fs::write(dest.join("v"), "old").unwrap(); + std::fs::create_dir(&staged).unwrap(); + std::fs::write(staged.join("v"), "new").unwrap(); + swap_into_place(&staged, &dest).unwrap(); + assert_eq!(std::fs::read_to_string(dest.join("v")).unwrap(), "new"); + assert!(!staged.exists() && !dir.path().join("app.old").exists()); + assert_eq!(std::fs::read_dir(dir.path()).unwrap().count(), 1); + // A missing staged copy leaves the installed one alone. + assert!(swap_into_place(&staged, &dest).is_err()); + assert_eq!(std::fs::read_to_string(dest.join("v")).unwrap(), "new"); + assert_eq!(std::fs::read_dir(dir.path()).unwrap().count(), 1); + } + + #[test] + fn swap_preserves_an_unrelated_backup_file() { + let dir = tempfile::tempdir().unwrap(); + let dest = dir.path().join("app"); + let staged = dir.path().join("app.new"); + let unrelated = dest.with_extension("old"); + std::fs::write(&dest, b"installed app").unwrap(); + std::fs::write(&staged, b"updated app").unwrap(); + std::fs::write(&unrelated, b"user backup").unwrap(); + swap_into_place(&staged, &dest).unwrap(); + assert_eq!(std::fs::read(&unrelated).unwrap(), b"user backup"); + assert_eq!(std::fs::read(&dest).unwrap(), b"updated app"); + assert_eq!(std::fs::read_dir(dir.path()).unwrap().count(), 2); + } + + #[test] + fn swap_preserves_an_unrelated_backup_directory() { + let dir = tempfile::tempdir().unwrap(); + let dest = dir.path().join("app"); + let staged = dir.path().join("app.new"); + let unrelated = dest.with_extension("old"); + std::fs::create_dir(&dest).unwrap(); + std::fs::write(dest.join("v"), b"installed app").unwrap(); + std::fs::create_dir(&staged).unwrap(); + std::fs::write(staged.join("v"), b"updated app").unwrap(); + std::fs::create_dir(&unrelated).unwrap(); + std::fs::write(unrelated.join("keep.txt"), b"user backup").unwrap(); + swap_into_place(&staged, &dest).unwrap(); + assert_eq!(std::fs::read(unrelated.join("keep.txt")).unwrap(), b"user backup"); + assert_eq!(std::fs::read(dest.join("v")).unwrap(), b"updated app"); + assert_eq!(std::fs::read_dir(dir.path()).unwrap().count(), 2); + } + + #[cfg(unix)] + #[test] + fn swap_preserves_a_dangling_backup_symlink() { + let dir = tempfile::tempdir().unwrap(); + let dest = dir.path().join("app"); + let staged = dir.path().join("app.new"); + let unrelated = dest.with_extension("old"); + let target = dir.path().join("missing"); + std::fs::write(&dest, b"installed app").unwrap(); + std::fs::write(&staged, b"updated app").unwrap(); + std::os::unix::fs::symlink(&target, &unrelated).unwrap(); + swap_into_place(&staged, &dest).unwrap(); + assert_eq!(std::fs::read_link(&unrelated).unwrap(), target); + assert_eq!(std::fs::read(&dest).unwrap(), b"updated app"); + } + + #[test] + fn a_failed_rollback_keeps_the_old_app_and_reports_its_backup_path() { + let dir = tempfile::tempdir().unwrap(); + let dest = dir.path().join("app"); + let staged = dir.path().join("app.new"); + std::fs::write(&dest, b"installed app").unwrap(); + std::fs::write(&staged, b"updated app").unwrap(); + let calls = std::cell::Cell::new(0); + let err = swap_into_place_via(&staged, &dest, |from, to| { + calls.set(calls.get() + 1); + match calls.get() { + 1 => std::fs::rename(from, to), + 2 => Err(std::io::Error::other("staged move failed")), + _ => Err(std::io::Error::other("restore failed")), + } + }) + .unwrap_err(); + assert_eq!(calls.get(), 3); + assert!(err.contains("staged move failed") && err.contains("restore failed")); + let backup = std::fs::read_dir(dir.path()).unwrap().map(|e| e.unwrap().path()).find(|p| p.is_dir()).unwrap(); + let saved = backup.join("old"); + assert_eq!(std::fs::read(&saved).unwrap(), b"installed app"); + assert!(err.contains(&saved.display().to_string())); + assert_eq!(std::fs::read(&staged).unwrap(), b"updated app"); + } + + #[test] + fn a_failed_install_keeps_the_download_for_the_next_try() { + use crate::http::tests::{Reply, body, serve}; + let base = App::by_id("wordcraft").unwrap(); + let wordcraft = App { icon_png: Some(b"\x89PNG\r\n\x1a\nfake-png-bytes"), ..*base }; + let app = &wordcraft; + let (body, sha) = body(); + let (url, requests) = serve(body.clone(), vec![Reply::Full, Reply::Full]); + let asset = Asset { name: "wordcraft-0.3.0-test.pkg".into(), size: body.len() as u64, url, sha256: Some(sha) }; + let release = Release { assets: vec![asset.clone()], ..Release::default() }; + let dir = tempfile::tempdir().unwrap(); + let paths = Paths::under(dir.path()); + let http = Http::new(crate::TEST_AGENT).unwrap(); + // A format no build of the launcher installs here stands in for any failing install step. + let kind = if cfg!(windows) { PackageKind::Dmg } else { PackageKind::Msi }; + let mut phases = Vec::new(); + for _ in 0..2 { + let mut phase = |p: JobPhase| phases.push(p); + let mut job = Job { + http: &http, + paths: &paths, + stop: &Stop::default(), + phase: &mut phase, + install_locations: &InstallLocations::default(), + replace: false, + }; + let err = install(app, &release, &asset, kind, None, None, &mut job).unwrap_err(); + assert!(err.contains("isn't supported"), "{err}"); + assert_eq!(std::fs::read(download_path(&paths, &asset)).unwrap(), body); + } + assert_eq!(requests.lock().unwrap().len(), 1, "the second try reuses the kept file"); + assert_eq!(phases.iter().filter(|p| **p == JobPhase::Installing).count(), 2); + } + + #[test] + fn an_unavailable_custom_folder_is_rejected_before_downloading() { + use crate::http::tests::{Reply, body, serve}; + let base = App::by_id("wordcraft").unwrap(); + let wordcraft = App { icon_png: Some(b"\x89PNG\r\n\x1a\nfake-png-bytes"), ..*base }; + let app = &wordcraft; + let (body, sha) = body(); + let (url, requests) = serve(body.clone(), vec![Reply::Full]); + let asset = Asset { name: "wordcraft-0.3.0-linux-x86_64.AppImage".into(), size: body.len() as u64, url, sha256: Some(sha) }; + let release = Release { assets: vec![asset.clone()], ..Release::default() }; + let dir = tempfile::tempdir().unwrap(); + let missing = dir.path().join("unavailable/Apps"); + let locations = InstallLocations { root: Some(missing.display().to_string()), ..Default::default() }; + let paths = Paths::under(dir.path()); + let http = Http::new(crate::TEST_AGENT).unwrap(); + let mut phases = vec![]; + let mut phase = |p| phases.push(p); + let mut job = Job { http: &http, paths: &paths, stop: &Stop::default(), phase: &mut phase, install_locations: &locations, replace: false }; + let error = install(app, &release, &asset, PackageKind::AppImage, None, None, &mut job).unwrap_err(); + assert!(error.contains("folder is unavailable") || (process::in_sandbox() && error.contains("aren't supported")), "{error}"); + assert!(requests.lock().unwrap().is_empty()); + assert!(phases.is_empty()); + assert!(!missing.exists()); + } + + #[test] + fn a_release_without_a_checksum_is_refused_before_anything_is_fetched() { + use crate::http::tests::{Reply, body, serve}; + let base = App::by_id("wordcraft").unwrap(); + let wordcraft = App { icon_png: Some(b"\x89PNG\r\n\x1a\nfake-png-bytes"), ..*base }; + let app = &wordcraft; + let (body, _) = body(); + let (url, requests) = serve(body.clone(), vec![Reply::Full]); + // Neither a per-file digest nor a SHA256SUMS.txt in the release. + let asset = Asset { name: "wordcraft-0.3.1-test.pkg".into(), size: body.len() as u64, url, sha256: None }; + let release = Release { assets: vec![asset.clone()], ..Release::default() }; + let dir = tempfile::tempdir().unwrap(); + let paths = Paths::under(dir.path()); + let http = Http::new(crate::TEST_AGENT).unwrap(); + let kind = if cfg!(windows) { PackageKind::Dmg } else { PackageKind::Msi }; + let mut phases = Vec::new(); + let mut phase = |p: JobPhase| phases.push(p); + let mut job = Job { + http: &http, + paths: &paths, + stop: &Stop::default(), + phase: &mut phase, + install_locations: &InstallLocations::default(), + replace: false, + }; + let err = install(app, &release, &asset, kind, None, None, &mut job).unwrap_err(); + assert!(err.contains("publishes no checksum"), "{err}"); + assert!(!download_path(&paths, &asset).exists()); + assert!(requests.lock().unwrap().is_empty(), "nothing was fetched"); + assert!(!phases.contains(&JobPhase::Installing)); + } + + #[cfg(unix)] + fn tarball(path: &Path, entries: &[&str]) { + let f = std::fs::File::create(path).unwrap(); + let mut b = tar::Builder::new(flate2::write::GzEncoder::new(f, flate2::Compression::fast())); + for name in entries { + let mut h = tar::Header::new_gnu(); + h.set_size(2); + h.set_mode(0o755); + h.set_cksum(); + b.append_data(&mut h, name, &b"hi"[..]).unwrap(); + } + b.into_inner().unwrap().finish().unwrap(); + } + + #[cfg(unix)] + #[test] + fn lays_out_both_freebsd_tarball_styles_like_usr_local() { + let base = App::by_id("wordcraft").unwrap(); + let wordcraft = App { icon_png: Some(b"\x89PNG\r\n\x1a\nfake-png-bytes"), ..*base }; + let app = &wordcraft; + let dir = tempfile::tempdir().unwrap(); + let tree_style = dir.path().join("tree.tar.gz"); + tarball( + &tree_style, + &["wordcraft-0.3.0-freebsd-x86_64/bin/wordcraft", "wordcraft-0.3.0-freebsd-x86_64/share/applications/ai.storyteller.wordcraft.desktop"], + ); + let flat_style = dir.path().join("flat.tar.gz"); + tarball( + &flat_style, + &[ + "wordcraft-0.3.0-freebsd-x86_64/wordcraft", + "wordcraft-0.3.0-freebsd-x86_64/wordcraft-cli", + "wordcraft-0.3.0-freebsd-x86_64/README.md", + "wordcraft-0.3.0-freebsd-x86_64/ai.storyteller.wordcraft.desktop", + "wordcraft-0.3.0-freebsd-x86_64/ai.storyteller.wordcraft.png", + ], + ); + for file in [tree_style, flat_style] { + let root = tree::unpack(app, &file, &dir.path().join("staging")).unwrap(); + let files = tree::files(&root).unwrap(); + assert!(files.contains(&PathBuf::from("bin/wordcraft")), "{files:?}"); + assert!(files.contains(&PathBuf::from("share/applications/ai.storyteller.wordcraft.desktop")), "{files:?}"); + assert!(!files.iter().any(|f| f.ends_with("README.md")), "{files:?}"); + } + let bad = dir.path().join("bad.tar.gz"); + tarball(&bad, &["x/README.md"]); + assert!(tree::unpack(app, &bad, &dir.path().join("staging")).is_err()); + } + + #[cfg(unix)] + #[test] + fn tree_files_does_not_traverse_directory_symlinks() { + let dir = tempfile::tempdir().unwrap(); + let root = dir.path().join("root"); + let external = dir.path().join("external"); + std::fs::create_dir_all(&root).unwrap(); + std::fs::create_dir_all(&external).unwrap(); + std::fs::write(root.join("regular.txt"), b"hi").unwrap(); + std::fs::write(external.join("secret.txt"), b"secret").unwrap(); + std::os::unix::fs::symlink(&external, root.join("link_to_external")).unwrap(); + + let list = tree::files(&root).unwrap(); + assert_eq!(list, vec![PathBuf::from("link_to_external"), PathBuf::from("regular.txt")]); + } + + #[cfg(target_os = "freebsd")] + #[test] + fn remove_files_refuses_escaping_paths() { + assert!(freebsd::remove_files(&[PathBuf::from("/etc/shadow")], false).is_err()); + if let Some(home) = dirs::home_dir() { + let prefix = home.join(".local"); + assert!(freebsd::remove_files(&[prefix.join("../escaped")], false).is_err()); + } + } + + #[test] + fn single_top_folder_rejects_symlink() { + let dir = tempfile::tempdir().unwrap(); + let real_dir = dir.path().join("real"); + std::fs::create_dir_all(&real_dir).unwrap(); + let staging = dir.path().join("staging"); + std::fs::create_dir_all(&staging).unwrap(); + #[cfg(unix)] + { + std::os::unix::fs::symlink(&real_dir, staging.join("symlink_dir")).unwrap(); + let res = single_top_folder(&staging).unwrap(); + assert_eq!(res, staging, "a directory symlink must not be chosen as top folder"); + } + } + + #[cfg(target_os = "linux")] + #[test] + fn a_tarball_install_lives_in_the_launchers_folder_and_keeps_the_apps_home() { + let base = App::by_id("wordcraft").unwrap(); + let wordcraft = App { icon_png: Some(b"\x89PNG\r\n\x1a\nfake-png-bytes"), ..*base }; + let app = &wordcraft; + let dir = tempfile::tempdir().unwrap(); + let paths = Paths::under(dir.path()); + let file = dir.path().join("wordcraft-0.3.0-linux-x86_64.tar.gz"); + tarball( + &file, + &[ + "wordcraft-0.3.0-linux-x86_64/bin/wordcraft", + "wordcraft-0.3.0-linux-x86_64/bin/wordcraft-cli", + "wordcraft-0.3.0-linux-x86_64/share/applications/ai.storyteller.wordcraft.desktop", + ], + ); + + let bin = linux::install_tree(app, &file, &paths).unwrap(); + let root = paths.apps().join("wordcraft"); + assert_eq!(bin, root.join("bin").join("wordcraft")); + assert!(bin.is_file(), "the app's binary wasn't unpacked"); + assert!(root.join("share/applications/ai.storyteller.wordcraft.desktop").is_file(), "share/ wasn't unpacked"); + assert!(!paths.cache.join("wordcraft.new").exists(), "the staging folder was left behind"); + + // The app's own home sits beside the tree, and an update keeps it. + let home = paths.app_home(app.id); + std::fs::create_dir_all(home.join(".config/wordcraft")).unwrap(); + std::fs::write(home.join(".config/wordcraft/settings.toml"), "keep me").unwrap(); + linux::install_tree(app, &file, &paths).unwrap(); + assert_eq!(std::fs::read_to_string(home.join(".config/wordcraft/settings.toml")).unwrap(), "keep me"); + + // Removing the install takes the tree and nothing else. + linux::remove_tree(app, &paths).unwrap(); + assert!(!bin.exists() && !root.join("share").exists()); + assert!(home.join(".config/wordcraft/settings.toml").is_file(), "the app's settings were removed with it"); + assert!(linux::remove_tree(app, &paths).is_err(), "a second removal says there is nothing there"); + } + + #[cfg(target_os = "linux")] + #[test] + fn a_tarball_binary_that_is_a_symlink_out_of_the_tree_is_refused() { + use std::io::Write; + use std::os::unix::fs::PermissionsExt; + let base = App::by_id("wordcraft").unwrap(); + let wordcraft = App { icon_png: Some(b"\x89PNG\r\n\x1a\nfake-png-bytes"), ..*base }; + let app = &wordcraft; + let dir = tempfile::tempdir().unwrap(); + let paths = Paths::under(dir.path()); + + // A working install the rejected update must leave untouched. + let good = dir.path().join("wordcraft-0.3.0-linux-x86_64.tar.gz"); + tarball(&good, &["wordcraft-0.3.0-linux-x86_64/bin/wordcraft"]); + let bin = linux::install_tree(app, &good, &paths).unwrap(); + assert_eq!(std::fs::read_to_string(&bin).unwrap(), "hi"); + + // A file outside the install tree that the archive tries to chmod through a symlink. + let outside = dir.path().join("victim"); + std::fs::write(&outside, b"secret").unwrap(); + std::fs::set_permissions(&outside, std::fs::Permissions::from_mode(0o600)).unwrap(); + + // A tarball whose bin/wordcraft is a symlink to that external file. + let file = dir.path().join("evil.tar.gz"); + let f = std::fs::File::create(&file).unwrap(); + let mut b = tar::Builder::new(flate2::write::GzEncoder::new(f, flate2::Compression::fast())); + let mut link = tar::Header::new_gnu(); + link.set_entry_type(tar::EntryType::Symlink); + link.set_size(0); + link.set_mode(0o777); + link.set_cksum(); + b.append_link(&mut link, "wordcraft-0.3.0-linux-x86_64/bin/wordcraft", &outside).unwrap(); + // A second real file so the top folder is detected. + let mut reg = tar::Header::new_gnu(); + reg.set_size(2); + reg.set_mode(0o644); + reg.set_cksum(); + b.append_data(&mut reg, "wordcraft-0.3.0-linux-x86_64/share/applications/ai.storyteller.wordcraft.desktop", &b"hi"[..]).unwrap(); + b.into_inner().unwrap().finish().unwrap(); + drop(std::io::stdout().flush()); + + let result = linux::install_tree(app, &file, &paths); + assert!(result.is_err(), "a symlink binary must be rejected: {result:?}"); + assert!(result.unwrap_err().contains("symlink")); + // The external file keeps its original mode: the chmod never reached it. + let mode = std::fs::metadata(&outside).unwrap().permissions().mode() & 0o777; + assert_eq!(mode, 0o600, "the external file's permissions must be untouched"); + // The rejected update left the previously installed, working binary fully intact. + assert!(bin.is_file(), "the rejected update must not remove the working binary"); + assert_eq!(std::fs::read_to_string(&bin).unwrap(), "hi"); + } + + #[cfg(target_os = "linux")] + #[test] + fn an_app_sees_its_own_home_and_the_xdg_folders_inside_it() { + let home = PathBuf::from("/data/apps/photocraft/home"); + let env = home_env(&home); + assert_eq!(env[0], ("HOME", home.clone())); + for (name, dir) in &env[1..] { + assert!(dir.starts_with(&home), "{name}: {}", dir.display()); + } + for wanted in ["XDG_CONFIG_HOME", "XDG_DATA_HOME", "XDG_CACHE_HOME", "XDG_STATE_HOME"] { + assert!(env.iter().any(|(name, _)| *name == wanted), "{wanted} isn't set"); + } + } + + #[cfg(any(target_os = "linux", target_os = "freebsd"))] + #[test] + fn root_commands_run_programs_by_absolute_path_from_the_given_dirs() { + use std::ffi::OsStr; + let root = tempfile::tempdir().unwrap(); + let (usr_bin, sbin) = (root.path().join("usr/bin"), root.path().join("sbin")); + std::fs::create_dir_all(&usr_bin).unwrap(); + std::fs::create_dir_all(&sbin).unwrap(); + std::fs::write(usr_bin.join("rpm"), b"").unwrap(); + std::fs::write(sbin.join("rpm"), b"").unwrap(); + std::fs::write(sbin.join("zypper"), b"").unwrap(); + let dirs = [&usr_bin, &sbin]; + let run = |args: &[&str]| { + let args: Vec<&OsStr> = args.iter().map(OsStr::new).collect(); + with_absolute_program(&dirs, &args).map(|a| a.into_iter().map(PathBuf::from).collect::>()) + }; + + // The first directory that has the program wins, and the arguments follow unchanged. + assert_eq!(run(&["rpm", "-e", "wordcraft"]), Ok(vec![usr_bin.join("rpm"), "-e".into(), "wordcraft".into()])); + assert_eq!(run(&["zypper"]), Ok(vec![sbin.join("zypper")])); + // A program that isn't in those directories isn't looked for anywhere else. + assert_eq!(run(&["dnf", "remove"]), Err("dnf isn't installed".into())); + // An absolute path is kept; a relative path with folders in it is refused. + assert_eq!(run(&["/bin/cp", "-R"]), Ok(vec!["/bin/cp".into(), "-R".into()])); + assert!(run(&["../usr/bin/rpm"]).is_err()); + assert!(run(&["bin/rpm"]).is_err()); + assert!(run(&[]).is_err()); + } + + #[cfg(target_os = "linux")] + #[test] + fn several_debs_fit_one_apt_call_and_share_its_result() { + // The files are appended to this, so it must end where apt expects packages. + assert_eq!( + linux::install_command(PackageKind::Deb), + Some(&["apt-get", "-o", "DPkg::Lock::Timeout=60", "install", "-y", "--allow-downgrades"][..]) + ); + assert_eq!(linux::install_command(PackageKind::Flatpak), None); + let failed = || Err("the system installer stopped with error 100".to_string()); + assert_eq!(linux::shared_outcome(Ok(()), 3), Ok(())); + assert_eq!(linux::shared_outcome(failed(), 1), failed()); + assert_eq!( + linux::shared_outcome(failed(), 3), + Err("the system installer stopped with error 100 while handling 3 apps together. Try them one at a time.".into()) + ); + // A dismissed password prompt stays a cancellation for every app, not an error. + assert_eq!(linux::shared_outcome(Err("cancelled".into()), 3), Err("cancelled".into())); + } + + /// Installs two `.deb` files that reach the installer together (both wait behind a running + /// install), with a stand-in for `pkexec` that exits with `code`. Returns every argument + /// list that reached it, and each job's result. + #[cfg(target_os = "linux")] + fn two_debs_through_pkexec(code: i32) -> (Vec>, Vec>) { + use std::sync::{Mutex, mpsc}; + let queue = crate::queue::Queue::new(); + let ran = Mutex::new(Vec::new()); + let run = |args: &[&std::ffi::OsStr]| { + ran.lock().unwrap().push(args.iter().map(|a| a.to_string_lossy().into_owned()).collect::>()); + root_outcome(Some(code)) + }; + let root = linux::Root { queue: &queue, run: &run }; + let (started, has_started) = mpsc::channel(); + let (release, released) = mpsc::channel::<()>(); + let (waiting, waits) = mpsc::channel(); + let results = std::thread::scope(|scope| { + let (queue, root) = (&queue, &root); + scope.spawn(move || { + queue.alone(&|| false, &mut |_| {}, || { + started.send(()).unwrap(); + released.recv().unwrap(); + }); + }); + has_started.recv().unwrap(); + let jobs = ["/downloads/a.deb", "/downloads/b.deb"].map(|file| { + let waiting = waiting.clone(); + scope.spawn(move || { + let mut phase = |p: JobPhase| { + if p == JobPhase::Waiting { + waiting.send(()).unwrap(); + } + }; + linux::install_package_via(root, PackageKind::Deb, Path::new(file), &Stop::default(), &mut phase) + }) + }); + waits.recv().unwrap(); + waits.recv().unwrap(); + release.send(()).unwrap(); + jobs.map(|j| j.join().unwrap()).to_vec() + }); + (ran.into_inner().unwrap(), results) + } + + #[cfg(target_os = "linux")] + #[test] + fn two_debs_reach_root_as_one_apt_call() { + let (ran, results) = two_debs_through_pkexec(0); + assert_eq!(ran.len(), 1, "{ran:?}"); + let (command, files) = ran[0].split_at(6); + assert_eq!(command, ["apt-get", "-o", "DPkg::Lock::Timeout=60", "install", "-y", "--allow-downgrades"]); + let mut files = files.to_vec(); + files.sort(); + assert_eq!(files, ["/downloads/a.deb", "/downloads/b.deb"]); + assert_eq!(results, [Ok(()), Ok(())]); + + // The password prompt was dismissed: one call, and both apps are cancelled, not failed. + let (ran, results) = two_debs_through_pkexec(126); + assert_eq!(ran.len(), 1, "{ran:?}"); + assert_eq!(results, vec![Err("cancelled".to_string()); 2]); + + // apt failed: both apps say so, and how to find the one at fault. + let (ran, results) = two_debs_through_pkexec(100); + assert_eq!(ran.len(), 1, "{ran:?}"); + let error = "the system installer stopped with error 100 while handling 2 apps together. Try them one at a time."; + assert_eq!(results, vec![Err(error.to_string()); 2]); + } + + #[cfg(target_os = "linux")] + #[test] + fn flatpak_bundle_install_failure_reports_clean_error() { + if !crate::process::is_flatpak_available() { + return; + } + let queue = crate::queue::Queue::new(); + let root = linux::Root { queue: &queue, run: &|_| Ok(()) }; + let result = linux::install_package_via(&root, PackageKind::Flatpak, Path::new("/nonexistent/file.flatpak"), &Stop::default(), &mut |_| ()); + assert!(result.is_err()); + let err = result.unwrap_err(); + assert!(err.starts_with("Flatpak couldn't install the bundle: "), "unexpected error: {err}"); + } + + #[cfg(target_os = "linux")] + #[test] + fn adds_and_removes_only_its_own_menu_entry_and_icon() { + let base = App::by_id("wordcraft").unwrap(); + let wordcraft = App { icon_png: Some(b"\x89PNG\r\n\x1a\nfake-png-bytes"), ..*base }; + let app = &wordcraft; + let root = tempfile::tempdir().unwrap(); + let data = root.path().join("share"); + let desktop = data.join("applications/ai.storyteller.wordcraft.desktop"); + let icon = data.join("icons/hicolor/128x128/apps/ai.storyteller.wordcraft.png"); + let old = root.path().join("My Apps/wordcraft-0.2.0-linux-x86_64.AppImage"); + let new = root.path().join("My Apps/wordcraft-0.3.0-linux-x86_64.AppImage"); + std::fs::create_dir_all(new.parent().unwrap()).unwrap(); + std::fs::write(&new, b"").unwrap(); + + // Fresh install: entry and icon written and recorded. + let files = linux::integrate_into(&data, app, &new, &[], None); + assert_eq!(files, [icon.clone(), desktop.clone()]); + let text = std::fs::read_to_string(&desktop).unwrap(); + assert!(text.contains("Exec=\"") && text.contains("My Apps/wordcraft-0.3.0-linux-x86_64.AppImage\" %F\n"), "{text}"); + assert!(text.contains("Icon=ai.storyteller.wordcraft\n")); + assert!(std::fs::read(&icon).unwrap().starts_with(b"\x89PNG")); + let rec = record("0.3.0", PackageKind::AppImage, new.clone(), files, false); + + // An update rewrites both. + assert_eq!(linux::integrate_into(&data, app, &new, &[], Some(&rec)).len(), 2); + + // Someone else's entry and icon are left alone (and not recorded). + std::fs::write(&desktop, "[Desktop Entry]\nExec=/usr/bin/wordcraft\n").unwrap(); + std::fs::write(&icon, b"theirs").unwrap(); + assert!(linux::integrate_into(&data, app, &new, &[], None).is_empty()); + assert_eq!(std::fs::read(&icon).unwrap(), b"theirs"); + // ...unless the entry runs a copy this install just replaced (the app wrote it itself). + std::fs::write(&desktop, format!("[Desktop Entry]\nExec=\"{}\" %F\n", old.display())).unwrap(); + assert_eq!(linux::integrate_into(&data, app, &new, std::slice::from_ref(&old), None), std::slice::from_ref(&desktop)); + assert!(desktop_entry_is_ours(&desktop)); + + // Uninstall: an icon the record doesn't list stays; the entry for the removed copy goes. + std::fs::remove_file(&new).unwrap(); + linux::unintegrate_from(&data, app, &new, None); + assert!(!desktop.exists()); + assert_eq!(std::fs::read(&icon).unwrap(), b"theirs"); + // With the record, the launcher's icon goes too. + std::fs::remove_file(&icon).unwrap(); + std::fs::write(&new, b"").unwrap(); + let files = linux::integrate_into(&data, app, &new, &[], None); + let rec = record("0.3.0", PackageKind::AppImage, new.clone(), files, false); + std::fs::remove_file(&new).unwrap(); + linux::unintegrate_from(&data, app, &new, Some(&rec)); + assert!(!desktop.exists() && !icon.exists()); + // Someone else's entry survives an uninstall. + std::fs::write(&desktop, "[Desktop Entry]\nExec=/usr/bin/wordcraft\n").unwrap(); + linux::unintegrate_from(&data, app, &new, Some(&rec)); + assert!(desktop.exists()); + } + + #[cfg(target_os = "linux")] + fn desktop_entry_is_ours(path: &Path) -> bool { + crate::desktop_entry::is_ours(&std::fs::read_to_string(path).unwrap()) + } + + #[cfg(target_os = "linux")] + #[test] + fn immutable_ostree_offers_no_deb_or_rpm() { + if !Path::new("/run/ostree-booted").exists() { + return; + } + let kinds = available_kinds(); + assert!(!kinds.contains(&PackageKind::Rpm), "{kinds:?}"); + assert!(!kinds.contains(&PackageKind::Deb), "{kinds:?}"); + assert!(kinds.contains(&PackageKind::AppImage), "{kinds:?}"); + } + + #[test] + fn updates_keep_the_installed_format() { + let inst = |source| Installed { version: None, source, location: String::new(), package: None }; + let app = crate::App::by_id("photocraft").unwrap(); + let available = available_kinds(); + if let Some(first) = available.first() { + assert_eq!(choose_kinds(app, None, None, &[]).first(), Some(first)); + } + #[cfg(target_os = "macos")] + assert_eq!(choose_kinds(app, Some(&inst(InstallSource::MacApp)), None, &[]), [PackageKind::Dmg]); + #[cfg(windows)] + assert_eq!(choose_kinds(app, Some(&inst(InstallSource::Msi)), None, &[]), [PackageKind::Msi]); + #[cfg(target_os = "linux")] + assert_eq!(choose_kinds(app, Some(&inst(InstallSource::AppImage)), None, &[]), [PackageKind::AppImage]); + let _ = inst; + } + + #[test] + fn only_the_release_arch_package_is_updated_by_the_launcher() { + let pacman = |name: &str| Installed { version: None, source: InstallSource::Pacman, location: String::new(), package: Some(name.into()) }; + let app = crate::App::by_id("wordcraft").unwrap(); + assert!(is_release_arch_package(app, &pacman("ai.storyteller.wordcraft"))); + for aur in ["wordcraft", "wordcraft-bin", "wordcraft-git"] { + assert!(!is_release_arch_package(app, &pacman(aur)), "{aur}"); + } + assert_eq!(managed_elsewhere_with(app, &pacman("ai.storyteller.wordcraft"), false, |_| None), None); + assert_eq!(managed_elsewhere_with(app, &pacman("wordcraft-bin"), true, |_| None), None); + } + + #[test] + fn arch_packages_from_a_repository_are_updated_with_the_system() { + let app = crate::App::by_id("wordcraft").unwrap(); + let hint = pacman_update_hint(app, "wordcraft-bin", Some("omarchy")); + assert!(hint.starts_with("WordCraft was installed from the omarchy repository or the AUR (wordcraft-bin).")); + assert!(hint.ends_with("or sudo pacman -Syu")); + + let aur_hint = pacman_update_hint(app, "wordcraft-bin", None); + assert!(aur_hint.starts_with("WordCraft was installed from the AUR (wordcraft-bin).")); + + let si = "Repository : omarchy\nName : wordcraft-bin\nVersion : 0.6.1-1\n"; + assert_eq!(parse_pacman_repo(si).as_deref(), Some("omarchy")); + assert_eq!(parse_pacman_repo("Name : wordcraft-bin\n"), None); + assert_eq!(parse_pacman_repo("Repository :\n"), None); + } + + #[test] + fn aur_packages_are_not_updated_as_arch_pkg() { + let pacman = |name: &str| Installed { version: None, source: InstallSource::Pacman, location: String::new(), package: Some(name.into()) }; + let app = crate::App::by_id("filmcraft").unwrap(); + let installed = pacman("filmcraft-bin"); + assert!(managed_elsewhere_with(app, &installed, false, |_| None).is_some()); + assert_eq!(choose_kinds_with(app, Some(&installed), None, &[], &[], false), Vec::::new()); + assert_eq!(choose_kinds_with(app, Some(&installed), None, &[PackageKind::ArchPkg], &[PackageKind::ArchPkg], true), Vec::::new()); + } + + #[test] + fn release_arch_packages_are_updated_as_arch_pkg() { + let pacman = |name: &str| Installed { version: None, source: InstallSource::Pacman, location: String::new(), package: Some(name.into()) }; + let app = crate::App::by_id("filmcraft").unwrap(); + let installed = pacman("ai.storyteller.filmcraft"); + assert_eq!(managed_elsewhere_with(app, &installed, false, |_| None), None); + assert_eq!(choose_kinds_with(app, Some(&installed), None, &[], &[], true), vec![PackageKind::ArchPkg]); + assert_eq!(choose_kinds_with(app, Some(&installed), None, &[], &[PackageKind::ArchPkg], false), vec![PackageKind::ArchPkg]); + } + + #[test] + fn unavailable_arch_package_not_selected_for_new_install() { + let app = crate::App::by_id("filmcraft").unwrap(); + assert_eq!(choose_kinds_with(app, None, None, &[PackageKind::ArchPkg], &[], true), Vec::::new()); + assert_eq!(choose_kinds_with(app, None, None, &[PackageKind::ArchPkg], &[], false), Vec::::new()); + assert_eq!(choose_kinds_with(app, None, None, &[PackageKind::ArchPkg], &[PackageKind::ArchPkg], false), vec![PackageKind::ArchPkg]); + } + + #[test] + fn install_refuses_to_downgrade_when_target_release_is_older() { + let dir = tempfile::tempdir().unwrap(); + let paths = Paths::under(dir.path()); + let http = Http::new(crate::TEST_AGENT).unwrap(); + let phase = &mut |_| (); + let locations = InstallLocations::default(); + let app = App::by_id("photocraft").unwrap(); + let release = Release { version: version::parse("0.4.0"), tag: "v0.4.0".into(), ..Release::default() }; + let asset = Asset { name: "photocraft-0.4.0-linux-x86_64.AppImage".into(), ..Asset::default() }; + let installed = Installed { + version: version::parse("0.5.0"), + source: InstallSource::Launcher, + location: dir.path().join("PhotoCraft.AppImage").display().to_string(), + package: None, + }; + let stop = Stop::default(); + let mut job = Job { http: &http, paths: &paths, stop: &stop, phase, install_locations: &locations, replace: false }; + let result = install_at(app, &release, &asset, PackageKind::AppImage, Some(&installed), None, &mut job, None); + assert!(result.is_err()); + assert!(result.unwrap_err().contains("installed version 0.5.0 is newer")); + } + + #[test] + fn install_rejects_an_artifact_whose_version_is_not_the_advertised_one() { + // The release advertises 99.0.0, but the selected artifact is an older official build + // (filename and URL tag say 0.4.0). The guard must refuse before any download. + let dir = tempfile::tempdir().unwrap(); + let paths = Paths::under(dir.path()); + let http = Http::new(crate::TEST_AGENT).unwrap(); + let phase = &mut |_| (); + let locations = InstallLocations::default(); + let app = App::by_id("photocraft").unwrap(); + let release = Release { version: version::parse("99.0.0"), tag: "v99.0.0".into(), ..Release::default() }; + let asset = Asset { + name: "photocraft-0.4.0-linux-x86_64.AppImage".into(), + url: "https://github.com/storytold/photocraft/releases/download/v0.4.0/photocraft-0.4.0-linux-x86_64.AppImage".into(), + ..Asset::default() + }; + let stop = Stop::default(); + let mut job = Job { http: &http, paths: &paths, stop: &stop, phase, install_locations: &locations, replace: false }; + let result = install_at(app, &release, &asset, PackageKind::AppImage, None, None, &mut job, None); + assert!(result.is_err(), "mismatched artifact version must be rejected"); + assert!(result.unwrap_err().contains("does not match the advertised version")); + + // A matching artifact passes the guard (it fails later for unrelated reasons, not the bind). + let good = Asset { + name: "photocraft-99.0.0-linux-x86_64.AppImage".into(), + url: "https://github.com/storytold/photocraft/releases/download/v99.0.0/photocraft-99.0.0-linux-x86_64.AppImage".into(), + ..Asset::default() + }; + assert!(bind_artifact_version(app, &release, &good).is_ok()); + + // URL filename mismatch is rejected (CWE-494). + let bad_url_name = Asset { + name: "photocraft-99.0.0-linux-x86_64.AppImage".into(), + url: "https://github.com/storytold/photocraft/releases/download/v99.0.0/other.AppImage".into(), + ..Asset::default() + }; + assert!(bind_artifact_version(app, &release, &bad_url_name).is_err()); + + // URL tag mismatch is rejected. + let bad_tag = Asset { + name: "photocraft-99.0.0-linux-x86_64.AppImage".into(), + url: "https://github.com/storytold/photocraft/releases/download/unparsed_tag/photocraft-99.0.0-linux-x86_64.AppImage".into(), + ..Asset::default() + }; + assert!(bind_artifact_version(app, &release, &bad_tag).is_err()); + } + + #[cfg(target_os = "linux")] + #[test] + fn package_kinds_order_matches_launcher_format() { + use super::linux::order_available_kinds; + + // Flatpak launcher defaults to Flatpak if available + let kinds = order_available_kinds(true, false, true, true, false, false); + assert_eq!(kinds.first(), Some(&PackageKind::Flatpak)); + assert!(kinds.contains(&PackageKind::Deb)); + assert!(kinds.contains(&PackageKind::AppImage)); + + // AppImage launcher defaults to AppImage + let kinds = order_available_kinds(false, true, true, true, false, false); + assert_eq!(kinds.first(), Some(&PackageKind::AppImage)); + assert!(kinds.contains(&PackageKind::Deb)); + assert!(kinds.contains(&PackageKind::Flatpak)); + + // Base Linux launcher defaults to native package format (deb, rpm, or arch) if available + let kinds = order_available_kinds(false, false, true, true, false, false); + assert_eq!(kinds.first(), Some(&PackageKind::Deb)); + + let kinds = order_available_kinds(false, false, true, false, true, false); + assert_eq!(kinds.first(), Some(&PackageKind::Rpm)); + + let kinds = order_available_kinds(false, false, true, false, false, true); + assert_eq!(kinds.first(), Some(&PackageKind::ArchPkg)); + + // Base Linux launcher without root/distro packaging falls back to Flatpak then AppImage + let kinds = order_available_kinds(false, false, true, false, false, false); + assert_eq!(kinds.first(), Some(&PackageKind::Flatpak)); + assert_eq!(kinds.get(1), Some(&PackageKind::AppImage)); + + // The tarball runs inside the sandbox, so it is only offered there: with a usable host + // Flatpak it stays an alternative, without one it is the default (an AppImage cannot + // mount in a sandbox, and a distro package needs root). + let kinds = order_available_kinds(true, false, true, false, false, false); + assert_eq!(kinds.first(), Some(&PackageKind::Flatpak)); + assert_eq!(kinds.get(1), Some(&PackageKind::TarGz)); + let kinds = order_available_kinds(true, false, false, false, false, false); + assert_eq!(kinds, [PackageKind::TarGz, PackageKind::AppImage]); + for (appimage, flatpak) in [(true, true), (false, false), (false, true)] { + assert!(!order_available_kinds(false, appimage, flatpak, true, true, false).contains(&PackageKind::TarGz)); + } + } + + #[cfg(target_os = "macos")] + mod macos_checks { + use crate::{SUITE, version}; + + use super::macos::{Expect, TEAM_ID, install_dmg, signed_by}; + use super::*; + + fn photocraft() -> &'static App { + SUITE.iter().find(|a| a.id == "photocraft").unwrap() + } + + /// A minimal `PhotoCraft.app` at `dir`, reporting `version`. + fn bundle(dir: &Path, version: &str) -> PathBuf { + let app = dir.join("PhotoCraft.app"); + let contents = app.join("Contents"); + std::fs::create_dir_all(contents.join("MacOS")).unwrap(); + std::fs::write( + contents.join("Info.plist"), + format!( + "\ + CFBundleIdentifierai.storyteller.photocraft\ + CFBundleExecutablePhotoCraft\ + CFBundleShortVersionString{version}" + ), + ) + .unwrap(); + std::fs::write(contents.join("MacOS/PhotoCraft"), version).unwrap(); + app + } + + /// A release-style disk image holding a `PhotoCraft.app` that reports `version`. The + /// disk-image helper turns requests away while it is busy (`attach` retries for the same + /// reason), so `hdiutil create` is tried a few times, and what it said is kept for the + /// failure message. + fn dmg(dir: &Path, version: &str) -> PathBuf { + let src = dir.join(format!("src-{version}")); + bundle(&src, version); + let dmg = dir.join(format!("photocraft-{version}-macos-universal.dmg")); + let _one_at_a_time = super::macos::DISK_IMAGES.lock().unwrap_or_else(std::sync::PoisonError::into_inner); + let mut said = String::new(); + for attempt in 0..5u64 { + if attempt > 0 { + std::thread::sleep(std::time::Duration::from_millis(400 * attempt)); + let _ = std::fs::remove_file(&dmg); + } + let output = std::process::Command::new("hdiutil") + .args(["create", "-quiet", "-format", "UDZO", "-volname", "PhotoCraft", "-srcfolder"]) + .arg(&src) + .arg(&dmg) + .output() + .unwrap(); + if output.status.success() { + return dmg; + } + said = String::from_utf8_lossy(&output.stderr).trim().to_string(); + } + panic!("hdiutil create failed after 5 tries: {said}"); + } + + #[test] + fn custom_bundles_are_detected_and_updates_stay_in_their_actual_location() { + let dir = tempfile::tempdir().unwrap(); + let apps = dir.path().join("Craft Apps"); + std::fs::create_dir(&apps).unwrap(); + let paths = Paths::under(dir.path()); + let image = dmg(dir.path(), "0.6.0"); + let expect = Expect { team: None, version: None }; + let placed = install_dmg(photocraft(), &image, None, Some(&apps), false, &paths, &expect).unwrap(); + assert_eq!(placed, apps.join("PhotoCraft.app")); + let mut manifest = crate::manifest::Manifest::default(); + manifest.apps.insert("photocraft".into(), record("0.6.0", PackageKind::Dmg, placed.clone(), vec![], false)); + let existing = crate::detect::detect(photocraft(), &manifest, &crate::detect::Snapshot::default()).unwrap(); + assert_eq!(existing.source, InstallSource::MacApp); + assert_eq!(existing.location, placed.display().to_string()); + assert_eq!(existing.version.unwrap().to_string(), "0.6.0"); + let existing = crate::detect::detect(photocraft(), &manifest, &crate::detect::Snapshot::default()).unwrap(); + let new_default = dir.path().join("Other Apps"); + std::fs::create_dir(&new_default).unwrap(); + let update = dmg(dir.path(), "0.7.0"); + assert_eq!(install_dmg(photocraft(), &update, Some(&existing), Some(&new_default), false, &paths, &expect).unwrap(), placed); + assert!(!new_default.join("PhotoCraft.app").exists()); + let detected = crate::detect::detect(photocraft(), &manifest, &crate::detect::Snapshot::default()).unwrap(); + assert_eq!(detected.version.unwrap().to_string(), "0.7.0"); + std::fs::write(placed.join("Contents/Info.plist"), "invalid").unwrap(); + assert_ne!( + crate::detect::detect(photocraft(), &manifest, &crate::detect::Snapshot::default()).map(|i| i.source), + Some(InstallSource::Launcher) + ); + } + + #[test] + fn an_unrelated_bundle_at_the_selected_destination_is_preserved() { + let dir = tempfile::tempdir().unwrap(); + let placed = bundle(dir.path(), "0.5.0"); + let plist = placed.join("Contents/Info.plist"); + let unrelated = std::fs::read_to_string(&plist).unwrap().replace("ai.storyteller.photocraft", "com.example.other"); + std::fs::write(&plist, &unrelated).unwrap(); + let image = dmg(dir.path(), "0.6.0"); + let result = + install_dmg(photocraft(), &image, None, Some(dir.path()), false, &Paths::under(dir.path()), &Expect { team: None, version: None }); + assert!(result.unwrap_err().contains("already exists")); + assert_eq!(std::fs::read_to_string(plist).unwrap(), unrelated); + assert_eq!(std::fs::read(placed.join("Contents/MacOS/PhotoCraft")).unwrap(), b"0.5.0"); + } + + #[test] + fn only_the_teams_signature_passes() { + let dir = tempfile::tempdir().unwrap(); + let app = bundle(dir.path(), "0.6.0"); + assert!(!signed_by(&app, TEAM_ID), "an unsigned bundle passed"); + + // A valid signature, but not from the team. + let ok = std::process::Command::new("codesign").args(["--force", "--sign", "-"]).arg(&app).status().unwrap().success(); + assert!(ok, "codesign failed"); + assert!(!signed_by(&app, TEAM_ID), "an ad-hoc signature passed"); + + // A team id can't smuggle anything into the requirement. + assert!(!signed_by(&app, "X\" or anchor apple generic or \"Y")); + assert!(!signed_by(&app, "")); + } + + #[test] + fn the_bundle_must_be_the_releases_version() { + let dir = tempfile::tempdir().unwrap(); + let apps = dir.path().join("Applications"); + let current = bundle(&apps, "0.5.0"); + let existing = Installed { version: None, source: InstallSource::MacApp, location: current.display().to_string(), package: None }; + let paths = Paths::under(dir.path()); + let image = dmg(dir.path(), "0.6.0"); + + for (release, ok) in [("0.6.0", true), ("0.7.0", false), ("0.6.0-rc.1", true), ("0.5.0", false)] { + std::fs::write(current.join("Contents/MacOS/PhotoCraft"), "0.5.0").unwrap(); + let want = version::parse(release).unwrap(); + let expect = Expect { team: None, version: Some(&want) }; + let result = install_dmg(photocraft(), &image, Some(&existing), None, false, &paths, &expect); + assert_eq!(result.is_ok(), ok, "release {release}: {result:?}"); + let now = std::fs::read_to_string(current.join("Contents/MacOS/PhotoCraft")).unwrap(); + assert_eq!(now, if ok { "0.6.0" } else { "0.5.0" }, "release {release}"); + if !ok { + assert_eq!(result, Err(format!("the app inside isn't version {release}, so it wasn't installed"))); + } + assert_eq!( + std::fs::read_dir(&apps).unwrap().map(|e| e.unwrap().file_name()).collect::>(), + [std::ffi::OsString::from("PhotoCraft.app")] + ); + } + + // A bundle that names a pre-release must name the release's. + let rc = dmg(dir.path(), "0.8.0-rc.2"); + let want = version::parse("0.8.0-rc.1").unwrap(); + let expect = Expect { team: None, version: Some(&want) }; + assert!(install_dmg(photocraft(), &rc, Some(&existing), None, false, &paths, &expect).is_err()); + } + + #[test] + fn an_unsigned_update_leaves_the_installed_copy_alone() { + let dir = tempfile::tempdir().unwrap(); + let apps = dir.path().join("Applications"); + let current = bundle(&apps, "0.5.0"); + let existing = Installed { version: None, source: InstallSource::MacApp, location: current.display().to_string(), package: None }; + let image = dmg(dir.path(), "0.6.0"); + let release = Release { tag: "v0.6.0".into(), version: version::parse("0.6.0"), ..Release::default() }; + + let result = install_dmg(photocraft(), &image, Some(&existing), None, false, &Paths::under(dir.path()), &Expect::release(&release)); + assert_eq!(result, Err("the app isn't signed by its developer, so it wasn't installed".into())); + assert_eq!(std::fs::read_to_string(current.join("Contents/MacOS/PhotoCraft")).unwrap(), "0.5.0"); + assert_eq!( + std::fs::read_dir(&apps).unwrap().map(|e| e.unwrap().file_name()).collect::>(), + [std::ffi::OsString::from("PhotoCraft.app")] + ); + } + + /// A real release passes. Run with `CRAFT_APP=/path/to/PhotoCraft.app cargo test -p + /// artcraft-launcher-native -- --ignored a_real_craft_app`. + #[test] + #[ignore = "needs a Craft App from a real release"] + fn a_real_craft_app_carries_the_teams_signature() { + let app = std::env::var("CRAFT_APP").expect("set CRAFT_APP to a Craft App bundle"); + assert!(signed_by(Path::new(&app), TEAM_ID), "{app} isn't signed by {TEAM_ID}"); + } + } +} diff --git a/crates/update/src/lib.rs b/crates/update/src/lib.rs new file mode 100644 index 000000000..ab61612ea --- /dev/null +++ b/crates/update/src/lib.rs @@ -0,0 +1,72 @@ +//! Release checking, downloading, installing and self-updating for the Craft App Suite and ArtCraft Launcher. + +#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] + +pub mod app; +pub mod desktop_entry; +pub mod detect; +pub mod distro; +pub mod http; +pub mod install; +pub mod manifest; +pub mod packages; +pub mod paths; +pub mod platform; +#[cfg(windows)] +pub mod portable; +pub mod process; +pub mod protocol; +pub mod proxy; +pub mod queue; +pub mod release; +pub mod self_update; +pub mod source; +pub mod status; +pub mod suite; +pub mod time; +pub mod version; + +#[cfg(feature = "egui")] +pub mod dialog; + +pub use app::App; +pub use http::{Http, Stop}; +pub use paths::Paths; +pub use platform::{Arch, Os, PackageKind, Platform, pick_asset}; +pub use protocol::{InstallLocations, JobPhase, LatestRelease, Source}; +pub use release::{Asset, Release, parse_github_release, parse_github_release_list}; +pub use status::{InstallSource, Installed}; +pub use suite::SUITE; +pub use time::parse_rfc3339; +pub use version::{Version, is_newer, parse as parse_version}; + +#[cfg(feature = "egui")] +pub use dialog::{StartupPrompt, StartupPromptResponse}; + +/// Checks for an update against GitHub releases for the given app. +pub fn check_for_update(app: &App, http: &Http, current_version: &str) -> Result, String> { + let url = format!("https://api.github.com/repos/{}/releases?per_page=30", app.repo); + match http.get_api(&url, None)? { + crate::http::Fetched::Body { text, .. } => { + let latest = crate::release::parse_github_release_list(&text, app.repo, false)?; + if let Some(release) = latest + && let Some(rel_v) = &release.version + && let Some(cur_v) = crate::version::parse(current_version) + && crate::version::is_newer(rel_v, &cur_v) + { + return Ok(Some(release)); + } + Ok(None) + } + crate::http::Fetched::RateLimited { .. } => Err("GitHub's request limit was reached".into()), + crate::http::Fetched::NotModified | crate::http::Fetched::NotFound => Ok(None), + } +} + +/// Unix seconds now. +pub fn now() -> i64 { + std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map_or(0, |d| i64::try_from(d.as_secs()).unwrap_or(i64::MAX)) +} + +#[cfg(test)] +pub(crate) const TEST_AGENT: &str = "ArtCraft-Launcher-Test/0.0"; diff --git a/crates/update/src/manifest.rs b/crates/update/src/manifest.rs new file mode 100644 index 000000000..bcf9b06cc --- /dev/null +++ b/crates/update/src/manifest.rs @@ -0,0 +1,61 @@ +//! The launcher's record of what it installed itself. + +use std::collections::BTreeMap; +use std::path::{Path, PathBuf}; + +use crate::PackageKind; +use serde::{Deserialize, Serialize}; + +/// One app the launcher installed. +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub struct Record { + pub version: String, + pub kind: PackageKind, + /// The `.app`, the exe, the AppImage, or the folder of an unpacked tarball. + pub path: PathBuf, + /// Unix seconds. + pub installed_at: i64, + /// Every file installed, for tree installs (FreeBSD), so removal takes exactly those. + #[serde(default)] + pub files: Vec, + /// The files are in a system prefix and removing them needs root. + #[serde(default)] + pub needs_root: bool, +} + +#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] +pub struct Manifest { + pub apps: BTreeMap, +} + +impl Manifest { + pub fn load(path: &Path) -> Self { + std::fs::read(path).ok().and_then(|b| serde_json::from_slice(&b).ok()).unwrap_or_default() + } + + pub fn save(&self, path: &Path) -> Result<(), String> { + let bytes = serde_json::to_vec_pretty(self).map_err(|e| e.to_string())?; + crate::paths::write_atomic(path, &bytes) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn round_trips_and_survives_garbage() { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("installed.json"); + assert_eq!(Manifest::load(&path), Manifest::default()); + let mut m = Manifest::default(); + m.apps.insert( + "photocraft".into(), + Record { version: "0.5.0".into(), kind: PackageKind::AppImage, path: "/x/y".into(), installed_at: 1, files: vec![], needs_root: false }, + ); + m.save(&path).unwrap(); + assert_eq!(Manifest::load(&path), m); + std::fs::write(&path, b"{not json").unwrap(); + assert_eq!(Manifest::load(&path), Manifest::default()); + } +} diff --git a/crates/update/src/packages.rs b/crates/update/src/packages.rs new file mode 100644 index 000000000..1820c2f15 --- /dev/null +++ b/crates/update/src/packages.rs @@ -0,0 +1,161 @@ +//! Reading system package lists (dpkg, rpm, pacman, Flatpak) and matching them to Craft Apps. +//! +//! Pure functions over the tools' text output, so they're tested on every platform; only Linux +//! runs the tools (`detect::linux`). + +use crate::{App, InstallSource, version}; + +/// One installed package that belongs to a Craft App. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct Package { + pub manager: InstallSource, + /// The package's name (`photocraft-bin`) or Flatpak app id. + pub name: String, + /// As the package manager reports it (`0.5.0-1`); empty when it doesn't say. + pub version: String, +} + +/// `dpkg-query -W -f '${Package}|${db:Status-Status}|${Version}\n'`: installed packages only. +pub fn parse_dpkg(text: &str) -> Vec<(String, String)> { + text.lines() + .filter_map(|line| { + let mut f = line.split('|'); + match (f.next(), f.next(), f.next()) { + (Some(name), Some("installed"), Some(ver)) => { + let name = name.split_once(':').map(|(n, _)| n).unwrap_or(name); + Some((name.to_string(), ver.to_string())) + } + _ => None, + } + }) + .collect() +} + +/// `rpm -qa --qf '%{NAME}|%{VERSION}\n'`. +pub fn parse_rpm(text: &str) -> Vec<(String, String)> { + text.lines().filter_map(|line| line.split_once('|').map(|(n, v)| (n.to_string(), v.to_string()))).collect() +} + +/// `pacman -Q`: `name version` per line. +pub fn parse_pacman(text: &str) -> Vec<(String, String)> { + text.lines() + .filter_map(|line| { + let mut f = line.split_whitespace(); + Some((f.next()?.to_string(), f.next()?.to_string())) + }) + .collect() +} + +/// `flatpak list --app --columns=application,version`: the version column is often empty for +/// bundles built from a release tarball. +pub fn parse_flatpak_list(text: &str) -> Vec<(String, String)> { + text.lines() + .filter_map(|line| { + let mut f = line.split('\t'); + let id = f.next()?.trim(); + (!id.is_empty()).then(|| (id.to_string(), f.next().unwrap_or("").trim().to_string())) + }) + .collect() +} + +/// The `Version:` line of `flatpak info `. +pub fn parse_flatpak_info(text: &str) -> Option { + text.lines().find_map(|l| l.trim().strip_prefix("Version:").map(|v| v.trim().to_string())).filter(|v| !v.is_empty()) +} + +/// The package names a Craft App may be installed under with `manager`. On Arch the AUR carries +/// each app three ways: built from source (`photocraft`), the release binary (`photocraft-bin`) +/// and the latest commit (`photocraft-git`). Debian and RPM packages may use either the short stem +/// (`photocraft`) or the reverse-DNS bundle id (`ai.storyteller.photocraft`). +pub fn candidate_names(app: &App, manager: InstallSource) -> Vec { + match manager { + InstallSource::Flatpak => vec![app.bundle_id()], + InstallSource::Pacman => { + app.stems().flat_map(|s| [s.to_string(), format!("{s}-bin"), format!("{s}-git")]).chain(std::iter::once(app.bundle_id())).collect() + } + InstallSource::Deb | InstallSource::Rpm => app.stems().map(str::to_string).chain(std::iter::once(app.bundle_id())).collect(), + _ => app.stems().map(str::to_string).collect(), + } +} + +/// The package of `app` among `installed`, preferring the manager listed first in `order`. +pub fn find<'a>(app: &App, installed: &'a [Package], order: &[InstallSource]) -> Option<&'a Package> { + order.iter().find_map(|manager| { + let names = candidate_names(app, *manager); + names.iter().find_map(|n| installed.iter().find(|p| p.manager == *manager && p.name == *n)) + }) +} + +/// A package's version, read the package-manager way (`0.5.0-1` is 0.5.0). +pub fn version_of(p: &Package) -> Option { + version::parse_package(&p.version) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn pkgs(manager: InstallSource, list: Vec<(String, String)>) -> Vec { + list.into_iter().map(|(name, version)| Package { manager, name, version }).collect() + } + + #[test] + fn reads_each_package_managers_listing() { + assert_eq!( + parse_dpkg("photocraft|installed|0.5.0-1\nphotocraft:amd64|installed|0.6.0-1\nwordcraft|config-files|0.3.0-1\n"), + [("photocraft".into(), "0.5.0-1".into()), ("photocraft".into(), "0.6.0-1".into())] + ); + assert_eq!(parse_rpm("gridcraft|0.3.0\n"), [("gridcraft".into(), "0.3.0".into())]); + assert_eq!(parse_pacman("photocraft-bin 0.5.0-1\nlinux 6.11.1.arch1-1\n").len(), 2); + assert_eq!( + parse_flatpak_list("ai.storyteller.wordcraft\t\nai.storyteller.cadcraft\t0.3.0\n"), + [("ai.storyteller.wordcraft".into(), String::new()), ("ai.storyteller.cadcraft".into(), "0.3.0".into())] + ); + let info = "\nWordCraft - Word processor\n\n ID: ai.storyteller.wordcraft\n Version: 0.3.0\n Branch: master\n"; + assert_eq!(parse_flatpak_info(info).as_deref(), Some("0.3.0")); + assert_eq!(parse_flatpak_info("Version: \n"), None); + } + + #[test] + fn candidate_names_cover_bundle_ids_for_deb_rpm_and_pacman() { + let photo = App::by_id("photocraft").unwrap(); + let deb_names = candidate_names(photo, InstallSource::Deb); + assert!(deb_names.contains(&"photocraft".to_string())); + assert!(deb_names.contains(&"ai.storyteller.photocraft".to_string())); + + let rpm_names = candidate_names(photo, InstallSource::Rpm); + assert!(rpm_names.contains(&"photocraft".to_string())); + assert!(rpm_names.contains(&"ai.storyteller.photocraft".to_string())); + + let pacman_names = candidate_names(photo, InstallSource::Pacman); + assert!(pacman_names.contains(&"photocraft".to_string())); + assert!(pacman_names.contains(&"photocraft-bin".to_string())); + assert!(pacman_names.contains(&"ai.storyteller.photocraft".to_string())); + } + + #[test] + fn finds_aur_packages_in_every_flavour() { + let photo = App::by_id("photocraft").unwrap(); + let pdf = App::by_id("pdfcraft").unwrap(); + let installed = + pkgs(InstallSource::Pacman, parse_pacman("linux 6.11.1.arch1-1\nphotocraft-git 0.3.0.r47.g47f9306-1\nprintcraft-bin 0.2.1-1\n")); + let hit = find(photo, &installed, &[InstallSource::Pacman]).unwrap(); + assert_eq!(hit.name, "photocraft-git"); + assert_eq!(version_of(hit).unwrap().to_string(), "0.3.0"); + // PdfCraft under its old name. + assert_eq!(find(pdf, &installed, &[InstallSource::Pacman]).unwrap().name, "printcraft-bin"); + assert!(find(App::by_id("wordcraft").unwrap(), &installed, &[InstallSource::Pacman]).is_none()); + } + + #[test] + fn prefers_managers_in_order_and_reads_revisions() { + let photo = App::by_id("photocraft").unwrap(); + let mut installed = pkgs(InstallSource::Deb, parse_dpkg("photocraft|installed|0.5.0-1\n")); + installed.extend(pkgs(InstallSource::Flatpak, parse_flatpak_list("ai.storyteller.photocraft\t0.4.0\n"))); + let hit = find(photo, &installed, &[InstallSource::Deb, InstallSource::Flatpak]).unwrap(); + assert_eq!(hit.manager, InstallSource::Deb); + // 0.5.0-1 is the 0.5.0 release, not a pre-release of it. + assert_eq!(version_of(hit).unwrap().to_string(), "0.5.0"); + assert_eq!(find(photo, &installed, &[InstallSource::Flatpak]).unwrap().version, "0.4.0"); + } +} diff --git a/crates/update/src/paths.rs b/crates/update/src/paths.rs new file mode 100644 index 000000000..b43feb15e --- /dev/null +++ b/crates/update/src/paths.rs @@ -0,0 +1,131 @@ +//! Where an installer keeps its own files: downloads, records, logs. + +use std::io::Write; +use std::path::{Path, PathBuf}; + +use crate::app::App; + +/// An installer's folders. +#[derive(Clone, Debug)] +pub struct Paths { + /// Records of what was installed, and tarball installs. + pub data: PathBuf, + /// Saved release answers and in-progress downloads. + pub cache: PathBuf, +} + +impl Paths { + /// Discovers paths for an app, respecting portable mode if `portable.txt` is next to current exe. + pub fn for_app(app: &App) -> Result { + if let Ok(exe) = std::env::current_exe() { + return Self::for_exe(app, &exe); + } + let dir = if cfg!(any(target_os = "macos", target_os = "windows")) { app.name } else { app.id }; + Self::per_user(dir) + } + + /// Discovers paths for an app from a specific executable path. + pub fn for_exe(app: &App, exe: &Path) -> Result { + if let Some(parent) = exe.parent() + && parent.join("portable.txt").is_file() + { + let data_dir_name = format!("{}Data", app.name.replace(' ', "")); + return Ok(Self::under(&parent.join(data_dir_name))); + } + let dir = if cfg!(any(target_os = "macos", target_os = "windows")) { app.name } else { app.id }; + Self::per_user(dir) + } + + /// The per-user folders named `dir` (`ArtCraft Launcher` on macOS and Windows, + /// `artcraft-launcher` elsewhere, by convention). + pub fn per_user(dir: &str) -> Result { + let data = dirs::data_local_dir().ok_or("no per-user data folder on this system")?.join(dir); + let cache = dirs::cache_dir().ok_or("no per-user cache folder on this system")?.join(dir); + Ok(Self { data, cache }) + } + + /// Everything under one folder (portable mode, tests). + pub fn under(root: &std::path::Path) -> Self { + Self { data: root.join("data"), cache: root.join("cache") } + } + + pub fn downloads(&self) -> PathBuf { + self.cache.join("downloads") + } + + /// Log files, and the error of a failed self-update waiting for the next start. + pub fn logs(&self) -> PathBuf { + self.data.join("logs") + } + + /// Where tarball installs (FreeBSD, Linux without AppImage) are unpacked: `/apps/`. + pub fn apps(&self) -> PathBuf { + self.data.join("apps") + } + + /// The folder an app unpacked in [`Self::apps`] sees as its home: `/apps//home`. + /// A Craft App keeps its settings in the XDG folders (`~/.config`, `~/.local/share`, + /// `~/.cache`), so pointing `HOME` and those variables here keeps every app's configuration + /// beside it, inside the folder the installer owns. + #[cfg(target_os = "linux")] + pub fn app_home(&self, id: &str) -> PathBuf { + self.apps().join(id).join("home") + } +} + +/// Writes `bytes` to `path` via a temporary file and a rename, so a crash never leaves half a file. +pub fn write_atomic(path: &std::path::Path, bytes: &[u8]) -> Result<(), String> { + let dir = path.parent().ok_or("no parent folder")?; + std::fs::create_dir_all(dir).map_err(|e| format!("couldn't create {} ({e})", dir.display()))?; + let mut tmp = tempfile::NamedTempFile::new_in(dir).map_err(|e| format!("couldn't create a temporary file in {} ({e})", dir.display()))?; + tmp.write_all(bytes).map_err(|e| format!("couldn't write {} ({e})", tmp.path().display()))?; + tmp.as_file().sync_all().map_err(|e| format!("couldn't sync {} ({e})", tmp.path().display()))?; + tmp.persist(path).map_err(|e| format!("couldn't replace {} ({e})", path.display()))?; + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn atomic_write_does_not_overwrite_an_unrelated_temporary_file() { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("installed.json"); + let unrelated = path.with_extension("tmp"); + std::fs::write(&unrelated, b"another writer's data").unwrap(); + write_atomic(&path, b"new manifest").unwrap(); + assert_eq!(std::fs::read(&unrelated).unwrap(), b"another writer's data"); + assert_eq!(std::fs::read(&path).unwrap(), b"new manifest"); + } + + #[test] + fn atomic_write_replaces_a_complete_file_and_cleans_up_on_failure() { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("installed.json"); + std::fs::write(&path, b"old manifest").unwrap(); + write_atomic(&path, b"new manifest").unwrap(); + assert_eq!(std::fs::read(&path).unwrap(), b"new manifest"); + assert_eq!(std::fs::read_dir(dir.path()).unwrap().count(), 1); + + let blocked = dir.path().join("directory.json"); + std::fs::create_dir(&blocked).unwrap(); + std::fs::write(blocked.join("keep.txt"), b"keep").unwrap(); + assert!(write_atomic(&blocked, b"replacement").is_err()); + assert_eq!(std::fs::read(blocked.join("keep.txt")).unwrap(), b"keep"); + assert_eq!(std::fs::read_dir(dir.path()).unwrap().count(), 2); + } + + #[cfg(unix)] + #[test] + fn atomic_write_does_not_follow_a_temporary_file_symlink() { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("installed.json"); + let victim = dir.path().join("unrelated.txt"); + std::fs::write(&victim, b"keep this file").unwrap(); + std::os::unix::fs::symlink(&victim, path.with_extension("tmp")).unwrap(); + write_atomic(&path, b"new manifest").unwrap(); + assert_eq!(std::fs::read(&victim).unwrap(), b"keep this file"); + assert_eq!(std::fs::read(&path).unwrap(), b"new manifest"); + } +} diff --git a/crates/update/src/platform.rs b/crates/update/src/platform.rs new file mode 100644 index 000000000..a16a852d5 --- /dev/null +++ b/crates/update/src/platform.rs @@ -0,0 +1,423 @@ +//! Platforms, package formats, and choosing the right release asset. +//! +//! Every Craft App release names its files `---[-portable].`, for +//! example `photocraft-0.5.0-windows-x64.msi` or `photocraft-0.5.0-linux-aarch64.AppImage`. +//! +//! Labels are English. The UI translates [`PackageKind::label`]; OS and architecture names are +//! shown as they are. + +use serde::{Deserialize, Serialize}; + +use crate::release::{Asset, Release}; + +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] +pub enum Os { + Windows, + MacOs, + Linux, + FreeBsd, +} + +impl Os { + pub fn label(self) -> &'static str { + match self { + Self::Windows => "Windows", + Self::MacOs => "macOS", + Self::Linux => "Linux", + Self::FreeBsd => "FreeBSD", + } + } +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] +pub enum Arch { + X64, + X86, + Arm64, + Riscv64, +} + +impl Arch { + pub fn label(self) -> &'static str { + match self { + Self::X64 => "x64", + Self::X86 => "x86", + Self::Arm64 => "ARM64", + Self::Riscv64 => "RISC-V", + } + } +} + +/// A package format a release ships. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] +pub enum PackageKind { + /// Windows Installer package (installs for all users into Program Files). + Msi, + /// Windows zip with the exe at the top level. + PortableZip, + /// macOS disk image with the `.app` bundle. + Dmg, + AppImage, + Deb, + Rpm, + /// Arch Linux package (`.pkg.tar.zst`). + ArchPkg, + Flatpak, + /// Linux or FreeBSD tarball (`bin/`, `share/…`). + TarGz, +} + +impl PackageKind { + pub fn label(self) -> &'static str { + match self { + Self::Msi => "Installer (.msi)", + Self::PortableZip => "Portable (.zip)", + Self::Dmg => "Disk image (.dmg)", + Self::AppImage => "AppImage", + Self::Deb => "Debian package (.deb)", + Self::Rpm => "RPM package (.rpm)", + Self::ArchPkg => "Arch package (.pkg.tar.zst)", + Self::Flatpak => "Flatpak bundle", + Self::TarGz => "Tarball (.tar.gz)", + } + } + + fn suffix(self) -> &'static str { + match self { + Self::Msi => ".msi", + Self::PortableZip => "-portable.zip", + Self::Dmg => ".dmg", + Self::AppImage => ".AppImage", + Self::Deb => ".deb", + Self::Rpm => ".rpm", + Self::ArchPkg => ".pkg.tar.zst", + Self::Flatpak => ".flatpak", + Self::TarGz => ".tar.gz", + } + } +} + +/// Where the launcher is running (or, on the web, the visitor's machine). +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] +pub struct Platform { + pub os: Os, + pub arch: Arch, +} + +impl Platform { + /// The platform this binary was built for, or `None` for wasm and unsupported targets. + pub fn current() -> Option { + let os = if cfg!(target_os = "windows") { + Os::Windows + } else if cfg!(target_os = "macos") { + Os::MacOs + } else if cfg!(target_os = "linux") { + Os::Linux + } else if cfg!(target_os = "freebsd") { + Os::FreeBsd + } else { + return None; + }; + let arch = if cfg!(target_arch = "x86_64") { + Arch::X64 + } else if cfg!(target_arch = "x86") { + Arch::X86 + } else if cfg!(target_arch = "aarch64") { + Arch::Arm64 + } else if cfg!(target_arch = "riscv64") { + Arch::Riscv64 + } else { + return None; + }; + Some(Self { os, arch }) + } + + pub fn label(self) -> String { + match self.os { + Os::MacOs => "macOS".into(), + os => format!("{} {}", os.label(), self.arch.label()), + } + } + + /// Asset platform tokens to try, best first. Windows on ARM and 64-bit Windows can fall back + /// to emulated builds; macOS prefers universal, then native builds. Apple silicon can fall + /// back to Intel builds, but Intel Macs cannot run ARM builds. + fn tokens(self) -> &'static [&'static str] { + match (self.os, self.arch) { + (Os::Windows, Arch::X64) => &["windows-x64", "windows-x86"], + (Os::Windows, Arch::X86) => &["windows-x86"], + (Os::Windows, Arch::Arm64) => &["windows-arm64", "windows-x64", "windows-x86"], + (Os::Windows, _) => &[], + (Os::MacOs, Arch::X64) => &["macos-universal", "macos-x86_64"], + (Os::MacOs, Arch::Arm64) => &["macos-universal", "macos-aarch64", "macos-arm64", "macos-x86_64"], + (Os::MacOs, _) => &[], + (Os::Linux, Arch::X64) => &["linux-x86_64"], + (Os::Linux, Arch::Arm64) => &["linux-aarch64"], + (Os::Linux, Arch::Riscv64) => &["linux-riscv64"], + (Os::Linux, Arch::X86) => &[], + (Os::FreeBsd, Arch::X64) => &["freebsd-x86_64"], + (Os::FreeBsd, Arch::Arm64) => &["freebsd-aarch64"], + (Os::FreeBsd, _) => &[], + } + } + + /// Package formats the launcher installs on this OS, in order of preference. + pub fn default_kinds(self) -> &'static [PackageKind] { + match self.os { + Os::Windows => &[PackageKind::Msi, PackageKind::PortableZip], + Os::MacOs => &[PackageKind::Dmg], + Os::Linux => &[PackageKind::AppImage, PackageKind::TarGz], + Os::FreeBsd => &[PackageKind::TarGz], + } + } + + /// Every format offered as a manual download on this OS (the web build's download menu). + pub fn download_kinds(self) -> &'static [PackageKind] { + match self.os { + Os::Windows => &[PackageKind::Msi, PackageKind::PortableZip], + Os::MacOs => &[PackageKind::Dmg], + Os::Linux => &[PackageKind::AppImage, PackageKind::Deb, PackageKind::Rpm, PackageKind::ArchPkg, PackageKind::Flatpak, PackageKind::TarGz], + Os::FreeBsd => &[PackageKind::TarGz], + } + } +} + +/// What an asset name says about itself. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct AssetName { + pub stem: String, + pub version: String, + pub token: String, + pub kind: PackageKind, +} + +/// Reads `---[-portable].`; `None` for checksums, zsync files, CLI +/// and web bundles, or anything else. +pub fn parse_asset_name(name: &str, stems: &[&str]) -> Option { + const KINDS: [PackageKind; 9] = [ + PackageKind::PortableZip, + PackageKind::TarGz, + PackageKind::Msi, + PackageKind::Dmg, + PackageKind::AppImage, + PackageKind::Deb, + PackageKind::Rpm, + PackageKind::ArchPkg, + PackageKind::Flatpak, + ]; + let (kind, base) = KINDS.iter().find_map(|k| name.strip_suffix(k.suffix()).map(|b| (*k, b)))?; + for stem in stems { + let Some(rest) = base.strip_prefix(stem).and_then(|r| r.strip_prefix('-')) else { continue }; + // rest = --; os and arch never contain '-', versions may. + let mut parts = rest.rsplitn(3, '-'); + let (arch, os, version) = (parts.next()?, parts.next()?, parts.next()?); + if crate::version::parse(version).is_none() || !version.starts_with(|c: char| c.is_ascii_digit()) { + continue; + } + return Some(AssetName { stem: (*stem).to_string(), version: version.to_string(), token: format!("{os}-{arch}"), kind }); + } + None +} + +/// The best asset of `release` for `platform`, trying `kinds` in order and, within a kind, the +/// platform's tokens in order (native before emulated). +pub fn pick_asset<'a>(release: &'a Release, stems: &[&str], platform: Platform, kinds: &[PackageKind]) -> Option<(&'a Asset, PackageKind)> { + let parsed: Vec<(&Asset, AssetName)> = release.assets.iter().filter_map(|a| parse_asset_name(&a.name, stems).map(|n| (a, n))).collect(); + for kind in kinds { + for token in platform.tokens() { + if let Some((asset, _)) = parsed.iter().find(|(_, n)| n.kind == *kind && n.token == *token) { + return Some((asset, *kind)); + } + } + } + None +} + +#[cfg(test)] +mod tests { + use super::*; + + fn release(names: &[&str]) -> Release { + Release { + assets: names.iter().map(|n| Asset { name: (*n).into(), size: 1, url: format!("https://x/{n}"), sha256: None }).collect(), + ..Release::default() + } + } + + const PHOTO: &[&str] = &[ + "photocraft-0.5.0-freebsd-x86_64.tar.gz", + "photocraft-0.5.0-linux-aarch64.AppImage", + "photocraft-0.5.0-linux-aarch64.AppImage.zsync", + "photocraft-0.5.0-linux-aarch64.deb", + "photocraft-0.5.0-linux-riscv64.tar.gz", + "photocraft-0.5.0-linux-x86_64.AppImage", + "photocraft-0.5.0-linux-x86_64.tar.gz", + "photocraft-0.5.0-macos-universal.dmg", + "photocraft-0.5.0-windows-x64-portable.zip", + "photocraft-0.5.0-windows-x64.msi", + "photocraft-0.5.0-windows-x86.msi", + "photocraft-cli-0.5.0-macos-universal.zip", + "photocraft-web-0.5.0.zip", + "SHA256SUMS.txt", + ]; + + #[test] + fn macos_prefers_the_published_universal_build() { + // PhotoCraft v0.9.0's GitHub metadata, trimmed to its desktop DMG. + let mut r = crate::release::parse_github_release(include_str!("../tests/fixtures/photocraft-release.json"), "storytold/photocraft").unwrap(); + // Split builds must not outrank the published universal DMG, even when listed first. + r.assets.splice(0..0, release(&["photocraft-0.9.0-macos-arm64.dmg", "photocraft-0.9.0-macos-x86_64.dmg"]).assets); + for arch in [Arch::X64, Arch::Arm64] { + let (asset, kind) = pick_asset(&r, &["photocraft"], Platform { os: Os::MacOs, arch }, &[PackageKind::Dmg]).unwrap(); + assert_eq!(asset.name, "photocraft-0.9.0-macos-universal.dmg"); + assert_eq!(kind, PackageKind::Dmg); + } + } + + #[test] + fn freebsd_picks_the_tarball_for_the_processor() { + // SoundCraft v0.10.0's GitHub metadata, trimmed to its FreeBSD tarballs. + let r = crate::release::parse_github_release(include_str!("../tests/fixtures/soundcraft-release.json"), "storytold/soundcraft").unwrap(); + let pick = |arch| { + let platform = Platform { os: Os::FreeBsd, arch }; + pick_asset(&r, &["soundcraft"], platform, platform.default_kinds()).map(|(a, k)| (a.name.as_str(), k)) + }; + assert_eq!(pick(Arch::X64), Some(("soundcraft-0.10.0-freebsd-x86_64.tar.gz", PackageKind::TarGz))); + assert_eq!(pick(Arch::Arm64), Some(("soundcraft-0.10.0-freebsd-aarch64.tar.gz", PackageKind::TarGz))); + for arch in [Arch::X86, Arch::Riscv64] { + assert_eq!(pick(arch), None); + } + // An x86_64 machine never takes the ARM tarball, nor an ARM machine the x86_64 one. + let only_arm = release(&["soundcraft-0.10.0-freebsd-aarch64.tar.gz"]); + assert_eq!(pick_asset(&only_arm, &["soundcraft"], Platform { os: Os::FreeBsd, arch: Arch::X64 }, &[PackageKind::TarGz]), None); + let only_x64 = release(&["soundcraft-0.10.0-freebsd-x86_64.tar.gz"]); + assert_eq!(pick_asset(&only_x64, &["soundcraft"], Platform { os: Os::FreeBsd, arch: Arch::Arm64 }, &[PackageKind::TarGz]), None); + } + + #[test] + fn macos_split_builds_match_the_processor() { + let r = release(&["photocraft-0.5.0-macos-arm64.dmg", "photocraft-0.5.0-macos-x86_64.dmg"]); + let pick = |arch| pick_asset(&r, &["photocraft"], Platform { os: Os::MacOs, arch }, &[PackageKind::Dmg]).map(|(a, _)| a.name.as_str()); + assert_eq!(pick(Arch::X64), Some("photocraft-0.5.0-macos-x86_64.dmg")); + assert_eq!(pick(Arch::Arm64), Some("photocraft-0.5.0-macos-arm64.dmg")); + } + + #[test] + fn intel_macos_never_selects_an_arm_only_build() { + for token in ["macos-aarch64", "macos-arm64"] { + let name = format!("photocraft-0.5.0-{token}.dmg"); + let r = release(&[&name]); + assert_eq!(pick_asset(&r, &["photocraft"], Platform { os: Os::MacOs, arch: Arch::X64 }, &[PackageKind::Dmg]), None); + assert_eq!( + pick_asset(&r, &["photocraft"], Platform { os: Os::MacOs, arch: Arch::Arm64 }, &[PackageKind::Dmg]).map(|(a, _)| a.name.as_str()), + Some(name.as_str()) + ); + } + } + + #[test] + fn apple_silicon_keeps_the_intel_fallback() { + let r = release(&["photocraft-0.5.0-macos-x86_64.dmg"]); + assert_eq!( + pick_asset(&r, &["photocraft"], Platform { os: Os::MacOs, arch: Arch::Arm64 }, &[PackageKind::Dmg]).map(|(a, _)| a.name.as_str()), + Some("photocraft-0.5.0-macos-x86_64.dmg") + ); + for arch in [Arch::X86, Arch::Riscv64] { + assert_eq!(pick_asset(&r, &["photocraft"], Platform { os: Os::MacOs, arch }, &[PackageKind::Dmg]), None); + } + } + + #[test] + fn reads_asset_names() { + let n = parse_asset_name("photocraft-0.2.0-rc.1-windows-arm64-portable.zip", &["photocraft"]).unwrap(); + assert_eq!((n.version.as_str(), n.token.as_str(), n.kind), ("0.2.0-rc.1", "windows-arm64", PackageKind::PortableZip)); + let n = parse_asset_name("printcraft-0.2.1-linux-x86_64.tar.gz", &["pdfcraft", "printcraft"]).unwrap(); + assert_eq!((n.stem.as_str(), n.kind), ("printcraft", PackageKind::TarGz)); + for other in + ["photocraft-cli-0.5.0-macos-universal.zip", "photocraft-web-0.5.0.zip", "SHA256SUMS.txt", "photocraft-0.5.0-linux-x86_64.AppImage.zsync"] + { + assert_eq!(parse_asset_name(other, &["photocraft"]), None, "{other}"); + } + // FigCraft also ships its collaboration server; that tarball is not the app. + assert_eq!(parse_asset_name("figcraft-server-0.5.1-linux-x86_64.tar.gz", &["figcraft"]), None); + assert!(parse_asset_name("figcraft-0.5.1-linux-x86_64.tar.gz", &["figcraft"]).is_some()); + } + + #[test] + fn picks_the_native_build_first_and_falls_back_to_emulation() { + let r = release(PHOTO); + let pick = |os, arch, kinds: &[PackageKind]| pick_asset(&r, &["photocraft"], Platform { os, arch }, kinds).map(|(a, _)| a.name.clone()); + assert_eq!(pick(Os::MacOs, Arch::Arm64, &[PackageKind::Dmg]).as_deref(), Some("photocraft-0.5.0-macos-universal.dmg")); + assert_eq!(pick(Os::Windows, Arch::X64, &[PackageKind::Msi]).as_deref(), Some("photocraft-0.5.0-windows-x64.msi")); + // No arm64 MSI in this release: Windows on ARM runs the x64 build. + assert_eq!(pick(Os::Windows, Arch::Arm64, &[PackageKind::Msi]).as_deref(), Some("photocraft-0.5.0-windows-x64.msi")); + assert_eq!(pick(Os::Linux, Arch::Arm64, &[PackageKind::AppImage]).as_deref(), Some("photocraft-0.5.0-linux-aarch64.AppImage")); + assert_eq!(pick(Os::Linux, Arch::Arm64, &[PackageKind::TarGz]), None); + assert_eq!(pick(Os::Linux, Arch::Riscv64, &[PackageKind::TarGz]).as_deref(), Some("photocraft-0.5.0-linux-riscv64.tar.gz")); + assert_eq!(pick(Os::FreeBsd, Arch::X64, &[PackageKind::TarGz]).as_deref(), Some("photocraft-0.5.0-freebsd-x86_64.tar.gz")); + // Preference order across kinds. + assert_eq!( + pick(Os::Windows, Arch::X64, &[PackageKind::PortableZip, PackageKind::Msi]).as_deref(), + Some("photocraft-0.5.0-windows-x64-portable.zip") + ); + assert_eq!(pick(Os::Windows, Arch::X86, &[PackageKind::PortableZip, PackageKind::Msi]).as_deref(), Some("photocraft-0.5.0-windows-x86.msi")); + + let p_riscv = Platform { os: Os::Linux, arch: Arch::Riscv64 }; + assert_eq!( + pick_asset(&r, &["photocraft"], p_riscv, p_riscv.default_kinds()).map(|(a, _)| a.name.clone()).as_deref(), + Some("photocraft-0.5.0-linux-riscv64.tar.gz") + ); + assert_eq!( + pick_asset(&r, &["photocraft"], p_riscv, p_riscv.download_kinds()).map(|(a, _)| a.name.clone()).as_deref(), + Some("photocraft-0.5.0-linux-riscv64.tar.gz") + ); + let r_riscv_deb = + release(&["photocraft-0.5.0-linux-riscv64.tar.gz", "photocraft-0.5.0-linux-riscv64.deb", "photocraft-0.5.0-linux-riscv64.rpm"]); + assert_eq!( + pick_asset(&r_riscv_deb, &["photocraft"], p_riscv, p_riscv.download_kinds()).map(|(a, _)| a.name.clone()).as_deref(), + Some("photocraft-0.5.0-linux-riscv64.deb") + ); + } + + #[test] + fn strictly_matches_architecture_for_all_linux_package_formats() { + let multi_arch_release = release(&[ + "photocraft-0.5.0-linux-aarch64.AppImage", + "photocraft-0.5.0-linux-x86_64.AppImage", + "photocraft-0.5.0-linux-aarch64.deb", + "photocraft-0.5.0-linux-x86_64.deb", + "photocraft-0.5.0-linux-aarch64.rpm", + "photocraft-0.5.0-linux-x86_64.rpm", + "photocraft-0.5.0-linux-aarch64.pkg.tar.zst", + "photocraft-0.5.0-linux-x86_64.pkg.tar.zst", + "photocraft-0.5.0-linux-aarch64.flatpak", + "photocraft-0.5.0-linux-x86_64.flatpak", + "photocraft-0.5.0-linux-aarch64.tar.gz", + "photocraft-0.5.0-linux-x86_64.tar.gz", + ]); + + let p_x64 = Platform { os: Os::Linux, arch: Arch::X64 }; + let p_arm64 = Platform { os: Os::Linux, arch: Arch::Arm64 }; + + for kind in [PackageKind::AppImage, PackageKind::Deb, PackageKind::Rpm, PackageKind::ArchPkg, PackageKind::Flatpak, PackageKind::TarGz] { + let pick_x64 = pick_asset(&multi_arch_release, &["photocraft"], p_x64, &[kind]).map(|(a, _)| a.name.clone()); + assert!(pick_x64.as_deref().unwrap().contains("linux-x86_64"), "Expected x86_64 asset for {kind:?}, got {pick_x64:?}"); + + let pick_arm64 = pick_asset(&multi_arch_release, &["photocraft"], p_arm64, &[kind]).map(|(a, _)| a.name.clone()); + assert!(pick_arm64.as_deref().unwrap().contains("linux-aarch64"), "Expected aarch64 asset for {kind:?}, got {pick_arm64:?}"); + } + + // On x86_64 Linux, an aarch64-only asset must NEVER match or fall back + let arm_only_release = release(&[ + "photocraft-0.5.0-linux-aarch64.AppImage", + "photocraft-0.5.0-linux-aarch64.deb", + "photocraft-0.5.0-linux-aarch64.rpm", + "photocraft-0.5.0-linux-aarch64.pkg.tar.zst", + "photocraft-0.5.0-linux-aarch64.flatpak", + "photocraft-0.5.0-linux-aarch64.tar.gz", + ]); + for kind in [PackageKind::AppImage, PackageKind::Deb, PackageKind::Rpm, PackageKind::ArchPkg, PackageKind::Flatpak, PackageKind::TarGz] { + assert_eq!(pick_asset(&arm_only_release, &["photocraft"], p_x64, &[kind]), None, "x86_64 must never match an aarch64 asset for {kind:?}"); + } + } +} diff --git a/crates/update/src/portable.rs b/crates/update/src/portable.rs new file mode 100644 index 000000000..257a2290b --- /dev/null +++ b/crates/update/src/portable.rs @@ -0,0 +1,857 @@ +//! Portable Windows packages: bounded extraction and replacement of recorded files only. + +use std::ffi::OsStr; +use std::path::{Component, Path, PathBuf}; + +use crate::manifest::Record; +use crate::paths::Paths; +use crate::{App, PackageKind}; + +const MAX_FILES: usize = 10_000; +const MAX_BYTES: u64 = 4 * 1024 * 1024 * 1024; + +/// Custom destinations may have OS-managed links in their host ancestors. +pub fn validate_directory(path: &Path) -> Result<(), String> { + if !path.is_absolute() || path.components().any(|c| matches!(c, Component::ParentDir | Component::CurDir)) { + return Err("Choose an absolute installation folder without parent-directory components.".into()); + } + // Resolve the closest existing ancestor: /home and /var can be OS-managed + // symlinks, and a custom destination need not exist yet. + for ancestor in path.ancestors() { + match std::fs::canonicalize(ancestor) { + Ok(resolved) => { + if !resolved.is_dir() { + return Err("The installation folder contains a non-directory component.".into()); + } + return Ok(()); + } + Err(error) if error.kind() == std::io::ErrorKind::NotFound => { + // A dangling link is not a missing directory we can safely create. + if std::fs::symlink_metadata(ancestor).is_ok() { + return Err("The installation folder contains a dangling link.".into()); + } + } + Err(error) => return Err(error.to_string()), + } + } + Err("The installation folder has no existing ancestor.".into()) +} + +/// Links are allowed above the ownership boundary, never inside it (including root). +fn validate_owned_directory(root: &Path, path: &Path) -> Result<(), String> { + validate_directory(path)?; + if !path.starts_with(root) { + return Err("An installation path points outside its folder.".into()); + } + for ancestor in path.ancestors().take_while(|ancestor| ancestor.starts_with(root)) { + match std::fs::symlink_metadata(ancestor) { + Ok(meta) if linked(&meta) || !meta.is_dir() => { + return Err("The installation folder contains a link or a non-directory component.".into()); + } + Err(error) if error.kind() != std::io::ErrorKind::NotFound => return Err(error.to_string()), + _ => {} + } + } + Ok(()) +} + +fn resolve_missing(path: &Path) -> Result { + for ancestor in path.ancestors() { + match std::fs::canonicalize(ancestor) { + Ok(resolved) => { + let suffix = path.strip_prefix(ancestor).map_err(|e| e.to_string())?; + return Ok(resolved.join(suffix)); + } + Err(error) if error.kind() == std::io::ErrorKind::NotFound => { + if std::fs::symlink_metadata(ancestor).is_ok() { + return Err("The installation folder contains a dangling link.".into()); + } + } + Err(error) => return Err(error.to_string()), + } + } + Err("The installation folder has no existing ancestor.".into()) +} + +/// Whether two paths name the same Windows folder or file: case, trailing separators and the +/// verbatim `\\?\` form that `canonicalize` returns don't count. +fn same_windows_path(left: &Path, right: &Path) -> bool { + /// `canonicalize` writes `\\?\C:\…` and, for a network share, `\\?\UNC\server\share\…`; + /// Windows reads either prefix in any case. + fn normalized(path: &Path) -> String { + let text = path.to_string_lossy().to_lowercase(); + let text = match text.strip_prefix(r"\\?\unc\") { + Some(share) => format!(r"\\{share}"), + None => text.strip_prefix(r"\\?\").unwrap_or(&text).to_string(), + }; + text.trim_end_matches(['\\', '/']).to_string() + } + normalized(left) == normalized(right) +} + +fn linked(meta: &std::fs::Metadata) -> bool { + #[cfg(windows)] + { + use std::os::windows::fs::MetadataExt; + meta.file_attributes() & 0x400 != 0 + } + #[cfg(not(windows))] + { + meta.file_type().is_symlink() + } +} + +struct Staging(PathBuf); +impl Staging { + /// Stop cleaning up and hand the directory back: used when a rollback failed and the backup + /// is the only copy left for manual recovery. + fn keep(self) -> PathBuf { + let path = self.0.clone(); + // Deliberately skipping Drop here is the whole point — it would delete the recovery + // files. The directory is handed to the operator, not leaked into nothing. + std::mem::forget(self); // NOSONAR + path + } +} +impl Drop for Staging { + fn drop(&mut self) { + let _ = std::fs::remove_dir_all(&self.0); + } +} + +fn extract(file: &Path, destination: &Path) -> Result<(Vec, Vec), String> { + let file = std::fs::File::open(file).map_err(|e| e.to_string())?; + let mut archive = zip::ZipArchive::new(file).map_err(|e| format!("couldn't read the portable ZIP ({e})"))?; + if archive.len() > MAX_FILES { + return Err("The portable ZIP contains too many files.".into()); + } + let mut files = Vec::new(); + let mut entries = Vec::new(); + let mut total = 0u64; + let mut seen = std::collections::BTreeSet::new(); + for index in 0..archive.len() { + let mut entry = archive.by_index(index).map_err(|e| e.to_string())?; + let relative = entry.enclosed_name().ok_or("The portable ZIP contains an unsafe path.")?; + if relative.components().any(|c| !matches!(c, Component::Normal(_))) + || entry.name().contains(['\\', ':']) + || entry.unix_mode().is_some_and(|mode| mode & 0o170000 == 0o120000) + { + return Err("The portable ZIP contains an unsafe path or link.".into()); + } + // Windows treats case variants as the same file; reject duplicate and colliding names. + if !seen.insert(relative.to_string_lossy().to_lowercase()) { + return Err("The portable ZIP contains duplicate paths.".into()); + } + let target = destination.join(&relative); + entries.push(relative.clone()); + if entry.is_dir() { + std::fs::create_dir_all(&target).map_err(|e| e.to_string())?; + continue; + } + total = total.checked_add(entry.size()).filter(|size| *size <= MAX_BYTES).ok_or("The portable ZIP is too large.")?; + if let Some(parent) = target.parent() { + std::fs::create_dir_all(parent).map_err(|e| e.to_string())?; + } + let mut output = std::fs::OpenOptions::new().write(true).create_new(true).open(&target).map_err(|e| e.to_string())?; + use std::io::Read; + let declared = entry.size(); + let copied = std::io::copy(&mut (&mut entry).take(declared.saturating_add(1)), &mut output).map_err(|e| e.to_string())?; + if copied != declared { + return Err("The portable ZIP has an unexpected unpacked size.".into()); + } + files.push(relative); + } + Ok((files, entries)) +} + +fn recorded_files<'a>(app: &App, record: &'a Record) -> Result<(&'a Path, Vec), String> { + let root = record.path.parent().ok_or("The portable installation has no parent folder.")?; + if record.kind != PackageKind::PortableZip || root.file_name().is_none_or(|n| n != app.id) { + return Err("The launcher has no valid ownership record for this portable folder.".into()); + } + let resolved = resolve_missing(root)?; + if !same_windows_path(root, &resolved) { + return Err("The recorded portable folder resolves through a changed alias. Reinstall it to choose a safe location.".into()); + } + validate_owned_directory(root, root)?; + let mut files = Vec::new(); + for file in &record.files { + let relative = file.strip_prefix(root).map_err(|_| "An installation record points outside its folder.")?; + if relative.as_os_str().is_empty() || relative.components().any(|c| !matches!(c, Component::Normal(_))) { + return Err("The installation record contains an unsafe path.".into()); + } + validate_owned_directory(root, file.parent().ok_or("A recorded file has no parent folder.")?)?; + if let Ok(meta) = std::fs::symlink_metadata(file) + && (linked(&meta) || !meta.is_file()) + { + return Err("A recorded file was replaced by a link or directory.".into()); + } + files.push(relative.to_path_buf()); + } + Ok((root, files)) +} + +/// Keeps existing portable installations at their location and preserves unrecorded user files, +/// unless `replace` allows writing over them. +#[allow(clippy::too_many_arguments)] +pub fn install( + app: &App, + file: &Path, + version: &str, + previous: Option<&Record>, + paths: &Paths, + directory: Option<&Path>, + replace: bool, + commit: &mut dyn FnMut(&Record) -> Result<(), String>, +) -> Result { + let (destination, mut old_files) = match previous { + Some(record) => { + let (root, files) = recorded_files(app, record)?; + (root.to_path_buf(), files) + } + None => { + let parent = directory.map(Path::to_path_buf).unwrap_or_else(|| paths.apps()); + validate_directory(&parent)?; + let parent = resolve_missing(&parent)?; + (parent.join(app.id), Vec::new()) + } + }; + validate_owned_directory(&destination, &destination)?; + let stamp = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map_err(|e| e.to_string())?.as_nanos(); + let staging = paths.cache.join("staging").join(format!("{}-{}-{stamp}", app.id, std::process::id())); + validate_owned_directory(&paths.cache, &staging)?; + std::fs::create_dir_all(staging.parent().ok_or("no staging folder")?).map_err(|e| e.to_string())?; + std::fs::create_dir(&staging).map_err(|e| e.to_string())?; + let staging = Staging(staging); + let unpacked = staging.0.join("unpacked"); + std::fs::create_dir(&unpacked).map_err(|e| e.to_string())?; + let (files, entries) = extract(file, &unpacked)?; + // Accept both a flat layout (files at the ZIP root) and a single wrapper-folder layout, + // but never arbitrary nesting. + let (unpacked, files) = flatten_zip_layout(app, &unpacked, files, &entries)?; + let executable = files + .iter() + .find(|relative| is_root_executable(relative, app)) + .ok_or("The portable ZIP does not contain the expected executable at its root.")? + .clone(); + // Validate the entire transaction before moving any existing program file. + for relative in &files { + let target = destination.join(relative); + validate_owned_directory(&destination, target.parent().ok_or("no destination folder")?)?; + if let Ok(meta) = std::fs::symlink_metadata(&target) { + let recorded = old_files.iter().any(|old| same_windows_path(old, relative)); + if linked(&meta) || !meta.is_file() || !(recorded || replace) { + return Err("The installation would overwrite an unrecorded file.".into()); + } + if !recorded { + old_files.push(relative.clone()); + } + } + } + std::fs::create_dir_all(&destination).map_err(|e| e.to_string())?; + let backup = staging.0.join("backup"); + let record = Record { + version: version.to_string(), + kind: PackageKind::PortableZip, + path: destination.join(executable), + installed_at: crate::now(), + files: files.iter().map(|relative| destination.join(relative)).collect(), + needs_root: false, + }; + let mut moved = Vec::new(); + let mut copied = Vec::new(); + let transaction = (|| { + for relative in &old_files { + let old = destination.join(relative); + if !old.exists() { + continue; + } + let saved = backup.join(relative); + std::fs::create_dir_all(saved.parent().ok_or("no backup folder")?).map_err(|e| e.to_string())?; + // Copy the rollback copy: cache and custom installation can be on different drives. + std::fs::copy(&old, &saved).map_err(|e| e.to_string())?; + std::fs::remove_file(&old).map_err(|e| e.to_string())?; + moved.push(relative.clone()); + } + for relative in &files { + let target = destination.join(relative); + std::fs::create_dir_all(target.parent().ok_or("no destination folder")?).map_err(|e| e.to_string())?; + let mut input = std::fs::File::open(unpacked.join(relative)).map_err(|e| e.to_string())?; + let mut output = std::fs::OpenOptions::new().write(true).create_new(true).open(&target).map_err(|e| e.to_string())?; + copied.push(relative.clone()); + std::io::copy(&mut input, &mut output).map_err(|e| e.to_string())?; + } + commit(&record)?; + Ok::<(), String>(()) + })(); + if let Err(error) = transaction { + for relative in copied { + let _ = std::fs::remove_file(destination.join(relative)); + } + for relative in moved { + if let Err(restore) = std::fs::copy(backup.join(&relative), destination.join(&relative)) { + // Preserve backups for manual recovery if the disk won't accept a rollback. + let backup_path = staging.keep(); + return Err(format!("{error}; could not restore the previous version ({restore}). Recovery files: {}", backup_path.display())); + } + } + return Err(error); + } + Ok(record) +} + +/// Whether a ZIP-relative path sits at the archive root and is one of the app's executables. +fn is_root_executable(relative: &Path, app: &App) -> bool { + relative.parent().is_some_and(|parent| parent.as_os_str().is_empty()) + && relative.file_name().and_then(OsStr::to_str).is_some_and(|name| app.stems().any(|stem| name.eq_ignore_ascii_case(&format!("{stem}.exe")))) +} + +/// Flattens a portable ZIP whose files all sit in one top-level folder (the official Windows +/// packaging script's `Compress-Archive -Path $Portable` puts everything under a versioned +/// directory) into the flat layout a root launcher exe requires, passing already-flat layouts +/// through. ZIPs mixing unrelated top-level folders are rejected. +fn flatten_zip_layout(app: &App, unpacked: &Path, files: Vec, entries: &[PathBuf]) -> Result<(PathBuf, Vec), String> { + if files.iter().any(|relative| is_root_executable(relative, app)) { + return Ok((unpacked.to_path_buf(), files)); + } + let prefix = files.first().and_then(|path| path.components().next()).ok_or("The portable ZIP contains no files.")?.as_os_str().to_os_string(); + let prefix = PathBuf::from(prefix); + // Include explicit ZIP directory entries as well as extracted files. + // The wrapper directory itself is allowed; unrelated roots are not. + if !entries.iter().all(|path| path.starts_with(&prefix)) + || !files.iter().all(|path| path.strip_prefix(&prefix).is_ok_and(|relative| relative.components().next().is_some())) + { + return Err("The portable ZIP has an unsupported directory layout.".into()); + } + let unpacked = unpacked.join(&prefix); + let files: Vec = + files.into_iter().map(|path| path.strip_prefix(&prefix).map(Path::to_path_buf).map_err(|e| e.to_string())).collect::, _>>()?; + Ok((unpacked, files)) +} + +/// Removes only recorded program files. Personal settings and documents remain. +pub fn remove(app: &App, record: &Record) -> Result<(), String> { + let (root, files) = recorded_files(app, record)?; + for relative in &files { + let file = root.join(relative); + if file.exists() { + std::fs::remove_file(&file).map_err(|e| e.to_string())?; + } + } + for relative in &files { + let mut directory = root.join(relative).parent().map(Path::to_path_buf); + while let Some(path) = directory.filter(|p| p.starts_with(root)) { + if std::fs::remove_dir(&path).is_err() || path == root { + break; + } + directory = path.parent().map(Path::to_path_buf); + } + } + Ok(()) +} + +/// Unpacks the launcher's own portable zip into `staging`, with the same checks as an app's, and +/// copies it over `folder` for a self-update. `check` must accept every unpacked file before +/// any is copied, and again before the backup is removed. The archive must include `executable`. +/// When every file sits in one top-level folder, that folder's contents are the program. A copy +/// that fails puts the previous files back. When one of them can't be put back, +/// the previous files move to a `-recovery-