Flow.Launcher/Flow.Launcher/SettingWindow.xaml.cs

375 lines
13 KiB
C#
Raw Normal View History

using Flow.Launcher.Core.ExternalPlugins;
2020-04-21 09:12:17 +00:00
using Flow.Launcher.Core.Plugin;
using Flow.Launcher.Core.Resource;
using Flow.Launcher.Helper;
2020-11-15 19:29:24 +00:00
using Flow.Launcher.Infrastructure;
using Flow.Launcher.Infrastructure.Hotkey;
2020-04-21 09:12:17 +00:00
using Flow.Launcher.Infrastructure.UserSettings;
using Flow.Launcher.Plugin;
2020-04-23 09:39:23 +00:00
using Flow.Launcher.Plugin.SharedCommands;
2020-04-21 09:12:17 +00:00
using Flow.Launcher.ViewModel;
using Microsoft.Win32;
using ModernWpf;
using System;
using System.IO;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Navigation;
using Button = System.Windows.Controls.Button;
using Control = System.Windows.Controls.Control;
using MessageBox = System.Windows.MessageBox;
using TextBox = System.Windows.Controls.TextBox;
using ThemeManager = ModernWpf.ThemeManager;
2020-04-21 09:12:17 +00:00
namespace Flow.Launcher
{
2016-05-09 21:45:20 +00:00
public partial class SettingWindow
{
2016-05-18 18:35:34 +00:00
private const string StartupPath = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";
2020-06-17 12:16:21 +00:00
public readonly IPublicAPI API;
private Settings settings;
private SettingWindowViewModel viewModel;
2021-10-14 04:34:46 +00:00
private static MainViewModel mainViewModel;
2016-05-21 21:44:27 +00:00
public SettingWindow(IPublicAPI api, SettingWindowViewModel viewModel)
{
InitializeComponent();
2020-06-17 12:16:21 +00:00
settings = viewModel.Settings;
2016-05-21 21:44:27 +00:00
DataContext = viewModel;
2020-06-17 12:16:21 +00:00
this.viewModel = viewModel;
API = api;
}
2015-02-07 13:27:48 +00:00
#region General
private void OnLoaded(object sender, RoutedEventArgs e)
{
RefreshMaximizeRestoreButton();
// Fix (workaround) for the window freezes after lock screen (Win+L)
// https://stackoverflow.com/questions/4951058/software-rendering-mode-wpf
HwndSource hwndSource = PresentationSource.FromVisual(this) as HwndSource;
HwndTarget hwndTarget = hwndSource.CompositionTarget;
hwndTarget.RenderMode = RenderMode.SoftwareOnly;
}
2015-02-07 13:27:48 +00:00
2016-05-21 22:16:32 +00:00
private void OnAutoStartupChecked(object sender, RoutedEventArgs e)
{
2016-05-18 18:35:34 +00:00
SetStartup();
}
2016-05-21 22:16:32 +00:00
private void OnAutoStartupUncheck(object sender, RoutedEventArgs e)
{
2016-05-18 18:35:34 +00:00
RemoveStartup();
2015-01-14 14:19:44 +00:00
}
2016-05-18 18:35:34 +00:00
public static void SetStartup()
2015-01-14 14:19:44 +00:00
{
using var key = Registry.CurrentUser.OpenSubKey(StartupPath, true);
key?.SetValue(Constant.FlowLauncher, Constant.ExecutablePath);
2015-01-14 14:19:44 +00:00
}
2014-03-18 17:20:51 +00:00
2016-05-18 18:35:34 +00:00
private void RemoveStartup()
2015-01-14 14:19:44 +00:00
{
using var key = Registry.CurrentUser.OpenSubKey(StartupPath, true);
key?.DeleteValue(Constant.FlowLauncher, false);
}
2016-05-18 18:35:34 +00:00
public static bool StartupSet()
{
using var key = Registry.CurrentUser.OpenSubKey(StartupPath, true);
var path = key?.GetValue(Constant.FlowLauncher) as string;
if (path != null)
2015-01-14 14:19:44 +00:00
{
return path == Constant.ExecutablePath;
2015-01-14 14:19:44 +00:00
}
return false;
}
2016-05-21 22:16:32 +00:00
private void OnSelectPythonDirectoryClick(object sender, RoutedEventArgs e)
{
var dlg = new FolderBrowserDialog
2016-05-06 20:25:53 +00:00
{
SelectedPath = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles)
};
var result = dlg.ShowDialog();
if (result == System.Windows.Forms.DialogResult.OK)
{
string pythonDirectory = dlg.SelectedPath;
if (!string.IsNullOrEmpty(pythonDirectory))
{
var pythonPath = Path.Combine(pythonDirectory, PluginsLoader.PythonExecutable);
if (File.Exists(pythonPath))
{
2020-06-17 12:16:21 +00:00
settings.PluginSettings.PythonDirectory = pythonDirectory;
2020-04-22 10:26:09 +00:00
MessageBox.Show("Remember to restart Flow Launcher use new Python path");
}
else
{
MessageBox.Show("Can't find python in given directory");
}
}
}
2021-10-30 18:49:23 +00:00
}
private void OnSelectFileManagerClick(object sender, RoutedEventArgs e)
{
2021-12-03 10:11:53 +00:00
SelectFileManagerWindow fileManagerChangeWindow = new SelectFileManagerWindow(settings);
fileManagerChangeWindow.ShowDialog();
}
2016-05-09 01:41:46 +00:00
2021-11-08 09:40:20 +00:00
private void OnSelectDefaultBrowserClick(object sender, RoutedEventArgs e)
{
var browserWindow = new SelectBrowserWindow(settings);
browserWindow.ShowDialog();
2021-11-08 09:40:20 +00:00
}
2015-02-07 13:27:48 +00:00
#endregion
#region Hotkey
private void OnHotkeyControlLoaded(object sender, RoutedEventArgs e)
{
2020-06-17 12:16:21 +00:00
HotkeyControl.SetHotkey(viewModel.Settings.Hotkey, false);
}
2021-12-03 19:45:36 +00:00
private void OnHotkeyControlFocused(object sender, RoutedEventArgs e)
{
HotKeyMapper.RemoveHotkey(settings.Hotkey);
}
2021-12-03 19:45:36 +00:00
private void OnHotkeyControlFocusLost(object sender, RoutedEventArgs e)
2014-02-22 07:52:20 +00:00
{
2016-05-21 18:04:52 +00:00
if (HotkeyControl.CurrentHotkeyAvailable)
2014-02-22 07:52:20 +00:00
{
HotKeyMapper.SetHotkey(HotkeyControl.CurrentHotkey, HotKeyMapper.OnToggleHotkey);
HotKeyMapper.RemoveHotkey(settings.Hotkey);
settings.Hotkey = HotkeyControl.CurrentHotkey.ToString();
2016-02-12 07:22:01 +00:00
}
else
{
HotKeyMapper.SetHotkey(new HotkeyModel(settings.Hotkey), HotKeyMapper.OnToggleHotkey);
}
2016-02-12 07:22:01 +00:00
}
private void OnDeleteCustomHotkeyClick(object sender, RoutedEventArgs e)
2014-02-22 07:52:20 +00:00
{
2020-06-17 12:16:21 +00:00
var item = viewModel.SelectedCustomPluginHotkey;
2015-01-02 08:16:09 +00:00
if (item == null)
{
2015-01-21 15:00:56 +00:00
MessageBox.Show(InternationalizationManager.Instance.GetTranslation("pleaseSelectAnItem"));
2015-01-02 08:16:09 +00:00
return;
}
2016-05-09 01:41:46 +00:00
string deleteWarning =
string.Format(InternationalizationManager.Instance.GetTranslation("deleteCustomHotkeyWarning"),
item.Hotkey);
if (
MessageBox.Show(deleteWarning, InternationalizationManager.Instance.GetTranslation("delete"),
MessageBoxButton.YesNo) == MessageBoxResult.Yes)
2014-02-22 07:52:20 +00:00
{
2020-06-17 12:16:21 +00:00
settings.CustomPluginHotkeys.Remove(item);
HotKeyMapper.RemoveHotkey(item.Hotkey);
2014-02-22 07:52:20 +00:00
}
}
private void OnnEditCustomHotkeyClick(object sender, RoutedEventArgs e)
2014-02-22 07:52:20 +00:00
{
2020-06-17 12:16:21 +00:00
var item = viewModel.SelectedCustomPluginHotkey;
2014-02-22 07:52:20 +00:00
if (item != null)
{
2020-06-17 12:16:21 +00:00
CustomQueryHotkeySetting window = new CustomQueryHotkeySetting(this, settings);
2014-02-22 07:52:20 +00:00
window.UpdateItem(item);
window.ShowDialog();
}
else
{
2015-01-21 15:00:56 +00:00
MessageBox.Show(InternationalizationManager.Instance.GetTranslation("pleaseSelectAnItem"));
2014-02-22 07:52:20 +00:00
}
}
private void OnAddCustomeHotkeyClick(object sender, RoutedEventArgs e)
2014-02-22 07:52:20 +00:00
{
2020-06-17 12:16:21 +00:00
new CustomQueryHotkeySetting(this, settings).ShowDialog();
2014-02-22 07:52:20 +00:00
}
#endregion
2015-02-07 13:27:48 +00:00
#region Plugin
2014-03-26 09:34:19 +00:00
private void OnPluginToggled(object sender, RoutedEventArgs e)
{
2020-06-17 12:16:21 +00:00
var id = viewModel.SelectedPlugin.PluginPair.Metadata.ID;
// used to sync the current status from the plugin manager into the setting to keep consistency after save
2021-01-05 08:11:38 +00:00
settings.PluginSettings.Plugins[id].Disabled = viewModel.SelectedPlugin.PluginPair.Metadata.Disabled;
}
2021-10-07 18:22:56 +00:00
private void OnPluginPriorityClick(object sender, RoutedEventArgs e)
2021-01-05 08:11:38 +00:00
{
if (sender is Control { DataContext: PluginViewModel pluginViewModel })
{
PriorityChangeWindow priorityChangeWindow = new PriorityChangeWindow(pluginViewModel.PluginPair.Metadata.ID, settings, pluginViewModel);
2021-01-05 08:11:38 +00:00
priorityChangeWindow.ShowDialog();
}
}
private void OnPluginActionKeywordsClick(object sender, RoutedEventArgs e)
{
var id = viewModel.SelectedPlugin.PluginPair.Metadata.ID;
ActionKeywords changeKeywordsWindow = new ActionKeywords(id, settings, viewModel.SelectedPlugin);
changeKeywordsWindow.ShowDialog();
}
2014-07-04 07:12:22 +00:00
private void OnPluginNameClick(object sender, MouseButtonEventArgs e)
2014-07-04 07:12:22 +00:00
{
if (e.ChangedButton == MouseButton.Left)
2014-07-04 07:12:22 +00:00
{
2020-06-17 12:16:21 +00:00
var website = viewModel.SelectedPlugin.PluginPair.Metadata.Website;
2016-07-30 15:16:28 +00:00
if (!string.IsNullOrEmpty(website))
2014-07-04 07:12:22 +00:00
{
2016-07-30 15:16:28 +00:00
var uri = new Uri(website);
if (Uri.CheckSchemeName(uri.Scheme))
2014-07-04 07:12:22 +00:00
{
website.NewTabInBrowser();
2014-07-04 07:12:22 +00:00
}
}
}
}
private void OnPluginDirecotyClick(object sender, MouseButtonEventArgs e)
{
if (e.ChangedButton == MouseButton.Left)
{
2020-06-17 12:16:21 +00:00
var directory = viewModel.SelectedPlugin.PluginPair.Metadata.PluginDirectory;
if (!string.IsNullOrEmpty(directory))
PluginManager.API.OpenDirectory(directory);
}
}
2015-02-07 13:27:48 +00:00
#endregion
#region Proxy
2016-05-09 01:41:46 +00:00
2016-05-22 21:49:41 +00:00
private void OnTestProxyClick(object sender, RoutedEventArgs e)
{ // TODO: change to command
2020-06-17 12:16:21 +00:00
var msg = viewModel.TestProxy();
MessageBox.Show(msg); // TODO: add message box service
2014-07-18 12:00:55 +00:00
}
2014-12-16 14:25:22 +00:00
2015-02-07 13:27:48 +00:00
#endregion
private async void OnCheckUpdates(object sender, RoutedEventArgs e)
2016-05-09 01:41:46 +00:00
{
2020-06-17 12:16:21 +00:00
viewModel.UpdateApp(); // TODO: change to command
2016-05-09 01:41:46 +00:00
}
private void OnRequestNavigate(object sender, RequestNavigateEventArgs e)
{
2021-12-05 19:34:23 +00:00
API.OpenUrl(e.Uri.AbsoluteUri);
e.Handled = true;
}
2016-05-22 23:36:48 +00:00
private void OnClosed(object sender, EventArgs e)
{
2020-06-17 12:16:21 +00:00
viewModel.Save();
2016-05-22 23:36:48 +00:00
}
private void OnCloseExecuted(object sender, ExecutedRoutedEventArgs e)
{
Close();
}
private void OpenThemeFolder(object sender, RoutedEventArgs e)
{
PluginManager.API.OpenDirectory(Path.Combine(DataLocation.DataDirectory(), Constant.Themes));
}
2021-01-05 08:11:38 +00:00
2021-11-16 15:28:42 +00:00
private void OpenSettingFolder(object sender, RoutedEventArgs e)
{
PluginManager.API.OpenDirectory(Path.Combine(DataLocation.DataDirectory(), Constant.Settings));
}
private void OpenWelcomeWindow(object sender, RoutedEventArgs e)
{
2021-12-03 10:11:53 +00:00
var WelcomeWindow = new WelcomeWindow(settings);
WelcomeWindow.ShowDialog();
}
2021-11-16 15:28:42 +00:00
private void OpenLogFolder(object sender, RoutedEventArgs e)
{
PluginManager.API.OpenDirectory(Path.Combine(DataLocation.DataDirectory(), Constant.Logs, Constant.Version));
2021-11-16 15:28:42 +00:00
}
private void OnPluginStoreRefreshClick(object sender, RoutedEventArgs e)
{
_ = viewModel.RefreshExternalPluginsAsync();
}
private void OnExternalPluginInstallClick(object sender, RoutedEventArgs e)
{
if(sender is Button { DataContext: UserPlugin plugin })
{
var pluginsManagerPlugin = PluginManager.GetPluginForId("9f8f9b14-2518-4907-b211-35ab6290dee7");
var actionKeyword = pluginsManagerPlugin.Metadata.ActionKeywords.Count == 0 ? "" : pluginsManagerPlugin.Metadata.ActionKeywords[0];
API.ChangeQuery($"{actionKeyword} install {plugin.Name}");
API.ShowMainWindow();
}
}
private void window_MouseDown(object sender, MouseButtonEventArgs e) /* for close hotkey popup */
{
if (Keyboard.FocusedElement is not TextBox textBox)
{
return;
}
var tRequest = new TraversalRequest(FocusNavigationDirection.Next);
textBox.MoveFocus(tRequest);
}
2021-11-29 22:07:52 +00:00
private void ColorSchemeSelectedIndexChanged(object sender, EventArgs e)
=> ThemeManager.Current.ApplicationTheme = settings.ColorScheme switch
{
Constant.Light => ApplicationTheme.Light,
Constant.Dark => ApplicationTheme.Dark,
Constant.System => null,
_ => ThemeManager.Current.ApplicationTheme
};
/* Custom TitleBar */
private void OnMinimizeButtonClick(object sender, RoutedEventArgs e)
{
WindowState = WindowState.Minimized;
}
private void OnMaximizeRestoreButtonClick(object sender, RoutedEventArgs e)
{
WindowState = WindowState == WindowState.Maximized ? WindowState.Normal : WindowState.Maximized;
}
private void OnCloseButtonClick(object sender, RoutedEventArgs e)
{
Close();
}
private void RefreshMaximizeRestoreButton()
{
if (WindowState == WindowState.Maximized)
{
maximizeButton.Visibility = Visibility.Collapsed;
restoreButton.Visibility = Visibility.Visible;
}
else
{
maximizeButton.Visibility = Visibility.Visible;
restoreButton.Visibility = Visibility.Collapsed;
}
}
private void Window_StateChanged(object sender, EventArgs e)
{
RefreshMaximizeRestoreButton();
}
}
2021-12-09 10:00:34 +00:00
}