Refactor Clcok/Date code to viewmodel

This commit is contained in:
Hongtao Zhang 2022-10-11 23:01:50 -05:00
parent f97fdaf98e
commit 50702d80cc
No known key found for this signature in database
GPG key ID: 75F655B91C7AC9BB
4 changed files with 156 additions and 172 deletions

View file

@ -17,7 +17,7 @@ namespace Flow.Launcher.Helper
internal static void Initialize(MainViewModel mainVM)
{
mainViewModel = mainVM;
settings = mainViewModel._settings;
settings = mainViewModel.Settings;
SetHotkey(settings.Hotkey, OnToggleHotkey);
LoadCustomPluginHotkey();

View file

@ -1,5 +1,4 @@
<Window
x:Class="Flow.Launcher.MainWindow"
<Window x:Class="Flow.Launcher.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:converters="clr-namespace:Flow.Launcher.Converters"
@ -36,144 +35,129 @@
<Window.Resources>
<converters:QuerySuggestionBoxConverter x:Key="QuerySuggestionBoxConverter" />
<converters:BorderClipConverter x:Key="BorderClipConverter" />
<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
</Window.Resources>
<Window.InputBindings>
<KeyBinding Key="Escape" Command="{Binding EscCommand}" />
<KeyBinding Key="F1" Command="{Binding StartHelpCommand}" />
<KeyBinding Key="F5" Command="{Binding ReloadPluginDataCommand}" />
<KeyBinding Key="Tab" Command="{Binding AutocompleteQueryCommand}" />
<KeyBinding
Key="Tab"
<KeyBinding Key="Escape"
Command="{Binding EscCommand}" />
<KeyBinding Key="F1"
Command="{Binding StartHelpCommand}" />
<KeyBinding Key="F5"
Command="{Binding ReloadPluginDataCommand}" />
<KeyBinding Key="Tab"
Command="{Binding AutocompleteQueryCommand}" />
<KeyBinding Key="Tab"
Command="{Binding AutocompleteQueryCommand}"
Modifiers="Shift" />
<KeyBinding
Key="I"
<KeyBinding Key="I"
Command="{Binding OpenSettingCommand}"
Modifiers="Ctrl" />
<KeyBinding
Key="N"
<KeyBinding Key="N"
Command="{Binding SelectNextItemCommand}"
Modifiers="Ctrl" />
<KeyBinding
Key="J"
<KeyBinding Key="J"
Command="{Binding SelectNextItemCommand}"
Modifiers="Ctrl" />
<KeyBinding
Key="D"
<KeyBinding Key="D"
Command="{Binding SelectNextPageCommand}"
Modifiers="Ctrl" />
<KeyBinding
Key="P"
<KeyBinding Key="P"
Command="{Binding SelectPrevItemCommand}"
Modifiers="Ctrl" />
<KeyBinding
Key="K"
<KeyBinding Key="K"
Command="{Binding SelectPrevItemCommand}"
Modifiers="Ctrl" />
<KeyBinding
Key="U"
<KeyBinding Key="U"
Command="{Binding SelectPrevPageCommand}"
Modifiers="Ctrl" />
<KeyBinding
Key="Home"
<KeyBinding Key="Home"
Command="{Binding SelectFirstResultCommand}"
Modifiers="Alt" />
<KeyBinding
Key="O"
<KeyBinding Key="O"
Command="{Binding LoadContextMenuCommand}"
Modifiers="Ctrl" />
<KeyBinding Key="Right" Command="{Binding LoadContextMenuCommand}" />
<KeyBinding Key="Left" Command="{Binding EscCommand}" />
<KeyBinding
Key="H"
<KeyBinding Key="Right"
Command="{Binding LoadContextMenuCommand}" />
<KeyBinding Key="Left"
Command="{Binding EscCommand}" />
<KeyBinding Key="H"
Command="{Binding LoadHistoryCommand}"
Modifiers="Ctrl" />
<KeyBinding
Key="Enter"
<KeyBinding Key="Enter"
Command="{Binding OpenResultCommand}"
Modifiers="Ctrl+Shift" />
<KeyBinding
Key="Enter"
<KeyBinding Key="Enter"
Command="{Binding LoadContextMenuCommand}"
Modifiers="Shift" />
<KeyBinding Key="Enter" Command="{Binding OpenResultCommand}" />
<KeyBinding
Key="Enter"
<KeyBinding Key="Enter"
Command="{Binding OpenResultCommand}" />
<KeyBinding Key="Enter"
Command="{Binding OpenResultCommand}"
Modifiers="Ctrl" />
<KeyBinding
Key="Enter"
<KeyBinding Key="Enter"
Command="{Binding OpenResultCommand}"
Modifiers="Alt" />
<KeyBinding
Key="D1"
<KeyBinding Key="D1"
Command="{Binding OpenResultCommand}"
CommandParameter="0"
Modifiers="{Binding OpenResultCommandModifiers}" />
<KeyBinding
Key="D2"
<KeyBinding Key="D2"
Command="{Binding OpenResultCommand}"
CommandParameter="1"
Modifiers="{Binding OpenResultCommandModifiers}" />
<KeyBinding
Key="D3"
<KeyBinding Key="D3"
Command="{Binding OpenResultCommand}"
CommandParameter="2"
Modifiers="{Binding OpenResultCommandModifiers}" />
<KeyBinding
Key="D4"
<KeyBinding Key="D4"
Command="{Binding OpenResultCommand}"
CommandParameter="3"
Modifiers="{Binding OpenResultCommandModifiers}" />
<KeyBinding
Key="D5"
<KeyBinding Key="D5"
Command="{Binding OpenResultCommand}"
CommandParameter="4"
Modifiers="{Binding OpenResultCommandModifiers}" />
<KeyBinding
Key="D6"
<KeyBinding Key="D6"
Command="{Binding OpenResultCommand}"
CommandParameter="5"
Modifiers="{Binding OpenResultCommandModifiers}" />
<KeyBinding
Key="D7"
<KeyBinding Key="D7"
Command="{Binding OpenResultCommand}"
CommandParameter="6"
Modifiers="{Binding OpenResultCommandModifiers}" />
<KeyBinding
Key="D8"
<KeyBinding Key="D8"
Command="{Binding OpenResultCommand}"
CommandParameter="7"
Modifiers="{Binding OpenResultCommandModifiers}" />
<KeyBinding
Key="D9"
<KeyBinding Key="D9"
Command="{Binding OpenResultCommand}"
CommandParameter="8"
Modifiers="{Binding OpenResultCommandModifiers}" />
<KeyBinding
Key="D0"
<KeyBinding Key="D0"
Command="{Binding OpenResultCommand}"
CommandParameter="9"
Modifiers="{Binding OpenResultCommandModifiers}" />
</Window.InputBindings>
<Grid>
<Border MouseDown="OnMouseDown" Style="{DynamicResource WindowBorderStyle}">
<Border MouseDown="OnMouseDown"
Style="{DynamicResource WindowBorderStyle}">
<StackPanel Orientation="Vertical">
<Grid>
<TextBox
x:Name="QueryTextSuggestionBox"
<TextBox x:Name="QueryTextSuggestionBox"
IsEnabled="False"
Style="{DynamicResource QuerySuggestionBoxStyle}">
<TextBox.Text>
<MultiBinding Converter="{StaticResource QuerySuggestionBoxConverter}">
<Binding ElementName="QueryTextBox" Mode="OneTime" />
<Binding ElementName="ResultListBox" Path="SelectedItem" />
<Binding ElementName="QueryTextBox" Path="Text" />
<Binding ElementName="QueryTextBox"
Mode="OneTime" />
<Binding ElementName="ResultListBox"
Path="SelectedItem" />
<Binding ElementName="QueryTextBox"
Path="Text" />
</MultiBinding>
</TextBox.Text>
</TextBox>
<TextBox
x:Name="QueryTextBox"
<TextBox x:Name="QueryTextBox"
AllowDrop="True"
Background="Transparent"
PreviewDragOver="OnPreviewDragOver"
@ -182,31 +166,42 @@
PreviewKeyUp="QueryTextBox_KeyUp"
Visibility="Visible">
<TextBox.CommandBindings>
<CommandBinding Command="ApplicationCommands.Copy" Executed="OnCopy" />
<CommandBinding Command="ApplicationCommands.Copy"
Executed="OnCopy" />
</TextBox.CommandBindings>
<TextBox.ContextMenu>
<ContextMenu>
<MenuItem Command="ApplicationCommands.Cut" Header="{DynamicResource cut}" />
<MenuItem Command="ApplicationCommands.Copy" Header="{DynamicResource copy}" />
<MenuItem Command="ApplicationCommands.Paste" Header="{DynamicResource paste}" />
<Separator
Margin="0"
<MenuItem Command="ApplicationCommands.Cut"
Header="{DynamicResource cut}" />
<MenuItem Command="ApplicationCommands.Copy"
Header="{DynamicResource copy}" />
<MenuItem Command="ApplicationCommands.Paste"
Header="{DynamicResource paste}" />
<Separator Margin="0"
Padding="0,4,0,4"
Background="{DynamicResource ContextSeparator}" />
<MenuItem Click="OnContextMenusForSettingsClick" Header="{DynamicResource flowlauncher_settings}" />
<MenuItem Command="{Binding EscCommand}" Header="{DynamicResource closeWindow}" />
<MenuItem Click="OnContextMenusForSettingsClick"
Header="{DynamicResource flowlauncher_settings}" />
<MenuItem Command="{Binding EscCommand}"
Header="{DynamicResource closeWindow}" />
</ContextMenu>
</TextBox.ContextMenu>
</TextBox>
<StackPanel x:Name="ClockPanel" Style="{DynamicResource ClockPanel}" IsHitTestVisible="False">
<TextBlock x:Name="DateBox" Style="{DynamicResource DateBox}" />
<TextBlock x:Name="ClockBox" Style="{DynamicResource ClockBox}" />
<StackPanel x:Name="ClockPanel"
Style="{DynamicResource ClockPanel}"
IsHitTestVisible="False">
<TextBlock
Visibility="{Binding Settings.UseDate, Converter={StaticResource BooleanToVisibilityConverter}}"
Style="{DynamicResource DateBox}"
Text="{Binding DateText}" />
<TextBlock Style="{DynamicResource ClockBox}"
Visibility="{Binding Settings.UseClock, Converter={StaticResource BooleanToVisibilityConverter}}"
Text="{Binding ClockText}"/>
</StackPanel>
<Canvas Style="{DynamicResource SearchIconPosition}">
<Image
x:Name="PluginActivationIcon"
<Image x:Name="PluginActivationIcon"
Width="32"
Height="32"
Margin="0,0,0,0"
@ -217,8 +212,7 @@
Source="{Binding PluginIconPath}"
Stretch="Uniform"
Style="{DynamicResource PluginActivationIcon}" />
<Path
Name="SearchIcon"
<Path Name="SearchIcon"
Margin="0"
Data="{DynamicResource SearchIconImg}"
Stretch="Fill"
@ -231,27 +225,32 @@
<ContentControl>
<ContentControl.Style>
<Style TargetType="ContentControl">
<Setter Property="Visibility" Value="Collapsed" />
<Setter Property="Visibility"
Value="Collapsed" />
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=ResultListBox, Path=Visibility}" Value="Visible">
<Setter Property="Visibility" Value="Visible" />
<DataTrigger Binding="{Binding ElementName=ResultListBox, Path=Visibility}"
Value="Visible">
<Setter Property="Visibility"
Value="Visible" />
</DataTrigger>
<DataTrigger Binding="{Binding ElementName=ContextMenu, Path=Visibility}" Value="Visible">
<Setter Property="Visibility" Value="Visible" />
<DataTrigger Binding="{Binding ElementName=ContextMenu, Path=Visibility}"
Value="Visible">
<Setter Property="Visibility"
Value="Visible" />
</DataTrigger>
<DataTrigger Binding="{Binding ElementName=History, Path=Visibility}" Value="Visible">
<Setter Property="Visibility" Value="Visible" />
<DataTrigger Binding="{Binding ElementName=History, Path=Visibility}"
Value="Visible">
<Setter Property="Visibility"
Value="Visible" />
</DataTrigger>
</Style.Triggers>
</Style>
</ContentControl.Style>
<Rectangle
Width="Auto"
<Rectangle Width="Auto"
HorizontalAlignment="Stretch"
Style="{DynamicResource SeparatorStyle}" />
</ContentControl>
<Line
x:Name="ProgressBar"
<Line x:Name="ProgressBar"
Width="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Grid}}, Path=ActualWidth}"
Height="2"
HorizontalAlignment="Right"
@ -267,14 +266,16 @@
<Border Style="{DynamicResource WindowRadius}">
<Border.Clip>
<MultiBinding Converter="{StaticResource BorderClipConverter}">
<Binding Path="ActualWidth" RelativeSource="{RelativeSource Self}" />
<Binding Path="ActualHeight" RelativeSource="{RelativeSource Self}" />
<Binding Path="CornerRadius" RelativeSource="{RelativeSource Self}" />
<Binding Path="ActualWidth"
RelativeSource="{RelativeSource Self}" />
<Binding Path="ActualHeight"
RelativeSource="{RelativeSource Self}" />
<Binding Path="CornerRadius"
RelativeSource="{RelativeSource Self}" />
</MultiBinding>
</Border.Clip>
<ContentControl>
<flowlauncher:ResultListBox
x:Name="ResultListBox"
<flowlauncher:ResultListBox x:Name="ResultListBox"
DataContext="{Binding Results}"
PreviewMouseDown="OnPreviewMouseButtonDown" />
</ContentControl>
@ -282,14 +283,16 @@
<Border Style="{DynamicResource WindowRadius}">
<Border.Clip>
<MultiBinding Converter="{StaticResource BorderClipConverter}">
<Binding Path="ActualWidth" RelativeSource="{RelativeSource Self}" />
<Binding Path="ActualHeight" RelativeSource="{RelativeSource Self}" />
<Binding Path="CornerRadius" RelativeSource="{RelativeSource Self}" />
<Binding Path="ActualWidth"
RelativeSource="{RelativeSource Self}" />
<Binding Path="ActualHeight"
RelativeSource="{RelativeSource Self}" />
<Binding Path="CornerRadius"
RelativeSource="{RelativeSource Self}" />
</MultiBinding>
</Border.Clip>
<ContentControl>
<flowlauncher:ResultListBox
x:Name="ContextMenu"
<flowlauncher:ResultListBox x:Name="ContextMenu"
DataContext="{Binding ContextMenu}"
PreviewMouseDown="OnPreviewMouseButtonDown" />
</ContentControl>
@ -297,14 +300,16 @@
<Border Style="{DynamicResource WindowRadius}">
<Border.Clip>
<MultiBinding Converter="{StaticResource BorderClipConverter}">
<Binding Path="ActualWidth" RelativeSource="{RelativeSource Self}" />
<Binding Path="ActualHeight" RelativeSource="{RelativeSource Self}" />
<Binding Path="CornerRadius" RelativeSource="{RelativeSource Self}" />
<Binding Path="ActualWidth"
RelativeSource="{RelativeSource Self}" />
<Binding Path="ActualHeight"
RelativeSource="{RelativeSource Self}" />
<Binding Path="CornerRadius"
RelativeSource="{RelativeSource Self}" />
</MultiBinding>
</Border.Clip>
<ContentControl>
<flowlauncher:ResultListBox
x:Name="History"
<flowlauncher:ResultListBox x:Name="History"
DataContext="{Binding History}"
PreviewMouseDown="OnPreviewMouseButtonDown" />
</ContentControl>
@ -312,4 +317,4 @@
</StackPanel>
</Border>
</Grid>
</Window>
</Window>

View file

@ -48,17 +48,7 @@ namespace Flow.Launcher
DataContext = mainVM;
_viewModel = mainVM;
_settings = settings;
var timer = new Timer
{
Interval = 1000,
};
timer.Tick += (s, evt) =>
{
ClockDisplay();
};
timer.Start();
InitializeComponent();
InitializePosition();
animationSound.Open(new Uri(AppDomain.CurrentDomain.BaseDirectory + "Resources\\open.wav"));
@ -69,28 +59,7 @@ namespace Flow.Launcher
InitializeComponent();
}
public void ClockDisplay()
{
if (_settings.UseClock == true)
{
ClockBox.Visibility = Visibility.Visible;
ClockBox.Text = System.DateTime.Now.ToString(_settings.TimeFormat);
}
else if (_settings.UseClock == false)
{
ClockBox.Visibility = Visibility.Collapsed;
}
if (_settings.UseDate == true)
{
DateBox.Visibility = Visibility.Visible;
DateBox.Text = System.DateTime.Now.ToString(_settings.DateFormat);
}
else if (_settings.UseDate == false)
{
DateBox.Visibility = Visibility.Collapsed;
}
}
private void OnCopy(object sender, ExecutedRoutedEventArgs e)
{
if (QueryTextBox.SelectionLength == 0)
@ -213,12 +182,6 @@ namespace Flow.Launcher
case nameof(Settings.Hotkey):
UpdateNotifyIconText();
break;
case nameof(Settings.UseClock):
ClockDisplay();
break;
case nameof(Settings.UseDate):
ClockDisplay();
break;
}
};
}

View file

@ -37,7 +37,6 @@ namespace Flow.Launcher.ViewModel
private readonly FlowLauncherJsonStorage<History> _historyItemsStorage;
private readonly FlowLauncherJsonStorage<UserSelectedRecord> _userSelectedRecordStorage;
private readonly FlowLauncherJsonStorage<TopMostRecord> _topMostRecordStorage;
internal readonly Settings _settings;
private readonly History _history;
private readonly UserSelectedRecord _userSelectedRecord;
private readonly TopMostRecord _topMostRecord;
@ -60,8 +59,8 @@ namespace Flow.Launcher.ViewModel
_queryText = "";
_lastQuery = new Query();
_settings = settings;
_settings.PropertyChanged += (_, args) =>
Settings = settings;
Settings.PropertyChanged += (_, args) =>
{
if (args.PropertyName == nameof(Settings.WindowSize))
{
@ -76,14 +75,15 @@ namespace Flow.Launcher.ViewModel
_userSelectedRecord = _userSelectedRecordStorage.Load();
_topMostRecord = _topMostRecordStorage.Load();
ContextMenu = new ResultsViewModel(_settings);
Results = new ResultsViewModel(_settings);
History = new ResultsViewModel(_settings);
ContextMenu = new ResultsViewModel(Settings);
Results = new ResultsViewModel(Settings);
History = new ResultsViewModel(Settings);
_selectedResults = Results;
InitializeKeyCommands();
RegisterViewUpdate();
RegisterResultsUpdatedEvent();
RegisterClockAndDateUpdateAsync();
SetOpenResultModifiers();
}
@ -307,7 +307,7 @@ namespace Flow.Launcher.ViewModel
Notification.Show(
InternationalizationManager.Instance.GetTranslation("success"),
InternationalizationManager.Instance.GetTranslation("completedSuccessfully")
);
);
}), TaskScheduler.Default)
.ConfigureAwait(false);
});
@ -317,10 +317,26 @@ namespace Flow.Launcher.ViewModel
#region ViewModel Properties
public Settings Settings { get; }
public object ClockText { get; private set; }
public string DateText { get; private set; }
private async Task RegisterClockAndDateUpdateAsync()
{
var timer = new PeriodicTimer(TimeSpan.FromSeconds(1));
// ReSharper disable once MethodSupportsCancellation
while (await timer.WaitForNextTickAsync().ConfigureAwait(false))
{
if (Settings.UseClock)
ClockText = DateTime.Now.ToString(Settings.TimeFormat);
if (Settings.UseDate)
DateText = DateTime.Now.ToString(Settings.DateFormat);
}
}
public ResultsViewModel Results { get; private set; }
public ResultsViewModel ContextMenu { get; private set; }
public ResultsViewModel History { get; private set; }
public bool GameModeStatus { get; set; }
@ -411,7 +427,7 @@ namespace Flow.Launcher.ViewModel
public Visibility SearchIconVisibility { get; set; }
public double MainWindowWidth => _settings.WindowSize;
public double MainWindowWidth => Settings.WindowSize;
public string PluginIconPath { get; set; } = null;
@ -592,7 +608,7 @@ namespace Flow.Launcher.ViewModel
PluginIconPath = null;
SearchIconVisibility = Visibility.Visible;
}
if (query.ActionKeyword == Plugin.Query.GlobalPluginWildcardSign)
{
@ -759,7 +775,7 @@ namespace Flow.Launcher.ViewModel
private void SetOpenResultModifiers()
{
OpenResultCommandModifiers = _settings.OpenResultModifiers ?? DefaultOpenResultModifiers;
OpenResultCommandModifiers = Settings.OpenResultModifiers ?? DefaultOpenResultModifiers;
}
public void ToggleFlowLauncher()
@ -788,24 +804,24 @@ namespace Flow.Launcher.ViewModel
// Trick for no delay
MainWindowOpacity = 0;
switch (_settings.LastQueryMode)
switch (Settings.LastQueryMode)
{
case LastQueryMode.Empty:
ChangeQueryText(string.Empty);
await Task.Delay(100); //Time for change to opacity
break;
case LastQueryMode.Preserved:
if (_settings.UseAnimation)
if (Settings.UseAnimation)
await Task.Delay(100);
LastQuerySelected = true;
break;
case LastQueryMode.Selected:
if (_settings.UseAnimation)
if (Settings.UseAnimation)
await Task.Delay(100);
LastQuerySelected = false;
break;
default:
throw new ArgumentException($"wrong LastQueryMode: <{_settings.LastQueryMode}>");
throw new ArgumentException($"wrong LastQueryMode: <{Settings.LastQueryMode}>");
}
MainWindowVisibilityStatus = false;
@ -820,7 +836,7 @@ namespace Flow.Launcher.ViewModel
/// </summary>
public bool ShouldIgnoreHotkeys()
{
return _settings.IgnoreHotkeysOnFullscreen && WindowsInteropHelper.IsWindowFullscreen();
return Settings.IgnoreHotkeysOnFullscreen && WindowsInteropHelper.IsWindowFullscreen();
}
@ -903,18 +919,18 @@ namespace Flow.Launcher.ViewModel
Clipboard.SetFileDropList(paths);
App.API.ShowMsg(
App.API.GetTranslation("copy")
+" "
+ (isFile? App.API.GetTranslation("fileTitle") : App.API.GetTranslation("folderTitle")),
App.API.GetTranslation("copy")
+ " "
+ (isFile ? App.API.GetTranslation("fileTitle") : App.API.GetTranslation("folderTitle")),
App.API.GetTranslation("completedSuccessfully"));
}
else
{
Clipboard.SetDataObject(copyText.ToString());
App.API.ShowMsg(
App.API.GetTranslation("copy")
+ " "
+ App.API.GetTranslation("textTitle"),
App.API.GetTranslation("copy")
+ " "
+ App.API.GetTranslation("textTitle"),
App.API.GetTranslation("completedSuccessfully"));
}
}