Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Launcher] use .net WPF #36215

Merged
merged 29 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
652050b
Initial implementation
mantaionut Nov 26, 2024
52ac97b
Fix fluent style
mantaionut Dec 4, 2024
efcaf86
Fix no endline
mantaionut Dec 4, 2024
3604572
Update expect.txt
crutkas Dec 4, 2024
c44b566
Fix formatting
mantaionut Dec 5, 2024
d8234be
Fix light theme looking bad on Windows 10
mantaionut Dec 5, 2024
e6744dc
fix formatting
mantaionut Dec 5, 2024
63c4e70
test change
mantaionut Dec 5, 2024
9ab827c
Now really fixed W10
mantaionut Dec 5, 2024
c66e432
Add a comment
mantaionut Dec 5, 2024
bd93564
Fix typos
mantaionut Dec 5, 2024
b369aea
Fix spellcheck errors
jaimecbernardo Dec 5, 2024
14007e2
Merge branch 'Launcher_use_net_wpf' of https://github.com/mantaionut/…
jaimecbernardo Dec 5, 2024
61ad561
Fix spellcheck pattern for websites
jaimecbernardo Dec 5, 2024
5f5f434
Change patterns for spellcheck in the right file
jaimecbernardo Dec 5, 2024
76ea8c2
Fix XAML styling
jaimecbernardo Dec 5, 2024
f0a582e
Fix contrast colors on W11
mantaionut Dec 6, 2024
8a564d6
Fix formatting
mantaionut Dec 6, 2024
3f74844
Removed emty line
mantaionut Dec 6, 2024
920db49
Fix formatting
mantaionut Dec 6, 2024
ecb2cba
Added comment to fluentHC file
mantaionut Dec 6, 2024
e5c9070
fix comment
mantaionut Dec 6, 2024
b4cc548
Fix Windows10 again.
mantaionut Dec 6, 2024
bcc7e65
Merge branch 'main' into Launcher_use_net_wpf
jaimecbernardo Dec 9, 2024
717d6cb
W11 fix chaning from high contrast to normal not having correct backg…
mantaionut Dec 9, 2024
d7b7468
W10 Fix high contrast not working after switching from light/dark moed
mantaionut Dec 10, 2024
87c05a7
Address feedback
mantaionut Dec 10, 2024
91c7706
Fix formatting
mantaionut Dec 10, 2024
ecc468a
Second W11 fix chaning from high contrast to normal not having correc…
mantaionut Dec 10, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Initial implementation
  • Loading branch information
mantaionut committed Nov 27, 2024
commit 652050be7fbed45bb759286e062691208f12a450
2 changes: 0 additions & 2 deletions src/modules/launcher/PowerLauncher/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ui:ThemesDictionary Theme="Dark" />
<ui:ControlsDictionary />
<ResourceDictionary Source="pack://application:,,,/Styles/Styles.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
Expand Down
89 changes: 66 additions & 23 deletions src/modules/launcher/PowerLauncher/Helper/ThemeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,88 @@
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Globalization;
using System.Linq;

using ManagedCommon;
using Microsoft.Win32;
using Wpf.Ui.Appearance;

namespace PowerLauncher.Helper
{
public static class ThemeExtensions
{
public static Theme ToTheme(this ApplicationTheme applicationTheme)
public static Theme GetCurrentTheme()
{
return applicationTheme switch
// Check for high-contrast mode
Theme highContrastTheme = GetHighContrastBaseType();
if (highContrastTheme != Theme.Light)
{
ApplicationTheme.Dark => Theme.Dark,
ApplicationTheme.Light => Theme.Light,
ApplicationTheme.HighContrast => GetHighContrastBaseType(),
_ => Theme.Light,
};
return highContrastTheme;
}

// Check if the system is using dark or light mode
return IsSystemDarkMode() ? Theme.Dark : Theme.Light;
}

private static bool IsSystemDarkMode()
{
const string registryKey = @"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Themes\Personalize";
const string registryValue = "AppsUseLightTheme";

try
{
// Retrieve the registry value, which is a DWORD (0 or 1)
object registryValueObj = Registry.GetValue(registryKey, registryValue, null);
if (registryValueObj != null)
{
// 0 = Dark mode, 1 = Light mode
bool isLightMode = Convert.ToBoolean((int)registryValueObj, CultureInfo.InvariantCulture);
return !isLightMode; // Invert because 0 = Dark
}
else
{
// Default to Light theme if the registry key is missing
return false; // Default to dark mode assumption
}
}
catch (Exception ex)
{
// Handle errors reading from the registry
Console.WriteLine($"Error reading registry: {ex.Message}");
return false; // Default to dark mode on error
}
}

private static Theme GetHighContrastBaseType()
public static Theme GetHighContrastBaseType()
{
string registryKey = @"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Themes";
string theme = (string)Registry.GetValue(registryKey, "CurrentTheme", string.Empty);
theme = theme.Split('\\').Last().Split('.').First().ToString();
const string registryKey = @"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Themes";
const string registryValue = "CurrentTheme";

switch (theme)
try
{
string themePath = (string)Registry.GetValue(registryKey, registryValue, string.Empty);
if (string.IsNullOrEmpty(themePath))
{
return Theme.Light; // Default to light theme if missing
}

string theme = themePath.Split('\\').Last().Split('.').First().ToLowerInvariant();

return theme switch
{
"hc1" => Theme.HighContrastOne,
"hc2" => Theme.HighContrastTwo,
"hcwhite" => Theme.HighContrastWhite,
"hcblack" => Theme.HighContrastBlack,
_ => Theme.Light,
};
}
catch (Exception ex)
{
case "hc1":
return Theme.HighContrastOne;
case "hc2":
return Theme.HighContrastTwo;
case "hcwhite":
return Theme.HighContrastWhite;
case "hcblack":
return Theme.HighContrastBlack;
default:
return Theme.HighContrastOne;
// Handle errors reading from the registry
Console.WriteLine($"Error reading registry: {ex.Message}");
return Theme.Light; // Default to light theme on error
}
}
}
Expand Down
116 changes: 84 additions & 32 deletions src/modules/launcher/PowerLauncher/Helper/ThemeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,56 +3,120 @@
// See the LICENSE file in the project root for more information.

using System;

using System.Runtime.CompilerServices;
using System.Windows;
using ControlzEx.Theming;
using ManagedCommon;
using Microsoft.Office.Interop.OneNote;
using Microsoft.Win32;
using UnitsNet;
using Wox.Infrastructure.Image;
using Wox.Infrastructure.UserSettings;
using Wpf.Ui.Appearance;
using static PowerLauncher.Helper.WindowsInteropHelper;

namespace PowerLauncher.Helper
{
public class ThemeManager : IDisposable
{
private readonly PowerToysRunSettings _settings;
private readonly MainWindow _mainWindow;
private Theme _currentTheme;
private ManagedCommon.Theme _currentTheme;
private bool _disposed;

public Theme CurrentTheme => _currentTheme;
public ManagedCommon.Theme CurrentTheme => _currentTheme;

public event Common.UI.ThemeChangedHandler ThemeChanged;

public ThemeManager(PowerToysRunSettings settings, MainWindow mainWindow)
{
_settings = settings;
_mainWindow = mainWindow;
_currentTheme = ApplicationThemeManager.GetAppTheme().ToTheme();
SetTheme(false);
SystemEvents.UserPreferenceChanged += OnUserPreferenceChanged;
}

ApplicationThemeManager.Changed += ApplicationThemeManager_Changed;
private void OnUserPreferenceChanged(object sender, UserPreferenceChangedEventArgs e)
{
if (e.Category == UserPreferenceCategory.General)
{
// Theme has changed, update resources
UpdateTheme();
}
}

public void SetTheme(bool fromSettings)
private void SetSystemTheme(ManagedCommon.Theme theme)
{
if (_settings.Theme == Theme.Light)
#pragma warning disable WPF0001
if (theme == ManagedCommon.Theme.Dark)
{
_currentTheme = Theme.Light;
_mainWindow?.Dispatcher.Invoke(() => ApplicationThemeManager.Apply(ApplicationTheme.Light, _mainWindow.WindowBackdropType));
_mainWindow.ThemeMode = ThemeMode.Dark;
ImageLoader.UpdateIconPath(ManagedCommon.Theme.Dark);
ThemeChanged?.Invoke(ManagedCommon.Theme.Dark, ManagedCommon.Theme.Light);
}
else if (_settings.Theme == Theme.Dark)
else if (theme == ManagedCommon.Theme.Light)
{
_currentTheme = Theme.Dark;
_mainWindow?.Dispatcher.Invoke(() => ApplicationThemeManager.Apply(ApplicationTheme.Dark, _mainWindow.WindowBackdropType));
_mainWindow.ThemeMode = ThemeMode.Light;
ImageLoader.UpdateIconPath(ManagedCommon.Theme.Light);
ThemeChanged?.Invoke(ManagedCommon.Theme.Light, ManagedCommon.Theme.Dark);
}
else if (fromSettings)
else
{
_mainWindow?.Dispatcher.Invoke(ApplicationThemeManager.ApplySystemTheme);
ImageLoader.UpdateIconPath(theme);
_mainWindow.ThemeMode = ThemeMode.None;
string themeString = theme switch
{
ManagedCommon.Theme.HighContrastOne => "Themes/HighContrast1.xaml",
ManagedCommon.Theme.HighContrastTwo => "Themes/HighContrast2.xaml",
ManagedCommon.Theme.HighContrastWhite => "Themes/HighContrastWhite.xaml",
_ => "Themes/HighContrastBlack.xaml",
};
if (_mainWindow.Resources.Contains("SystemColorWindowColorBrush"))
{
_mainWindow.Resources.Remove("SystemColorWindowColorBrush");
}

_mainWindow.Resources.MergedDictionaries.Clear();
if (_mainWindow.Resources.Contains("SystemColorWindowColorBrush"))
{
_mainWindow.Resources.Remove("SystemColorWindowColorBrush");
}

/* _mainWindow.Resources.MergedDictionaries.Add(new ResourceDictionary
{
Source = new Uri("pack://application:,,,/PresentationFramework.Fluent;component/Themes/Fluent.xaml", UriKind.Absolute),
});*/
_mainWindow.Resources.MergedDictionaries.Add(new ResourceDictionary
{
Source = new Uri("Styles/Styles.xaml", UriKind.Relative),
});
if (_mainWindow.Resources.Contains("SystemColorWindowColorBrush"))
{
_mainWindow.Resources.Remove("SystemColorWindowColorBrush");
}

_mainWindow.Resources.MergedDictionaries.Add(new ResourceDictionary
{
Source = new Uri(themeString, UriKind.Relative),
});
}
}

ImageLoader.UpdateIconPath(_currentTheme);
public void UpdateTheme()
{
_currentTheme = _settings.Theme;
ManagedCommon.Theme theme = ThemeExtensions.GetHighContrastBaseType();
if (theme != ManagedCommon.Theme.Light)
{
_currentTheme = theme;
}
else if (_settings.Theme == ManagedCommon.Theme.System)
{
_currentTheme = ThemeExtensions.GetCurrentTheme();
}

// oldTheme isn't used
ThemeChanged?.Invoke(_currentTheme, _currentTheme);
_mainWindow.Dispatcher.Invoke(() =>
{
SetSystemTheme(_currentTheme);
});
}

public void Dispose()
Expand All @@ -61,18 +125,6 @@ public void Dispose()
GC.SuppressFinalize(this);
}

private void ApplicationThemeManager_Changed(ApplicationTheme currentApplicationTheme, System.Windows.Media.Color systemAccent)
{
var newTheme = currentApplicationTheme.ToTheme();
if (_currentTheme == newTheme)
{
return;
}

_currentTheme = newTheme;
SetTheme(false);
}

protected virtual void Dispose(bool disposing)
{
if (_disposed)
Expand All @@ -82,7 +134,7 @@ protected virtual void Dispose(bool disposing)

if (disposing)
{
ApplicationThemeManager.Changed -= ApplicationThemeManager_Changed;
SystemEvents.UserPreferenceChanged -= OnUserPreferenceChanged;
}

_disposed = true;
Expand Down
33 changes: 17 additions & 16 deletions src/modules/launcher/PowerLauncher/LauncherControl.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
xmlns:local="clr-namespace:PowerLauncher"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:p="clr-namespace:PowerLauncher.Properties"
xmlns:ui="http://schemas.lepo.co/wpfui/2022/xaml"

d:DesignHeight="300"
d:DesignWidth="720"
mc:Ignorable="d">
<UserControl.Resources>
<Style
x:Key="QueryTextBoxStyle"
BasedOn="{StaticResource DefaultTextBoxStyle}"
TargetType="{x:Type TextBox}">
<Style
x:Key="QueryTextBoxStyle"
BasedOn="{StaticResource DefaultTextBoxStyle}"
TargetType="{x:Type TextBox}">
<Setter Property="Background" Value="Transparent" />
<Setter Property="BorderThickness" Value="0" />
<Setter Property="Foreground" Value="{DynamicResource TextFillColorPrimaryBrush}" />
Expand All @@ -34,7 +34,7 @@
Margin="6,0,0,0"
VerticalAlignment="Center"
FontSize="{DynamicResource TitleFontSize}"
Text="{TemplateBinding Tag}">
Text="{TemplateBinding Tag}">
<TextBlock.Style>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Foreground" Value="Transparent" />
Expand All @@ -48,16 +48,16 @@
</TextBlock>
<Border
x:Name="border"
Background="{TemplateBinding Background}"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
SnapsToDevicePixels="True">
BorderThickness="{TemplateBinding BorderThickness}"
SnapsToDevicePixels="True">
<ScrollViewer
x:Name="PART_ContentHost"
Background="{TemplateBinding Background}"
Focusable="false"
HorizontalScrollBarVisibility="Hidden"
VerticalScrollBarVisibility="Hidden">
Background="{TemplateBinding Background}"
Focusable="false"
HorizontalScrollBarVisibility="Hidden"
VerticalScrollBarVisibility="Hidden">
<ScrollViewer.ContentTemplate>
<DataTemplate>
<Grid Background="{Binding Background, ElementName=PART_ContentHost}">
Expand Down Expand Up @@ -116,10 +116,11 @@
Canvas.ZIndex="-1"
FontSize="{DynamicResource TitleFontSize}"
Foreground="{DynamicResource TextPlaceholderColorBrush}" />
<ui:SymbolIcon
AutomationProperties.Name="{x:Static p:Resources.SearchIcon}"
<TextBlock
AutomationProperties.Name="{x:Static p:Resources.SearchIcon}"
FontSize="20"
Foreground="{DynamicResource TextPlaceholderColorBrush}"
Symbol="Search12" />
FontFamily="Segoe Fluent Icons, Segoe MDL2 Assets"
Text="&#xE094;" />
</Grid>
</UserControl>
Loading