Skip to content

Commit

Permalink
profile selection, fix selection bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
aprxi committed Aug 18, 2024
1 parent 60a5cc3 commit 0c99272
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 25 deletions.
31 changes: 20 additions & 11 deletions lumni/src/apps/builtin/llm/prompt/src/tui/modals/profiles/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,6 @@ impl ProfileEditModal {
self.ui_state.set_focus(Focus::SettingsList);
}
}
(EditMode::NotEditing, KeyCode::Tab) => {
if !self.profile_list.is_new_profile_selected() {
self.ui_state.set_focus(Focus::SettingsList);
}
}
(EditMode::NotEditing, KeyCode::Char('r') | KeyCode::Char('R')) => {
if !self.profile_list.is_new_profile_selected() {
self.ui_state.set_edit_mode(EditMode::RenamingProfile);
Expand Down Expand Up @@ -305,7 +300,7 @@ impl ProfileEditModal {
fn cancel_new_profile_creation(&mut self) {
self.ui_state.set_edit_mode(EditMode::NotEditing);
self.ui_state.set_focus(Focus::ProfileList);
self.profile_list.reset_selection();
self.profile_list.select_new_profile();
self.settings_editor.clear();
}

Expand Down Expand Up @@ -611,13 +606,22 @@ impl ModalWindowTrait for ProfileEditModal {
let key_code = key_event.current_key().code;
let result = match self.ui_state.focus {
Focus::ProfileList => match key_code {
KeyCode::Right | KeyCode::Tab => {
if !self.profile_list.is_empty() {
KeyCode::Tab => {
if !self.profile_list.is_new_profile_selected() {
self.ui_state.set_focus(Focus::SettingsList);
self.load_profile().await?;
}
Ok(WindowEvent::Modal(ModalAction::WaitForKeyEvent))
}
KeyCode::Esc => {
if self.ui_state.edit_mode == EditMode::CreatingNewProfile {
self.cancel_new_profile_creation();
self.load_profile_or_clear().await?;
} else {
return Ok(WindowEvent::PromptWindow(None));
}
Ok(WindowEvent::Modal(ModalAction::WaitForKeyEvent))
}
_ => Ok(self.handle_profile_list_input(key_code).await?),
},
Focus::SettingsList => match key_code {
Expand All @@ -634,9 +638,14 @@ impl ModalWindowTrait for ProfileEditModal {
}
_ => Ok(self.handle_settings_list_input(key_code).await?),
},
Focus::NewProfileType => {
Ok(self.handle_new_profile_type_input(key_code).await?)
}
Focus::NewProfileType => match key_code {
KeyCode::Esc => {
self.cancel_new_profile_creation();
self.load_profile_or_clear().await?;
Ok(WindowEvent::Modal(ModalAction::WaitForKeyEvent))
}
_ => Ok(self.handle_new_profile_type_input(key_code).await?),
},
Focus::RenamingProfile => {
Ok(self.handle_profile_list_input(key_code).await?)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,27 @@ impl ProfileList {
}
}

pub fn is_empty(&self) -> bool {
self.profiles.is_empty()
}

pub fn get_selected_profile(&self) -> Option<&str> {
self.profiles.get(self.selected_index).map(|s| s.as_str())
}

pub fn reset_selection(&mut self) {
if !self.profiles.is_empty() {
self.selected_index = self.profiles.len() - 1;
} else {
self.selected_index = 0;
}
}

pub fn is_new_profile_selected(&self) -> bool {
self.selected_index == self.profiles.len()
pub fn select_new_profile(&mut self) {
self.selected_index = self.profiles.len();
}

pub fn move_selection_up(&mut self) {
if self.selected_index > 0 {
self.selected_index -= 1;
} else if self.selected_index == 0 && !self.profiles.is_empty() {
// If at the top and "New Profile" is selected, wrap to the bottom
self.selected_index = self.profiles.len() - 1;
}
}

pub fn is_new_profile_selected(&self) -> bool {
self.selected_index == self.profiles.len()
}

pub fn move_selection_down(&mut self) {
if self.selected_index < self.profiles.len() {
self.selected_index += 1;
Expand Down

0 comments on commit 0c99272

Please sign in to comment.