Skip to content

Code Quality: Fixed an issue where the text is revereted when pasting text via the context menu #17475

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/Files.App.Controls/Omnibar/Omnibar.Events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ args.InputDevice is FocusInputDeviceKind.Keyboard ||

private void AutoSuggestBox_GotFocus(object sender, RoutedEventArgs e)
{
if (IsFocused)
return;

GlobalHelper.WriteDebugStringForOmnibar("The TextBox got the focus.");

IsFocused = true;
Expand All @@ -48,8 +51,8 @@ private void AutoSuggestBox_GotFocus(object sender, RoutedEventArgs e)
private void AutoSuggestBox_LostFocus(object sender, RoutedEventArgs e)
{
// TextBox still has focus if the context menu for selected text is open
var element = Microsoft.UI.Xaml.Input.FocusManager.GetFocusedElement(this.XamlRoot);
if (element is FlyoutBase or Popup)
var element = FocusManager.GetFocusedElement(this.XamlRoot);
if (element is FlyoutBase or Popup || !IsFocused)
return;

GlobalHelper.WriteDebugStringForOmnibar("The TextBox lost the focus.");
Expand All @@ -66,7 +69,7 @@ private void AutoSuggestBox_LostFocus(object sender, RoutedEventArgs e)
}
}

private async void AutoSuggestBox_KeyDown(object sender, KeyRoutedEventArgs e)
private void AutoSuggestBox_KeyDown(object sender, KeyRoutedEventArgs e)
{
if (e.Key is VirtualKey.Enter)
{
Expand Down Expand Up @@ -137,10 +140,15 @@ private void AutoSuggestBox_TextChanged(object sender, TextChangedEventArgs e)
// UpdateSuggestionListView();

if (_textChangeReason is OmnibarTextChangeReason.ProgrammaticChange)
{
_textBox.SelectAll();
}
else
{
_userInput = _textBox.Text;

if (_textChangeReason is OmnibarTextChangeReason.None)
_textChangeReason = OmnibarTextChangeReason.UserInput;
}

TextChanged?.Invoke(this, new(CurrentSelectedMode, _textChangeReason));
Expand Down
15 changes: 9 additions & 6 deletions src/Files.App.Controls/Omnibar/Omnibar.Properties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,27 @@ partial void OnCurrentSelectedModePropertyChanged(DependencyPropertyChangedEvent
CurrentSelectedModeName = newMode.Name;
}

partial void OnCurrentSelectedModeNameChanged(string? newValue)
partial void OnCurrentSelectedModeNamePropertyChanged(DependencyPropertyChangedEventArgs e)
{
if (string.IsNullOrEmpty(newValue) ||
if (e.OldValue is not string oldValue ||
e.NewValue is not string newValue ||
string.IsNullOrEmpty(newValue) ||
string.IsNullOrEmpty(CurrentSelectedMode?.Name) ||
CurrentSelectedMode.Name.Equals(newValue) ||
CurrentSelectedMode.Name.Equals(newValue, StringComparison.OrdinalIgnoreCase) ||
oldValue.Equals(newValue, StringComparison.OrdinalIgnoreCase) ||
Modes is null)
return;

var newMode = Modes.Where(x => x.Name?.Equals(newValue) ?? false).FirstOrDefault();
var newMode = Modes.Where(x => x.Name?.Equals(newValue, StringComparison.OrdinalIgnoreCase) ?? false).FirstOrDefault();
if (newMode is null)
return;

CurrentSelectedMode = newMode;
}

partial void OnIsFocusedChanged(bool newValue)
partial void OnIsFocusedPropertyChanged(DependencyPropertyChangedEventArgs e)
{
if (CurrentSelectedMode is null || _textBox is null)
if (CurrentSelectedMode is null || _textBox is null || e.OldValue is not bool oldValue || e.NewValue is not bool newValue || oldValue == newValue)
return;

GlobalHelper.WriteDebugStringForOmnibar($"{nameof(IsFocused)} has been changed to {IsFocused}");
Expand Down
6 changes: 5 additions & 1 deletion src/Files.App.Controls/Omnibar/Omnibar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -249,10 +249,14 @@ public void ChooseSuggestionItem(object obj, bool isOriginatedFromArrowKey = fal

internal protected void ChangeTextBoxText(string text)
{
if (text.Equals(_textBox.Text, StringComparison.OrdinalIgnoreCase) || CurrentSelectedMode is null)
return;

_textBox.Text = text;
CurrentSelectedMode.Text = text;

// Move the cursor to the end of the TextBox
if (_textChangeReason == OmnibarTextChangeReason.SuggestionChosen)
if (_textChangeReason is OmnibarTextChangeReason.SuggestionChosen)
_textBox?.Select(_textBox.Text.Length, 0);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,6 @@ private void UserSettingsService_OnSettingChangedEvent(object? sender, SettingCh
}
}

[Obsolete("Superseded by Omnibar.")]
public void PathBoxItem_DragLeave(object sender, DragEventArgs e)
{
if (((FrameworkElement)sender).DataContext is not PathBoxItem pathBoxItem ||
Expand All @@ -354,7 +353,6 @@ public void PathBoxItem_DragLeave(object sender, DragEventArgs e)
_dragOverPath = null;
}

[Obsolete("Superseded by Omnibar.")]
public async Task PathBoxItem_Drop(object sender, DragEventArgs e)
{
if (_lockFlag)
Expand Down Expand Up @@ -393,7 +391,6 @@ public async Task PathBoxItem_Drop(object sender, DragEventArgs e)
_lockFlag = false;
}

[Obsolete("Superseded by Omnibar.")]
public async Task PathBoxItem_DragOver(object sender, DragEventArgs e)
{
if (IsSingleItemOverride ||
Expand Down
Loading