Skip to content

DesignCraft: An open-source page and layout design editor written by scratch in pure Rust

feat: startup update prompt modal and direct self-updating

Dylan M. Taylord607c068b1c1
diff --git a/Cargo.toml b/Cargo.toml
index afa0d7be9..53460c838 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -13,6 +13,7 @@ repository = "https://github.com/storytold/designcraft"
[workspace.dependencies]
# Shared reader and writer; publish the immutable revision before this consumer.
craft-pdf = { git = "https://github.com/storytold/craft-libs", rev = "a0916808f7d78bed8c7b90cdf937176015454a7f", version = "=0.1.0", default-features = false }
+craft-update = { git = "https://github.com/storytold/craft-libs", version = "=0.1.0", rev = "5b0f3c572a3b5c1e51bdb90d7ff788e0212ec1cc", features = ["egui"] }
# internal
designcraft-geom = { path = "crates/geom" }
designcraft-color = { path = "crates/color" }
diff --git a/apps/designcraft/Cargo.toml b/apps/designcraft/Cargo.toml
index 1d47ef762..36415546e 100644
--- a/apps/designcraft/Cargo.toml
+++ b/apps/designcraft/Cargo.toml
@@ -32,6 +32,7 @@ arboard = { workspace = true }
winit = { version = "0.30", default-features = false, features = ["x11"] }
[target.'cfg(windows)'.dependencies]
+craft-update = { workspace = true }
# EnumDisplayDevices (src/gpu.rs): which GPU drives the display, so the window draws on the GPU
# that shows it; drawing on a GPU without a monitor reset the display driver. Safe wrappers over
# Win32 (MIT); the `user` feature is User32.dll only.
diff --git a/apps/designcraft/src/main.rs b/apps/designcraft/src/main.rs
index 12f423d79..ac695774b 100644
--- a/apps/designcraft/src/main.rs
+++ b/apps/designcraft/src/main.rs
@@ -398,6 +398,11 @@ fn control_port_from(source: &str, value: Option<String>) -> Option<u16> {
}
fn main() -> std::process::ExitCode {
+ // Windows: the update helper is a copy of this program started with `--finish-update`.
+ #[cfg(windows)]
+ if let Some(result) = craft_update::self_update::finish(&craft_update::suite::DESIGNCRAFT, &std::env::args_os().skip(1).collect::<Vec<_>>()) {
+ std::process::exit(i32::from(result.is_err()));
+ }
// First, so every start-up record (and the engine's panic hook, installed with the first
// Session) is captured; see `logging`.
let logger = logging::install();
@@ -553,6 +558,7 @@ fn main() -> std::process::ExitCode {
session.recovery_dir = profile::recovery_dir();
let recovered = session.execute("file.recovery.open", &serde_json::json!({})).ok();
let mut app = DesignApp::new(session, services());
+ app.set_startup_update_check(true);
if let Some(line) = restarted.or_else(|| created.status_line()) {
app.status(line);
}
diff --git a/crates/ui-egui/Cargo.toml b/crates/ui-egui/Cargo.toml
index 0a826b69e..3d4a7c6f0 100644
--- a/crates/ui-egui/Cargo.toml
+++ b/crates/ui-egui/Cargo.toml
@@ -44,3 +44,6 @@ designcraft-fonts = { workspace = true, features = ["testing"] }
[build-dependencies]
serde_json = { workspace = true }
toml = "1"
+
+[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
+craft-update = { workspace = true }
diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs
index 95d53b798..f3571d38b 100644
--- a/crates/ui-egui/src/lib.rs
+++ b/crates/ui-egui/src/lib.rs
@@ -33,6 +33,7 @@ pub mod story_editor;
pub mod taskbar;
pub mod theme;
pub mod toolbar;
+pub mod updates_ui;
pub mod widgets;
use std::collections::HashMap;
@@ -836,6 +837,7 @@ pub struct DesignApp {
pub cursors: cursors::Images,
/// A panel's live preview (a slider being dragged), committed when it is let go.
live_preview: LivePreview,
+ pub(crate) updates_rt: updates_ui::Runtime,
}
/// The preview a panel opened with `edit.preview`: it stays open while a panel holds it each frame.
@@ -935,6 +937,7 @@ impl DesignApp {
quitting: None,
cursors: cursors::Images::default(),
live_preview: LivePreview::default(),
+ updates_rt: updates_ui::Runtime::default(),
}
}
@@ -1384,6 +1387,7 @@ impl DesignApp {
fn logic_tick(&mut self, ctx: &egui::Context) {
self.update_screenshot_epoch(ctx);
self.drain_control(ctx);
+ updates_ui::tick(self, ctx);
if self.fonts_ready && !self.host_order_pending() {
self.drain_inbox();
self.open_os_documents();
@@ -2149,6 +2153,7 @@ impl DesignApp {
story_editor::show(self, &ctx);
dialogs::show(self, &ctx);
about::show(self, &ctx);
+ updates_ui::show(self, &ctx);
menus::palette(self, &ctx);
// The IME is observed once, here: after the canvas, the floating panels, the story editor,
// dialogs and Quick Apply have all applied this pass's commands and focus changes.
diff --git a/crates/ui-egui/src/menus.rs b/crates/ui-egui/src/menus.rs
index fda15c6bb..e3226044b 100644
--- a/crates/ui-egui/src/menus.rs
+++ b/crates/ui-egui/src/menus.rs
@@ -348,6 +348,7 @@ pub const UI_COMMANDS: &[(&str, &str, Option<&str>, &str)] = &[
None,
"{open?: true, tab?: \"about\"|\"contributors\"|\"models\"} — the About window: splash with community links, contributor and model credits (open: false closes it)",
),
+ ("help.checkForUpdates", "Check for Updates…", None, "{} — checks for newer releases and offers to update"),
("window.toolsDoubleColumn", "Tools: Double Column", None, "{}"),
(
"window.workspace",
@@ -990,6 +991,7 @@ pub const MENUS: &[(&str, &[&str])] = &[
"-",
"cmd:file.newSample",
"-",
+ "ui:help.checkForUpdates",
"ui:help.about",
],
),
@@ -1911,6 +1913,7 @@ fn run_ui_inner(app: &mut DesignApp, id: &str, p: &Value) -> Option<Result<Value
app.ui.about = p.get("open").and_then(Value::as_bool).unwrap_or(true);
Ok(json!(app.ui.about))
}
+ "help.checkForUpdates" => crate::updates_ui::check(app),
"help.app" => match p.get("app").and_then(Value::as_str) {
Some(slug) if !slug.is_empty() && slug.chars().all(|c| c.is_ascii_alphanumeric() || c == '-') => {
let url = format!("{}/apps/{slug}", designcraft_engine::links::WEBSITE);
@@ -2418,6 +2421,7 @@ pub enum AppMenuItem {
/// The macOS application menu. Settings… is `app.preferences`, which `native_menu_tree` leaves out of Edit.
pub const MAC_APP_MENU: &[AppMenuItem] = &[
AppMenuItem::Cmd { id: "dc-about", label: "About DesignCraft", command: "help.about" },
+ AppMenuItem::Cmd { id: "dc-updates", label: "Check for Updates…", command: "help.checkForUpdates" },
AppMenuItem::Cmd { id: "dc-discord", label: "Join the ArtCraft Discord…", command: "help.discord" },
AppMenuItem::Sep,
AppMenuItem::Cmd { id: "dc-settings", label: "Settings…", command: "app.preferences" },
diff --git a/crates/ui-egui/src/updates_ui.rs b/crates/ui-egui/src/updates_ui.rs
new file mode 100644
index 000000000..283242f0e
--- /dev/null
+++ b/crates/ui-egui/src/updates_ui.rs
@@ -0,0 +1,161 @@
+//! Software updates, background check at startup, and startup prompt modal.
+
+use serde_json::Value;
+
+use crate::DesignApp;
+
+#[cfg(not(target_arch = "wasm32"))]
+use craft_update::updater::{Action, Status, Updater};
+
+pub(crate) struct Runtime {
+ #[cfg(not(target_arch = "wasm32"))]
+ updater: Updater,
+ /// The desktop app turns the check at start on; tests and the web build leave it off.
+ #[cfg(not(target_arch = "wasm32"))]
+ startup_check: bool,
+ #[cfg(not(target_arch = "wasm32"))]
+ check_requested: bool,
+ pub(crate) manual_window_open: bool,
+ pub(crate) status_message: Option<String>,
+}
+
+impl Default for Runtime {
+ fn default() -> Self {
+ Self {
+ #[cfg(not(target_arch = "wasm32"))]
+ updater: Updater::new(&craft_update::suite::DESIGNCRAFT, env!("CARGO_PKG_VERSION")),
+ #[cfg(not(target_arch = "wasm32"))]
+ startup_check: false,
+ #[cfg(not(target_arch = "wasm32"))]
+ check_requested: false,
+ manual_window_open: false,
+ status_message: None,
+ }
+ }
+}
+
+impl DesignApp {
+ /// Whether the app asks for a newer release when it starts (off until the desktop app turns it on).
+ #[cfg(not(target_arch = "wasm32"))]
+ pub fn set_startup_update_check(&mut self, on: bool) {
+ self.updates_rt.startup_check = on;
+ }
+}
+
+pub(crate) fn check(app: &mut DesignApp) -> Result<Value, String> {
+ #[cfg(not(target_arch = "wasm32"))]
+ {
+ app.updates_rt.check_requested = true;
+ Ok(Value::Null)
+ }
+ #[cfg(target_arch = "wasm32")]
+ {
+ app.updates_rt.manual_window_open = true;
+ app.updates_rt.status_message = Some("Update check is not supported in the web browser.".into());
+ Ok(Value::Null)
+ }
+}
+
+pub(crate) fn tick(app: &mut DesignApp, ctx: &egui::Context) {
+ #[cfg(not(target_arch = "wasm32"))]
+ {
+ let rt = &mut app.updates_rt;
+ if std::mem::take(&mut rt.check_requested) {
+ rt.updater.check_now(ctx);
+ } else if rt.startup_check {
+ rt.updater.check_at_start(ctx);
+ }
+ rt.updater.tick(ctx);
+ }
+ #[cfg(target_arch = "wasm32")]
+ let _ = (app, ctx);
+}
+
+pub(crate) fn show(app: &mut DesignApp, ctx: &egui::Context) {
+ #[cfg(not(target_arch = "wasm32"))]
+ {
+ match app.updates_rt.updater.show(ctx) {
+ Action::OpenUrl(url) => ctx.open_url(egui::OpenUrl::new_tab(url)),
+ Action::Close => ctx.send_viewport_cmd(egui::ViewportCommand::Close),
+ Action::None => {}
+ }
+ // The window answers Help ▸ Check for Updates, so it follows the updater's status.
+ let rt = &mut app.updates_rt;
+ (rt.manual_window_open, rt.status_message) = match rt.updater.status() {
+ Status::Idle => (false, None),
+ Status::Checking => (true, Some("Checking for updates…".to_string())),
+ Status::UpToDate => (true, Some(format!("DesignCraft {} is currently the newest version available.", env!("CARGO_PKG_VERSION")))),
+ Status::Failed(e) => (true, Some(format!("Could not check for updates:\n{e}"))),
+ };
+ }
+
+ if app.updates_rt.manual_window_open {
+ let mut open = true;
+ let title = crate::i18n::tr(&app.ui.language, "DesignCraft Updates");
+ let ok_label = crate::i18n::tr(&app.ui.language, "OK");
+ egui::Window::new(title)
+ .id(egui::Id::new("designcraft_manual_updates"))
+ .open(&mut open)
+ .collapsible(false)
+ .resizable(false)
+ .pivot(egui::Align2::CENTER_CENTER)
+ .default_pos(ctx.content_rect().center())
+ .show(ctx, |ui| {
+ if let Some(msg) = &app.updates_rt.status_message {
+ ui.label(msg);
+ }
+ ui.add_space(8.0);
+ if ui.button(ok_label).clicked() {
+ app.updates_rt.manual_window_open = false;
+ }
+ });
+ if !open {
+ app.updates_rt.manual_window_open = false;
+ }
+ }
+
+ // Closing the window dismisses the outcome it showed.
+ #[cfg(not(target_arch = "wasm32"))]
+ if !app.updates_rt.manual_window_open {
+ app.updates_rt.updater.dismiss_status();
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use designcraft_engine::Session;
+
+ #[test]
+ fn default_runtime_is_idle() {
+ let rt = Runtime::default();
+ assert!(!rt.manual_window_open);
+ #[cfg(not(target_arch = "wasm32"))]
+ {
+ assert!(!rt.startup_check);
+ assert!(!rt.updater.prompt_open());
+ assert_eq!(rt.updater.status(), &Status::Idle);
+ }
+ }
+
+ #[test]
+ #[cfg(not(target_arch = "wasm32"))]
+ fn startup_prompt_shows_and_closes() {
+ let mut app = DesignApp::new(Session::new(), Default::default());
+ let ctx = egui::Context::default();
+ let release = craft_update::Release {
+ tag: "v99.0.0".into(),
+ version: craft_update::parse_version("99.0.0"),
+ name: "DesignCraft 99.0.0".into(),
+ html_url: "https://github.com/storytold/designcraft/releases/tag/v99.0.0".into(),
+ ..Default::default()
+ };
+ app.updates_rt.updater.offer(release);
+
+ let mut output = ctx.run_ui(egui::RawInput::default(), |ui| {
+ show(&mut app, ui.ctx());
+ });
+ output.textures_delta.clear();
+ assert!(app.updates_rt.updater.prompt_open());
+ }
+}