diff --git a/crates/midra-layout/src/device_ui.rs b/crates/midra-layout/src/device_ui.rs index 2b2e5ae..e080ada 100644 --- a/crates/midra-layout/src/device_ui.rs +++ b/crates/midra-layout/src/device_ui.rs @@ -20,6 +20,7 @@ pub(super) struct DeviceUi { pub(super) prepare_send: bool, target: Target, pub(super) allow_program: bool, + pub(super) autoconnect_at_startup: bool, sizing: Sizing, pub(super) sending: bool, message: String, @@ -31,6 +32,11 @@ pub(super) struct DeviceUi { error: String, } impl DeviceUi { + pub(super) fn try_startup_connection(&mut self, ctx: &egui::Context) { + if self.autoconnect_at_startup && !self.host.trim().is_empty() { + self.fetch(ctx); + } + } pub(super) fn reset_mappings(&mut self) { self.destinations.fill(Destination::Skip); } @@ -530,6 +536,26 @@ mod tests { .unwrap() } #[test] + fn startup_connection_requires_opt_in_and_an_address_and_never_sends() { + let ctx = egui::Context::default(); + for (enabled, host, expected) in [ + (false, "invalid-ip", false), + (true, " ", false), + (true, "invalid-ip", true), + ] { + // Invalid IP fails before any network access, while exercising the real worker path. + let mut state = DeviceUi { + autoconnect_at_startup: enabled, + host: host.into(), + ..Default::default() + }; + state.try_startup_connection(&ctx); + assert_eq!(state.pending.is_some(), expected); + assert!(!state.sending); + assert!(!state.online()); + } + } + #[test] fn all_linked_plans_include_hidden_locked_unselected_layers_across_screens() { let mut snapshot = linked_test_snapshot(); let second = snapshot.raw["device"]["screenList"]["items"]["1"].clone(); diff --git a/crates/midra-layout/src/gui.rs b/crates/midra-layout/src/gui.rs index 94c3dd5..673f5cc 100644 --- a/crates/midra-layout/src/gui.rs +++ b/crates/midra-layout/src/gui.rs @@ -266,7 +266,9 @@ fn bounds(g: Geometry) -> String { impl Editor { pub(super) fn with_saved_settings(ctx: &egui::Context) -> Self { let (store, saved) = settings::Store::open(); - Self::with_settings(ctx, store, saved) + let mut editor = Self::with_settings(ctx, store, saved); + editor.device.try_startup_connection(ctx); + editor } fn with_settings( ctx: &egui::Context, @@ -275,11 +277,13 @@ impl Editor { ) -> Self { theme::Theme::restore_recent(ctx, saved.recent_themes.clone()); saved.theme.apply(ctx); + let mut device = device_ui::DeviceUi::with_connection_preferences( + saved.midra_host, + saved.allow_sending_to_program, + ); + device.autoconnect_at_startup = saved.autoconnect_at_startup; Self { - device: device_ui::DeviceUi::with_connection_preferences( - saved.midra_host, - saved.allow_sending_to_program, - ), + device, settings: store, snapping: saved.snapping, keep_aspect_ratio: saved.keep_aspect_ratio, @@ -291,6 +295,7 @@ impl Editor { fn application_settings(&self, ctx: &egui::Context) -> settings::Settings { settings::Settings { midra_host: self.device.host.clone(), + autoconnect_at_startup: self.device.autoconnect_at_startup, allow_sending_to_program: self.device.allow_program, theme: theme::Theme::current(ctx), recent_themes: theme::Theme::recent(ctx), diff --git a/crates/midra-layout/src/preferences_ui.rs b/crates/midra-layout/src/preferences_ui.rs index bbae210..e12f5d3 100644 --- a/crates/midra-layout/src/preferences_ui.rs +++ b/crates/midra-layout/src/preferences_ui.rs @@ -97,6 +97,7 @@ impl Editor { theme::Theme::restore_recent(ctx, defaults.recent_themes); defaults.theme.apply(ctx); self.device.allow_program = false; + self.device.autoconnect_at_startup = false; self.snapping = defaults.snapping; self.keep_aspect_ratio = defaults.keep_aspect_ratio; self.show_pattern = defaults.show_source_pattern; @@ -105,6 +106,7 @@ impl Editor { } Section::Device => { ui.heading("Midra integration"); + ui.checkbox(&mut self.device.autoconnect_at_startup, "Try autoconnecting at startup"); ui.checkbox(&mut self.device.allow_program, "Allow sending to Program"); ui.label("Read Preview layer containers from an Eikos 4K and review their mapping before import."); if ui.button("Open Midra connection…").clicked() { self.device.connection_open = true; } diff --git a/crates/midra-layout/src/settings.rs b/crates/midra-layout/src/settings.rs index 9777125..b0f2560 100644 --- a/crates/midra-layout/src/settings.rs +++ b/crates/midra-layout/src/settings.rs @@ -8,6 +8,7 @@ use std::{io::Read, path::PathBuf}; pub struct Settings { pub version: u32, pub midra_host: String, + pub autoconnect_at_startup: bool, pub allow_sending_to_program: bool, pub theme: Theme, pub recent_themes: Vec, @@ -19,7 +20,8 @@ pub struct Settings { impl Default for Settings { fn default() -> Self { Self { - version: 5, + version: 6, + autoconnect_at_startup: false, allow_sending_to_program: false, midra_host: String::new(), theme: Theme::Midra, @@ -72,12 +74,19 @@ impl Settings { value["allow_sending_to_program"] = false.into(); value["version"] = 5.into(); } + if value["version"] == 5 { + if value.get("autoconnect_at_startup").is_some() { + return Err("Unexpected autoconnect_at_startup in version 5 settings".into()); + } + value["autoconnect_at_startup"] = false.into(); + value["version"] = 6.into(); + } let settings: Self = serde_json::from_value(value).map_err(|e| e.to_string())?; settings.validate()?; Ok(settings) } fn validate(&self) -> Result<(), String> { - if self.version != 5 { + if self.version != 6 { return Err(format!("unsupported settings version {}", self.version)); } if self.recent_themes.is_empty() @@ -197,6 +206,7 @@ mod tests { settings.show_source_pattern = true; settings.midra_host = "192.168.10.143".into(); settings.allow_sending_to_program = true; + settings.autoconnect_at_startup = true; store.save(&settings); assert!(store.error.is_none()); assert_eq!(Store::at(path.clone()).1, settings); @@ -216,7 +226,7 @@ mod tests { for text in [ "broken".to_string(), serde_json::to_string(&Settings { - version: 6, + version: 7, ..Default::default() }) .unwrap(), @@ -241,6 +251,22 @@ mod tests { assert_eq!(Store::at(blocker.join("settings.json")).1, settings); } #[test] + fn version_six_migrates_with_autoconnect_disabled() { + let example = + include_str!("../../../docs/interfaces/midra-layout-settings-v6.example.json"); + assert_eq!(Settings::parse(example).unwrap(), Settings::default()); + let mut legacy: serde_json::Value = serde_json::from_str(include_str!( + "../../../docs/interfaces/midra-layout-settings-v5.example.json" + )) + .unwrap(); + legacy["midra_host"] = "192.168.10.143".into(); + let migrated = Settings::parse(&legacy.to_string()).unwrap(); + assert!(!migrated.autoconnect_at_startup); + assert_eq!(migrated.midra_host, "192.168.10.143"); + legacy["autoconnect_at_startup"] = true.into(); + assert!(Settings::parse(&legacy.to_string()).is_err()); + } + #[test] fn version_five_disables_program_for_legacy_settings() { let example = include_str!("../../../docs/interfaces/midra-layout-settings-v5.example.json"); diff --git a/docs/interfaces/midra-layout-settings-v6.example.json b/docs/interfaces/midra-layout-settings-v6.example.json new file mode 100644 index 0000000..a3b7e25 --- /dev/null +++ b/docs/interfaces/midra-layout-settings-v6.example.json @@ -0,0 +1,19 @@ +{ + "version": 6, + "theme": "midra", + "recent_themes": [ + "midra" + ], + "show_toolbar_icon_names": false, + "keep_aspect_ratio": true, + "show_source_pattern": false, + "snapping": { + "enabled": false, + "pixels": true, + "screen_edges": true, + "layer_edges": true + }, + "midra_host": "", + "allow_sending_to_program": false, + "autoconnect_at_startup": false +} diff --git a/docs/interfaces/midra-layout-settings.md b/docs/interfaces/midra-layout-settings.md index c48429f..fad64a5 100644 --- a/docs/interfaces/midra-layout-settings.md +++ b/docs/interfaces/midra-layout-settings.md @@ -1,13 +1,13 @@ -# Midra Layout application settings, version 5 +# Midra Layout application settings, version 6 Implementation: `crates/midra-layout/src/settings.rs`. This per-user JSON file is independent of the [project JSON and SVG contracts](midra-layout.md). -[Complete default example](midra-layout-settings-v5.example.json). +[Complete default example](midra-layout-settings-v6.example.json). Version 1 files migrate automatically: the saved theme becomes the only entry in `recent_themes`, with all other preferences preserved. The [version 1 example](midra-layout-settings-v1.example.json) remains a migration fixture. Version 2 files then migrate by adding disabled snapping with all target types enabled. The [version 2 example](midra-layout-settings-v2.example.json) -remains a migration fixture too. Version 3 files migrate by adding an empty `midra_host`; the [version 3 example](midra-layout-settings-v3.example.json) remains a migration fixture. Version 4 files migrate by adding `allow_sending_to_program: false`; the [version 4 example](midra-layout-settings-v4.example.json) remains a migration fixture. Saving writes version 5; old applications reject it. +remains a migration fixture too. Version 3 files migrate by adding an empty `midra_host`; the [version 3 example](midra-layout-settings-v3.example.json) remains a migration fixture. Version 4 files migrate by adding `allow_sending_to_program: false`; the [version 4 example](midra-layout-settings-v4.example.json) remains a migration fixture. Version 5 files migrate with `autoconnect_at_startup: false`; the [version 5 example](midra-layout-settings-v5.example.json) remains a migration fixture. Saving writes version 6; old applications reject it. The `directories` 6 crate resolves `ProjectDirs::from("org", "beffroi", "midra-layout")`. The filename is `settings.json` beneath `config_dir()`: @@ -24,7 +24,8 @@ unknown theme names are rejected. There are no numeric coordinates or units. | Field | Values / default | | --- | --- | -| `version` | Integer `5` | +| `version` | Integer `6` | +| `autoconnect_at_startup` | Boolean; default false. Try one background connection at application startup if the saved address is nonblank. | | `allow_sending_to_program` | Boolean; default false. Enables the Program destination in the exporter. Turning it off restores Preview; applies to quick Apply too. | | `midra_host` | Device IP address text; default `""`. Saved as typed, including incomplete drafts; validated when connecting. | | `theme` | Theme ID from the catalog below; default `"midra"` | @@ -56,7 +57,7 @@ the previous destination where atomic publication failed; Options offers a retry Failure to locate a configuration directory also disables persistence with an Options error. Tests use temporary directories and never touch user settings. -The device address is saved immediately when edited in **Midra connection…**. Loading settings never connects or sends. Opening a project preserves the current connection and address, clears import choices, and loads the project’s saved layer destination links. Connection status and snapshots are not persisted. Layer destination links are saved in project JSON, not application settings. Restore default preferences leaves the connection address unchanged; edit it in the connection window. +The device address is saved immediately when edited in **Midra connection…**. Startup attempts a read-only connection only when `autoconnect_at_startup` is enabled and the saved address is nonblank. It never sends layer settings. Opening a project preserves the current connection and address, clears import choices, and loads the project’s saved layer destination links. Connection status and snapshots are not persisted. Layer destination links are saved in project JSON, not application settings. Restore default preferences disables autoconnect and leaves the connection address unchanged; edit it in the connection window. No layer geometry, source crops, camera state, project paths, image bytes or clipboard contents are stored. No settings are exported to SVG. Breaking or additive schema changes under this strict contract require a version @@ -107,3 +108,5 @@ when the resulting edge actually reaches its target. Shift temporarily inverts snapping during layer resizing, including release, without changing the saved option. Alt temporarily disables it and takes precedence over Shift. **Preferences → Midra integration → Allow sending to Program** saves immediately. Restore default preferences disables it. Projects do not override this application preference; the Preview/Program destination itself remains session-only. + +**Try autoconnecting at startup** is in Preferences → Midra integration. It saves immediately but applies on the next launch; toggling it does not initiate a connection. One background attempt uses the regular connection timeout and error handling, with no automatic retry loop. Failures leave the app offline and are reported in Midra connection. Project loading does not trigger an attempt. diff --git a/docs/midra-layout-device-check.md b/docs/midra-layout-device-check.md index 16699be..9ec399d 100644 --- a/docs/midra-layout-device-check.md +++ b/docs/midra-layout-device-check.md @@ -53,8 +53,7 @@ or an existing visible, unlocked editor layer. Click **Import layers** directly in this window; no prior selection or matching layer count is required. Duplicate or removed destinations are rejected without partial changes. The entire import, including new layers, is one undo step. Output resolutions must match. Reading never -changes the document; only applying the reviewed import does. No automatic -connection or polling occurs. The address is remembered in application settings; no credentials are stored. The +changes the document; only applying the reviewed import does. An optional **Try autoconnecting at startup** preference makes one background read attempt using the remembered address. No polling or automatic retries occur. The address is remembered in application settings; no credentials are stored. The footer shows **ONLINE** after a successful read (last-read status, not continuous monitoring), and **OFFLINE** when refreshing, after a failed read, or after editing the address. diff --git a/docs/midra-layout.md b/docs/midra-layout.md index 71e4e5a..238da41 100644 --- a/docs/midra-layout.md +++ b/docs/midra-layout.md @@ -517,7 +517,7 @@ handles retain their existing behavior. One drag remains one undo step, with Esc ## Midra connection and sending -Open **Midra connection…** in the main menu (also in **Preferences → Midra integration**), enter the device IP, then **Connect**. The address saves automatically in the application config and survives project loads and restarts; startup does not connect automatically. +Open **Midra connection…** in the main menu (also in **Preferences → Midra integration**), enter the device IP, then **Connect**. The address saves automatically in the application config and survives project loads and restarts; startup connects automatically only if **Try autoconnecting at startup** is enabled in Preferences → Midra integration. Open **Import → Midra layers…** for the separate layer import window. **Refresh layers** reads a fresh snapshot; **Connection settings…** opens the connection window. Choose a destination beside each device layer: existing layer, new layer, or skip.