Added setting for fade in/out settings tabs
Moorly CI / linux-build (push) Successful in 3m28s
Moorly CI / windows-build (push) Successful in 3m30s

This commit is contained in:
Squid committed 2026-11-04 22:10:46 +01:00
1 parent 2deea1d69f
commit 0f442e84e5
1 file changed
+170 -16
+170 -16
View File
@@ -5,7 +5,8 @@ use iced::widget::{
stack, text, text_input, toggler, tooltip, stack, text, text_input, toggler, tooltip,
}; };
use iced::widget::{Id, operation}; use iced::widget::{Id, operation};
use iced::{Alignment, Element, Length, Padding, Subscription, Task, keyboard, mouse}; use iced::time::{Duration, Instant};
use iced::{Alignment, Animation, Element, Length, Padding, Subscription, Task, keyboard, mouse, window};
#[allow(dead_code)] #[allow(dead_code)]
mod terminal; mod terminal;
@@ -40,7 +41,7 @@ fn main() -> iced::Result {
} }
fn subscription(state: &Moorly) -> Subscription<Message> { fn subscription(state: &Moorly) -> Subscription<Message> {
keyboard::listen() let keyboard = keyboard::listen()
.with(( .with((
state.shortcuts.settings.clone(), state.shortcuts.settings.clone(),
state.shortcuts.quit.clone(), state.shortcuts.quit.clone(),
@@ -92,7 +93,17 @@ fn subscription(state: &Moorly) -> Subscription<Message> {
} }
_ => None, _ => None,
} }
}) });
// Keep frames coming until the fade-out has been swapped for the new
// section; otherwise the last frame can be missed right as it completes.
if state.fade_animation.is_animating(Instant::now())
|| state.pending_settings_section.is_some()
{
Subscription::batch([keyboard, window::frames().map(Message::Frame)])
} else {
keyboard
}
} }
#[derive(Debug, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Clone, PartialEq, Eq, Hash)]
@@ -589,8 +600,12 @@ struct Moorly {
tabs: Vec<Tab>, tabs: Vec<Tab>,
active_tab: Option<Tab>, active_tab: Option<Tab>,
settings_section: SettingsSection, settings_section: SettingsSection,
pending_settings_section: Option<SettingsSection>,
show_start_tab: bool, show_start_tab: bool,
show_start_tab_draft: bool, show_start_tab_draft: bool,
fade_settings: bool,
fade_settings_draft: bool,
fade_animation: Animation<bool>,
profile_filter: String, profile_filter: String,
profile_folders: Vec<ProfileFolder>, profile_folders: Vec<ProfileFolder>,
selected: Option<Selection>, selected: Option<Selection>,
@@ -657,24 +672,39 @@ fn config_path() -> Option<PathBuf> {
theme::config_path().map(|path| path.with_file_name("config")) theme::config_path().map(|path| path.with_file_name("config"))
} }
/// Whether the Start tab opens on launch. Defaults to enabled. /// Reads a boolean setting from the config file. Defaults to enabled.
fn load_show_start_tab() -> bool { fn load_setting(key: &str) -> bool {
config_path() config_path()
.and_then(|path| std::fs::read_to_string(path).ok()) .and_then(|path| std::fs::read_to_string(path).ok())
.and_then(|text| { .and_then(|text| {
let prefix = format!("{key}=");
text.lines() text.lines()
.find_map(|line| line.strip_prefix("show_start_tab=")) .find_map(|line| line.strip_prefix(&prefix))
.map(|value| value.trim().eq_ignore_ascii_case("true")) .map(|value| value.trim().eq_ignore_ascii_case("true"))
}) })
.unwrap_or(true) .unwrap_or(true)
} }
/// 0.5s total: 250ms fading out plus 250ms fading in.
fn new_fade_animation() -> Animation<bool> {
Animation::new(true).duration(Duration::from_millis(250))
}
/// Commits the draft settings to the running state. /// Commits the draft settings to the running state.
fn apply_settings(state: &mut Moorly) { fn apply_settings(state: &mut Moorly) {
state.show_start_tab = state.show_start_tab_draft; state.show_start_tab = state.show_start_tab_draft;
state.fade_settings = state.fade_settings_draft;
if !state.fade_settings {
if let Some(pending) = state.pending_settings_section.take() {
state.settings_section = pending;
}
state.fade_animation = new_fade_animation();
}
} }
fn save_show_start_tab(value: bool) { fn save_settings(show_start_tab: bool, fade_settings: bool) {
let Some(path) = config_path() else { let Some(path) = config_path() else {
return; return;
}; };
@@ -683,12 +713,16 @@ fn save_show_start_tab(value: bool) {
let _ = std::fs::create_dir_all(parent); let _ = std::fs::create_dir_all(parent);
} }
let _ = std::fs::write(path, format!("show_start_tab={value}\n")); let _ = std::fs::write(
path,
format!("show_start_tab={show_start_tab}\nfade_settings={fade_settings}\n"),
);
} }
impl Default for Moorly { impl Default for Moorly {
fn default() -> Self { fn default() -> Self {
let show_start_tab = load_show_start_tab(); let show_start_tab = load_setting("show_start_tab");
let fade_settings = load_setting("fade_settings");
Self { Self {
menu_open: false, menu_open: false,
@@ -703,8 +737,12 @@ impl Default for Moorly {
}, },
active_tab: show_start_tab.then_some(Tab::Start), active_tab: show_start_tab.then_some(Tab::Start),
settings_section: SettingsSection::default(), settings_section: SettingsSection::default(),
pending_settings_section: None,
show_start_tab, show_start_tab,
show_start_tab_draft: show_start_tab, show_start_tab_draft: show_start_tab,
fade_settings,
fade_settings_draft: fade_settings,
fade_animation: new_fade_animation(),
profile_filter: String::new(), profile_filter: String::new(),
profile_folders: vec![ProfileFolder::default_folder()], profile_folders: vec![ProfileFolder::default_folder()],
selected: None, selected: None,
@@ -734,8 +772,10 @@ enum Message {
CloseTab(Tab), CloseTab(Tab),
SelectSettingsSection(SettingsSection), SelectSettingsSection(SettingsSection),
SetShowStartTab(bool), SetShowStartTab(bool),
SetFadeSettings(bool),
SaveSettings, SaveSettings,
UndoChanges, UndoChanges,
Frame(Instant),
ProfileFilterChanged(String), ProfileFilterChanged(String),
ClearProfileFilter, ClearProfileFilter,
ToggleAddMenu, ToggleAddMenu,
@@ -850,13 +890,41 @@ fn update(state: &mut Moorly, message: Message) -> Task<Message> {
return reveal_active_tab(state); return reveal_active_tab(state);
} }
} }
Message::SelectSettingsSection(section) => state.settings_section = section, Message::SelectSettingsSection(section) => {
if section == state.settings_section && state.pending_settings_section.is_none() {
// Already showing this section.
} else if !state.fade_settings {
state.settings_section = section;
state.pending_settings_section = None;
state.fade_animation = new_fade_animation();
} else if section == state.settings_section {
// Clicking the current section cancels a fade to another one.
state.pending_settings_section = None;
state.fade_animation.go_mut(true, Instant::now());
} else {
state.pending_settings_section = Some(section);
state.fade_animation.go_mut(false, Instant::now());
}
}
Message::SetShowStartTab(value) => state.show_start_tab_draft = value, Message::SetShowStartTab(value) => state.show_start_tab_draft = value,
Message::SetFadeSettings(value) => state.fade_settings_draft = value,
Message::SaveSettings => { Message::SaveSettings => {
apply_settings(state); apply_settings(state);
save_show_start_tab(state.show_start_tab); save_settings(state.show_start_tab, state.fade_settings);
}
Message::UndoChanges => {
state.show_start_tab_draft = state.show_start_tab;
state.fade_settings_draft = state.fade_settings;
}
Message::Frame(now) => {
if !state.fade_animation.is_animating(now)
&& !state.fade_animation.value()
&& let Some(pending) = state.pending_settings_section.take()
{
state.settings_section = pending;
state.fade_animation.go_mut(true, now);
}
} }
Message::UndoChanges => state.show_start_tab_draft = state.show_start_tab,
Message::ProfileFilterChanged(value) => state.profile_filter = value, Message::ProfileFilterChanged(value) => state.profile_filter = value,
Message::ClearProfileFilter => state.profile_filter.clear(), Message::ClearProfileFilter => state.profile_filter.clear(),
Message::ToggleAddMenu => state.add_menu_open = !state.add_menu_open, Message::ToggleAddMenu => state.add_menu_open = !state.add_menu_open,
@@ -2134,6 +2202,17 @@ fn start_page(state: &Moorly) -> Element<'_, Message> {
/// General settings that are edited as a draft until saved. /// General settings that are edited as a draft until saved.
fn general_panel(state: &Moorly) -> Element<'_, Message> { fn general_panel(state: &Moorly) -> Element<'_, Message> {
row![
text("Fade In/Fade out animation"),
space::horizontal(),
toggler(state.fade_settings_draft).on_toggle(Message::SetFadeSettings),
]
.align_y(Alignment::Center)
.into()
}
/// Startup settings that are edited as a draft until saved.
fn startup_panel(state: &Moorly) -> Element<'_, Message> {
row![ row![
text("Show Start tab at startup"), text("Show Start tab at startup"),
space::horizontal(), space::horizontal(),
@@ -2166,12 +2245,13 @@ fn settings_page(state: &Moorly) -> Element<'_, Message> {
.padding(8); .padding(8);
let mut content = column![].spacing(12); let mut content = column![].spacing(12);
if state.settings_section != SettingsSection::Appearance {
content = content.push(text(state.settings_section.label()).size(18)); content = content.push(text(state.settings_section.label()).size(18));
}
if state.settings_section == SettingsSection::General { if state.settings_section == SettingsSection::General {
content = content.push(general_panel(state)); content = content.push(general_panel(state));
} }
if state.settings_section == SettingsSection::Startup {
content = content.push(startup_panel(state));
}
if state.settings_section == SettingsSection::Appearance { if state.settings_section == SettingsSection::Appearance {
content = content.push(theme_picker(state.theme)); content = content.push(theme_picker(state.theme));
} }
@@ -2187,12 +2267,42 @@ fn settings_page(state: &Moorly) -> Element<'_, Message> {
.height(Length::Fill) .height(Length::Fill)
.padding(16); .padding(16);
// Fade the content pane to the frame background, swap sections at
// full opacity, then fade back in.
let now = Instant::now();
let fading = state.fade_animation.is_animating(now) || !state.fade_animation.value();
let fade = state.fade_animation.interpolate(0.0, 1.0, now);
let content: Element<'_, Message> = if fading {
let overlay = container(space::horizontal())
.width(Length::Fill)
.height(Length::Fill)
.style(move |theme: &iced::Theme| {
let mut color = theme.extended_palette().background.base.color;
color.a = 1.0 - fade;
container::Style {
background: Some(color.into()),
..container::Style::default()
}
});
stack![content, overlay]
.width(Length::Fill)
.height(Length::Fill)
.into()
} else {
content.into()
};
let frame = container(row![menu, rule::vertical(1), content]) let frame = container(row![menu, rule::vertical(1), content])
.width(960) .width(960)
.height(Length::Fill) .height(Length::Fill)
.style(theme::settings_frame); .style(theme::settings_frame);
let page: Element<'_, Message> = if state.show_start_tab_draft != state.show_start_tab { let dirty = state.show_start_tab_draft != state.show_start_tab
|| state.fade_settings_draft != state.fade_settings;
let page: Element<'_, Message> = if dirty {
let bar = container( let bar = container(
row![ row![
text("There is unsaved changes"), text("There is unsaved changes"),
@@ -2487,8 +2597,12 @@ mod tests {
tabs: Vec::new(), tabs: Vec::new(),
active_tab: None, active_tab: None,
settings_section: super::SettingsSection::default(), settings_section: super::SettingsSection::default(),
pending_settings_section: None,
show_start_tab: true, show_start_tab: true,
show_start_tab_draft: true, show_start_tab_draft: true,
fade_settings: true,
fade_settings_draft: true,
fade_animation: super::new_fade_animation(),
profile_filter: String::new(), profile_filter: String::new(),
profile_folders: vec![super::ProfileFolder::default_folder()], profile_folders: vec![super::ProfileFolder::default_folder()],
selected: None, selected: None,
@@ -2515,7 +2629,47 @@ mod tests {
} }
#[test] #[test]
fn general_changes_wait_for_save_or_undo() { fn fade_starts_visible_and_ends_hidden() {
let start = iced::time::Instant::now();
let mut fade = super::new_fade_animation();
assert_eq!(fade.interpolate(0.0, 1.0, start), 1.0);
fade.go_mut(false, start);
assert!(fade.interpolate(0.0, 1.0, start) > 0.99);
assert!(
fade.interpolate(
0.0,
1.0,
start + iced::time::Duration::from_millis(250)
) < 0.01
);
}
#[test]
fn section_switch_waits_for_the_fade_out() {
let mut state = state();
state.settings_section = super::SettingsSection::General;
let _ = update(
&mut state,
Message::SelectSettingsSection(super::SettingsSection::Startup),
);
assert_eq!(state.settings_section, super::SettingsSection::General);
assert_eq!(
state.pending_settings_section,
Some(super::SettingsSection::Startup)
);
assert!(state.fade_animation.is_animating(iced::time::Instant::now()));
let later = iced::time::Instant::now() + iced::time::Duration::from_secs(1);
let _ = update(&mut state, Message::Frame(later));
assert_eq!(state.settings_section, super::SettingsSection::Startup);
assert_eq!(state.pending_settings_section, None);
}
#[test]
fn startup_changes_wait_for_save_or_undo() {
let mut state = state(); let mut state = state();
let _ = update(&mut state, Message::SetShowStartTab(false)); let _ = update(&mut state, Message::SetShowStartTab(false));