Skip to content

Commit 84182fe

Browse files
committed
Update SettingWindow.xaml.cs
Fix System Aware Theme and Code Refactor
1 parent d1bf471 commit 84182fe

File tree

1 file changed

+48
-65
lines changed

1 file changed

+48
-65
lines changed

Flow.Launcher/SettingWindow.xaml.cs

Lines changed: 48 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
1-
using System;
2-
using System.IO;
3-
using System.Windows;
4-
using System.Windows.Input;
5-
using System.Windows.Interop;
6-
using System.Windows.Navigation;
7-
using Microsoft.Win32;
1+
using Flow.Launcher.Core.ExternalPlugins;
82
using Flow.Launcher.Core.Plugin;
93
using Flow.Launcher.Core.Resource;
4+
using Flow.Launcher.Helper;
105
using Flow.Launcher.Infrastructure;
116
using Flow.Launcher.Infrastructure.UserSettings;
127
using Flow.Launcher.Plugin;
138
using Flow.Launcher.Plugin.SharedCommands;
149
using Flow.Launcher.ViewModel;
15-
using Flow.Launcher.Helper;
16-
using System.Windows.Controls;
17-
using Flow.Launcher.Core.ExternalPlugins;
18-
using System.Runtime.InteropServices;
10+
using Microsoft.Win32;
11+
using ModernWpf;
12+
using System;
13+
using System.IO;
14+
using System.Windows;
15+
using System.Windows.Forms;
16+
using System.Windows.Input;
17+
using System.Windows.Interop;
18+
using System.Windows.Navigation;
19+
using Button = System.Windows.Controls.Button;
20+
using Control = System.Windows.Controls.Control;
21+
using MessageBox = System.Windows.MessageBox;
22+
using TextBox = System.Windows.Controls.TextBox;
1923
using ThemeManager = ModernWpf.ThemeManager;
20-
using ApplicationTheme = ModernWpf.ApplicationTheme;
2124

2225
namespace Flow.Launcher
2326
{
@@ -62,39 +65,30 @@ private void OnAutoStartupUncheck(object sender, RoutedEventArgs e)
6265

6366
public static void SetStartup()
6467
{
65-
using (var key = Registry.CurrentUser.OpenSubKey(StartupPath, true))
66-
{
67-
key?.SetValue(Infrastructure.Constant.FlowLauncher, Infrastructure.Constant.ExecutablePath);
68-
}
68+
using var key = Registry.CurrentUser.OpenSubKey(StartupPath, true);
69+
key?.SetValue(Constant.FlowLauncher, Constant.ExecutablePath);
6970
}
7071

7172
private void RemoveStartup()
7273
{
73-
using (var key = Registry.CurrentUser.OpenSubKey(StartupPath, true))
74-
{
75-
key?.DeleteValue(Infrastructure.Constant.FlowLauncher, false);
76-
}
74+
using var key = Registry.CurrentUser.OpenSubKey(StartupPath, true);
75+
key?.DeleteValue(Constant.FlowLauncher, false);
7776
}
7877

7978
public static bool StartupSet()
8079
{
81-
using (var key = Registry.CurrentUser.OpenSubKey(StartupPath, true))
80+
using var key = Registry.CurrentUser.OpenSubKey(StartupPath, true);
81+
var path = key?.GetValue(Constant.FlowLauncher) as string;
82+
if (path != null)
8283
{
83-
var path = key?.GetValue(Infrastructure.Constant.FlowLauncher) as string;
84-
if (path != null)
85-
{
86-
return path == Infrastructure.Constant.ExecutablePath;
87-
}
88-
else
89-
{
90-
return false;
91-
}
84+
return path == Constant.ExecutablePath;
9285
}
86+
return false;
9387
}
9488

9589
private void OnSelectPythonDirectoryClick(object sender, RoutedEventArgs e)
9690
{
97-
var dlg = new System.Windows.Forms.FolderBrowserDialog
91+
var dlg = new FolderBrowserDialog
9892
{
9993
SelectedPath = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles)
10094
};
@@ -223,7 +217,7 @@ private void OnPluginNameClick(object sender, MouseButtonEventArgs e)
223217
var uri = new Uri(website);
224218
if (Uri.CheckSchemeName(uri.Scheme))
225219
{
226-
SearchWeb.NewTabInBrowser(website);
220+
website.NewTabInBrowser();
227221
}
228222
}
229223
}
@@ -257,7 +251,7 @@ private async void OnCheckUpdates(object sender, RoutedEventArgs e)
257251

258252
private void OnRequestNavigate(object sender, RequestNavigateEventArgs e)
259253
{
260-
SearchWeb.NewTabInBrowser(e.Uri.AbsoluteUri);
254+
e.Uri.AbsoluteUri.NewTabInBrowser();
261255
e.Handled = true;
262256
}
263257

@@ -296,74 +290,63 @@ private void OnExternalPluginInstallClick(object sender, RoutedEventArgs e)
296290
if(sender is Button { DataContext: UserPlugin plugin })
297291
{
298292
var pluginsManagerPlugin = PluginManager.GetPluginForId("9f8f9b14-2518-4907-b211-35ab6290dee7");
299-
var actionKeywrod = pluginsManagerPlugin.Metadata.ActionKeywords.Count == 0 ? "" : pluginsManagerPlugin.Metadata.ActionKeywords[0];
300-
API.ChangeQuery($"{actionKeywrod} install {plugin.Name}");
293+
var actionKeyword = pluginsManagerPlugin.Metadata.ActionKeywords.Count == 0 ? "" : pluginsManagerPlugin.Metadata.ActionKeywords[0];
294+
API.ChangeQuery($"{actionKeyword} install {plugin.Name}");
301295
API.ShowMainWindow();
302296
}
303297
}
304298

305299
private void window_MouseDown(object sender, MouseButtonEventArgs e) /* for close hotkey popup */
306300
{
307-
TextBox textBox = Keyboard.FocusedElement as TextBox;
308-
if (textBox != null)
301+
if (Keyboard.FocusedElement is not TextBox textBox)
309302
{
310-
TraversalRequest tRequest = new TraversalRequest(FocusNavigationDirection.Next);
311-
textBox.MoveFocus(tRequest);
303+
return;
312304
}
305+
var tRequest = new TraversalRequest(FocusNavigationDirection.Next);
306+
textBox.MoveFocus(tRequest);
313307
}
314308

315-
private void DarkModeSelectedIndexChanged(object sender, EventArgs e)
309+
private void DarkModeSelectedIndexChanged(object sender, EventArgs e) => ThemeManager.Current.ApplicationTheme = settings.DarkMode switch
316310
{
317-
if (settings.DarkMode == "Light")
318-
{
319-
ModernWpf.ThemeManager.Current.ApplicationTheme = ModernWpf.ApplicationTheme.Light;
320-
}
321-
else if (settings.DarkMode == "Dark")
322-
{
323-
ModernWpf.ThemeManager.Current.ApplicationTheme = ModernWpf.ApplicationTheme.Dark;
324-
}
325-
}
311+
"Light" => ApplicationTheme.Light,
312+
"Dark" => ApplicationTheme.Dark,
313+
"System" => null,
314+
_ => ThemeManager.Current.ApplicationTheme
315+
};
326316

327317
/* Custom TitleBar */
328318

329319
private void OnMinimizeButtonClick(object sender, RoutedEventArgs e)
330320
{
331-
this.WindowState = WindowState.Minimized;
321+
WindowState = WindowState.Minimized;
332322
}
333323

334324
private void OnMaximizeRestoreButtonClick(object sender, RoutedEventArgs e)
335325
{
336-
if (this.WindowState == WindowState.Maximized)
337-
{
338-
this.WindowState = WindowState.Normal;
339-
}
340-
else
341-
{
342-
this.WindowState = WindowState.Maximized;
343-
}
326+
WindowState = WindowState == WindowState.Maximized ? WindowState.Normal : WindowState.Maximized;
344327
}
345328

346329
private void OnCloseButtonClick(object sender, RoutedEventArgs e)
347330
{
348-
this.Close();
331+
Close();
349332
}
350333

351334
private void RefreshMaximizeRestoreButton()
352335
{
353-
if (this.WindowState == WindowState.Maximized)
336+
if (WindowState == WindowState.Maximized)
354337
{
355-
this.maximizeButton.Visibility = Visibility.Collapsed;
356-
this.restoreButton.Visibility = Visibility.Visible;
338+
maximizeButton.Visibility = Visibility.Collapsed;
339+
restoreButton.Visibility = Visibility.Visible;
357340
}
358341
else
359342
{
360-
this.maximizeButton.Visibility = Visibility.Visible;
361-
this.restoreButton.Visibility = Visibility.Collapsed;
343+
maximizeButton.Visibility = Visibility.Visible;
344+
restoreButton.Visibility = Visibility.Collapsed;
362345
}
363346
}
364347
private void Window_StateChanged(object sender, EventArgs e)
365348
{
366-
this.RefreshMaximizeRestoreButton();
349+
RefreshMaximizeRestoreButton();
367350
}
368351

369352
}

0 commit comments

Comments
 (0)