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
Prev Previous commit
Next Next commit
Fix fluent style
  • Loading branch information
mantaionut committed Dec 4, 2024
commit 52ac97b61b4def9326217c6e46c1ed7d870607e6
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright (c) Microsoft Corporation
// 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.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;

#pragma warning disable IDE0130 // Namespace does not match folder structure
namespace Fluent.Controls
#pragma warning restore IDE0130 // Namespace does not match folder structure
{
internal sealed class AnimationFactorToValueConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (values[0] is not double completeValue)
{
return 0.0;
}

if (values[1] is not double factor)
{
return 0.0;
}

if (parameter is "negative")
{
factor = -factor;
}

return factor * completeValue;
}

public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright (c) Microsoft Corporation
// 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.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Windows.Data;

using System.Windows.Media;

#pragma warning disable IDE0130 // Namespace does not match folder structure
namespace Fluent.Controls
#pragma warning restore IDE0130 // Namespace does not match folder structure
{
internal sealed class FallbackBrushConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is SolidColorBrush brush)
{
return brush;
}

if (value is Color color)
{
return new SolidColorBrush(color);
}

// We draw red to visibly see an invalid bind in the UI.
return Brushes.Red;
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
60 changes: 21 additions & 39 deletions src/modules/launcher/PowerLauncher/Helper/ThemeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,18 @@ 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)
{
// 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
}
// 0 = Dark mode, 1 = Light mode
bool isLightMode = Convert.ToBoolean((int)registryValueObj, CultureInfo.InvariantCulture);
return !isLightMode; // Invert because 0 = Dark
}
catch (Exception ex)
else
{
// Handle errors reading from the registry
Console.WriteLine($"Error reading registry: {ex.Message}");
return false; // Default to dark mode on error
// Default to Light theme if the registry key is missing
return false; // Default to dark mode assumption
}
}

Expand All @@ -60,31 +51,22 @@ public static Theme GetHighContrastBaseType()
const string registryKey = @"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Themes";
const string registryValue = "CurrentTheme";

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

string theme = themePath.Split('\\').Last().Split('.').First().ToLowerInvariant();
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)
return theme switch
{
// Handle errors reading from the registry
Console.WriteLine($"Error reading registry: {ex.Message}");
return Theme.Light; // Default to light theme on error
}
"hc1" => Theme.HighContrastOne,
"hc2" => Theme.HighContrastTwo,
"hcwhite" => Theme.HighContrastWhite,
"hcblack" => Theme.HighContrastBlack,
_ => Theme.Light,
};
}
}
}
102 changes: 46 additions & 56 deletions src/modules/launcher/PowerLauncher/Helper/ThemeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,11 @@
// See the LICENSE file in the project root for more information.

using System;
using System.Runtime.CompilerServices;
using System.IO;
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 static PowerLauncher.Helper.WindowsInteropHelper;

namespace PowerLauncher.Helper
{
Expand All @@ -38,84 +33,79 @@ private void OnUserPreferenceChanged(object sender, UserPreferenceChangedEventAr
{
if (e.Category == UserPreferenceCategory.General)
{
// Theme has changed, update resources
// When switching from high contrast to dark mode we have to use UserPreferenceCategory.General otherwise it will crash when loading fluent.xaml
UpdateTheme();
}
else if (e.Category == UserPreferenceCategory.Color)
{
// https://github.com/dotnet/wpf/issues/10043 When switching to high contrast we have to use UserPreferenceCategory.Color or it will crash due to fluent.xaml being already loaded.
if (_currentTheme is ManagedCommon.Theme.Dark or ManagedCommon.Theme.Light)
{
UpdateTheme();
}
}
}

private void SetSystemTheme(ManagedCommon.Theme theme)
{
#pragma warning disable WPF0001
if (theme == ManagedCommon.Theme.Dark)
{
_mainWindow.ThemeMode = ThemeMode.Dark;
ImageLoader.UpdateIconPath(ManagedCommon.Theme.Dark);
ThemeChanged?.Invoke(ManagedCommon.Theme.Dark, ManagedCommon.Theme.Light);
}
else if (theme == ManagedCommon.Theme.Light)
_mainWindow.Resources.MergedDictionaries.Clear();

if (theme is ManagedCommon.Theme.Dark or ManagedCommon.Theme.Light)
{
_mainWindow.ThemeMode = ThemeMode.Light;
ImageLoader.UpdateIconPath(ManagedCommon.Theme.Light);
ThemeChanged?.Invoke(ManagedCommon.Theme.Light, ManagedCommon.Theme.Dark);
string themeString = theme == ManagedCommon.Theme.Light ? "pack://application:,,,/PresentationFramework.Fluent;component/Themes/Fluent.Light.xaml"
: "pack://application:,,,/PresentationFramework.Fluent;component/Themes/Fluent.Dark.xaml";
ResourceDictionary fluentThemeDictionary = new()
{
Source = new Uri(themeString, UriKind.Absolute),
};
_mainWindow.Resources.MergedDictionaries.Add(fluentThemeDictionary);
}
else
{
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),
Source = new Uri("Styles/FluentHC.xaml", UriKind.Relative),
});
}

_mainWindow.Resources.MergedDictionaries.Add(new ResourceDictionary
{
Source = new Uri("Styles/Styles.xaml", UriKind.Relative),
});
string styleThemeString = theme switch
{
ManagedCommon.Theme.Light => "Themes/Light.xaml",
ManagedCommon.Theme.Dark => "Themes/Dark.xaml",
ManagedCommon.Theme.HighContrastOne => "Themes/HighContrast1.xaml",
ManagedCommon.Theme.HighContrastTwo => "Themes/HighContrast2.xaml",
ManagedCommon.Theme.HighContrastWhite => "Themes/HighContrastWhite.xaml",
_ => "Themes/HighContrastBlack.xaml",
};
_mainWindow.Resources.MergedDictionaries.Add(new ResourceDictionary
{
Source = new Uri(styleThemeString, UriKind.Relative),
});
ImageLoader.UpdateIconPath(theme);
ThemeChanged(_currentTheme, theme);
_currentTheme = theme;
}

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

_mainWindow.Dispatcher.Invoke(() =>
{
SetSystemTheme(_currentTheme);
SetSystemTheme(newTheme);
});
}

Expand Down
31 changes: 16 additions & 15 deletions src/modules/launcher/PowerLauncher/LauncherControl.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@
xmlns:local="clr-namespace:PowerLauncher"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:p="clr-namespace:PowerLauncher.Properties"

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 +33,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 +47,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 @@ -117,10 +116,12 @@
FontSize="{DynamicResource TitleFontSize}"
Foreground="{DynamicResource TextPlaceholderColorBrush}" />
<TextBlock
AutomationProperties.Name="{x:Static p:Resources.SearchIcon}"
AutomationProperties.Name="{x:Static p:Resources.SearchIcon}"
FontSize="20"
Foreground="{DynamicResource TextPlaceholderColorBrush}"
FontFamily="Segoe Fluent Icons, Segoe MDL2 Assets"
Text="&#xE094;" />
Text="&#xE094;"
Grid.Column="0"
Margin="10,7,0,0" />
</Grid>
</UserControl>
Loading