Added a toaster system, and custom theme coloring for toasters
Moorly CI / windows-build (push) Successful in 2m47s
Moorly CI / linux-build (push) Successful in 3m32s

This commit is contained in:
Squid committed 2026-11-06 22:39:33 +01:00
1 parent 8d9f4584dd
commit 891bb85d92
2 files changed
+927 -19

No files matched your search

+432 -19
View File
@@ -16,6 +16,7 @@ use iced::{
#[allow(dead_code)]
mod terminal;
mod notifications;
mod theme;
const MENUBAR_HEIGHT: f32 = 40.0;
@@ -96,6 +97,14 @@ fn subscription(state: &Moorly) -> Subscription<Message> {
{
Some(Message::AddTab)
}
keyboard::Key::Character(key)
if cfg!(debug_assertions)
&& key.eq_ignore_ascii_case("n")
&& modifiers.control()
&& modifiers.shift() =>
{
Some(Message::ShowNotificationDemo)
}
keyboard::Key::Character(key)
if key.eq_ignore_ascii_case("w")
&& modifiers.control()
@@ -127,6 +136,17 @@ fn subscription(state: &Moorly) -> Subscription<Message> {
subscriptions.push(window::frames().map(Message::Frame));
}
// Timed notifications only need a coarse expiry timer; fades use frames.
if state.notifications.has_timed() {
subscriptions.push(
iced::time::every(Duration::from_millis(250)).map(Message::NotificationTick),
);
}
if state.notifications.is_animating(now) {
subscriptions.push(window::frames().map(Message::NotificationTick));
}
Subscription::batch(subscriptions)
}
@@ -641,7 +661,10 @@ struct Moorly {
advanced_open: bool,
colors: [Color; 5],
saved_colors: [Color; 5],
custom_toasts: bool,
toast_colors: [Color; 9],
open_color_picker: Option<ColorOption>,
open_toast_color_picker: Option<ToastColorOption>,
fade_animation: Animation<bool>,
hovered_tab: Option<Tab>,
tab_close_fades: HashMap<Tab, Animation<bool>>,
@@ -657,6 +680,7 @@ struct Moorly {
tab_offset: f32,
tab_viewport_width: f32,
tab_content_width: f32,
notifications: notifications::NotificationManager,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
@@ -723,6 +747,61 @@ const DEFAULT_COLORS: [Color; 5] = [
theme::TAB_TEXT,
];
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum ToastColorOption {
Background,
Text,
Title,
Border,
Info,
Success,
Warning,
Error,
Dismiss,
}
impl ToastColorOption {
const ALL: [Self; 9] = [
Self::Background,
Self::Text,
Self::Title,
Self::Border,
Self::Info,
Self::Success,
Self::Warning,
Self::Error,
Self::Dismiss,
];
fn label(self) -> &'static str {
match self {
Self::Background => "Notification background",
Self::Text => "Notification text",
Self::Title => "Notification title",
Self::Border => "Notification border",
Self::Info => "Info accent",
Self::Success => "Success accent",
Self::Warning => "Warning accent",
Self::Error => "Error accent",
Self::Dismiss => "Dismiss icon",
}
}
fn index(self) -> usize {
match self {
Self::Background => 0,
Self::Text => 1,
Self::Title => 2,
Self::Border => 3,
Self::Info => 4,
Self::Success => 5,
Self::Warning => 6,
Self::Error => 7,
Self::Dismiss => 8,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
enum ThemeChoice {
BuiltIn(theme::Preference),
@@ -768,6 +847,8 @@ struct CustomTheme {
name: String,
based_on: theme::Preference,
colors: [Color; 5],
custom_toasts: bool,
toast_colors: [Color; 9],
}
#[derive(Debug, Serialize, Deserialize)]
@@ -784,6 +865,32 @@ struct ThemeFile {
sidebar_background: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
text: Option<String>,
#[serde(default)]
toast: ToastFile,
}
#[derive(Debug, Default, Serialize, Deserialize)]
struct ToastFile {
#[serde(default)]
enabled: bool,
#[serde(skip_serializing_if = "Option::is_none")]
background: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
text: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
title: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
border: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
info: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
success: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
warning: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
error: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
dismiss: Option<String>,
}
impl ThemeFile {
@@ -792,6 +899,8 @@ impl ThemeFile {
based_on: theme::Preference,
colors: &[Color; 5],
base: &[Color; 5],
custom_toasts: bool,
toast_colors: &[Color; 9],
) -> Self {
let changed = |option: ColorOption| {
let index = option.index();
@@ -807,6 +916,7 @@ impl ThemeFile {
main_background: changed(ColorOption::MainBackground),
sidebar_background: changed(ColorOption::SidebarBackground),
text: changed(ColorOption::Text),
toast: ToastFile::from_colors(custom_toasts, toast_colors),
}
}
@@ -829,10 +939,55 @@ impl ThemeFile {
}
}
impl ToastFile {
fn from_colors(enabled: bool, colors: &[Color; 9]) -> Self {
Self {
enabled,
background: Some(color_to_hex(colors[0])),
text: Some(color_to_hex(colors[1])),
title: Some(color_to_hex(colors[2])),
border: Some(color_to_hex(colors[3])),
info: Some(color_to_hex(colors[4])),
success: Some(color_to_hex(colors[5])),
warning: Some(color_to_hex(colors[6])),
error: Some(color_to_hex(colors[7])),
dismiss: Some(color_to_hex(colors[8])),
}
}
fn colors(&self, based_on: theme::Preference) -> [Color; 9] {
let mut colors = builtin_toast_colors(based_on);
for (color, value) in colors.iter_mut().zip([
&self.background,
&self.text,
&self.title,
&self.border,
&self.info,
&self.success,
&self.warning,
&self.error,
&self.dismiss,
]) {
if let Some(parsed) = value.as_deref().and_then(color_from_hex) {
*color = parsed;
}
}
colors
}
}
fn builtin_colors(_preference: theme::Preference) -> [Color; 5] {
DEFAULT_COLORS
}
fn builtin_toast_colors(preference: theme::Preference) -> [Color; 9] {
let theme = preference.iced().unwrap_or(iced::Theme::Dark);
notifications::ToastColors::default_for(&theme).as_array()
}
/// Everything persisted in `config.toml`, grouped by settings tab.
#[derive(Debug, Clone, Serialize, Deserialize)]
struct ConfigFile {
@@ -1012,11 +1167,14 @@ fn load_custom_themes() -> Vec<CustomTheme> {
};
let colors = file.colors(based_on);
let toast_colors = file.toast.colors(based_on);
themes.push(CustomTheme {
id,
name: file.name,
based_on,
colors,
custom_toasts: file.toast.enabled,
toast_colors,
});
}
@@ -1064,12 +1222,21 @@ fn write_custom_theme(
name: &str,
based_on: theme::Preference,
colors: &[Color; 5],
custom_toasts: bool,
toast_colors: &[Color; 9],
) -> Option<()> {
let dir = themes_dir()?;
std::fs::create_dir_all(&dir).ok()?;
let base = builtin_colors(based_on);
let file = ThemeFile::from_colors(name.to_owned(), based_on, colors, &base);
let file = ThemeFile::from_colors(
name.to_owned(),
based_on,
colors,
&base,
custom_toasts,
toast_colors,
);
let text = toml::to_string_pretty(&file).ok()?;
std::fs::write(dir.join(format!("{id}.toml")), text).ok()?;
@@ -1081,11 +1248,15 @@ fn apply_theme_choice(state: &mut Moorly, choice: ThemeChoice) {
ThemeChoice::BuiltIn(preference) => {
state.theme = *preference;
state.colors = builtin_colors(*preference);
state.custom_toasts = false;
state.toast_colors = builtin_toast_colors(*preference);
}
ThemeChoice::Custom { id, .. } => {
if let Some(theme) = state.custom_themes.iter().find(|theme| theme.id == *id) {
state.theme = theme.based_on;
state.colors = theme.colors;
state.custom_toasts = theme.custom_toasts;
state.toast_colors = theme.toast_colors;
state.theme_choice = ThemeChoice::Custom {
id: theme.id.clone(),
name: theme.name.clone(),
@@ -1181,7 +1352,7 @@ fn load_config() -> ConfigFile {
fn resolve_theme(
appearance: &AppearanceConfig,
custom_themes: &[CustomTheme],
) -> (ThemeChoice, theme::Preference, [Color; 5]) {
) -> (ThemeChoice, theme::Preference, [Color; 5], bool, [Color; 9]) {
if let Some(id) = &appearance.custom_theme
&& let Some(theme) = custom_themes.iter().find(|theme| theme.id == *id)
{
@@ -1192,12 +1363,20 @@ fn resolve_theme(
},
theme.based_on,
theme.colors,
theme.custom_toasts,
theme.toast_colors,
);
}
let preference = theme::Preference::parse(&appearance.theme).unwrap_or_default();
(ThemeChoice::BuiltIn(preference), preference, builtin_colors(preference))
(
ThemeChoice::BuiltIn(preference),
preference,
builtin_colors(preference),
false,
builtin_toast_colors(preference),
)
}
/// 0.5s total: 250ms fading out plus 250ms fading in.
@@ -1247,6 +1426,19 @@ fn undo_settings(state: &mut Moorly) {
state.theme_choice = state.saved_theme_choice.clone();
state.theme = state.saved_theme;
state.colors = state.saved_colors;
let (custom_toasts, toast_colors) = match &state.saved_theme_choice {
ThemeChoice::BuiltIn(preference) => (false, builtin_toast_colors(*preference)),
ThemeChoice::Custom { id, .. } => state
.custom_themes
.iter()
.find(|theme| theme.id == *id)
.map(|theme| (theme.custom_toasts, theme.toast_colors))
.unwrap_or_else(|| (false, builtin_toast_colors(state.saved_theme))),
};
state.custom_toasts = custom_toasts;
state.toast_colors = toast_colors;
}
fn save_config(state: &Moorly) {
@@ -1301,7 +1493,8 @@ impl Default for Moorly {
fn default() -> Self {
let config = load_config();
let custom_themes = load_custom_themes();
let (theme_choice, theme, colors) = resolve_theme(&config.appearance, &custom_themes);
let (theme_choice, theme, colors, custom_toasts, toast_colors) =
resolve_theme(&config.appearance, &custom_themes);
let show_start_tab = config.startup.show_start_tab;
let fade_settings = config.general.fade_settings;
let hide_tab_close = config.appearance.hide_tab_close;
@@ -1340,7 +1533,10 @@ impl Default for Moorly {
advanced_open: false,
colors,
saved_colors: colors,
custom_toasts,
toast_colors,
open_color_picker: None,
open_toast_color_picker: None,
fade_animation: new_fade_animation(),
hovered_tab: None,
tab_close_fades: HashMap::new(),
@@ -1356,6 +1552,7 @@ impl Default for Moorly {
tab_offset: 0.0,
tab_viewport_width: 0.0,
tab_content_width: 0.0,
notifications: notifications::NotificationManager::default(),
}
}
}
@@ -1383,11 +1580,17 @@ enum Message {
SetFadeSettings(bool),
SetHideTabClose(bool),
ToggleAdvanced,
SetCustomToasts(bool),
OpenColorPicker(ColorOption),
SubmitColor(ColorOption, Color),
OpenToastColorPicker(ToastColorOption),
SubmitToastColor(ToastColorOption, Color),
CancelColorPicker,
SaveSettings,
UndoChanges,
Notification(notifications::Message),
NotificationTick(Instant),
ShowNotificationDemo,
Frame(Instant),
ProfileFilterChanged(String),
ClearProfileFilter,
@@ -1519,7 +1722,16 @@ fn update(state: &mut Moorly, message: Message) -> Task<Message> {
let id = uuid::Uuid::new_v4().to_string();
if write_custom_theme(&id, &name, based_on, &state.colors).is_none() {
if write_custom_theme(
&id,
&name,
based_on,
&state.colors,
state.custom_toasts,
&state.toast_colors,
)
.is_none()
{
return Task::none();
}
@@ -1546,13 +1758,24 @@ fn update(state: &mut Moorly, message: Message) -> Task<Message> {
{
let based_on = theme.based_on;
if write_custom_theme(id, name, based_on, &state.colors).is_some() {
if write_custom_theme(
id,
name,
based_on,
&state.colors,
state.custom_toasts,
&state.toast_colors,
)
.is_some()
{
if let Some(theme) = state
.custom_themes
.iter_mut()
.find(|theme| theme.id == *id)
{
theme.colors = state.colors;
theme.custom_toasts = state.custom_toasts;
theme.toast_colors = state.toast_colors;
}
if matches!(
@@ -1572,6 +1795,8 @@ fn update(state: &mut Moorly, message: Message) -> Task<Message> {
{
state.theme = theme.based_on;
state.colors = theme.colors;
state.custom_toasts = theme.custom_toasts;
state.toast_colors = theme.toast_colors;
if matches!(
&state.saved_theme_choice,
@@ -1590,15 +1815,20 @@ fn update(state: &mut Moorly, message: Message) -> Task<Message> {
ThemeChoice::Custom { id, .. } => Some(id.clone()),
ThemeChoice::BuiltIn(_) => None,
};
let old_theme_colors = selected_id.as_ref().and_then(|id| {
let old_theme = selected_id.as_ref().and_then(|id| {
state
.custom_themes
.iter()
.find(|theme| theme.id == *id)
.map(|theme| theme.colors)
.cloned()
});
let had_no_edits = old_theme_colors
.map(|colors| colors == state.colors)
let had_no_edits = old_theme
.as_ref()
.map(|theme| {
theme.colors == state.colors
&& theme.custom_toasts == state.custom_toasts
&& theme.toast_colors == state.toast_colors
})
.unwrap_or(true);
state.custom_themes = load_custom_themes();
@@ -1614,6 +1844,8 @@ fn update(state: &mut Moorly, message: Message) -> Task<Message> {
if had_no_edits {
state.theme = theme.based_on;
state.colors = theme.colors;
state.custom_toasts = theme.custom_toasts;
state.toast_colors = theme.toast_colors;
}
if matches!(
@@ -1626,6 +1858,8 @@ fn update(state: &mut Moorly, message: Message) -> Task<Message> {
} else {
state.theme_choice = ThemeChoice::BuiltIn(state.theme);
state.colors = builtin_colors(state.theme);
state.custom_toasts = false;
state.toast_colors = builtin_toast_colors(state.theme);
if matches!(
&state.saved_theme_choice,
@@ -1677,17 +1911,71 @@ fn update(state: &mut Moorly, message: Message) -> Task<Message> {
Message::SetFadeSettings(value) => state.fade_settings_draft = value,
Message::SetHideTabClose(value) => state.hide_tab_close_draft = value,
Message::ToggleAdvanced => state.advanced_open = !state.advanced_open,
Message::OpenColorPicker(option) => state.open_color_picker = Some(option),
Message::SetCustomToasts(value) => {
state.custom_toasts = value;
if !value {
state.open_toast_color_picker = None;
}
}
Message::OpenColorPicker(option) => {
state.open_color_picker = Some(option);
state.open_toast_color_picker = None;
}
Message::SubmitColor(option, color) => {
state.colors[option.index()] = color;
state.open_color_picker = None;
}
Message::CancelColorPicker => state.open_color_picker = None,
Message::OpenToastColorPicker(option) => {
state.open_toast_color_picker = Some(option);
state.open_color_picker = None;
}
Message::SubmitToastColor(option, color) => {
state.toast_colors[option.index()] = color;
state.open_toast_color_picker = None;
}
Message::CancelColorPicker => {
state.open_color_picker = None;
state.open_toast_color_picker = None;
}
Message::SaveSettings => {
apply_settings(state);
save_config(state);
}
Message::UndoChanges => undo_settings(state),
Message::Notification(action) => match action {
notifications::Message::Dismiss(id) => state.notifications.dismiss(id),
},
Message::NotificationTick(now) => state.notifications.remove_expired(now),
Message::ShowNotificationDemo => {
use notifications::{Notification, NotificationKind};
state.notifications.add(
Notification::new(NotificationKind::Info, "This is a timed info notification.")
.title("Info"),
);
state.notifications.add(
Notification::new(NotificationKind::Success, "The operation completed successfully.")
.title("Success")
.timed(Duration::from_secs(8)),
);
state.notifications.add(
Notification::new(
NotificationKind::Warning,
"This warning stays until you dismiss it.",
)
.title("Persistent warning")
.persistent(),
);
state.notifications.add(
Notification::new(
NotificationKind::Error,
"This error is persistent as well.\nUse the close button to dismiss it.",
)
.title("Persistent error")
.persistent(),
);
}
Message::Frame(now) => {
if !state.fade_animation.is_animating(now)
&& !state.fade_animation.value()
@@ -2374,6 +2662,22 @@ fn advanced_panel(state: &Moorly) -> Element<'_, Message> {
rows = rows.push(color_row(state, option));
}
rows = rows.push(rule::horizontal(1));
rows = rows.push(
row![
text("Custom toaster colors"),
space::horizontal(),
toggler(state.custom_toasts).on_toggle(Message::SetCustomToasts),
]
.align_y(Alignment::Center),
);
if state.custom_toasts {
for option in ToastColorOption::ALL {
rows = rows.push(toast_color_row(state, option));
}
}
let actions = match &state.theme_choice {
ThemeChoice::BuiltIn(_) => row![
space::horizontal(),
@@ -2412,10 +2716,35 @@ fn advanced_panel(state: &Moorly) -> Element<'_, Message> {
.into()
}
/// One color option: label on the left, clickable swatch on the right.
/// One theme color option: label on the left, clickable swatch on the right.
fn color_row(state: &Moorly, option: ColorOption) -> Element<'_, Message> {
let color = state.colors[option.index()];
color_swatch_row(
option.label(),
state.colors[option.index()],
state.open_color_picker == Some(option),
Message::OpenColorPicker(option),
move |color| Message::SubmitColor(option, color),
)
}
/// One custom-toast color option, using the same swatch control as the theme.
fn toast_color_row(state: &Moorly, option: ToastColorOption) -> Element<'_, Message> {
color_swatch_row(
option.label(),
state.toast_colors[option.index()],
state.open_toast_color_picker == Some(option),
Message::OpenToastColorPicker(option),
move |color| Message::SubmitToastColor(option, color),
)
}
fn color_swatch_row<'a>(
label: &'static str,
color: Color,
open: bool,
on_open: Message,
on_submit: impl Fn(Color) -> Message + 'static,
) -> Element<'a, Message> {
let swatch = container(space::horizontal())
.width(24)
.height(24)
@@ -2430,19 +2759,19 @@ fn color_row(state: &Moorly, option: ColorOption) -> Element<'_, Message> {
});
let button = button(swatch)
.on_press(Message::OpenColorPicker(option))
.on_press(on_open)
.padding(3)
.style(button::text);
let picker = iced_aw::color_picker(
state.open_color_picker == Some(option),
open,
color,
button,
Message::CancelColorPicker,
move |color| Message::SubmitColor(option, color),
on_submit,
);
row![text(option.label()), space::horizontal(), picker]
row![text(label), space::horizontal(), picker]
.align_y(Alignment::Center)
.into()
}
@@ -3563,7 +3892,7 @@ fn tab_scroll_limits(state: &Moorly) -> (bool, bool) {
(can_scroll_left, can_scroll_right)
}
fn view(state: &Moorly) -> Element<'_, Message> {
fn main_view(state: &Moorly) -> Element<'_, Message> {
// Tab strip: size tabs to the strip's current width so as many as
// possible fit before scrolling starts.
let tabs = state.tabs.clone();
@@ -3784,6 +4113,30 @@ fn view(state: &Moorly) -> Element<'_, Message> {
.into()
}
/// The application content with the notification stack rendered above it.
fn view(state: &Moorly) -> Element<'_, Message> {
let content = main_view(state);
if state.notifications.is_empty() {
return content;
}
let toasts = state
.notifications
.view(
Instant::now(),
state
.custom_toasts
.then(|| notifications::ToastColors::from_array(state.toast_colors)),
)
.map(Message::Notification);
stack![content, toasts]
.width(Length::Fill)
.height(Length::Fill)
.into()
}
#[cfg(test)]
mod tests {
use super::{Message, Moorly, Tab, theme, update};
@@ -3816,7 +4169,10 @@ mod tests {
advanced_open: false,
colors: super::DEFAULT_COLORS,
saved_colors: super::DEFAULT_COLORS,
custom_toasts: false,
toast_colors: super::builtin_toast_colors(theme::Preference::default()),
open_color_picker: None,
open_toast_color_picker: None,
fade_animation: super::new_fade_animation(),
hovered_tab: None,
tab_close_fades: Default::default(),
@@ -3832,6 +4188,7 @@ mod tests {
tab_offset: 0.0,
tab_viewport_width: 0.0,
tab_content_width: 0.0,
notifications: super::notifications::NotificationManager::default(),
}
}
@@ -3999,6 +4356,14 @@ mod tests {
assert_eq!(state.saved_shortcuts, state.shortcuts);
}
#[test]
fn notification_demo_adds_all_types() {
let mut state = state();
let _ = update(&mut state, Message::ShowNotificationDemo);
assert_eq!(state.notifications.len(), 4);
}
#[test]
fn long_titles_are_ellipsized_to_max_width() {
assert_eq!(super::fit_title("Settings"), "Settings");
@@ -4041,6 +4406,8 @@ mod tests {
theme::Preference::Dark,
&colors,
&super::DEFAULT_COLORS,
true,
&super::builtin_toast_colors(theme::Preference::Dark),
);
let text = toml::to_string(&file).unwrap();
@@ -4048,6 +4415,10 @@ mod tests {
assert!(text.contains("based_on = \"dark\""));
assert!(text.contains("accent = \"#010203\""));
assert!(!text.contains("active_tab_outline"));
assert!(text.contains("[toast]"));
assert!(text.contains("enabled = true"));
assert!(text.contains("background = \"#"));
assert!(file.toast.enabled);
let loaded = file.colors(theme::Preference::Dark);
assert_eq!(
@@ -4074,6 +4445,48 @@ mod tests {
assert_eq!(state.open_color_picker, None);
}
#[test]
fn toaster_color_picker_updates_only_toast_colors() {
let mut state = state();
let option = super::ToastColorOption::Info;
let color = iced::Color::from_rgb(0.4, 0.5, 0.6);
let _ = update(&mut state, Message::SetCustomToasts(true));
let _ = update(&mut state, Message::OpenToastColorPicker(option));
assert_eq!(state.open_toast_color_picker, Some(option));
let _ = update(&mut state, Message::SubmitToastColor(option, color));
assert_eq!(state.toast_colors[option.index()], color);
assert_eq!(state.open_toast_color_picker, None);
assert_eq!(state.colors, super::DEFAULT_COLORS);
let _ = update(&mut state, Message::SetCustomToasts(false));
assert!(!state.custom_toasts);
}
#[test]
fn theme_file_round_trips_toast_settings() {
let mut toast = super::builtin_toast_colors(theme::Preference::Dark);
toast[super::ToastColorOption::Background.index()] = iced::Color::from_rgb8(1, 2, 3);
let file = super::ThemeFile::from_colors(
"Test theme".to_owned(),
theme::Preference::Dark,
&super::DEFAULT_COLORS,
&super::DEFAULT_COLORS,
true,
&toast,
);
let text = toml::to_string_pretty(&file).unwrap();
let loaded: super::ThemeFile = toml::from_str(&text).unwrap();
assert!(loaded.toast.enabled);
assert_eq!(
loaded.toast.colors(theme::Preference::Dark)[super::ToastColorOption::Background.index()],
iced::Color::from_rgb8(1, 2, 3)
);
}
#[test]
fn tab_close_fades_in_and_out() {
let mut state = state();