From d63814bde1766838d4c6db0b573cb4861916f91e Mon Sep 17 00:00:00 2001 From: CRT Date: Wed, 30 Sep 2026 11:34:13 +0000 Subject: [PATCH] Host: 256 feature flags, the onboarding mode (mode_get/mode_set, shell-only), and the one-time move of replaced plugin files to feature flags - settings: MAX_FEATURES 64 -> 256 (an older app's whole-map backup still validates) - modes: ModeState {mode, onboarded, existing} in modes.json; existing is decided natively on the first start - plugins::migrate: PluginHost.superseded (file -> flag); enabled and as shipped -> flag on and the file goes; edited -> kept, flag left off; marker written last, every step repeatable; map_backup for imported enabled lists - a superseded file is never written again by install_bundled - create-waybetter: the host command list and PluginHost.superseded --- crates/waybetter-host/src/app.rs | 48 ++- crates/waybetter-host/src/commands.rs | 2 + crates/waybetter-host/src/lib.rs | 3 + crates/waybetter-host/src/modes.rs | 85 +++++ crates/waybetter-host/src/plugins.rs | 7 +- crates/waybetter-host/src/plugins/migrate.rs | 316 +++++++++++++++++++ crates/waybetter-host/src/settings.rs | 17 +- crates/waybetter-host/src/store/install.rs | 1 + packages/create-waybetter/src/app.js | 4 +- 9 files changed, 477 insertions(+), 6 deletions(-) create mode 100644 crates/waybetter-host/src/modes.rs create mode 100644 crates/waybetter-host/src/plugins/migrate.rs diff --git a/crates/waybetter-host/src/app.rs b/crates/waybetter-host/src/app.rs index a0ffc15..9b223f4 100644 --- a/crates/waybetter-host/src/app.rs +++ b/crates/waybetter-host/src/app.rs @@ -22,6 +22,7 @@ use crate::{ host::{self, emit_shell, emit_tab_state, read_json, shell_only, site_tab_only, tab_only, tab_webview, write_json}, i18n::{self, NativeStrings}, layout, + modes::{ModeState, MODES_FILE}, plugins::{self, PluginHost, PluginStore, ScriptPlugin}, policy, profiles::{self, AccountSeen, ProfileSettings}, @@ -203,6 +204,8 @@ pub struct PageState { /// A JSON object of booleans ([`settings::validate_features`]). pub features: serde_json::Value, pub settings: ShellSettings, + /// The onboarding mode the owner started from and whether the first-run screen was answered. + pub modes: ModeState, } struct Profiles(Mutex); @@ -715,6 +718,25 @@ async fn features_set(webview: Webview, app: AppHandle, features: serde_json::Ma set_features(&app, features) } +/// The onboarding mode: what was chosen, whether the first-run screen was answered, and whether +/// this app was set up before modes existed. +#[tauri::command] +async fn mode_get(webview: Webview, app: AppHandle) -> Result { + shell_only(&webview)?; + Ok(page(&app)?.modes.clone()) +} + +/// Remembers the chosen mode (or none) and that the first-run screen was answered. The preset's +/// flags are the shell's to apply (`features_set`); this only records the choice. +#[tauri::command] +async fn mode_set(webview: Webview, app: AppHandle, mode: Option, onboarded: bool) -> Result { + shell_only(&webview)?; + let next = page(&app)?.modes.chosen(mode, onboarded)?; + write_json(&app, MODES_FILE, &next)?; + page(&app)?.modes = next.clone(); + Ok(next) +} + #[tauri::command] async fn settings_get(webview: Webview, app: AppHandle) -> Result { shell_only(&webview)?; @@ -952,6 +974,8 @@ fn host_handler() -> impl Fn(tauri::ipc::Invoke) -> bool + Send + Sync + 's custom_css_set, features_get, features_set, + mode_get, + mode_set, settings_get, settings_set, tab_shortcut, @@ -1019,6 +1043,8 @@ impl App { } let directory = tauri_app.path().app_data_dir()?; std::fs::create_dir_all(&directory)?; + // Someone set this app up before (read before anything below writes a file). + let existing = [PROFILES_FILE, SETTINGS_FILE, FEATURES_FILE, PLUGINS_DIR].iter().any(|name| directory.join(name).exists()); #[cfg(unix)] { use std::os::unix::fs::PermissionsExt; @@ -1042,10 +1068,28 @@ impl App { let settings = read_json::(&handle, SETTINGS_FILE) .and_then(|value| ShellSettings::from_value(value, &home).ok()) .unwrap_or_else(|| ShellSettings::defaults(&home)); - let features = read_json::(&handle, FEATURES_FILE) + let mut features = read_json::(&handle, FEATURES_FILE) .filter(serde_json::Value::is_object) .unwrap_or_else(|| serde_json::json!({})); - tauri_app.manage(Page(Mutex::new(PageState { plugins, features, settings }))); + // Plugin files the app turned into features: what was enabled becomes its flag, once + // (plugins::migrate). The flags are saved before any file is touched. + let migrated = plugins.migrate_superseded(config.plugins.superseded, |flags| { + let Some(map) = features.as_object_mut().filter(|_| !flags.is_empty()) else { return Ok(()) }; + for flag in flags { + map.insert(flag.clone(), serde_json::Value::Bool(true)); + } + settings::validate_features(map)?; + write_json(&handle, FEATURES_FILE, &*map) + }); + if let Err(error) = migrated { + eprintln!("{}: {error}", config.title); + } + let modes = read_json::(&handle, MODES_FILE).and_then(|state| state.validated().ok()).unwrap_or_else(|| { + let state = ModeState::first_run(existing); + let _ = write_json(&handle, MODES_FILE, &state); + state + }); + tauri_app.manage(Page(Mutex::new(PageState { plugins, features, settings, modes }))); tauri_app.manage(Host { config, hooks }); if let Some(setup) = setup { setup(&handle)?; diff --git a/crates/waybetter-host/src/commands.rs b/crates/waybetter-host/src/commands.rs index 7cfbf5a..cd744bb 100644 --- a/crates/waybetter-host/src/commands.rs +++ b/crates/waybetter-host/src/commands.rs @@ -28,6 +28,8 @@ pub const SHELL: &[&str] = &[ "custom_css_set", "features_get", "features_set", + "mode_get", + "mode_set", "settings_get", "settings_set", ]; diff --git a/crates/waybetter-host/src/lib.rs b/crates/waybetter-host/src/lib.rs index b940963..2db229c 100644 --- a/crates/waybetter-host/src/lib.rs +++ b/crates/waybetter-host/src/lib.rs @@ -10,6 +10,7 @@ //! - [`policy`]: webview labels, origin checks and the tab rectangle clamp (security boundary). //! - [`profiles`]: account profiles, one WebView data directory each, with the account seen inside. //! - [`plugins`]: owner-installed `*.plugin.js` files, custom CSS and bundled-plugin upgrades; +//! [`plugins::migrate`]: the one-time move of replaced plugin files to feature flags; //! [`plugins::commands`]: the palette commands site tabs report and the script that runs one. //! - [`hotfix`]: the signed, data-only hotfix channel. //! - [`update`]: silent-update scheduling and the crash handshake. @@ -25,6 +26,7 @@ //! - [`devmode`]: developer mode's debounced watcher of plugins, custom CSS and a theme file. //! - [`settings`]: the shell's settings (home page, where links open, session restore) and the page's //! feature flags, as validated data and the scripts that hand them to site tabs. +//! - [`modes`]: the onboarding mode the owner started from (the presets are the app's feature table). //! - [`alerts`]: local keyword alerts (keywords, snippets, per-item/per-keyword rate limits). //! - [`media`]: gallery folders (path containment) and the opt-in clipboard history (private copies skipped). //! - [`gfonts`]: self-hosted Google Fonts: CSS parsing, URL/woff2 checks, the cache format, the page script; @@ -54,6 +56,7 @@ pub mod hotfix; pub mod i18n; pub mod layout; pub mod media; +pub mod modes; pub mod plugins; pub mod policy; pub mod profiles; diff --git a/crates/waybetter-host/src/modes.rs b/crates/waybetter-host/src/modes.rs new file mode 100644 index 0000000..5dccdf0 --- /dev/null +++ b/crates/waybetter-host/src/modes.rs @@ -0,0 +1,85 @@ +//! The onboarding mode: which preset (Basic / Advanced / Power User, whatever the app defines) the +//! owner started from, and whether the first-run screen was answered. A mode is a starting point: +//! the presets themselves are the app's (its feature table) and are applied by the shell through +//! `features_set`; native code only remembers the choice (`modes.json`), so the screen shows once. +//! +//! `existing` is decided natively, once, on the first start of a version that has modes: an app +//! data folder that already held settings belongs to someone who set the app up before. They keep +//! exactly what they have, and the screen they see can be dismissed without choosing. + +use serde::{Deserialize, Serialize}; + +pub const MODES_FILE: &str = "modes.json"; +const MAX_MODE_ID: usize = 32; + +#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase", default)] +pub struct ModeState { + /// The mode chosen last; `None` = never chosen (the app's defaults, or an earlier setup). + pub mode: Option, + /// The first-run screen was answered or dismissed. + pub onboarded: bool, + /// The app was set up before modes existed. + pub existing: bool, +} + +/// A mode id: lowercase letters, digits and `-`, starting with a letter, at most 32 characters. +pub fn valid_mode(id: &str) -> bool { + !id.is_empty() + && id.len() <= MAX_MODE_ID + && id.as_bytes()[0].is_ascii_lowercase() + && id.bytes().all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || b == b'-') +} + +impl ModeState { + /// The state on the first start: nothing chosen, the screen still to be shown. + pub fn first_run(existing: bool) -> Self { + Self { mode: None, onboarded: false, existing } + } + + pub fn validated(self) -> Result { + if self.mode.as_deref().is_some_and(|mode| !valid_mode(mode)) { + return Err("VALIDATION: invalid mode".into()); + } + Ok(self) + } + + /// What the shell may change: the chosen mode and that the screen was answered. `existing` + /// stays what native code decided. + pub fn chosen(&self, mode: Option, onboarded: bool) -> Result { + Self { mode, onboarded, existing: self.existing }.validated() + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn mode_ids_are_short_plain_words() { + for good in ["basic", "advanced", "power-user", "m2"] { + assert!(valid_mode(good), "{good}"); + } + for bad in ["", "Basic", "2fast", "a b", "a/b", "../x", &"a".repeat(33)] { + assert!(!valid_mode(bad), "{bad}"); + } + } + + #[test] + fn the_shell_chooses_a_mode_but_never_what_kind_of_install_this_is() { + let fresh = ModeState::first_run(false); + assert_eq!(fresh, ModeState { mode: None, onboarded: false, existing: false }); + let existing = ModeState::first_run(true); + let dismissed = existing.chosen(None, true).unwrap(); + assert_eq!(dismissed, ModeState { mode: None, onboarded: true, existing: true }, "dismissed without choosing: nothing else changes"); + let chosen = dismissed.chosen(Some("power".into()), true).unwrap(); + assert_eq!(chosen.mode.as_deref(), Some("power")); + assert!(chosen.existing); + assert!(existing.chosen(Some("