Merge pull request #1712 from VictoriousRaptor/RefactorMainWindow

Refactor some logic in main window
This commit is contained in:
VictoriousRaptor 2022-12-25 03:35:06 +08:00 committed by GitHub
commit 9027a2085c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 46 additions and 51 deletions

View file

@ -1,4 +1,4 @@
using Flow.Launcher.Infrastructure.Hotkey;
using Flow.Launcher.Infrastructure.Hotkey;
using Flow.Launcher.Infrastructure.UserSettings;
using System;
using NHotkey;
@ -25,7 +25,7 @@ namespace Flow.Launcher.Helper
internal static void OnToggleHotkey(object sender, HotkeyEventArgs args)
{
if (!mainViewModel.ShouldIgnoreHotkeys() && !mainViewModel.GameModeStatus)
if (!mainViewModel.ShouldIgnoreHotkeys())
mainViewModel.ToggleFlowLauncher();
}
@ -74,7 +74,7 @@ namespace Flow.Launcher.Helper
{
SetHotkey(hotkey.Hotkey, (s, e) =>
{
if (mainViewModel.ShouldIgnoreHotkeys() || mainViewModel.GameModeStatus)
if (mainViewModel.ShouldIgnoreHotkeys())
return;
mainViewModel.Show();

View file

@ -86,14 +86,10 @@
Key="O"
Command="{Binding LoadContextMenuCommand}"
Modifiers="Ctrl" />
<KeyBinding Key="Right" Command="{Binding LoadContextMenuCommand}" />
<KeyBinding Key="Left" Command="{Binding EscCommand}" />
<KeyBinding
Key="H"
Command="{Binding LoadHistoryCommand}"
Modifiers="Ctrl" />
<KeyBinding Key="Right" Command="{Binding LoadContextMenuCommand}" />
<KeyBinding Key="Left" Command="{Binding EscCommand}" />
<KeyBinding
Key="OemCloseBrackets"
Command="{Binding IncreaseWidthCommand}"
@ -181,6 +177,10 @@
Command="{Binding OpenResultCommand}"
CommandParameter="9"
Modifiers="{Binding OpenResultCommandModifiers}" />
<KeyBinding
Key="F12"
Command="{Binding ToggleGameModeCommand}"
Modifiers="Ctrl"/>
</Window.InputBindings>
<Grid>
<Border MouseDown="OnMouseDown" Style="{DynamicResource WindowBorderStyle}">

View file

@ -70,6 +70,7 @@ namespace Flow.Launcher
_viewModel.ResultCopy(QueryTextBox.SelectedText);
}
}
private async void OnClosing(object sender, CancelEventArgs e)
{
_settings.WindowTop = Top;
@ -100,6 +101,7 @@ namespace Flow.Launcher
// since the default main window visibility is visible
// so we need set focus during startup
QueryTextBox.Focus();
_viewModel.PropertyChanged += (o, e) =>
{
switch (e.PropertyName)
@ -168,9 +170,12 @@ namespace Flow.Launcher
_viewModel.QueryTextCursorMovedToEnd = false;
}
break;
case nameof(MainViewModel.GameModeStatus):
_notifyIcon.Icon = _viewModel.GameModeStatus ? Properties.Resources.gamemode : Properties.Resources.app;
break;
}
};
_settings.PropertyChanged += (o, e) =>
{
switch (e.PropertyName)
@ -285,7 +290,7 @@ namespace Flow.Launcher
};
open.Click += (o, e) => _viewModel.ToggleFlowLauncher();
gamemode.Click += (o, e) => ToggleGameMode();
gamemode.Click += (o, e) => _viewModel.ToggleGameMode();
positionreset.Click += (o, e) => PositionReset();
settings.Click += (o, e) => App.API.OpenSettingDialog();
exit.Click += (o, e) => Close();
@ -324,24 +329,13 @@ namespace Flow.Launcher
OpenWelcomeWindow();
}
}
private void OpenWelcomeWindow()
{
var WelcomeWindow = new WelcomeWindow(_settings);
WelcomeWindow.Show();
}
private void ToggleGameMode()
{
if (_viewModel.GameModeStatus)
{
_notifyIcon.Icon = Properties.Resources.app;
_viewModel.GameModeStatus = false;
}
else
{
_notifyIcon.Icon = Properties.Resources.gamemode;
_viewModel.GameModeStatus = true;
}
}
private async void PositionReset()
{
_viewModel.Show();
@ -349,6 +343,7 @@ namespace Flow.Launcher
Left = HorizonCenter();
Top = VerticalCenter();
}
private void InitProgressbarAnimation()
{
var da = new DoubleAnimation(ProgressBar.X2, ActualWidth + 100, new Duration(new TimeSpan(0, 0, 0, 0, 1600)));
@ -361,6 +356,7 @@ namespace Flow.Launcher
_viewModel.ProgressBarVisibility = Visibility.Hidden;
isProgressBarStoryboardPaused = true;
}
public void WindowAnimator()
{
if (_animating)
@ -475,7 +471,6 @@ namespace Flow.Launcher
App.API.OpenSettingDialog();
}
private async void OnDeactivated(object sender, EventArgs e)
{
_settings.WindowLeft = Left;
@ -596,12 +591,6 @@ namespace Flow.Launcher
e.Handled = true;
}
break;
case Key.F12:
if (specialKeyState.CtrlPressed)
{
ToggleGameMode();
}
break;
case Key.Back:
if (specialKeyState.CtrlPressed)
{
@ -644,6 +633,7 @@ namespace Flow.Launcher
Preview.Visibility = Visibility.Collapsed;
}
}
public void PreviewToggle()
{

View file

@ -4,7 +4,6 @@ using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using Flow.Launcher.Core.Plugin;
using Flow.Launcher.Core.Resource;
using Flow.Launcher.Helper;
@ -24,7 +23,6 @@ using System.IO;
using System.Collections.Specialized;
using CommunityToolkit.Mvvm.Input;
using System.Globalization;
using System.Windows.Threading;
namespace Flow.Launcher.ViewModel
{
@ -32,8 +30,6 @@ namespace Flow.Launcher.ViewModel
{
#region Private Fields
private const string DefaultOpenResultModifiers = "Alt";
private bool _isQueryRunning;
private Query _lastQuery;
private string _queryTextBeforeLeaveResults;
@ -74,6 +70,9 @@ namespace Flow.Launcher.ViewModel
case nameof(Settings.AlwaysStartEn):
OnPropertyChanged(nameof(StartWithEnglishMode));
break;
case nameof(Settings.OpenResultModifiers):
OnPropertyChanged(nameof(OpenResultCommandModifiers));
break;
}
};
@ -102,9 +101,7 @@ namespace Flow.Launcher.ViewModel
RegisterViewUpdate();
RegisterResultsUpdatedEvent();
RegisterClockAndDateUpdateAsync();
SetOpenResultModifiers();
_ = RegisterClockAndDateUpdateAsync();
}
private void RegisterViewUpdate()
@ -136,8 +133,6 @@ namespace Flow.Launcher.ViewModel
Log.Error("MainViewModel", "Unexpected ResultViewUpdate ends");
}
;
void continueAction(Task t)
{
#if DEBUG
@ -181,6 +176,7 @@ namespace Flow.Launcher.ViewModel
await PluginManager.ReloadDataAsync().ConfigureAwait(false);
Notification.Show(InternationalizationManager.Instance.GetTranslation("success"), InternationalizationManager.Instance.GetTranslation("completedSuccessfully"));
}
[RelayCommand]
private void LoadHistory()
{
@ -194,6 +190,7 @@ namespace Flow.Launcher.ViewModel
SelectedResults = Results;
}
}
[RelayCommand]
private void LoadContextMenu()
{
@ -209,6 +206,7 @@ namespace Flow.Launcher.ViewModel
SelectedResults = Results;
}
}
[RelayCommand]
private void Backspace(object index)
{
@ -221,6 +219,7 @@ namespace Flow.Launcher.ViewModel
ChangeQueryText($"{actionKeyword}{path}");
}
[RelayCommand]
private void AutocompleteQuery()
{
@ -247,6 +246,7 @@ namespace Flow.Launcher.ViewModel
ChangeQueryText(autoCompleteText);
}
}
[RelayCommand]
private async Task OpenResultAsync(string index)
{
@ -281,6 +281,7 @@ namespace Flow.Launcher.ViewModel
SelectedResults = Results;
}
}
[RelayCommand]
private void OpenSetting()
{
@ -298,6 +299,7 @@ namespace Flow.Launcher.ViewModel
{
SelectedResults.SelectFirstResult();
}
[RelayCommand]
private void SelectPrevPage()
{
@ -309,11 +311,13 @@ namespace Flow.Launcher.ViewModel
{
SelectedResults.SelectNextPage();
}
[RelayCommand]
private void SelectPrevItem()
{
SelectedResults.SelectPrevResult();
}
[RelayCommand]
private void SelectNextItem()
{
@ -333,6 +337,12 @@ namespace Flow.Launcher.ViewModel
}
}
[RelayCommand]
public void ToggleGameMode()
{
GameModeStatus = !GameModeStatus;
}
#endregion
#region ViewModel Properties
@ -361,7 +371,7 @@ namespace Flow.Launcher.ViewModel
public ResultsViewModel History { get; private set; }
public bool GameModeStatus { get; set; }
public bool GameModeStatus { get; set; } = false;
private string _queryText;
public string QueryText
@ -375,7 +385,6 @@ namespace Flow.Launcher.ViewModel
}
}
[RelayCommand]
private void IncreaseWidth()
{
@ -513,7 +522,7 @@ namespace Flow.Launcher.ViewModel
public string PluginIconPath { get; set; } = null;
public string OpenResultCommandModifiers { get; private set; }
public string OpenResultCommandModifiers => Settings.OpenResultModifiers;
public string Image => Constant.QueryTextBoxIconImagePath;
@ -521,6 +530,8 @@ namespace Flow.Launcher.ViewModel
#endregion
#region Query
public void Query()
{
if (SelectedIsFromQueryResults())
@ -874,12 +885,9 @@ namespace Flow.Launcher.ViewModel
return selected;
}
#region Hotkey
#endregion
private void SetOpenResultModifiers()
{
OpenResultCommandModifiers = Settings.OpenResultModifiers ?? DefaultOpenResultModifiers;
}
#region Hotkey
public void ToggleFlowLauncher()
{
@ -934,18 +942,15 @@ namespace Flow.Launcher.ViewModel
MainWindowVisibility = Visibility.Collapsed;
}
#endregion
/// <summary>
/// Checks if Flow Launcher should ignore any hotkeys
/// </summary>
public bool ShouldIgnoreHotkeys()
{
return Settings.IgnoreHotkeysOnFullscreen && WindowsInteropHelper.IsWindowFullscreen();
return Settings.IgnoreHotkeysOnFullscreen && WindowsInteropHelper.IsWindowFullscreen() || GameModeStatus;
}
#endregion
#region Public Methods