Skip to content

Commit

Permalink
初步完成主题功能
Browse files Browse the repository at this point in the history
  • Loading branch information
zsh2401 committed Mar 13, 2018
1 parent 1b60af8 commit 2c7962c
Show file tree
Hide file tree
Showing 13 changed files with 110 additions and 18 deletions.
3 changes: 3 additions & 0 deletions AutumnBox.GUI/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
<setting name="SkipVersion" serializeAs="String">
<value>0.0.1</value>
</setting>
<setting name="Theme" serializeAs="String">
<value>随机-Random</value>
</setting>
</AutumnBox.GUI.Properties.Settings>
</userSettings>
</configuration>
1 change: 1 addition & 0 deletions AutumnBox.GUI/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ await Task.Run(() =>
});
loadingWindowApi.SetProgress(60);
loadingWindowApi.SetTip(Resources["ldmsgStartDeviceMonitor"].ToString());
ThemeHelper.LoadFromSetting();
App.Current.MainWindow = new MainWindow();
DevicesMonitor.Begin();
loadingWindowApi.SetProgress(80);
Expand Down
73 changes: 64 additions & 9 deletions AutumnBox.GUI/Helper/ThemeHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
** date: 2018/3/13 17:56:01 (UTC +8:00)
** desc: ...
*************************************************/
using AutumnBox.GUI.Helper;
using AutumnBox.GUI.Properties;
using AutumnBox.Support.Log;
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -12,29 +15,81 @@

namespace AutumnBox.GUI.Helper
{
internal class Theme
internal abstract class Theme
{
public string Name { get { return Resource["ThemeName"].ToString(); } }
public ResourceDictionary Resource { get; }
public Theme(string fileName)
public abstract string Name { get; }
public abstract ResourceDictionary Resource { get; }
}
internal class RandomTheme : Theme
{
public override string Name => $"随机-Random";
private static readonly Random ran = new Random();
public override ResourceDictionary Resource => resouce;
private ResourceDictionary resouce;
public void Next()
{
var themes = ThemeHelper.Themes;
int ranIndex = ran.Next(1, themes.Length);
Logger.Debug(this,ranIndex);
resouce = themes[ranIndex].Resource;
}
}
internal class FileTheme : Theme
{
public override string Name { get { return Resource["ThemeName"].ToString(); } }
public override ResourceDictionary Resource => resouce;
private readonly ResourceDictionary resouce;
public FileTheme(string fileName)
{
Resource = new ResourceDictionary { Source = new Uri(ThemeHelper.Path + fileName) };
resouce = new ResourceDictionary() { Source = new Uri(ThemeHelper.Path + fileName) };
}
}
internal static class ThemeHelper
{
public static readonly Theme[] Themes;
public static Theme[] Themes => themes.ToArray();
private static readonly List<Theme> themes;
public const string Path = "pack://application:,,,/AutumnBox.GUI;component/Resources/Themes/";
static ThemeHelper()
{
Themes = new Theme[] {
new Theme("LightTheme.xaml"),
new Theme("NightTheme.xaml")
themes = new List<Theme>() {
new RandomTheme(),
new FileTheme("LightTheme.xaml"),
new FileTheme("NightTheme.xaml")
};
(themes[0] as RandomTheme).Next();
}
public static void LoadFromSetting()
{
ChangeTheme(Settings.Default.Theme);
}
static bool usingRandomTheme = false;
public static void ChangeTheme(Theme theme)
{
usingRandomTheme = theme is RandomTheme;
(theme as RandomTheme)?.Next();
App.Current.Resources.MergedDictionaries[1] = theme.Resource;
Settings.Default.Theme = theme.Name;
Logger.Info("ThemeHelper",$"Theme setting saved,value {theme.Name}");
}
public static void ChangeTheme(string themeName)
{

var theme = themes.Find((_theme) => { return _theme.Name == themeName; });
Logger.Debug("ThemeHelper",$"Theme finded {theme.Name}");
ChangeTheme(theme);
}
public static int GetCrtIndex()
{
if (usingRandomTheme) return 0;
else
{
int index;
index = themes.FindIndex((theme) =>
{
return theme.Name == App.Current.Resources["ThemeName"].ToString();
});
return index;
}
}
}
}
1 change: 1 addition & 0 deletions AutumnBox.GUI/I18N/Langs/en-US.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@
<s:String x:Key="TimeUsed">Time used</s:String>
<s:String x:Key="Transparency">Transparency</s:String>
<s:String x:Key="Language">Language</s:String>
<s:String x:Key="Theme">Theme</s:String>
<s:String x:Key="Notice">Notice</s:String>
<s:String x:Key="ckbSkipThisVersion">Skip this version</s:String>
<s:String x:Key="GetFail">Failed</s:String>
Expand Down
1 change: 1 addition & 0 deletions AutumnBox.GUI/I18N/Langs/zh-CN.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@
<s:String x:Key="TimeUsed">已用时间</s:String>
<s:String x:Key="Transparency">透明度</s:String>
<s:String x:Key="Language">语言</s:String>
<s:String x:Key="Theme">主题</s:String>
<s:String x:Key="Notice">提示</s:String>
<s:String x:Key="ckbSkipThisVersion">此版本不再提示</s:String>
<s:String x:Key="GetFail">获取失败</s:String>
Expand Down
12 changes: 12 additions & 0 deletions AutumnBox.GUI/Properties/Settings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions AutumnBox.GUI/Properties/Settings.settings
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,8 @@
<Setting Name="SkipVersion" Type="System.String" Scope="User">
<Value Profile="(Default)">0.0.1</Value>
</Setting>
<Setting Name="Theme" Type="System.String" Scope="User">
<Value Profile="(Default)">随机-Random</Value>
</Setting>
</Settings>
</SettingsFile>
2 changes: 1 addition & 1 deletion AutumnBox.GUI/Resources/Themes/LightTheme.xaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:s="clr-namespace:System;assembly=mscorlib">
<s:String x:Key="ThemeName">Light</s:String>
<s:String x:Key="ThemeName">明亮-Light</s:String>

<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="LightTheme_files/ColorAndBrushes.xaml"/>
Expand Down
2 changes: 1 addition & 1 deletion AutumnBox.GUI/Resources/Themes/NightTheme.xaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:s="clr-namespace:System;assembly=mscorlib">
<s:String x:Key="ThemeName">Basic</s:String>
<s:String x:Key="ThemeName">夜间-Night</s:String>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="NightTheme_files/ColorAndBrushes.xaml"/>

Expand Down
4 changes: 3 additions & 1 deletion AutumnBox.GUI/UI/CstPanels/SettingsPanel.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
<TextBlock Foreground="{DynamicResource ForegroundOnPrimaryBrushKey}" Style="{DynamicResource AtmbxTextBlockStyle}" TextAlignment="Right" Text="{DynamicResource Language}" Margin="10,38,792,507" RenderTransformOrigin="0.5,0.5" FontSize="16" />
<Button Foreground="{DynamicResource ForegroundOnPrimaryBrushKey}" Style="{DynamicResource AtmbxButtonStyle}" Content="{DynamicResource btnLaunchDebugWindow}" HorizontalAlignment="Left" Margin="153,364,0,0" VerticalAlignment="Top" Width="176" BorderBrush="{x:Null}" Background="{DynamicResource BtnBrush}" FontFamily="Microsoft YaHei UI Light" Click="Button_Click"/>
<CheckBox Foreground="{DynamicResource ForegroundOnPrimaryBrushKey}" x:Name="CKBShowDebugWindowOnNextLaunch" Content="{DynamicResource ckbShowDebugWindowOnNextLaunch}" HorizontalAlignment="Left" Margin="153,394,0,0" VerticalAlignment="Top" Width="291" />
<Button Click="Button_Click_1" Content="Button" HorizontalAlignment="Left" Margin="432,183,0,0" VerticalAlignment="Top" Width="75"/>

<ComboBox x:Name="CbBoxTheme" DisplayMemberPath="Name" HorizontalAlignment="Left" Margin="153,80,0,0" VerticalAlignment="Top" Width="225" Height="27"/>
<TextBlock Foreground="{DynamicResource ForegroundOnPrimaryBrushKey}" Style="{DynamicResource AtmbxTextBlockStyle}" TextAlignment="Right" Text="{DynamicResource Theme}" Margin="10,80,792,465" RenderTransformOrigin="0.5,0.5" FontSize="16" />
</Grid>
</fp:FastPanelChild>
10 changes: 10 additions & 0 deletions AutumnBox.GUI/UI/CstPanels/SettingsPanel.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,17 @@ public SettingsPanel()
CbBoxLanguage.ItemsSource = LanguageHelper.Langs;
CbBoxLanguage.SelectedIndex = LanguageHelper.FindIndex(App.Current.Resources["LanguageCode"].ToString());
CbBoxLanguage.SelectionChanged += CbBoxLanguage_SelectionChanged;

CbBoxTheme.ItemsSource = ThemeHelper.Themes;
CbBoxTheme.SelectedIndex = ThemeHelper.GetCrtIndex();
CbBoxTheme.SelectionChanged += CbBoxTheme_SelectionChanged;
}

private void CbBoxTheme_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ThemeHelper.ChangeTheme((Theme)CbBoxTheme.SelectedItem);
}

public override void OnPanelClosed()
{
base.OnPanelClosed();
Expand Down
12 changes: 8 additions & 4 deletions AutumnBox.GUI/UI/Fp/FastPanel.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@ public FastPanel(Panel father, FastPanelChild child)
{
BtnClose.Visibility = Visibility.Collapsed;
}
this.Background = child.PanelBackground;
this.BtnClose.Foreground = child.BtnCloseForeground;
if (child.PanelBackground != null)
this.Background = child.PanelBackground;
if (child.BtnCloseForeground != null)
this.BtnClose.Foreground = child.BtnCloseForeground;
child.OnPanelInited(new PanelArgs() { Height = this.Height, Width = this.Width });
}
private void InitAnimation()
Expand Down Expand Up @@ -91,7 +93,8 @@ public void Display(bool animation = true)
}
public void Hide()
{
if (Displayed) {
if (Displayed)
{
Displayed = false;
BeginAnimation(MarginProperty, HideAnimation);
}
Expand All @@ -103,7 +106,8 @@ public void Close()
Displayed = false;
BeginAnimation(MarginProperty, CloseAnimation);
}
else {
else
{
_Close();
}
}
Expand Down
4 changes: 2 additions & 2 deletions AutumnBox.GUI/UI/Fp/FastPanelChild.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public virtual void OnPanelDisplayed() { }
public virtual void OnPanelBtnCloseClicked(ref bool prevent) {}
public virtual void OnPanelClosed() { }
public virtual void OnPanelHide() { }
public virtual Brush PanelBackground => App.Current.Resources["PrimaryBrushKey"] as SolidColorBrush;
public virtual Brush BtnCloseForeground => App.Current.Resources["ForegroundOnPrimaryBrushKey"] as SolidColorBrush;
public virtual Brush PanelBackground => null;
public virtual Brush BtnCloseForeground => null;
public FastPanel Father { private get; set; }
protected void Dispay() {
Father.Display();
Expand Down

0 comments on commit 2c7962c

Please sign in to comment.