diff --git a/.gitmodules b/.gitmodules index fc31e70..c0fe5fd 100644 --- a/.gitmodules +++ b/.gitmodules @@ -10,3 +10,6 @@ [submodule "lib/luajit2-sys"] path = lib/luajit2-sys url = https://github.com/sclu1034/luajit2-sys.git +[submodule "lib/color-eyre"] + path = lib/color-eyre + url = https://github.com/sclu1034/color-eyre.git diff --git a/Cargo.lock b/Cargo.lock index 98ee3be..16bea81 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -441,8 +441,6 @@ dependencies = [ [[package]] name = "color-eyre" version = "0.6.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5a667583cca8c4f8436db8de46ea8233c42a7d9ae424a82d338f2e4675229204" dependencies = [ "backtrace", "color-spantrace", diff --git a/Cargo.toml b/Cargo.toml index 2f47e65..ab29a65 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,6 +10,12 @@ members = [ "lib/steamlocate-rs", ] +[patch.crates-io] +color-eyre = { path = "lib/color-eyre" } + +[profile.dev.package.backtrace] +opt-level = 3 + [profile.release] strip = "debuginfo" diff --git a/crates/dtmm/src/ui/window/dialog.rs b/crates/dtmm/src/ui/window/dialog.rs index 0c2bc18..06a6c02 100644 --- a/crates/dtmm/src/ui/window/dialog.rs +++ b/crates/dtmm/src/ui/window/dialog.rs @@ -1,19 +1,39 @@ -use color_eyre::Report; -use druid::widget::{CrossAxisAlignment, Flex, Label, LineBreaking, MainAxisAlignment}; -use druid::{Data, WidgetExt, WindowDesc, WindowHandle, WindowSizePolicy}; +use color_eyre::{Handler, HelpInfo, Report}; +use druid::widget::{CrossAxisAlignment, Flex, Label, LineBreaking}; +use druid::{Data, WidgetExt, WindowDesc, WindowHandle}; +use crate::ui::theme; use crate::ui::widget::button::Button; +const WINDOW_SIZE: (f64, f64) = (600., 250.); + pub fn error(err: Report, _parent: WindowHandle) -> WindowDesc { - let msg = { - let msg = format!("A critical error ocurred: {:?}", err); - if let Ok(stripped) = strip_ansi_escapes::strip(msg.as_bytes()) { - String::from_utf8_lossy(&stripped).to_string() + let (title, msg) = { + let count = err.chain().count(); + + if count == 1 { + ( + String::from("An error occurred!"), + err.root_cause().to_string(), + ) } else { - msg + let first = err.chain().next().unwrap(); + let root = err.root_cause(); + + if count > 2 { + // The second to last one, the context to the root cause + let context = err.chain().nth(count - 2).unwrap(); + + (format!("{first}!"), format!("{}: {}", context, root)) + } else { + (format!("{first}!"), root.to_string()) + } } }; + let title = Label::new(title) + .with_text_size(24.) + .with_text_color(theme::COLOR_RED_LIGHT); let text = Label::new(msg).with_line_break_mode(LineBreaking::WordWrap); let button = Button::with_label("Ok") @@ -22,19 +42,40 @@ pub fn error(err: Report, _parent: WindowHandle) -> WindowDesc { }) .align_right(); - let widget = Flex::column() - .main_axis_alignment(MainAxisAlignment::SpaceBetween) - .must_fill_main_axis(true) - .cross_axis_alignment(CrossAxisAlignment::End) - .with_child(text) - .with_spacer(20.) - .with_child(button) - .padding(10.); + let mut widget = Flex::column() + .cross_axis_alignment(CrossAxisAlignment::Start) + .with_child(title) + .with_default_spacer() + .with_child(text); + + if let Some(handler) = err.handler().downcast_ref::() { + let mut first = true; + for section in handler.sections() { + if let HelpInfo::Suggestion(data, _) = section { + if first { + widget.add_default_spacer(); + first = false; + } + + let w = Flex::row() + .cross_axis_alignment(CrossAxisAlignment::Start) + .with_child(Label::new("Suggestion:").with_text_color(theme::COLOR_GREEN_LIGHT)) + .with_spacer(2.) + .with_child( + Label::new(data.to_string()).with_line_break_mode(LineBreaking::WordWrap), + ); + + widget.add_child(w); + } + } + } + + let widget = widget.with_flex_spacer(1.).with_child(button).padding(10.); WindowDesc::new(widget) - .title("Error") + .title("Critical Error") .show_titlebar(true) - .window_size_policy(WindowSizePolicy::Content) + .with_min_size(WINDOW_SIZE) .set_always_on_top(true) .resizable(false) } diff --git a/lib/color-eyre b/lib/color-eyre new file mode 160000 index 0000000..55f8c6b --- /dev/null +++ b/lib/color-eyre @@ -0,0 +1 @@ +Subproject commit 55f8c6b7481d462e50ee4a03a43253d80d648ae2