Skip to content

Commit 205184f

Browse files
authored
feat(win): portable install location picker (rebased on i18n, review fixes folded in) (#8)
Lets Windows users choose where the portable Codex build installs, persisted across uninstalls. The backend gains a validated `install_root` (migrated from old settings) used across plan/stage/status/adopt/perform/uninstall; the UI adds a native folder picker (invoked through a Rust command), a Settings location row, and a fresh-install location sheet. Rebased onto main after the i18n PR (#7) merged, so the previously duplicated install-location strings now come from #7 only. The unused npm @tauri-apps/plugin-dialog dependency is dropped — the picker goes through a Rust command and nothing imports the JS binding (the Rust tauri-plugin-dialog stays). Review fixes folded in: - validate_install_root_path no longer creates the target directory to probe it (it probes the nearest existing ancestor), so selecting/resetting a location no longer leaves folders on disk; the dir is created at install time. - the chosen install root is persisted only AFTER a successful install, and the Browse flow is one-shot, so a failed/cancelled attempt keeps the saved default. - a managed portable build is gracefully closed before MSIX sideload, instead of potentially being left running after the switch. - uninstall and status use is_managed_build (path + build) like the macOS path, not path-only is_managed — safer now that the install root is user-configurable and paths can be reused.
1 parent 996fe15 commit 205184f

16 files changed

Lines changed: 919 additions & 54 deletions

File tree

‎src-tauri/Cargo.lock‎

Lines changed: 68 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src-tauri/Cargo.toml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ serde = { version = "1.0", features = ["derive"] }
2828
serde_json = "1.0"
2929
tauri = { version = "2.11.2", features = ["tray-icon", "image-png", "macos-private-api"] }
3030
tauri-plugin-autostart = "2"
31+
tauri-plugin-dialog = "2.7.1"
3132
tauri-plugin-process = "2"
3233
tauri-plugin-updater = "2"
3334
thiserror = "2.0"
35+
windows-sys = { version = "0.61.2", features = ["Win32_Storage_FileSystem"] }

‎src-tauri/capabilities/default.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
"core:default",
88
"core:window:allow-start-dragging",
99
"core:window:allow-close",
10+
"dialog:allow-open",
1011
"updater:default",
1112
"process:default"
1213
]
1314
}
14-

‎src-tauri/src/app/settings_store.rs‎

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
//! Codex bundle), mirroring `provenance::ProvenanceStore`.
55
66
use serde::{Deserialize, Serialize};
7+
use std::path::PathBuf;
78

9+
use crate::adapters::host;
10+
use crate::domain::target::Target;
811
use crate::errors::AppError;
912

1013
#[derive(Debug, Clone, Serialize, Deserialize)]
@@ -25,6 +28,10 @@ pub struct AppSettings {
2528
/// still fall back to portable when the machine blocks sideloading.
2629
#[serde(default = "default_windows_install_mode")]
2730
pub windows_install_mode: String,
31+
/// Portable Windows install root. Kept across uninstall so the next fresh
32+
/// portable install can default to the user's last chosen location.
33+
#[serde(default = "default_install_root")]
34+
pub install_root: String,
2835
}
2936

3037
fn default_true() -> bool {
@@ -35,6 +42,10 @@ fn default_windows_install_mode() -> String {
3542
"msix".to_string()
3643
}
3744

45+
pub fn default_install_root() -> String {
46+
host::default_install_root(&Target::current())
47+
}
48+
3849
impl Default for AppSettings {
3950
fn default() -> Self {
4051
Self {
@@ -45,6 +56,7 @@ impl Default for AppSettings {
4556
signed_only: true,
4657
confirm_close: true,
4758
windows_install_mode: default_windows_install_mode(),
59+
install_root: default_install_root(),
4860
}
4961
}
5062
}
@@ -55,6 +67,20 @@ fn store_path() -> Option<std::path::PathBuf> {
5567
}
5668

5769
impl AppSettings {
70+
pub fn normalize(&mut self) {
71+
self.signed_only = true; // enforce regardless of what is on disk
72+
if !matches!(self.windows_install_mode.as_str(), "msix" | "portable") {
73+
self.windows_install_mode = default_windows_install_mode();
74+
}
75+
if self.install_root.trim().is_empty()
76+
|| !PathBuf::from(self.install_root.trim()).is_absolute()
77+
{
78+
self.install_root = default_install_root();
79+
} else {
80+
self.install_root = self.install_root.trim().to_string();
81+
}
82+
}
83+
5884
pub fn load() -> Self {
5985
let Some(path) = store_path() else {
6086
return Self::default();
@@ -63,10 +89,7 @@ impl AppSettings {
6389
.ok()
6490
.and_then(|bytes| serde_json::from_slice(&bytes).ok())
6591
.unwrap_or_default();
66-
s.signed_only = true; // enforce regardless of what is on disk
67-
if !matches!(s.windows_install_mode.as_str(), "msix" | "portable") {
68-
s.windows_install_mode = default_windows_install_mode();
69-
}
92+
s.normalize();
7093
s
7194
}
7295

‎src-tauri/src/app/win_update.rs‎

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,13 @@ fn detect_managed_codex(
179179
return Some(portable);
180180
}
181181
}
182+
for record in &store.managed {
183+
if let Some(portable) = detect_portable_install(PathBuf::from(&record.path).as_path()) {
184+
if store.is_managed(&portable.path) {
185+
return Some(portable);
186+
}
187+
}
188+
}
182189
detect_installed_codex(root.as_path())
183190
}
184191

@@ -426,7 +433,8 @@ pub fn auto_stage_windows_update_with_install_mode(
426433
}
427434
}
428435

429-
let stage = stage_windows_update_with_install_mode(endpoints, settings, install_mode, &no_progress)?;
436+
let stage =
437+
stage_windows_update_with_install_mode(endpoints, settings, install_mode, &no_progress)?;
430438
let notes = if stage.install_ready {
431439
vec!["Windows package is staged and ready for user-confirmed installation.".to_string()]
432440
} else {
@@ -470,9 +478,11 @@ pub fn perform_windows_update_with_install_mode(
470478
));
471479
}
472480

473-
let previous_installed =
474-
detect_installed_codex(PathBuf::from(&settings.install_root).as_path());
475-
let stage = stage_windows_update_with_install_mode(endpoints, settings, install_mode, progress)?;
481+
let store = ProvenanceStore::load();
482+
let previous_installed = detect_managed_codex(settings, &store)
483+
.or_else(|| detect_installed_codex(PathBuf::from(&settings.install_root).as_path()));
484+
let stage =
485+
stage_windows_update_with_install_mode(endpoints, settings, install_mode, progress)?;
476486
if stage.up_to_date {
477487
return Ok(WinPerformReport {
478488
success: true,
@@ -503,6 +513,16 @@ pub fn perform_windows_update_with_install_mode(
503513
.map_err(engine_err)?;
504514
}
505515
}
516+
// A managed portable build (possibly under a previous install root) is not
517+
// stopped by the MSIX sideload below, so close it first — otherwise it keeps
518+
// running after we switch the user over to the MSIX package. `previous_installed`
519+
// comes from the provenance-aware detect_managed_codex above.
520+
if let Some(previous) = &previous_installed {
521+
if previous.source == "portable" {
522+
close_codex_gracefully_for_root(30, PathBuf::from(&previous.path).as_path())
523+
.map_err(engine_err)?;
524+
}
525+
}
506526
let sideload =
507527
install_msix_sideload(PathBuf::from(staged_path).as_path()).map_err(engine_err)?;
508528

@@ -551,9 +571,14 @@ fn install_portable_after_stage(
551571
.staged_path
552572
.as_ref()
553573
.ok_or_else(|| AppError::Engine("staged MSIX path missing".to_string()))?;
574+
let install_root = previous_installed
575+
.as_ref()
576+
.filter(|installed| installed.source == "portable")
577+
.map(|installed| installed.path.clone())
578+
.unwrap_or_else(|| settings.install_root.clone());
554579
let portable = install_portable_from_msix(
555580
PathBuf::from(staged_path).as_path(),
556-
PathBuf::from(&settings.install_root).as_path(),
581+
PathBuf::from(&install_root).as_path(),
557582
true,
558583
)
559584
.map_err(engine_err)?;
@@ -562,7 +587,7 @@ fn install_portable_after_stage(
562587
// which prefers MSIX and would return a still-present older MSIX package
563588
// (e.g. when sideload was blocked by policy), recording the wrong target so
564589
// the user keeps seeing the same update and the portable build goes unmanaged.
565-
let installed = detect_portable_install(PathBuf::from(&settings.install_root).as_path());
590+
let installed = detect_portable_install(PathBuf::from(&install_root).as_path());
566591
if let Some(installed) = &installed {
567592
let mut store = ProvenanceStore::load();
568593
if let Some(previous) = &previous_installed {
@@ -608,7 +633,10 @@ pub fn win_install_status(settings: &AppSettings) -> WinInstallStatus {
608633
let installed = detect_managed_codex(settings, &store);
609634
let status = match &installed {
610635
None => "none",
611-
Some(codex) if store.is_managed(&codex.path) => "managed",
636+
// Build-aware (matching the macOS path): a self-updated or path-reused
637+
// install no longer matches its record and reads as "external" so the
638+
// user is prompted to re-adopt rather than silently treated as managed.
639+
Some(codex) if store.is_managed_build(&codex.path, version_key(&codex.version)) => "managed",
612640
Some(_) => "external",
613641
}
614642
.to_string();
@@ -654,7 +682,11 @@ pub fn uninstall_windows_codex(
654682
};
655683

656684
let mut store = ProvenanceStore::load();
657-
if !store.is_managed(&installed_before.path) {
685+
// Boundary (matching the macOS uninstall): refuse to delete anything that
686+
// isn't an install we manage at this exact build. Path-only matching could
687+
// delete a path-reused external install or one left by a stale record —
688+
// more likely now that the install root is user-configurable.
689+
if !store.is_managed_build(&installed_before.path, version_key(&installed_before.version)) {
658690
return Ok(WinUninstallReport {
659691
success: false,
660692
action: "external-not-managed".to_string(),
@@ -702,7 +734,7 @@ pub fn uninstall_windows_codex(
702734
}
703735

704736
let portable = uninstall_portable(
705-
PathBuf::from(&settings.install_root).as_path(),
737+
PathBuf::from(&installed_before.path).as_path(),
706738
purge_user_data,
707739
)
708740
.map_err(engine_err)?;

0 commit comments

Comments
 (0)