use std::cmp::Ordering; use itertools::Itertools;use pathfinder_color::ColorU;use warp_core::features::FeatureFlag;use warp_core::ui::appearance::Appearance;use warp_editor::editor::NavigationKey;use warpui::elements::{ ChildView, ConstrainedBox, Container, CrossAxisAlignment, Fill, Flex, MainAxisAlignment, MainAxisSize, ParentElement, Shrinkable,};use warpui::text_layout::TextStyle;use warpui::ui_components::button::ButtonVariant;use warpui::ui_components::components::{Coords, UiComponent, UiComponentStyles};use warpui::{AppContext, Element, SingletonEntity as _, ViewContext, ViewHandle}; use super::alias_argument_selector::{AliasArgumentSelector, AliasArgumentSelectorEvent};use super::{ BUTTON_BORDER_RADIUS, EDITOR_FONT_SIZE, HORIZONTAL_TEXT_INPUT_PADDING, SECTION_SPACING, VERTICAL_TEXT_INPUT_PADDING, WORKFLOW_PARAMETER_HIGHLIGHT_COLOR, WorkflowAction, WorkflowView, WorkflowViewEvent,};use crate::drive::workflows::workflow_arg_selector::{ WorkflowArgSelector, WorkflowArgSelectorStyles,};use crate::drive::workflows::workflow_arg_type_helpers::{self, ArgumentTypeEditor};use crate::editor::{ EditOrigin, EditorView, Event as EditorEvent, InteractionState, PlainTextEditorViewAction as EditorAction,};use crate::pane_group::PaneEvent;use crate::ui_components::buttons::icon_button;use crate::ui_components::icons::Icon;use crate::workflows::workflow::Workflow;use crate::workspace::WorkspaceAction; const ARGUMENT_INPUT_HEIGHT: f32 = 30.;const ARGUMENT_LABEL_TEXT: &str = "Arguments";const ARGUMENT_LABEL_HEIGHT: f32 = 20.;const ARGUMENT_LABEL_MARGIN_BOTTOM: f32 = 5.;const ARGUMENT_DESCRIPTION_PLACEHOLDER_TEXT: &str = "Description";const ARGUMENT_ALIAS_DESCRIPTION_PLACEHOLDER_TEXT: &str = "Value (optional)";const ARGUMENT_DEFAULT_VALUE_PLACEHOLDER_TEXT: &str = "Default value (optional)";pub const DEFAULT_ARGUMENT_PREFIX: &str = "argument"; /// Width of the argument editor in alias mode.pub const ALIAS_ARGUMENT_EDITOR_WIDTH: f32 = 300.; /// Which version of the argument-editing section to show.#[derive(Debug, Clone, Copy, PartialEq, Eq)]pub enum ArgumentEditorMode { /// Edit argument definitions, as part of editing the workflow itself. WorkflowDefinition, /// Edit argument values for an alias. Alias, /// Edit argument values to fill out and copy. Viewer,} pub struct ArgumentEditorRow { pub(super) name: String, pub(super) description_editor: ViewHandle<EditorView>, pub(super) default_value_editor: ViewHandle<EditorView>, pub(super) argument_editor: ViewHandle<EditorView>, pub arg_type_editor: ViewHandle<WorkflowArgSelector>, // The editor for alias arguments. Can be a text editor or a dropdown. pub alias_argument_selector: ViewHandle<AliasArgumentSelector>,} impl ArgumentTypeEditor for ArgumentEditorRow { fn arg_type_editor(&self) -> &ViewHandle<WorkflowArgSelector> { &self.arg_type_editor }} impl WorkflowView { pub(super) fn update_arguments_rows(&mut self, ctx: &mut ViewContext<Self>) { let appearance = Appearance::as_ref(ctx); let ui_font_family = appearance.ui_font_family(); match self .arguments_rows .len() .cmp(&self.arguments_state.arguments.len()) { Ordering::Equal => { self.arguments_state .arguments .iter() .enumerate() .for_each(|(index, argument)| { self.arguments_rows[index].name.clone_from(&argument.name); }); } Ordering::Less | Ordering::Greater => { // first, get rid of all rows that have names not present in the updated args state let argument_names = self .arguments_state .arguments .iter() .map(|argument| argument.name.clone()) .collect::<Vec<_>>(); self.arguments_rows .retain(|row| argument_names.contains(&row.name)); // next, go over each item in the args state, and either add a row at this position, // or skip over it if we've found a match self.arguments_state .arguments .iter() .enumerate() .for_each(|(index, argument)| { // if we reach the end of the state struct and we still // haven't inserted a row OR we find a mismatched name, // we know to add a row at this particular index if index == self.arguments_rows.len() || !argument.name.eq(&self.arguments_rows[index].name) { let description_editor = Self::create_editor_handle( ctx, Some(EDITOR_FONT_SIZE), Some(ui_font_family), Some(ARGUMENT_DESCRIPTION_PLACEHOLDER_TEXT), false, /* vim_keybindings */ true, false, ); ctx.subscribe_to_view( &description_editor, |me, emitter, event, ctx| { me.handle_argument_editor_event(emitter, event, ctx); }, ); let default_value_editor = Self::create_editor_handle( ctx, Some(EDITOR_FONT_SIZE), Some(ui_font_family), Some(ARGUMENT_DEFAULT_VALUE_PLACEHOLDER_TEXT), false, /* vim_keybindings */ true, false, ); ctx.subscribe_to_view( &default_value_editor, |me, emitter, event, ctx| { me.handle_argument_editor_event(emitter, event, ctx); }, ); let argument_editor = Self::create_editor_handle( ctx, Some(EDITOR_FONT_SIZE), Some(ui_font_family), None, // none at first will be updated later false, true, false, ); ctx.subscribe_to_view(&argument_editor, |me, emitter, event, ctx| { me.handle_argument_editor_event(emitter, event, ctx); }); let arg_type_editor = ctx.add_typed_action_view(|ctx| { WorkflowArgSelector::new( WorkflowArgSelectorStyles { editor_padding: Coords { left: HORIZONTAL_TEXT_INPUT_PADDING, right: HORIZONTAL_TEXT_INPUT_PADDING, top: VERTICAL_TEXT_INPUT_PADDING, bottom: VERTICAL_TEXT_INPUT_PADDING, }, height: Some(ARGUMENT_INPUT_HEIGHT), width: None, dropdown_background: |appearance| { appearance.theme().surface_2() }, border_color: |appearance| { appearance.theme().foreground().with_opacity(20) }, border_radius: BUTTON_BORDER_RADIUS, }, &self.all_workflow_enums, ctx, ) }); ctx.subscribe_to_view(&arg_type_editor, |me, emitter, event, ctx| { me.handle_type_selector_event(emitter, event, ctx); }); let alias_argument_selector = ctx.add_typed_action_view(AliasArgumentSelector::new); ctx.subscribe_to_view( &alias_argument_selector, |me, emitter, event, ctx| { me.handle_alias_argument_selector_event(emitter, event, ctx); }, ); self.arguments_rows.insert( index, ArgumentEditorRow { name: argument.name.clone(), description_editor, default_value_editor, argument_editor, arg_type_editor, alias_argument_selector, }, ); } }); } } } /// Copy argument information (defaults, types, etc.) into the editors for each argument. /// /// This assumes that [`Self::update_arguments_rows`] has been called first. pub(super) fn load_argument_data( &mut self, workflow_data: &Workflow, ctx: &mut ViewContext<Self>, ) { workflow_data .arguments() .iter() .enumerate() .for_each(|(index, argument)| { if let Some(description) = &argument.description { self.arguments_rows[index] .description_editor .update(ctx, |editor, ctx| { editor.set_buffer_text_with_base_buffer(description.as_str(), ctx); }); } self.arguments_rows[index] .arg_type_editor .update(ctx, |selector, ctx| { selector.set_workflow_enums(&self.all_workflow_enums, ctx); workflow_arg_type_helpers::load_argument_into_selector( selector, argument, &mut self.all_workflow_enums, ctx, ); }); if let Some(default_value) = &argument.default_value { self.arguments_rows[index] .default_value_editor .update(ctx, |editor, ctx| { editor.set_buffer_text_with_base_buffer(default_value.as_str(), ctx); }); // Argument editor is used in the view mode only. We're updating the // placeholder to reflect the default value of this argument // (when a user hasn't manually changed the argument in view mode). self.arguments_rows[index] .argument_editor .update(ctx, |editor, ctx| { editor.set_placeholder_text(default_value.as_str(), ctx); }); } else { // Clear the argument editor if there is no default value. self.arguments_rows[index] .argument_editor .update(ctx, |editor, _| { editor.clear_all_placeholder_text(); }); } }); } pub(super) fn has_dirty_argument_editor(&self, app: &AppContext) -> bool { self.arguments_rows.iter().any(|row| { let selector_is_dirty = { let editor = row.arg_type_editor.as_ref(app); let editor_is_dirty = editor.is_dirty(app); let enum_is_dirty = editor .get_selected_enum() .and_then(|id| self.all_workflow_enums.get(&id)) .map(|enum_data| enum_data.new_data.is_some()) .unwrap_or(false); editor_is_dirty || enum_is_dirty }; selector_is_dirty || row.default_value_editor.as_ref(app).is_dirty(app) || row.description_editor.as_ref(app).is_dirty(app) }) } pub(super) fn handle_alias_argument_selector_event( &mut self, handle: ViewHandle<AliasArgumentSelector>, event: &AliasArgumentSelectorEvent, ctx: &mut ViewContext<Self>, ) { match event { AliasArgumentSelectorEvent::ValueSet(value) => { self.arguments_rows.iter().for_each(|row| { if row.alias_argument_selector == handle && self.alias_bar.as_ref(ctx).has_selected_alias() { self.alias_bar.update(ctx, |bar, ctx| { bar.set_current_argument_value(&row.name, value.clone(), ctx); }) } }); } AliasArgumentSelectorEvent::Navigate(NavigationKey::Tab) => { self.arguments_rows .iter() .enumerate() .for_each(|(index, row)| { if row.alias_argument_selector == handle { // If there's another row, tab to its alias argument selector. if let Some(next_row) = self .arguments_rows .get(index + 1) .or(self.arguments_rows.first()) { ctx.focus(&next_row.alias_argument_selector) } } }); } AliasArgumentSelectorEvent::Navigate(NavigationKey::ShiftTab) => { self.arguments_rows .iter() .enumerate() .for_each(|(index, row)| { if row.alias_argument_selector == handle { // If there's a previous row, tab to its argument editor. let previous_row = match index { 0 => self.arguments_rows.last(), _ => self.arguments_rows.get(index - 1), }; if let Some(previous_row) = previous_row { ctx.focus(&previous_row.alias_argument_selector) } } }); } _ => {} } } /// Handle an event from one of the argument definition editors. pub(super) fn handle_argument_editor_event( &mut self, handle: ViewHandle<EditorView>, event: &EditorEvent, ctx: &mut ViewContext<Self>, ) { match event { // because the number of editor views we have depends on how many arguments // are in the command or query, tabbing/shift-tabbing is slightly complex. // `handle_argument_editor_event` is used for all of these views, so broadly // speaking there are two steps for each interaction: // 1. iterate through every row, looking for which editor fired this event // 2. decide what editor to focus next based on what editor's ahead/behind us EditorEvent::Navigate(NavigationKey::Tab) => { self.arguments_rows .iter() .enumerate() .for_each(|(index, row)| { // tabbing in a description editor just means we focus // the corresponding default value editor if row.description_editor == handle { ctx.focus(&row.arg_type_editor); } else if row.default_value_editor == handle { // if we have another row ahead of us, tabbing in the default // value editor moves to the following row's description editor. // otherwise, it wraps around to the title. match self.arguments_rows.get(index + 1) { Some(next_row) => ctx.focus(&next_row.description_editor), None => ctx.focus(&self.name_editor), } } else if row.argument_editor == handle { // If there's another row, tab to its argument editor. if let Some(next_row) = self .arguments_rows .get(index + 1) .or(self.arguments_rows.first()) { ctx.focus(&next_row.argument_editor) } } }); } EditorEvent::Navigate(NavigationKey::ShiftTab) => { self.arguments_rows .iter() .enumerate() .for_each(|(index, row)| { // if we have another row behind us, shift-tabbing in the description // editor moves to the previous row's default value editor. // otherwise, it focuses the content editor. if row.description_editor == handle { if index == 0 { ctx.focus(&self.content_editor); } else { ctx.focus(&self.arguments_rows[index - 1].arg_type_editor); } // shift-tabbing in a default value editor just means we // focus the corresponding default value editor } else if row.default_value_editor == handle { ctx.focus(&row.description_editor); } else if row.argument_editor == handle { // If there's a previous row, tab to its argument editor. let previous_row = match index { 0 => self.arguments_rows.last(), _ => self.arguments_rows.get(index - 1), }; if let Some(previous_row) = previous_row { ctx.focus(&previous_row.argument_editor) } } }); } EditorEvent::Edited(origin) => { self.arguments_rows.iter().for_each(|row| { if row.argument_editor == handle { let mut updated_args = handle.as_ref(ctx).buffer_text(ctx); if self.alias_bar.as_ref(ctx).has_selected_alias() { // When switching between aliases, we repopulate all the argument // editors - don't count that as an edit to the alias. if *origin != EditOrigin::SystemEdit { self.alias_bar.update(ctx, |bar, ctx| { bar.set_current_argument_value(&row.name, updated_args, ctx); }) } } else { // if we don't have anything filled use the default arguments if updated_args.is_empty() { updated_args = row.default_value_editor.as_ref(ctx).buffer_text(ctx); } // if there are no default arguments use the argument name if updated_args.is_empty() { updated_args.clone_from(&row.name); } self.command_display_data .set_argument_value(row.name.clone(), updated_args); let text_style_ranges = self .command_display_data .argument_ranges() .into_iter() .map(|range| { ( range, TextStyle::new().with_background_color(ColorU::from_u32( WORKFLOW_PARAMETER_HIGHLIGHT_COLOR, )), ) }) .collect_vec(); self.view_only_content_editor.update(ctx, |editor, ctx| { // first make it editable so we can make changes editor.set_interaction_state(InteractionState::Editable, ctx); editor.clear_buffer(ctx); editor.insert_with_styles( self.command_display_data.to_command_string().as_str(), //&updated_ranges, &text_style_ranges, EditorAction::SystemInsert, ctx, ); // once done revert to being selectable only editor.set_interaction_state(InteractionState::Selectable, ctx); }); if !self.is_for_agent_mode { // debounce the syntax highlighting change to avoid flicker per // keystroke and only do the highlighting when the editing has ended. // The flicker would occur because we replace the buffer above with // insert_with_styles for capturing arguments changes and then perform // the syntax highlighting here. self.view_only_content_editor_highlight_model.update( ctx, |model, _ctx| { model.debounce_highlight(); }, ); }Lines 1–500 of 850. Load more
use std::cmp::Ordering; use itertools::Itertools; use pathfinder_color::ColorU; use warp_core::features::FeatureFlag; use warp_core::ui::appearance::Appearance; use warp_editor::editor::NavigationKey; use warpui::elements::{ ChildView, ConstrainedBox, Container, CrossAxisAlignment, Fill, Flex, MainAxisAlignment, MainAxisSize, ParentElement, Shrinkable, }; use warpui::text_layout::TextStyle; use warpui::ui_components::button::ButtonVariant; use warpui::ui_components::components::{Coords, UiComponent, UiComponentStyles}; use warpui::{AppContext, Element, SingletonEntity as _, ViewContext, ViewHandle}; use super::alias_argument_selector::{AliasArgumentSelector, AliasArgumentSelectorEvent}; use super::{ BUTTON_BORDER_RADIUS, EDITOR_FONT_SIZE, HORIZONTAL_TEXT_INPUT_PADDING, SECTION_SPACING, VERTICAL_TEXT_INPUT_PADDING, WORKFLOW_PARAMETER_HIGHLIGHT_COLOR, WorkflowAction, WorkflowView, WorkflowViewEvent, }; use crate::drive::workflows::workflow_arg_selector::{ WorkflowArgSelector, WorkflowArgSelectorStyles, }; use crate::drive::workflows::workflow_arg_type_helpers::{self, ArgumentTypeEditor}; use crate::editor::{ EditOrigin, EditorView, Event as EditorEvent, InteractionState, PlainTextEditorViewAction as EditorAction, }; use crate::pane_group::PaneEvent; use crate::ui_components::buttons::icon_button; use crate::ui_components::icons::Icon; use crate::workflows::workflow::Workflow; use crate::workspace::WorkspaceAction; const ARGUMENT_INPUT_HEIGHT: f32 = 30.; const ARGUMENT_LABEL_TEXT: &str = "Arguments"; const ARGUMENT_LABEL_HEIGHT: f32 = 20.; const ARGUMENT_LABEL_MARGIN_BOTTOM: f32 = 5.; const ARGUMENT_DESCRIPTION_PLACEHOLDER_TEXT: &str = "Description"; const ARGUMENT_ALIAS_DESCRIPTION_PLACEHOLDER_TEXT: &str = "Value (optional)"; const ARGUMENT_DEFAULT_VALUE_PLACEHOLDER_TEXT: &str = "Default value (optional)"; pub const DEFAULT_ARGUMENT_PREFIX: &str = "argument"; /// Width of the argument editor in alias mode. pub const ALIAS_ARGUMENT_EDITOR_WIDTH: f32 = 300.; /// Which version of the argument-editing section to show. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum ArgumentEditorMode { /// Edit argument definitions, as part of editing the workflow itself. WorkflowDefinition, /// Edit argument values for an alias. Alias, /// Edit argument values to fill out and copy. Viewer, } pub struct ArgumentEditorRow { pub(super) name: String, pub(super) description_editor: ViewHandle<EditorView>, pub(super) default_value_editor: ViewHandle<EditorView>, pub(super) argument_editor: ViewHandle<EditorView>, pub arg_type_editor: ViewHandle<WorkflowArgSelector>, // The editor for alias arguments. Can be a text editor or a dropdown. pub alias_argument_selector: ViewHandle<AliasArgumentSelector>, } impl ArgumentTypeEditor for ArgumentEditorRow { fn arg_type_editor(&self) -> &ViewHandle<WorkflowArgSelector> { &self.arg_type_editor } } impl WorkflowView { pub(super) fn update_arguments_rows(&mut self, ctx: &mut ViewContext<Self>) { let appearance = Appearance::as_ref(ctx); let ui_font_family = appearance.ui_font_family(); match self .arguments_rows .len() .cmp(&self.arguments_state.arguments.len()) { Ordering::Equal => { self.arguments_state .arguments .iter() .enumerate() .for_each(|(index, argument)| { self.arguments_rows[index].name.clone_from(&argument.name); }); } Ordering::Less | Ordering::Greater => { // first, get rid of all rows that have names not present in the updated args state let argument_names = self .arguments_state .arguments .iter() .map(|argument| argument.name.clone()) .collect::<Vec<_>>(); self.arguments_rows .retain(|row| argument_names.contains(&row.name)); // next, go over each item in the args state, and either add a row at this position, // or skip over it if we've found a match self.arguments_state .arguments .iter() .enumerate() .for_each(|(index, argument)| { // if we reach the end of the state struct and we still // haven't inserted a row OR we find a mismatched name, // we know to add a row at this particular index if index == self.arguments_rows.len() || !argument.name.eq(&self.arguments_rows[index].name) { let description_editor = Self::create_editor_handle( ctx, Some(EDITOR_FONT_SIZE), Some(ui_font_family), Some(ARGUMENT_DESCRIPTION_PLACEHOLDER_TEXT), false, /* vim_keybindings */ true, false, ); ctx.subscribe_to_view( &description_editor, |me, emitter, event, ctx| { me.handle_argument_editor_event(emitter, event, ctx); }, ); let default_value_editor = Self::create_editor_handle( ctx, Some(EDITOR_FONT_SIZE), Some(ui_font_family), Some(ARGUMENT_DEFAULT_VALUE_PLACEHOLDER_TEXT), false, /* vim_keybindings */ true, false, ); ctx.subscribe_to_view( &default_value_editor, |me, emitter, event, ctx| { me.handle_argument_editor_event(emitter, event, ctx); }, ); let argument_editor = Self::create_editor_handle( ctx, Some(EDITOR_FONT_SIZE), Some(ui_font_family), None, // none at first will be updated later false, true, false, ); ctx.subscribe_to_view(&argument_editor, |me, emitter, event, ctx| { me.handle_argument_editor_event(emitter, event, ctx); }); let arg_type_editor = ctx.add_typed_action_view(|ctx| { WorkflowArgSelector::new( WorkflowArgSelectorStyles { editor_padding: Coords { left: HORIZONTAL_TEXT_INPUT_PADDING, right: HORIZONTAL_TEXT_INPUT_PADDING, top: VERTICAL_TEXT_INPUT_PADDING, bottom: VERTICAL_TEXT_INPUT_PADDING, }, height: Some(ARGUMENT_INPUT_HEIGHT), width: None, dropdown_background: |appearance| { appearance.theme().surface_2() }, border_color: |appearance| { appearance.theme().foreground().with_opacity(20) }, border_radius: BUTTON_BORDER_RADIUS, }, &self.all_workflow_enums, ctx, ) }); ctx.subscribe_to_view(&arg_type_editor, |me, emitter, event, ctx| { me.handle_type_selector_event(emitter, event, ctx); }); let alias_argument_selector = ctx.add_typed_action_view(AliasArgumentSelector::new); ctx.subscribe_to_view( &alias_argument_selector, |me, emitter, event, ctx| { me.handle_alias_argument_selector_event(emitter, event, ctx); }, ); self.arguments_rows.insert( index, ArgumentEditorRow { name: argument.name.clone(), description_editor, default_value_editor, argument_editor, arg_type_editor, alias_argument_selector, }, ); } }); } } } /// Copy argument information (defaults, types, etc.) into the editors for each argument. /// /// This assumes that [`Self::update_arguments_rows`] has been called first. pub(super) fn load_argument_data( &mut self, workflow_data: &Workflow, ctx: &mut ViewContext<Self>, ) { workflow_data .arguments() .iter() .enumerate() .for_each(|(index, argument)| { if let Some(description) = &argument.description { self.arguments_rows[index] .description_editor .update(ctx, |editor, ctx| { editor.set_buffer_text_with_base_buffer(description.as_str(), ctx); }); } self.arguments_rows[index] .arg_type_editor .update(ctx, |selector, ctx| { selector.set_workflow_enums(&self.all_workflow_enums, ctx); workflow_arg_type_helpers::load_argument_into_selector( selector, argument, &mut self.all_workflow_enums, ctx, ); }); if let Some(default_value) = &argument.default_value { self.arguments_rows[index] .default_value_editor .update(ctx, |editor, ctx| { editor.set_buffer_text_with_base_buffer(default_value.as_str(), ctx); }); // Argument editor is used in the view mode only. We're updating the // placeholder to reflect the default value of this argument // (when a user hasn't manually changed the argument in view mode). self.arguments_rows[index] .argument_editor .update(ctx, |editor, ctx| { editor.set_placeholder_text(default_value.as_str(), ctx); }); } else { // Clear the argument editor if there is no default value. self.arguments_rows[index] .argument_editor .update(ctx, |editor, _| { editor.clear_all_placeholder_text(); }); } }); } pub(super) fn has_dirty_argument_editor(&self, app: &AppContext) -> bool { self.arguments_rows.iter().any(|row| { let selector_is_dirty = { let editor = row.arg_type_editor.as_ref(app); let editor_is_dirty = editor.is_dirty(app); let enum_is_dirty = editor .get_selected_enum() .and_then(|id| self.all_workflow_enums.get(&id)) .map(|enum_data| enum_data.new_data.is_some()) .unwrap_or(false); editor_is_dirty || enum_is_dirty }; selector_is_dirty || row.default_value_editor.as_ref(app).is_dirty(app) || row.description_editor.as_ref(app).is_dirty(app) }) } pub(super) fn handle_alias_argument_selector_event( &mut self, handle: ViewHandle<AliasArgumentSelector>, event: &AliasArgumentSelectorEvent, ctx: &mut ViewContext<Self>, ) { match event { AliasArgumentSelectorEvent::ValueSet(value) => { self.arguments_rows.iter().for_each(|row| { if row.alias_argument_selector == handle && self.alias_bar.as_ref(ctx).has_selected_alias() { self.alias_bar.update(ctx, |bar, ctx| { bar.set_current_argument_value(&row.name, value.clone(), ctx); }) } }); } AliasArgumentSelectorEvent::Navigate(NavigationKey::Tab) => { self.arguments_rows .iter() .enumerate() .for_each(|(index, row)| { if row.alias_argument_selector == handle { // If there's another row, tab to its alias argument selector. if let Some(next_row) = self .arguments_rows .get(index + 1) .or(self.arguments_rows.first()) { ctx.focus(&next_row.alias_argument_selector) } } }); } AliasArgumentSelectorEvent::Navigate(NavigationKey::ShiftTab) => { self.arguments_rows .iter() .enumerate() .for_each(|(index, row)| { if row.alias_argument_selector == handle { // If there's a previous row, tab to its argument editor. let previous_row = match index { 0 => self.arguments_rows.last(), _ => self.arguments_rows.get(index - 1), }; if let Some(previous_row) = previous_row { ctx.focus(&previous_row.alias_argument_selector) } } }); } _ => {} } } /// Handle an event from one of the argument definition editors. pub(super) fn handle_argument_editor_event( &mut self, handle: ViewHandle<EditorView>, event: &EditorEvent, ctx: &mut ViewContext<Self>, ) { match event { // because the number of editor views we have depends on how many arguments // are in the command or query, tabbing/shift-tabbing is slightly complex. // `handle_argument_editor_event` is used for all of these views, so broadly // speaking there are two steps for each interaction: // 1. iterate through every row, looking for which editor fired this event // 2. decide what editor to focus next based on what editor's ahead/behind us EditorEvent::Navigate(NavigationKey::Tab) => { self.arguments_rows .iter() .enumerate() .for_each(|(index, row)| { // tabbing in a description editor just means we focus // the corresponding default value editor if row.description_editor == handle { ctx.focus(&row.arg_type_editor); } else if row.default_value_editor == handle { // if we have another row ahead of us, tabbing in the default // value editor moves to the following row's description editor. // otherwise, it wraps around to the title. match self.arguments_rows.get(index + 1) { Some(next_row) => ctx.focus(&next_row.description_editor), None => ctx.focus(&self.name_editor), } } else if row.argument_editor == handle { // If there's another row, tab to its argument editor. if let Some(next_row) = self .arguments_rows .get(index + 1) .or(self.arguments_rows.first()) { ctx.focus(&next_row.argument_editor) } } }); } EditorEvent::Navigate(NavigationKey::ShiftTab) => { self.arguments_rows .iter() .enumerate() .for_each(|(index, row)| { // if we have another row behind us, shift-tabbing in the description // editor moves to the previous row's default value editor. // otherwise, it focuses the content editor. if row.description_editor == handle { if index == 0 { ctx.focus(&self.content_editor); } else { ctx.focus(&self.arguments_rows[index - 1].arg_type_editor); } // shift-tabbing in a default value editor just means we // focus the corresponding default value editor } else if row.default_value_editor == handle { ctx.focus(&row.description_editor); } else if row.argument_editor == handle { // If there's a previous row, tab to its argument editor. let previous_row = match index { 0 => self.arguments_rows.last(), _ => self.arguments_rows.get(index - 1), }; if let Some(previous_row) = previous_row { ctx.focus(&previous_row.argument_editor) } } }); } EditorEvent::Edited(origin) => { self.arguments_rows.iter().for_each(|row| { if row.argument_editor == handle { let mut updated_args = handle.as_ref(ctx).buffer_text(ctx); if self.alias_bar.as_ref(ctx).has_selected_alias() { // When switching between aliases, we repopulate all the argument // editors - don't count that as an edit to the alias. if *origin != EditOrigin::SystemEdit { self.alias_bar.update(ctx, |bar, ctx| { bar.set_current_argument_value(&row.name, updated_args, ctx); }) } } else { // if we don't have anything filled use the default arguments if updated_args.is_empty() { updated_args = row.default_value_editor.as_ref(ctx).buffer_text(ctx); } // if there are no default arguments use the argument name if updated_args.is_empty() { updated_args.clone_from(&row.name); } self.command_display_data .set_argument_value(row.name.clone(), updated_args); let text_style_ranges = self .command_display_data .argument_ranges() .into_iter() .map(|range| { ( range, TextStyle::new().with_background_color(ColorU::from_u32( WORKFLOW_PARAMETER_HIGHLIGHT_COLOR, )), ) }) .collect_vec(); self.view_only_content_editor.update(ctx, |editor, ctx| { // first make it editable so we can make changes editor.set_interaction_state(InteractionState::Editable, ctx); editor.clear_buffer(ctx); editor.insert_with_styles( self.command_display_data.to_command_string().as_str(), //&updated_ranges, &text_style_ranges, EditorAction::SystemInsert, ctx, ); // once done revert to being selectable only editor.set_interaction_state(InteractionState::Selectable, ctx); }); if !self.is_for_agent_mode { // debounce the syntax highlighting change to avoid flicker per // keystroke and only do the highlighting when the editing has ended. // The flicker would occur because we replace the buffer above with // insert_with_styles for capturing arguments changes and then perform // the syntax highlighting here. self.view_only_content_editor_highlight_model.update( ctx, |model, _ctx| { model.debounce_highlight(); }, ); }