mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-07 11:46:30 +02:00
Theme aware plugin (#4499)
* Migrate theme manager to infrastructure and added it as input to public API instance * Working event-delegate for PublicAPIInstance * Theme aware UWP applications * Theme aware program plugin * Update query icon on theme change * Theme aware calculator plugin * Fix issue with query running before theme change * Theme based changes in ImageLoader * Removed ErrorIcon direct references and added references from ImageLoader * Nit fixes * Removed unnecessary TODO in UWP.cs * Added preference to theme based icons * Added IDisposable interfaces to unsubscribe events
This commit is contained in:
committed by
GitHub
parent
d9597d5ad5
commit
d3b10d0d4d
|
Before Width: | Height: | Size: 597 B After Width: | Height: | Size: 597 B |
Binary file not shown.
|
After Width: | Height: | Size: 597 B |
@@ -1,4 +1,6 @@
|
||||
using System.Collections.Generic;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading;
|
||||
@@ -8,7 +10,7 @@ using Wox.Plugin;
|
||||
|
||||
namespace Microsoft.Plugin.Calculator
|
||||
{
|
||||
public class Main : IPlugin, IPluginI18n
|
||||
public class Main : IPlugin, IPluginI18n, IDisposable
|
||||
{
|
||||
private static readonly Regex RegValidExpressChar = new Regex(
|
||||
@"^(" +
|
||||
@@ -22,6 +24,8 @@ namespace Microsoft.Plugin.Calculator
|
||||
private static readonly Regex RegBrackets = new Regex(@"[\(\)\[\]]", RegexOptions.Compiled);
|
||||
private static readonly Engine MagesEngine;
|
||||
private PluginInitContext Context { get; set; }
|
||||
private string IconPath { get; set; }
|
||||
private bool _disposed = false;
|
||||
|
||||
static Main()
|
||||
{
|
||||
@@ -58,7 +62,7 @@ namespace Microsoft.Plugin.Calculator
|
||||
new Result
|
||||
{
|
||||
Title = result.ToString(),
|
||||
IcoPath = "Images/calculator.png",
|
||||
IcoPath = IconPath,
|
||||
Score = 300,
|
||||
SubTitle = Context.API.GetTranslation("wox_plugin_calculator_copy_number_to_clipboard"),
|
||||
Action = c =>
|
||||
@@ -115,6 +119,26 @@ namespace Microsoft.Plugin.Calculator
|
||||
public void Init(PluginInitContext context)
|
||||
{
|
||||
Context = context;
|
||||
Context.API.ThemeChanged += OnThemeChanged;
|
||||
UpdateIconPath(Context.API.GetCurrentTheme());
|
||||
}
|
||||
|
||||
// Todo : Update with theme based IconPath
|
||||
private void UpdateIconPath(Theme theme)
|
||||
{
|
||||
if (theme == Theme.Light || theme == Theme.HighContrastWhite)
|
||||
{
|
||||
IconPath = "Images/calculator_light.png";
|
||||
}
|
||||
else
|
||||
{
|
||||
IconPath = "Images/calculator_dark.png";
|
||||
}
|
||||
}
|
||||
|
||||
private void OnThemeChanged(Theme _, Theme newTheme)
|
||||
{
|
||||
UpdateIconPath(newTheme);
|
||||
}
|
||||
|
||||
public string GetTranslatedPluginTitle()
|
||||
@@ -126,5 +150,23 @@ namespace Microsoft.Plugin.Calculator
|
||||
{
|
||||
return Context.API.GetTranslation("wox_plugin_calculator_plugin_description");
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(disposing: true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
protected virtual void Dispose(bool disposing)
|
||||
{
|
||||
if (!_disposed)
|
||||
{
|
||||
if (disposing)
|
||||
{
|
||||
Context.API.ThemeChanged -= OnThemeChanged;
|
||||
_disposed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,7 +49,7 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Include="Images\calculator.png">
|
||||
<None Include="Images\calculator_light.png">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
@@ -107,5 +107,11 @@
|
||||
<PackageReference Include="Mages" Version="1.6.0" />
|
||||
<PackageReference Include="System.Runtime" Version="4.3.1" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Update="Images\calculator_dark.png">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -13,11 +13,11 @@ using Wox.Plugin;
|
||||
using Microsoft.Plugin.Program.Views;
|
||||
|
||||
using Stopwatch = Wox.Infrastructure.Stopwatch;
|
||||
|
||||
using Microsoft.Plugin.Program.Programs;
|
||||
|
||||
namespace Microsoft.Plugin.Program
|
||||
{
|
||||
public class Main : ISettingProvider, IPlugin, IPluginI18n, IContextMenu, ISavable, IReloadable
|
||||
public class Main : ISettingProvider, IPlugin, IPluginI18n, IContextMenu, ISavable, IReloadable, IDisposable
|
||||
{
|
||||
private static readonly object IndexLock = new object();
|
||||
internal static Programs.Win32[] _win32s { get; set; }
|
||||
@@ -34,6 +34,7 @@ namespace Microsoft.Plugin.Program
|
||||
private static BinaryStorage<Programs.Win32[]> _win32Storage;
|
||||
private static BinaryStorage<Programs.UWP.Application[]> _uwpStorage;
|
||||
private readonly PluginJsonStorage<Settings> _settingsStorage;
|
||||
private bool _disposed = false;
|
||||
|
||||
public Main()
|
||||
{
|
||||
@@ -104,6 +105,21 @@ namespace Microsoft.Plugin.Program
|
||||
public void Init(PluginInitContext context)
|
||||
{
|
||||
_context = context;
|
||||
_context.API.ThemeChanged += OnThemeChanged;
|
||||
UpdateUWPIconPath(_context.API.GetCurrentTheme());
|
||||
}
|
||||
|
||||
public void OnThemeChanged(Theme _, Theme currentTheme)
|
||||
{
|
||||
UpdateUWPIconPath(currentTheme);
|
||||
}
|
||||
|
||||
public void UpdateUWPIconPath(Theme theme)
|
||||
{
|
||||
foreach (UWP.Application app in _uwps)
|
||||
{
|
||||
app.UpdatePath(theme);
|
||||
}
|
||||
}
|
||||
|
||||
public static void IndexWin32Programs()
|
||||
@@ -186,6 +202,25 @@ namespace Microsoft.Plugin.Program
|
||||
public void UpdateSettings(PowerLauncherSettings settings)
|
||||
{
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(disposing: true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
protected virtual void Dispose(bool disposing)
|
||||
{
|
||||
if (!_disposed)
|
||||
{
|
||||
if (disposing)
|
||||
{
|
||||
_context.API.ThemeChanged -= OnThemeChanged;
|
||||
_disposed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void InitializeFileWatchers()
|
||||
{
|
||||
// Create a new FileSystemWatcher and set its properties.
|
||||
|
||||
@@ -21,6 +21,7 @@ using System.Windows.Input;
|
||||
using System.Runtime.InteropServices.ComTypes;
|
||||
using Wox.Plugin.SharedCommands;
|
||||
using System.Reflection;
|
||||
using Wox.Infrastructure.Image;
|
||||
|
||||
namespace Microsoft.Plugin.Program.Programs
|
||||
{
|
||||
@@ -404,7 +405,6 @@ namespace Microsoft.Plugin.Program.Programs
|
||||
DisplayName = ResourceFromPri(package.FullName, DisplayName);
|
||||
Description = ResourceFromPri(package.FullName, Description);
|
||||
LogoUri = LogoUriFromManifest(manifestApp);
|
||||
LogoPath = LogoPathFromUri(LogoUri);
|
||||
|
||||
Enabled = true;
|
||||
CanRunElevated = IfApplicationcanRunElevated();
|
||||
@@ -516,7 +516,19 @@ namespace Microsoft.Plugin.Program.Programs
|
||||
}
|
||||
}
|
||||
|
||||
internal string LogoPathFromUri(string uri)
|
||||
public void UpdatePath(Theme theme)
|
||||
{
|
||||
if (theme == Theme.Light || theme == Theme.HighContrastWhite)
|
||||
{
|
||||
LogoPath = LogoPathFromUri(LogoUri, "contrast-white");
|
||||
}
|
||||
else
|
||||
{
|
||||
LogoPath = LogoPathFromUri(LogoUri, "contrast-black");
|
||||
}
|
||||
}
|
||||
|
||||
internal string LogoPathFromUri(string uri, string theme)
|
||||
{
|
||||
// all https://msdn.microsoft.com/windows/uwp/controls-and-patterns/tiles-and-notifications-app-assets
|
||||
// windows 10 https://msdn.microsoft.com/en-us/library/windows/apps/dn934817.aspx
|
||||
@@ -539,11 +551,7 @@ namespace Microsoft.Plugin.Program.Programs
|
||||
{
|
||||
var end = path.Length - extension.Length;
|
||||
var prefix = path.Substring(0, end);
|
||||
var paths = new List<string> { path };
|
||||
|
||||
// TODO: This value must be set in accordance to the WPF theme (work in progress).
|
||||
// Must be set to `contrast-white` for light theme and to `contrast-black` for dark theme to get an icon of the contrasting color.
|
||||
var theme = "contrast-black";
|
||||
var paths = new List<string> { path };
|
||||
|
||||
var scaleFactors = new Dictionary<PackageVersion, List<int>>
|
||||
{
|
||||
@@ -563,6 +571,7 @@ namespace Microsoft.Plugin.Program.Programs
|
||||
}
|
||||
}
|
||||
|
||||
paths = paths.OrderByDescending(x => x.Contains(theme)).ToList();
|
||||
var selected = paths.FirstOrDefault(File.Exists);
|
||||
if (!string.IsNullOrEmpty(selected))
|
||||
{
|
||||
@@ -589,6 +598,7 @@ namespace Microsoft.Plugin.Program.Programs
|
||||
pathFactorPairs.Add(prefixThemePath, factor);
|
||||
}
|
||||
|
||||
paths = paths.OrderByDescending(x => x.Contains(theme)).ToList();
|
||||
var selectedIconPath = paths.OrderBy(x => Math.Abs(pathFactorPairs.GetValueOrDefault(x) - appIconSize)).FirstOrDefault(File.Exists);
|
||||
if (!string.IsNullOrEmpty(selectedIconPath))
|
||||
{
|
||||
@@ -630,7 +640,7 @@ namespace Microsoft.Plugin.Program.Programs
|
||||
ProgramLogger.LogException($"|UWP|ImageFromPath|{path}" +
|
||||
$"|Unable to get logo for {UserModelId} from {path} and" +
|
||||
$" located in {Package.Location}", new FileNotFoundException());
|
||||
return new BitmapImage(new Uri(Constant.ErrorIcon));
|
||||
return new BitmapImage(new Uri(ImageLoader.ErrorIconPath));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user