Replace Window with ContentDialog when editing a hotkey in HotkeyControl2

This commit is contained in:
Yusyuriv 2024-04-19 19:37:35 +06:00
parent aaf770afa2
commit 51eb8b95a9
No known key found for this signature in database
GPG key ID: A91C52E6F73148E0
3 changed files with 87 additions and 95 deletions

View file

@ -1,6 +1,7 @@
#nullable enable
using System.Collections.ObjectModel;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using Flow.Launcher.Core.Resource;
@ -11,13 +12,16 @@ namespace Flow.Launcher
{
public partial class HotkeyControl2
{
public string WindowTitle { get; set; } = string.Empty;
public string WindowTitle {
get { return (string)GetValue(WindowTitleProperty); }
set { SetValue(WindowTitleProperty, value); }
}
public static readonly DependencyProperty WindowTitleProperty = DependencyProperty.Register(
nameof(WindowTitle),
typeof(string),
typeof(HotkeyControl2),
new PropertyMetadata(default(string))
new PropertyMetadata(string.Empty)
);
/// <summary>
@ -107,29 +111,25 @@ namespace Flow.Launcher
public void GetNewHotkey(object sender, RoutedEventArgs e)
{
OpenHotkeyDialog();
}
private async Task OpenHotkeyDialog()
{
if (!string.IsNullOrEmpty(Hotkey))
{
HotKeyMapper.RemoveHotkey(Hotkey);
}
var owner = Window.GetWindow(this);
var width = owner?.ActualWidth ?? 0;
var height = owner?.ActualHeight ?? 0;
var w = new HotkeyControl2Dialog
{
Title = WindowTitle,
Owner = owner,
Width = width,
Height = height
};
w.ShowDialog();
switch (w.ResultType)
var dialog = new HotkeyControl2Dialog(Hotkey, WindowTitle);
await dialog.ShowAsync();
switch (dialog.ResultType)
{
case HotkeyControl2Dialog.EResultType.Cancel:
return;
case HotkeyControl2Dialog.EResultType.Save:
SetHotkey(w.ResultValue);
SetHotkey(dialog.ResultValue);
break;
case HotkeyControl2Dialog.EResultType.Reset:
ResetToDefault();

View file

@ -1,76 +1,62 @@
<Window x:Class="Flow.Launcher.HotkeyControl2Dialog"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
WindowStartupLocation="CenterOwner"
Width="400"
Height="300"
MinWidth="400"
MinHeight="300"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
Icon="Images\app.ico"
ResizeMode="NoResize"
ShowInTaskbar="False"
Background="Transparent"
AllowsTransparency="True"
WindowStyle="None"
PreviewKeyDown="OnPreviewKeyDown">
<WindowChrome.WindowChrome>
<WindowChrome
CaptionHeight="0"
GlassFrameThickness="0" />
</WindowChrome.WindowChrome>
<Border Background="#AA000000" CornerRadius="8">
<Border Background="DarkGray" Width="400" Height="300" HorizontalAlignment="Center" VerticalAlignment="Center"
CornerRadius="8">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<ui:ContentDialog x:Class="Flow.Launcher.HotkeyControl2Dialog"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ui="http://schemas.modernwpf.com/2019"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
PreviewKeyDown="OnPreviewKeyDown">
<ui:ContentDialog.Resources>
<Thickness x:Key="ContentDialogPadding">0</Thickness>
<Thickness x:Key="ContentDialogTitleMargin">0</Thickness>
</ui:ContentDialog.Resources>
<Grid Grid.Row="0" Grid.Column="0">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Text="{Binding Title}" />
<StackPanel Grid.Row="1" VerticalAlignment="Center" HorizontalAlignment="Center">
<ItemsControl ItemsSource="{Binding KeysToDisplay}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border Background="Red" CornerRadius="6" Padding="8" Margin="0 0 10 0">
<TextBlock Text="{Binding}" />
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</Grid>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Border Grid.Row="0" Grid.Column="1" Background="Gray" CornerRadius="0 8 0 0">
<StackPanel>
<Button Content="Reset" Click="Reset" />
<Button Content="Delete" Click="Delete" />
</StackPanel>
</Border>
<!-- Window title and the keys in the hotkey -->
<Grid Grid.Row="0" Grid.Column="0">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Text="{Binding WindowTitle}" />
<Border Grid.Row="1" Height="200" Width="350">
<StackPanel VerticalAlignment="Center" HorizontalAlignment="Center">
<ItemsControl ItemsSource="{Binding KeysToDisplay}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border Background="Red" CornerRadius="6" Padding="8" Margin="5 0 5 0">
<TextBlock Text="{Binding}" />
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</Border>
</Grid>
<Border Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" Background="Black" CornerRadius="0 0 8 8">
<StackPanel HorizontalAlignment="Center" Orientation="Horizontal" Margin="10">
<!-- Action buttons to the right of the title and keys -->
<StackPanel Grid.Row="0" Grid.Column="1">
<Button Content="Reset" Click="Reset" />
<Button Content="Delete" Click="Delete" />
</StackPanel>
<Button Content="Save" Margin="0 0 10 0" Click="Save" />
<Button Content="Cancel" Click="Cancel" />
</StackPanel>
</Border>
</Grid>
</Border>
</Border>
</Window>
<!-- Action buttons at the bottom of the dialog -->
<StackPanel Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" HorizontalAlignment="Center"
Orientation="Horizontal" Margin="10">
<Button Content="Save" Margin="0 0 10 0" Click="Save" />
<Button Content="Cancel" Click="Cancel" />
</StackPanel>
</Grid>
</ui:ContentDialog>

View file

@ -4,14 +4,16 @@ using System.Windows.Input;
using Flow.Launcher.Core.Resource;
using Flow.Launcher.Infrastructure.Hotkey;
using Flow.Launcher.Plugin;
using ModernWpf.Controls;
namespace Flow.Launcher;
public partial class HotkeyControl2Dialog : Window
public partial class HotkeyControl2Dialog : ContentDialog
{
public string Hotkey { get; set; } = string.Empty;
public HotkeyModel CurrentHotkey { get; set; } = new(false, false, false, false, Key.None);
public ObservableCollection<string> KeysToDisplay { get; set; } = new();
public string WindowTitle { get; }
public HotkeyModel CurrentHotkey { get; private set; }
public ObservableCollection<string> KeysToDisplay { get; } = new();
public enum EResultType
{
@ -24,34 +26,38 @@ public partial class HotkeyControl2Dialog : Window
public EResultType ResultType { get; private set; } = EResultType.Cancel;
public string ResultValue { get; private set; } = string.Empty;
public string EmptyHotkey => InternationalizationManager.Instance.GetTranslation("none");
public HotkeyControl2Dialog()
public HotkeyControl2Dialog(string hotkey, string windowTitle = "")
{
WindowTitle = windowTitle;
CurrentHotkey = new HotkeyModel(hotkey);
SetKeysToDisplay(CurrentHotkey);
InitializeComponent();
}
public void Reset(object sender, RoutedEventArgs routedEventArgs)
{
ResultType = EResultType.Reset;
Close();
Hide();
}
public void Delete(object sender, RoutedEventArgs routedEventArgs)
{
ResultType = EResultType.Delete;
Close();
Hide();
}
public void Cancel(object sender, RoutedEventArgs routedEventArgs)
{
ResultType = EResultType.Cancel;
Close();
Hide();
}
public void Save(object sender, RoutedEventArgs routedEventArgs)
{
ResultType = EResultType.Save;
ResultValue = string.Join("+", KeysToDisplay);
Close();
Hide();
}
private void OnPreviewKeyDown(object sender, KeyEventArgs e)