working entering to the edit mode

This commit is contained in:
Priec
2025-08-22 09:56:55 +02:00
parent 4ed8e7b421
commit ae8aa16208
2 changed files with 60 additions and 33 deletions

View File

@@ -665,19 +665,24 @@ impl EventHandler {
// First let the canvas editor try to handle the key // First let the canvas editor try to handle the key
if app_state.ui.show_form { if app_state.ui.show_form {
if let Some(editor) = &mut app_state.form_editor { if let Some(editor) = &mut app_state.form_editor {
match editor.handle_key_event(key_event) { let outcome = editor.handle_key_event(key_event);
let new_mode = AppMode::from(editor.mode());
match outcome {
KeyEventOutcome::Consumed(Some(msg)) => { KeyEventOutcome::Consumed(Some(msg)) => {
app_state.update_mode(new_mode);
return Ok(EventOutcome::Ok(msg)); return Ok(EventOutcome::Ok(msg));
} }
KeyEventOutcome::Consumed(None) => { KeyEventOutcome::Consumed(None) => {
app_state.update_mode(new_mode);
return Ok(EventOutcome::Ok(String::new())); return Ok(EventOutcome::Ok(String::new()));
} }
KeyEventOutcome::Pending => { KeyEventOutcome::Pending => {
// Waiting for multi-key sequence (e.g. after pressing 'g') app_state.update_mode(new_mode);
return Ok(EventOutcome::Ok(String::new())); return Ok(EventOutcome::Ok(String::new()));
} }
KeyEventOutcome::NotMatched => { KeyEventOutcome::NotMatched => {
// Fall through to client-level actions app_state.update_mode(new_mode);
// Fall through
} }
} }
} }
@@ -721,18 +726,24 @@ impl EventHandler {
AppMode::Highlight => { AppMode::Highlight => {
if app_state.ui.show_form { if app_state.ui.show_form {
if let Some(editor) = &mut app_state.form_editor { if let Some(editor) = &mut app_state.form_editor {
match editor.handle_key_event(key_event) { let outcome = editor.handle_key_event(key_event);
let new_mode = AppMode::from(editor.mode());
match outcome {
KeyEventOutcome::Consumed(Some(msg)) => { KeyEventOutcome::Consumed(Some(msg)) => {
app_state.update_mode(new_mode);
return Ok(EventOutcome::Ok(msg)); return Ok(EventOutcome::Ok(msg));
} }
KeyEventOutcome::Consumed(None) => { KeyEventOutcome::Consumed(None) => {
app_state.update_mode(new_mode);
return Ok(EventOutcome::Ok(String::new())); return Ok(EventOutcome::Ok(String::new()));
} }
KeyEventOutcome::Pending => { KeyEventOutcome::Pending => {
app_state.update_mode(new_mode);
return Ok(EventOutcome::Ok(String::new())); return Ok(EventOutcome::Ok(String::new()));
} }
KeyEventOutcome::NotMatched => { KeyEventOutcome::NotMatched => {
// Fall through to client-level actions app_state.update_mode(new_mode);
// Fall through
} }
} }
} }
@@ -764,20 +775,25 @@ impl EventHandler {
// Let the canvas editor handle edit-mode keys // Let the canvas editor handle edit-mode keys
if app_state.ui.show_form { if app_state.ui.show_form {
if let Some(editor) = &mut app_state.form_editor { if let Some(editor) = &mut app_state.form_editor {
match editor.handle_key_event(key_event) { let outcome = editor.handle_key_event(key_event);
let new_mode = AppMode::from(editor.mode());
match outcome {
KeyEventOutcome::Consumed(Some(msg)) => { KeyEventOutcome::Consumed(Some(msg)) => {
self.command_message = msg.clone(); self.command_message = msg.clone();
app_state.update_mode(new_mode);
return Ok(EventOutcome::Ok(msg)); return Ok(EventOutcome::Ok(msg));
} }
KeyEventOutcome::Consumed(None) => { KeyEventOutcome::Consumed(None) => {
app_state.update_mode(new_mode);
return Ok(EventOutcome::Ok(String::new())); return Ok(EventOutcome::Ok(String::new()));
} }
KeyEventOutcome::Pending => { KeyEventOutcome::Pending => {
// Rare in edit mode, but allow multi-key sequences if defined app_state.update_mode(new_mode);
return Ok(EventOutcome::Ok(String::new())); return Ok(EventOutcome::Ok(String::new()));
} }
KeyEventOutcome::NotMatched => { KeyEventOutcome::NotMatched => {
// Fall through to client-level actions app_state.update_mode(new_mode);
// Fall through
} }
} }
} }

View File

@@ -10,40 +10,47 @@ pub enum AppMode {
General, // For intro and admin screens General, // For intro and admin screens
ReadOnly, // Canvas read-only mode ReadOnly, // Canvas read-only mode
Edit, // Canvas edit mode Edit, // Canvas edit mode
Highlight, // Cnavas highlight/visual mode Highlight, // Canvas highlight/visual mode
Command, // Command mode overlay Command, // Command mode overlay
} }
impl From<canvas::AppMode> for AppMode {
fn from(mode: canvas::AppMode) -> Self {
match mode {
canvas::AppMode::General => AppMode::General,
canvas::AppMode::ReadOnly => AppMode::ReadOnly,
canvas::AppMode::Edit => AppMode::Edit,
canvas::AppMode::Highlight => AppMode::Highlight,
canvas::AppMode::Command => AppMode::Command,
}
}
}
pub struct ModeManager; pub struct ModeManager;
impl ModeManager { impl ModeManager {
// Determine current mode based on app state /// Determine current mode based on app state
pub fn derive_mode( pub fn derive_mode(
app_state: &AppState, app_state: &AppState,
event_handler: &EventHandler, event_handler: &EventHandler,
admin_state: &AdminState, admin_state: &AdminState,
) -> AppMode { ) -> AppMode {
// Navigation palette always forces General
if event_handler.navigation_state.active { if event_handler.navigation_state.active {
return AppMode::General; return AppMode::General;
} }
// Explicit command mode flag
if event_handler.command_mode { if event_handler.command_mode {
return AppMode::Command; return AppMode::Command;
} }
// NEW: delegate highlight detection to FormEditor // ✅ Always trust the FormEditor when a form is active
if let Some(editor) = &app_state.form_editor { if let Some(editor) = &app_state.form_editor {
if editor.mode() == CanvasMode::Highlight { return AppMode::from(editor.mode());
return AppMode::Highlight;
}
} }
let is_canvas_view = app_state.ui.show_login // --- Non-form views (add_logic, add_table, etc.) ---
|| app_state.ui.show_register
|| app_state.ui.show_form
|| app_state.ui.show_add_table
|| app_state.ui.show_add_logic;
if app_state.ui.show_add_logic { if app_state.ui.show_add_logic {
match admin_state.add_logic_state.current_focus { match admin_state.add_logic_state.current_focus {
AddLogicFocus::InputLogicName AddLogicFocus::InputLogicName
@@ -65,10 +72,11 @@ impl ModeManager {
} else { } else {
AppMode::ReadOnly AppMode::ReadOnly
} }
} else if is_canvas_view { } else if app_state.ui.show_login
if app_state.ui.focus_outside_canvas { || app_state.ui.show_register
AppMode::General {
} else if event_handler.is_edit_mode { // login/register still use the old flag
if event_handler.is_edit_mode {
AppMode::Edit AppMode::Edit
} else { } else {
AppMode::ReadOnly AppMode::ReadOnly
@@ -79,7 +87,7 @@ impl ModeManager {
} }
// Mode transition rules // Mode transition rules
pub fn can_enter_command_mode(current_mode: AppMode) -> bool { pub fn can_enter_command_mode(current_mode: AppMode) -> bool {
!matches!(current_mode, AppMode::Edit) !matches!(current_mode, AppMode::Edit)
} }
@@ -88,7 +96,10 @@ pub fn can_enter_command_mode(current_mode: AppMode) -> bool {
} }
pub fn can_enter_read_only_mode(current_mode: AppMode) -> bool { pub fn can_enter_read_only_mode(current_mode: AppMode) -> bool {
matches!(current_mode, AppMode::Edit | AppMode::Command | AppMode::Highlight) matches!(
current_mode,
AppMode::Edit | AppMode::Command | AppMode::Highlight
)
} }
pub fn can_enter_highlight_mode(current_mode: AppMode) -> bool { pub fn can_enter_highlight_mode(current_mode: AppMode) -> bool {