feat: place caret inside inserted function parentheses\n\n- Add CaretIndex to MainViewModel and bind TextBox.CaretIndex\n- After function insertion, set caret to inside parentheses\n- Return focus to input box for immediate typing

This commit is contained in:
Codex CLI 2025-09-01 03:01:10 -05:00
commit b8ba57a707
3 changed files with 15 additions and 1 deletions

View file

@ -35,6 +35,10 @@ public partial class MainViewModel : ViewModelBase
[ObservableProperty]
private int _selectedHistoryIndex = -1;
// Caret position in the input TextBox
[ObservableProperty]
private int _caretIndex;
public ObservableCollection<HistoryItem> History { get; }
public ObservableCollection<VariableItem> Variables { get; }
@ -61,6 +65,11 @@ public partial class MainViewModel : ViewModelBase
if (string.IsNullOrWhiteSpace(functionDefintionItem.FunctionName))
return;
InsertToken(functionDefintionItem.FunctionName + "()");
// Place caret inside the parentheses
if (!string.IsNullOrEmpty(InputText))
{
CaretIndex = Math.Max(0, InputText.Length - 1);
}
// Close the functions flyout after insertion for smoother UX
IsFunctionsPanelOpen = false;
}

View file

@ -183,7 +183,9 @@
<TextBlock FontFamily="{StaticResource MDI}" Text="{x:Static m:IconFont.Function}" />
</Button>
<TextBox Grid.Column="2" Text="{Binding InputText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" MinHeight="44"
<TextBox x:Name="InputBox" Grid.Column="2" Text="{Binding InputText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
CaretIndex="{Binding CaretIndex, Mode=TwoWay}"
MinHeight="44"
AutomationProperties.Name="Expression input"
Watermark="Type an expression (e.g., 2 + 2)">
<InputElement.KeyBindings>

View file

@ -94,6 +94,9 @@ public partial class MainView : UserControl
_vm?.InsertFunctionCommand.CanExecute(item) == true)
{
_vm.InsertFunctionCommand.Execute(item);
// Return focus to the input box so the caret is active
var input = this.FindControl<TextBox>("InputBox");
input?.Focus();
e.Handled = true;
}
}