Skip to content

Commit

Permalink
(chocolateyGH-685) Add ThemeMode and own ToggleSwitch with icons for …
Browse files Browse the repository at this point in the history
…dark and light mode
  • Loading branch information
punker76 committed Mar 2, 2021
1 parent 779519b commit c0c69f2
Show file tree
Hide file tree
Showing 7 changed files with 581 additions and 51 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Windows.Input;
using System.Windows.Media;
using ChocolateyGui.Common.Base;
using ChocolateyGui.Common.Enums;
using ChocolateyGui.Common.Windows.Commands;
using ChocolateyGui.Common.Windows.Theming;
using ControlzEx.Theming;
Expand All @@ -17,6 +18,7 @@ namespace ChocolateyGui.Common.Windows.Services
{
public class BundledThemeService : ObservableBase, IBundledThemeService
{
private ThemeMode _themeMode;
private bool _isLightTheme;
private Theme _light;
private Theme _dark;
Expand Down Expand Up @@ -46,24 +48,43 @@ public Theme Dark
private set => SetPropertyValue(ref _dark, value);
}

/// <inheritdoc />
public ThemeMode ThemeMode
{
get => _themeMode;
private set => SetPropertyValue(ref _themeMode, value);
}

/// <inheritdoc />
public bool IsLightTheme
{
get => _isLightTheme;
set => SetPropertyValue(ref _isLightTheme, value);
set
{
if (SetPropertyValue(ref _isLightTheme, value))
{
ThemeMode = value ? ThemeMode.Light : ThemeMode.Dark;
}
}
}

/// <inheritdoc />
public void SyncTheme()
public void SyncTheme(ThemeMode mode)
{
ThemeManager.Current.ThemeSyncMode = ThemeSyncMode.SyncWithAppMode;
ThemeManager.Current.SyncTheme();
if (mode == ThemeMode.WindowsDefault)
{
ThemeManager.Current.ThemeSyncMode = ThemeSyncMode.SyncWithAppMode;
ThemeManager.Current.SyncTheme();

var theme = ThemeManager.Current.DetectTheme();
if (theme != null)
var theme = ThemeManager.Current.DetectTheme();
IsLightTheme = theme is null || theme.BaseColorScheme == ThemeManager.BaseColorLight;
}
else
{
IsLightTheme = theme.BaseColorScheme == ThemeManager.BaseColorLight;
IsLightTheme = mode == ThemeMode.Light;
}

ThemeMode = mode;
}

/// <inheritdoc />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// --------------------------------------------------------------------------------------------------------------------

using System.Windows.Input;
using ChocolateyGui.Common.Enums;
using ControlzEx.Theming;

namespace ChocolateyGui.Common.Windows.Services
Expand All @@ -27,6 +28,11 @@ public interface IBundledThemeService
/// </summary>
Theme Dark { get; }

/// <summary>
/// Gets or sets a value indicating whether the current selected theme.
/// </summary>
ThemeMode ThemeMode { get; }

/// <summary>
/// Gets or sets a value indicating whether the current selected theme is the light theme.
/// </summary>
Expand All @@ -35,7 +41,8 @@ public interface IBundledThemeService
/// <summary>
/// Syncs the application with the "app mode" setting from windows which should be detected at runtime and the current <see cref="T:ControlzEx.Theming.Theme" /> be changed accordingly.
/// </summary>
void SyncTheme();
/// <param name="mode">The sync mode for this application.</param>
void SyncTheme(ThemeMode mode);

/// <summary>
/// Generates the themes for this application.
Expand Down
84 changes: 44 additions & 40 deletions Source/ChocolateyGui.Common.Windows/Views/ShellView.xaml
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
<metro:MetroWindow x:Class="ChocolateyGui.Common.Windows.Views.ShellView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:metro="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:cal="http://www.caliburnproject.org"
xmlns:properties="clr-namespace:ChocolateyGui.Common.Properties;assembly=ChocolateyGui.Common"
xmlns:system="clr-namespace:System;assembly=mscorlib"
xmlns:viewModels="clr-namespace:ChocolateyGui.Common.Windows.ViewModels;assembly=ChocolateyGui.Common.Windows"
xmlns:theming="clr-namespace:ChocolateyGui.Common.Windows.Theming"
xmlns:iconPacks="http://metro.mahapps.com/winfx/xaml/iconpacks"
mc:Ignorable="d"
Height="768" Width="1366"
GlowBrush="{StaticResource MahApps.Brushes.Highlight}"
d:DataContext="{d:DesignInstance Type=viewModels:ShellViewModel}"
SaveWindowPosition="True"
TitleForeground="Transparent"
ShowDialogsOverTitleBar="False"
ShowIconOnTitleBar="False"
OverlayOpacity="0.3">
<Window.CommandBindings>
<mah:MetroWindow x:Class="ChocolateyGui.Common.Windows.Views.ShellView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mah="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:cal="http://www.caliburnproject.org"
xmlns:properties="clr-namespace:ChocolateyGui.Common.Properties;assembly=ChocolateyGui.Common"
xmlns:system="clr-namespace:System;assembly=mscorlib"
xmlns:viewModels="clr-namespace:ChocolateyGui.Common.Windows.ViewModels;assembly=ChocolateyGui.Common.Windows"
xmlns:theming="clr-namespace:ChocolateyGui.Common.Windows.Theming"
mc:Ignorable="d"
Height="768" Width="1366"
GlowBrush="{DynamicResource MahApps.Brushes.Highlight}"
d:DataContext="{d:DesignInstance Type=viewModels:ShellViewModel}"
SaveWindowPosition="True"
TitleForeground="Transparent"
ShowDialogsOverTitleBar="False"
ShowIconOnTitleBar="False"
OverlayOpacity="0.3">
<Window.CommandBindings>
<CommandBinding
Command="NavigationCommands.GoToPage"
Executed="PerformGoToPage"
Expand Down Expand Up @@ -75,33 +74,38 @@
</Window.InputBindings>
<Window.Title>
<PriorityBinding>
<Binding Path="ActiveItem.DisplayName"></Binding>
<Binding Path="DisplayName"></Binding>
<Binding Path="ActiveItem.DisplayName" />
<Binding Path="DisplayName" />
</PriorityBinding>
</Window.Title>

<metro:MetroWindow.RightWindowCommands>
<metro:WindowCommands>
<metro:ToggleSwitch Margin="4 0"
Foreground="{DynamicResource MahApps.Brushes.IdealForeground}"
OnContent="{iconPacks:FontAwesome SunSolid}"
OffContent="{iconPacks:FontAwesome MoonSolid}"
IsOn="{Binding Source={x:Static theming:ThemeAssist.BundledTheme}, Path=IsLightTheme}"
OnCommand="{Binding Source={x:Static theming:ThemeAssist.BundledTheme}, Path=ToggleTheme}"
OffCommand="{Binding Source={x:Static theming:ThemeAssist.BundledTheme}, Path=ToggleTheme}" />
<mah:MetroWindow.RightWindowCommands>
<mah:WindowCommands>
<mah:WindowCommands.Resources>
<GridLength x:Key="ToggleSwitchPreContentMargin">0</GridLength>
<GridLength x:Key="ToggleSwitchPostContentMargin">0</GridLength>
<system:Double x:Key="ToggleSwitchOffStrokeThickness">1</system:Double>
<GridLength x:Key="ToggleSwitchContentSpaceMargin">0</GridLength>
</mah:WindowCommands.Resources>
<mah:ToggleSwitch Style="{StaticResource Chocolatey.Styles.ToggleSwitch.Theme}"
IsOn="{Binding Source={x:Static theming:ThemeAssist.BundledTheme}, Path=IsLightTheme}"
OnCommand="{Binding Source={x:Static theming:ThemeAssist.BundledTheme}, Path=ToggleTheme}"
OffCommand="{Binding Source={x:Static theming:ThemeAssist.BundledTheme}, Path=ToggleTheme}" />
<Button x:Name="ShowSettings"
Visibility="{Binding Path=CanShowSettings, Converter={StaticResource BooleanToVisibility}, ConverterParameter=False}"
IsEnabled="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type metro:MetroWindow}}, Path=IsAnyDialogOpen, Converter={StaticResource BooleanInverter}}"
Content="{x:Static properties:Resources.ShellView_ButtonSettings}" ClickMode="Release" cal:Message.Attach="ShowSettings()"/>
IsEnabled="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type mah:MetroWindow}}, Path=IsAnyDialogOpen, Converter={StaticResource BooleanInverter}}"
Content="{x:Static properties:Resources.ShellView_ButtonSettings}" ClickMode="Release"
cal:Message.Attach="ShowSettings()" />
<Button x:Name="ShowAbout"
IsEnabled="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type metro:MetroWindow}}, Path=IsAnyDialogOpen, Converter={StaticResource BooleanInverter}}"
Content="{x:Static properties:Resources.ShellView_ButtonAbout}" ClickMode="Release" cal:Message.Attach="ShowAbout()"/>
</metro:WindowCommands>
</metro:MetroWindow.RightWindowCommands>
IsEnabled="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type mah:MetroWindow}}, Path=IsAnyDialogOpen, Converter={StaticResource BooleanInverter}}"
Content="{x:Static properties:Resources.ShellView_ButtonAbout}" ClickMode="Release"
cal:Message.Attach="ShowAbout()" />
</mah:WindowCommands>
</mah:MetroWindow.RightWindowCommands>

<Border Background="{DynamicResource {x:Static theming:ChocolateyBrushes.BackgroundKey}}">
<Grid>
<ContentControl x:Name="ActiveItem" />
</Grid>
</Border>
</metro:MetroWindow>
</mah:MetroWindow>
3 changes: 2 additions & 1 deletion Source/ChocolateyGui.Common/ChocolateyGui.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@
<Compile Include="Controls\ObservableRingBufferCollection.cs" />
<Compile Include="Enums\ListViewMode.cs" />
<Compile Include="Enums\PackagesChangedEventType.cs" />
<Compile Include="Enums\ThemeMode.cs" />
<Compile Include="Hacks.cs" />
<Compile Include="Models\AppConfiguration.cs" />
<Compile Include="Models\ChocolateyAggregatedSources.cs" />
Expand Down Expand Up @@ -217,7 +218,7 @@
<Analyzer Include="..\packages\StyleCop.Analyzers.1.0.2\analyzers\dotnet\cs\StyleCop.Analyzers.dll" />
</ItemGroup>
<ItemGroup>
<SourceRoot Include="$(MSBuildThisFileDirectory)/../"/>
<SourceRoot Include="$(MSBuildThisFileDirectory)/../" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
16 changes: 16 additions & 0 deletions Source/ChocolateyGui.Common/Enums/ThemeMode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright company="Chocolatey" file="ThemeMode.cs">
// Copyright 2017 - Present Chocolatey Software, LLC
// Copyright 2014 - 2017 Rob Reynolds, the maintainers of Chocolatey, and RealDimensions Software, LLC
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace ChocolateyGui.Common.Enums
{
public enum ThemeMode
{
WindowsDefault,
Dark,
Light
}
}
3 changes: 2 additions & 1 deletion Source/ChocolateyGui/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Autofac;
using chocolatey;
using chocolatey.infrastructure.registration;
using ChocolateyGui.Common.Enums;
using ChocolateyGui.Common.Services;
using ChocolateyGui.Common.Windows;
using ChocolateyGui.Common.Windows.Theming;
Expand Down Expand Up @@ -103,7 +104,7 @@ protected override void OnStartup(StartupEventArgs e)
base.OnStartup(e);

ThemeAssist.BundledTheme.Generate("ChocolateyGui");
ThemeAssist.BundledTheme.SyncTheme();
ThemeAssist.BundledTheme.SyncTheme(ThemeMode.WindowsDefault); // todo Use the saved mode
}
}
}

0 comments on commit c0c69f2

Please sign in to comment.