From da752bcfd79be17e76f56769ea599310ce72a7c4 Mon Sep 17 00:00:00 2001 From: squid Date: Fri, 6 Nov 2026 20:15:07 +0100 Subject: [PATCH] Redesigned the tabs and its colors, sizes and positions --- src/main.rs | 1734 +++++++++++--------------------------------------- src/theme.rs | 119 ++-- 2 files changed, 465 insertions(+), 1388 deletions(-) diff --git a/src/main.rs b/src/main.rs index 766cc26..559e503 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,18 +1,26 @@ +use std::collections::HashMap; use std::path::PathBuf; use iced::widget::{ - button, column, container, mouse_area, pick_list, responsive, row, rule, scrollable, space, - stack, text, text_input, toggler, tooltip, + button, column, container, mouse_area, responsive, row, rule, scrollable, space, stack, text, + text_input, toggler, tooltip, }; use iced::widget::{Id, operation}; use iced::time::{Duration, Instant}; -use iced::{Alignment, Animation, Element, Length, Padding, Subscription, Task, keyboard, mouse, window}; +use iced::{ + Alignment, Animation, Border, Color, Element, Length, Padding, Subscription, Task, keyboard, + mouse, window, +}; #[allow(dead_code)] mod terminal; mod theme; -const MENUBAR_HEIGHT: f32 = 21.0; +const MENUBAR_HEIGHT: f32 = 40.0; +/// Tab width bounds and gap, used to fit as many tabs as the strip allows. +const TAB_MAX_WIDTH: f32 = 230.0; +const TAB_MIN_WIDTH: f32 = 100.0; +const TAB_SPACING: f32 = 2.0; /// Space kept clear for the hamburger/close button at the end of the menubar. const HAMBURGER_WIDTH: f32 = 32.0; /// Longest tab title shown before it gets ellipsized. @@ -27,10 +35,6 @@ const MENU_PADDING: f32 = 4.0; const TAB_SCROLL_STEP: f32 = 120.0; /// Identifies the tab-strip scrollable for programmatic scrolling. const TAB_SCROLL_ID: Id = Id::new("tab-strip"); -/// Identifies the new-folder name field so it can be focused when opened. -const FOLDER_NAME_INPUT: Id = Id::new("folder-name"); -/// Identifies the new-profile name field so it can be focused when opened. -const PROFILE_NAME_INPUT: Id = Id::new("profile-name"); fn main() -> iced::Result { iced::application(Moorly::default, update, view) @@ -96,9 +100,16 @@ fn subscription(state: &Moorly) -> Subscription { }); // 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()) + // section or the tab close icons have finished fading. + let now = Instant::now(); + let tab_close_animating = state + .tab_close_fades + .values() + .any(|animation| animation.is_animating(now)); + + if state.fade_animation.is_animating(now) || state.pending_settings_section.is_some() + || tab_close_animating { Subscription::batch([keyboard, window::frames().map(Message::Frame)]) } else { @@ -292,6 +303,7 @@ struct ShortcutCapture { } #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] +#[allow(dead_code)] // Stored for when profiles can actually start sessions. enum ProfileKind { #[default] Ssh, @@ -299,19 +311,8 @@ enum ProfileKind { PowerShell, } -impl ProfileKind { - const ALL: [Self; 3] = [Self::Ssh, Self::Terminal, Self::PowerShell]; - - fn label(self) -> &'static str { - match self { - Self::Ssh => "SSH", - Self::Terminal => "Terminal", - Self::PowerShell => "PowerShell", - } - } -} - #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] +#[allow(dead_code)] // Stored for when profiles can actually start sessions. enum SessionEnd { #[default] Auto, @@ -320,26 +321,8 @@ enum SessionEnd { Close, } -impl SessionEnd { - const ALL: [Self; 4] = [Self::Auto, Self::Keep, Self::Reconnect, Self::Close]; - - fn label(self) -> &'static str { - match self { - Self::Auto => "Auto", - Self::Keep => "Keep", - Self::Reconnect => "Reconnect", - Self::Close => "Close", - } - } -} - -impl std::fmt::Display for SessionEnd { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.write_str(self.label()) - } -} - #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] +#[allow(dead_code)] // Stored for when profiles can actually start sessions. enum AuthMethod { #[default] Password, @@ -348,28 +331,6 @@ enum AuthMethod { Interactive, } -impl AuthMethod { - const ALL: [Self; 4] = [Self::Password, Self::Key, Self::Agent, Self::Interactive]; - - fn label(self) -> &'static str { - match self { - Self::Password => "Password", - Self::Key => "Key", - Self::Agent => "Agent", - Self::Interactive => "Interactive", - } - } - - fn glyph(self) -> &'static str { - match self { - Self::Password => "🔑", - Self::Key => "🗝", - Self::Agent => "🤖", - Self::Interactive => "⌨", - } - } -} - #[derive(Debug, Clone)] #[allow(dead_code)] // Stored for when profiles can actually start sessions. struct Profile { @@ -385,188 +346,6 @@ struct Profile { private_key: String, } -/// Editable state of the "add profile" dialog. -#[derive(Debug, Clone)] -struct ProfileDraft { - name: String, - folder: usize, - kind: ProfileKind, - session_end: SessionEnd, - host: String, - port: String, - username: String, - auth: AuthMethod, - password: String, - password_visible: bool, - start_path: String, - private_key: String, -} - -impl ProfileDraft { - fn new(folder: usize) -> Self { - Self { - name: String::new(), - folder, - kind: ProfileKind::default(), - session_end: SessionEnd::default(), - host: String::new(), - port: "22".to_owned(), - username: String::new(), - auth: AuthMethod::default(), - password: String::new(), - password_visible: false, - start_path: default_start_path(), - private_key: String::new(), - } - } - - fn port_valid(&self) -> bool { - match self.kind { - ProfileKind::Ssh => self - .port - .parse::() - .is_ok_and(|port| port <= 65534), - _ => true, - } - } - - fn valid(&self) -> bool { - !self.name.trim().is_empty() && self.port_valid() - } -} - -/// An entry in the folder drop-down. -#[derive(Debug, Clone, PartialEq, Eq)] -struct FolderChoice { - index: usize, - name: String, -} - -impl std::fmt::Display for FolderChoice { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.write_str(&self.name) - } -} - -/// Which profile field a path picker is filling. -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -enum PickerTarget { - StartPath, - PrivateKey, -} - -/// In-app path browser state. In folder mode only directories are listed and -/// the current directory can be chosen; in file mode files are listed too. -#[derive(Debug, Clone)] -struct DirPicker { - path: PathBuf, - entries: Vec, - selected: Option, - files: bool, - target: PickerTarget, - error: Option, -} - -impl DirPicker { - fn open(path: PathBuf, files: bool, target: PickerTarget) -> Self { - let mut picker = Self { - path, - entries: Vec::new(), - selected: None, - files, - target, - error: None, - }; - picker.reload(); - picker - } - - fn reload(&mut self) { - match std::fs::read_dir(&self.path) { - Ok(read) => { - let mut directories = Vec::new(); - let mut files = Vec::new(); - - for entry in read.filter_map(|entry| entry.ok()) { - let path = entry.path(); - if path.is_dir() { - directories.push(path); - } else if self.files { - files.push(path); - } - } - - directories.sort(); - files.sort(); - self.entries = directories; - self.entries.extend(files); - self.selected = None; - self.error = None; - } - Err(error) => { - self.entries.clear(); - self.selected = None; - self.error = Some(error.to_string()); - } - } - } - - fn navigate(&mut self, path: PathBuf) { - self.path = path; - self.reload(); - } - - fn can_confirm(&self) -> bool { - !self.files || self.selected.is_some() - } -} - -fn home_dir() -> PathBuf { - if cfg!(windows) { - std::env::var_os("USERPROFILE") - .map(PathBuf::from) - .or_else(|| { - let drive = std::env::var_os("HOMEDRIVE")?; - let path = std::env::var_os("HOMEPATH")?; - let mut home = PathBuf::from(drive); - home.push(path); - Some(home) - }) - .unwrap_or_else(|| PathBuf::from("C:\\")) - } else { - std::env::var_os("HOME") - .map(PathBuf::from) - .unwrap_or_else(|| PathBuf::from("/")) - } -} - -fn default_start_path() -> String { - if cfg!(windows) { - std::env::var("HOMEPATH") - .unwrap_or_else(|_| home_dir().to_string_lossy().into_owned()) - } else { - "~/".to_owned() - } -} - -/// Resolves a typed path, expanding `~` to the home directory. -fn resolve_start_path(input: &str) -> PathBuf { - let input = input.trim(); - - if input.is_empty() || input == "~" { - return home_dir(); - } - - if let Some(rest) = input - .strip_prefix("~/") - .or_else(|| input.strip_prefix("~\\")) - { - return home_dir().join(rest); - } - - PathBuf::from(input) -} - /// A named group of profiles. #[derive(Debug, Clone)] struct ProfileFolder { @@ -605,22 +384,22 @@ struct Moorly { show_start_tab_draft: bool, fade_settings: bool, fade_settings_draft: bool, + hide_tab_close: bool, + hide_tab_close_draft: bool, fade_animation: Animation, + hovered_tab: Option, + tab_close_fades: HashMap>, profile_filter: String, profile_folders: Vec, selected: Option, hovered_folder: Option, - add_menu_open: bool, - folder_dialog: Option, - profile_dialog: Option, - dir_picker: Option, next_blank_tab: u32, tab_offset: f32, tab_viewport_width: f32, tab_content_width: f32, } -#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] enum Tab { Start, Settings, @@ -691,10 +470,16 @@ fn new_fade_animation() -> Animation { Animation::new(true).duration(Duration::from_millis(250)) } +/// Fade used by the tab close icon when the tab is hovered or left. +fn new_tab_close_animation() -> Animation { + Animation::new(false).duration(Duration::from_millis(750)) +} + /// 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; + state.hide_tab_close = state.hide_tab_close_draft; if !state.fade_settings { if let Some(pending) = state.pending_settings_section.take() { @@ -704,7 +489,7 @@ fn apply_settings(state: &mut Moorly) { } } -fn save_settings(show_start_tab: bool, fade_settings: bool) { +fn save_settings(show_start_tab: bool, fade_settings: bool, hide_tab_close: bool) { let Some(path) = config_path() else { return; }; @@ -715,7 +500,9 @@ fn save_settings(show_start_tab: bool, fade_settings: bool) { let _ = std::fs::write( path, - format!("show_start_tab={show_start_tab}\nfade_settings={fade_settings}\n"), + format!( + "show_start_tab={show_start_tab}\nfade_settings={fade_settings}\nhide_tab_close={hide_tab_close}\n" + ), ); } @@ -723,6 +510,7 @@ impl Default for Moorly { fn default() -> Self { let show_start_tab = load_setting("show_start_tab"); let fade_settings = load_setting("fade_settings"); + let hide_tab_close = load_setting("hide_tab_close"); Self { menu_open: false, @@ -742,15 +530,15 @@ impl Default for Moorly { show_start_tab_draft: show_start_tab, fade_settings, fade_settings_draft: fade_settings, + hide_tab_close, + hide_tab_close_draft: hide_tab_close, fade_animation: new_fade_animation(), + hovered_tab: None, + tab_close_fades: HashMap::new(), profile_filter: String::new(), profile_folders: vec![ProfileFolder::default_folder()], selected: None, hovered_folder: None, - add_menu_open: false, - folder_dialog: None, - profile_dialog: None, - dir_picker: None, next_blank_tab: 1, tab_offset: 0.0, tab_viewport_width: 0.0, @@ -773,42 +561,16 @@ enum Message { SelectSettingsSection(SettingsSection), SetShowStartTab(bool), SetFadeSettings(bool), + SetHideTabClose(bool), SaveSettings, UndoChanges, Frame(Instant), ProfileFilterChanged(String), ClearProfileFilter, - ToggleAddMenu, - CloseAddMenu, ToggleFolder(usize), HoverFolder(Option), + HoverTab { tab: Tab, hovered: bool }, SelectProfile { folder: usize, profile: usize }, - OpenProfileDialog, - ProfileNameChanged(String), - ProfileKindSelected(ProfileKind), - ProfileFolderSelected(FolderChoice), - SessionEndSelected(SessionEnd), - ProfileHostChanged(String), - ProfilePortChanged(String), - ProfileUsernameChanged(String), - AuthMethodSelected(AuthMethod), - ProfilePasswordChanged(String), - TogglePasswordVisibility, - ProfileStartPathChanged(String), - ProfilePrivateKeyChanged(String), - OpenDirectoryPicker, - OpenPrivateKeyPicker, - DirPickerEnter(PathBuf), - DirPickerSelect(PathBuf), - DirPickerUp, - ConfirmDirectoryPicker, - CancelDirectoryPicker, - ConfirmProfile, - CancelProfile, - OpenFolderDialog, - FolderNameChanged(String), - ConfirmFolder, - CancelFolder, Ignore, OpenShortcutCapture(ShortcutAction), CancelShortcut, @@ -908,13 +670,19 @@ fn update(state: &mut Moorly, message: Message) -> Task { } Message::SetShowStartTab(value) => state.show_start_tab_draft = value, Message::SetFadeSettings(value) => state.fade_settings_draft = value, + Message::SetHideTabClose(value) => state.hide_tab_close_draft = value, Message::SaveSettings => { apply_settings(state); - save_settings(state.show_start_tab, state.fade_settings); + save_settings( + state.show_start_tab, + state.fade_settings, + state.hide_tab_close, + ); } Message::UndoChanges => { state.show_start_tab_draft = state.show_start_tab; state.fade_settings_draft = state.fade_settings; + state.hide_tab_close_draft = state.hide_tab_close; } Message::Frame(now) => { if !state.fade_animation.is_animating(now) @@ -927,8 +695,6 @@ fn update(state: &mut Moorly, message: Message) -> Task { } Message::ProfileFilterChanged(value) => state.profile_filter = value, Message::ClearProfileFilter => state.profile_filter.clear(), - Message::ToggleAddMenu => state.add_menu_open = !state.add_menu_open, - Message::CloseAddMenu => state.add_menu_open = false, Message::ToggleFolder(index) => { if let Some(folder) = state.profile_folders.get_mut(index) { folder.expanded = !folder.expanded; @@ -936,213 +702,34 @@ fn update(state: &mut Moorly, message: Message) -> Task { state.selected = Some(Selection::Folder(index)); } Message::HoverFolder(folder) => state.hovered_folder = folder, + Message::HoverTab { tab, hovered } => { + let now = Instant::now(); + + if hovered { + state.hovered_tab = Some(tab); + + for (key, animation) in &mut state.tab_close_fades { + if *key != tab { + animation.go_mut(false, now); + } + } + + state + .tab_close_fades + .entry(tab) + .or_insert_with(new_tab_close_animation) + .go_mut(true, now); + } else if state.hovered_tab == Some(tab) { + state.hovered_tab = None; + + for animation in state.tab_close_fades.values_mut() { + animation.go_mut(false, now); + } + } + } Message::SelectProfile { folder, profile } => { state.selected = Some(Selection::Profile { folder, profile }); } - Message::OpenProfileDialog => { - let folder = match state.selected { - Some(Selection::Folder(index)) => index, - Some(Selection::Profile { folder, .. }) => folder, - None => 0, - }; - state.add_menu_open = false; - state.profile_dialog = Some(ProfileDraft::new(folder)); - return operation::focus(PROFILE_NAME_INPUT); - } - Message::ProfileNameChanged(value) => { - if let Some(draft) = state.profile_dialog.as_mut() { - draft.name = value.chars().take(150).collect(); - } - } - Message::ProfileKindSelected(kind) => { - if let Some(draft) = state.profile_dialog.as_mut() { - draft.kind = kind; - } - } - Message::ProfileFolderSelected(choice) => { - if let Some(draft) = state.profile_dialog.as_mut() { - draft.folder = choice.index; - } - } - Message::SessionEndSelected(session_end) => { - if let Some(draft) = state.profile_dialog.as_mut() { - draft.session_end = session_end; - } - } - Message::ProfileHostChanged(value) => { - if let Some(draft) = state.profile_dialog.as_mut() { - draft.host = value; - } - } - Message::ProfilePortChanged(value) => { - let valid = value.chars().all(|c| c.is_ascii_digit()) - && (value.is_empty() - || value.parse::().is_ok_and(|port| port <= 65534)); - - if valid { - if let Some(draft) = state.profile_dialog.as_mut() { - draft.port = value; - } - } - } - Message::ProfileUsernameChanged(value) => { - if let Some(draft) = state.profile_dialog.as_mut() { - draft.username = value; - } - } - Message::AuthMethodSelected(auth) => { - if let Some(draft) = state.profile_dialog.as_mut() { - draft.auth = auth; - } - } - Message::ProfilePasswordChanged(value) => { - if let Some(draft) = state.profile_dialog.as_mut() { - draft.password = value; - } - } - Message::TogglePasswordVisibility => { - if let Some(draft) = state.profile_dialog.as_mut() { - draft.password_visible = !draft.password_visible; - } - } - Message::ProfileStartPathChanged(value) => { - if let Some(draft) = state.profile_dialog.as_mut() { - draft.start_path = value; - } - } - Message::ProfilePrivateKeyChanged(value) => { - if let Some(draft) = state.profile_dialog.as_mut() { - draft.private_key = value; - } - } - Message::OpenDirectoryPicker => { - let path = state - .profile_dialog - .as_ref() - .map(|draft| resolve_start_path(&draft.start_path)) - .filter(|path| path.is_dir()) - .unwrap_or_else(home_dir); - - state.dir_picker = Some(DirPicker::open(path, false, PickerTarget::StartPath)); - } - Message::OpenPrivateKeyPicker => { - let typed = state - .profile_dialog - .as_ref() - .map(|draft| resolve_start_path(&draft.private_key)) - .unwrap_or_default(); - - let directory = if typed.is_dir() { - typed - } else { - typed - .parent() - .filter(|path| path.is_dir()) - .map(PathBuf::from) - .unwrap_or_else(home_dir) - }; - - state.dir_picker = Some(DirPicker::open(directory, true, PickerTarget::PrivateKey)); - } - Message::DirPickerEnter(path) => { - if let Some(picker) = state.dir_picker.as_mut() { - picker.navigate(path); - } - } - Message::DirPickerSelect(path) => { - if let Some(picker) = state.dir_picker.as_mut() { - picker.selected = Some(path); - } - } - Message::DirPickerUp => { - if let Some(picker) = state.dir_picker.as_mut() { - if let Some(parent) = picker.path.parent().map(PathBuf::from) { - picker.navigate(parent); - } - } - } - Message::ConfirmDirectoryPicker => { - if let Some(picker) = state.dir_picker.take() { - let value = match picker.target { - PickerTarget::StartPath => Some(picker.path.clone()), - PickerTarget::PrivateKey => picker.selected.clone(), - }; - - if let (Some(value), Some(draft)) = (value, state.profile_dialog.as_mut()) { - let value = value.to_string_lossy().into_owned(); - match picker.target { - PickerTarget::StartPath => draft.start_path = value, - PickerTarget::PrivateKey => draft.private_key = value, - } - } - } - } - Message::CancelDirectoryPicker => state.dir_picker = None, - Message::ConfirmProfile => { - let Some(draft) = state.profile_dialog.as_ref() else { - return Task::none(); - }; - - if !draft.valid() { - return Task::none(); - } - - let folder_index = draft.folder.min(state.profile_folders.len().saturating_sub(1)); - let Some(folder) = state.profile_folders.get_mut(folder_index) else { - return Task::none(); - }; - - folder.profiles.push(Profile { - name: draft.name.trim().to_owned(), - kind: draft.kind, - session_end: draft.session_end, - host: draft.host.trim().to_owned(), - port: draft.port.clone(), - username: draft.username.trim().to_owned(), - auth: draft.auth, - password: draft.password.clone(), - start_path: draft.start_path.clone(), - private_key: draft.private_key.clone(), - }); - folder.expanded = true; - state.selected = Some(Selection::Profile { - folder: folder_index, - profile: folder.profiles.len() - 1, - }); - state.profile_dialog = None; - } - Message::CancelProfile => state.profile_dialog = None, - Message::OpenFolderDialog => { - state.add_menu_open = false; - state.folder_dialog = Some(String::new()); - return operation::focus(FOLDER_NAME_INPUT); - } - Message::FolderNameChanged(value) => { - if let Some(draft) = state.folder_dialog.as_mut() { - *draft = value; - } - } - Message::ConfirmFolder => { - let Some(name) = state - .folder_dialog - .as_deref() - .map(str::trim) - .filter(|name| !name.is_empty()) - .map(str::to_owned) - else { - return Task::none(); - }; - - let index = state.profile_folders.len(); - state.profile_folders.push(ProfileFolder { - name, - profiles: Vec::new(), - expanded: true, - }); - state.selected = Some(Selection::Folder(index)); - state.folder_dialog = None; - } - Message::CancelFolder => state.folder_dialog = None, Message::Ignore => {} Message::OpenShortcutCapture(action) => { state.shortcut_capture = Some(ShortcutCapture { @@ -1272,17 +859,16 @@ fn tab_reveal_delta(state: &Moorly) -> Option { return None; } - // ponytail: assumes roughly equal tab widths; good enough for visibility. - let tab_width = state.tab_content_width / count; - let start = index as f32 * tab_width; + let width = tab_width(state.tab_viewport_width, state.tabs.len()) + TAB_SPACING; + let start = index as f32 * width; let visible_start = state.tab_offset; let visible_end = visible_start + state.tab_viewport_width; - if start >= visible_start && start + tab_width <= visible_end { + if start >= visible_start && start + width <= visible_end { return None; } - let target = (start - (state.tab_viewport_width - tab_width) / 2.0).max(0.0); + let target = (start - (state.tab_viewport_width - width) / 2.0).max(0.0); Some(target - state.tab_offset) } @@ -1311,7 +897,7 @@ fn hamburger(open: bool) -> iced::widget::Button<'static, Message> { .on_press(Message::ToggleMenu) .width(HAMBURGER_WIDTH) .padding([2, 8]) - .style(button::text) + .style(theme::menubar_button) } /// Shortens a title to `MAX_TAB_TITLE` characters, ellipsizing the rest. @@ -1325,26 +911,161 @@ fn fit_title(title: &str) -> String { fitted } -fn tab_view(tab: Tab, active: bool) -> Element<'static, Message> { - let title = tab.title(); +/// Width that lets the strip show as many tabs as possible, between the +/// minimum and maximum tab widths. +fn tab_width(available: f32, count: usize) -> f32 { + let count = count as f32; - let content = container( - row![ - button(text(fit_title(&title)).size(12)) - .on_press(Message::SelectTab(tab)) - .padding([1, 4]) - .style(button::text), - button(text("✕").size(10)) - .on_press(Message::CloseTab(tab)) - .padding([1, 4]) - .style(button::text), - ] - .spacing(0) - .align_y(Alignment::Center), + if count == 0.0 || available <= 0.0 { + return TAB_MAX_WIDTH; + } + + let available = available - TAB_SPACING * (count - 1.0); + (available / count).clamp(TAB_MIN_WIDTH, TAB_MAX_WIDTH) +} + +fn tab_shell( + content: Element<'static, Message>, + active: bool, + width: f32, +) -> Element<'static, Message> { + let tab = container(content) + .width(width) + .height(Length::Fill) + .align_y(Alignment::Center) + .style(move |theme| theme::tab(theme, active)); + + // Draw the active outline as three separate edges so there is no bottom + // border to blend away. + if active { + let top = container(space::horizontal()) + .width(Length::Fill) + .height(1.0) + .style(theme::tab_outline); + + let left = container(space::vertical()) + .width(1.0) + .height(Length::Fill) + .style(theme::tab_outline); + + let right = container( + container(space::vertical()) + .width(1.0) + .height(Length::Fill) + .style(theme::tab_outline), + ) + .width(Length::Fill) + .height(Length::Fill) + .align_x(Alignment::End); + + stack![tab, top, left, right].into() + } else { + tab.into() + } +} + +fn tab_view( + tab: Tab, + active: bool, + hide_close: bool, + close_opacity: f32, + width: f32, +) -> Element<'static, Message> { + let title = tab.title(); + let padding = Padding::new(10.0); + + let title_button = button( + container(text(fit_title(&title)).size(15)) + .width(Length::Fill) + .align_x(Alignment::Center), ) - .height(Length::Fill) - .align_y(Alignment::Center) - .style(move |theme| theme::tab(theme, active)); + .on_press(Message::SelectTab(tab)) + .padding(padding) + .width(Length::Fill) + .clip(true) + .style(move |theme, status| { + let mut style = button::text(theme, status); + style.text_color = if active { + theme::TAB_TEXT + } else { + theme::TAB_TEXT.scale_alpha(0.6) + }; + style + }); + + let close_icon = |padding: Padding| { + button(text("×").size(15)) + .on_press(Message::CloseTab(tab)) + .padding(padding) + .style(move |theme, status| { + let mut style = button::text(theme, status); + style.text_color = theme::TAB_TEXT; + style.text_color.a = close_opacity; + style + }) + }; + + let tab_element = if hide_close { + let tab = tab_shell(title_button.into(), active, width); + + if close_opacity <= 0.01 { + tab + } else { + let close = + container(close_icon(Padding::new(4.0))).style(move |_theme: &iced::Theme| { + let mut background = if active { + theme::TAB_ACTIVE_BG + } else { + theme::TAB_INACTIVE_BG + }; + background.a = close_opacity; + + container::Style { + background: Some(background.into()), + border: Border { + color: Color::TRANSPARENT, + width: 0.0, + radius: 4.0.into(), + }, + ..container::Style::default() + } + }); + + let overlay = container(close) + .width(Length::Fill) + .height(Length::Fill) + .align_x(Alignment::End) + .align_y(Alignment::Center) + .padding(Padding::ZERO.right(12.0)); + + stack![tab, overlay] + .width(width) + .height(Length::Fill) + .into() + } + } else { + tab_shell( + row![ + title_button, + close_icon(Padding::new(10.0)) + ] + .spacing(0) + .align_y(Alignment::Center) + .into(), + active, + width, + ) + }; + + let content = mouse_area(tab_element) + .on_enter(Message::HoverTab { + tab, + hovered: true, + }) + .on_exit(Message::HoverTab { + tab, + hovered: false, + }); tooltip( content, @@ -1503,20 +1224,6 @@ fn profiles_panel(state: &Moorly) -> Element<'_, Message> { filter = filter.push(clear); } - let add_button = button( - row![text("+").size(16), text("Add"), text("▾").size(12)] - .spacing(6) - .align_y(Alignment::Center), - ) - .on_press(Message::ToggleAddMenu) - .padding([6, 10]) - .style(theme::outlined_option); - - let filter_row = row![filter, add_button] - .spacing(12) - .align_y(Alignment::Center) - .width(Length::Fill); - let mut list = column![].width(Length::Fill); for (index, folder) in state.profile_folders.iter().enumerate() { if index > 0 { @@ -1535,555 +1242,13 @@ fn profiles_panel(state: &Moorly) -> Element<'_, Message> { .height(Length::Fill) .style(theme::settings_frame); - let base = column![filter_row, list] + column![filter, list] .spacing(12) .width(Length::Fill) - .height(Length::Fill); - - let dialog_open = state.folder_dialog.is_some() - || state.profile_dialog.is_some() - || state.dir_picker.is_some(); - - if !state.add_menu_open && !dialog_open { - return stack![base].into(); - } - - // Clicking anywhere outside the open layer dismisses it. - let backdrop = mouse_area( - container(space::horizontal()) - .width(Length::Fill) - .height(Length::Fill), - ) - .on_press(if state.dir_picker.is_some() { - Message::CancelDirectoryPicker - } else if state.folder_dialog.is_some() { - Message::CancelFolder - } else if state.profile_dialog.is_some() { - Message::CancelProfile - } else { - Message::CloseAddMenu - }); - - let mut layers = stack![base, backdrop]; - - if state.add_menu_open { - let menu = container( - column![ - button( - row![text("👤").size(14), text("Profile")] - .spacing(8) - .align_y(Alignment::Center), - ) - .on_press(Message::OpenProfileDialog) - .width(Length::Fill) - .padding([6, 10]) - .style(button::text), - button( - row![text("📁").size(14), text("Folder")] - .spacing(8) - .align_y(Alignment::Center), - ) - .on_press(Message::OpenFolderDialog) - .width(Length::Fill) - .padding([6, 10]) - .style(button::text), - ] - .width(Length::Fill), - ) - .width(120) - .padding(4) - .style(theme::menu); - - // ponytail: fixed offset under the filter row; keep in sync with its height. - let menu_layer = container(menu) - .width(Length::Fill) - .height(Length::Fill) - .align_x(Alignment::End) - .align_y(Alignment::Start) - .padding(Padding::ZERO.top(44.0)); - - layers = layers.push(menu_layer); - } - - if let Some(draft) = &state.folder_dialog { - let dialog = container( - column![ - text("New folder").size(14), - text_input("Folder name", draft) - .id(FOLDER_NAME_INPUT) - .on_input(Message::FolderNameChanged) - .on_submit(Message::ConfirmFolder) - .padding(6) - .style(theme::filter_field) - .width(Length::Fill), - row![ - space::horizontal(), - button("Cancel") - .on_press(Message::CancelFolder) - .style(theme::danger_option), - button("Add") - .on_press_maybe( - (!draft.trim().is_empty()).then_some(Message::ConfirmFolder) - ) - .style(theme::positive_option), - ] - .spacing(8), - ] - .spacing(10) - .width(Length::Fill), - ) - .width(260) - .padding(12) - .style(theme::menu); - - // Swallow clicks on the dialog surface so only the backdrop outside it - // dismisses the dialog. - let dialog = mouse_area(dialog).on_press(Message::Ignore); - - let dialog_layer = container(dialog) - .width(Length::Fill) - .height(Length::Fill) - .align_x(Alignment::Center) - .align_y(Alignment::Center); - - layers = layers.push(dialog_layer); - } - - if let Some(draft) = &state.profile_dialog { - let dialog = profile_dialog_view(draft, &state.profile_folders); - let dialog = mouse_area(dialog).on_press(Message::Ignore); - - let dialog_layer = container(dialog) - .width(Length::Fill) - .height(Length::Fill) - .align_x(Alignment::Center) - .align_y(Alignment::Center); - - layers = layers.push(dialog_layer); - } - - if let Some(picker) = &state.dir_picker { - let dialog = directory_picker_view(picker); - let dialog = mouse_area(dialog).on_press(Message::Ignore); - - let dialog_layer = container(dialog) - .width(Length::Fill) - .height(Length::Fill) - .align_x(Alignment::Center) - .align_y(Alignment::Center); - - layers = layers.push(dialog_layer); - } - - layers.into() -} - -/// Folder/file browser used by the Terminal start path and SSH private key. -fn directory_picker_view(picker: &DirPicker) -> Element<'_, Message> { - let mut entries = column![].width(Length::Fill); - - if picker.path.parent().is_some() { - entries = entries.push( - button(row![text("📁").size(14), text("..")].spacing(8)) - .on_press(Message::DirPickerUp) - .width(Length::Fill) - .padding([6, 8]) - .style(button::text), - ); - } - - for path in &picker.entries { - let is_dir = path.is_dir(); - let name = path - .file_name() - .map(|name| name.to_string_lossy().into_owned()) - .unwrap_or_else(|| path.display().to_string()); - - let glyph = if is_dir { "📁" } else { "📄" }; - let message = if is_dir { - Message::DirPickerEnter(path.clone()) - } else { - Message::DirPickerSelect(path.clone()) - }; - let selected = picker.selected.as_ref() == Some(path); - - entries = entries.push( - button( - row![text(glyph).size(14), text(name)] - .spacing(8) - .align_y(Alignment::Center), - ) - .on_press(message) - .width(Length::Fill) - .padding([6, 8]) - .style(move |theme, status| { - theme::selectable_row(theme, status, selected) - }), - ); - } - - let listing = container(scrollable(entries).width(Length::Fill).height(Length::Fill)) - .width(Length::Fill) - .height(240) - .style(theme::settings_frame); - - let title = if picker.files { - "Select private key" - } else { - "Select folder" - }; - - let mut content = column![ - text(title).size(16), - text(picker.path.display().to_string()).size(12), - listing, - ] - .spacing(10) - .width(Length::Fill); - - if let Some(selected) = &picker.selected { - content = content.push(text(selected.display().to_string()).size(12)); - } - - if let Some(error) = &picker.error { - content = content.push(text(error).size(12).color(theme::DANGER)); - } - - content = content.push( - row![ - space::horizontal(), - button("Cancel") - .on_press(Message::CancelDirectoryPicker) - .style(theme::danger_option), - button("Select") - .on_press_maybe(picker.can_confirm().then_some(Message::ConfirmDirectoryPicker)) - .style(theme::positive_option), - ] - .spacing(8), - ); - - container(content) - .width(520) - .padding(16) - .style(theme::menu) + .height(Length::Fill) .into() } -/// The add-profile dialog: details on the left, type-specific view on the right. -fn profile_dialog_view<'a>( - draft: &'a ProfileDraft, - folders: &'a [ProfileFolder], -) -> Element<'a, Message> { - let title = if draft.name.trim().is_empty() { - "New profile" - } else { - draft.name.as_str() - }; - - let choices: Vec = folders - .iter() - .enumerate() - .map(|(index, folder)| FolderChoice { - index, - name: folder.name.clone(), - }) - .collect(); - let selected_folder = choices.get(draft.folder).cloned(); - - let left = column![ - text(title).size(18), - column![ - text("Name").size(12), - text_input("Profile name", &draft.name) - .id(PROFILE_NAME_INPUT) - .on_input(Message::ProfileNameChanged) - .padding(6) - .style(theme::filter_field) - .width(Length::Fill), - ] - .spacing(4), - column![ - text("Folder").size(12), - pick_list(choices, selected_folder, Message::ProfileFolderSelected) - .width(Length::Fill), - ] - .spacing(4), - row![ - column![ - text("When a session ends").size(13), - text("Only close the tab when session is explicitly terminated").size(10), - ] - .spacing(2) - .width(Length::Fill), - pick_list( - SessionEnd::ALL, - Some(draft.session_end), - Message::SessionEndSelected, - ) - .width(110), - ] - .spacing(10) - .align_y(Alignment::Center), - ] - .spacing(14) - .width(260); - - let mut kind_picker = row![].spacing(6); - for kind in ProfileKind::ALL { - let item = button(text(kind.label())) - .on_press(Message::ProfileKindSelected(kind)) - .padding([6, 10]); - - kind_picker = kind_picker.push(if kind == draft.kind { - item.style(theme::selected_option) - } else { - item.style(theme::outlined_option) - }); - } - - let right = column![kind_picker, profile_kind_view(draft)] - .spacing(14) - .width(Length::Fill); - - let body = row![left, right] - .spacing(24) - .align_y(Alignment::Start) - .width(Length::Fill); - - let buttons = row![ - space::horizontal(), - button("Cancel") - .on_press(Message::CancelProfile) - .style(theme::danger_option), - button("Add") - .on_press_maybe(draft.valid().then_some(Message::ConfirmProfile)) - .style(theme::positive_option), - ] - .spacing(8); - - container(column![body, buttons].spacing(16).width(Length::Fill)) - .width(720) - .padding(16) - .style(theme::menu) - .into() -} - -/// Right-hand side of the profile dialog for the selected profile type. -fn profile_kind_view(draft: &ProfileDraft) -> Element<'_, Message> { - match draft.kind { - ProfileKind::Terminal => { - let field = text_input("Start path", &draft.start_path) - .on_input(Message::ProfileStartPathChanged) - .padding(Padding { - top: 6.0, - right: 40.0, - bottom: 6.0, - left: 8.0, - }) - .style(theme::filter_field) - .width(Length::Fill); - - let browse = container( - button("...") - .on_press(Message::OpenDirectoryPicker) - .padding([2, 6]) - .style(|theme, status| { - theme::selectable_row(theme, status, false) - }), - ) - .width(Length::Fill) - .height(Length::Fill) - .align_x(Alignment::End) - .align_y(Alignment::Center) - .padding(Padding::ZERO.right(6.0)); - - column![ - text("Terminal").size(18), - column![ - text("Start path").size(12), - stack![field, browse].width(Length::Fill), - ] - .spacing(4), - ] - .spacing(12) - .into() - } - ProfileKind::PowerShell => text("PowerShell").size(18).into(), - ProfileKind::Ssh => { - let host_port = row![ - column![ - text("Host").size(12), - text_input("Hostname or IP", &draft.host) - .on_input(Message::ProfileHostChanged) - .padding(6) - .style(theme::filter_field) - .width(Length::Fill), - ] - .spacing(4) - .width(292), - space::horizontal(), - column![ - text("Port").size(12), - text_input("22", &draft.port) - .on_input(Message::ProfilePortChanged) - .padding(6) - .style(theme::filter_field) - .width(Length::Fill), - ] - .spacing(4) - .width(64), - ] - .spacing(12); - - let username = column![ - text("Username").size(12), - text_input("Username", &draft.username) - .on_input(Message::ProfileUsernameChanged) - .padding(6) - .style(theme::filter_field) - .width(Length::Fill), - ] - .spacing(4) - .width(292); - - let mut auth_buttons = row![].spacing(6).width(Length::Fill); - for auth in AuthMethod::ALL { - let item = button( - column![ - text(auth.glyph()).size(18), - text(auth.label()).size(11), - ] - .spacing(2) - .align_x(Alignment::Center) - .width(Length::Fill), - ) - .on_press(Message::AuthMethodSelected(auth)) - .width(Length::Fill) - .padding([13, 4]); - - auth_buttons = auth_buttons.push(if auth == draft.auth { - item.style(theme::selected_option) - } else { - item.style(theme::outlined_option) - }); - } - - let mut content = column![ - text("SSH Connection").size(18), - host_port, - username, - auth_buttons, - ] - .spacing(12); - - if draft.auth == AuthMethod::Password { - content = content.push(password_field(draft)); - } - - if draft.auth == AuthMethod::Key { - content = content.push(private_key_field(draft)); - } - - content.into() - } - } -} - -/// Password label and masked field with an eye toggle. -fn password_field(draft: &ProfileDraft) -> Element<'_, Message> { - let field = text_input("Password", &draft.password) - .secure(!draft.password_visible) - .on_input(Message::ProfilePasswordChanged) - .padding(Padding { - top: 6.0, - right: 32.0, - bottom: 6.0, - left: 8.0, - }) - .style(theme::filter_field) - .width(Length::Fill); - - let eye_icon: Element<'_, Message> = if draft.password_visible { - text("👁").size(16).into() - } else { - // Eye with a line across it, rather than an ambiguous curve. - stack![ - text("👁").size(16), - container( - rule::horizontal(1).style(|theme: &iced::Theme| iced::widget::rule::Style { - color: theme.extended_palette().background.base.text, - radius: 0.0.into(), - fill_mode: iced::widget::rule::FillMode::Full, - snap: true, - }), - ) - .width(Length::Fill) - .height(Length::Fill) - .align_y(Alignment::Center), - ] - .into() - }; - - let eye_button = button(eye_icon) - .on_press(Message::TogglePasswordVisibility) - .padding([2, 4]); - - let eye_button = if draft.password_visible { - eye_button.style(theme::revealed_eye) - } else { - eye_button.style(button::text) - }; - - let eye = container(eye_button) - .width(Length::Fill) - .height(Length::Fill) - .align_x(Alignment::End) - .align_y(Alignment::Center) - .padding(Padding::ZERO.right(6.0)); - - column![ - text("Password").size(12), - stack![field, eye].width(Length::Fill), - ] - .spacing(4) - .into() -} - -/// Private key label and file field with a browse button. -fn private_key_field(draft: &ProfileDraft) -> Element<'_, Message> { - let field = text_input("Private key", &draft.private_key) - .on_input(Message::ProfilePrivateKeyChanged) - .padding(Padding { - top: 6.0, - right: 40.0, - bottom: 6.0, - left: 8.0, - }) - .style(theme::filter_field) - .width(Length::Fill); - - let browse = container( - button("...") - .on_press(Message::OpenPrivateKeyPicker) - .padding([2, 6]) - .style(|theme, status| { - theme::selectable_row(theme, status, false) - }), - ) - .width(Length::Fill) - .height(Length::Fill) - .align_x(Alignment::End) - .align_y(Alignment::Center) - .padding(Padding::ZERO.right(6.0)); - - column![ - text("Private Key").size(12), - stack![field, browse].width(Length::Fill), - ] - .spacing(4) - .into() -} - /// One folder heading followed by its profiles. Clicking the heading toggles /// the profile list; clicking a row selects it. fn profile_folder_row( @@ -2222,6 +1387,21 @@ fn startup_panel(state: &Moorly) -> Element<'_, Message> { .into() } +/// Appearance settings that are edited as a draft until saved. +fn appearance_panel(state: &Moorly) -> Element<'_, Message> { + column![ + theme_picker(state.theme), + row![ + text("Hide tab close icon"), + space::horizontal(), + toggler(state.hide_tab_close_draft).on_toggle(Message::SetHideTabClose), + ] + .align_y(Alignment::Center), + ] + .spacing(12) + .into() +} + fn settings_page(state: &Moorly) -> Element<'_, Message> { let menu_item = |section: SettingsSection| { let label = container(text(section.label())) @@ -2233,7 +1413,7 @@ fn settings_page(state: &Moorly) -> Element<'_, Message> { .padding([6, 10]); if section == state.settings_section { - item.style(theme::selected_option) + item.style(theme::settings_active) } else { item.style(button::text) } @@ -2253,7 +1433,7 @@ fn settings_page(state: &Moorly) -> Element<'_, Message> { content = content.push(startup_panel(state)); } if state.settings_section == SettingsSection::Appearance { - content = content.push(theme_picker(state.theme)); + content = content.push(appearance_panel(state)); } if state.settings_section == SettingsSection::Shortcuts { content = content.push(shortcuts_panel(state)); @@ -2276,8 +1456,8 @@ fn settings_page(state: &Moorly) -> Element<'_, Message> { 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; + .style(move |_| { + let mut color = theme::TAB_ACTIVE_BG; color.a = 1.0 - fade; container::Style { @@ -2294,13 +1474,13 @@ fn settings_page(state: &Moorly) -> Element<'_, Message> { content.into() }; - let frame = container(row![menu, rule::vertical(1), content]) - .width(960) - .height(Length::Fill) - .style(theme::settings_frame); + let frame = row![menu, content] + .width(Length::Fill) + .height(Length::Fill); let dirty = state.show_start_tab_draft != state.show_start_tab - || state.fade_settings_draft != state.fade_settings; + || state.fade_settings_draft != state.fade_settings + || state.hide_tab_close_draft != state.hide_tab_close; let page: Element<'_, Message> = if dirty { let bar = container( @@ -2337,19 +1517,14 @@ fn settings_page(state: &Moorly) -> Element<'_, Message> { .align_y(Alignment::End); stack![frame, bar_layer] - .width(960) + .width(Length::Fill) .height(Length::Fill) .into() } else { frame.into() }; - container(page) - .width(Length::Fill) - .height(Length::Fill) - .center(Length::Fill) - .padding(24) - .into() + page } /// A left-aligned menu row with an optional right-aligned shortcut. The @@ -2400,7 +1575,7 @@ fn scroll_button(symbol: &'static str, delta: f32, enabled: bool) -> iced::widge button(text(symbol).size(17)) .on_press_maybe(enabled.then_some(Message::ScrollTabsBy(delta))) .padding([0, 6]) - .style(button::text) + .style(theme::menubar_button) } /// Whether the tab strip can still scroll left/right, based on the last @@ -2413,23 +1588,55 @@ fn tab_scroll_limits(state: &Moorly) -> (bool, bool) { } fn view(state: &Moorly) -> Element<'_, Message> { - // Tab strip: push tab widgets into this row as they get added. - let mut tab_row = row![].spacing(2); - for &tab in &state.tabs { - tab_row = tab_row.push(tab_view(tab, state.active_tab == Some(tab))); - } + // Tab strip: size tabs to the strip's current width so as many as + // possible fit before scrolling starts. + let tabs = state.tabs.clone(); + let active_tab = state.active_tab; + let hide_close = state.hide_tab_close; + let now = Instant::now(); + let close_opacities: HashMap = tabs + .iter() + .map(|tab| { + let opacity = if hide_close { + state + .tab_close_fades + .get(tab) + .map(|animation| animation.interpolate(0.0, 1.0, now)) + .unwrap_or(0.0) + } else { + 1.0 + }; - let tab_strip = scrollable(tab_row) - .direction(scrollable::Direction::Horizontal( - scrollable::Scrollbar::hidden(), - )) - .id(TAB_SCROLL_ID) - .width(Length::Fill) - .height(Length::Fill) - .on_scroll(Message::TabsScrolled); + (*tab, opacity) + }) + .collect(); - // Convert vertical wheel input into horizontal tab-strip scrolling. - let tab_strip = mouse_area(tab_strip).on_scroll(Message::ScrollTabs); + let tab_strip = responsive(move |size| { + let width = tab_width(size.width, tabs.len()); + let mut tab_row = row![].spacing(TAB_SPACING).height(Length::Fill); + + for &tab in &tabs { + tab_row = tab_row.push(tab_view( + tab, + active_tab == Some(tab), + hide_close, + close_opacities.get(&tab).copied().unwrap_or(0.0), + width, + )); + } + + let tab_strip = scrollable(tab_row) + .direction(scrollable::Direction::Horizontal( + scrollable::Scrollbar::hidden(), + )) + .id(TAB_SCROLL_ID) + .width(Length::Fill) + .height(Length::Fill) + .on_scroll(Message::TabsScrolled); + + // Convert vertical wheel input into horizontal tab-strip scrolling. + mouse_area(tab_strip).on_scroll(Message::ScrollTabs).into() + }); let (can_scroll_left, can_scroll_right) = tab_scroll_limits(state); @@ -2452,7 +1659,12 @@ fn view(state: &Moorly) -> Element<'_, Message> { .width(Length::Fill) .height(MENUBAR_HEIGHT) .align_y(Alignment::Center) - .padding([0, 8]) + .padding(Padding { + top: 0.0, + right: 8.0, + bottom: 0.0, + left: 5.0, + }) .style(theme::menubar); let main_area: Element<'_, Message> = match state.active_tab { @@ -2464,6 +1676,14 @@ fn view(state: &Moorly) -> Element<'_, Message> { .into(), }; + let main_area = container(main_area) + .width(Length::Fill) + .height(Length::Fill) + .style(|_| container::Style { + background: Some(theme::TAB_ACTIVE_BG.into()), + ..container::Style::default() + }); + let base = column![menubar, main_area]; if !state.menu_open { @@ -2602,15 +1822,15 @@ mod tests { show_start_tab_draft: true, fade_settings: true, fade_settings_draft: true, + hide_tab_close: true, + hide_tab_close_draft: true, fade_animation: super::new_fade_animation(), + hovered_tab: None, + tab_close_fades: Default::default(), profile_filter: String::new(), profile_folders: vec![super::ProfileFolder::default_folder()], selected: None, hovered_folder: None, - add_menu_open: false, - folder_dialog: None, - profile_dialog: None, - dir_picker: None, next_blank_tab: 1, tab_offset: 0.0, tab_viewport_width: 0.0, @@ -2685,6 +1905,23 @@ mod tests { assert_eq!(state.show_start_tab_draft, state.show_start_tab); } + #[test] + fn hiding_tab_close_waits_for_save_or_undo() { + let mut state = state(); + + let _ = update(&mut state, Message::SetHideTabClose(false)); + assert!(state.hide_tab_close); + assert!(!state.hide_tab_close_draft); + + let _ = update(&mut state, Message::UndoChanges); + assert_eq!(state.hide_tab_close_draft, state.hide_tab_close); + + let _ = update(&mut state, Message::SetHideTabClose(false)); + super::apply_settings(&mut state); + assert!(!state.hide_tab_close); + assert_eq!(state.hide_tab_close_draft, state.hide_tab_close); + } + #[test] fn long_titles_are_ellipsized_to_max_width() { assert_eq!(super::fit_title("Settings"), "Settings"); @@ -2695,6 +1932,46 @@ mod tests { assert_eq!(fitted.chars().count(), 20); } + #[test] + fn tab_width_fits_the_strip_within_bounds() { + assert_eq!(super::tab_width(0.0, 2), super::TAB_MAX_WIDTH); + assert_eq!(super::tab_width(1000.0, 2), super::TAB_MAX_WIDTH); + assert_eq!(super::tab_width(500.0, 10), super::TAB_MIN_WIDTH); + + let width = super::tab_width(500.0, 3); + assert!((width - (500.0 - 2.0 * super::TAB_SPACING) / 3.0).abs() < 0.01); + } + + #[test] + fn tab_close_fades_in_and_out() { + let mut state = state(); + let tab = Tab::Start; + + let _ = update( + &mut state, + Message::HoverTab { + tab, + hovered: true, + }, + ); + + let later = iced::time::Instant::now() + iced::time::Duration::from_secs(1); + let fade = state.tab_close_fades.get(&tab).unwrap(); + assert!(fade.interpolate(0.0, 1.0, later) > 0.99); + + let _ = update( + &mut state, + Message::HoverTab { + tab, + hovered: false, + }, + ); + + let end = iced::time::Instant::now() + iced::time::Duration::from_secs(2); + let fade = state.tab_close_fades.get(&tab).unwrap(); + assert!(fade.interpolate(0.0, 1.0, end) < 0.01); + } + #[test] fn tab_scroll_buttons_follow_the_viewport() { let mut state = state(); @@ -2733,9 +2010,9 @@ mod tests { state.tabs = (1..=10).map(Tab::Blank).collect(); state.active_tab = Some(Tab::Blank(10)); state.tab_viewport_width = 400.0; - state.tab_content_width = 1000.0; + state.tab_content_width = 10.0 * super::TAB_MIN_WIDTH + 9.0 * super::TAB_SPACING; - state.tab_offset = 600.0; + state.tab_offset = 620.0; assert_eq!(super::tab_reveal_delta(&state), None); state.tab_offset = 0.0; @@ -2931,46 +2208,6 @@ mod tests { assert!(state.profile_folders[0].profiles.is_empty()); } - #[test] - fn add_menu_opens_the_folder_dialog() { - let mut state = state(); - let _ = update(&mut state, Message::ToggleAddMenu); - assert!(state.add_menu_open); - - let _ = update(&mut state, Message::OpenFolderDialog); - assert_eq!(state.folder_dialog.as_deref(), Some("")); - assert!(!state.add_menu_open); - - let _ = update(&mut state, Message::FolderNameChanged("Work".to_owned())); - let _ = update(&mut state, Message::ConfirmFolder); - - assert_eq!(state.profile_folders.len(), 2); - assert_eq!(state.profile_folders[1].name, "Work"); - assert!(state.folder_dialog.is_none()); - } - - #[test] - fn cancelling_a_folder_does_not_add_it() { - let mut state = state(); - let _ = update(&mut state, Message::OpenFolderDialog); - let _ = update(&mut state, Message::FolderNameChanged("Work".to_owned())); - let _ = update(&mut state, Message::CancelFolder); - - assert_eq!(state.profile_folders.len(), 1); - assert!(state.folder_dialog.is_none()); - } - - #[test] - fn empty_folder_names_are_rejected() { - let mut state = state(); - let _ = update(&mut state, Message::OpenFolderDialog); - let _ = update(&mut state, Message::FolderNameChanged(" ".to_owned())); - let _ = update(&mut state, Message::ConfirmFolder); - - assert_eq!(state.profile_folders.len(), 1); - assert!(state.folder_dialog.is_some()); - } - #[test] fn clicking_a_folder_toggles_its_profiles() { let mut state = state(); @@ -2983,197 +2220,4 @@ mod tests { let _ = update(&mut state, Message::ToggleFolder(0)); assert!(state.profile_folders[0].expanded); } - - #[test] - fn adding_a_profile_opens_its_folder_and_selects_it() { - let mut state = state(); - let _ = update(&mut state, Message::ToggleFolder(0)); - assert!(!state.profile_folders[0].expanded); - - let _ = update(&mut state, Message::OpenProfileDialog); - let _ = update(&mut state, Message::ProfileNameChanged("Server".to_owned())); - let _ = update(&mut state, Message::ConfirmProfile); - - assert!(state.profile_folders[0].expanded); - assert_eq!( - state.selected, - Some(super::Selection::Profile { - folder: 0, - profile: 0, - }) - ); - } - - #[test] - fn profile_dialog_stores_the_entered_details() { - let mut state = state(); - let _ = update(&mut state, Message::OpenProfileDialog); - let _ = update(&mut state, Message::ProfileNameChanged("Server".to_owned())); - let _ = update(&mut state, Message::ProfileHostChanged("example.com".to_owned())); - let _ = update(&mut state, Message::ProfilePortChanged("2222".to_owned())); - let _ = update(&mut state, Message::ProfileUsernameChanged("root".to_owned())); - let _ = update( - &mut state, - Message::ProfilePasswordChanged("hunter2".to_owned()), - ); - let _ = update( - &mut state, - Message::ProfileKindSelected(super::ProfileKind::Ssh), - ); - let _ = update( - &mut state, - Message::SessionEndSelected(super::SessionEnd::Reconnect), - ); - let _ = update(&mut state, Message::ConfirmProfile); - - let profile = &state.profile_folders[0].profiles[0]; - assert_eq!(profile.name, "Server"); - assert_eq!(profile.host, "example.com"); - assert_eq!(profile.port, "2222"); - assert_eq!(profile.username, "root"); - assert_eq!(profile.password, "hunter2"); - assert_eq!(profile.session_end, super::SessionEnd::Reconnect); - } - - #[test] - fn auth_method_is_exclusive_and_password_visibility_toggles() { - let mut state = state(); - let _ = update(&mut state, Message::OpenProfileDialog); - - let _ = update( - &mut state, - Message::AuthMethodSelected(super::AuthMethod::Key), - ); - assert_eq!( - state.profile_dialog.as_ref().unwrap().auth, - super::AuthMethod::Key - ); - - let _ = update(&mut state, Message::TogglePasswordVisibility); - assert!(state.profile_dialog.as_ref().unwrap().password_visible); - - let _ = update(&mut state, Message::TogglePasswordVisibility); - assert!(!state.profile_dialog.as_ref().unwrap().password_visible); - } - - #[test] - fn profile_port_only_accepts_0_to_65534() { - let mut state = state(); - let _ = update(&mut state, Message::OpenProfileDialog); - - let _ = update(&mut state, Message::ProfilePortChanged("65534".to_owned())); - assert_eq!(state.profile_dialog.as_ref().unwrap().port, "65534"); - - let _ = update(&mut state, Message::ProfilePortChanged("65535".to_owned())); - assert_eq!(state.profile_dialog.as_ref().unwrap().port, "65534"); - - let _ = update(&mut state, Message::ProfilePortChanged("abc".to_owned())); - assert_eq!(state.profile_dialog.as_ref().unwrap().port, "65534"); - - let _ = update(&mut state, Message::ProfilePortChanged(String::new())); - assert_eq!(state.profile_dialog.as_ref().unwrap().port, ""); - } - - #[test] - fn profile_names_are_limited_to_150_chars() { - let mut state = state(); - let _ = update(&mut state, Message::OpenProfileDialog); - let _ = update(&mut state, Message::ProfileNameChanged("a".repeat(200))); - - assert_eq!( - state.profile_dialog.as_ref().unwrap().name.chars().count(), - 150 - ); - } - - #[test] - fn terminal_profiles_store_the_start_path() { - let mut state = state(); - let _ = update(&mut state, Message::OpenProfileDialog); - - let draft = state.profile_dialog.as_ref().unwrap(); - if cfg!(windows) { - assert!(!draft.start_path.is_empty()); - } else { - assert_eq!(draft.start_path, "~/"); - } - - let _ = update( - &mut state, - Message::ProfileKindSelected(super::ProfileKind::Terminal), - ); - let _ = update(&mut state, Message::ProfileNameChanged("Term".to_owned())); - let _ = update( - &mut state, - Message::ProfileStartPathChanged("/tmp".to_owned()), - ); - let _ = update(&mut state, Message::ConfirmProfile); - - assert_eq!(state.profile_folders[0].profiles[0].start_path, "/tmp"); - } - - #[test] - fn directory_picker_lists_subdirectories() { - let stamp = std::time::SystemTime::now() - .duration_since(std::time::UNIX_EPOCH) - .unwrap() - .as_nanos(); - let root = std::env::temp_dir().join(format!("moorly-test-{stamp}")); - let child = root.join("child"); - std::fs::create_dir_all(&child).unwrap(); - - let picker = super::DirPicker::open( - root.clone(), - false, - super::PickerTarget::StartPath, - ); - assert_eq!(picker.entries, vec![child]); - assert!(picker.error.is_none()); - - std::fs::remove_dir_all(&root).unwrap(); - } - - #[test] - fn private_key_picker_selects_a_file() { - let stamp = std::time::SystemTime::now() - .duration_since(std::time::UNIX_EPOCH) - .unwrap() - .as_nanos(); - let root = std::env::temp_dir().join(format!("moorly-key-test-{stamp}")); - std::fs::create_dir_all(&root).unwrap(); - let key = root.join("id_test"); - std::fs::write(&key, b"key").unwrap(); - - let mut picker = - super::DirPicker::open(root.clone(), true, super::PickerTarget::PrivateKey); - assert_eq!(picker.entries, vec![key.clone()]); - assert!(!picker.can_confirm()); - - picker.selected = Some(key.clone()); - assert!(picker.can_confirm()); - - let mut state = state(); - state.profile_dialog = Some(super::ProfileDraft::new(0)); - state.dir_picker = Some(picker); - let _ = update(&mut state, Message::ConfirmDirectoryPicker); - - assert_eq!( - state.profile_dialog.as_ref().unwrap().private_key, - key.to_string_lossy() - ); - assert!(state.dir_picker.is_none()); - - std::fs::remove_dir_all(&root).unwrap(); - } - - #[test] - fn adding_a_folder_selects_it() { - let mut state = state(); - let _ = update(&mut state, Message::OpenFolderDialog); - let _ = update(&mut state, Message::FolderNameChanged("Work".to_owned())); - let _ = update(&mut state, Message::ConfirmFolder); - - assert_eq!(state.profile_folders[1].name, "Work"); - assert_eq!(state.selected, Some(super::Selection::Folder(1))); - } } diff --git a/src/theme.rs b/src/theme.rs index 4623f19..d339850 100644 --- a/src/theme.rs +++ b/src/theme.rs @@ -14,6 +14,15 @@ use iced::{Border, Color, Theme}; /// Accent red used for destructive/close affordances in both themes. pub const DANGER: Color = Color::from_rgb8(0xff, 0x3b, 0x30); +/// Accent blue used for the active tab outline. +pub const ACTIVE_TAB: Color = Color::from_rgb8(0x60, 0x82, 0xB6); +/// Highlight for the selected settings section. +pub const SETTINGS_ACTIVE: Color = Color::from_rgb8(0x5D, 0x3F, 0xD3); +/// Tab bar surfaces: the active tab merges with the content area below. +pub const TAB_ACTIVE_BG: Color = Color::from_rgb8(0x19, 0x19, 0x19); +pub const TAB_INACTIVE_BG: Color = Color::from_rgb8(0x13, 0x13, 0x13); +/// Text and icons drawn on the dark tab bar. +pub const TAB_TEXT: Color = Color::from_rgb8(0xe6, 0xe6, 0xe6); #[derive(Debug, Clone, Copy, Default, PartialEq, Eq)] pub enum Preference { @@ -96,20 +105,24 @@ pub(crate) fn config_path() -> Option { } /// Style for the top menubar. -pub fn menubar(theme: &Theme) -> container::Style { - let palette = theme.extended_palette(); - +pub fn menubar(_theme: &Theme) -> container::Style { container::Style { - background: Some(palette.background.weak.color.into()), - border: Border { - color: palette.background.strong.color, - width: 1.0, - radius: 0.0.into(), - }, + background: Some(TAB_INACTIVE_BG.into()), ..container::Style::default() } } +/// Style for buttons that sit directly on the dark menubar. +pub fn menubar_button(theme: &Theme, status: button::Status) -> button::Style { + let mut style = button::text(theme, status); + style.text_color = if matches!(status, button::Status::Disabled) { + TAB_TEXT.scale_alpha(0.4) + } else { + TAB_TEXT + }; + style +} + /// Style for dropdown menu panels. pub fn menu(theme: &Theme) -> container::Style { let mut style = container::rounded_box(theme); @@ -117,21 +130,51 @@ pub fn menu(theme: &Theme) -> container::Style { style } -/// Style for a tab in the menubar; active tabs sit on the base surface. -pub fn tab(theme: &Theme, active: bool) -> container::Style { - let palette = theme.extended_palette(); - +/// Style for a tab in the menubar; active tabs sit on the content surface. +/// The active outline is drawn separately so it has no bottom edge. +pub fn tab(_theme: &Theme, active: bool) -> container::Style { container::Style { - background: active.then(|| palette.background.base.color.into()), + background: Some(if active { + TAB_ACTIVE_BG.into() + } else { + TAB_INACTIVE_BG.into() + }), border: Border { - color: palette.background.strong.color, - width: if active { 1.0 } else { 0.0 }, - radius: 3.0.into(), + color: Color::TRANSPARENT, + width: 0.0, + radius: Radius { + top_left: 3.0, + top_right: 3.0, + bottom_right: 0.0, + bottom_left: 0.0, + }, }, ..container::Style::default() } } +/// Style for the active tab's thin top/left/right outline. +pub fn tab_outline(_theme: &Theme) -> container::Style { + container::Style { + background: Some(ACTIVE_TAB.into()), + ..container::Style::default() + } +} + +/// Style for the selected settings section in the settings menu. +pub fn settings_active(_theme: &Theme, _status: button::Status) -> button::Style { + button::Style { + background: Some(SETTINGS_ACTIVE.into()), + text_color: Color::WHITE, + border: Border { + color: SETTINGS_ACTIVE, + width: 1.0, + radius: 4.0.into(), + }, + ..button::Style::default() + } +} + /// Style for the currently selected option in a group of choice buttons. pub fn selected_option(theme: &Theme, _status: button::Status) -> button::Style { let palette = theme.extended_palette(); @@ -172,15 +215,6 @@ pub fn positive_option(theme: &Theme, status: button::Status) -> button::Style { }) } -/// Style for a destructive/cancelling action button (red). -pub fn danger_option(theme: &Theme, status: button::Status) -> button::Style { - let danger = theme.extended_palette().danger; - filled_option(match status { - button::Status::Hovered | button::Status::Pressed => danger.strong, - _ => danger.base, - }) -} - fn filled_option(pair: iced::theme::palette::Pair) -> button::Style { button::Style { background: Some(pair.color.into()), @@ -194,22 +228,6 @@ fn filled_option(pair: iced::theme::palette::Pair) -> button::Style { } } -/// Style for the password eye button while the password is revealed. -pub fn revealed_eye(theme: &Theme, status: button::Status) -> button::Style { - let palette = theme.extended_palette(); - - button::Style { - background: None, - text_color: palette.background.base.text, - border: Border { - color: palette.primary.base.color, - width: 1.0, - radius: 4.0.into(), - }, - ..button::text(theme, status) - } -} - /// Style for a selectable list row; the selected one gets a tinted background. pub fn selectable_row(theme: &Theme, status: button::Status, selected: bool) -> button::Style { if selected { @@ -294,7 +312,7 @@ pub fn settings_frame(theme: &Theme) -> container::Style { #[cfg(test)] mod tests { - use super::Preference; + use super::{Preference, Theme}; #[test] fn preference_round_trips_through_text() { @@ -303,4 +321,19 @@ mod tests { } assert_eq!(Preference::parse("unknown"), None); } + + #[test] + fn active_tabs_are_visually_distinct() { + let theme = Theme::Light; + let active = super::tab(&theme, true); + let inactive = super::tab(&theme, false); + + assert_ne!(active.background, inactive.background); + assert_eq!(active.border.width, 0.0); + assert_eq!(inactive.border.width, 0.0); + assert_eq!( + super::tab_outline(&theme).background, + Some(super::ACTIVE_TAB.into()) + ); + } }