You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
78 lines
2.6 KiB
78 lines
2.6 KiB
using MultiTerm.Core.ViewModel;
|
|
using System;
|
|
using System.Linq;
|
|
using System.Windows.Controls;
|
|
using System.Windows.Input;
|
|
|
|
namespace MultiTerm.Wpf.View;
|
|
|
|
public partial class ConsoleView : UserControl
|
|
{
|
|
private ConsoleViewModel? viewModel;
|
|
|
|
public ConsoleView()
|
|
{
|
|
InitializeComponent();
|
|
}
|
|
|
|
/// <summary>
|
|
/// After user control finished loading, the data context is casted and stored locally.
|
|
/// </summary>
|
|
private void UserControl_Loaded(object sender, System.Windows.RoutedEventArgs e)
|
|
{
|
|
// cast to correct type
|
|
if (this.DataContext is not ConsoleViewModel dataContextVm)
|
|
{
|
|
throw new InvalidOperationException($"{nameof(ConsoleView)} got wrong type of DataContext.");
|
|
}
|
|
// store locally
|
|
this.viewModel = dataContextVm;
|
|
}
|
|
|
|
private void SendableMessageTextBox_PreviewKeyDown(object sender, KeyEventArgs e)
|
|
{
|
|
// send if the key was enter
|
|
if (e.Key == Key.Enter)
|
|
{
|
|
// do not forward key => is handled
|
|
e.Handled = true;
|
|
this.viewModel!.Send();
|
|
}
|
|
// key up => recall previous history element
|
|
else if (e.Key == Key.Up)
|
|
{
|
|
// do not forward key => is handled
|
|
e.Handled = true;
|
|
this.viewModel!.RecallElementFromHistory(previous: true);
|
|
this.sendableMessageTextBox.CaretIndex = this.sendableMessageTextBox.Text.Length; // set cursor to end
|
|
}
|
|
// key down => recall next history element
|
|
else if (e.Key == Key.Down)
|
|
{
|
|
// do not forward key => is handled
|
|
e.Handled = true;
|
|
this.viewModel!.RecallElementFromHistory(previous: false);
|
|
this.sendableMessageTextBox.CaretIndex = this.sendableMessageTextBox.Text.Length; // set cursor to end
|
|
}
|
|
}
|
|
|
|
private void DataTextBox_TextChanged(object sender, TextChangedEventArgs e)
|
|
{
|
|
// scroll to end
|
|
if (sender is not TextBox textBox) { return; }
|
|
textBox.ScrollToEnd();
|
|
}
|
|
|
|
private void SendableMessageTextBox_TextChanged(object sender, TextChangedEventArgs e)
|
|
{
|
|
// more than one change and added length is more than 1 letter
|
|
// => suspect change results from inserting an element from the send history or pasted from the clipboard
|
|
if (e.Changes.Count >= 1 && e.Changes.First().AddedLength > 1)
|
|
{
|
|
// focus the textbox
|
|
this.sendableMessageTextBox.Focus();
|
|
// set cursor to end
|
|
this.sendableMessageTextBox.CaretIndex = this.sendableMessageTextBox.Text.Length;
|
|
}
|
|
}
|
|
} |