working general mode only with canvas, removing highlight, readonly or edit
This commit is contained in:
@@ -318,7 +318,7 @@ impl EventHandler {
|
||||
return Ok(EventOutcome::Ok(message));
|
||||
}
|
||||
|
||||
if !matches!(current_mode, AppMode::Edit | AppMode::Command) {
|
||||
if current_mode == AppMode::General {
|
||||
if let Some(action) = config.get_action_for_key_in_mode(
|
||||
&config.keybindings.global,
|
||||
key_code,
|
||||
@@ -352,9 +352,7 @@ impl EventHandler {
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(action) =
|
||||
config.get_general_action(key_code, modifiers)
|
||||
{
|
||||
if let Some(action) = config.get_general_action(key_code, modifiers) {
|
||||
if action == "open_search" {
|
||||
if let Page::Form(_) = &router.current {
|
||||
if let Some(table_name) =
|
||||
@@ -520,143 +518,35 @@ impl EventHandler {
|
||||
}
|
||||
}
|
||||
|
||||
AppMode::ReadOnly => {
|
||||
// First let the canvas editor try to handle the key
|
||||
if let Page::Form(_) = &router.current {
|
||||
if let Some(editor) = &mut app_state.form_editor {
|
||||
let outcome = editor.handle_key_event(key_event);
|
||||
let new_mode = AppMode::from(editor.mode());
|
||||
match outcome {
|
||||
KeyEventOutcome::Consumed(Some(msg)) => {
|
||||
app_state.update_mode(new_mode);
|
||||
return Ok(EventOutcome::Ok(msg));
|
||||
}
|
||||
KeyEventOutcome::Consumed(None) => {
|
||||
app_state.update_mode(new_mode);
|
||||
return Ok(EventOutcome::Ok(String::new()));
|
||||
}
|
||||
KeyEventOutcome::Pending => {
|
||||
app_state.update_mode(new_mode);
|
||||
return Ok(EventOutcome::Ok(String::new()));
|
||||
}
|
||||
KeyEventOutcome::NotMatched => {
|
||||
app_state.update_mode(new_mode);
|
||||
// Fall through
|
||||
AppMode::General => {
|
||||
match &router.current {
|
||||
Page::Form(_)
|
||||
| Page::Login(_)
|
||||
| Page::Register(_)
|
||||
| Page::AddTable(_)
|
||||
| Page::AddLogic(_) => {
|
||||
if !app_state.ui.focus_outside_canvas {
|
||||
if let Some(editor) = &mut app_state.form_editor {
|
||||
editor.set_keymap(config.build_canvas_keymap());
|
||||
match editor.handle_key_event(key_event) {
|
||||
KeyEventOutcome::Consumed(Some(msg)) => {
|
||||
return Ok(EventOutcome::Ok(msg));
|
||||
}
|
||||
KeyEventOutcome::Consumed(None) => {
|
||||
return Ok(EventOutcome::Ok(String::new()));
|
||||
}
|
||||
KeyEventOutcome::Pending => {
|
||||
return Ok(EventOutcome::Ok(String::new()));
|
||||
}
|
||||
KeyEventOutcome::NotMatched => {
|
||||
// fall through to client actions
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
// Entering command mode is still a client-level action
|
||||
if config.get_app_action(key_code, modifiers) == Some("enter_command_mode")
|
||||
&& ModeManager::can_enter_command_mode(current_mode)
|
||||
{
|
||||
if let Some(editor) = &mut app_state.form_editor {
|
||||
editor.set_mode(CanvasMode::Command);
|
||||
}
|
||||
self.command_mode = true;
|
||||
self.command_input.clear();
|
||||
self.command_message.clear();
|
||||
return Ok(EventOutcome::Ok(String::new()));
|
||||
}
|
||||
|
||||
// Handle common actions (save, quit, etc.)
|
||||
if let Some(action) = config.get_app_action(key_code, modifiers) {
|
||||
match action {
|
||||
"save" | "force_quit" | "save_and_quit" | "revert" => {
|
||||
return self
|
||||
.handle_core_action(
|
||||
action,
|
||||
auth_state,
|
||||
terminal,
|
||||
app_state,
|
||||
router,
|
||||
)
|
||||
.await;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
return Ok(EventOutcome::Ok(self.command_message.clone()));
|
||||
}
|
||||
|
||||
AppMode::Highlight => {
|
||||
if let Page::Form(_) = &router.current {
|
||||
if let Some(editor) = &mut app_state.form_editor {
|
||||
let outcome = editor.handle_key_event(key_event);
|
||||
let new_mode = AppMode::from(editor.mode());
|
||||
match outcome {
|
||||
KeyEventOutcome::Consumed(Some(msg)) => {
|
||||
app_state.update_mode(new_mode);
|
||||
return Ok(EventOutcome::Ok(msg));
|
||||
}
|
||||
KeyEventOutcome::Consumed(None) => {
|
||||
app_state.update_mode(new_mode);
|
||||
return Ok(EventOutcome::Ok(String::new()));
|
||||
}
|
||||
KeyEventOutcome::Pending => {
|
||||
app_state.update_mode(new_mode);
|
||||
return Ok(EventOutcome::Ok(String::new()));
|
||||
}
|
||||
KeyEventOutcome::NotMatched => {
|
||||
app_state.update_mode(new_mode);
|
||||
// Fall through
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return Ok(EventOutcome::Ok(self.command_message.clone()));
|
||||
}
|
||||
|
||||
AppMode::Edit => {
|
||||
// Handle common actions (save, quit, etc.)
|
||||
if let Some(action) = config.get_app_action(key_code, modifiers) {
|
||||
match action {
|
||||
"save" | "force_quit" | "save_and_quit" | "revert" => {
|
||||
return self
|
||||
.handle_core_action(
|
||||
action,
|
||||
auth_state,
|
||||
terminal,
|
||||
app_state,
|
||||
router,
|
||||
)
|
||||
.await;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
// Let the canvas editor handle edit-mode keys
|
||||
if let Page::Form(_) = &router.current {
|
||||
if let Some(editor) = &mut app_state.form_editor {
|
||||
let outcome = editor.handle_key_event(key_event);
|
||||
let new_mode = AppMode::from(editor.mode());
|
||||
match outcome {
|
||||
KeyEventOutcome::Consumed(Some(msg)) => {
|
||||
self.command_message = msg.clone();
|
||||
app_state.update_mode(new_mode);
|
||||
return Ok(EventOutcome::Ok(msg));
|
||||
}
|
||||
KeyEventOutcome::Consumed(None) => {
|
||||
app_state.update_mode(new_mode);
|
||||
return Ok(EventOutcome::Ok(String::new()));
|
||||
}
|
||||
KeyEventOutcome::Pending => {
|
||||
app_state.update_mode(new_mode);
|
||||
return Ok(EventOutcome::Ok(String::new()));
|
||||
}
|
||||
KeyEventOutcome::NotMatched => {
|
||||
app_state.update_mode(new_mode);
|
||||
// Fall through
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return Ok(EventOutcome::Ok(self.command_message.clone()));
|
||||
}
|
||||
|
||||
AppMode::Command => {
|
||||
|
||||
@@ -1,34 +1,27 @@
|
||||
// src/modes/handlers/mode_manager.rs
|
||||
|
||||
use crate::state::app::state::AppState;
|
||||
use crate::modes::handlers::event::EventHandler;
|
||||
use crate::state::pages::add_logic::AddLogicFocus;
|
||||
use crate::pages::routing::{Router, Page};
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum AppMode {
|
||||
General, // For intro and admin screens
|
||||
ReadOnly, // Canvas read-only mode
|
||||
Edit, // Canvas edit mode
|
||||
Highlight, // Canvas highlight/visual mode
|
||||
Command, // Command mode overlay
|
||||
}
|
||||
/// General mode = when focus is outside any canvas
|
||||
/// (Intro, Admin, Login/Register buttons, AddTable/AddLogic menus, dialogs, etc.)
|
||||
General,
|
||||
|
||||
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,
|
||||
}
|
||||
}
|
||||
/// Command overlay (":" or "ctrl+;"), available globally
|
||||
Command,
|
||||
}
|
||||
|
||||
pub struct ModeManager;
|
||||
|
||||
impl ModeManager {
|
||||
/// Determine current mode based on app state + router
|
||||
/// Determine current mode:
|
||||
/// - If navigation palette is active → General
|
||||
/// - If command overlay is active → Command
|
||||
/// - If focus is inside a canvas (Form, Login, Register, AddTable, AddLogic) → let canvas handle its own mode
|
||||
/// - Otherwise → General
|
||||
pub fn derive_mode(
|
||||
app_state: &AppState,
|
||||
event_handler: &EventHandler,
|
||||
@@ -39,76 +32,28 @@ impl ModeManager {
|
||||
return AppMode::General;
|
||||
}
|
||||
|
||||
// Explicit command mode flag
|
||||
// Explicit command overlay flag
|
||||
if event_handler.command_mode {
|
||||
return AppMode::Command;
|
||||
}
|
||||
|
||||
// If focus is inside a canvas, we don't duplicate canvas modes here.
|
||||
// Canvas crate owns ReadOnly/Edit/Highlight internally.
|
||||
match &router.current {
|
||||
// --- Form view ---
|
||||
Page::Form(_) if !app_state.ui.focus_outside_canvas => {
|
||||
if let Some(editor) = &app_state.form_editor {
|
||||
return AppMode::from(editor.mode());
|
||||
}
|
||||
Page::Form(_)
|
||||
| Page::Login(_)
|
||||
| Page::Register(_)
|
||||
| Page::AddTable(_)
|
||||
| Page::AddLogic(_) if !app_state.ui.focus_outside_canvas => {
|
||||
// Canvas active → let canvas handle its own AppMode
|
||||
AppMode::General
|
||||
}
|
||||
|
||||
// --- AddLogic view ---
|
||||
Page::AddLogic(state) => match state.current_focus {
|
||||
AddLogicFocus::InputLogicName
|
||||
| AddLogicFocus::InputTargetColumn
|
||||
| AddLogicFocus::InputDescription => {
|
||||
if event_handler.is_edit_mode {
|
||||
AppMode::Edit
|
||||
} else {
|
||||
AppMode::ReadOnly
|
||||
}
|
||||
}
|
||||
_ => AppMode::General,
|
||||
},
|
||||
|
||||
// --- AddTable view ---
|
||||
Page::AddTable(_) => {
|
||||
if app_state.ui.focus_outside_canvas {
|
||||
AppMode::General
|
||||
} else if event_handler.is_edit_mode {
|
||||
AppMode::Edit
|
||||
} else {
|
||||
AppMode::ReadOnly
|
||||
}
|
||||
}
|
||||
|
||||
// --- Login/Register views ---
|
||||
Page::Login(_) | Page::Register(_) => {
|
||||
if event_handler.is_edit_mode {
|
||||
AppMode::Edit
|
||||
} else {
|
||||
AppMode::ReadOnly
|
||||
}
|
||||
}
|
||||
|
||||
// --- Everything else (Intro, Admin, etc.) ---
|
||||
_ => AppMode::General,
|
||||
}
|
||||
}
|
||||
|
||||
// Mode transition rules
|
||||
pub fn can_enter_command_mode(current_mode: AppMode) -> bool {
|
||||
!matches!(current_mode, AppMode::Edit)
|
||||
}
|
||||
|
||||
pub fn can_enter_edit_mode(current_mode: AppMode) -> bool {
|
||||
matches!(current_mode, AppMode::ReadOnly)
|
||||
}
|
||||
|
||||
pub fn can_enter_read_only_mode(current_mode: AppMode) -> bool {
|
||||
matches!(
|
||||
current_mode,
|
||||
AppMode::Edit | AppMode::Command | AppMode::Highlight
|
||||
)
|
||||
}
|
||||
|
||||
pub fn can_enter_highlight_mode(current_mode: AppMode) -> bool {
|
||||
matches!(current_mode, AppMode::ReadOnly)
|
||||
/// Command overlay can be entered from anywhere (General or Canvas).
|
||||
pub fn can_enter_command_mode(_current_mode: AppMode) -> bool {
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user