From cbcf5a3fa4bc1034b8ffd95fe193ac16aeb12391 Mon Sep 17 00:00:00 2001 From: squid Date: Sun, 1 Nov 2026 16:47:38 +0100 Subject: [PATCH] Basic profile for terminal and ssh selection --- src/main.rs | 644 ++++++++++++++++++++++++++++++++++++++++++++++++--- src/theme.rs | 16 ++ 2 files changed, 631 insertions(+), 29 deletions(-) diff --git a/src/main.rs b/src/main.rs index 5f3210b..3ac2aca 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,6 @@ use iced::widget::{ - button, column, container, mouse_area, row, rule, scrollable, space, stack, text, text_input, - tooltip, + button, column, container, mouse_area, pick_list, row, rule, scrollable, space, stack, text, + text_input, tooltip, }; use iced::widget::{Id, operation}; use iced::{Alignment, Element, Length, Padding, Subscription, Task, keyboard, mouse}; @@ -26,6 +26,8 @@ const TAB_SCROLL_STEP: f32 = 120.0; 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) @@ -276,11 +278,162 @@ struct ShortcutCapture { error: Option, } -/// A named group of profiles. Profiles themselves can be added later. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] +enum ProfileKind { + #[default] + Ssh, + Terminal, + 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)] +enum SessionEnd { + #[default] + Auto, + Keep, + Reconnect, + 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)] +enum AuthMethod { + #[default] + Password, + Key, + Agent, + 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 { + name: String, + kind: ProfileKind, + session_end: SessionEnd, + host: String, + port: String, + username: String, + auth: AuthMethod, + password: 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, +} + +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, + } + } + + 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) + } +} + +/// A named group of profiles. #[derive(Debug, Clone)] struct ProfileFolder { name: String, - profiles: Vec, + profiles: Vec, expanded: bool, } @@ -315,6 +468,7 @@ struct Moorly { hovered_folder: Option, add_menu_open: bool, folder_dialog: Option, + profile_dialog: Option, next_blank_tab: u32, tab_offset: f32, tab_viewport_width: f32, @@ -383,6 +537,7 @@ impl Default for Moorly { hovered_folder: None, add_menu_open: false, folder_dialog: None, + profile_dialog: None, next_blank_tab: 1, tab_offset: 0.0, tab_viewport_width: 0.0, @@ -410,7 +565,19 @@ enum Message { ToggleFolder(usize), HoverFolder(Option), SelectProfile { folder: usize, profile: usize }, - AddProfile, + OpenProfileDialog, + ProfileNameChanged(String), + ProfileKindSelected(ProfileKind), + ProfileFolderSelected(FolderChoice), + SessionEndSelected(SessionEnd), + ProfileHostChanged(String), + ProfilePortChanged(String), + ProfileUsernameChanged(String), + AuthMethodSelected(AuthMethod), + ProfilePasswordChanged(String), + TogglePasswordVisibility, + ConfirmProfile, + CancelProfile, OpenFolderDialog, FolderNameChanged(String), ConfirmFolder, @@ -511,24 +678,104 @@ fn update(state: &mut Moorly, message: Message) -> Task { Message::SelectProfile { folder, profile } => { state.selected = Some(Selection::Profile { folder, profile }); } - Message::AddProfile => { - let folder_index = match state.selected { + Message::OpenProfileDialog => { + let folder = match state.selected { Some(Selection::Folder(index)) => index, Some(Selection::Profile { folder, .. }) => folder, None => 0, }; - - if let Some(folder) = state.profile_folders.get_mut(folder_index) { - let number = folder.profiles.len() + 1; - folder.profiles.push(format!("Profile {number}")); - folder.expanded = true; - state.selected = Some(Selection::Profile { - folder: folder_index, - profile: folder.profiles.len() - 1, - }); - } 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::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(), + }); + 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()); @@ -957,7 +1204,7 @@ fn profiles_panel(state: &Moorly) -> Element<'_, Message> { .width(Length::Fill) .height(Length::Fill); - let dialog_open = state.folder_dialog.is_some(); + let dialog_open = state.folder_dialog.is_some() || state.profile_dialog.is_some(); if !state.add_menu_open && !dialog_open { return stack![base].into(); @@ -969,8 +1216,10 @@ fn profiles_panel(state: &Moorly) -> Element<'_, Message> { .width(Length::Fill) .height(Length::Fill), ) - .on_press(if dialog_open { + .on_press(if state.folder_dialog.is_some() { Message::CancelFolder + } else if state.profile_dialog.is_some() { + Message::CancelProfile } else { Message::CloseAddMenu }); @@ -985,7 +1234,7 @@ fn profiles_panel(state: &Moorly) -> Element<'_, Message> { .spacing(8) .align_y(Alignment::Center), ) - .on_press(Message::AddProfile) + .on_press(Message::OpenProfileDialog) .width(Length::Fill) .padding([6, 10]) .style(button::text), @@ -1060,9 +1309,262 @@ fn profiles_panel(state: &Moorly) -> Element<'_, Message> { 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); + } + layers.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 => text("Terminal").size(18).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)); + } + + 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() +} + /// One folder heading followed by its profiles. Clicking the heading toggles /// the profile list; clicking a row selects it. fn profile_folder_row( @@ -1074,8 +1576,10 @@ fn profile_folder_row( let is_selected = selected == Some(Selection::Folder(index)); let expanded = folder.expanded; + let folder_glyph = if expanded { "📂" } else { "📁" }; + let mut heading_content = row![ - text("📁").size(14), + text(folder_glyph).size(14), text(folder.name.as_str()).size(14), space::horizontal(), ] @@ -1120,7 +1624,7 @@ fn profile_folder_row( rows = rows.push( button( - row![text("👤").size(13), text(profile.as_str()).size(13)] + row![text("👤").size(13), text(profile.name.as_str()).size(13)] .spacing(8) .align_y(Alignment::Center), ) @@ -1182,7 +1686,7 @@ fn settings_page(state: &Moorly) -> Element<'_, Message> { .padding(16); let frame = container(row![menu, rule::vertical(1), content]) - .width(800) + .width(960) .height(Length::Fill) .style(theme::settings_frame); @@ -1443,6 +1947,7 @@ mod tests { hovered_folder: None, add_menu_open: false, folder_dialog: None, + profile_dialog: None, next_blank_tab: 1, tab_offset: 0.0, tab_viewport_width: 0.0, @@ -1697,17 +2202,14 @@ mod tests { } #[test] - fn add_menu_adds_profiles_and_folders() { + 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::AddProfile); - assert_eq!(state.profile_folders[0].profiles, vec!["Profile 1".to_owned()]); - 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); @@ -1758,7 +2260,9 @@ mod tests { let _ = update(&mut state, Message::ToggleFolder(0)); assert!(!state.profile_folders[0].expanded); - let _ = update(&mut state, Message::AddProfile); + 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!( @@ -1770,6 +2274,88 @@ mod tests { ); } + #[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 adding_a_folder_selects_it() { let mut state = state(); diff --git a/src/theme.rs b/src/theme.rs index 17d315e..003d86d 100644 --- a/src/theme.rs +++ b/src/theme.rs @@ -193,6 +193,22 @@ 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 {