Added setting for fade in/out settings tabs
This commit is contained in:
1 parent
2deea1d69f
commit
0f442e84e5
1 file changed
+170
-16
+170
-16
@@ -5,7 +5,8 @@ use iced::widget::{
|
||||
stack, text, text_input, toggler, tooltip,
|
||||
};
|
||||
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)]
|
||||
mod terminal;
|
||||
@@ -40,7 +41,7 @@ fn main() -> iced::Result {
|
||||
}
|
||||
|
||||
fn subscription(state: &Moorly) -> Subscription<Message> {
|
||||
keyboard::listen()
|
||||
let keyboard = keyboard::listen()
|
||||
.with((
|
||||
state.shortcuts.settings.clone(),
|
||||
state.shortcuts.quit.clone(),
|
||||
@@ -92,7 +93,17 @@ fn subscription(state: &Moorly) -> Subscription<Message> {
|
||||
}
|
||||
_ => 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)]
|
||||
@@ -589,8 +600,12 @@ struct Moorly {
|
||||
tabs: Vec<Tab>,
|
||||
active_tab: Option<Tab>,
|
||||
settings_section: SettingsSection,
|
||||
pending_settings_section: Option<SettingsSection>,
|
||||
show_start_tab: bool,
|
||||
show_start_tab_draft: bool,
|
||||
fade_settings: bool,
|
||||
fade_settings_draft: bool,
|
||||
fade_animation: Animation<bool>,
|
||||
profile_filter: String,
|
||||
profile_folders: Vec<ProfileFolder>,
|
||||
selected: Option<Selection>,
|
||||
@@ -657,24 +672,39 @@ fn config_path() -> Option<PathBuf> {
|
||||
theme::config_path().map(|path| path.with_file_name("config"))
|
||||
}
|
||||
|
||||
/// Whether the Start tab opens on launch. Defaults to enabled.
|
||||
fn load_show_start_tab() -> bool {
|
||||
/// Reads a boolean setting from the config file. Defaults to enabled.
|
||||
fn load_setting(key: &str) -> bool {
|
||||
config_path()
|
||||
.and_then(|path| std::fs::read_to_string(path).ok())
|
||||
.and_then(|text| {
|
||||
let prefix = format!("{key}=");
|
||||
|
||||
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"))
|
||||
})
|
||||
.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.
|
||||
fn apply_settings(state: &mut Moorly) {
|
||||
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 {
|
||||
return;
|
||||
};
|
||||
@@ -683,12 +713,16 @@ fn save_show_start_tab(value: bool) {
|
||||
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 {
|
||||
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 {
|
||||
menu_open: false,
|
||||
@@ -703,8 +737,12 @@ impl Default for Moorly {
|
||||
},
|
||||
active_tab: show_start_tab.then_some(Tab::Start),
|
||||
settings_section: SettingsSection::default(),
|
||||
pending_settings_section: None,
|
||||
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_folders: vec![ProfileFolder::default_folder()],
|
||||
selected: None,
|
||||
@@ -734,8 +772,10 @@ enum Message {
|
||||
CloseTab(Tab),
|
||||
SelectSettingsSection(SettingsSection),
|
||||
SetShowStartTab(bool),
|
||||
SetFadeSettings(bool),
|
||||
SaveSettings,
|
||||
UndoChanges,
|
||||
Frame(Instant),
|
||||
ProfileFilterChanged(String),
|
||||
ClearProfileFilter,
|
||||
ToggleAddMenu,
|
||||
@@ -850,13 +890,41 @@ fn update(state: &mut Moorly, message: Message) -> Task<Message> {
|
||||
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::SetFadeSettings(value) => state.fade_settings_draft = value,
|
||||
Message::SaveSettings => {
|
||||
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::ClearProfileFilter => state.profile_filter.clear(),
|
||||
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.
|
||||
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![
|
||||
text("Show Start tab at startup"),
|
||||
space::horizontal(),
|
||||
@@ -2166,12 +2245,13 @@ fn settings_page(state: &Moorly) -> Element<'_, Message> {
|
||||
.padding(8);
|
||||
|
||||
let mut content = column![].spacing(12);
|
||||
if state.settings_section != SettingsSection::Appearance {
|
||||
content = content.push(text(state.settings_section.label()).size(18));
|
||||
}
|
||||
if state.settings_section == SettingsSection::General {
|
||||
content = content.push(general_panel(state));
|
||||
}
|
||||
if state.settings_section == SettingsSection::Startup {
|
||||
content = content.push(startup_panel(state));
|
||||
}
|
||||
if state.settings_section == SettingsSection::Appearance {
|
||||
content = content.push(theme_picker(state.theme));
|
||||
}
|
||||
@@ -2187,12 +2267,42 @@ fn settings_page(state: &Moorly) -> Element<'_, Message> {
|
||||
.height(Length::Fill)
|
||||
.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])
|
||||
.width(960)
|
||||
.height(Length::Fill)
|
||||
.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(
|
||||
row![
|
||||
text("There is unsaved changes"),
|
||||
@@ -2487,8 +2597,12 @@ mod tests {
|
||||
tabs: Vec::new(),
|
||||
active_tab: None,
|
||||
settings_section: super::SettingsSection::default(),
|
||||
pending_settings_section: None,
|
||||
show_start_tab: true,
|
||||
show_start_tab_draft: true,
|
||||
fade_settings: true,
|
||||
fade_settings_draft: true,
|
||||
fade_animation: super::new_fade_animation(),
|
||||
profile_filter: String::new(),
|
||||
profile_folders: vec![super::ProfileFolder::default_folder()],
|
||||
selected: None,
|
||||
@@ -2515,7 +2629,47 @@ mod tests {
|
||||
}
|
||||
|
||||
#[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 _ = update(&mut state, Message::SetShowStartTab(false));
|
||||
|
||||
Reference in new issue
Block a user