From 98f1b5fa36a41f4802b83591a360b1f08dd1f35a Mon Sep 17 00:00:00 2001 From: Putta Khunchalee Date: Mon, 18 Nov 2024 22:39:08 +0700 Subject: [PATCH] Moves Slint files to ui directory (#1114) --- gui/Cargo.toml | 26 +++-- gui/build.rs | 4 +- gui/src/main.rs | 115 +++++++--------------- gui/{slint => ui}/globals.slint | 0 gui/{slint => ui}/main.slint | 0 gui/{slint => ui}/main/actions.slint | 0 gui/{slint => ui}/main/menu.slint | 0 gui/{slint => ui}/main/menu.svg | 0 gui/{slint => ui}/main/tabs.slint | 0 gui/{slint => ui}/main/tabs/cpu.slint | 0 gui/{slint => ui}/main/tabs/display.slint | 0 gui/{slint => ui}/wizard.slint | 0 12 files changed, 55 insertions(+), 90 deletions(-) rename gui/{slint => ui}/globals.slint (100%) rename gui/{slint => ui}/main.slint (100%) rename gui/{slint => ui}/main/actions.slint (100%) rename gui/{slint => ui}/main/menu.slint (100%) rename gui/{slint => ui}/main/menu.svg (100%) rename gui/{slint => ui}/main/tabs.slint (100%) rename gui/{slint => ui}/main/tabs/cpu.slint (100%) rename gui/{slint => ui}/main/tabs/display.slint (100%) rename gui/{slint => ui}/wizard.slint (100%) diff --git a/gui/Cargo.toml b/gui/Cargo.toml index 3d059a837..fdae13785 100644 --- a/gui/Cargo.toml +++ b/gui/Cargo.toml @@ -13,12 +13,7 @@ path = "src/main.rs" required-features = ["slint"] [features] -slint = [ - "dep:slint", - "dep:clap", - "dep:rfd", - "dep:open", -] +slint = ["dep:slint", "dep:clap", "dep:rfd", "dep:open"] qt = [] [dependencies] @@ -29,7 +24,10 @@ gdbstub_arch = "0.3.1" humansize = "2.1.3" libc = "0.2.155" obconf = { path = "../src/obconf", features = ["serde", "virt"] } -obfw = { git = "https://github.com/obhq/firmware-dumper.git", features = ["read", "std"] } +obfw = { git = "https://github.com/obhq/firmware-dumper.git", features = [ + "read", + "std", +] } param = { path = "../src/param" } pkg = { path = "../src/pkg" } ciborium = "0.2.2" @@ -48,12 +46,22 @@ x86-64 = { path = "../arch/x86-64" } [dependencies.slint] git = "https://github.com/slint-ui/slint" rev = "875ca075fb5b2dfe4c3ab0a499d5759412fc1395" -features = ["compat-1-2", "std", "accessibility", "raw-window-handle-06", "backend-winit", "renderer-skia"] +features = [ + "compat-1-2", + "std", + "accessibility", + "raw-window-handle-06", + "backend-winit", + "renderer-skia", +] default-features = false optional = true [target.'cfg(not(target_os = "macos"))'.dependencies] -ash = { version = "0.38.0", features = ["linked", "std"], default-features = false } +ash = { version = "0.38.0", features = [ + "linked", + "std", +], default-features = false } [target.'cfg(windows)'.dependencies.windows-sys] version = "0.52.0" diff --git a/gui/build.rs b/gui/build.rs index fd0846963..b3c2fea99 100644 --- a/gui/build.rs +++ b/gui/build.rs @@ -19,9 +19,9 @@ fn main() { } fn build_bin() { - // Compile main + // Compile Slint. slint_build::compile_with_config( - "slint/main.slint", + "ui/main.slint", CompilerConfiguration::new().with_style(String::from("fluent-dark")), ) .unwrap(); diff --git a/gui/src/main.rs b/gui/src/main.rs index 3fea9cb12..7daaa2f91 100644 --- a/gui/src/main.rs +++ b/gui/src/main.rs @@ -1,10 +1,11 @@ +use self::ui::ErrorDialog; use args::CliArgs; use clap::Parser; use debug::DebugServer; use graphics::{GraphicsApi, PhysicalDevice}; use slint::{ComponentHandle, Global, ModelExt, ModelRc, SharedString, VecModel}; use std::path::Path; -use std::process::{ExitCode, Termination}; +use std::process::ExitCode; use thiserror::Error; mod args; @@ -22,39 +23,21 @@ mod system; mod ui; mod vmm; -fn main() -> AppExit { - let res = run().inspect_err(|e| { - ui::ErrorDialog::new() - .and_then(|error_dialog| { - error_dialog.set_message(SharedString::from(format!( - "Error running application: {}", - full_error_reason(e) - ))); - - error_dialog.run() - }) - .unwrap(); - }); - - AppExit::from(res) +fn main() -> ExitCode { + match run() { + Ok(_) => ExitCode::SUCCESS, + Err(e) => { + display_error(e); + ExitCode::FAILURE + } + } } fn run() -> Result<(), ApplicationError> { let args = CliArgs::try_parse().map_err(ApplicationError::ParseArgs)?; #[cfg(unix)] - if let Err(e) = rlim::set_rlimit_nofile() { - ui::ErrorDialog::new() - .and_then(|error_dialog| { - error_dialog.set_message(SharedString::from(format!( - "Error setting rlimit: {}", - full_error_reason(e) - ))); - - error_dialog.run() - }) - .unwrap(); - } + rlim::set_rlimit_nofile().map_err(ApplicationError::FdLimit)?; // TODO: check if already configured and skip wizard run_wizard().map_err(ApplicationError::RunWizard)?; @@ -107,6 +90,25 @@ fn run_main_app() -> Result<(), ApplicationError> { Ok(()) } +fn display_error(e: impl std::error::Error) { + use std::fmt::Write; + + // Get full message. + let mut msg = e.to_string(); + let mut src = e.source(); + + while let Some(e) = src { + write!(&mut msg, " -> {e}").unwrap(); + src = e.source(); + } + + // Show error window. + let win = ErrorDialog::new().unwrap(); + + win.set_message(format!("An unexpected error has occurred: {msg}.").into()); + win.run().unwrap(); +} + fn setup_global_callbacks<'a, T>(component: &'a T) where ui::Globals<'a>: Global<'a, T>, @@ -130,16 +132,7 @@ where let url = url.as_str(); if let Err(e) = open::that(url) { - ui::ErrorDialog::new() - .and_then(|error_dialog| { - error_dialog.set_message(SharedString::from(format!( - "Error opening {url}: {}", - full_error_reason(e) - ))); - - error_dialog.show() - }) - .unwrap(); + // TODO: Show a modal dialog. } }); } @@ -178,51 +171,15 @@ fn run_wizard() -> Result<(), slint::PlatformError> { }) } -fn full_error_reason(e: T) -> String -where - T: std::error::Error, -{ - use std::fmt::Write; - - let mut msg = format!("{e}"); - let mut src = e.source(); - - while let Some(e) = src { - write!(&mut msg, " -> {e}").unwrap(); - src = e.source(); - } - - msg -} - -pub enum AppExit { - Ok, - Err(ApplicationError), -} - -impl Termination for AppExit { - fn report(self) -> ExitCode { - match self { - AppExit::Ok => ExitCode::SUCCESS, - AppExit::Err(e) => ExitCode::FAILURE, - } - } -} - -impl From> for AppExit { - fn from(v: Result<(), ApplicationError>) -> Self { - match v { - Ok(_) => AppExit::Ok, - Err(e) => AppExit::Err(e), - } - } -} - #[derive(Debug, Error)] -pub enum ApplicationError { +enum ApplicationError { #[error(transparent)] ParseArgs(clap::Error), + #[cfg(unix)] + #[error("couldn't increase file descriptor limit")] + FdLimit(#[source] self::rlim::RlimitError), + #[error("failed to run wizard")] RunWizard(#[source] slint::PlatformError), diff --git a/gui/slint/globals.slint b/gui/ui/globals.slint similarity index 100% rename from gui/slint/globals.slint rename to gui/ui/globals.slint diff --git a/gui/slint/main.slint b/gui/ui/main.slint similarity index 100% rename from gui/slint/main.slint rename to gui/ui/main.slint diff --git a/gui/slint/main/actions.slint b/gui/ui/main/actions.slint similarity index 100% rename from gui/slint/main/actions.slint rename to gui/ui/main/actions.slint diff --git a/gui/slint/main/menu.slint b/gui/ui/main/menu.slint similarity index 100% rename from gui/slint/main/menu.slint rename to gui/ui/main/menu.slint diff --git a/gui/slint/main/menu.svg b/gui/ui/main/menu.svg similarity index 100% rename from gui/slint/main/menu.svg rename to gui/ui/main/menu.svg diff --git a/gui/slint/main/tabs.slint b/gui/ui/main/tabs.slint similarity index 100% rename from gui/slint/main/tabs.slint rename to gui/ui/main/tabs.slint diff --git a/gui/slint/main/tabs/cpu.slint b/gui/ui/main/tabs/cpu.slint similarity index 100% rename from gui/slint/main/tabs/cpu.slint rename to gui/ui/main/tabs/cpu.slint diff --git a/gui/slint/main/tabs/display.slint b/gui/ui/main/tabs/display.slint similarity index 100% rename from gui/slint/main/tabs/display.slint rename to gui/ui/main/tabs/display.slint diff --git a/gui/slint/wizard.slint b/gui/ui/wizard.slint similarity index 100% rename from gui/slint/wizard.slint rename to gui/ui/wizard.slint