From 11b073c2fdf06a927f23521261586fb2883dbf87 Mon Sep 17 00:00:00 2001 From: Priec Date: Thu, 21 Aug 2025 10:33:52 +0200 Subject: [PATCH] removing highlightmode from the app, handled by the library now --- client/src/components/admin/add_logic.rs | 1 - client/src/components/admin/add_table.rs | 1 - client/src/components/auth/login.rs | 1 - client/src/components/auth/register.rs | 11 --- client/src/modes/handlers/event.rs | 92 +++++++++-------------- client/src/modes/handlers/mode_manager.rs | 41 +++++----- client/src/state/app.rs | 1 - client/src/state/app/highlight.rs | 20 ----- client/src/state/pages/form.rs | 2 - client/src/ui/handlers/render.rs | 14 ---- client/src/ui/handlers/ui.rs | 1 - 11 files changed, 54 insertions(+), 131 deletions(-) delete mode 100644 client/src/state/app/highlight.rs diff --git a/client/src/components/admin/add_logic.rs b/client/src/components/admin/add_logic.rs index 21b2875..723bb10 100644 --- a/client/src/components/admin/add_logic.rs +++ b/client/src/components/admin/add_logic.rs @@ -1,6 +1,5 @@ // src/components/admin/add_logic.rs use crate::config::colors::themes::Theme; -use crate::state::app::highlight::HighlightState; use crate::state::app::state::AppState; use crate::state::pages::add_logic::{AddLogicFocus, AddLogicState}; use canvas::{render_canvas, FormEditor}; diff --git a/client/src/components/admin/add_table.rs b/client/src/components/admin/add_table.rs index d65641f..e545818 100644 --- a/client/src/components/admin/add_table.rs +++ b/client/src/components/admin/add_table.rs @@ -1,6 +1,5 @@ // src/components/admin/add_table.rs use crate::config::colors::themes::Theme; -use crate::state::app::highlight::HighlightState; use crate::state::app::state::AppState; use crate::state::pages::add_table::{AddTableFocus, AddTableState}; use canvas::{render_canvas_default, render_canvas, FormEditor}; diff --git a/client/src/components/auth/login.rs b/client/src/components/auth/login.rs index 263ff1d..39de80e 100644 --- a/client/src/components/auth/login.rs +++ b/client/src/components/auth/login.rs @@ -12,7 +12,6 @@ use ratatui::{ widgets::{Block, BorderType, Borders, Paragraph}, Frame, }; -use crate::state::app::highlight::HighlightState; use canvas::{ FormEditor, render_canvas, diff --git a/client/src/components/auth/register.rs b/client/src/components/auth/register.rs index c8401fb..254fc4e 100644 --- a/client/src/components/auth/register.rs +++ b/client/src/components/auth/register.rs @@ -13,18 +13,7 @@ use ratatui::{ widgets::{Block, BorderType, Borders, Paragraph}, Frame, }; -use crate::state::app::highlight::HighlightState; use canvas::{FormEditor, render_canvas_default, render_canvas, render_suggestions_dropdown, DefaultCanvasTheme}; -use canvas::canvas::HighlightState as CanvasHighlightState; - -// Helper function to convert between HighlightState types -fn convert_highlight_state(local: &HighlightState) -> CanvasHighlightState { - match local { - HighlightState::Off => CanvasHighlightState::Off, - HighlightState::Characterwise { anchor } => CanvasHighlightState::Characterwise { anchor: *anchor }, - HighlightState::Linewise { anchor_line } => CanvasHighlightState::Linewise { anchor_line: *anchor_line }, - } -} pub fn render_register( f: &mut Frame, diff --git a/client/src/modes/handlers/event.rs b/client/src/modes/handlers/event.rs index 97ab8fa..977f7e4 100644 --- a/client/src/modes/handlers/event.rs +++ b/client/src/modes/handlers/event.rs @@ -20,7 +20,6 @@ use canvas::{FormEditor, AppMode as CanvasMode}; use crate::state::{ app::{ buffer::{AppView, BufferState}, - highlight::HighlightState, search::SearchState, state::AppState, }, @@ -71,7 +70,6 @@ pub struct EventHandler { pub command_input: String, pub command_message: String, pub is_edit_mode: bool, - pub highlight_state: HighlightState, pub edit_mode_cooldown: bool, pub ideal_cursor_column: usize, pub key_sequence_tracker: KeySequenceTracker, @@ -103,7 +101,6 @@ impl EventHandler { command_input: String::new(), command_message: String::new(), is_edit_mode: false, - highlight_state: HighlightState::Off, edit_mode_cooldown: false, ideal_cursor_column: 0, key_sequence_tracker: KeySequenceTracker::new(400), @@ -653,45 +650,30 @@ impl EventHandler { } AppMode::ReadOnly => { - // Handle highlight mode transitions - if config.get_read_only_action_for_key(key_code, modifiers) == Some("enter_highlight_mode_linewise") + // Handle highlight mode transitions (delegated to FormEditor) + if config.get_read_only_action_for_key(key_code, modifiers) + == Some("enter_highlight_mode_linewise") && ModeManager::can_enter_highlight_mode(current_mode) { - let current_field_index = Self::get_current_field_for_state( - app_state, - login_state, - register_state, - form_state - ); - self.highlight_state = HighlightState::Linewise { - anchor_line: current_field_index - }; + if let Some(editor) = &mut app_state.form_editor { + editor.enter_highlight_line_mode(); + } self.command_message = "-- LINE HIGHLIGHT --".to_string(); return Ok(EventOutcome::Ok(self.command_message.clone())); - } - else if config.get_read_only_action_for_key(key_code, modifiers) == Some("enter_highlight_mode") + } else if config.get_read_only_action_for_key(key_code, modifiers) + == Some("enter_highlight_mode") && ModeManager::can_enter_highlight_mode(current_mode) { - let current_field_index = Self::get_current_field_for_state( - app_state, - login_state, - register_state, - form_state - ); - let current_cursor_pos = Self::get_current_cursor_pos_for_state( - app_state, - login_state, - register_state, - form_state - ); - let anchor = (current_field_index, current_cursor_pos); - self.highlight_state = HighlightState::Characterwise { anchor }; + if let Some(editor) = &mut app_state.form_editor { + editor.enter_highlight_mode(); + } self.command_message = "-- HIGHLIGHT --".to_string(); return Ok(EventOutcome::Ok(self.command_message.clone())); } // Handle edit mode transitions - else if config.get_read_only_action_for_key(key_code, modifiers).as_deref() == Some("enter_edit_mode_before") + else if config.get_read_only_action_for_key(key_code, modifiers).as_deref() + == Some("enter_edit_mode_before") && ModeManager::can_enter_edit_mode(current_mode) { if let Some(editor) = &mut app_state.form_editor { @@ -701,21 +683,18 @@ impl EventHandler { self.edit_mode_cooldown = true; self.command_message = "Edit mode".to_string(); return Ok(EventOutcome::Ok(self.command_message.clone())); - } - else if config.get_read_only_action_for_key(key_code, modifiers).as_deref() == Some("enter_edit_mode_after") + } else if config.get_read_only_action_for_key(key_code, modifiers).as_deref() + == Some("enter_edit_mode_after") && ModeManager::can_enter_edit_mode(current_mode) { let current_input = Self::get_current_input_for_state( app_state, login_state, register_state, - form_state - ); - let current_cursor_pos = Self::get_cursor_pos_for_mixed_state( - app_state, - login_state, - form_state + form_state, ); + let current_cursor_pos = + Self::get_cursor_pos_for_mixed_state(app_state, login_state, form_state); // Move cursor forward if possible if !current_input.is_empty() && current_cursor_pos < current_input.len() { @@ -725,13 +704,13 @@ impl EventHandler { login_state, register_state, form_state, - new_cursor_pos + new_cursor_pos, ); self.ideal_cursor_column = Self::get_current_cursor_pos_for_state( app_state, login_state, register_state, - form_state + form_state, ); } @@ -743,8 +722,8 @@ impl EventHandler { app_state.ui.focus_outside_canvas = false; self.command_message = "Edit mode (after cursor)".to_string(); return Ok(EventOutcome::Ok(self.command_message.clone())); - } - else if config.get_read_only_action_for_key(key_code, modifiers) == Some("enter_command_mode") + } else if config.get_read_only_action_for_key(key_code, modifiers) + == Some("enter_command_mode") && ModeManager::can_enter_command_mode(current_mode) { if let Some(editor) = &mut app_state.form_editor { @@ -779,12 +758,9 @@ impl EventHandler { // Try canvas action for form first if app_state.ui.show_form { if let Some(editor) = &mut app_state.form_editor { - if let Ok(Some(canvas_message)) = self.handle_form_canvas_action( - key_event, - editor, - config, - false, - ).await { + if let Ok(Some(canvas_message)) = + self.handle_form_canvas_action(key_event, editor, config, false).await + { return Ok(EventOutcome::Ok(canvas_message)); } } @@ -794,20 +770,22 @@ impl EventHandler { } AppMode::Highlight => { - if config.get_highlight_action_for_key(key_code, modifiers) == Some("exit_highlight_mode") { + if config.get_highlight_action_for_key(key_code, modifiers) + == Some("exit_highlight_mode") + { if let Some(editor) = &mut app_state.form_editor { editor.exit_highlight_mode(); } - self.highlight_state = HighlightState::Off; self.command_message = "Exited highlight mode".to_string(); return Ok(EventOutcome::Ok(self.command_message.clone())); - } else if config.get_highlight_action_for_key(key_code, modifiers) == Some("enter_highlight_mode_linewise") { - if let HighlightState::Characterwise { anchor } = self.highlight_state { - self.highlight_state = HighlightState::Linewise { anchor_line: anchor.0 }; - self.command_message = "-- LINE HIGHLIGHT --".to_string(); - return Ok(EventOutcome::Ok(self.command_message.clone())); + } else if config.get_highlight_action_for_key(key_code, modifiers) + == Some("enter_highlight_mode_linewise") + { + if let Some(editor) = &mut app_state.form_editor { + editor.enter_highlight_line_mode(); } - return Ok(EventOutcome::Ok("".to_string())); + self.command_message = "-- LINE HIGHLIGHT --".to_string(); + return Ok(EventOutcome::Ok(self.command_message.clone())); } return Ok(EventOutcome::Ok(self.command_message.clone())); diff --git a/client/src/modes/handlers/mode_manager.rs b/client/src/modes/handlers/mode_manager.rs index e02446d..15fbd10 100644 --- a/client/src/modes/handlers/mode_manager.rs +++ b/client/src/modes/handlers/mode_manager.rs @@ -2,8 +2,8 @@ use crate::state::app::state::AppState; use crate::modes::handlers::event::EventHandler; use crate::state::pages::add_logic::AddLogicFocus; -use crate::state::app::highlight::HighlightState; use crate::state::pages::admin::AdminState; +use canvas::AppMode as CanvasMode; #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum AppMode { @@ -31,8 +31,11 @@ impl ModeManager { return AppMode::Command; } - if !matches!(event_handler.highlight_state, HighlightState::Off) { - return AppMode::Highlight; + // NEW: delegate highlight detection to FormEditor + if let Some(editor) = &app_state.form_editor { + if editor.mode() == CanvasMode::Highlight { + return AppMode::Highlight; + } } let is_canvas_view = app_state.ui.show_login @@ -42,39 +45,33 @@ impl ModeManager { || app_state.ui.show_add_logic; if app_state.ui.show_add_logic { - // Specific logic for AddLogic view match admin_state.add_logic_state.current_focus { AddLogicFocus::InputLogicName - | AddLogicFocus::InputTargetColumn - | AddLogicFocus::InputDescription => { - // These are canvas inputs - if event_handler.is_edit_mode { - AppMode::Edit - } else { - AppMode::ReadOnly + | AddLogicFocus::InputTargetColumn + | AddLogicFocus::InputDescription => { + if event_handler.is_edit_mode { + AppMode::Edit + } else { + AppMode::ReadOnly + } } - } _ => AppMode::General, } } else if app_state.ui.show_add_table { if app_state.ui.focus_outside_canvas { AppMode::General + } else if event_handler.is_edit_mode { + AppMode::Edit } else { - if event_handler.is_edit_mode { - AppMode::Edit - } else { - AppMode::ReadOnly - } + AppMode::ReadOnly } } else if is_canvas_view { if app_state.ui.focus_outside_canvas { AppMode::General + } else if event_handler.is_edit_mode { + AppMode::Edit } else { - if event_handler.is_edit_mode { - AppMode::Edit - } else { - AppMode::ReadOnly - } + AppMode::ReadOnly } } else { AppMode::General diff --git a/client/src/state/app.rs b/client/src/state/app.rs index 8f3ce93..d3b27a6 100644 --- a/client/src/state/app.rs +++ b/client/src/state/app.rs @@ -3,4 +3,3 @@ pub mod state; pub mod buffer; pub mod search; -pub mod highlight; diff --git a/client/src/state/app/highlight.rs b/client/src/state/app/highlight.rs deleted file mode 100644 index 28f9968..0000000 --- a/client/src/state/app/highlight.rs +++ /dev/null @@ -1,20 +0,0 @@ -// src/state/app/highlight.rs - -/// Represents the different states of text highlighting. -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub enum HighlightState { - /// Highlighting is inactive. - Off, - /// Highlighting character by character. Stores the anchor point (line index, char index). - Characterwise { anchor: (usize, usize) }, - /// Highlighting line by line. Stores the anchor line index. - Linewise { anchor_line: usize }, -} - -impl Default for HighlightState { - /// The default state is no highlighting. - fn default() -> Self { - HighlightState::Off - } -} - diff --git a/client/src/state/pages/form.rs b/client/src/state/pages/form.rs index f3320a4..cded381 100644 --- a/client/src/state/pages/form.rs +++ b/client/src/state/pages/form.rs @@ -2,7 +2,6 @@ use crate::config::colors::themes::Theme; use canvas::{DataProvider, AppMode, EditorState, FormEditor}; -use canvas::canvas::HighlightState; use common::proto::komp_ac::search::search_response::Hit; use ratatui::layout::Rect; use ratatui::Frame; @@ -123,7 +122,6 @@ impl FormState { area: Rect, theme: &Theme, is_edit_mode: bool, - highlight_state: &HighlightState, ) { // Wrap in FormEditor for new API let mut editor = FormEditor::new(self.clone()); diff --git a/client/src/ui/handlers/render.rs b/client/src/ui/handlers/render.rs index 6740261..a528cf8 100644 --- a/client/src/ui/handlers/render.rs +++ b/client/src/ui/handlers/render.rs @@ -17,8 +17,6 @@ use crate::components::{ use crate::config::colors::themes::Theme; use crate::modes::general::command_navigation::NavigationState; use crate::state::app::buffer::BufferState; -use crate::state::app::highlight::HighlightState as LocalHighlightState; -use canvas::canvas::HighlightState as CanvasHighlightState; use crate::state::app::state::AppState; use crate::state::pages::admin::AdminState; use crate::state::pages::auth::AuthState; @@ -31,15 +29,6 @@ use ratatui::{ Frame, }; -// Helper function to convert between HighlightState types -fn convert_highlight_state(local: &LocalHighlightState) -> CanvasHighlightState { - match local { - LocalHighlightState::Off => CanvasHighlightState::Off, - LocalHighlightState::Characterwise { anchor } => CanvasHighlightState::Characterwise { anchor: *anchor }, - LocalHighlightState::Linewise { anchor_line } => CanvasHighlightState::Linewise { anchor_line: *anchor_line }, - } -} - #[allow(clippy::too_many_arguments)] pub fn render_ui( f: &mut Frame, @@ -52,7 +41,6 @@ pub fn render_ui( buffer_state: &BufferState, theme: &Theme, is_event_handler_edit_mode: bool, - highlight_state: &LocalHighlightState, // Keep using local version event_handler_command_input: &str, event_handler_command_mode_active: bool, event_handler_command_message: &str, @@ -205,13 +193,11 @@ pub fn render_ui( }; // CHANGED: Convert local HighlightState to canvas HighlightState for FormState - let canvas_highlight_state = convert_highlight_state(highlight_state); form_state.render( f, form_render_area, theme, is_event_handler_edit_mode, - &canvas_highlight_state, // Use converted version ); } diff --git a/client/src/ui/handlers/ui.rs b/client/src/ui/handlers/ui.rs index 818bbfd..96d3a7d 100644 --- a/client/src/ui/handlers/ui.rs +++ b/client/src/ui/handlers/ui.rs @@ -599,7 +599,6 @@ pub async fn run_ui() -> Result<()> { &buffer_state, &theme, event_handler.is_edit_mode, - &event_handler.highlight_state, &event_handler.command_input, event_handler.command_mode, &event_handler.command_message,