Skip to content

Commit 358b1fd

Browse files
committed
Move theme data and functions into api
1 parent 67cc1e2 commit 358b1fd

File tree

4 files changed

+116
-18
lines changed

4 files changed

+116
-18
lines changed

Flow.Launcher.Core/Resource/Theme.cs

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,6 @@ public Theme(IPublicAPI publicAPI, Settings settings)
8181

8282
#region Theme Resources
8383

84-
public string GetCurrentTheme()
85-
{
86-
return _settings.Theme;
87-
}
88-
8984
private void MakeSureThemeDirectoriesExist()
9085
{
9186
foreach (var dir in _themeDirectories.Where(dir => !Directory.Exists(dir)))
@@ -127,7 +122,7 @@ public void UpdateFonts()
127122
try
128123
{
129124
// Load a ResourceDictionary for the specified theme.
130-
var themeName = GetCurrentTheme();
125+
var themeName = _settings.Theme;
131126
var dict = GetThemeResourceDictionary(themeName);
132127

133128
// Apply font settings to the theme resource.
@@ -330,7 +325,7 @@ private ResourceDictionary GetResourceDictionary(string theme)
330325

331326
private ResourceDictionary GetCurrentResourceDictionary()
332327
{
333-
return GetResourceDictionary(GetCurrentTheme());
328+
return GetResourceDictionary(_settings.Theme);
334329
}
335330

336331
private ThemeData GetThemeDataFromPath(string path)
@@ -383,9 +378,15 @@ private string GetThemePath(string themeName)
383378

384379
#endregion
385380

386-
#region Load & Change
381+
#region Get & Change Theme
382+
383+
public ThemeData GetCurrentTheme()
384+
{
385+
var themes = GetAvailableThemes();
386+
return themes.FirstOrDefault(t => t.FileNameWithoutExtension == _settings.Theme) ?? themes.FirstOrDefault();
387+
}
387388

388-
public List<ThemeData> LoadAvailableThemes()
389+
public List<ThemeData> GetAvailableThemes()
389390
{
390391
List<ThemeData> themes = new List<ThemeData>();
391392
foreach (var themeDirectory in _themeDirectories)
@@ -403,7 +404,7 @@ public List<ThemeData> LoadAvailableThemes()
403404
public bool ChangeTheme(string theme = null)
404405
{
405406
if (string.IsNullOrEmpty(theme))
406-
theme = GetCurrentTheme();
407+
theme = _settings.Theme;
407408

408409
string path = GetThemePath(theme);
409410
try
@@ -591,7 +592,7 @@ await Application.Current.Dispatcher.InvokeAsync(() =>
591592
{
592593
AutoDropShadow(useDropShadowEffect);
593594
}
594-
SetBlurForWindow(GetCurrentTheme(), backdropType);
595+
SetBlurForWindow(_settings.Theme, backdropType);
595596

596597
if (!BlurEnabled)
597598
{
@@ -610,7 +611,7 @@ await Application.Current.Dispatcher.InvokeAsync(() =>
610611
// Get the actual backdrop type and drop shadow effect settings
611612
var (backdropType, _) = GetActualValue();
612613

613-
SetBlurForWindow(GetCurrentTheme(), backdropType);
614+
SetBlurForWindow(_settings.Theme, backdropType);
614615
}, DispatcherPriority.Render);
615616
}
616617

@@ -898,11 +899,5 @@ private static bool IsBlurTheme()
898899
}
899900

900901
#endregion
901-
902-
#region Classes
903-
904-
public record ThemeData(string FileNameWithoutExtension, string Name, bool? IsDark = null, bool? HasBlur = null);
905-
906-
#endregion
907902
}
908903
}

Flow.Launcher.Plugin/Interfaces/IPublicAPI.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,5 +344,24 @@ public interface IPublicAPI
344344
/// Stop the loading bar in main window
345345
/// </summary>
346346
public void StopLoadingBar();
347+
348+
/// <summary>
349+
/// Get all available themes
350+
/// </summary>
351+
/// <returns></returns>
352+
public List<ThemeData> GetAvailableThemes();
353+
354+
/// <summary>
355+
/// Get the current theme
356+
/// </summary>
357+
/// <returns></returns>
358+
public ThemeData GetCurrentTheme();
359+
360+
/// <summary>
361+
/// Set the current theme
362+
/// </summary>
363+
/// <param name="theme"></param>
364+
/// <returns></returns>
365+
public void SetCurrentTheme(ThemeData theme);
347366
}
348367
}

Flow.Launcher.Plugin/ThemeData.cs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
namespace Flow.Launcher.Plugin;
2+
3+
/// <summary>
4+
/// Theme data model
5+
/// </summary>
6+
public class ThemeData
7+
{
8+
/// <summary>
9+
/// Theme file name without extension
10+
/// </summary>
11+
public string FileNameWithoutExtension { get; private init; }
12+
13+
/// <summary>
14+
/// Theme name
15+
/// </summary>
16+
public string Name { get; private init; }
17+
18+
/// <summary>
19+
/// Theme file path
20+
/// </summary>
21+
public bool? IsDark { get; private init; }
22+
23+
/// <summary>
24+
/// Theme file path
25+
/// </summary>
26+
public bool? HasBlur { get; private init; }
27+
28+
/// <summary>
29+
/// Theme data constructor
30+
/// </summary>
31+
public ThemeData(string fileNameWithoutExtension, string name, bool? isDark = null, bool? hasBlur = null)
32+
{
33+
FileNameWithoutExtension = fileNameWithoutExtension;
34+
Name = name;
35+
IsDark = isDark;
36+
HasBlur = hasBlur;
37+
}
38+
39+
/// <inheritdoc />
40+
public static bool operator ==(ThemeData left, ThemeData right)
41+
{
42+
return left.Equals(right);
43+
}
44+
45+
/// <inheritdoc />
46+
public static bool operator !=(ThemeData left, ThemeData right)
47+
{
48+
return !(left == right);
49+
}
50+
51+
/// <inheritdoc />
52+
public override bool Equals(object obj)
53+
{
54+
if (obj is not ThemeData other)
55+
return false;
56+
return FileNameWithoutExtension == other.FileNameWithoutExtension &&
57+
Name == other.Name;
58+
}
59+
60+
/// <inheritdoc />
61+
public override int GetHashCode()
62+
{
63+
return Name?.GetHashCode() ?? 0;
64+
}
65+
66+
/// <inheritdoc />
67+
public override string ToString()
68+
{
69+
return Name;
70+
}
71+
}

Flow.Launcher/PublicAPIInstance.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ public class PublicAPIInstance : IPublicAPI
3737
private readonly Internationalization _translater;
3838
private readonly MainViewModel _mainVM;
3939

40+
private Theme _theme;
41+
private Theme Theme => _theme ??= Ioc.Default.GetRequiredService<Theme>();
42+
4043
private readonly object _saveSettingsLock = new();
4144

4245
#region Constructor
@@ -354,6 +357,16 @@ public MessageBoxResult ShowMsgBox(string messageBoxText, string caption = "", M
354357

355358
public Task ShowProgressBoxAsync(string caption, Func<Action<double>, Task> reportProgressAsync, Action cancelProgress = null) => ProgressBoxEx.ShowAsync(caption, reportProgressAsync, cancelProgress);
356359

360+
public List<ThemeData> GetAvailableThemes() => Theme.GetAvailableThemes();
361+
362+
public ThemeData GetCurrentTheme() => Theme.GetCurrentTheme();
363+
364+
public void SetCurrentTheme(ThemeData theme)
365+
{
366+
Theme.ChangeTheme(theme.FileNameWithoutExtension);
367+
_ = _theme.RefreshFrameAsync();
368+
}
369+
357370
#endregion
358371

359372
#region Private Methods

0 commit comments

Comments
 (0)