Updated terminal profile settings
This commit is contained in:
1 parent
cbcf5a3fa4
commit
aa4d0d8e61
1 file changed
+307
-3
+307
-3
@@ -1,3 +1,5 @@
|
||||
use std::path::PathBuf;
|
||||
|
||||
use iced::widget::{
|
||||
button, column, container, mouse_area, pick_list, row, rule, scrollable, space, stack, text,
|
||||
text_input, tooltip,
|
||||
@@ -368,6 +370,7 @@ struct Profile {
|
||||
username: String,
|
||||
auth: AuthMethod,
|
||||
password: String,
|
||||
start_path: String,
|
||||
}
|
||||
|
||||
/// Editable state of the "add profile" dialog.
|
||||
@@ -383,6 +386,7 @@ struct ProfileDraft {
|
||||
auth: AuthMethod,
|
||||
password: String,
|
||||
password_visible: bool,
|
||||
start_path: String,
|
||||
}
|
||||
|
||||
impl ProfileDraft {
|
||||
@@ -398,6 +402,7 @@ impl ProfileDraft {
|
||||
auth: AuthMethod::default(),
|
||||
password: String::new(),
|
||||
password_visible: false,
|
||||
start_path: default_start_path(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -429,6 +434,96 @@ impl std::fmt::Display for FolderChoice {
|
||||
}
|
||||
}
|
||||
|
||||
/// In-app directory browser state.
|
||||
#[derive(Debug, Clone)]
|
||||
struct DirPicker {
|
||||
path: PathBuf,
|
||||
entries: Vec<PathBuf>,
|
||||
error: Option<String>,
|
||||
}
|
||||
|
||||
impl DirPicker {
|
||||
fn open(path: PathBuf) -> Self {
|
||||
let mut picker = Self {
|
||||
path,
|
||||
entries: Vec::new(),
|
||||
error: None,
|
||||
};
|
||||
picker.reload();
|
||||
picker
|
||||
}
|
||||
|
||||
fn reload(&mut self) {
|
||||
match std::fs::read_dir(&self.path) {
|
||||
Ok(read) => {
|
||||
let mut entries: Vec<PathBuf> = read
|
||||
.filter_map(|entry| entry.ok())
|
||||
.map(|entry| entry.path())
|
||||
.filter(|path| path.is_dir())
|
||||
.collect();
|
||||
entries.sort();
|
||||
self.entries = entries;
|
||||
self.error = None;
|
||||
}
|
||||
Err(error) => {
|
||||
self.entries.clear();
|
||||
self.error = Some(error.to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn navigate(&mut self, path: PathBuf) {
|
||||
self.path = path;
|
||||
self.reload();
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
@@ -469,6 +564,7 @@ struct Moorly {
|
||||
add_menu_open: bool,
|
||||
folder_dialog: Option<String>,
|
||||
profile_dialog: Option<ProfileDraft>,
|
||||
dir_picker: Option<DirPicker>,
|
||||
next_blank_tab: u32,
|
||||
tab_offset: f32,
|
||||
tab_viewport_width: f32,
|
||||
@@ -538,6 +634,7 @@ impl Default for Moorly {
|
||||
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,
|
||||
@@ -576,6 +673,12 @@ enum Message {
|
||||
AuthMethodSelected(AuthMethod),
|
||||
ProfilePasswordChanged(String),
|
||||
TogglePasswordVisibility,
|
||||
ProfileStartPathChanged(String),
|
||||
OpenDirectoryPicker,
|
||||
DirPickerEnter(PathBuf),
|
||||
DirPickerUp,
|
||||
ConfirmDirectoryPicker,
|
||||
CancelDirectoryPicker,
|
||||
ConfirmProfile,
|
||||
CancelProfile,
|
||||
OpenFolderDialog,
|
||||
@@ -744,6 +847,41 @@ fn update(state: &mut Moorly, message: Message) -> Task<Message> {
|
||||
draft.password_visible = !draft.password_visible;
|
||||
}
|
||||
}
|
||||
Message::ProfileStartPathChanged(value) => {
|
||||
if let Some(draft) = state.profile_dialog.as_mut() {
|
||||
draft.start_path = 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));
|
||||
}
|
||||
Message::DirPickerEnter(path) => {
|
||||
if let Some(picker) = state.dir_picker.as_mut() {
|
||||
picker.navigate(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() {
|
||||
if let Some(draft) = state.profile_dialog.as_mut() {
|
||||
draft.start_path = picker.path.to_string_lossy().into_owned();
|
||||
}
|
||||
}
|
||||
}
|
||||
Message::CancelDirectoryPicker => state.dir_picker = None,
|
||||
Message::ConfirmProfile => {
|
||||
let Some(draft) = state.profile_dialog.as_ref() else {
|
||||
return Task::none();
|
||||
@@ -767,6 +905,7 @@ fn update(state: &mut Moorly, message: Message) -> Task<Message> {
|
||||
username: draft.username.trim().to_owned(),
|
||||
auth: draft.auth,
|
||||
password: draft.password.clone(),
|
||||
start_path: draft.start_path.clone(),
|
||||
});
|
||||
folder.expanded = true;
|
||||
state.selected = Some(Selection::Profile {
|
||||
@@ -1204,7 +1343,9 @@ fn profiles_panel(state: &Moorly) -> Element<'_, Message> {
|
||||
.width(Length::Fill)
|
||||
.height(Length::Fill);
|
||||
|
||||
let dialog_open = state.folder_dialog.is_some() || state.profile_dialog.is_some();
|
||||
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();
|
||||
@@ -1216,7 +1357,9 @@ fn profiles_panel(state: &Moorly) -> Element<'_, Message> {
|
||||
.width(Length::Fill)
|
||||
.height(Length::Fill),
|
||||
)
|
||||
.on_press(if state.folder_dialog.is_some() {
|
||||
.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
|
||||
@@ -1322,9 +1465,92 @@ fn profiles_panel(state: &Moorly) -> Element<'_, Message> {
|
||||
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 browser used by the Terminal "Start path" field.
|
||||
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 name = path
|
||||
.file_name()
|
||||
.map(|name| name.to_string_lossy().into_owned())
|
||||
.unwrap_or_else(|| path.display().to_string());
|
||||
|
||||
entries = entries.push(
|
||||
button(
|
||||
row![text("📁").size(14), text(name)]
|
||||
.spacing(8)
|
||||
.align_y(Alignment::Center),
|
||||
)
|
||||
.on_press(Message::DirPickerEnter(path.clone()))
|
||||
.width(Length::Fill)
|
||||
.padding([6, 8])
|
||||
.style(button::text),
|
||||
);
|
||||
}
|
||||
|
||||
let listing = container(scrollable(entries).width(Length::Fill).height(Length::Fill))
|
||||
.width(Length::Fill)
|
||||
.height(240)
|
||||
.style(theme::settings_frame);
|
||||
|
||||
let mut content = column![
|
||||
text("Select folder").size(16),
|
||||
text(picker.path.display().to_string()).size(12),
|
||||
listing,
|
||||
]
|
||||
.spacing(10)
|
||||
.width(Length::Fill);
|
||||
|
||||
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(Message::ConfirmDirectoryPicker)
|
||||
.style(theme::positive_option),
|
||||
]
|
||||
.spacing(8),
|
||||
);
|
||||
|
||||
container(content)
|
||||
.width(520)
|
||||
.padding(16)
|
||||
.style(theme::menu)
|
||||
.into()
|
||||
}
|
||||
|
||||
/// The add-profile dialog: details on the left, type-specific view on the right.
|
||||
fn profile_dialog_view<'a>(
|
||||
draft: &'a ProfileDraft,
|
||||
@@ -1427,7 +1653,41 @@ fn profile_dialog_view<'a>(
|
||||
/// 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::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(button::text),
|
||||
)
|
||||
.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![
|
||||
@@ -1948,6 +2208,7 @@ mod tests {
|
||||
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,
|
||||
@@ -2356,6 +2617,49 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[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());
|
||||
assert_eq!(picker.entries, vec![child]);
|
||||
assert!(picker.error.is_none());
|
||||
|
||||
std::fs::remove_dir_all(&root).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn adding_a_folder_selects_it() {
|
||||
let mut state = state();
|
||||
|
||||
Reference in new issue
Block a user