Skip to content

Commit

Permalink
When you edit a status, it will now automatically focus into the stat…
Browse files Browse the repository at this point in the history
…us message. You can use Enter to accept the change or Esc to discard it.
  • Loading branch information
BoiHanny committed Jul 22, 2023
1 parent 7f03d12 commit 2c49fed
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 5 deletions.
61 changes: 61 additions & 0 deletions vrcosc-magicchatbox/Classes/XamlCommunication/TextBoxBehaviour.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System.Windows.Input;
using System.Windows;
using vrcosc_magicchatbox.ViewModels;
using System.Windows.Controls;

namespace vrcosc_magicchatbox.Classes
{
public static class TextBoxBehaviour
{
public static bool GetHandleEnterKey(DependencyObject obj)
{
return (bool)obj.GetValue(HandleEnterKeyProperty);
}

public static void SetHandleEnterKey(DependencyObject obj, bool value)
{
obj.SetValue(HandleEnterKeyProperty, value);
}

public static readonly DependencyProperty HandleEnterKeyProperty =
DependencyProperty.RegisterAttached("HandleEnterKey", typeof(bool), typeof(TextBoxBehaviour), new PropertyMetadata(false, HandleEnterKeyPropertyChanged));

private static void HandleEnterKeyPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is TextBox textBox)
{
textBox.KeyDown -= TextBox_KeyDown;

if ((bool)e.NewValue)
{
textBox.KeyDown += TextBox_KeyDown;
}
}
}

private static void TextBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Escape || e.Key == Key.Enter)
{
var textBox = sender as TextBox;
var item = textBox.DataContext as StatusItem;
if (item == null) return;

if (e.Key == Key.Escape)
{
// Run your logic to cancel the edit here
item.editMsg = "";
item.IsEditing = false;
}
else if (e.Key == Key.Enter)
{
// If enter is pressed, IsEditing is set to false
item.editMsg = textBox.Text;
item.IsEditing = false;
}
}
}

}
}

4 changes: 4 additions & 0 deletions vrcosc-magicchatbox/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -737,10 +737,12 @@
Text="{Binding msg}"
Visibility="{Binding IsEditing, Converter={StaticResource InverseBoolToVisibilityConverter}, UpdateSourceTrigger=PropertyChanged}" />
<TextBox
x:Name="EditTextBox"
Grid.Row="0"
Grid.Column="1"
Margin="7,0,30,0"
VerticalAlignment="Center"
local:TextBoxBehaviour.HandleEnterKey="True"
Background="Transparent"
BorderThickness="0"
FontFamily="Comfortaa Light"
Expand All @@ -751,6 +753,8 @@
Text="{Binding editMsg}"
Visibility="{Binding IsEditing, Converter={StaticResource InverseBoolToHiddenConverter}, UpdateSourceTrigger=PropertyChanged}" />



<Button
x:Name="CancelEditbutton"
Grid.Row="0"
Expand Down
20 changes: 15 additions & 5 deletions vrcosc-magicchatbox/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Threading;
using vrcosc_magicchatbox.Classes;
using vrcosc_magicchatbox.Classes.DataAndSecurity;
Expand Down Expand Up @@ -773,27 +774,36 @@ private void Editbutton_Checked(object sender, RoutedEventArgs e)
if ((bool)button.IsChecked)
{
item.editMsg = item.msg;
// Find the TextBox in the same container as the ToggleButton
var parent = VisualTreeHelper.GetParent(button);
while (!(parent is ContentPresenter))
{
parent = VisualTreeHelper.GetParent(parent);
}
var contentPresenter = parent as ContentPresenter;
var dataTemplate = contentPresenter.ContentTemplate;
var editTextBox = (TextBox)dataTemplate.FindName("EditTextBox", contentPresenter);
editTextBox.Focus();
// Set the cursor at the end of the text
editTextBox.CaretIndex = editTextBox.Text.Length;
}
else
{
if(item.editMsg.Count() < 145 && !string.IsNullOrEmpty(item.editMsg))
if (item.editMsg.Count() < 145 && !string.IsNullOrEmpty(item.editMsg))
{

item.msg = item.editMsg;
item.IsEditing = false;
item.editMsg = "";
item.LastEdited = DateTime.Now;
}


}
}
catch (Exception)
{

}
}


private void SortEdited_Click(object sender, RoutedEventArgs e)
{
{
Expand Down

0 comments on commit 2c49fed

Please sign in to comment.