Skip to content

Commit 92ee763

Browse files
committed
Add Darkmode in setting
Add System darkmode detection helper Add InitializeDarkmode in onload
1 parent ef71f6d commit 92ee763

File tree

9 files changed

+227
-197
lines changed

9 files changed

+227
-197
lines changed

Flow.Launcher.Infrastructure/UserSettings/Settings.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public class Settings : BaseModel
1515
private string language = "en";
1616
public string Hotkey { get; set; } = $"{KeyConstant.Alt} + {KeyConstant.Space}";
1717
public string OpenResultModifiers { get; set; } = KeyConstant.Alt;
18+
public string DarkMode { get; set; }
1819
public bool ShowOpenResultHotkey { get; set; } = true;
1920
public double WindowSize { get; set; } = 580;
2021

Flow.Launcher/App.xaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<Application.Resources>
99
<ResourceDictionary>
1010
<ResourceDictionary.MergedDictionaries>
11-
<ui:ThemeResources RequestedTheme="Dark">
11+
<ui:ThemeResources>
1212
<ui:ThemeResources.ThemeDictionaries>
1313
<ResourceDictionary x:Key="Light">
1414
<ResourceDictionary.MergedDictionaries>
@@ -19,7 +19,6 @@
1919
<ResourceDictionary.MergedDictionaries>
2020
<ResourceDictionary Source="pack://application:,,,/Resources/Dark.xaml" />
2121
</ResourceDictionary.MergedDictionaries>
22-
<SolidColorBrush x:Key="NavigationViewSelectionIndicatorForeground" Color="{StaticResource SystemBaseHighColor}" />
2322
</ResourceDictionary>
2423
</ui:ThemeResources.ThemeDictionaries>
2524
</ui:ThemeResources>

Flow.Launcher/Flow.Launcher.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@
9696
</PackageReference>
9797
<PackageReference Include="PropertyChanged.Fody" Version="3.4.0" />
9898
<PackageReference Include="SharpVectors" Version="1.7.6" />
99+
<PackageReference Include="System.Management" Version="6.0.0" />
99100
</ItemGroup>
100101

101102
<ItemGroup>

Flow.Launcher/Helper/SystemTheme.cs

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using System.Diagnostics;
7+
using System.Management;
8+
using System.Security.Principal;
9+
10+
namespace Flow.Launcher.Helper
11+
{
12+
class SystemTheme
13+
{
14+
15+
private const string PersonalizeKey = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize";
16+
private const string SysThemeValueName = "SystemUsesLightTheme";
17+
18+
public static event EventHandler<SystemThemeChangedEventArgs> SystemThemeChanged;
19+
20+
public static bool GetIsSystemLightTheme() => ReadDWord(SysThemeValueName);
21+
22+
private static bool ReadDWord(string valueName)
23+
{
24+
var regkey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(PersonalizeKey);
25+
if (regkey == null)
26+
{
27+
return false;
28+
}
29+
30+
return (int)regkey.GetValue(valueName, 0) > 0;
31+
}
32+
33+
public static void Initialize()
34+
{
35+
UpdateTheme();
36+
try
37+
{
38+
var currentUser = WindowsIdentity.GetCurrent();
39+
var query = new WqlEventQuery(string.Format("SELECT * FROM RegistryValueChangeEvent WHERE Hive='HKEY_USERS' AND KeyPath='{0}\\\\{1}' AND ValueName='{2}'",
40+
currentUser.User.Value, PersonalizeKey.Replace("\\", "\\\\"), SysThemeValueName));
41+
42+
ManagementEventWatcher watcher = new(query);
43+
44+
watcher.EventArrived += new EventArrivedEventHandler(HandleEvent);
45+
46+
watcher.Start();
47+
}
48+
catch (ManagementException managementException)
49+
{
50+
Debug.WriteLine($"{nameof(SystemTheme)}: " + managementException.Message);
51+
}
52+
}
53+
54+
private static void HandleEvent(object sender, EventArrivedEventArgs e)
55+
{
56+
UpdateTheme();
57+
}
58+
59+
private static void UpdateTheme()
60+
{
61+
bool isSystemLightTheme = GetIsSystemLightTheme();
62+
var args = new SystemThemeChangedEventArgs(isSystemLightTheme);
63+
SystemThemeChanged?.Invoke(null, args);
64+
}
65+
}
66+
67+
public class SystemThemeChangedEventArgs : EventArgs
68+
{
69+
public SystemThemeChangedEventArgs(bool isSystemLightTheme)
70+
{
71+
IsSystemLightTheme = isSystemLightTheme;
72+
}
73+
74+
public bool IsSystemLightTheme { get; private set; }
75+
}
76+
}

Flow.Launcher/MainWindow.xaml.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ private void OnLoaded(object sender, RoutedEventArgs _)
6767
{
6868
// show notify icon when flowlauncher is hidden
6969
InitializeNotifyIcon();
70-
70+
InitializeDarkMode();
7171
WindowsInteropHelper.DisableControlBox(this);
7272
InitProgressbarAnimation();
7373
InitializePosition();
@@ -372,5 +372,19 @@ private void MoveQueryTextToEnd()
372372
{
373373
QueryTextBox.CaretIndex = QueryTextBox.Text.Length;
374374
}
375+
376+
public void InitializeDarkMode()
377+
{
378+
if (_settings.DarkMode == "Light")
379+
{
380+
ModernWpf.ThemeManager.Current.ApplicationTheme = ModernWpf.ApplicationTheme.Light;
381+
}
382+
else if (_settings.DarkMode == "Dark")
383+
{
384+
ModernWpf.ThemeManager.Current.ApplicationTheme = ModernWpf.ApplicationTheme.Dark;
385+
}
386+
else
387+
{ }
388+
}
375389
}
376390
}

Flow.Launcher/Resources/CustomControlTemplate.xaml

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -7,37 +7,37 @@
77

88
<!-- Popup Color -->
99
<Style x:Key="PageBackgroundColor" TargetType="{x:Type Window}">
10-
<Setter Property="Background" Value="{StaticResource Color05B}" />
11-
<Setter Property="Foreground" Value="{StaticResource Color05B}" />
10+
<Setter Property="Background" Value="{DynamicResource Color05B}" />
11+
<Setter Property="Foreground" Value="{DynamicResource Color05B}" />
1212
</Style>
1313

1414
<!-- Setting Window -->
1515
<Style x:Key="PageTitle" TargetType="{x:Type TextBlock}">
16-
<Setter Property="Foreground" Value="{StaticResource Color05B}" />
16+
<Setter Property="Foreground" Value="{DynamicResource Color05B}" />
1717
</Style>
1818
<Style x:Key="Glyph" TargetType="{x:Type TextBlock}">
1919
<Setter Property="Grid.Column" Value="0" />
2020
<Setter Property="Margin" Value="24,0,16,0" />
2121
<Setter Property="VerticalAlignment" Value="Center" />
2222
<Setter Property="FontSize" Value="20" />
2323
<Setter Property="FontFamily" Value="/Resources/#Segoe Fluent Icons" />
24-
<Setter Property="Foreground" Value="{StaticResource Color05B}" />
24+
<Setter Property="Foreground" Value="{DynamicResource Color05B}" />
2525
</Style>
2626
<Style x:Key="TabMenu" TargetType="{x:Type TextBlock}">
2727
<Setter Property="FontSize" Value="14" />
2828
<Setter Property="FontFamily" Value="Segoe UI" />
2929
<Setter Property="VerticalAlignment" Value="Center" />
30-
<Setter Property="Foreground" Value="{StaticResource Color05B}" />
30+
<Setter Property="Foreground" Value="{DynamicResource Color05B}" />
3131
</Style>
3232
<Style x:Key="TabMenuIcon" TargetType="{x:Type TextBlock}">
3333
<Setter Property="Margin" Value="0,0,12,0" />
3434
<Setter Property="FontSize" Value="16" />
3535
<Setter Property="VerticalAlignment" Value="Center" />
3636
<Setter Property="FontFamily" Value="/Resources/#Segoe Fluent Icons" />
37-
<Setter Property="Foreground" Value="{StaticResource Color05B}" />
37+
<Setter Property="Foreground" Value="{DynamicResource Color05B}" />
3838
</Style>
3939
<Style x:Key="SettingSeparatorStyle" TargetType="{x:Type Separator}">
40-
<Setter Property="BorderBrush" Value="{StaticResource Color03B}" />
40+
<Setter Property="BorderBrush" Value="{DynamicResource Color03B}" />
4141
</Style>
4242

4343
<!-- Title Bar -->
@@ -53,7 +53,7 @@
5353
</Style>
5454

5555
<Style x:Key="TitleBarButtonStyle" TargetType="Button">
56-
<Setter Property="Foreground" Value="{StaticResource Color05B}" />
56+
<Setter Property="Foreground" Value="{DynamicResource Color05B}" />
5757
<Setter Property="Padding" Value="0" />
5858
<Setter Property="WindowChrome.IsHitTestVisibleInChrome" Value="True" />
5959
<Setter Property="IsTabStop" Value="False" />
@@ -62,7 +62,7 @@
6262
<ControlTemplate TargetType="{x:Type Button}">
6363
<Border
6464
x:Name="border"
65-
Background="{StaticResource Color01B}"
65+
Background="{DynamicResource Color01B}"
6666
BorderThickness="0"
6767
SnapsToDevicePixels="true">
6868
<ContentPresenter
@@ -75,10 +75,10 @@
7575
</Border>
7676
<ControlTemplate.Triggers>
7777
<Trigger Property="IsMouseOver" Value="true">
78-
<Setter TargetName="border" Property="Background" Value="{StaticResource Color06B}" />
78+
<Setter TargetName="border" Property="Background" Value="{DynamicResource Color06B}" />
7979
</Trigger>
8080
<Trigger Property="IsPressed" Value="true">
81-
<Setter TargetName="border" Property="Background" Value="{StaticResource Color09B}" />
81+
<Setter TargetName="border" Property="Background" Value="{DynamicResource Color09B}" />
8282
</Trigger>
8383
</ControlTemplate.Triggers>
8484
</ControlTemplate>
@@ -87,7 +87,7 @@
8787
</Style>
8888

8989
<Style x:Key="TitleBarCloseButtonStyle" TargetType="Button">
90-
<Setter Property="Foreground" Value="{StaticResource Color05B}" />
90+
<Setter Property="Foreground" Value="{DynamicResource Color05B}" />
9191
<Setter Property="Padding" Value="0" />
9292
<Setter Property="WindowChrome.IsHitTestVisibleInChrome" Value="True" />
9393
<Setter Property="IsTabStop" Value="False" />
@@ -109,11 +109,11 @@
109109
</Border>
110110
<ControlTemplate.Triggers>
111111
<Trigger Property="IsMouseOver" Value="true">
112-
<Setter TargetName="border" Property="Background" Value="{StaticResource Color10B}" />
112+
<Setter TargetName="border" Property="Background" Value="{DynamicResource Color10B}" />
113113
<Setter Property="Foreground" Value="{DynamicResource MouseOverWindowCloseButtonForegroundBrush}" />
114114
</Trigger>
115115
<Trigger Property="IsPressed" Value="true">
116-
<Setter TargetName="border" Property="Background" Value="{StaticResource Color11B}" />
116+
<Setter TargetName="border" Property="Background" Value="{DynamicResource Color11B}" />
117117
<Setter Property="Foreground" Value="{DynamicResource MouseOverWindowCloseButtonForegroundBrush}" />
118118
</Trigger>
119119
</ControlTemplate.Triggers>
@@ -1394,16 +1394,16 @@
13941394
<ControlTemplate TargetType="Button">
13951395
<Border
13961396
x:Name="Background"
1397-
Background="{StaticResource ButtonBackgroundColor}"
1398-
BorderBrush="{StaticResource ButtonOutBorder}"
1399-
BorderThickness="{StaticResource CustomButtonOutBorderThickness}"
1397+
Background="{DynamicResource ButtonBackgroundColor}"
1398+
BorderBrush="{DynamicResource ButtonOutBorder}"
1399+
BorderThickness="{DynamicResource CustomButtonOutBorderThickness}"
14001400
CornerRadius="4"
14011401
SnapsToDevicePixels="True">
14021402
<Border
14031403
x:Name="Border"
14041404
Padding="{TemplateBinding Padding}"
1405-
BorderBrush="{StaticResource ButtonInsideBorder}"
1406-
BorderThickness="{StaticResource CustomButtonInsideBorderThickness}"
1405+
BorderBrush="{DynamicResource ButtonInsideBorder}"
1406+
BorderThickness="{DynamicResource CustomButtonInsideBorderThickness}"
14071407
CornerRadius="4">
14081408
<ContentPresenter
14091409
x:Name="ContentPresenter"
@@ -1416,27 +1416,27 @@
14161416
</Border>
14171417
<ControlTemplate.Triggers>
14181418
<Trigger Property="IsMouseOver" Value="True">
1419-
<Setter TargetName="Background" Property="Background" Value="{StaticResource ButtonMouseOver}" />
1420-
<Setter TargetName="Background" Property="BorderThickness" Value="{StaticResource CustomButtonOutBorderThickness}" />
1421-
<Setter TargetName="Background" Property="BorderBrush" Value="{StaticResource ButtonOutBorder}" />
1422-
<Setter TargetName="Border" Property="BorderBrush" Value="{StaticResource ButtonInsideBorder}" />
1423-
<Setter TargetName="Border" Property="BorderThickness" Value="{StaticResource CustomButtonInsideBorderThickness}" />
1419+
<Setter TargetName="Background" Property="Background" Value="{DynamicResource ButtonMouseOver}" />
1420+
<Setter TargetName="Background" Property="BorderThickness" Value="{DynamicResource CustomButtonOutBorderThickness}" />
1421+
<Setter TargetName="Background" Property="BorderBrush" Value="{DynamicResource ButtonOutBorder}" />
1422+
<Setter TargetName="Border" Property="BorderBrush" Value="{DynamicResource ButtonInsideBorder}" />
1423+
<Setter TargetName="Border" Property="BorderThickness" Value="{DynamicResource CustomButtonInsideBorderThickness}" />
14241424
</Trigger>
14251425
<Trigger Property="IsPressed" Value="True">
1426-
<Setter TargetName="Background" Property="Background" Value="{StaticResource ButtonMousePressed}" />
1427-
<Setter TargetName="Background" Property="BorderThickness" Value="{StaticResource PressedCustomButtonOutBorderThickness}" />
1428-
<Setter TargetName="Background" Property="BorderBrush" Value="{StaticResource ButtonMousePressedInsideBorder}" />
1429-
<Setter TargetName="Border" Property="BorderBrush" Value="{StaticResource ButtonInsideMouseOverBorder}" />
1430-
<Setter TargetName="Border" Property="BorderThickness" Value="{StaticResource PressedCustomButtonInsideBorderThickness}" />
1431-
<Setter TargetName="ContentPresenter" Property="TextElement.Foreground" Value="{StaticResource ButtonMousePressedText}" />
1426+
<Setter TargetName="Background" Property="Background" Value="{DynamicResource ButtonMousePressed}" />
1427+
<Setter TargetName="Background" Property="BorderThickness" Value="{DynamicResource PressedCustomButtonOutBorderThickness}" />
1428+
<Setter TargetName="Background" Property="BorderBrush" Value="{DynamicResource ButtonMousePressedInsideBorder}" />
1429+
<Setter TargetName="Border" Property="BorderBrush" Value="{DynamicResource ButtonInsideMouseOverBorder}" />
1430+
<Setter TargetName="Border" Property="BorderThickness" Value="{DynamicResource PressedCustomButtonInsideBorderThickness}" />
1431+
<Setter TargetName="ContentPresenter" Property="TextElement.Foreground" Value="{DynamicResource ButtonMousePressedText}" />
14321432
</Trigger>
14331433
<Trigger Property="IsEnabled" Value="False">
1434-
<Setter TargetName="Background" Property="Background" Value="{StaticResource ButtonBackgroundDisabledColor}" />
1435-
<Setter TargetName="Background" Property="BorderThickness" Value="{StaticResource DisabledCustomButtonOutBorderThickness}" />
1436-
<Setter TargetName="Background" Property="BorderBrush" Value="{StaticResource ButtonOutBorder}" />
1437-
<Setter TargetName="Border" Property="BorderBrush" Value="{StaticResource ButtonInsideBorderDisabled}" />
1438-
<Setter TargetName="Border" Property="BorderThickness" Value="{StaticResource DisabledCustomButtonInsideBorderThickness}" />
1439-
<Setter TargetName="ContentPresenter" Property="TextElement.Foreground" Value="{StaticResource ButtonDisabledText}" />
1434+
<Setter TargetName="Background" Property="Background" Value="{DynamicResource ButtonBackgroundDisabledColor}" />
1435+
<Setter TargetName="Background" Property="BorderThickness" Value="{DynamicResource DisabledCustomButtonOutBorderThickness}" />
1436+
<Setter TargetName="Background" Property="BorderBrush" Value="{DynamicResource ButtonOutBorder}" />
1437+
<Setter TargetName="Border" Property="BorderBrush" Value="{DynamicResource ButtonInsideBorderDisabled}" />
1438+
<Setter TargetName="Border" Property="BorderThickness" Value="{DynamicResource DisabledCustomButtonInsideBorderThickness}" />
1439+
<Setter TargetName="ContentPresenter" Property="TextElement.Foreground" Value="{DynamicResource ButtonDisabledText}" />
14401440
</Trigger>
14411441
</ControlTemplate.Triggers>
14421442
</ControlTemplate>
@@ -2606,7 +2606,7 @@
26062606
ui:ControlHelper.CornerRadius="4"
26072607
Content="{StaticResource ChevronUp}"
26082608
FontSize="{TemplateBinding FontSize}"
2609-
Style="{StaticResource NumberBoxSpinButtonStyle}"
2609+
Style="{DynamicResource NumberBoxSpinButtonStyle}"
26102610
Visibility="Collapsed" />
26112611

26122612
<RepeatButton
@@ -2616,7 +2616,7 @@
26162616
ui:ControlHelper.CornerRadius="4"
26172617
Content="{StaticResource ChevronDown}"
26182618
FontSize="{TemplateBinding FontSize}"
2619-
Style="{StaticResource NumberBoxSpinButtonStyle}"
2619+
Style="{DynamicResource NumberBoxSpinButtonStyle}"
26202620
Visibility="Collapsed" />
26212621

26222622
<ContentPresenter
@@ -2679,7 +2679,7 @@
26792679
</Trigger>
26802680
<!-- SpinButtonsPopup -->
26812681
<Trigger Property="SpinButtonPlacementMode" Value="Compact">
2682-
<Setter TargetName="InputBox" Property="Style" Value="{StaticResource NumberBoxTextBoxStyle}" />
2682+
<Setter TargetName="InputBox" Property="Style" Value="{DynamicResource NumberBoxTextBoxStyle}" />
26832683
</Trigger>
26842684
</ControlTemplate.Triggers>
26852685
</ControlTemplate>

0 commit comments

Comments
 (0)