Compare commits
10 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e4982f871f | ||
|
|
4e0338276f | ||
|
|
fe193f4f91 | ||
|
|
0011ba0c04 | ||
|
|
3c2eef9596 | ||
|
|
dac788351f | ||
|
|
8d5bc1296e | ||
|
|
969ad229e4 | ||
|
|
0d291fcf57 | ||
|
|
d711f4c491 |
2
Cargo.lock
generated
2
Cargo.lock
generated
@@ -482,6 +482,8 @@ dependencies = [
|
||||
"tokio",
|
||||
"tokio-test",
|
||||
"toml",
|
||||
"tracing",
|
||||
"tracing-subscriber",
|
||||
"unicode-width 0.2.0",
|
||||
]
|
||||
|
||||
|
||||
@@ -19,6 +19,9 @@ toml = { workspace = true }
|
||||
serde = { workspace = true }
|
||||
unicode-width.workspace = true
|
||||
|
||||
tracing = "0.1.41"
|
||||
tracing-subscriber = "0.3.19"
|
||||
|
||||
[dev-dependencies]
|
||||
tokio-test = "0.4.4"
|
||||
|
||||
|
||||
@@ -16,7 +16,7 @@ highlight_current_field = true
|
||||
move_left = ["h"]
|
||||
move_right = ["l"]
|
||||
move_up = ["k"]
|
||||
move_down = ["p"]
|
||||
move_down = ["j"]
|
||||
move_word_next = ["w"]
|
||||
move_word_end = ["e"]
|
||||
move_word_prev = ["b"]
|
||||
@@ -42,6 +42,8 @@ move_word_next = ["Ctrl+Right"]
|
||||
move_word_prev = ["Ctrl+Left"]
|
||||
next_field = ["Tab"]
|
||||
prev_field = ["Shift+Tab"]
|
||||
trigger_autocomplete = ["Ctrl+p"]
|
||||
|
||||
|
||||
# Suggestion/autocomplete keybindings
|
||||
[keybindings.suggestions]
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
// src/autocomplete/actions.rs
|
||||
// canvas/src/autocomplete/actions.rs
|
||||
|
||||
use crate::canvas::state::{CanvasState, ActionContext};
|
||||
use crate::autocomplete::state::AutocompleteCanvasState;
|
||||
use crate::canvas::actions::types::{CanvasAction, ActionResult};
|
||||
use crate::canvas::actions::edit::handle_generic_canvas_action; // Import the core function
|
||||
use crate::canvas::actions::edit::handle_generic_canvas_action;
|
||||
use crate::config::CanvasConfig;
|
||||
use anyhow::Result;
|
||||
|
||||
/// Version for states that implement rich autocomplete
|
||||
@@ -11,6 +12,7 @@ pub async fn execute_canvas_action_with_autocomplete<S: CanvasState + Autocomple
|
||||
action: CanvasAction,
|
||||
state: &mut S,
|
||||
ideal_cursor_column: &mut usize,
|
||||
config: Option<&CanvasConfig>,
|
||||
) -> Result<ActionResult> {
|
||||
// 1. Try feature-specific handler first
|
||||
let context = ActionContext {
|
||||
@@ -20,31 +22,77 @@ pub async fn execute_canvas_action_with_autocomplete<S: CanvasState + Autocomple
|
||||
current_field: state.current_field(),
|
||||
};
|
||||
|
||||
if let Some(result) = state.handle_feature_action(&action, &context) {
|
||||
return Ok(ActionResult::HandledByFeature(result));
|
||||
}
|
||||
|
||||
// 2. Handle rich autocomplete actions
|
||||
if let Some(result) = handle_rich_autocomplete_action(&action, state)? {
|
||||
if let Some(result) = handle_rich_autocomplete_action(action.clone(), state, &context) {
|
||||
return Ok(result);
|
||||
}
|
||||
|
||||
// 3. Handle generic canvas actions
|
||||
handle_generic_canvas_action(action, state, ideal_cursor_column).await
|
||||
// 2. Handle generic actions and add auto-trigger logic
|
||||
let result = handle_generic_canvas_action(action.clone(), state, ideal_cursor_column, config).await?;
|
||||
|
||||
// 3. AUTO-TRIGGER LOGIC: Check if we should activate/deactivate autocomplete
|
||||
if let Some(cfg) = config {
|
||||
println!("{:?}, {}", action, cfg.should_auto_trigger_autocomplete());
|
||||
if cfg.should_auto_trigger_autocomplete() {
|
||||
println!("AUTO-TRIGGER");
|
||||
match action {
|
||||
CanvasAction::InsertChar(_) => {
|
||||
println!("AUTO-T on Ins");
|
||||
let current_field = state.current_field();
|
||||
let current_input = state.get_current_input();
|
||||
|
||||
if state.supports_autocomplete(current_field)
|
||||
&& !state.is_autocomplete_active()
|
||||
&& current_input.len() >= 1
|
||||
{
|
||||
println!("ACT AUTOC");
|
||||
state.activate_autocomplete();
|
||||
}
|
||||
}
|
||||
|
||||
CanvasAction::NextField | CanvasAction::PrevField => {
|
||||
println!("AUTO-T on nav");
|
||||
let current_field = state.current_field();
|
||||
|
||||
if state.supports_autocomplete(current_field) && !state.is_autocomplete_active() {
|
||||
state.activate_autocomplete();
|
||||
} else if !state.supports_autocomplete(current_field) && state.is_autocomplete_active() {
|
||||
state.deactivate_autocomplete();
|
||||
}
|
||||
}
|
||||
|
||||
_ => {} // No auto-trigger for other actions
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
/// Handle rich autocomplete actions for AutocompleteCanvasState
|
||||
fn handle_rich_autocomplete_action<S: CanvasState + AutocompleteCanvasState>(
|
||||
action: &CanvasAction,
|
||||
action: CanvasAction,
|
||||
state: &mut S,
|
||||
) -> Result<Option<ActionResult>> {
|
||||
_context: &ActionContext,
|
||||
) -> Option<ActionResult> {
|
||||
match action {
|
||||
CanvasAction::TriggerAutocomplete => {
|
||||
if state.supports_autocomplete(state.current_field()) {
|
||||
let current_field = state.current_field();
|
||||
if state.supports_autocomplete(current_field) {
|
||||
state.activate_autocomplete();
|
||||
Ok(Some(ActionResult::success_with_message("Autocomplete activated - fetching suggestions...")))
|
||||
Some(ActionResult::success_with_message("Autocomplete activated"))
|
||||
} else {
|
||||
Ok(Some(ActionResult::error("Autocomplete not supported for this field")))
|
||||
Some(ActionResult::success_with_message("Autocomplete not supported for this field"))
|
||||
}
|
||||
}
|
||||
|
||||
CanvasAction::SuggestionUp => {
|
||||
if state.is_autocomplete_ready() {
|
||||
if let Some(autocomplete_state) = state.autocomplete_state_mut() {
|
||||
autocomplete_state.select_previous();
|
||||
}
|
||||
Some(ActionResult::success())
|
||||
} else {
|
||||
Some(ActionResult::success_with_message("No suggestions available"))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -52,42 +100,34 @@ fn handle_rich_autocomplete_action<S: CanvasState + AutocompleteCanvasState>(
|
||||
if state.is_autocomplete_ready() {
|
||||
if let Some(autocomplete_state) = state.autocomplete_state_mut() {
|
||||
autocomplete_state.select_next();
|
||||
return Ok(Some(ActionResult::success()));
|
||||
}
|
||||
Some(ActionResult::success())
|
||||
} else {
|
||||
Some(ActionResult::success_with_message("No suggestions available"))
|
||||
}
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
CanvasAction::SuggestionUp => {
|
||||
if state.is_autocomplete_ready() {
|
||||
if let Some(autocomplete_state) = state.autocomplete_state_mut() {
|
||||
autocomplete_state.select_previous();
|
||||
return Ok(Some(ActionResult::success()));
|
||||
}
|
||||
}
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
CanvasAction::SelectSuggestion => {
|
||||
if state.is_autocomplete_ready() {
|
||||
if let Some(message) = state.apply_autocomplete_selection() {
|
||||
return Ok(Some(ActionResult::success_with_message(message)));
|
||||
if let Some(msg) = state.apply_autocomplete_selection() {
|
||||
Some(ActionResult::success_with_message(&msg))
|
||||
} else {
|
||||
return Ok(Some(ActionResult::error("No suggestion selected")));
|
||||
Some(ActionResult::success_with_message("No suggestion selected"))
|
||||
}
|
||||
} else {
|
||||
Some(ActionResult::success_with_message("No suggestions available"))
|
||||
}
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
CanvasAction::ExitSuggestions => {
|
||||
if state.is_autocomplete_active() {
|
||||
state.deactivate_autocomplete();
|
||||
Ok(Some(ActionResult::success_with_message("Autocomplete cancelled")))
|
||||
Some(ActionResult::success_with_message("Exited autocomplete"))
|
||||
} else {
|
||||
Ok(None)
|
||||
Some(ActionResult::success())
|
||||
}
|
||||
}
|
||||
|
||||
_ => Ok(None),
|
||||
_ => None, // Not a rich autocomplete action
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,8 +56,6 @@ fn render_loading_indicator<T: CanvasTheme>(
|
||||
);
|
||||
|
||||
let loading_block = Block::default()
|
||||
.borders(Borders::ALL)
|
||||
.border_style(Style::default().fg(theme.accent()))
|
||||
.style(Style::default().bg(theme.bg()));
|
||||
|
||||
let loading_paragraph = Paragraph::new(loading_text)
|
||||
@@ -92,8 +90,6 @@ fn render_suggestions_dropdown<T: CanvasTheme>(
|
||||
|
||||
// Background
|
||||
let dropdown_block = Block::default()
|
||||
.borders(Borders::ALL)
|
||||
.border_style(Style::default().fg(theme.accent()))
|
||||
.style(Style::default().bg(theme.bg()));
|
||||
|
||||
// List items
|
||||
@@ -111,7 +107,7 @@ fn render_suggestions_dropdown<T: CanvasTheme>(
|
||||
f.render_stateful_widget(list, dropdown_area, &mut list_state);
|
||||
}
|
||||
|
||||
/// Calculate dropdown size based on suggestions
|
||||
/// Calculate dropdown size based on suggestions - updated to match client dimensions
|
||||
#[cfg(feature = "gui")]
|
||||
fn calculate_dropdown_dimensions(display_texts: &[&str]) -> DropdownDimensions {
|
||||
let max_width = display_texts
|
||||
@@ -120,9 +116,9 @@ fn calculate_dropdown_dimensions(display_texts: &[&str]) -> DropdownDimensions {
|
||||
.max()
|
||||
.unwrap_or(0) as u16;
|
||||
|
||||
let horizontal_padding = 4; // borders + padding
|
||||
let width = (max_width + horizontal_padding).max(12);
|
||||
let height = (display_texts.len() as u16).min(8) + 2; // max 8 visible items + borders
|
||||
let horizontal_padding = 2; // Changed from 4 to 2 to match client
|
||||
let width = (max_width + horizontal_padding).max(10); // Changed from 12 to 10 to match client
|
||||
let height = (display_texts.len() as u16).min(5); // Removed +2 since no borders
|
||||
|
||||
DropdownDimensions { width, height }
|
||||
}
|
||||
@@ -155,7 +151,7 @@ fn calculate_dropdown_position(
|
||||
dropdown_area
|
||||
}
|
||||
|
||||
/// Create styled list items
|
||||
/// Create styled list items - updated to match client spacing
|
||||
#[cfg(feature = "gui")]
|
||||
fn create_suggestion_list_items<'a, T: CanvasTheme>(
|
||||
display_texts: &'a [&'a str],
|
||||
@@ -163,8 +159,8 @@ fn create_suggestion_list_items<'a, T: CanvasTheme>(
|
||||
dropdown_width: u16,
|
||||
theme: &T,
|
||||
) -> Vec<ListItem<'a>> {
|
||||
let horizontal_padding = 4;
|
||||
let available_width = dropdown_width.saturating_sub(horizontal_padding);
|
||||
let horizontal_padding = 2; // Changed from 4 to 2 to match client
|
||||
let available_width = dropdown_width; // No border padding needed
|
||||
|
||||
display_texts
|
||||
.iter()
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
// canvas/src/actions/edit.rs
|
||||
// canvas/src/canvas/actions/edit.rs
|
||||
|
||||
use crate::canvas::state::{CanvasState, ActionContext};
|
||||
use crate::canvas::actions::types::{CanvasAction, ActionResult};
|
||||
use crate::config::CanvasConfig;
|
||||
use anyhow::Result;
|
||||
|
||||
/// Execute a typed canvas action on any CanvasState implementation
|
||||
@@ -9,6 +10,7 @@ pub async fn execute_canvas_action<S: CanvasState>(
|
||||
action: CanvasAction,
|
||||
state: &mut S,
|
||||
ideal_cursor_column: &mut usize,
|
||||
config: Option<&CanvasConfig>,
|
||||
) -> Result<ActionResult> {
|
||||
let context = ActionContext {
|
||||
key_code: None,
|
||||
@@ -21,7 +23,7 @@ pub async fn execute_canvas_action<S: CanvasState>(
|
||||
return Ok(ActionResult::HandledByFeature(result));
|
||||
}
|
||||
|
||||
handle_generic_canvas_action(action, state, ideal_cursor_column).await
|
||||
handle_generic_canvas_action(action, state, ideal_cursor_column, config).await
|
||||
}
|
||||
|
||||
/// Handle core canvas actions with full type safety
|
||||
@@ -29,126 +31,84 @@ pub async fn handle_generic_canvas_action<S: CanvasState>(
|
||||
action: CanvasAction,
|
||||
state: &mut S,
|
||||
ideal_cursor_column: &mut usize,
|
||||
config: Option<&CanvasConfig>,
|
||||
) -> Result<ActionResult> {
|
||||
match action {
|
||||
CanvasAction::InsertChar(c) => {
|
||||
let cursor_pos = state.current_cursor_pos();
|
||||
let field_value = state.get_current_input_mut();
|
||||
let mut chars: Vec<char> = field_value.chars().collect();
|
||||
let input = state.get_current_input_mut();
|
||||
input.insert(cursor_pos, c);
|
||||
state.set_current_cursor_pos(cursor_pos + 1);
|
||||
state.set_has_unsaved_changes(true);
|
||||
*ideal_cursor_column = cursor_pos + 1;
|
||||
Ok(ActionResult::success())
|
||||
}
|
||||
|
||||
if cursor_pos <= chars.len() {
|
||||
chars.insert(cursor_pos, c);
|
||||
*field_value = chars.into_iter().collect();
|
||||
state.set_current_cursor_pos(cursor_pos + 1);
|
||||
state.set_has_unsaved_changes(true);
|
||||
*ideal_cursor_column = state.current_cursor_pos();
|
||||
Ok(ActionResult::success())
|
||||
} else {
|
||||
Ok(ActionResult::error("Invalid cursor position for character insertion"))
|
||||
}
|
||||
CanvasAction::NextField | CanvasAction::PrevField => {
|
||||
let old_field = state.current_field();
|
||||
let total_fields = state.fields().len();
|
||||
|
||||
// Perform field navigation
|
||||
let new_field = match action {
|
||||
CanvasAction::NextField => {
|
||||
if config.map_or(true, |c| c.behavior.wrap_around_fields) {
|
||||
(old_field + 1) % total_fields
|
||||
} else {
|
||||
(old_field + 1).min(total_fields - 1)
|
||||
}
|
||||
}
|
||||
CanvasAction::PrevField => {
|
||||
if config.map_or(true, |c| c.behavior.wrap_around_fields) {
|
||||
if old_field == 0 { total_fields - 1 } else { old_field - 1 }
|
||||
} else {
|
||||
old_field.saturating_sub(1)
|
||||
}
|
||||
}
|
||||
_ => unreachable!(),
|
||||
};
|
||||
|
||||
state.set_current_field(new_field);
|
||||
*ideal_cursor_column = state.current_cursor_pos();
|
||||
Ok(ActionResult::success())
|
||||
}
|
||||
|
||||
CanvasAction::DeleteBackward => {
|
||||
if state.current_cursor_pos() > 0 {
|
||||
let cursor_pos = state.current_cursor_pos();
|
||||
let field_value = state.get_current_input_mut();
|
||||
let mut chars: Vec<char> = field_value.chars().collect();
|
||||
|
||||
if cursor_pos <= chars.len() {
|
||||
chars.remove(cursor_pos - 1);
|
||||
*field_value = chars.into_iter().collect();
|
||||
let new_pos = cursor_pos - 1;
|
||||
state.set_current_cursor_pos(new_pos);
|
||||
state.set_has_unsaved_changes(true);
|
||||
*ideal_cursor_column = new_pos;
|
||||
}
|
||||
let cursor_pos = state.current_cursor_pos();
|
||||
if cursor_pos > 0 {
|
||||
let input = state.get_current_input_mut();
|
||||
input.remove(cursor_pos - 1);
|
||||
state.set_current_cursor_pos(cursor_pos - 1);
|
||||
state.set_has_unsaved_changes(true);
|
||||
*ideal_cursor_column = cursor_pos - 1;
|
||||
}
|
||||
Ok(ActionResult::success())
|
||||
}
|
||||
|
||||
CanvasAction::DeleteForward => {
|
||||
let cursor_pos = state.current_cursor_pos();
|
||||
let field_value = state.get_current_input_mut();
|
||||
let mut chars: Vec<char> = field_value.chars().collect();
|
||||
|
||||
if cursor_pos < chars.len() {
|
||||
chars.remove(cursor_pos);
|
||||
*field_value = chars.into_iter().collect();
|
||||
let input = state.get_current_input_mut();
|
||||
if cursor_pos < input.len() {
|
||||
input.remove(cursor_pos);
|
||||
state.set_has_unsaved_changes(true);
|
||||
*ideal_cursor_column = cursor_pos;
|
||||
}
|
||||
Ok(ActionResult::success())
|
||||
}
|
||||
|
||||
CanvasAction::NextField => {
|
||||
let num_fields = state.fields().len();
|
||||
if num_fields > 0 {
|
||||
let current_field = state.current_field();
|
||||
let new_field = (current_field + 1) % num_fields;
|
||||
state.set_current_field(new_field);
|
||||
let current_input = state.get_current_input();
|
||||
let max_pos = current_input.len();
|
||||
state.set_current_cursor_pos((*ideal_cursor_column).min(max_pos));
|
||||
}
|
||||
Ok(ActionResult::success())
|
||||
}
|
||||
|
||||
CanvasAction::PrevField => {
|
||||
let num_fields = state.fields().len();
|
||||
if num_fields > 0 {
|
||||
let current_field = state.current_field();
|
||||
let new_field = if current_field == 0 {
|
||||
num_fields - 1
|
||||
} else {
|
||||
current_field - 1
|
||||
};
|
||||
state.set_current_field(new_field);
|
||||
let current_input = state.get_current_input();
|
||||
let max_pos = current_input.len();
|
||||
state.set_current_cursor_pos((*ideal_cursor_column).min(max_pos));
|
||||
}
|
||||
Ok(ActionResult::success())
|
||||
}
|
||||
|
||||
CanvasAction::MoveLeft => {
|
||||
let new_pos = state.current_cursor_pos().saturating_sub(1);
|
||||
state.set_current_cursor_pos(new_pos);
|
||||
*ideal_cursor_column = new_pos;
|
||||
let cursor_pos = state.current_cursor_pos();
|
||||
if cursor_pos > 0 {
|
||||
state.set_current_cursor_pos(cursor_pos - 1);
|
||||
*ideal_cursor_column = cursor_pos - 1;
|
||||
}
|
||||
Ok(ActionResult::success())
|
||||
}
|
||||
|
||||
CanvasAction::MoveRight => {
|
||||
let cursor_pos = state.current_cursor_pos();
|
||||
let current_input = state.get_current_input();
|
||||
let current_pos = state.current_cursor_pos();
|
||||
if current_pos < current_input.len() {
|
||||
let new_pos = current_pos + 1;
|
||||
state.set_current_cursor_pos(new_pos);
|
||||
*ideal_cursor_column = new_pos;
|
||||
}
|
||||
Ok(ActionResult::success())
|
||||
}
|
||||
|
||||
CanvasAction::MoveUp => {
|
||||
let num_fields = state.fields().len();
|
||||
if num_fields > 0 {
|
||||
let current_field = state.current_field();
|
||||
let new_field = current_field.saturating_sub(1);
|
||||
state.set_current_field(new_field);
|
||||
let current_input = state.get_current_input();
|
||||
let max_pos = current_input.len();
|
||||
state.set_current_cursor_pos((*ideal_cursor_column).min(max_pos));
|
||||
}
|
||||
Ok(ActionResult::success())
|
||||
}
|
||||
|
||||
CanvasAction::MoveDown => {
|
||||
let num_fields = state.fields().len();
|
||||
if num_fields > 0 {
|
||||
let new_field = (state.current_field() + 1).min(num_fields - 1);
|
||||
state.set_current_field(new_field);
|
||||
let current_input = state.get_current_input();
|
||||
let max_pos = current_input.len();
|
||||
state.set_current_cursor_pos((*ideal_cursor_column).min(max_pos));
|
||||
if cursor_pos < current_input.len() {
|
||||
state.set_current_cursor_pos(cursor_pos + 1);
|
||||
*ideal_cursor_column = cursor_pos + 1;
|
||||
}
|
||||
Ok(ActionResult::success())
|
||||
}
|
||||
@@ -160,43 +120,55 @@ pub async fn handle_generic_canvas_action<S: CanvasState>(
|
||||
}
|
||||
|
||||
CanvasAction::MoveLineEnd => {
|
||||
let current_input = state.get_current_input();
|
||||
let new_pos = current_input.len();
|
||||
state.set_current_cursor_pos(new_pos);
|
||||
*ideal_cursor_column = new_pos;
|
||||
let end_pos = state.get_current_input().len();
|
||||
state.set_current_cursor_pos(end_pos);
|
||||
*ideal_cursor_column = end_pos;
|
||||
Ok(ActionResult::success())
|
||||
}
|
||||
|
||||
CanvasAction::MoveUp => {
|
||||
// For single-line fields, move to previous field
|
||||
let current_field = state.current_field();
|
||||
if current_field > 0 {
|
||||
state.set_current_field(current_field - 1);
|
||||
*ideal_cursor_column = state.current_cursor_pos();
|
||||
}
|
||||
Ok(ActionResult::success())
|
||||
}
|
||||
|
||||
CanvasAction::MoveDown => {
|
||||
// For single-line fields, move to next field
|
||||
let current_field = state.current_field();
|
||||
let total_fields = state.fields().len();
|
||||
if current_field < total_fields - 1 {
|
||||
state.set_current_field(current_field + 1);
|
||||
*ideal_cursor_column = state.current_cursor_pos();
|
||||
}
|
||||
Ok(ActionResult::success())
|
||||
}
|
||||
|
||||
CanvasAction::MoveFirstLine => {
|
||||
let num_fields = state.fields().len();
|
||||
if num_fields > 0 {
|
||||
state.set_current_field(0);
|
||||
let current_input = state.get_current_input();
|
||||
let max_pos = current_input.len();
|
||||
state.set_current_cursor_pos((*ideal_cursor_column).min(max_pos));
|
||||
}
|
||||
Ok(ActionResult::success_with_message("Moved to first field"))
|
||||
state.set_current_field(0);
|
||||
state.set_current_cursor_pos(0);
|
||||
*ideal_cursor_column = 0;
|
||||
Ok(ActionResult::success())
|
||||
}
|
||||
|
||||
CanvasAction::MoveLastLine => {
|
||||
let num_fields = state.fields().len();
|
||||
if num_fields > 0 {
|
||||
let new_field = num_fields - 1;
|
||||
state.set_current_field(new_field);
|
||||
let current_input = state.get_current_input();
|
||||
let max_pos = current_input.len();
|
||||
state.set_current_cursor_pos((*ideal_cursor_column).min(max_pos));
|
||||
}
|
||||
Ok(ActionResult::success_with_message("Moved to last field"))
|
||||
let last_field = state.fields().len() - 1;
|
||||
state.set_current_field(last_field);
|
||||
let end_pos = state.get_current_input().len();
|
||||
state.set_current_cursor_pos(end_pos);
|
||||
*ideal_cursor_column = end_pos;
|
||||
Ok(ActionResult::success())
|
||||
}
|
||||
|
||||
CanvasAction::MoveWordNext => {
|
||||
let current_input = state.get_current_input();
|
||||
if !current_input.is_empty() {
|
||||
let new_pos = find_next_word_start(current_input, state.current_cursor_pos());
|
||||
let final_pos = new_pos.min(current_input.len());
|
||||
state.set_current_cursor_pos(final_pos);
|
||||
*ideal_cursor_column = final_pos;
|
||||
state.set_current_cursor_pos(new_pos);
|
||||
*ideal_cursor_column = new_pos;
|
||||
}
|
||||
Ok(ActionResult::success())
|
||||
}
|
||||
@@ -204,19 +176,9 @@ pub async fn handle_generic_canvas_action<S: CanvasState>(
|
||||
CanvasAction::MoveWordEnd => {
|
||||
let current_input = state.get_current_input();
|
||||
if !current_input.is_empty() {
|
||||
let current_pos = state.current_cursor_pos();
|
||||
let new_pos = find_word_end(current_input, current_pos);
|
||||
|
||||
let final_pos = if new_pos == current_pos {
|
||||
find_word_end(current_input, new_pos + 1)
|
||||
} else {
|
||||
new_pos
|
||||
};
|
||||
|
||||
let max_valid_index = current_input.len().saturating_sub(1);
|
||||
let clamped_pos = final_pos.min(max_valid_index);
|
||||
state.set_current_cursor_pos(clamped_pos);
|
||||
*ideal_cursor_column = clamped_pos;
|
||||
let new_pos = find_word_end(current_input, state.current_cursor_pos());
|
||||
state.set_current_cursor_pos(new_pos);
|
||||
*ideal_cursor_column = new_pos;
|
||||
}
|
||||
Ok(ActionResult::success())
|
||||
}
|
||||
@@ -231,148 +193,61 @@ pub async fn handle_generic_canvas_action<S: CanvasState>(
|
||||
Ok(ActionResult::success())
|
||||
}
|
||||
|
||||
CanvasAction::MoveWordEndPrev => {
|
||||
let current_input = state.get_current_input();
|
||||
if !current_input.is_empty() {
|
||||
let new_pos = find_prev_word_end(current_input, state.current_cursor_pos());
|
||||
state.set_current_cursor_pos(new_pos);
|
||||
*ideal_cursor_column = new_pos;
|
||||
}
|
||||
Ok(ActionResult::success_with_message("Moved to previous word end"))
|
||||
}
|
||||
|
||||
CanvasAction::Custom(action_str) => {
|
||||
Ok(ActionResult::error(format!("Unknown or unhandled custom action: {}", action_str)))
|
||||
Ok(ActionResult::success_with_message(&format!("Custom action: {}", action_str)))
|
||||
}
|
||||
|
||||
// Autocomplete actions are handled by the autocomplete module
|
||||
CanvasAction::TriggerAutocomplete | CanvasAction::SuggestionUp | CanvasAction::SuggestionDown |
|
||||
CanvasAction::SelectSuggestion | CanvasAction::ExitSuggestions => {
|
||||
Ok(ActionResult::error("Autocomplete actions should be handled by autocomplete module"))
|
||||
}
|
||||
_ => Ok(ActionResult::success_with_message("Action not implemented")),
|
||||
}
|
||||
}
|
||||
|
||||
// Word movement helper functions
|
||||
#[derive(PartialEq)]
|
||||
enum CharType {
|
||||
Whitespace,
|
||||
Alphanumeric,
|
||||
Punctuation,
|
||||
}
|
||||
|
||||
fn get_char_type(c: char) -> CharType {
|
||||
if c.is_whitespace() {
|
||||
CharType::Whitespace
|
||||
} else if c.is_alphanumeric() {
|
||||
CharType::Alphanumeric
|
||||
} else {
|
||||
CharType::Punctuation
|
||||
}
|
||||
}
|
||||
|
||||
fn find_next_word_start(text: &str, current_pos: usize) -> usize {
|
||||
// Helper functions for word navigation
|
||||
fn find_next_word_start(text: &str, cursor_pos: usize) -> usize {
|
||||
let chars: Vec<char> = text.chars().collect();
|
||||
let len = chars.len();
|
||||
if len == 0 || current_pos >= len {
|
||||
return len;
|
||||
}
|
||||
|
||||
let mut pos = current_pos;
|
||||
let initial_type = get_char_type(chars[pos]);
|
||||
|
||||
while pos < len && get_char_type(chars[pos]) == initial_type {
|
||||
let mut pos = cursor_pos;
|
||||
|
||||
// Skip current word
|
||||
while pos < chars.len() && chars[pos].is_alphanumeric() {
|
||||
pos += 1;
|
||||
}
|
||||
|
||||
while pos < len && get_char_type(chars[pos]) == CharType::Whitespace {
|
||||
|
||||
// Skip whitespace
|
||||
while pos < chars.len() && chars[pos].is_whitespace() {
|
||||
pos += 1;
|
||||
}
|
||||
|
||||
|
||||
pos
|
||||
}
|
||||
|
||||
fn find_word_end(text: &str, current_pos: usize) -> usize {
|
||||
fn find_word_end(text: &str, cursor_pos: usize) -> usize {
|
||||
let chars: Vec<char> = text.chars().collect();
|
||||
let len = chars.len();
|
||||
if len == 0 {
|
||||
return 0;
|
||||
}
|
||||
|
||||
let mut pos = current_pos.min(len - 1);
|
||||
|
||||
if get_char_type(chars[pos]) == CharType::Whitespace {
|
||||
pos = find_next_word_start(text, pos);
|
||||
}
|
||||
|
||||
if pos >= len {
|
||||
return len.saturating_sub(1);
|
||||
}
|
||||
|
||||
let word_type = get_char_type(chars[pos]);
|
||||
while pos < len && get_char_type(chars[pos]) == word_type {
|
||||
let mut pos = cursor_pos;
|
||||
|
||||
// Move to end of current word
|
||||
while pos < chars.len() && chars[pos].is_alphanumeric() {
|
||||
pos += 1;
|
||||
}
|
||||
|
||||
pos.saturating_sub(1).min(len.saturating_sub(1))
|
||||
}
|
||||
|
||||
fn find_prev_word_start(text: &str, current_pos: usize) -> usize {
|
||||
let chars: Vec<char> = text.chars().collect();
|
||||
if chars.is_empty() || current_pos == 0 {
|
||||
return 0;
|
||||
}
|
||||
|
||||
let mut pos = current_pos.saturating_sub(1);
|
||||
|
||||
while pos > 0 && get_char_type(chars[pos]) == CharType::Whitespace {
|
||||
pos -= 1;
|
||||
}
|
||||
|
||||
if pos == 0 && get_char_type(chars[pos]) == CharType::Whitespace {
|
||||
return 0;
|
||||
}
|
||||
|
||||
let word_type = get_char_type(chars[pos]);
|
||||
while pos > 0 && get_char_type(chars[pos - 1]) == word_type {
|
||||
pos -= 1;
|
||||
}
|
||||
|
||||
|
||||
pos
|
||||
}
|
||||
|
||||
fn find_prev_word_end(text: &str, current_pos: usize) -> usize {
|
||||
fn find_prev_word_start(text: &str, cursor_pos: usize) -> usize {
|
||||
if cursor_pos == 0 {
|
||||
return 0;
|
||||
}
|
||||
|
||||
let chars: Vec<char> = text.chars().collect();
|
||||
let len = chars.len();
|
||||
if len == 0 || current_pos == 0 {
|
||||
return 0;
|
||||
}
|
||||
|
||||
let mut pos = current_pos.saturating_sub(1);
|
||||
|
||||
while pos > 0 && get_char_type(chars[pos]) == CharType::Whitespace {
|
||||
let mut pos = cursor_pos.saturating_sub(1);
|
||||
|
||||
// Skip whitespace
|
||||
while pos > 0 && chars[pos].is_whitespace() {
|
||||
pos -= 1;
|
||||
}
|
||||
|
||||
if pos == 0 && get_char_type(chars[pos]) == CharType::Whitespace {
|
||||
return 0;
|
||||
}
|
||||
if pos == 0 && get_char_type(chars[pos]) != CharType::Whitespace {
|
||||
return 0;
|
||||
}
|
||||
|
||||
let word_type = get_char_type(chars[pos]);
|
||||
while pos > 0 && get_char_type(chars[pos - 1]) == word_type {
|
||||
|
||||
// Skip to start of word
|
||||
while pos > 0 && chars[pos - 1].is_alphanumeric() {
|
||||
pos -= 1;
|
||||
}
|
||||
|
||||
while pos > 0 && get_char_type(chars[pos - 1]) == CharType::Whitespace {
|
||||
pos -= 1;
|
||||
}
|
||||
|
||||
if pos > 0 {
|
||||
pos - 1
|
||||
} else {
|
||||
0
|
||||
}
|
||||
|
||||
pos
|
||||
}
|
||||
|
||||
@@ -4,4 +4,4 @@ pub mod edit;
|
||||
|
||||
// Re-export the main types for convenience
|
||||
pub use types::{CanvasAction, ActionResult};
|
||||
pub use edit::execute_canvas_action; // Remove execute_edit_action
|
||||
pub use edit::execute_canvas_action;
|
||||
|
||||
@@ -1,9 +1,6 @@
|
||||
// canvas/src/actions/types.rs
|
||||
// src/canvas/actions/types.rs
|
||||
|
||||
use crossterm::event::KeyCode;
|
||||
|
||||
/// All possible canvas actions, type-safe and exhaustive
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub enum CanvasAction {
|
||||
// Character input
|
||||
InsertChar(char),
|
||||
@@ -28,37 +25,43 @@ pub enum CanvasAction {
|
||||
MoveWordNext,
|
||||
MoveWordEnd,
|
||||
MoveWordPrev,
|
||||
MoveWordEndPrev,
|
||||
|
||||
// Field navigation
|
||||
NextField,
|
||||
PrevField,
|
||||
|
||||
// AUTOCOMPLETE ACTIONS (NEW)
|
||||
/// Manually trigger autocomplete for current field
|
||||
// Autocomplete actions
|
||||
TriggerAutocomplete,
|
||||
/// Move to next suggestion
|
||||
SuggestionUp,
|
||||
/// Move to previous suggestion
|
||||
SuggestionDown,
|
||||
/// Select the currently highlighted suggestion
|
||||
SelectSuggestion,
|
||||
/// Cancel/exit autocomplete mode
|
||||
ExitSuggestions,
|
||||
|
||||
// Custom actions (escape hatch for feature-specific behavior)
|
||||
// Custom actions
|
||||
Custom(String),
|
||||
}
|
||||
|
||||
impl CanvasAction {
|
||||
/// Convert a string action to typed action (for backwards compatibility during migration)
|
||||
pub fn from_key(key: crossterm::event::KeyCode) -> Option<Self> {
|
||||
match key {
|
||||
crossterm::event::KeyCode::Char(c) => Some(Self::InsertChar(c)),
|
||||
crossterm::event::KeyCode::Backspace => Some(Self::DeleteBackward),
|
||||
crossterm::event::KeyCode::Delete => Some(Self::DeleteForward),
|
||||
crossterm::event::KeyCode::Left => Some(Self::MoveLeft),
|
||||
crossterm::event::KeyCode::Right => Some(Self::MoveRight),
|
||||
crossterm::event::KeyCode::Up => Some(Self::MoveUp),
|
||||
crossterm::event::KeyCode::Down => Some(Self::MoveDown),
|
||||
crossterm::event::KeyCode::Home => Some(Self::MoveLineStart),
|
||||
crossterm::event::KeyCode::End => Some(Self::MoveLineEnd),
|
||||
crossterm::event::KeyCode::Tab => Some(Self::NextField),
|
||||
crossterm::event::KeyCode::BackTab => Some(Self::PrevField),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
// Backward compatibility method
|
||||
pub fn from_string(action: &str) -> Self {
|
||||
match action {
|
||||
"insert_char" => {
|
||||
// This is a bit tricky - we need the char from context
|
||||
// For now, we'll use Custom until we refactor the call sites
|
||||
Self::Custom(action.to_string())
|
||||
}
|
||||
"delete_char_backward" => Self::DeleteBackward,
|
||||
"delete_char_forward" => Self::DeleteForward,
|
||||
"move_left" => Self::MoveLeft,
|
||||
@@ -72,10 +75,8 @@ impl CanvasAction {
|
||||
"move_word_next" => Self::MoveWordNext,
|
||||
"move_word_end" => Self::MoveWordEnd,
|
||||
"move_word_prev" => Self::MoveWordPrev,
|
||||
"move_word_end_prev" => Self::MoveWordEndPrev,
|
||||
"next_field" => Self::NextField,
|
||||
"prev_field" => Self::PrevField,
|
||||
// Autocomplete actions
|
||||
"trigger_autocomplete" => Self::TriggerAutocomplete,
|
||||
"suggestion_up" => Self::SuggestionUp,
|
||||
"suggestion_down" => Self::SuggestionDown,
|
||||
@@ -84,94 +85,13 @@ impl CanvasAction {
|
||||
_ => Self::Custom(action.to_string()),
|
||||
}
|
||||
}
|
||||
|
||||
/// Get string representation (for logging, debugging)
|
||||
pub fn as_str(&self) -> &str {
|
||||
match self {
|
||||
Self::InsertChar(_) => "insert_char",
|
||||
Self::DeleteBackward => "delete_char_backward",
|
||||
Self::DeleteForward => "delete_char_forward",
|
||||
Self::MoveLeft => "move_left",
|
||||
Self::MoveRight => "move_right",
|
||||
Self::MoveUp => "move_up",
|
||||
Self::MoveDown => "move_down",
|
||||
Self::MoveLineStart => "move_line_start",
|
||||
Self::MoveLineEnd => "move_line_end",
|
||||
Self::MoveFirstLine => "move_first_line",
|
||||
Self::MoveLastLine => "move_last_line",
|
||||
Self::MoveWordNext => "move_word_next",
|
||||
Self::MoveWordEnd => "move_word_end",
|
||||
Self::MoveWordPrev => "move_word_prev",
|
||||
Self::MoveWordEndPrev => "move_word_end_prev",
|
||||
Self::NextField => "next_field",
|
||||
Self::PrevField => "prev_field",
|
||||
// Autocomplete actions
|
||||
Self::TriggerAutocomplete => "trigger_autocomplete",
|
||||
Self::SuggestionUp => "suggestion_up",
|
||||
Self::SuggestionDown => "suggestion_down",
|
||||
Self::SelectSuggestion => "select_suggestion",
|
||||
Self::ExitSuggestions => "exit_suggestions",
|
||||
Self::Custom(s) => s,
|
||||
}
|
||||
}
|
||||
|
||||
/// Create action from KeyCode for common cases
|
||||
pub fn from_key(key: KeyCode) -> Option<Self> {
|
||||
match key {
|
||||
KeyCode::Char(c) => Some(Self::InsertChar(c)),
|
||||
KeyCode::Backspace => Some(Self::DeleteBackward),
|
||||
KeyCode::Delete => Some(Self::DeleteForward),
|
||||
KeyCode::Left => Some(Self::MoveLeft),
|
||||
KeyCode::Right => Some(Self::MoveRight),
|
||||
KeyCode::Up => Some(Self::MoveUp),
|
||||
KeyCode::Down => Some(Self::MoveDown),
|
||||
KeyCode::Home => Some(Self::MoveLineStart),
|
||||
KeyCode::End => Some(Self::MoveLineEnd),
|
||||
KeyCode::Tab => Some(Self::NextField),
|
||||
KeyCode::BackTab => Some(Self::PrevField),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Check if this action modifies content
|
||||
pub fn is_modifying(&self) -> bool {
|
||||
matches!(self,
|
||||
Self::InsertChar(_) |
|
||||
Self::DeleteBackward |
|
||||
Self::DeleteForward |
|
||||
Self::SelectSuggestion
|
||||
)
|
||||
}
|
||||
|
||||
/// Check if this action moves the cursor
|
||||
pub fn is_movement(&self) -> bool {
|
||||
matches!(self,
|
||||
Self::MoveLeft | Self::MoveRight | Self::MoveUp | Self::MoveDown |
|
||||
Self::MoveLineStart | Self::MoveLineEnd | Self::MoveFirstLine | Self::MoveLastLine |
|
||||
Self::MoveWordNext | Self::MoveWordEnd | Self::MoveWordPrev | Self::MoveWordEndPrev |
|
||||
Self::NextField | Self::PrevField
|
||||
)
|
||||
}
|
||||
|
||||
/// Check if this is a suggestion-related action
|
||||
pub fn is_suggestion(&self) -> bool {
|
||||
matches!(self,
|
||||
Self::TriggerAutocomplete | Self::SuggestionUp | Self::SuggestionDown |
|
||||
Self::SelectSuggestion | Self::ExitSuggestions
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/// Result of executing a canvas action
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub enum ActionResult {
|
||||
/// Action completed successfully, optional message for user feedback
|
||||
Success(Option<String>),
|
||||
/// Action was handled by custom feature logic
|
||||
HandledByFeature(String),
|
||||
/// Action requires additional context or cannot be performed
|
||||
RequiresContext(String),
|
||||
/// Action failed with error message
|
||||
Error(String),
|
||||
}
|
||||
|
||||
@@ -180,11 +100,11 @@ impl ActionResult {
|
||||
Self::Success(None)
|
||||
}
|
||||
|
||||
pub fn success_with_message(msg: impl Into<String>) -> Self {
|
||||
Self::Success(Some(msg.into()))
|
||||
pub fn success_with_message(msg: &str) -> Self {
|
||||
Self::Success(Some(msg.to_string()))
|
||||
}
|
||||
|
||||
pub fn error(msg: impl Into<String>) -> Self {
|
||||
pub fn error(msg: &str) -> Self {
|
||||
Self::Error(msg.into())
|
||||
}
|
||||
|
||||
@@ -201,37 +121,3 @@ impl ActionResult {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_action_from_string() {
|
||||
assert_eq!(CanvasAction::from_string("move_left"), CanvasAction::MoveLeft);
|
||||
assert_eq!(CanvasAction::from_string("delete_char_backward"), CanvasAction::DeleteBackward);
|
||||
assert_eq!(CanvasAction::from_string("trigger_autocomplete"), CanvasAction::TriggerAutocomplete);
|
||||
assert_eq!(CanvasAction::from_string("unknown"), CanvasAction::Custom("unknown".to_string()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_action_from_key() {
|
||||
assert_eq!(CanvasAction::from_key(KeyCode::Char('a')), Some(CanvasAction::InsertChar('a')));
|
||||
assert_eq!(CanvasAction::from_key(KeyCode::Left), Some(CanvasAction::MoveLeft));
|
||||
assert_eq!(CanvasAction::from_key(KeyCode::Backspace), Some(CanvasAction::DeleteBackward));
|
||||
assert_eq!(CanvasAction::from_key(KeyCode::F(1)), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_action_properties() {
|
||||
assert!(CanvasAction::InsertChar('a').is_modifying());
|
||||
assert!(!CanvasAction::MoveLeft.is_modifying());
|
||||
|
||||
assert!(CanvasAction::MoveLeft.is_movement());
|
||||
assert!(!CanvasAction::InsertChar('a').is_movement());
|
||||
|
||||
assert!(CanvasAction::SuggestionUp.is_suggestion());
|
||||
assert!(CanvasAction::TriggerAutocomplete.is_suggestion());
|
||||
assert!(!CanvasAction::MoveLeft.is_suggestion());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -180,6 +180,20 @@ impl CanvasConfig {
|
||||
Self::from_toml(&contents)
|
||||
}
|
||||
|
||||
/// NEW: Check if autocomplete should auto-trigger (simple logic)
|
||||
pub fn should_auto_trigger_autocomplete(&self) -> bool {
|
||||
// If trigger_autocomplete keybinding exists anywhere, use manual mode only
|
||||
// If no trigger_autocomplete keybinding, use auto-trigger mode
|
||||
!self.has_trigger_autocomplete_keybinding()
|
||||
}
|
||||
|
||||
/// NEW: Check if user has configured manual trigger keybinding
|
||||
pub fn has_trigger_autocomplete_keybinding(&self) -> bool {
|
||||
self.keybindings.edit.contains_key("trigger_autocomplete") ||
|
||||
self.keybindings.read_only.contains_key("trigger_autocomplete") ||
|
||||
self.keybindings.global.contains_key("trigger_autocomplete")
|
||||
}
|
||||
|
||||
/// Get action for key in read-only mode
|
||||
pub fn get_read_only_action(&self, key: KeyCode, modifiers: KeyModifiers) -> Option<&str> {
|
||||
self.get_action_in_mode(&self.keybindings.read_only, key, modifiers)
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
use crate::canvas::state::CanvasState;
|
||||
use crate::canvas::actions::{CanvasAction, ActionResult, execute_canvas_action};
|
||||
use crate::config::CanvasConfig;
|
||||
|
||||
/// High-level action dispatcher that coordinates between different action types
|
||||
pub struct ActionDispatcher;
|
||||
@@ -13,7 +14,9 @@ impl ActionDispatcher {
|
||||
state: &mut S,
|
||||
ideal_cursor_column: &mut usize,
|
||||
) -> anyhow::Result<ActionResult> {
|
||||
execute_canvas_action(action, state, ideal_cursor_column).await
|
||||
|
||||
// Load config once here instead of threading it everywhere
|
||||
execute_canvas_action(action, state, ideal_cursor_column, Some(&CanvasConfig::load())).await
|
||||
}
|
||||
|
||||
/// Quick action dispatch from KeyCode
|
||||
|
||||
@@ -42,6 +42,7 @@ move_word_next = ["Ctrl+Right"]
|
||||
move_word_prev = ["Ctrl+Left"]
|
||||
next_field = ["Tab"]
|
||||
prev_field = ["Shift+Tab"]
|
||||
trigger_autocomplete = ["Ctrl+p"]
|
||||
|
||||
# Suggestion/autocomplete keybindings
|
||||
[keybindings.suggestions]
|
||||
@@ -49,6 +50,7 @@ suggestion_up = ["Up", "Ctrl+p"]
|
||||
suggestion_down = ["Down", "Ctrl+n"]
|
||||
select_suggestion = ["Enter", "Tab"]
|
||||
exit_suggestions = ["Esc"]
|
||||
trigger_autocomplete = ["Tab"]
|
||||
|
||||
# Global keybindings (work in both modes)
|
||||
[keybindings.global]
|
||||
|
||||
@@ -69,11 +69,10 @@ prev_field = ["shift+enter"]
|
||||
exit = ["esc", "ctrl+e"]
|
||||
delete_char_forward = ["delete"]
|
||||
delete_char_backward = ["backspace"]
|
||||
move_left = [""]
|
||||
move_left = ["left"]
|
||||
move_right = ["right"]
|
||||
suggestion_down = ["ctrl+n", "tab"]
|
||||
suggestion_up = ["ctrl+p", "shift+tab"]
|
||||
trigger_autocomplete = ["left"]
|
||||
|
||||
[keybindings.command]
|
||||
exit_command_mode = ["ctrl+g", "esc"]
|
||||
|
||||
124
client/docs/canvas_add_functionality.md
Normal file
124
client/docs/canvas_add_functionality.md
Normal file
@@ -0,0 +1,124 @@
|
||||
## How Canvas Library Custom Functionality Works
|
||||
|
||||
### 1. **The Canvas Library Calls YOUR Custom Code First**
|
||||
|
||||
When you call `ActionDispatcher::dispatch()`, here's what happens:
|
||||
|
||||
```rust
|
||||
// Inside canvas library (canvas/src/actions/edit.rs):
|
||||
pub async fn execute_canvas_action<S: CanvasState>(
|
||||
action: CanvasAction,
|
||||
state: &mut S,
|
||||
ideal_cursor_column: &mut usize,
|
||||
) -> Result<ActionResult> {
|
||||
// 1. FIRST: Canvas library calls YOUR custom handler
|
||||
if let Some(result) = state.handle_feature_action(&action, &context) {
|
||||
return Ok(ActionResult::HandledByFeature(result)); // YOUR code handled it
|
||||
}
|
||||
|
||||
// 2. ONLY IF your code returns None: Canvas handles generic actions
|
||||
handle_generic_canvas_action(action, state, ideal_cursor_column).await
|
||||
}
|
||||
```
|
||||
|
||||
### 2. **Your Extension Point: `handle_feature_action`**
|
||||
|
||||
You add custom functionality by implementing `handle_feature_action` in your states:
|
||||
|
||||
```rust
|
||||
// In src/state/pages/auth.rs
|
||||
impl CanvasState for LoginState {
|
||||
// ... other methods ...
|
||||
|
||||
fn handle_feature_action(&mut self, action: &CanvasAction, context: &ActionContext) -> Option<String> {
|
||||
match action {
|
||||
// Custom login-specific actions
|
||||
CanvasAction::Custom(action_str) if action_str == "submit_login" => {
|
||||
if self.username.is_empty() || self.password.is_empty() {
|
||||
Some("Please fill in all required fields".to_string())
|
||||
} else {
|
||||
// Trigger login process
|
||||
Some(format!("Logging in user: {}", self.username))
|
||||
}
|
||||
}
|
||||
|
||||
CanvasAction::Custom(action_str) if action_str == "clear_form" => {
|
||||
self.username.clear();
|
||||
self.password.clear();
|
||||
self.set_has_unsaved_changes(false);
|
||||
Some("Login form cleared".to_string())
|
||||
}
|
||||
|
||||
// Custom behavior for standard actions
|
||||
CanvasAction::NextField => {
|
||||
// Custom validation when moving between fields
|
||||
if self.current_field == 0 && self.username.is_empty() {
|
||||
Some("Username cannot be empty".to_string())
|
||||
} else {
|
||||
None // Let canvas library handle the normal field movement
|
||||
}
|
||||
}
|
||||
|
||||
// Let canvas library handle everything else
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 3. **Multiple Ways to Add Custom Functionality**
|
||||
|
||||
#### A) **Custom Actions via Config**
|
||||
```toml
|
||||
# In config.toml
|
||||
[keybindings.edit]
|
||||
submit_login = ["ctrl+enter"]
|
||||
clear_form = ["ctrl+r"]
|
||||
```
|
||||
|
||||
#### B) **Override Standard Actions**
|
||||
```rust
|
||||
fn handle_feature_action(&mut self, action: &CanvasAction, context: &ActionContext) -> Option<String> {
|
||||
match action {
|
||||
CanvasAction::InsertChar('p') if self.current_field == 1 => {
|
||||
// Custom behavior when typing 'p' in password field
|
||||
Some("Password field - use secure input".to_string())
|
||||
}
|
||||
_ => None, // Let canvas handle normally
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
#### C) **Context-Aware Logic**
|
||||
```rust
|
||||
fn handle_feature_action(&mut self, action: &CanvasAction, context: &ActionContext) -> Option<String> {
|
||||
match action {
|
||||
CanvasAction::MoveDown => {
|
||||
// Custom logic based on current state
|
||||
if context.current_field == 1 && context.current_input.len() < 8 {
|
||||
Some("Password should be at least 8 characters".to_string())
|
||||
} else {
|
||||
None // Normal field movement
|
||||
}
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## The Canvas Library Philosophy
|
||||
|
||||
**Canvas Library = Generic behavior + Your extension points**
|
||||
|
||||
- ✅ **Canvas handles**: Character insertion, cursor movement, field navigation, etc.
|
||||
- ✅ **You handle**: Validation, submission, clearing, app-specific logic
|
||||
- ✅ **You decide**: Return `Some(message)` to override, `None` to use canvas default
|
||||
|
||||
## Summary
|
||||
|
||||
You **don't communicate with the library elsewhere**. Instead:
|
||||
|
||||
1. **Canvas library calls your code first** via `handle_feature_action`
|
||||
2. **Your code decides** whether to handle the action or let canvas handle it
|
||||
3. **Canvas library handles** generic form behavior when you return `None`
|
||||
|
||||
@@ -3,7 +3,7 @@ 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 crate::state::pages::canvas_state::CanvasState;
|
||||
use canvas::canvas::{render_canvas, CanvasState, HighlightState as CanvasHighlightState}; // Use canvas library
|
||||
use ratatui::{
|
||||
layout::{Alignment, Constraint, Direction, Layout, Rect},
|
||||
style::{Modifier, Style},
|
||||
@@ -11,10 +11,18 @@ use ratatui::{
|
||||
widgets::{Block, BorderType, Borders, Paragraph},
|
||||
Frame,
|
||||
};
|
||||
use crate::components::handlers::canvas::render_canvas;
|
||||
use crate::components::common::{dialog, autocomplete}; // Added autocomplete
|
||||
use crate::config::binds::config::EditorKeybindingMode;
|
||||
|
||||
// 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_add_logic(
|
||||
f: &mut Frame,
|
||||
area: Rect,
|
||||
@@ -77,18 +85,18 @@ pub fn render_add_logic(
|
||||
let editor_borrow = add_logic_state.script_content_editor.borrow();
|
||||
editor_borrow.cursor() // Returns (row, col) as (usize, usize)
|
||||
};
|
||||
|
||||
|
||||
let (cursor_line, cursor_col) = current_cursor;
|
||||
|
||||
|
||||
// Account for TextArea's block borders (1 for each side)
|
||||
let block_offset_x = 1;
|
||||
let block_offset_y = 1;
|
||||
|
||||
|
||||
// Position autocomplete at current cursor position
|
||||
// Add 1 to column to position dropdown right after the cursor
|
||||
let autocomplete_x = cursor_col + 1;
|
||||
let autocomplete_y = cursor_line;
|
||||
|
||||
|
||||
let input_rect = Rect {
|
||||
x: (inner_area.x + block_offset_x + autocomplete_x as u16).min(inner_area.right().saturating_sub(20)),
|
||||
y: (inner_area.y + block_offset_y + autocomplete_y as u16).min(inner_area.bottom().saturating_sub(5)),
|
||||
@@ -152,40 +160,37 @@ pub fn render_add_logic(
|
||||
);
|
||||
f.render_widget(profile_text, top_info_area);
|
||||
|
||||
// Canvas
|
||||
// Canvas - USING CANVAS LIBRARY
|
||||
let focus_on_canvas_inputs = matches!(
|
||||
add_logic_state.current_focus,
|
||||
AddLogicFocus::InputLogicName
|
||||
| AddLogicFocus::InputTargetColumn
|
||||
| AddLogicFocus::InputDescription
|
||||
);
|
||||
// Call render_canvas and get the active_field_rect
|
||||
|
||||
let canvas_highlight_state = convert_highlight_state(highlight_state);
|
||||
let active_field_rect = render_canvas(
|
||||
f,
|
||||
canvas_area,
|
||||
add_logic_state, // Pass the whole state as it impl CanvasState
|
||||
&add_logic_state.fields(),
|
||||
&add_logic_state.current_field(),
|
||||
&add_logic_state.inputs(),
|
||||
theme,
|
||||
is_edit_mode && focus_on_canvas_inputs, // is_edit_mode for canvas fields
|
||||
highlight_state,
|
||||
add_logic_state, // AddLogicState implements CanvasState
|
||||
theme, // Theme implements CanvasTheme
|
||||
is_edit_mode && focus_on_canvas_inputs,
|
||||
&canvas_highlight_state,
|
||||
);
|
||||
|
||||
// --- Render Autocomplete for Target Column ---
|
||||
// `is_edit_mode` here refers to the general edit mode of the EventHandler
|
||||
if is_edit_mode && add_logic_state.current_field() == 1 { // Target Column field
|
||||
if let Some(suggestions) = add_logic_state.get_suggestions() { // Uses CanvasState impl
|
||||
let selected = add_logic_state.get_selected_suggestion_index();
|
||||
if !suggestions.is_empty() { // Only render if there are suggestions to show
|
||||
if add_logic_state.in_target_column_suggestion_mode && add_logic_state.show_target_column_suggestions {
|
||||
if !add_logic_state.target_column_suggestions.is_empty() {
|
||||
if let Some(input_rect) = active_field_rect {
|
||||
autocomplete::render_autocomplete_dropdown(
|
||||
f,
|
||||
input_rect,
|
||||
f.area(), // Full frame area for clamping
|
||||
theme,
|
||||
suggestions,
|
||||
selected,
|
||||
&add_logic_state.target_column_suggestions,
|
||||
add_logic_state.selected_target_column_suggestion_index,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,8 +3,7 @@ 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 crate::state::pages::canvas_state::CanvasState;
|
||||
// use crate::state::pages::add_table::{ColumnDefinition, LinkDefinition}; // Not directly used here
|
||||
use canvas::canvas::{render_canvas, CanvasState, HighlightState as CanvasHighlightState}; // Use canvas library
|
||||
use ratatui::{
|
||||
layout::{Alignment, Constraint, Direction, Layout, Rect},
|
||||
style::{Modifier, Style},
|
||||
@@ -12,16 +11,24 @@ use ratatui::{
|
||||
widgets::{Block, BorderType, Borders, Cell, Paragraph, Row, Table},
|
||||
Frame,
|
||||
};
|
||||
use crate::components::handlers::canvas::render_canvas;
|
||||
use crate::components::common::dialog;
|
||||
|
||||
// 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 },
|
||||
}
|
||||
}
|
||||
|
||||
/// Renders the Add New Table page layout, structuring the display of table information,
|
||||
/// input fields, and action buttons. Adapts layout based on terminal width.
|
||||
pub fn render_add_table(
|
||||
f: &mut Frame,
|
||||
area: Rect,
|
||||
theme: &Theme,
|
||||
app_state: &AppState, // Currently unused, might be needed later
|
||||
app_state: &AppState,
|
||||
add_table_state: &mut AddTableState,
|
||||
is_edit_mode: bool, // Determines if canvas inputs are in edit mode
|
||||
highlight_state: &HighlightState, // For text highlighting in canvas
|
||||
@@ -349,17 +356,15 @@ pub fn render_add_table(
|
||||
&mut add_table_state.column_table_state,
|
||||
);
|
||||
|
||||
// --- Canvas Rendering (Column Definition Input) ---
|
||||
// --- Canvas Rendering (Column Definition Input) - USING CANVAS LIBRARY ---
|
||||
let canvas_highlight_state = convert_highlight_state(highlight_state);
|
||||
let _active_field_rect = render_canvas(
|
||||
f,
|
||||
canvas_area,
|
||||
add_table_state,
|
||||
&add_table_state.fields(),
|
||||
&add_table_state.current_field(),
|
||||
&add_table_state.inputs(),
|
||||
theme,
|
||||
add_table_state, // AddTableState implements CanvasState
|
||||
theme, // Theme implements CanvasTheme
|
||||
is_edit_mode && focus_on_canvas_inputs,
|
||||
highlight_state,
|
||||
&canvas_highlight_state,
|
||||
);
|
||||
|
||||
// --- Button Style Helpers ---
|
||||
@@ -557,7 +562,7 @@ pub fn render_add_table(
|
||||
|
||||
// --- DIALOG ---
|
||||
// Render the dialog overlay if it's active
|
||||
if app_state.ui.dialog.dialog_show { // Use the passed-in app_state
|
||||
if app_state.ui.dialog.dialog_show {
|
||||
dialog::render_dialog(
|
||||
f,
|
||||
f.area(), // Render over the whole frame area
|
||||
|
||||
@@ -13,6 +13,16 @@ use ratatui::{
|
||||
Frame,
|
||||
};
|
||||
use crate::state::app::highlight::HighlightState;
|
||||
use canvas::canvas::{render_canvas, HighlightState as CanvasHighlightState}; // Use canvas library's render function
|
||||
|
||||
// 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_login(
|
||||
f: &mut Frame,
|
||||
@@ -48,17 +58,15 @@ pub fn render_login(
|
||||
])
|
||||
.split(inner_area);
|
||||
|
||||
// --- FORM RENDERING ---
|
||||
crate::components::handlers::canvas::render_canvas(
|
||||
// --- FORM RENDERING (Using canvas library directly) ---
|
||||
let canvas_highlight_state = convert_highlight_state(highlight_state);
|
||||
render_canvas(
|
||||
f,
|
||||
chunks[0],
|
||||
login_state,
|
||||
&["Username/Email", "Password"],
|
||||
&login_state.current_field,
|
||||
&[&login_state.username, &login_state.password],
|
||||
theme,
|
||||
login_state, // LoginState implements CanvasState
|
||||
theme, // Theme implements CanvasTheme
|
||||
is_edit_mode,
|
||||
highlight_state,
|
||||
&canvas_highlight_state,
|
||||
);
|
||||
|
||||
// --- ERROR MESSAGE ---
|
||||
@@ -71,7 +79,7 @@ pub fn render_login(
|
||||
);
|
||||
}
|
||||
|
||||
// --- BUTTONS ---
|
||||
// --- BUTTONS (unchanged) ---
|
||||
let button_chunks = Layout::default()
|
||||
.direction(Direction::Horizontal)
|
||||
.constraints([Constraint::Percentage(50), Constraint::Percentage(50)])
|
||||
@@ -83,7 +91,7 @@ pub fn render_login(
|
||||
app_state.focused_button_index== login_button_index
|
||||
} else {
|
||||
false
|
||||
};
|
||||
};
|
||||
let mut login_style = Style::default().fg(theme.fg);
|
||||
let mut login_border = Style::default().fg(theme.border);
|
||||
if login_active {
|
||||
@@ -105,12 +113,12 @@ pub fn render_login(
|
||||
);
|
||||
|
||||
// Return Button
|
||||
let return_button_index = 1; // Assuming Return is the second general element
|
||||
let return_button_index = 1;
|
||||
let return_active = if app_state.ui.focus_outside_canvas {
|
||||
app_state.focused_button_index== return_button_index
|
||||
} else {
|
||||
false // Not active if focus is in canvas or other modes
|
||||
};
|
||||
false
|
||||
};
|
||||
let mut return_style = Style::default().fg(theme.fg);
|
||||
let mut return_border = Style::default().fg(theme.border);
|
||||
if return_active {
|
||||
@@ -132,17 +140,15 @@ pub fn render_login(
|
||||
);
|
||||
|
||||
// --- DIALOG ---
|
||||
// Check the correct field name for showing the dialog
|
||||
if app_state.ui.dialog.dialog_show {
|
||||
// Pass all 7 arguments correctly
|
||||
dialog::render_dialog(
|
||||
f,
|
||||
f.area(),
|
||||
theme,
|
||||
&app_state.ui.dialog.dialog_title,
|
||||
&app_state.ui.dialog.dialog_message,
|
||||
&app_state.ui.dialog.dialog_buttons, // Pass buttons slice
|
||||
app_state.ui.dialog.dialog_active_button_index,
|
||||
&app_state.ui.dialog.dialog_buttons,
|
||||
app_state.ui.dialog.dialog_active_button_index,
|
||||
app_state.ui.dialog.is_loading,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -2,10 +2,9 @@
|
||||
|
||||
use crate::{
|
||||
config::colors::themes::Theme,
|
||||
state::pages::auth::RegisterState, // Use RegisterState
|
||||
components::common::{dialog, autocomplete},
|
||||
state::pages::auth::RegisterState,
|
||||
components::common::dialog,
|
||||
state::app::state::AppState,
|
||||
state::pages::canvas_state::CanvasState,
|
||||
modes::handlers::mode_manager::AppMode,
|
||||
};
|
||||
use ratatui::{
|
||||
@@ -15,12 +14,24 @@ use ratatui::{
|
||||
Frame,
|
||||
};
|
||||
use crate::state::app::highlight::HighlightState;
|
||||
use canvas::canvas::{render_canvas, HighlightState as CanvasHighlightState}; // Use canvas library's render function
|
||||
use canvas::autocomplete::gui::render_autocomplete_dropdown;
|
||||
use canvas::autocomplete::AutocompleteCanvasState;
|
||||
|
||||
// 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,
|
||||
area: Rect,
|
||||
theme: &Theme,
|
||||
state: &RegisterState, // Use RegisterState
|
||||
state: &RegisterState,
|
||||
app_state: &AppState,
|
||||
is_edit_mode: bool,
|
||||
highlight_state: &HighlightState,
|
||||
@@ -29,7 +40,7 @@ pub fn render_register(
|
||||
.borders(Borders::ALL)
|
||||
.border_type(BorderType::Plain)
|
||||
.border_style(Style::default().fg(theme.border))
|
||||
.title(" Register ") // Update title
|
||||
.title(" Register ")
|
||||
.style(Style::default().bg(theme.bg));
|
||||
|
||||
f.render_widget(block, area);
|
||||
@@ -39,7 +50,6 @@ pub fn render_register(
|
||||
vertical: 1,
|
||||
});
|
||||
|
||||
// Adjust constraints for 4 fields + error + buttons
|
||||
let chunks = Layout::default()
|
||||
.direction(Direction::Vertical)
|
||||
.constraints([
|
||||
@@ -50,23 +60,15 @@ pub fn render_register(
|
||||
])
|
||||
.split(inner_area);
|
||||
|
||||
// --- FORM RENDERING (Using render_canvas) ---
|
||||
let active_field_rect = crate::components::handlers::canvas::render_canvas(
|
||||
// --- FORM RENDERING (Using canvas library directly) ---
|
||||
let canvas_highlight_state = convert_highlight_state(highlight_state);
|
||||
let input_rect = render_canvas(
|
||||
f,
|
||||
chunks[0], // Area for the canvas
|
||||
state, // The state object (RegisterState)
|
||||
&[ // Field labels
|
||||
"Username",
|
||||
"Email*",
|
||||
"Password*",
|
||||
"Confirm Password",
|
||||
"Role* (Tab)",
|
||||
],
|
||||
&state.current_field(), // Pass current field index
|
||||
&state.inputs().iter().map(|s| *s).collect::<Vec<&String>>(), // Pass inputs directly
|
||||
theme,
|
||||
chunks[0],
|
||||
state, // RegisterState implements CanvasState
|
||||
theme, // Theme implements CanvasTheme
|
||||
is_edit_mode,
|
||||
highlight_state,
|
||||
&canvas_highlight_state,
|
||||
);
|
||||
|
||||
// --- HELP TEXT ---
|
||||
@@ -75,7 +77,6 @@ pub fn render_register(
|
||||
.alignment(Alignment::Center);
|
||||
f.render_widget(help_text, chunks[1]);
|
||||
|
||||
|
||||
// --- ERROR MESSAGE ---
|
||||
if let Some(err) = &state.error_message {
|
||||
f.render_widget(
|
||||
@@ -107,7 +108,7 @@ pub fn render_register(
|
||||
}
|
||||
|
||||
f.render_widget(
|
||||
Paragraph::new("Register") // Update button text
|
||||
Paragraph::new("Register")
|
||||
.style(register_style)
|
||||
.alignment(Alignment::Center)
|
||||
.block(
|
||||
@@ -119,7 +120,7 @@ pub fn render_register(
|
||||
button_chunks[0],
|
||||
);
|
||||
|
||||
// Return Button (logic remains similar)
|
||||
// Return Button
|
||||
let return_button_index = 1;
|
||||
let return_active = if app_state.ui.focus_outside_canvas {
|
||||
app_state.focused_button_index== return_button_index
|
||||
@@ -146,19 +147,22 @@ pub fn render_register(
|
||||
button_chunks[1],
|
||||
);
|
||||
|
||||
// --- Render Autocomplete Dropdown (Draw AFTER buttons) ---
|
||||
// --- AUTOCOMPLETE DROPDOWN (Using canvas library directly) ---
|
||||
if app_state.current_mode == AppMode::Edit {
|
||||
if let Some(suggestions) = state.get_suggestions() {
|
||||
let selected = state.get_selected_suggestion_index();
|
||||
if !suggestions.is_empty() {
|
||||
if let Some(input_rect) = active_field_rect {
|
||||
autocomplete::render_autocomplete_dropdown(f, input_rect, f.area(), theme, suggestions, selected);
|
||||
}
|
||||
if let Some(autocomplete_state) = state.autocomplete_state() {
|
||||
if let Some(input_rect) = input_rect {
|
||||
render_autocomplete_dropdown(
|
||||
f,
|
||||
f.area(), // Frame area
|
||||
input_rect, // Current input field rect
|
||||
theme, // Theme implements CanvasTheme
|
||||
autocomplete_state,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// --- DIALOG --- (Keep dialog logic)
|
||||
// --- DIALOG ---
|
||||
if app_state.ui.dialog.dialog_show {
|
||||
dialog::render_dialog(
|
||||
f,
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
// src/functions/modes/edit/add_logic_e.rs
|
||||
use crate::state::pages::add_logic::AddLogicState;
|
||||
use crate::state::pages::canvas_state::CanvasState;
|
||||
use canvas::canvas::CanvasState;
|
||||
use anyhow::Result;
|
||||
use crossterm::event::{KeyCode, KeyEvent};
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// src/functions/modes/edit/add_table_e.rs
|
||||
use crate::state::pages::add_table::AddTableState;
|
||||
use crate::state::pages::canvas_state::CanvasState; // Use trait
|
||||
use crossterm::event::{KeyCode, KeyEvent};
|
||||
use canvas::canvas::CanvasState;
|
||||
use anyhow::Result;
|
||||
|
||||
#[derive(PartialEq)]
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
// src/functions/modes/edit/auth_e.rs
|
||||
|
||||
use crate::services::grpc_client::GrpcClient;
|
||||
use crate::state::pages::canvas_state::CanvasState;
|
||||
use crate::state::pages::form::FormState;
|
||||
use crate::state::pages::auth::RegisterState;
|
||||
use crate::state::app::state::AppState;
|
||||
use crate::tui::functions::common::form::{revert, save};
|
||||
use crossterm::event::{KeyCode, KeyEvent};
|
||||
use canvas::autocomplete::AutocompleteCanvasState;
|
||||
use canvas::canvas::CanvasState;
|
||||
use std::any::Any;
|
||||
use anyhow::Result;
|
||||
|
||||
@@ -295,53 +296,42 @@ pub async fn execute_edit_action<S: CanvasState + Any + Send>(
|
||||
"suggestion_down" | "suggestion_up" | "select_suggestion" | "exit_suggestion_mode" => {
|
||||
// Attempt to downcast to RegisterState to handle suggestion logic here
|
||||
if let Some(register_state) = (state as &mut dyn Any).downcast_mut::<RegisterState>() {
|
||||
// Only handle if it's the role field (index 4)
|
||||
if register_state.current_field() == 4 {
|
||||
// Only handle if it's the role field (index 4) and autocomplete is active
|
||||
if register_state.current_field() == 4 && register_state.is_autocomplete_active() {
|
||||
match action {
|
||||
"suggestion_down" if register_state.in_suggestion_mode => {
|
||||
let max_index = register_state.role_suggestions.len().saturating_sub(1);
|
||||
let current_index = register_state.selected_suggestion_index.unwrap_or(0);
|
||||
register_state.selected_suggestion_index = Some(if current_index >= max_index { 0 } else { current_index + 1 });
|
||||
Ok("Suggestion changed down".to_string())
|
||||
"suggestion_down" => {
|
||||
if let Some(autocomplete_state) = register_state.autocomplete_state_mut() {
|
||||
autocomplete_state.select_next();
|
||||
Ok("Suggestion changed down".to_string())
|
||||
} else {
|
||||
Ok("No autocomplete state".to_string())
|
||||
}
|
||||
}
|
||||
"suggestion_up" if register_state.in_suggestion_mode => {
|
||||
let max_index = register_state.role_suggestions.len().saturating_sub(1);
|
||||
let current_index = register_state.selected_suggestion_index.unwrap_or(0);
|
||||
register_state.selected_suggestion_index = Some(if current_index == 0 { max_index } else { current_index.saturating_sub(1) });
|
||||
Ok("Suggestion changed up".to_string())
|
||||
"suggestion_up" => {
|
||||
if let Some(autocomplete_state) = register_state.autocomplete_state_mut() {
|
||||
autocomplete_state.select_previous();
|
||||
Ok("Suggestion changed up".to_string())
|
||||
} else {
|
||||
Ok("No autocomplete state".to_string())
|
||||
}
|
||||
}
|
||||
"select_suggestion" if register_state.in_suggestion_mode => {
|
||||
if let Some(index) = register_state.selected_suggestion_index {
|
||||
if let Some(selected_role) = register_state.role_suggestions.get(index).cloned() {
|
||||
register_state.role = selected_role.clone(); // Update the role field
|
||||
register_state.in_suggestion_mode = false; // Exit suggestion mode
|
||||
register_state.show_role_suggestions = false; // Hide suggestions
|
||||
register_state.selected_suggestion_index = None; // Clear selection
|
||||
Ok(format!("Selected role: {}", selected_role)) // Return success message
|
||||
} else {
|
||||
Ok("Selected suggestion index out of bounds.".to_string()) // Error case
|
||||
}
|
||||
"select_suggestion" => {
|
||||
if let Some(message) = register_state.apply_autocomplete_selection() {
|
||||
Ok(message)
|
||||
} else {
|
||||
Ok("No suggestion selected".to_string())
|
||||
}
|
||||
}
|
||||
"exit_suggestion_mode" => { // Handle Esc or other conditions
|
||||
register_state.show_role_suggestions = false;
|
||||
register_state.selected_suggestion_index = None;
|
||||
register_state.in_suggestion_mode = false;
|
||||
"exit_suggestion_mode" => {
|
||||
register_state.deactivate_autocomplete();
|
||||
Ok("Suggestions hidden".to_string())
|
||||
}
|
||||
_ => {
|
||||
// Action is suggestion-related but state doesn't match (e.g., not in suggestion mode)
|
||||
Ok("Suggestion action ignored: State mismatch.".to_string())
|
||||
}
|
||||
_ => Ok("Suggestion action ignored: State mismatch.".to_string())
|
||||
}
|
||||
} else {
|
||||
// It's RegisterState, but not the role field
|
||||
Ok("Suggestion action ignored: Not on role field.".to_string())
|
||||
}
|
||||
Ok("Suggestion action ignored: Not on role field or autocomplete not active.".to_string())
|
||||
}
|
||||
} else {
|
||||
// Downcast failed - this action is only for RegisterState
|
||||
Ok(format!("Action '{}' not applicable for this state type.", action))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
// src/functions/modes/read_only/add_logic_ro.rs
|
||||
use crate::config::binds::key_sequences::KeySequenceTracker;
|
||||
use crate::state::pages::add_logic::AddLogicState; // Changed
|
||||
use crate::state::pages::canvas_state::CanvasState;
|
||||
use crate::state::app::state::AppState;
|
||||
use canvas::canvas::CanvasState;
|
||||
use anyhow::Result;
|
||||
|
||||
// Word navigation helpers (get_char_type, find_next_word_start, etc.)
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
// src/functions/modes/read_only/add_table_ro.rs
|
||||
use crate::config::binds::key_sequences::KeySequenceTracker;
|
||||
use crate::state::pages::add_table::AddTableState;
|
||||
use crate::state::pages::canvas_state::CanvasState;
|
||||
use crate::state::app::state::AppState;
|
||||
use canvas::canvas::CanvasState;
|
||||
use anyhow::Result;
|
||||
|
||||
// Re-use word navigation helpers if they are public or move them to a common module
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
// src/functions/modes/read_only/auth_ro.rs
|
||||
|
||||
use crate::config::binds::key_sequences::KeySequenceTracker;
|
||||
use crate::state::pages::canvas_state::CanvasState;
|
||||
use crate::state::app::state::AppState;
|
||||
use canvas::canvas::CanvasState;
|
||||
use anyhow::Result;
|
||||
|
||||
#[derive(PartialEq)]
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// src/modes/canvas/edit.rs
|
||||
use crate::config::binds::config::Config;
|
||||
use crate::functions::modes::edit::{
|
||||
add_logic_e, add_table_e, auth_e, form_e,
|
||||
add_logic_e, add_table_e, form_e,
|
||||
};
|
||||
use crate::modes::handlers::event::EventHandler;
|
||||
use crate::services::grpc_client::GrpcClient;
|
||||
@@ -127,6 +127,60 @@ pub async fn handle_form_edit_with_canvas(
|
||||
Ok(String::new())
|
||||
}
|
||||
|
||||
/// NEW: Unified canvas action handler for any CanvasState (LoginState, RegisterState, etc.)
|
||||
/// This replaces the old auth_e::execute_edit_action calls with the new canvas library
|
||||
async fn handle_canvas_state_edit<S: CanvasState>(
|
||||
key: KeyEvent,
|
||||
config: &Config,
|
||||
state: &mut S,
|
||||
ideal_cursor_column: &mut usize,
|
||||
) -> Result<String> {
|
||||
// Try direct key mapping first (same pattern as FormState)
|
||||
if let Some(canvas_action) = CanvasAction::from_key(key.code) {
|
||||
match ActionDispatcher::dispatch(canvas_action, state, ideal_cursor_column).await {
|
||||
Ok(ActionResult::Success(msg)) => {
|
||||
return Ok(msg.unwrap_or_default());
|
||||
}
|
||||
Ok(ActionResult::HandledByFeature(msg)) => {
|
||||
return Ok(msg);
|
||||
}
|
||||
Ok(ActionResult::Error(msg)) => {
|
||||
return Ok(format!("Error: {}", msg));
|
||||
}
|
||||
Ok(ActionResult::RequiresContext(msg)) => {
|
||||
return Ok(format!("Context needed: {}", msg));
|
||||
}
|
||||
Err(_) => {
|
||||
// Fall through to try config mapping
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Try config-mapped action (same pattern as FormState)
|
||||
if let Some(action_str) = config.get_edit_action_for_key(key.code, key.modifiers) {
|
||||
let canvas_action = CanvasAction::from_string(&action_str);
|
||||
match ActionDispatcher::dispatch(canvas_action, state, ideal_cursor_column).await {
|
||||
Ok(ActionResult::Success(msg)) => {
|
||||
return Ok(msg.unwrap_or_default());
|
||||
}
|
||||
Ok(ActionResult::HandledByFeature(msg)) => {
|
||||
return Ok(msg);
|
||||
}
|
||||
Ok(ActionResult::Error(msg)) => {
|
||||
return Ok(format!("Error: {}", msg));
|
||||
}
|
||||
Ok(ActionResult::RequiresContext(msg)) => {
|
||||
return Ok(format!("Context needed: {}", msg));
|
||||
}
|
||||
Err(e) => {
|
||||
return Ok(format!("Action failed: {}", e));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(String::new())
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub async fn handle_edit_event(
|
||||
key: KeyEvent,
|
||||
@@ -283,39 +337,39 @@ pub async fn handle_edit_event(
|
||||
return Ok(EditEventOutcome::ExitEditMode);
|
||||
}
|
||||
|
||||
// Handle all other edit actions
|
||||
// Handle all other edit actions - NOW USING CANVAS LIBRARY
|
||||
let msg = if app_state.ui.show_login {
|
||||
// FIX: Pass &mut event_handler.ideal_cursor_column
|
||||
auth_e::execute_edit_action(
|
||||
action_str,
|
||||
// NEW: Use unified canvas handler instead of auth_e::execute_edit_action
|
||||
handle_canvas_state_edit(
|
||||
key,
|
||||
config,
|
||||
login_state,
|
||||
&mut event_handler.ideal_cursor_column,
|
||||
)
|
||||
.await?
|
||||
} else if app_state.ui.show_add_table {
|
||||
// FIX: Pass &mut event_handler.ideal_cursor_column
|
||||
add_table_e::execute_edit_action(
|
||||
action_str,
|
||||
// NEW: Use unified canvas handler instead of add_table_e::execute_edit_action
|
||||
handle_canvas_state_edit(
|
||||
key,
|
||||
config,
|
||||
&mut admin_state.add_table_state,
|
||||
&mut event_handler.ideal_cursor_column,
|
||||
)
|
||||
.await?
|
||||
} else if app_state.ui.show_add_logic {
|
||||
// FIX: Pass &mut event_handler.ideal_cursor_column
|
||||
add_logic_e::execute_edit_action(
|
||||
action_str,
|
||||
// NEW: Use unified canvas handler instead of add_logic_e::execute_edit_action
|
||||
handle_canvas_state_edit(
|
||||
key,
|
||||
config,
|
||||
&mut admin_state.add_logic_state,
|
||||
&mut event_handler.ideal_cursor_column,
|
||||
)
|
||||
.await?
|
||||
} else if app_state.ui.show_register {
|
||||
// FIX: Pass &mut event_handler.ideal_cursor_column
|
||||
auth_e::execute_edit_action(
|
||||
action_str,
|
||||
// NEW: Use unified canvas handler instead of auth_e::execute_edit_action
|
||||
handle_canvas_state_edit(
|
||||
key,
|
||||
config,
|
||||
register_state,
|
||||
&mut event_handler.ideal_cursor_column,
|
||||
)
|
||||
@@ -336,37 +390,37 @@ pub async fn handle_edit_event(
|
||||
// --- FALLBACK FOR CHARACTER INSERTION (IF NO OTHER BINDING MATCHED) ---
|
||||
if let KeyCode::Char(_) = key.code {
|
||||
let msg = if app_state.ui.show_login {
|
||||
// FIX: Pass &mut event_handler.ideal_cursor_column
|
||||
auth_e::execute_edit_action(
|
||||
"insert_char",
|
||||
// NEW: Use unified canvas handler instead of auth_e::execute_edit_action
|
||||
handle_canvas_state_edit(
|
||||
key,
|
||||
config,
|
||||
login_state,
|
||||
&mut event_handler.ideal_cursor_column,
|
||||
)
|
||||
.await?
|
||||
} else if app_state.ui.show_add_table {
|
||||
// FIX: Pass &mut event_handler.ideal_cursor_column
|
||||
add_table_e::execute_edit_action(
|
||||
"insert_char",
|
||||
// NEW: Use unified canvas handler instead of add_table_e::execute_edit_action
|
||||
handle_canvas_state_edit(
|
||||
key,
|
||||
config,
|
||||
&mut admin_state.add_table_state,
|
||||
&mut event_handler.ideal_cursor_column,
|
||||
)
|
||||
.await?
|
||||
} else if app_state.ui.show_add_logic {
|
||||
// FIX: Pass &mut event_handler.ideal_cursor_column
|
||||
add_logic_e::execute_edit_action(
|
||||
"insert_char",
|
||||
// NEW: Use unified canvas handler instead of add_logic_e::execute_edit_action
|
||||
handle_canvas_state_edit(
|
||||
key,
|
||||
config,
|
||||
&mut admin_state.add_logic_state,
|
||||
&mut event_handler.ideal_cursor_column,
|
||||
)
|
||||
.await?
|
||||
} else if app_state.ui.show_register {
|
||||
// FIX: Pass &mut event_handler.ideal_cursor_column
|
||||
auth_e::execute_edit_action(
|
||||
"insert_char",
|
||||
// NEW: Use unified canvas handler instead of auth_e::execute_edit_action
|
||||
handle_canvas_state_edit(
|
||||
key,
|
||||
config,
|
||||
register_state,
|
||||
&mut event_handler.ideal_cursor_column,
|
||||
)
|
||||
|
||||
@@ -3,14 +3,15 @@
|
||||
use crate::config::binds::config::Config;
|
||||
use crate::config::binds::key_sequences::KeySequenceTracker;
|
||||
use crate::services::grpc_client::GrpcClient;
|
||||
use crate::state::pages::{canvas_state::CanvasState, auth::RegisterState};
|
||||
use crate::state::pages::auth::LoginState;
|
||||
use crate::state::pages::auth::RegisterState;
|
||||
use crate::state::pages::canvas_state::CanvasState as LocalCanvasState;
|
||||
use crate::state::pages::form::FormState;
|
||||
use crate::state::pages::add_logic::AddLogicState;
|
||||
use crate::state::pages::add_table::AddTableState;
|
||||
use crate::state::app::state::AppState;
|
||||
use crate::functions::modes::read_only::{add_logic_ro, auth_ro, form_ro, add_table_ro};
|
||||
use canvas::{canvas::CanvasAction, dispatcher::ActionDispatcher, canvas::ActionResult};
|
||||
use canvas::{canvas::{CanvasAction, CanvasState, ActionResult}, dispatcher::ActionDispatcher};
|
||||
use crossterm::event::KeyEvent;
|
||||
use anyhow::Result;
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
use crate::tui::terminal::core::TerminalCore;
|
||||
use crate::state::app::state::AppState;
|
||||
use crate::state::pages::{form::FormState, auth::LoginState, auth::RegisterState};
|
||||
use crate::state::pages::canvas_state::CanvasState;
|
||||
use canvas::canvas::CanvasState;
|
||||
use anyhow::Result;
|
||||
|
||||
pub struct CommandHandler;
|
||||
|
||||
@@ -8,10 +8,10 @@ use crate::state::pages::auth::LoginState;
|
||||
use crate::state::pages::auth::RegisterState;
|
||||
use crate::state::pages::intro::IntroState;
|
||||
use crate::state::pages::admin::AdminState;
|
||||
use crate::state::pages::canvas_state::CanvasState;
|
||||
use crate::ui::handlers::context::UiContext;
|
||||
use crate::modes::handlers::event::EventOutcome;
|
||||
use crate::modes::general::command_navigation::{handle_command_navigation_event, NavigationState};
|
||||
use canvas::canvas::CanvasState;
|
||||
use anyhow::Result;
|
||||
|
||||
pub async fn handle_navigation_event(
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
// src/state/pages/add_logic.rs
|
||||
use crate::config::binds::config::{EditorConfig, EditorKeybindingMode};
|
||||
use crate::state::pages::canvas_state::CanvasState;
|
||||
use canvas::canvas::{CanvasState, ActionContext, CanvasAction}; // External library
|
||||
use crate::components::common::text_editor::{TextEditor, VimState};
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
@@ -50,7 +50,7 @@ pub struct AddLogicState {
|
||||
pub script_editor_trigger_position: Option<(usize, usize)>, // (line, column)
|
||||
pub all_table_names: Vec<String>,
|
||||
pub script_editor_filter_text: String,
|
||||
|
||||
|
||||
// New fields for same-profile table names and column autocomplete
|
||||
pub same_profile_table_names: Vec<String>, // Tables from same profile only
|
||||
pub script_editor_awaiting_column_autocomplete: Option<String>, // Table name waiting for column fetch
|
||||
@@ -88,7 +88,7 @@ impl AddLogicState {
|
||||
script_editor_trigger_position: None,
|
||||
all_table_names: Vec::new(),
|
||||
script_editor_filter_text: String::new(),
|
||||
|
||||
|
||||
same_profile_table_names: Vec::new(),
|
||||
script_editor_awaiting_column_autocomplete: None,
|
||||
}
|
||||
@@ -181,7 +181,7 @@ impl AddLogicState {
|
||||
}
|
||||
self.same_profile_table_names.contains(&suggestion.to_string())
|
||||
}
|
||||
|
||||
|
||||
/// Sets table columns for autocomplete suggestions
|
||||
pub fn set_table_columns(&mut self, columns: Vec<String>) {
|
||||
self.table_columns_for_suggestions = columns.clone();
|
||||
@@ -225,6 +225,46 @@ impl AddLogicState {
|
||||
self.script_editor_trigger_position = None;
|
||||
self.script_editor_filter_text.clear();
|
||||
}
|
||||
|
||||
/// Helper method to validate and save logic
|
||||
pub fn save_logic(&mut self) -> Option<String> {
|
||||
if self.logic_name_input.trim().is_empty() {
|
||||
return Some("Logic name is required".to_string());
|
||||
}
|
||||
|
||||
if self.target_column_input.trim().is_empty() {
|
||||
return Some("Target column is required".to_string());
|
||||
}
|
||||
|
||||
let script_content = {
|
||||
let editor_borrow = self.script_content_editor.borrow();
|
||||
editor_borrow.lines().join("\n")
|
||||
};
|
||||
|
||||
if script_content.trim().is_empty() {
|
||||
return Some("Script content is required".to_string());
|
||||
}
|
||||
|
||||
// Here you would typically save to database/storage
|
||||
// For now, just clear the form and mark as saved
|
||||
self.has_unsaved_changes = false;
|
||||
Some(format!("Logic '{}' saved successfully", self.logic_name_input.trim()))
|
||||
}
|
||||
|
||||
/// Helper method to clear the form
|
||||
pub fn clear_form(&mut self) -> Option<String> {
|
||||
let profile = self.profile_name.clone();
|
||||
let table_id = self.selected_table_id;
|
||||
let table_name = self.selected_table_name.clone();
|
||||
let editor_config = EditorConfig::default(); // You might want to preserve the actual config
|
||||
|
||||
*self = Self::new(&editor_config);
|
||||
self.profile_name = profile;
|
||||
self.selected_table_id = table_id;
|
||||
self.selected_table_name = table_name;
|
||||
|
||||
Some("Form cleared".to_string())
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for AddLogicState {
|
||||
@@ -233,59 +273,18 @@ impl Default for AddLogicState {
|
||||
}
|
||||
}
|
||||
|
||||
// Implement external library's CanvasState for AddLogicState
|
||||
impl CanvasState for AddLogicState {
|
||||
fn current_field(&self) -> usize {
|
||||
match self.current_focus {
|
||||
AddLogicFocus::InputLogicName => 0,
|
||||
AddLogicFocus::InputTargetColumn => 1,
|
||||
AddLogicFocus::InputDescription => 2,
|
||||
// If focus is elsewhere, return the last canvas field used
|
||||
_ => self.last_canvas_field,
|
||||
}
|
||||
}
|
||||
|
||||
fn current_cursor_pos(&self) -> usize {
|
||||
match self.current_focus {
|
||||
AddLogicFocus::InputLogicName => self.logic_name_cursor_pos,
|
||||
AddLogicFocus::InputTargetColumn => self.target_column_cursor_pos,
|
||||
AddLogicFocus::InputDescription => self.description_cursor_pos,
|
||||
_ => 0,
|
||||
}
|
||||
}
|
||||
|
||||
fn has_unsaved_changes(&self) -> bool {
|
||||
self.has_unsaved_changes
|
||||
}
|
||||
|
||||
fn inputs(&self) -> Vec<&String> {
|
||||
vec![
|
||||
&self.logic_name_input,
|
||||
&self.target_column_input,
|
||||
&self.description_input,
|
||||
]
|
||||
}
|
||||
|
||||
fn get_current_input(&self) -> &str {
|
||||
match self.current_focus {
|
||||
AddLogicFocus::InputLogicName => &self.logic_name_input,
|
||||
AddLogicFocus::InputTargetColumn => &self.target_column_input,
|
||||
AddLogicFocus::InputDescription => &self.description_input,
|
||||
_ => "",
|
||||
}
|
||||
}
|
||||
|
||||
fn get_current_input_mut(&mut self) -> &mut String {
|
||||
match self.current_focus {
|
||||
AddLogicFocus::InputLogicName => &mut self.logic_name_input,
|
||||
AddLogicFocus::InputTargetColumn => &mut self.target_column_input,
|
||||
AddLogicFocus::InputDescription => &mut self.description_input,
|
||||
_ => &mut self.logic_name_input,
|
||||
}
|
||||
}
|
||||
|
||||
fn fields(&self) -> Vec<&str> {
|
||||
vec!["Logic Name", "Target Column", "Description"]
|
||||
}
|
||||
|
||||
fn set_current_field(&mut self, index: usize) {
|
||||
let new_focus = match index {
|
||||
0 => AddLogicFocus::InputLogicName,
|
||||
@@ -303,6 +302,15 @@ impl CanvasState for AddLogicState {
|
||||
}
|
||||
}
|
||||
|
||||
fn current_cursor_pos(&self) -> usize {
|
||||
match self.current_focus {
|
||||
AddLogicFocus::InputLogicName => self.logic_name_cursor_pos,
|
||||
AddLogicFocus::InputTargetColumn => self.target_column_cursor_pos,
|
||||
AddLogicFocus::InputDescription => self.description_cursor_pos,
|
||||
_ => 0,
|
||||
}
|
||||
}
|
||||
|
||||
fn set_current_cursor_pos(&mut self, pos: usize) {
|
||||
match self.current_focus {
|
||||
AddLogicFocus::InputLogicName => {
|
||||
@@ -318,29 +326,117 @@ impl CanvasState for AddLogicState {
|
||||
}
|
||||
}
|
||||
|
||||
fn get_current_input(&self) -> &str {
|
||||
match self.current_focus {
|
||||
AddLogicFocus::InputLogicName => &self.logic_name_input,
|
||||
AddLogicFocus::InputTargetColumn => &self.target_column_input,
|
||||
AddLogicFocus::InputDescription => &self.description_input,
|
||||
_ => "", // Should not happen if called correctly
|
||||
}
|
||||
}
|
||||
|
||||
fn get_current_input_mut(&mut self) -> &mut String {
|
||||
match self.current_focus {
|
||||
AddLogicFocus::InputLogicName => &mut self.logic_name_input,
|
||||
AddLogicFocus::InputTargetColumn => &mut self.target_column_input,
|
||||
AddLogicFocus::InputDescription => &mut self.description_input,
|
||||
_ => &mut self.logic_name_input, // Fallback
|
||||
}
|
||||
}
|
||||
|
||||
fn inputs(&self) -> Vec<&String> {
|
||||
vec![
|
||||
&self.logic_name_input,
|
||||
&self.target_column_input,
|
||||
&self.description_input,
|
||||
]
|
||||
}
|
||||
|
||||
fn fields(&self) -> Vec<&str> {
|
||||
vec!["Logic Name", "Target Column", "Description"]
|
||||
}
|
||||
|
||||
fn has_unsaved_changes(&self) -> bool {
|
||||
self.has_unsaved_changes
|
||||
}
|
||||
|
||||
fn set_has_unsaved_changes(&mut self, changed: bool) {
|
||||
self.has_unsaved_changes = changed;
|
||||
}
|
||||
|
||||
fn get_suggestions(&self) -> Option<&[String]> {
|
||||
if self.current_field() == 1
|
||||
&& self.in_target_column_suggestion_mode
|
||||
&& self.show_target_column_suggestions
|
||||
{
|
||||
Some(&self.target_column_suggestions)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
fn handle_feature_action(&mut self, action: &CanvasAction, _context: &ActionContext) -> Option<String> {
|
||||
match action {
|
||||
// Handle saving logic script
|
||||
CanvasAction::Custom(action_str) if action_str == "save_logic" => {
|
||||
self.save_logic()
|
||||
}
|
||||
|
||||
fn get_selected_suggestion_index(&self) -> Option<usize> {
|
||||
if self.current_field() == 1
|
||||
&& self.in_target_column_suggestion_mode
|
||||
&& self.show_target_column_suggestions
|
||||
{
|
||||
self.selected_target_column_suggestion_index
|
||||
} else {
|
||||
None
|
||||
// Handle clearing the form
|
||||
CanvasAction::Custom(action_str) if action_str == "clear_form" => {
|
||||
self.clear_form()
|
||||
}
|
||||
|
||||
// Handle target column autocomplete activation
|
||||
CanvasAction::Custom(action_str) if action_str == "activate_autocomplete" => {
|
||||
if self.current_field() == 1 { // Target Column field
|
||||
self.in_target_column_suggestion_mode = true;
|
||||
self.update_target_column_suggestions();
|
||||
Some("Autocomplete activated".to_string())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
// Handle target column suggestion selection
|
||||
CanvasAction::Custom(action_str) if action_str == "select_suggestion" => {
|
||||
if self.current_field() == 1 && self.in_target_column_suggestion_mode {
|
||||
if let Some(selected_idx) = self.selected_target_column_suggestion_index {
|
||||
if let Some(suggestion) = self.target_column_suggestions.get(selected_idx) {
|
||||
self.target_column_input = suggestion.clone();
|
||||
self.target_column_cursor_pos = suggestion.len();
|
||||
self.in_target_column_suggestion_mode = false;
|
||||
self.show_target_column_suggestions = false;
|
||||
self.has_unsaved_changes = true;
|
||||
return Some(format!("Selected: {}", suggestion));
|
||||
}
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
// Custom validation when moving between fields
|
||||
CanvasAction::NextField => {
|
||||
match self.current_field() {
|
||||
0 => { // Logic Name field
|
||||
if self.logic_name_input.trim().is_empty() {
|
||||
Some("Logic name cannot be empty".to_string())
|
||||
} else {
|
||||
None // Let canvas library handle the normal field movement
|
||||
}
|
||||
}
|
||||
1 => { // Target Column field
|
||||
// Update suggestions when entering target column field
|
||||
self.update_target_column_suggestions();
|
||||
None
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
// Handle character insertion with validation
|
||||
CanvasAction::InsertChar(c) => {
|
||||
if self.current_field() == 1 { // Target Column field
|
||||
// Update suggestions after character insertion
|
||||
// Note: Canvas library will handle the actual insertion
|
||||
// This is just for triggering suggestion updates
|
||||
None // Let canvas handle insertion, then we'll update suggestions
|
||||
} else {
|
||||
None // Let canvas handle normally
|
||||
}
|
||||
}
|
||||
|
||||
// Let canvas library handle everything else
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
// src/state/pages/add_table.rs
|
||||
use crate::state::pages::canvas_state::CanvasState;
|
||||
use canvas::canvas::{CanvasState, ActionContext, CanvasAction}; // External library
|
||||
use ratatui::widgets::TableState;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
@@ -67,7 +67,6 @@ pub struct AddTableState {
|
||||
|
||||
impl Default for AddTableState {
|
||||
fn default() -> Self {
|
||||
// Initialize with some dummy data for demonstration
|
||||
AddTableState {
|
||||
profile_name: "default".to_string(),
|
||||
table_name: String::new(),
|
||||
@@ -92,16 +91,91 @@ impl Default for AddTableState {
|
||||
|
||||
impl AddTableState {
|
||||
pub const INPUT_FIELD_COUNT: usize = 3;
|
||||
|
||||
/// Helper method to add a column from current inputs
|
||||
pub fn add_column_from_inputs(&mut self) -> Option<String> {
|
||||
if self.column_name_input.trim().is_empty() || self.column_type_input.trim().is_empty() {
|
||||
return Some("Both column name and type are required".to_string());
|
||||
}
|
||||
|
||||
// Check for duplicate column names
|
||||
if self.columns.iter().any(|col| col.name == self.column_name_input.trim()) {
|
||||
return Some("Column name already exists".to_string());
|
||||
}
|
||||
|
||||
// Add the column
|
||||
self.columns.push(ColumnDefinition {
|
||||
name: self.column_name_input.trim().to_string(),
|
||||
data_type: self.column_type_input.trim().to_string(),
|
||||
selected: false,
|
||||
});
|
||||
|
||||
// Clear inputs and reset focus to column name for next entry
|
||||
self.column_name_input.clear();
|
||||
self.column_type_input.clear();
|
||||
self.column_name_cursor_pos = 0;
|
||||
self.column_type_cursor_pos = 0;
|
||||
self.current_focus = AddTableFocus::InputColumnName;
|
||||
self.last_canvas_field = 1;
|
||||
self.has_unsaved_changes = true;
|
||||
|
||||
Some(format!("Column '{}' added successfully", self.columns.last().unwrap().name))
|
||||
}
|
||||
|
||||
/// Helper method to delete selected items
|
||||
pub fn delete_selected_items(&mut self) -> Option<String> {
|
||||
let mut deleted_items = Vec::new();
|
||||
|
||||
// Remove selected columns
|
||||
let initial_column_count = self.columns.len();
|
||||
self.columns.retain(|col| {
|
||||
if col.selected {
|
||||
deleted_items.push(format!("column '{}'", col.name));
|
||||
false
|
||||
} else {
|
||||
true
|
||||
}
|
||||
});
|
||||
|
||||
// Remove selected indexes
|
||||
let initial_index_count = self.indexes.len();
|
||||
self.indexes.retain(|idx| {
|
||||
if idx.selected {
|
||||
deleted_items.push(format!("index '{}'", idx.name));
|
||||
false
|
||||
} else {
|
||||
true
|
||||
}
|
||||
});
|
||||
|
||||
// Remove selected links
|
||||
let initial_link_count = self.links.len();
|
||||
self.links.retain(|link| {
|
||||
if link.selected {
|
||||
deleted_items.push(format!("link to '{}'", link.linked_table_name));
|
||||
false
|
||||
} else {
|
||||
true
|
||||
}
|
||||
});
|
||||
|
||||
if deleted_items.is_empty() {
|
||||
Some("No items selected for deletion".to_string())
|
||||
} else {
|
||||
self.has_unsaved_changes = true;
|
||||
Some(format!("Deleted: {}", deleted_items.join(", ")))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Implement CanvasState for the input fields
|
||||
// Implement external library's CanvasState for AddTableState
|
||||
impl CanvasState for AddTableState {
|
||||
fn current_field(&self) -> usize {
|
||||
match self.current_focus {
|
||||
AddTableFocus::InputTableName => 0,
|
||||
AddTableFocus::InputColumnName => 1,
|
||||
AddTableFocus::InputColumnType => 2,
|
||||
// If focus is elsewhere, default to the first field for canvas rendering logic
|
||||
// If focus is elsewhere, return the last canvas field used
|
||||
_ => self.last_canvas_field,
|
||||
}
|
||||
}
|
||||
@@ -115,37 +189,6 @@ impl CanvasState for AddTableState {
|
||||
}
|
||||
}
|
||||
|
||||
fn has_unsaved_changes(&self) -> bool {
|
||||
self.has_unsaved_changes
|
||||
}
|
||||
|
||||
fn inputs(&self) -> Vec<&String> {
|
||||
vec![&self.table_name_input, &self.column_name_input, &self.column_type_input]
|
||||
}
|
||||
|
||||
fn get_current_input(&self) -> &str {
|
||||
match self.current_focus {
|
||||
AddTableFocus::InputTableName => &self.table_name_input,
|
||||
AddTableFocus::InputColumnName => &self.column_name_input,
|
||||
AddTableFocus::InputColumnType => &self.column_type_input,
|
||||
_ => "", // Should not happen if called correctly
|
||||
}
|
||||
}
|
||||
|
||||
fn get_current_input_mut(&mut self) -> &mut String {
|
||||
match self.current_focus {
|
||||
AddTableFocus::InputTableName => &mut self.table_name_input,
|
||||
AddTableFocus::InputColumnName => &mut self.column_name_input,
|
||||
AddTableFocus::InputColumnType => &mut self.column_type_input,
|
||||
_ => &mut self.table_name_input,
|
||||
}
|
||||
}
|
||||
|
||||
fn fields(&self) -> Vec<&str> {
|
||||
// These must match the order used in render_add_table
|
||||
vec!["Table name", "Name", "Type"]
|
||||
}
|
||||
|
||||
fn set_current_field(&mut self, index: usize) {
|
||||
// Update both current focus and last canvas field
|
||||
self.current_focus = match index {
|
||||
@@ -174,17 +217,84 @@ impl CanvasState for AddTableState {
|
||||
}
|
||||
}
|
||||
|
||||
fn get_current_input(&self) -> &str {
|
||||
match self.current_focus {
|
||||
AddTableFocus::InputTableName => &self.table_name_input,
|
||||
AddTableFocus::InputColumnName => &self.column_name_input,
|
||||
AddTableFocus::InputColumnType => &self.column_type_input,
|
||||
_ => "", // Should not happen if called correctly
|
||||
}
|
||||
}
|
||||
|
||||
fn get_current_input_mut(&mut self) -> &mut String {
|
||||
match self.current_focus {
|
||||
AddTableFocus::InputTableName => &mut self.table_name_input,
|
||||
AddTableFocus::InputColumnName => &mut self.column_name_input,
|
||||
AddTableFocus::InputColumnType => &mut self.column_type_input,
|
||||
_ => &mut self.table_name_input, // Fallback
|
||||
}
|
||||
}
|
||||
|
||||
fn inputs(&self) -> Vec<&String> {
|
||||
vec![&self.table_name_input, &self.column_name_input, &self.column_type_input]
|
||||
}
|
||||
|
||||
fn fields(&self) -> Vec<&str> {
|
||||
// These must match the order used in render_add_table
|
||||
vec!["Table name", "Name", "Type"]
|
||||
}
|
||||
|
||||
fn has_unsaved_changes(&self) -> bool {
|
||||
self.has_unsaved_changes
|
||||
}
|
||||
|
||||
fn set_has_unsaved_changes(&mut self, changed: bool) {
|
||||
self.has_unsaved_changes = changed;
|
||||
}
|
||||
|
||||
// --- Autocomplete Support (Not needed for this form yet) ---
|
||||
fn get_suggestions(&self) -> Option<&[String]> {
|
||||
None
|
||||
}
|
||||
fn handle_feature_action(&mut self, action: &CanvasAction, _context: &ActionContext) -> Option<String> {
|
||||
match action {
|
||||
// Handle adding column when user presses Enter on the Add button or uses specific action
|
||||
CanvasAction::Custom(action_str) if action_str == "add_column" => {
|
||||
self.add_column_from_inputs()
|
||||
}
|
||||
|
||||
fn get_selected_suggestion_index(&self) -> Option<usize> {
|
||||
None
|
||||
// Handle table saving
|
||||
CanvasAction::Custom(action_str) if action_str == "save_table" => {
|
||||
if self.table_name_input.trim().is_empty() {
|
||||
Some("Table name is required".to_string())
|
||||
} else if self.columns.is_empty() {
|
||||
Some("At least one column is required".to_string())
|
||||
} else {
|
||||
Some(format!("Saving table: {}", self.table_name_input))
|
||||
}
|
||||
}
|
||||
|
||||
// Handle deleting selected items
|
||||
CanvasAction::Custom(action_str) if action_str == "delete_selected" => {
|
||||
self.delete_selected_items()
|
||||
}
|
||||
|
||||
// Handle canceling (clear form)
|
||||
CanvasAction::Custom(action_str) if action_str == "cancel" => {
|
||||
// Reset to defaults but keep profile_name
|
||||
let profile = self.profile_name.clone();
|
||||
*self = Self::default();
|
||||
self.profile_name = profile;
|
||||
Some("Form cleared".to_string())
|
||||
}
|
||||
|
||||
// Custom validation when moving between fields
|
||||
CanvasAction::NextField => {
|
||||
// When leaving table name field, update the table_name for display
|
||||
if self.current_field() == 0 && !self.table_name_input.trim().is_empty() {
|
||||
self.table_name = self.table_name_input.trim().to_string();
|
||||
}
|
||||
None // Let canvas library handle the normal field movement
|
||||
}
|
||||
|
||||
// Let canvas library handle everything else
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
// src/state/pages/auth.rs
|
||||
use crate::state::pages::canvas_state::CanvasState;
|
||||
use canvas::canvas::{CanvasState, ActionContext, CanvasAction};
|
||||
use canvas::autocomplete::{AutocompleteCanvasState, AutocompleteState, SuggestionItem};
|
||||
use lazy_static::lazy_static;
|
||||
|
||||
lazy_static! {
|
||||
@@ -44,91 +45,61 @@ pub struct RegisterState {
|
||||
pub current_field: usize,
|
||||
pub current_cursor_pos: usize,
|
||||
pub has_unsaved_changes: bool,
|
||||
pub show_role_suggestions: bool,
|
||||
pub role_suggestions: Vec<String>,
|
||||
pub selected_suggestion_index: Option<usize>,
|
||||
pub in_suggestion_mode: bool,
|
||||
// NEW: Replace old autocomplete with external library's system
|
||||
pub autocomplete: AutocompleteState<String>,
|
||||
}
|
||||
|
||||
impl AuthState {
|
||||
/// Creates a new empty AuthState (unauthenticated)
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
auth_token: None,
|
||||
user_id: None,
|
||||
role: None,
|
||||
decoded_username: None,
|
||||
}
|
||||
Self::default()
|
||||
}
|
||||
}
|
||||
|
||||
impl LoginState {
|
||||
/// Creates a new empty LoginState
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
username: String::new(),
|
||||
password: String::new(),
|
||||
error_message: None,
|
||||
current_field: 0,
|
||||
current_cursor_pos: 0,
|
||||
has_unsaved_changes: false,
|
||||
login_request_pending: false,
|
||||
}
|
||||
Self::default()
|
||||
}
|
||||
}
|
||||
|
||||
impl RegisterState {
|
||||
/// Creates a new empty RegisterState
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
username: String::new(),
|
||||
email: String::new(),
|
||||
password: String::new(),
|
||||
password_confirmation: String::new(),
|
||||
role: String::new(),
|
||||
error_message: None,
|
||||
current_field: 0,
|
||||
current_cursor_pos: 0,
|
||||
has_unsaved_changes: false,
|
||||
show_role_suggestions: false,
|
||||
role_suggestions: Vec::new(),
|
||||
selected_suggestion_index: None,
|
||||
in_suggestion_mode: false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Updates role suggestions based on current input
|
||||
pub fn update_role_suggestions(&mut self) {
|
||||
let current_input = self.role.to_lowercase();
|
||||
self.role_suggestions = AVAILABLE_ROLES
|
||||
let mut state = Self {
|
||||
autocomplete: AutocompleteState::new(),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
// Initialize autocomplete with role suggestions
|
||||
let suggestions: Vec<SuggestionItem<String>> = AVAILABLE_ROLES
|
||||
.iter()
|
||||
.filter(|role| role.to_lowercase().contains(¤t_input))
|
||||
.cloned()
|
||||
.map(|role| SuggestionItem::simple(role.clone(), role.clone()))
|
||||
.collect();
|
||||
self.show_role_suggestions = !self.role_suggestions.is_empty();
|
||||
|
||||
// Set suggestions but keep inactive initially
|
||||
state.autocomplete.set_suggestions(suggestions);
|
||||
state.autocomplete.is_active = false; // Not active by default
|
||||
|
||||
state
|
||||
}
|
||||
}
|
||||
|
||||
// Implement external library's CanvasState for LoginState
|
||||
impl CanvasState for LoginState {
|
||||
fn current_field(&self) -> usize {
|
||||
self.current_field
|
||||
}
|
||||
|
||||
fn current_cursor_pos(&self) -> usize {
|
||||
let len = match self.current_field {
|
||||
0 => self.username.len(),
|
||||
1 => self.password.len(),
|
||||
_ => 0,
|
||||
};
|
||||
self.current_cursor_pos.min(len)
|
||||
self.current_cursor_pos
|
||||
}
|
||||
|
||||
fn has_unsaved_changes(&self) -> bool {
|
||||
self.has_unsaved_changes
|
||||
fn set_current_field(&mut self, index: usize) {
|
||||
if index < 2 {
|
||||
self.current_field = index;
|
||||
}
|
||||
}
|
||||
|
||||
fn inputs(&self) -> Vec<&String> {
|
||||
vec![&self.username, &self.password]
|
||||
fn set_current_cursor_pos(&mut self, pos: usize) {
|
||||
self.current_cursor_pos = pos;
|
||||
}
|
||||
|
||||
fn get_current_input(&self) -> &str {
|
||||
@@ -147,73 +118,61 @@ impl CanvasState for LoginState {
|
||||
}
|
||||
}
|
||||
|
||||
fn inputs(&self) -> Vec<&String> {
|
||||
vec![&self.username, &self.password]
|
||||
}
|
||||
|
||||
fn fields(&self) -> Vec<&str> {
|
||||
vec!["Username/Email", "Password"]
|
||||
}
|
||||
|
||||
fn set_current_field(&mut self, index: usize) {
|
||||
if index < 2 {
|
||||
self.current_field = index;
|
||||
let len = match self.current_field {
|
||||
0 => self.username.len(),
|
||||
1 => self.password.len(),
|
||||
_ => 0,
|
||||
};
|
||||
self.current_cursor_pos = self.current_cursor_pos.min(len);
|
||||
}
|
||||
}
|
||||
|
||||
fn set_current_cursor_pos(&mut self, pos: usize) {
|
||||
let len = match self.current_field {
|
||||
0 => self.username.len(),
|
||||
1 => self.password.len(),
|
||||
_ => 0,
|
||||
};
|
||||
self.current_cursor_pos = pos.min(len);
|
||||
}
|
||||
|
||||
fn set_has_unsaved_changes(&mut self, changed: bool) {
|
||||
self.has_unsaved_changes = changed;
|
||||
}
|
||||
|
||||
fn get_suggestions(&self) -> Option<&[String]> {
|
||||
None
|
||||
}
|
||||
|
||||
fn get_selected_suggestion_index(&self) -> Option<usize> {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
impl CanvasState for RegisterState {
|
||||
fn current_field(&self) -> usize {
|
||||
self.current_field
|
||||
}
|
||||
|
||||
fn current_cursor_pos(&self) -> usize {
|
||||
let len = match self.current_field {
|
||||
0 => self.username.len(),
|
||||
1 => self.email.len(),
|
||||
2 => self.password.len(),
|
||||
3 => self.password_confirmation.len(),
|
||||
4 => self.role.len(),
|
||||
_ => 0,
|
||||
};
|
||||
self.current_cursor_pos.min(len)
|
||||
}
|
||||
|
||||
fn has_unsaved_changes(&self) -> bool {
|
||||
self.has_unsaved_changes
|
||||
}
|
||||
|
||||
fn inputs(&self) -> Vec<&String> {
|
||||
vec![
|
||||
&self.username,
|
||||
&self.email,
|
||||
&self.password,
|
||||
&self.password_confirmation,
|
||||
&self.role,
|
||||
]
|
||||
fn set_has_unsaved_changes(&mut self, changed: bool) {
|
||||
self.has_unsaved_changes = changed;
|
||||
}
|
||||
|
||||
fn handle_feature_action(&mut self, action: &CanvasAction, _context: &ActionContext) -> Option<String> {
|
||||
match action {
|
||||
CanvasAction::Custom(action_str) if action_str == "submit" => {
|
||||
if !self.username.is_empty() && !self.password.is_empty() {
|
||||
Some(format!("Submitting login for: {}", self.username))
|
||||
} else {
|
||||
Some("Please fill in all required fields".to_string())
|
||||
}
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Implement external library's CanvasState for RegisterState
|
||||
impl CanvasState for RegisterState {
|
||||
fn current_field(&self) -> usize {
|
||||
self.current_field
|
||||
}
|
||||
|
||||
fn current_cursor_pos(&self) -> usize {
|
||||
self.current_cursor_pos
|
||||
}
|
||||
|
||||
fn set_current_field(&mut self, index: usize) {
|
||||
if index < 5 {
|
||||
self.current_field = index;
|
||||
|
||||
// Auto-activate autocomplete when moving to role field (index 4)
|
||||
if index == 4 && !self.autocomplete.is_active {
|
||||
self.activate_autocomplete();
|
||||
} else if index != 4 && self.autocomplete.is_active {
|
||||
self.deactivate_autocomplete();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn set_current_cursor_pos(&mut self, pos: usize) {
|
||||
self.current_cursor_pos = pos;
|
||||
}
|
||||
|
||||
fn get_current_input(&self) -> &str {
|
||||
@@ -238,6 +197,16 @@ impl CanvasState for RegisterState {
|
||||
}
|
||||
}
|
||||
|
||||
fn inputs(&self) -> Vec<&String> {
|
||||
vec![
|
||||
&self.username,
|
||||
&self.email,
|
||||
&self.password,
|
||||
&self.password_confirmation,
|
||||
&self.role,
|
||||
]
|
||||
}
|
||||
|
||||
fn fields(&self) -> Vec<&str> {
|
||||
vec![
|
||||
"Username",
|
||||
@@ -248,50 +217,99 @@ impl CanvasState for RegisterState {
|
||||
]
|
||||
}
|
||||
|
||||
fn set_current_field(&mut self, index: usize) {
|
||||
if index < 5 {
|
||||
self.current_field = index;
|
||||
let len = match self.current_field {
|
||||
0 => self.username.len(),
|
||||
1 => self.email.len(),
|
||||
2 => self.password.len(),
|
||||
3 => self.password_confirmation.len(),
|
||||
4 => self.role.len(),
|
||||
_ => 0,
|
||||
};
|
||||
self.current_cursor_pos = self.current_cursor_pos.min(len);
|
||||
}
|
||||
}
|
||||
|
||||
fn set_current_cursor_pos(&mut self, pos: usize) {
|
||||
let len = match self.current_field {
|
||||
0 => self.username.len(),
|
||||
1 => self.email.len(),
|
||||
2 => self.password.len(),
|
||||
3 => self.password_confirmation.len(),
|
||||
4 => self.role.len(),
|
||||
_ => 0,
|
||||
};
|
||||
self.current_cursor_pos = pos.min(len);
|
||||
fn has_unsaved_changes(&self) -> bool {
|
||||
self.has_unsaved_changes
|
||||
}
|
||||
|
||||
fn set_has_unsaved_changes(&mut self, changed: bool) {
|
||||
self.has_unsaved_changes = changed;
|
||||
}
|
||||
|
||||
fn get_suggestions(&self) -> Option<&[String]> {
|
||||
if self.current_field == 4 && self.in_suggestion_mode && self.show_role_suggestions {
|
||||
Some(&self.role_suggestions)
|
||||
fn handle_feature_action(&mut self, action: &CanvasAction, _context: &ActionContext) -> Option<String> {
|
||||
match action {
|
||||
CanvasAction::Custom(action_str) if action_str == "submit" => {
|
||||
if !self.username.is_empty() {
|
||||
Some(format!("Submitting registration for: {}", self.username))
|
||||
} else {
|
||||
Some("Username is required".to_string())
|
||||
}
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Add autocomplete support for RegisterState
|
||||
impl AutocompleteCanvasState for RegisterState {
|
||||
type SuggestionData = String;
|
||||
|
||||
fn supports_autocomplete(&self, field_index: usize) -> bool {
|
||||
field_index == 4 // Only role field supports autocomplete
|
||||
}
|
||||
|
||||
fn autocomplete_state(&self) -> Option<&AutocompleteState<Self::SuggestionData>> {
|
||||
Some(&self.autocomplete)
|
||||
}
|
||||
|
||||
fn autocomplete_state_mut(&mut self) -> Option<&mut AutocompleteState<Self::SuggestionData>> {
|
||||
Some(&mut self.autocomplete)
|
||||
}
|
||||
|
||||
fn activate_autocomplete(&mut self) {
|
||||
let current_field = self.current_field();
|
||||
if self.supports_autocomplete(current_field) {
|
||||
self.autocomplete.activate(current_field);
|
||||
|
||||
// Re-filter suggestions based on current input
|
||||
let current_input = self.role.to_lowercase();
|
||||
let filtered_suggestions: Vec<SuggestionItem<String>> = AVAILABLE_ROLES
|
||||
.iter()
|
||||
.filter(|role| role.to_lowercase().contains(¤t_input))
|
||||
.map(|role| SuggestionItem::simple(role.clone(), role.clone()))
|
||||
.collect();
|
||||
|
||||
self.autocomplete.set_suggestions(filtered_suggestions);
|
||||
}
|
||||
}
|
||||
|
||||
fn deactivate_autocomplete(&mut self) {
|
||||
self.autocomplete.deactivate();
|
||||
}
|
||||
|
||||
fn is_autocomplete_active(&self) -> bool {
|
||||
self.autocomplete.is_active
|
||||
}
|
||||
|
||||
fn is_autocomplete_ready(&self) -> bool {
|
||||
self.autocomplete.is_ready()
|
||||
}
|
||||
|
||||
fn apply_autocomplete_selection(&mut self) -> Option<String> {
|
||||
// First, get the data we need and clone it to avoid borrowing conflicts
|
||||
let selection_info = self.autocomplete.get_selected().map(|selected| {
|
||||
(selected.value_to_store.clone(), selected.display_text.clone())
|
||||
});
|
||||
|
||||
// Now do the mutable operations
|
||||
if let Some((value, display_text)) = selection_info {
|
||||
self.role = value;
|
||||
self.set_has_unsaved_changes(true);
|
||||
self.deactivate_autocomplete();
|
||||
Some(format!("Selected role: {}", display_text))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
fn get_selected_suggestion_index(&self) -> Option<usize> {
|
||||
if self.current_field == 4 && self.in_suggestion_mode && self.show_role_suggestions {
|
||||
self.selected_suggestion_index
|
||||
} else {
|
||||
None
|
||||
fn set_autocomplete_suggestions(&mut self, suggestions: Vec<SuggestionItem<Self::SuggestionData>>) {
|
||||
if let Some(state) = self.autocomplete_state_mut() {
|
||||
state.set_suggestions(suggestions);
|
||||
}
|
||||
}
|
||||
|
||||
fn set_autocomplete_loading(&mut self, loading: bool) {
|
||||
if let Some(state) = self.autocomplete_state_mut() {
|
||||
state.is_loading = loading;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,9 +6,9 @@ use crate::state::pages::auth::LoginState;
|
||||
use crate::state::app::state::AppState;
|
||||
use crate::state::app::buffer::{AppView, BufferState};
|
||||
use crate::config::storage::storage::{StoredAuthData, save_auth_data};
|
||||
use crate::state::pages::canvas_state::CanvasState;
|
||||
use crate::ui::handlers::context::DialogPurpose;
|
||||
use common::proto::komp_ac::auth::LoginResponse;
|
||||
use canvas::canvas::CanvasState;
|
||||
use anyhow::{Context, Result};
|
||||
use tokio::spawn;
|
||||
use tokio::sync::mpsc;
|
||||
|
||||
@@ -4,11 +4,11 @@ use crate::services::auth::AuthClient;
|
||||
use crate::state::{
|
||||
pages::auth::RegisterState,
|
||||
app::state::AppState,
|
||||
pages::canvas_state::CanvasState,
|
||||
};
|
||||
use crate::ui::handlers::context::DialogPurpose;
|
||||
use crate::state::app::buffer::{AppView, BufferState};
|
||||
use common::proto::komp_ac::auth::AuthResponse;
|
||||
use canvas::canvas::CanvasState;
|
||||
use anyhow::Context;
|
||||
use tokio::spawn;
|
||||
use tokio::sync::mpsc;
|
||||
|
||||
@@ -16,7 +16,8 @@ use crate::components::{
|
||||
};
|
||||
use crate::config::colors::themes::Theme;
|
||||
use crate::modes::general::command_navigation::NavigationState;
|
||||
use crate::state::pages::canvas_state::CanvasState;
|
||||
use crate::state::pages::canvas_state::CanvasState as LocalCanvasState; // Keep local one with alias
|
||||
use canvas::canvas::CanvasState; // Import external library's CanvasState trait
|
||||
use crate::state::app::buffer::BufferState;
|
||||
use crate::state::app::highlight::HighlightState as LocalHighlightState; // CHANGED: Alias local version
|
||||
use canvas::canvas::HighlightState as CanvasHighlightState; // CHANGED: Import canvas version with alias
|
||||
@@ -136,7 +137,7 @@ pub fn render_ui(
|
||||
theme,
|
||||
register_state,
|
||||
app_state,
|
||||
register_state.current_field() < 4,
|
||||
register_state.current_field() < 4, // Now using CanvasState trait method
|
||||
highlight_state, // Uses local version
|
||||
);
|
||||
} else if app_state.ui.show_add_table {
|
||||
@@ -166,7 +167,7 @@ pub fn render_ui(
|
||||
theme,
|
||||
login_state,
|
||||
app_state,
|
||||
login_state.current_field() < 2,
|
||||
login_state.current_field() < 2, // Now using CanvasState trait method
|
||||
highlight_state, // Uses local version
|
||||
);
|
||||
} else if app_state.ui.show_admin {
|
||||
@@ -208,7 +209,7 @@ pub fn render_ui(
|
||||
])
|
||||
.split(form_actual_area)[1]
|
||||
};
|
||||
|
||||
|
||||
// CHANGED: Convert local HighlightState to canvas HighlightState for FormState
|
||||
let canvas_highlight_state = convert_highlight_state(highlight_state);
|
||||
form_state.render(
|
||||
|
||||
@@ -8,7 +8,8 @@ use crate::config::storage::storage::load_auth_data;
|
||||
use crate::modes::common::commands::CommandHandler;
|
||||
use crate::modes::handlers::event::{EventHandler, EventOutcome};
|
||||
use crate::modes::handlers::mode_manager::{AppMode, ModeManager};
|
||||
use crate::state::pages::canvas_state::CanvasState;
|
||||
use crate::state::pages::canvas_state::CanvasState as LocalCanvasState; // Keep local one with alias
|
||||
use canvas::canvas::CanvasState; // Import external library's CanvasState trait
|
||||
use crate::state::pages::form::{FormState, FieldDefinition}; // Import FieldDefinition
|
||||
use crate::state::pages::auth::AuthState;
|
||||
use crate::state::pages::auth::LoginState;
|
||||
@@ -38,6 +39,7 @@ use crate::state::app::state::DebugState;
|
||||
#[cfg(feature = "ui-debug")]
|
||||
use crate::utils::debug_logger::pop_next_debug_message;
|
||||
|
||||
// Rest of the file remains the same...
|
||||
pub async fn run_ui() -> Result<()> {
|
||||
let config = Config::load().context("Failed to load configuration")?;
|
||||
let theme = Theme::from_str(&config.colors.theme);
|
||||
@@ -346,25 +348,25 @@ pub async fn run_ui() -> Result<()> {
|
||||
}
|
||||
}
|
||||
|
||||
// Continue with the rest of the function...
|
||||
// (The rest remains the same, but now CanvasState trait methods are available)
|
||||
|
||||
if app_state.ui.show_form {
|
||||
let current_view_profile = app_state.current_view_profile_name.clone();
|
||||
let current_view_table = app_state.current_view_table_name.clone();
|
||||
|
||||
// This condition correctly detects a table switch.
|
||||
if prev_view_profile_name != current_view_profile
|
||||
|| prev_view_table_name != current_view_table
|
||||
{
|
||||
if let (Some(prof_name), Some(tbl_name)) =
|
||||
(current_view_profile.as_ref(), current_view_table.as_ref())
|
||||
{
|
||||
// --- START OF REFACTORED LOGIC ---
|
||||
app_state.show_loading_dialog(
|
||||
"Loading Table",
|
||||
&format!("Fetching data for {}.{}...", prof_name, tbl_name),
|
||||
);
|
||||
needs_redraw = true;
|
||||
|
||||
// 1. Call our new, central function. It handles fetching AND caching.
|
||||
match UiService::load_table_view(
|
||||
&mut grpc_client,
|
||||
&mut app_state,
|
||||
@@ -374,72 +376,62 @@ pub async fn run_ui() -> Result<()> {
|
||||
.await
|
||||
{
|
||||
Ok(mut new_form_state) => {
|
||||
// 2. The function succeeded, we have a new FormState.
|
||||
// Now, fetch its data.
|
||||
if let Err(e) = UiService::fetch_and_set_table_count(
|
||||
&mut grpc_client,
|
||||
&mut new_form_state,
|
||||
)
|
||||
.await
|
||||
{
|
||||
// Handle count fetching error
|
||||
app_state.update_dialog_content(
|
||||
&format!("Error fetching count: {}", e),
|
||||
vec!["OK".to_string()],
|
||||
DialogPurpose::LoginFailed, // Or a more appropriate purpose
|
||||
DialogPurpose::LoginFailed,
|
||||
);
|
||||
} else if new_form_state.total_count > 0 {
|
||||
// If there are records, load the first/last one
|
||||
if let Err(e) = UiService::load_table_data_by_position(
|
||||
&mut grpc_client,
|
||||
&mut new_form_state,
|
||||
)
|
||||
.await
|
||||
{
|
||||
// Handle data loading error
|
||||
app_state.update_dialog_content(
|
||||
&format!("Error loading data: {}", e),
|
||||
vec!["OK".to_string()],
|
||||
DialogPurpose::LoginFailed, // Or a more appropriate purpose
|
||||
DialogPurpose::LoginFailed,
|
||||
);
|
||||
} else {
|
||||
// Success! Hide the loading dialog.
|
||||
app_state.hide_dialog();
|
||||
}
|
||||
} else {
|
||||
// No records, so just reset to an empty form.
|
||||
new_form_state.reset_to_empty();
|
||||
app_state.hide_dialog();
|
||||
}
|
||||
|
||||
// 3. CRITICAL: Replace the old form_state with the new one.
|
||||
form_state = new_form_state;
|
||||
|
||||
// 4. Update our tracking variables.
|
||||
prev_view_profile_name = current_view_profile;
|
||||
prev_view_table_name = current_view_table;
|
||||
table_just_switched = true;
|
||||
}
|
||||
Err(e) => {
|
||||
// This handles errors from load_table_view (e.g., schema fetch failed)
|
||||
app_state.update_dialog_content(
|
||||
&format!("Error loading table: {}", e),
|
||||
vec!["OK".to_string()],
|
||||
DialogPurpose::LoginFailed, // Or a more appropriate purpose
|
||||
DialogPurpose::LoginFailed,
|
||||
);
|
||||
// Revert the view change in app_state to avoid a loop
|
||||
app_state.current_view_profile_name =
|
||||
prev_view_profile_name.clone();
|
||||
app_state.current_view_table_name =
|
||||
prev_view_table_name.clone();
|
||||
}
|
||||
}
|
||||
// --- END OF REFACTORED LOGIC ---
|
||||
}
|
||||
needs_redraw = true;
|
||||
}
|
||||
}
|
||||
|
||||
// Continue with the rest of the positioning logic...
|
||||
// Now we can use CanvasState methods like get_current_input(), current_field(), etc.
|
||||
|
||||
if let Some((profile_name, table_name)) = app_state.pending_table_structure_fetch.take() {
|
||||
if app_state.ui.show_add_logic {
|
||||
if admin_state.add_logic_state.profile_name == profile_name &&
|
||||
|
||||
@@ -5,6 +5,7 @@ use sqlx::{PgPool, Transaction, Postgres};
|
||||
use serde_json::json;
|
||||
use common::proto::komp_ac::table_definition::{PostTableDefinitionRequest, TableDefinitionResponse};
|
||||
|
||||
// TODO CRITICAL add decimal with optional precision"
|
||||
const PREDEFINED_FIELD_TYPES: &[(&str, &str)] = &[
|
||||
("text", "TEXT"),
|
||||
("string", "TEXT"),
|
||||
|
||||
Reference in New Issue
Block a user