Skip to content

Commit

Permalink
refactor settings modal to enable stateful widgets, ensure TextAreaWi…
Browse files Browse the repository at this point in the history
…dget is stateful and is able to receive keyevents
  • Loading branch information
aprxi committed Sep 4, 2024
1 parent 4f58d3f commit 1ada12f
Show file tree
Hide file tree
Showing 21 changed files with 641 additions and 490 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::collections::HashMap;
use std::path::PathBuf;

use super::*;
Expand Down Expand Up @@ -200,4 +199,13 @@ impl UserProfileDbHandler {
})
.map_err(ApplicationError::from)
}

pub async fn rename_provider_config(
&self,
profile: &ProviderConfig,
new_name: &str,
) -> Result<(), ApplicationError> {
eprintln!("TODO: Implement rename_provider_config");
Ok(())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use lumni::api::error::ApplicationError;
use super::key_event::KeyTrack;
use super::text_window_event::handle_text_window_event;
use super::{
AppUi, LineType, PromptAction, TextArea, TextWindowTrait, WindowEvent,
AppUi, LineType, PromptAction, PromptWindow, TextWindowTrait, WindowEvent,
};
use crate::apps::builtin::llm::prompt::src::tui::WindowKind;
pub use crate::external as lumni;
Expand Down Expand Up @@ -110,7 +110,7 @@ pub fn handle_prompt_window_event(
handle_text_window_event(key_track, &mut app_ui.prompt, is_running)
}

fn is_closed_block(prompt_window: &mut TextArea) -> Option<bool> {
fn is_closed_block(prompt_window: &mut PromptWindow) -> Option<bool> {
// return None if not inside a block
// return Some(true) if block is closed, else return Some(false)
let code_block = prompt_window.current_code_block();
Expand All @@ -121,7 +121,7 @@ fn is_closed_block(prompt_window: &mut TextArea) -> Option<bool> {
}

fn ensure_closed_block(
prompt_window: &mut TextArea,
prompt_window: &mut PromptWindow,
) -> Result<(), ApplicationError> {
if let Some(closed_block) = is_closed_block(prompt_window) {
if !closed_block {
Expand All @@ -132,7 +132,7 @@ fn ensure_closed_block(
Ok(())
}

fn in_editing_block(prompt_window: &mut TextArea) -> bool {
fn in_editing_block(prompt_window: &mut PromptWindow) -> bool {
let line_type = prompt_window.current_line_type().unwrap_or(LineType::Text);
match line_type {
LineType::Code(block_line) => !block_line.is_end(),
Expand Down
24 changes: 12 additions & 12 deletions lumni/src/apps/builtin/llm/prompt/src/tui/events/key_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use super::{
pub struct KeyTrack {
previous_key_str: Option<String>,
numeric_input: NumericInput,
current_key: KeyEvent,
pub current_key: KeyEvent,
leader_key_set: bool,
}

Expand All @@ -29,6 +29,14 @@ impl KeyTrack {
}
}

pub fn process_key(&mut self, key_event: KeyEvent) {
if !self.leader_key_set {
self.update_previous_key(key_event);
} else {
self.update_previous_key_with_leader(key_event);
}
}

pub fn previous_key_str(&self) -> Option<&str> {
self.previous_key_str.as_deref()
}
Expand All @@ -37,7 +45,7 @@ impl KeyTrack {
self.current_key
}

pub fn update_previous_key(&mut self, key_event: KeyEvent) {
fn update_previous_key(&mut self, key_event: KeyEvent) {
if let KeyCode::Char(c) = self.current_key.code {
// copy previous key_event to previous_char
self.previous_key_str = Some(c.to_string());
Expand All @@ -56,7 +64,7 @@ impl KeyTrack {
}
}

pub fn update_previous_key_with_leader(
fn update_previous_key_with_leader(
&mut self,
key_event: KeyEvent,
) -> Option<&str> {
Expand Down Expand Up @@ -163,15 +171,7 @@ impl KeyEventHandler {
is_running: Arc<AtomicBool>,
handler: &mut ConversationDbHandler,
) -> Result<WindowEvent, ApplicationError> {
if !self.key_track.leader_key_set()
|| self
.key_track
.update_previous_key_with_leader(key_event)
.is_none()
{
// leader key not set or updating leader is un-successful
self.key_track.update_previous_key(key_event);
}
self.key_track.process_key(key_event);

// try to catch Shift+Enter key press in prompt window
match current_mode {
Expand Down
2 changes: 1 addition & 1 deletion lumni/src/apps/builtin/llm/prompt/src/tui/events/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use super::clipboard::ClipboardProvider;
use super::modals::{ModalAction, ModalWindowType};
use super::ui::AppUi;
use super::window::{
LineType, MoveCursor, TextArea, TextDocumentTrait, TextWindowTrait,
LineType, MoveCursor, PromptWindow, TextDocumentTrait, TextWindowTrait,
WindowKind,
};
use super::{ConversationDbHandler, NewConversation, ThreadedChatSession};
Expand Down
4 changes: 2 additions & 2 deletions lumni/src/apps/builtin/llm/prompt/src/tui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ use lumni::api::error::ApplicationError;
pub use modals::{ModalAction, ModalWindowTrait, ModalWindowType};
pub use ui::AppUi;
pub use window::{
CommandLine, ReadDocument, ReadWriteDocument, ResponseWindow, SimpleString,
TextArea, TextBuffer, TextDocumentTrait, TextLine, TextSegment,
CommandLine, PromptWindow, ReadDocument, ReadWriteDocument, ResponseWindow,
SimpleString, TextBuffer, TextDocumentTrait, TextLine, TextSegment,
TextWindowTrait, WindowKind,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use ratatui::Frame;
use super::{
ApplicationError, Conversation, ConversationDbHandler, ConversationStatus,
KeyTrack, ModalAction, ModalWindowTrait, ModalWindowType,
PromptInstruction, TextArea, TextWindowTrait, ThreadedChatSession,
PromptInstruction, PromptWindow, TextWindowTrait, ThreadedChatSession,
UserEvent, WindowEvent,
};
use crate::apps::builtin::llm::prompt::src::chat::db::ConversationId;
Expand All @@ -31,7 +31,7 @@ pub struct ConversationListModal<'a> {
conversations: Vec<Conversation>,
current_tab: ConversationStatus,
tab_indices: HashMap<ConversationStatus, usize>,
edit_name_line: Option<TextArea<'a>>,
edit_name_line: Option<PromptWindow<'a>>,
editing_index: Option<usize>,
last_selected_conversation_id: Option<ConversationId>,
}
Expand Down Expand Up @@ -234,7 +234,7 @@ impl<'a> ConversationListModal<'a> {

async fn edit_conversation_name(&mut self) -> Result<(), ApplicationError> {
if let Some(conversation) = self.get_current_conversation() {
let mut command_line = TextArea::new();
let mut command_line = PromptWindow::new();
command_line.text_set(&conversation.name, None)?;
command_line.set_status_insert();
self.edit_name_line = Some(command_line);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ use ratatui::text::{Line, Span};
use ratatui::widgets::{Block, Borders, Clear, Paragraph};
use ratatui::Frame;

use super::widgets::{FileBrowserState, FileBrowserWidget};
use super::{
ApplicationError, ConversationDbHandler, FileBrowserState,
FileBrowserWidget, KeyTrack, ModalAction, ModalWindowTrait,
ModalWindowType, ThreadedChatSession, WindowEvent,
ApplicationError, ConversationDbHandler, KeyTrack, ModalAction,
ModalWindowTrait, ModalWindowType, ThreadedChatSession, WindowEvent,
};
pub use crate::external as lumni;

Expand Down
8 changes: 4 additions & 4 deletions lumni/src/apps/builtin/llm/prompt/src/tui/modals/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ pub use filebrowser::FileBrowserModal;
use ratatui::layout::Rect;
use ratatui::Frame;
pub use settings::SettingsModal;
use widgets::{FileBrowserState, FileBrowserWidget};

pub use super::widgets;
use super::{
ApplicationError, Conversation, ConversationDbHandler, ConversationStatus,
KeyTrack, MaskMode, ModelServer, ModelSpec, PromptInstruction,
ProviderConfig, ProviderConfigOptions, ServerTrait, SimpleString, TextArea,
TextLine, TextWindowTrait, ThreadedChatSession, UserEvent, UserProfile,
UserProfileDbHandler, WindowEvent, SUPPORTED_MODEL_ENDPOINTS,
PromptWindow, ProviderConfig, ProviderConfigOptions, ReadDocument,
ServerTrait, SimpleString, TextLine, TextWindowTrait, ThreadedChatSession,
UserEvent, UserProfile, UserProfileDbHandler, WindowEvent,
SUPPORTED_MODEL_ENDPOINTS,
};

#[derive(Debug, Clone, PartialEq)]
Expand Down
21 changes: 15 additions & 6 deletions lumni/src/apps/builtin/llm/prompt/src/tui/modals/settings/list.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::*;

pub trait ListItem: Clone {
pub trait ListItemTrait: Clone {
fn name(&self) -> &str;
fn id(&self) -> i64;
fn with_new_name(&self, new_name: String) -> Self;
Expand All @@ -9,13 +9,13 @@ pub trait ListItem: Clone {
Self: Sized;
}

pub struct SettingsList<T: ListItem> {
pub struct SettingsList<T: ListItemTrait> {
items: Vec<T>,
selected_index: usize,
pub default_item: Option<T>,
}

impl<T: ListItem> SettingsList<T> {
impl<T: ListItemTrait> SettingsList<T> {
pub fn new(items: Vec<T>, default_item: Option<T>) -> Self {
let mut list = SettingsList {
items,
Expand Down Expand Up @@ -106,21 +106,30 @@ impl<T: ListItem> SettingsList<T> {
}
}

impl<T: ListItem> SettingsListTrait for SettingsList<T> {
impl<T: ListItemTrait + SettingsItem> SettingsListTrait for SettingsList<T> {
type Item = T;

fn get_items(&self) -> Vec<String> {
self.get_items()
}

fn get_selected_index(&self) -> usize {
self.selected_index
}

fn get_selected_item(&self) -> Option<&Self::Item> {
self.items.get(self.selected_index)
}
}

pub trait SettingsListTrait {
type Item: ListItemTrait + SettingsItem;
fn get_items(&self) -> Vec<String>;
fn get_selected_index(&self) -> usize;
fn get_selected_item(&self) -> Option<&Self::Item>;
}

impl ListItem for UserProfile {
impl ListItemTrait for UserProfile {
fn name(&self) -> &str {
&self.name
}
Expand All @@ -141,7 +150,7 @@ impl ListItem for UserProfile {
}
}

impl ListItem for ProviderConfig {
impl ListItemTrait for ProviderConfig {
fn name(&self) -> &str {
&self.name
}
Expand Down
Loading

0 comments on commit 1ada12f

Please sign in to comment.