Skip to content

Commit 59b4859

Browse files
authored
Merge pull request #2448 from Odotocodot/flow-theme-selector-plugin
Add Flow Launcher Theme Selector to Sys plugin
2 parents 9b2148f + f503e41 commit 59b4859

File tree

6 files changed

+142
-4
lines changed

6 files changed

+142
-4
lines changed

Flow.Launcher.Core/Resource/Theme.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
using Flow.Launcher.Infrastructure;
1313
using Flow.Launcher.Infrastructure.Logger;
1414
using Flow.Launcher.Infrastructure.UserSettings;
15-
using CommunityToolkit.Mvvm.DependencyInjection;
1615
using Flow.Launcher.Plugin;
1716

1817
namespace Flow.Launcher.Core.Resource
@@ -35,6 +34,8 @@ public class Theme
3534
private string DirectoryPath => Path.Combine(Constant.ProgramDirectory, Folder);
3635
private string UserDirectoryPath => Path.Combine(DataLocation.DataDirectory(), Folder);
3736

37+
public string CurrentTheme => _settings.Theme;
38+
3839
public bool BlurEnabled { get; set; }
3940

4041
private double mainWindowWidth;

Plugins/Flow.Launcher.Plugin.Sys/Flow.Launcher.Plugin.Sys.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
<ItemGroup>
4040
<ProjectReference Include="..\..\Flow.Launcher.Infrastructure\Flow.Launcher.Infrastructure.csproj" />
4141
<ProjectReference Include="..\..\Flow.Launcher.Plugin\Flow.Launcher.Plugin.csproj" />
42+
<ProjectReference Include="..\..\Flow.Launcher.Core\Flow.Launcher.Core.csproj" />
4243
</ItemGroup>
4344

4445
<ItemGroup>
6.47 KB
Loading

Plugins/Flow.Launcher.Plugin.Sys/Languages/en.xaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
<system:String x:Key="flowlauncher_plugin_sys_open_docs_tips_cmd">Flow Launcher Tips</system:String>
2828
<system:String x:Key="flowlauncher_plugin_sys_open_userdata_location_cmd">Flow Launcher UserData Folder</system:String>
2929
<system:String x:Key="flowlauncher_plugin_sys_toggle_game_mode_cmd">Toggle Game Mode</system:String>
30+
<system:String x:Key="flowlauncher_plugin_sys_theme_selector_cmd">Set the Flow Launcher Theme</system:String>
3031

3132
<!-- Command Descriptions -->
3233
<system:String x:Key="flowlauncher_plugin_sys_shutdown_computer">Shutdown Computer</system:String>
@@ -49,8 +50,9 @@
4950
<system:String x:Key="flowlauncher_plugin_sys_open_docs_tips">Visit Flow Launcher's documentation for more help and how to use tips</system:String>
5051
<system:String x:Key="flowlauncher_plugin_sys_open_userdata_location">Open the location where Flow Launcher's settings are stored</system:String>
5152
<system:String x:Key="flowlauncher_plugin_sys_toggle_game_mode">Toggle Game Mode</system:String>
53+
<system:String x:Key="flowlauncher_plugin_sys_theme_selector">Quickly change the Flow Launcher theme</system:String>
5254

53-
<!-- Dialogs -->
55+
<!-- Dialogs -->
5456
<system:String x:Key="flowlauncher_plugin_sys_dlgtitle_success">Success</system:String>
5557
<system:String x:Key="flowlauncher_plugin_sys_dlgtext_all_settings_saved">All Flow Launcher settings saved</system:String>
5658
<system:String x:Key="flowlauncher_plugin_sys_dlgtext_all_applicableplugins_reloaded">Reloaded all applicable plugin data</system:String>

Plugins/Flow.Launcher.Plugin.Sys/Main.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
using Flow.Launcher.Infrastructure;
88
using Flow.Launcher.Infrastructure.Logger;
99
using Flow.Launcher.Infrastructure.UserSettings;
10-
using Flow.Launcher.Plugin.SharedCommands;
1110
using Windows.Win32;
1211
using Windows.Win32.Foundation;
1312
using Windows.Win32.Security;
@@ -20,6 +19,7 @@ namespace Flow.Launcher.Plugin.Sys
2019
public class Main : IPlugin, ISettingProvider, IPluginI18n
2120
{
2221
private PluginInitContext context;
22+
private ThemeSelector themeSelector;
2323
private Dictionary<string, string> KeywordTitleMappings = new Dictionary<string, string>();
2424

2525
// SHTDN_REASON_MAJOR_OTHER indicates a generic shutdown reason that isn't categorized under hardware failure, software updates, or other predefined reasons.
@@ -34,6 +34,11 @@ public Control CreateSettingPanel()
3434

3535
public List<Result> Query(Query query)
3636
{
37+
if(query.Search.StartsWith(ThemeSelector.Keyword))
38+
{
39+
return themeSelector.Query(query);
40+
}
41+
3742
var commands = Commands();
3843
var results = new List<Result>();
3944
foreach (var c in commands)
@@ -82,6 +87,7 @@ private string GetDynamicTitle(Query query, Result result)
8287
public void Init(PluginInitContext context)
8388
{
8489
this.context = context;
90+
themeSelector = new ThemeSelector(context);
8591
KeywordTitleMappings = new Dictionary<string, string>{
8692
{"Shutdown", "flowlauncher_plugin_sys_shutdown_computer_cmd"},
8793
{"Restart", "flowlauncher_plugin_sys_restart_computer_cmd"},
@@ -102,7 +108,8 @@ public void Init(PluginInitContext context)
102108
{"Open Log Location", "flowlauncher_plugin_sys_open_log_location_cmd"},
103109
{"Flow Launcher Tips", "flowlauncher_plugin_sys_open_docs_tips_cmd"},
104110
{"Flow Launcher UserData Folder", "flowlauncher_plugin_sys_open_userdata_location_cmd"},
105-
{"Toggle Game Mode", "flowlauncher_plugin_sys_toggle_game_mode_cmd"}
111+
{"Toggle Game Mode", "flowlauncher_plugin_sys_toggle_game_mode_cmd"},
112+
{"Set Flow Launcher Theme", "flowlauncher_plugin_sys_theme_selector_cmd"}
106113
};
107114
}
108115

@@ -451,6 +458,18 @@ private List<Result> Commands()
451458
context.API.ToggleGameMode();
452459
return true;
453460
}
461+
},
462+
new Result
463+
{
464+
Title = "Set Flow Launcher Theme",
465+
SubTitle = context.API.GetTranslation("flowlauncher_plugin_sys_theme_selector"),
466+
IcoPath = "Images\\app.png",
467+
Glyph = new GlyphInfo (FontFamily:"/Resources/#Segoe Fluent Icons", Glyph:"\ue7fc"),
468+
Action = c =>
469+
{
470+
context.API.ChangeQuery($"{ThemeSelector.Keyword} ");
471+
return false;
472+
}
454473
}
455474
});
456475

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using CommunityToolkit.Mvvm.DependencyInjection;
4+
using Flow.Launcher.Core.Resource;
5+
using Flow.Launcher.Infrastructure.UserSettings;
6+
7+
namespace Flow.Launcher.Plugin.Sys
8+
{
9+
public class ThemeSelector
10+
{
11+
public const string Keyword = "fltheme";
12+
13+
private readonly Settings _settings;
14+
private readonly Theme _theme;
15+
private readonly PluginInitContext _context;
16+
17+
#region Theme Selection
18+
19+
// Theme select codes simplified from SettingsPaneThemeViewModel.cs
20+
21+
private Theme.ThemeData _selectedTheme;
22+
private Theme.ThemeData SelectedTheme
23+
{
24+
get => _selectedTheme ??= Themes.Find(v => v.FileNameWithoutExtension == _theme.CurrentTheme);
25+
set
26+
{
27+
_selectedTheme = value;
28+
_theme.ChangeTheme(value.FileNameWithoutExtension);
29+
30+
if (_theme.BlurEnabled && _settings.UseDropShadowEffect)
31+
{
32+
_theme.RemoveDropShadowEffectFromCurrentTheme();
33+
_settings.UseDropShadowEffect = false;
34+
}
35+
}
36+
}
37+
38+
private List<Theme.ThemeData> Themes => _theme.LoadAvailableThemes();
39+
40+
#endregion
41+
42+
public ThemeSelector(PluginInitContext context)
43+
{
44+
_context = context;
45+
_theme = Ioc.Default.GetRequiredService<Theme>();
46+
_settings = Ioc.Default.GetRequiredService<Settings>();
47+
}
48+
49+
public List<Result> Query(Query query)
50+
{
51+
var search = query.SecondToEndSearch;
52+
if (string.IsNullOrWhiteSpace(search))
53+
{
54+
return Themes.Select(CreateThemeResult)
55+
.OrderBy(x => x.Title)
56+
.ToList();
57+
}
58+
59+
return Themes.Select(theme => (theme, matchResult: _context.API.FuzzySearch(search, theme.Name)))
60+
.Where(x => x.matchResult.IsSearchPrecisionScoreMet())
61+
.Select(x => CreateThemeResult(x.theme, x.matchResult.Score, x.matchResult.MatchData))
62+
.OrderBy(x => x.Title)
63+
.ToList();
64+
}
65+
66+
private Result CreateThemeResult(Theme.ThemeData theme) => CreateThemeResult(theme, 0, null);
67+
68+
private Result CreateThemeResult(Theme.ThemeData theme, int score, IList<int> highlightData)
69+
{
70+
string themeName = theme.Name;
71+
string title;
72+
if (theme == SelectedTheme)
73+
{
74+
title = $"{theme.Name} ★";
75+
// Set current theme to the top
76+
score = 2000;
77+
}
78+
else
79+
{
80+
title = theme.Name;
81+
// Set them to 1000 so that they are higher than other non-theme records
82+
score = 1000;
83+
}
84+
85+
string description = string.Empty;
86+
if (theme.IsDark == true)
87+
{
88+
description += _context.API.GetTranslation("TypeIsDarkToolTip");
89+
}
90+
91+
if (theme.HasBlur == true)
92+
{
93+
if (!string.IsNullOrEmpty(description))
94+
description += " ";
95+
description += _context.API.GetTranslation("TypeHasBlurToolTip");
96+
}
97+
98+
return new Result
99+
{
100+
Title = title,
101+
TitleHighlightData = highlightData,
102+
SubTitle = description,
103+
IcoPath = "Images\\theme_selector.png",
104+
Glyph = new GlyphInfo("/Resources/#Segoe Fluent Icons", "\ue790"),
105+
Score = score,
106+
Action = c =>
107+
{
108+
SelectedTheme = theme;
109+
_context.API.ReQuery();
110+
return false;
111+
}
112+
};
113+
}
114+
}
115+
}

0 commit comments

Comments
 (0)