diff --git a/src/main.rs b/src/main.rs index 7452838..ec2494f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,8 @@ use std::path::PathBuf; use iced::widget::{ - button, column, container, mouse_area, pick_list, row, rule, scrollable, space, stack, text, - text_input, tooltip, + button, column, container, mouse_area, pick_list, responsive, row, rule, scrollable, space, + stack, text, text_input, toggler, tooltip, }; use iced::widget::{Id, operation}; use iced::{Alignment, Element, Length, Padding, Subscription, Task, keyboard, mouse}; @@ -589,6 +589,8 @@ struct Moorly { tabs: Vec, active_tab: Option, settings_section: SettingsSection, + show_start_tab: bool, + show_start_tab_draft: bool, profile_filter: String, profile_folders: Vec, selected: Option, @@ -605,6 +607,7 @@ struct Moorly { #[derive(Debug, Clone, Copy, PartialEq, Eq)] enum Tab { + Start, Settings, Blank(u32), } @@ -612,6 +615,7 @@ enum Tab { impl Tab { fn title(self) -> String { match self { + Self::Start => "Start".to_owned(), Self::Settings => "Settings".to_owned(), Self::Blank(number) => format!("temp {number}"), } @@ -648,17 +652,59 @@ impl SettingsSection { } } +/// Path of the general settings file, alongside the theme file. +fn config_path() -> Option { + 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 { + config_path() + .and_then(|path| std::fs::read_to_string(path).ok()) + .and_then(|text| { + text.lines() + .find_map(|line| line.strip_prefix("show_start_tab=")) + .map(|value| value.trim().eq_ignore_ascii_case("true")) + }) + .unwrap_or(true) +} + +/// Commits the draft settings to the running state. +fn apply_settings(state: &mut Moorly) { + state.show_start_tab = state.show_start_tab_draft; +} + +fn save_show_start_tab(value: bool) { + let Some(path) = config_path() else { + return; + }; + + if let Some(parent) = path.parent() { + let _ = std::fs::create_dir_all(parent); + } + + let _ = std::fs::write(path, format!("show_start_tab={value}\n")); +} + impl Default for Moorly { fn default() -> Self { + let show_start_tab = load_show_start_tab(); + Self { menu_open: false, tabs_submenu_open: false, theme: theme::load(), shortcuts: Shortcuts::default(), shortcut_capture: None, - tabs: Vec::new(), - active_tab: None, + tabs: if show_start_tab { + vec![Tab::Start] + } else { + Vec::new() + }, + active_tab: show_start_tab.then_some(Tab::Start), settings_section: SettingsSection::default(), + show_start_tab, + show_start_tab_draft: show_start_tab, profile_filter: String::new(), profile_folders: vec![ProfileFolder::default_folder()], selected: None, @@ -687,6 +733,9 @@ enum Message { SelectTab(Tab), CloseTab(Tab), SelectSettingsSection(SettingsSection), + SetShowStartTab(bool), + SaveSettings, + UndoChanges, ProfileFilterChanged(String), ClearProfileFilter, ToggleAddMenu, @@ -802,6 +851,12 @@ fn update(state: &mut Moorly, message: Message) -> Task { } } Message::SelectSettingsSection(section) => state.settings_section = section, + Message::SetShowStartTab(value) => state.show_start_tab_draft = value, + Message::SaveSettings => { + apply_settings(state); + save_show_start_tab(state.show_start_tab); + } + 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, @@ -2040,6 +2095,54 @@ fn profile_folder_row( rows.into() } +/// Landing tab: the profile listing with a shortcut to the Settings page. +fn start_page(state: &Moorly) -> Element<'_, Message> { + responsive(move |size| { + // Half the window and 60% of its height, with sensible minimums. + let width = (size.width * 0.5).max(400.0); + let list_height = (size.height * 0.6).max(300.0); + + let settings = button(text("Settings")) + .on_press(Message::SelectTab(Tab::Settings)) + .padding([6, 12]) + .style(theme::outlined_option); + + container( + container( + column![ + container(profiles_panel(state)) + .width(Length::Fill) + .height(list_height), + settings, + ] + .spacing(12) + .width(Length::Fill) + .align_x(Alignment::Center), + ) + .width(width) + .padding(12) + .style(theme::settings_frame), + ) + .width(Length::Fill) + .height(Length::Fill) + .align_x(Alignment::Center) + .padding(16) + .into() + }) + .into() +} + +/// General settings that are edited as a draft until saved. +fn general_panel(state: &Moorly) -> Element<'_, Message> { + row![ + text("Show Start tab at startup"), + space::horizontal(), + toggler(state.show_start_tab_draft).on_toggle(Message::SetShowStartTab), + ] + .align_y(Alignment::Center) + .into() +} + fn settings_page(state: &Moorly) -> Element<'_, Message> { let menu_item = |section: SettingsSection| { let label = container(text(section.label())) @@ -2066,6 +2169,9 @@ fn settings_page(state: &Moorly) -> Element<'_, Message> { 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::Appearance { content = content.push(theme_picker(state.theme)); } @@ -2086,7 +2192,49 @@ fn settings_page(state: &Moorly) -> Element<'_, Message> { .height(Length::Fill) .style(theme::settings_frame); - container(frame) + let page: Element<'_, Message> = if state.show_start_tab_draft != state.show_start_tab { + let bar = container( + row![ + text("There is unsaved changes"), + space::horizontal(), + button( + row![text("✓"), text("Save changes")] + .spacing(6) + .align_y(Alignment::Center), + ) + .on_press(Message::SaveSettings) + .padding([6, 12]) + .style(theme::positive_option), + button( + row![text("↶"), text("Undo changes")] + .spacing(6) + .align_y(Alignment::Center), + ) + .on_press(Message::UndoChanges) + .padding([6, 12]) + .style(theme::outlined_option), + ] + .spacing(8) + .align_y(Alignment::Center), + ) + .width(Length::Fill) + .padding([8, 12]) + .style(theme::settings_bar); + + let bar_layer = container(bar) + .width(Length::Fill) + .height(Length::Fill) + .align_y(Alignment::End); + + stack![frame, bar_layer] + .width(960) + .height(Length::Fill) + .into() + } else { + frame.into() + }; + + container(page) .width(Length::Fill) .height(Length::Fill) .center(Length::Fill) @@ -2198,6 +2346,7 @@ fn view(state: &Moorly) -> Element<'_, Message> { .style(theme::menubar); let main_area: Element<'_, Message> = match state.active_tab { + Some(Tab::Start) => start_page(state), Some(Tab::Settings) => settings_page(state), _ => container(column![]) .width(Length::Fill) @@ -2338,6 +2487,8 @@ mod tests { tabs: Vec::new(), active_tab: None, settings_section: super::SettingsSection::default(), + show_start_tab: true, + show_start_tab_draft: true, profile_filter: String::new(), profile_folders: vec![super::ProfileFolder::default_folder()], selected: None, @@ -2353,6 +2504,33 @@ mod tests { } } + #[test] + fn start_tab_follows_the_saved_setting() { + let state = Moorly::default(); + let expected = state.show_start_tab.then_some(Tab::Start); + + assert_eq!(state.tabs, expected.into_iter().collect::>()); + assert_eq!(state.active_tab, expected); + assert_eq!(Tab::Start.title(), "Start"); + } + + #[test] + fn general_changes_wait_for_save_or_undo() { + let mut state = state(); + + let _ = update(&mut state, Message::SetShowStartTab(false)); + assert!(state.show_start_tab); + assert!(!state.show_start_tab_draft); + + let _ = update(&mut state, Message::UndoChanges); + assert_eq!(state.show_start_tab_draft, state.show_start_tab); + + let _ = update(&mut state, Message::SetShowStartTab(false)); + super::apply_settings(&mut state); + assert!(!state.show_start_tab); + assert_eq!(state.show_start_tab_draft, state.show_start_tab); + } + #[test] fn long_titles_are_ellipsized_to_max_width() { assert_eq!(super::fit_title("Settings"), "Settings"); diff --git a/src/theme.rs b/src/theme.rs index 003d86d..4623f19 100644 --- a/src/theme.rs +++ b/src/theme.rs @@ -8,6 +8,7 @@ use std::env; use std::fs; use std::path::PathBuf; +use iced::border::Radius; use iced::widget::{button, container, text_input}; use iced::{Border, Color, Theme}; @@ -82,7 +83,7 @@ pub fn save(preference: Preference) { let _ = fs::write(path, preference.as_str()); } -fn config_path() -> Option { +pub(crate) fn config_path() -> Option { let base = if cfg!(windows) { env::var_os("APPDATA").map(PathBuf::from) } else { @@ -257,6 +258,25 @@ pub fn filter_field(theme: &Theme, status: text_input::Status) -> text_input::St style } +/// Style for the unsaved-changes bar at the bottom of the settings frame. +pub fn settings_bar(theme: &Theme) -> container::Style { + let palette = theme.extended_palette(); + + container::Style { + background: Some(palette.background.weak.color.into()), + border: Border { + color: palette.background.strong.color, + width: 1.0, + radius: Radius { + top_left: 6.0, + top_right: 6.0, + ..Radius::default() + }, + }, + ..container::Style::default() + } +} + /// Style for the settings window frame. pub fn settings_frame(theme: &Theme) -> container::Style { let palette = theme.extended_palette();