Skip to content

Commit

Permalink
UI完善和主题切换功能
Browse files Browse the repository at this point in the history
  • Loading branch information
laggage committed Jan 30, 2020
1 parent 60736c0 commit 2607bad
Show file tree
Hide file tree
Showing 12 changed files with 301 additions and 56 deletions.
5 changes: 4 additions & 1 deletion WinPowerHelper.Wpf/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:viewmodels="clr-namespace:WinPowerHelper.Wpf.ViewModels"
StartupUri="MainWindow.xaml">
StartupUri="MainWindow.xaml"
xmlns:system="clr-namespace:System;assembly=System.Runtime">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
Expand All @@ -11,6 +12,8 @@
</ResourceDictionary.MergedDictionaries>

<viewmodels:MainViewModel x:Key="MainViewModel"/>
<system:Double x:Key="Window.TitleBar.Height">32</system:Double>
<system:Double x:Key="FontSize.WindowTitle" >14</system:Double>
</ResourceDictionary>
</Application.Resources>
</Application>
29 changes: 20 additions & 9 deletions WinPowerHelper.Wpf/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,38 @@
xmlns:extensions="clr-namespace:WinPowerHelper.Wpf.Extensions"
mc:Ignorable="d"
DataContext="{StaticResource MainViewModel}"
Title="{Binding AppTitle}"
MinHeight="240" MinWidth="420" Width="500" Height="250"
MaxHeight="400" MaxWidth="680" ResizeMode="CanMinimize">
Title="{Binding AppTitle}" WindowStyle="None"
MinHeight="240" MinWidth="420" Width="500" Height="200"
BorderThickness="1"
MaxHeight="400" MaxWidth="680" ResizeMode="CanMinimize" BorderBrush="{DynamicResource Brushes.Accent}">
<Window.Resources>
<extensions:EnumDescriptionConverter x:Key="EnumDescriptionConverter" />
<extensions:BoolNotConverter x:Key="BoolNotConverter" />
<extensions:BoolToStringConverter x:Key="BoolToStringConverter"/>
<Style x:Key="WindowStyle1" TargetType="{x:Type Window}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Window}">
<ContentPresenter />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<views:WindowTitleBar x:Name="TitleBar"/>
<views:TimerControl x:Name="timer"
Grid.Row="1"
Padding="12 3"
Interval="{Binding Path=Interval, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></views:TimerControl>
<Border Grid.Row="1">
<Border Grid.Row="2">
<WrapPanel HorizontalAlignment="Center">
<WrapPanel.Resources>
<Style TargetType="{x:Type Button}" BasedOn="{StaticResource Button.Square.Accent}">
Expand All @@ -47,7 +59,7 @@
Command="{Binding SetIntervalToZeroCmd}">set to zero</Button>
</WrapPanel>
</Border>
<Border Grid.Row="2" Padding="12 5">
<Border Grid.Row="3" Padding="12 5">
<Grid >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto"/>
Expand All @@ -65,8 +77,7 @@
ItemsSource="{Binding PowerOptions}"
Style="{StaticResource ComboBox}"
SelectedValue="{Binding SelectedPowerOption, Mode=OneWayToSource, UpdateSourceTrigger=PropertyChanged, FallbackValue=Sleep}"
IsEditable="False"
ItemContainerStyle="{StaticResource ComboBoxItem.Accent}">
IsEditable="False" >
<ComboBox.ItemTemplate>
<DataTemplate DataType="{x:Type models:PowerOptions}" >
<TextBlock Text="{Binding Path=., Converter={StaticResource EnumDescriptionConverter}}"/>
Expand Down
77 changes: 40 additions & 37 deletions WinPowerHelper.Wpf/Styles/Controls.ComboBox.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:theme="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero2">


<Style x:Key="ComboBoxToggleButton" TargetType="{x:Type ToggleButton}">
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="IsTabStop" Value="false"/>
Expand Down Expand Up @@ -137,20 +137,57 @@
</Setter>
</Style>



<Style x:Key="ComboBoxItem.Accent" TargetType="ComboBoxItem">
<Setter Property="OverridesDefaultStyle" Value="True"/>
<Setter Property="Padding" Value="{Binding Path=Padding, RelativeSource={RelativeSource AncestorType=ComboBox}}"/>
<Setter Property="Background"
Value="{DynamicResource Brushes.White}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ComboBoxItem}">
<Border BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Background="{TemplateBinding Background}">
<Grid >
<ContentControl Margin="{TemplateBinding Padding}"
ContentTemplate="{TemplateBinding ContentTemplate}"
Content="{TemplateBinding Content}"/>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background"
Value="{DynamicResource Brushes.Accent3}"/>
</Trigger>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background"
Value="{DynamicResource Brushes.Accent}"/>
<Setter Property="Foreground"
Value="{DynamicResource Brushes.White}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

<Style x:Key="ComboBox" TargetType="{x:Type ComboBox}">
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="BorderBrush" Value="{DynamicResource Brushes.Black}"/>
<Setter Property="Background" Value="{DynamicResource Brushes.White}"/>
<Setter Property="RenderOptions.ClearTypeHint" Value="Enabled"/>
<Setter Property="SnapsToDevicePixels" Value="True"/>
<Setter Property="Padding" Value="6 8"/>
<Setter Property="ItemContainerStyle" Value="{StaticResource ComboBoxItem.Accent}"/>
<Setter Property="VerticalContentAlignment" Value="Center" />
<!--<Setter Property="OverridesDefaultStyle" Value="True"/>-->
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ComboBox}">
<Grid x:Name="templateRoot"
SnapsToDevicePixels="true">
SnapsToDevicePixels="true">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition MinWidth="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}" Width="0"/>
Expand Down Expand Up @@ -205,7 +242,7 @@
IsHitTestVisible="false"
Margin="{TemplateBinding Padding}"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="HasDropShadow" SourceName="PART_Popup" Value="true">
Expand All @@ -232,38 +269,4 @@
</Setter.Value>
</Setter>
</Style>

<Style x:Key="ComboBoxItem.Accent" TargetType="ComboBoxItem">
<Setter Property="OverridesDefaultStyle" Value="True"/>
<Setter Property="Padding" Value="{Binding Path=Padding, RelativeSource={RelativeSource AncestorType=ComboBox}}"/>
<Setter Property="Background"
Value="{DynamicResource Brushes.White}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ComboBoxItem}">
<Border BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Background="{TemplateBinding Background}">
<Grid >
<ContentControl Margin="{TemplateBinding Padding}"
ContentTemplate="{TemplateBinding ContentTemplate}"
Content="{TemplateBinding Content}"/>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background"
Value="{DynamicResource Brushes.Accent3}"/>
</Trigger>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background"
Value="{DynamicResource Brushes.Accent}"/>
<Setter Property="Foreground"
Value="{DynamicResource Brushes.White}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
2 changes: 1 addition & 1 deletion WinPowerHelper.Wpf/Styles/Themes/Light.Blue.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
xmlns:system="clr-namespace:System;assembly=System.Runtime"
mc:Ignorable="options">
<system:String x:Key="Theme.Name">Light.Blue</system:String>
<system:String x:Key="Them.DisplayName">Blue (Light)</system:String>
<system:String x:Key="Theme.DisplayName">Blue (Light)</system:String>
<system:String x:Key="Theme.BaseColorScheme">Light</system:String>
<system:String x:Key="Theme.ColorScheme">Blue</system:String>
<SolidColorBrush x:Key="Theme.ShowcaseBrush" Color="#FF0078D7" options:Freeze="True" />
Expand Down
2 changes: 1 addition & 1 deletion WinPowerHelper.Wpf/Styles/Themes/Light.Pink.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
mc:Ignorable="options">

<system:String x:Key="Theme.Name">Light.Pink</system:String>
<system:String x:Key="Them.DisplayName">Pink (Light)</system:String>
<system:String x:Key="Theme.DisplayName">Pink (Light)</system:String>
<system:String x:Key="Theme.BaseColorScheme">Light</system:String>
<system:String x:Key="Theme.ColorScheme">Pink</system:String>
<SolidColorBrush x:Key="Theme.ShowcaseBrush" Color="#FFF472D0" options:Freeze="True" />
Expand Down
2 changes: 1 addition & 1 deletion WinPowerHelper.Wpf/ThemeManager/Theme.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public Theme(ResourceDictionary resources)
public const string ThemeNameKey = "Theme.Name";
public const string ThemeDisplayNameKey = "Theme.DisplayName";
public const string ThemeBaseColorSchemeKey = "Theme.BaseColorScheme";
public const string ThemeColorSchemeKey = "Theme.ColorSchema";
public const string ThemeColorSchemeKey = "Theme.ColorScheme";
public const string ThemeShowcaseBrushKey = "Theme.ShowcaseBrush";

public string Name { get; set; }
Expand Down
53 changes: 47 additions & 6 deletions WinPowerHelper.Wpf/ThemeManager/ThemeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Resources;
using System.Text;
using System.Windows;

public static class ThemeManager
Expand All @@ -28,11 +28,18 @@ private set
}
}

/// <summary>
/// 确保 <see cref="Themes"/> 存在数据
/// </summary>
/// <note>
/// 这个方法中不能调用属性 <see cref="Themes"/>, 否则会造成 "循环引用"
/// 这个方法会被一直递归, 直到堆栈溢出
/// </note>
private static void EnsureThemes()
{
if (Themes.Count > 0) return;
if (_themes.Count > 0) return;
var assembly = typeof(ThemeManager).Assembly;
var assemblyName = assembly.GetName();
var assemblyName = assembly.GetName().Name;
var resourceNames = assembly.GetManifestResourceNames();

foreach (var resourceName in resourceNames)
Expand All @@ -44,13 +51,47 @@ private static void EnsureThemes()
{
foreach (DictionaryEntry entry in reader)
{
throw new NotImplementedException();
string stringKey = entry.Key as string;
if (stringKey == null ||
!stringKey.Contains("/themes/", StringComparison.OrdinalIgnoreCase) ||
!stringKey.EndsWith(".baml", StringComparison.OrdinalIgnoreCase) ||
stringKey.EndsWith("generic.baml", StringComparison.OrdinalIgnoreCase)) continue;

var resourceDictionary = new ResourceDictionary()
{
Source = new Uri(
$"pack://application:,,,/{assemblyName};component/{stringKey.Replace("baml", "xaml")}")
};

if (resourceDictionary != null &&
resourceDictionary.MergedDictionaries.Count == 0)
_themes.Add(new Theme(resourceDictionary));

}
}


}
}

public static void ChangeThemeColorScheme(ResourceDictionary resources, string newColorScheme)
{
var oldThemeResource = resources.MergedDictionaries.FirstOrDefault(
x => x.Source.LocalPath.Contains(
"light.", StringComparison.OrdinalIgnoreCase));

if (oldThemeResource[Theme.ThemeColorSchemeKey] as string == newColorScheme) return;

var newTheme = Themes.FirstOrDefault(x =>
x.ColorScheme.Equals(newColorScheme, StringComparison.OrdinalIgnoreCase));

string assemblyName = typeof(ThemeManager).Assembly.GetName().Name;
var newThemeResource = new ResourceDictionary
{
Source = new Uri(
$"pack://application:,,,/{assemblyName};component/Styles/Themes/Light.{newColorScheme}.xaml")
};
resources.MergedDictionaries.Add(newThemeResource);
if (oldThemeResource != null)
resources.MergedDictionaries.Remove(oldThemeResource);
}
}
}
20 changes: 20 additions & 0 deletions WinPowerHelper.Wpf/ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
using WinPowerHelper.Core.Models;
using WinPowerHelper.Core.Services;
using WinPowerHelper.Wpf.Views;
using ThemeManager;
using System.Windows;
using System.Linq;

internal class MainViewModel : BindableBase
{
Expand All @@ -24,6 +27,11 @@ public MainViewModel()
Core.Models.PowerOptions.Shutdown,
Core.Models.PowerOptions.Restart,
};
Themes = ThemeManager.Themes;
SelectedTheme =
Themes.FirstOrDefault(
x => x.ColorScheme.Contains(
"blue", StringComparison.OrdinalIgnoreCase));
}

public static string AppTitle => "Windows定时关机助手";
Expand All @@ -35,9 +43,21 @@ public TimeSpan Interval
set => SetProperty(ref _interval, value);
}

public IList<Theme> Themes { get; set; }
public List<PowerOptions> PowerOptions { get; set; }
public PowerOptions SelectedPowerOption { get; set; }

private Theme _selectedTheme;
public Theme SelectedTheme
{
get => _selectedTheme;
set
{
SetProperty(ref _selectedTheme, value);
ThemeManager.ChangeThemeColorScheme(Application.Current.Resources, value.ColorScheme);
}
}

public ICommand SetIntervalCmd { get; private set; }
public ICommand SetIntervalToZeroCmd { get; private set; }
public ICommand BeginOrStopTimingCmd { get; private set; }
Expand Down
Loading

0 comments on commit 2607bad

Please sign in to comment.