[ColorPicker] Close only flyout with escape key (#12441)

When the escape key is pressed in the Color Picker editor, only the
adjust color flyout should be closed instead of the whole window.
This commit is contained in:
Jaime Bernardo
2021-07-27 10:31:43 +01:00
committed by GitHub
parent 5e4f50fa45
commit 65618ffa9c
4 changed files with 31 additions and 4 deletions

View File

@@ -297,6 +297,7 @@
<ui:FlyoutService.Flyout> <ui:FlyoutService.Flyout>
<ui:Flyout x:Name="DetailsFlyout" <ui:Flyout x:Name="DetailsFlyout"
Placement="Bottom" Placement="Bottom"
Opened="DetailsFlyout_Opened"
Closed="DetailsFlyout_Closed"> Closed="DetailsFlyout_Closed">
<Grid Margin="0,4,0,12" <Grid Margin="0,4,0,12"
KeyboardNavigation.TabNavigation="Contained" KeyboardNavigation.TabNavigation="Contained"

View File

@@ -242,6 +242,7 @@ namespace ColorPicker.Controls
#pragma warning restore CA1801 // Review unused parameters #pragma warning restore CA1801 // Review unused parameters
{ {
HideDetails(); HideDetails();
AppStateHandler.BlockEscapeKeyClosingColorPickerEditor = false;
// Revert to original color // Revert to original color
var originalColorBackground = new SolidColorBrush(_originalColor); var originalColorBackground = new SolidColorBrush(_originalColor);
@@ -250,6 +251,15 @@ namespace ColorPicker.Controls
HexCode.Text = ColorToHex(_originalColor); HexCode.Text = ColorToHex(_originalColor);
} }
#pragma warning disable CA1822 // Mark members as static
#pragma warning disable CA1801 // Review unused parameters
private void DetailsFlyout_Opened(object sender, object e)
#pragma warning restore CA1801 // Review unused parameters
#pragma warning restore CA1822 // Mark members as static
{
AppStateHandler.BlockEscapeKeyClosingColorPickerEditor = true;
}
private void ColorVariationButton_Click(object sender, RoutedEventArgs e) private void ColorVariationButton_Click(object sender, RoutedEventArgs e)
{ {
var selectedColor = ((SolidColorBrush)((Button)sender).Background).Color; var selectedColor = ((SolidColorBrush)((Button)sender).Background).Color;

View File

@@ -20,6 +20,9 @@ namespace ColorPicker.Helpers
private bool _colorPickerShown; private bool _colorPickerShown;
private object _colorPickerVisibilityLock = new object(); private object _colorPickerVisibilityLock = new object();
// Blocks using the escape key to close the color picker editor when the adjust color flyout is open.
public static bool BlockEscapeKeyClosingColorPickerEditor { get; set; }
[ImportingConstructor] [ImportingConstructor]
public AppStateHandler(IColorEditorViewModel colorEditorViewModel, IUserSettings userSettings) public AppStateHandler(IColorEditorViewModel colorEditorViewModel, IUserSettings userSettings)
{ {
@@ -36,6 +39,7 @@ namespace ColorPicker.Helpers
public void StartUserSession() public void StartUserSession()
{ {
EndUserSession(); // Ends current user session if there's an active one.
lock (_colorPickerVisibilityLock) lock (_colorPickerVisibilityLock)
{ {
if (!_colorPickerShown && !IsColorPickerEditorVisible()) if (!_colorPickerShown && !IsColorPickerEditorVisible())
@@ -149,7 +153,7 @@ namespace ColorPicker.Helpers
} }
} }
private bool IsColorPickerEditorVisible() public bool IsColorPickerEditorVisible()
{ {
if (_colorEditorWindow != null) if (_colorEditorWindow != null)
{ {
@@ -160,6 +164,11 @@ namespace ColorPicker.Helpers
return false; return false;
} }
public bool IsColorPickerVisible()
{
return _colorPickerShown;
}
private void MainWindow_Closed(object sender, EventArgs e) private void MainWindow_Closed(object sender, EventArgs e)
{ {
AppClosed?.Invoke(this, EventArgs.Empty); AppClosed?.Invoke(this, EventArgs.Empty);

View File

@@ -70,10 +70,17 @@ namespace ColorPicker.Keyboard
var virtualCode = e.KeyboardData.VirtualCode; var virtualCode = e.KeyboardData.VirtualCode;
// ESC pressed // ESC pressed
if (virtualCode == KeyInterop.VirtualKeyFromKey(Key.Escape)) if (virtualCode == KeyInterop.VirtualKeyFromKey(Key.Escape)
&& e.KeyboardState == GlobalKeyboardHook.KeyboardState.KeyDown
)
{ {
e.Handled = _appStateHandler.EndUserSession(); if (_appStateHandler.IsColorPickerVisible()
return; || !AppStateHandler.BlockEscapeKeyClosingColorPickerEditor
)
{
e.Handled = _appStateHandler.EndUserSession();
return;
}
} }
if ((System.Windows.Application.Current as ColorPickerUI.App).IsRunningDetachedFromPowerToys()) if ((System.Windows.Application.Current as ColorPickerUI.App).IsRunningDetachedFromPowerToys())