Skip to content

Commit b9735f3

Browse files
Jack251970TBM13
authored andcommitted
Merge pull request Flow-Launcher#3797 from Flow-Launcher/theme_mode_api
Add Theme mode API Function
1 parent bbca50c commit b9735f3

File tree

5 files changed

+61
-4
lines changed

5 files changed

+61
-4
lines changed

Flow.Launcher.Plugin/EventHandler.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,14 @@ namespace Flow.Launcher.Plugin
3939
/// <param name="sender"></param>
4040
/// <param name="args"></param>
4141
public delegate void VisibilityChangedEventHandler(object sender, VisibilityChangedEventArgs args);
42-
42+
43+
/// <summary>
44+
/// A delegate for when the actual application theme is changed
45+
/// </summary>
46+
/// <param name="sender"></param>
47+
/// <param name="args"></param>
48+
public delegate void ActualApplicationThemeChangedEventHandler(object sender, ActualApplicationThemeChangedEventArgs args);
49+
4350
/// <summary>
4451
/// The event args for <see cref="VisibilityChangedEventHandler"/>
4552
/// </summary>
@@ -77,4 +84,15 @@ public class FlowLauncherQueryEventArgs
7784
/// </summary>
7885
public Query Query { get; set; }
7986
}
87+
88+
/// <summary>
89+
/// The event args for <see cref="ActualApplicationThemeChangedEventHandler"/>
90+
/// </summary>
91+
public class ActualApplicationThemeChangedEventArgs : EventArgs
92+
{
93+
/// <summary>
94+
/// <see langword="true"/> if the application has changed actual theme
95+
/// </summary>
96+
public bool IsDark { get; init; }
97+
}
8098
}

Flow.Launcher.Plugin/Interfaces/IPublicAPI.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,5 +501,16 @@ public interface IPublicAPI
501501
/// </summary>
502502
/// <returns>The time taken to execute the method in milliseconds</returns>
503503
public Task<long> StopwatchLogInfoAsync(string className, string message, Func<Task> action, [CallerMemberName] string methodName = "");
504+
505+
/// <summary>
506+
/// Representing whether the application is using a dark theme
507+
/// </summary>
508+
/// <returns></returns>
509+
bool IsApplicationDarkTheme();
510+
511+
/// <summary>
512+
/// Invoked when the actual theme of the application has changed. Currently, the plugin will continue to be subscribed even if it is turned off.
513+
/// </summary>
514+
event ActualApplicationThemeChangedEventHandler ActualApplicationThemeChanged;
504515
}
505516
}

Flow.Launcher/MainWindow.xaml.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
using Flow.Launcher.Infrastructure.Hotkey;
2121
using Flow.Launcher.Infrastructure.Image;
2222
using Flow.Launcher.Infrastructure.UserSettings;
23+
using Flow.Launcher.Plugin;
2324
using Flow.Launcher.Plugin.SharedCommands;
2425
using Flow.Launcher.ViewModel;
2526
using Microsoft.Win32;
@@ -82,7 +83,7 @@ public MainWindow()
8283
UpdatePosition();
8384

8485
DataObject.AddPastingHandler(QueryTextBox, QueryTextBox_OnPaste);
85-
ModernWpf.ThemeManager.Current.ActualApplicationThemeChanged += ThemeManager_ActualApplicationThemeChanged;
86+
_viewModel.ActualApplicationThemeChanged += ViewModel_ActualApplicationThemeChanged;
8687
}
8788

8889
#endregion
@@ -91,7 +92,7 @@ public MainWindow()
9192

9293
#pragma warning disable VSTHRD100 // Avoid async void methods
9394

94-
private void ThemeManager_ActualApplicationThemeChanged(ModernWpf.ThemeManager sender, object args)
95+
private void ViewModel_ActualApplicationThemeChanged(object sender, ActualApplicationThemeChangedEventArgs args)
9596
{
9697
_ = _theme.RefreshFrameAsync();
9798
}
@@ -1020,7 +1021,7 @@ protected virtual void Dispose(bool disposing)
10201021
{
10211022
_hwndSource?.Dispose();
10221023
_notifyIcon?.Dispose();
1023-
ModernWpf.ThemeManager.Current.ActualApplicationThemeChanged -= ThemeManager_ActualApplicationThemeChanged;
1024+
_viewModel.ActualApplicationThemeChanged -= ViewModel_ActualApplicationThemeChanged;
10241025
}
10251026

10261027
_disposed = true;

Flow.Launcher/PublicAPIInstance.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
using Flow.Launcher.Plugin.SharedModels;
3131
using Flow.Launcher.ViewModel;
3232
using JetBrains.Annotations;
33+
using ModernWpf;
3334
using Stopwatch = Flow.Launcher.Infrastructure.Stopwatch;
3435

3536
namespace Flow.Launcher
@@ -549,6 +550,17 @@ public long StopwatchLogInfo(string className, string message, Action action, [C
549550
public Task<long> StopwatchLogInfoAsync(string className, string message, Func<Task> action, [CallerMemberName] string methodName = "") =>
550551
Stopwatch.InfoAsync(className, message, action, methodName);
551552

553+
public bool IsApplicationDarkTheme()
554+
{
555+
return ThemeManager.Current.ActualApplicationTheme == ApplicationTheme.Dark;
556+
}
557+
558+
public event ActualApplicationThemeChangedEventHandler ActualApplicationThemeChanged
559+
{
560+
add => _mainVM.ActualApplicationThemeChanged += value;
561+
remove => _mainVM.ActualApplicationThemeChanged -= value;
562+
}
563+
552564
#endregion
553565

554566
#region Private Methods

Flow.Launcher/ViewModel/MainViewModel.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
using Flow.Launcher.Plugin.SharedCommands;
2323
using Flow.Launcher.Storage;
2424
using Microsoft.VisualStudio.Threading;
25+
using ModernWpf;
2526

2627
namespace Flow.Launcher.ViewModel
2728
{
@@ -154,6 +155,18 @@ public MainViewModel()
154155

155156
RegisterViewUpdate();
156157
_ = RegisterClockAndDateUpdateAsync();
158+
159+
ThemeManager.Current.ActualApplicationThemeChanged += ThemeManager_ActualApplicationThemeChanged;
160+
}
161+
162+
private void ThemeManager_ActualApplicationThemeChanged(ThemeManager sender, object args)
163+
{
164+
ActualApplicationThemeChanged?.Invoke(
165+
Application.Current,
166+
new ActualApplicationThemeChangedEventArgs()
167+
{
168+
IsDark = sender.ActualApplicationTheme == ApplicationTheme.Dark
169+
});
157170
}
158171

159172
private void RegisterViewUpdate()
@@ -703,6 +716,7 @@ private ResultsViewModel SelectedResults
703716
public bool MainWindowVisibilityStatus { get; set; } = true;
704717

705718
public event VisibilityChangedEventHandler VisibilityChanged;
719+
public event ActualApplicationThemeChangedEventHandler ActualApplicationThemeChanged;
706720

707721
public Visibility ClockPanelVisibility { get; set; }
708722
public Visibility SearchIconVisibility { get; set; }
@@ -1735,6 +1749,7 @@ protected virtual void Dispose(bool disposing)
17351749
{
17361750
_resultsViewUpdateTask.Dispose();
17371751
}
1752+
ThemeManager.Current.ActualApplicationThemeChanged -= ThemeManager_ActualApplicationThemeChanged;
17381753
_disposed = true;
17391754
}
17401755
}

0 commit comments

Comments
 (0)