Skip to content

Commit

Permalink
Make the theme changer use a color picker.
Browse files Browse the repository at this point in the history
  • Loading branch information
CarbonNeuron committed Dec 16, 2020
1 parent 2016890 commit 5b1d308
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 32 deletions.
30 changes: 6 additions & 24 deletions AUCapture-WPF/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
<mah:MetroWindow.Flyouts>
<mah:FlyoutsControl>
<mah:Flyout x:Name="SettingsFlyout" Header="Settings" Position="Right"
Width="{Binding ActualWidth,ElementName=MainGrid, Converter={dialogs:MathMultiplyConverter}, ConverterParameter=0.51}"
Width="{Binding ActualWidth,ElementName=MainGrid, Converter={dialogs:MathMultiplyConverter}, ConverterParameter=0.75}"
Theme="Adapt"
IsAutoCloseEnabled="False" AnimateOpacity="True" ExternalCloseButton="Left">
<Grid Margin="10">
Expand Down Expand Up @@ -96,7 +96,7 @@
</ScrollViewer>
</mah:MetroTabItem>
<mah:MetroTabItem Header="Theme">
<ScrollViewer VerticalScrollBarVisibility="Auto" CanContentScroll="True">
<ScrollViewer VerticalScrollBarVisibility="Auto">
<StackPanel Orientation="Vertical">
<mah:ToggleSwitch x:Name="Darkmode_toggleswitch" Header="Dark mode"
IsOn="{Binding Settings.DarkMode, Mode=TwoWay}"
Expand All @@ -107,28 +107,10 @@
Width="{Binding ActualWidth,ElementName=MainGrid, Converter={dialogs:MathMultiplyConverter}, ConverterParameter=0.25}"
Value="{Binding Settings.fontSize, Mode=TwoWay}" />
</mah:MetroHeader>
<mah:MetroHeader Header="Accent">
<ListBox AlternationCount="2" x:Name="AccentSelector" SelectionChanged="Selector_OnSelectionChanged" BorderThickness="1" Style="{StaticResource MahApps.Styles.ListBox.Virtualized}" ItemsSource="{Binding AccentColors, Mode=OneWay}" MaxHeight="100" HorizontalContentAlignment="Stretch">
<ListBox.ItemContainerStyle>
<Style BasedOn="{StaticResource MahApps.Styles.ListBoxItem}" TargetType="{x:Type ListBoxItem}">
<Setter Property="HorizontalContentAlignment" Value="Left"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Style.Triggers>
<Trigger Property="ItemsControl.AlternationIndex" Value="0">
<Setter Property="Background" Value="{DynamicResource MahApps.Brushes.Gray10}" />
</Trigger>
</Style.Triggers>
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" CanVerticallyScroll="True">
<Ellipse Width="16" Height="16" Fill="{Binding ColorBrush, Mode=OneWay}" />
<TextBlock Text="{Binding Name, Mode=OneWay}"></TextBlock>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<mah:MetroHeader Header="Color Picker">
<mah:ColorPicker x:Name="AccentColorPicker" SelectedColorChanged="AccentColorPicker_OnSelectedColorChanged" mah:TextBoxHelper.AutoWatermark="True"
mah:TextBoxHelper.UseFloatingWatermark="True"
mah:TextBoxHelper.Watermark="Select a color"/>
</mah:MetroHeader>

</StackPanel>
Expand Down
66 changes: 58 additions & 8 deletions AUCapture-WPF/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Threading;
using Color = System.Drawing.Color;
Expand All @@ -39,6 +40,7 @@ public partial class MainWindow
private readonly bool connected;
private readonly object locker = new object();
private readonly Queue<string> DeadMessages = new Queue<string>();
private Task ThemeGeneration;

public MainWindow()
{
Expand Down Expand Up @@ -95,15 +97,34 @@ public MainWindow()

Version version = new Version(context.Version);
Version latestVersion = new Version(context.LatestVersion);
System.Windows.Media.Color savedColor;
try
{
AccentSelector.SelectedItem = context.AccentColors.Single(x => x.Name == context.Settings.SelectedAccent);
}
catch (System.InvalidOperationException)
{
AccentSelector.SelectedItem = null;
savedColor = JsonConvert.DeserializeObject<System.Windows.Media.Color>(context.Settings.SelectedAccent);
AccentColorPicker.SelectedColor = savedColor;
string BaseColor;
if (context.Settings.DarkMode)
{
BaseColor = ThemeManager.BaseColorDark;
}
else
{
BaseColor = ThemeManager.BaseColorLight;
}
Theme newTheme = new Theme(name: "CustomTheme",
displayName: "CustomTheme",
baseColorScheme: BaseColor,
colorScheme: "CustomAccent",
primaryAccentColor: savedColor,
showcaseBrush: new SolidColorBrush(savedColor),
isRuntimeGenerated: true,
isHighContrast: false);

ThemeManager.Current.ChangeTheme(this, newTheme);
}

catch (Exception e)
{ }

#if PUBLISH
if (latestVersion.CompareTo(version) > 0)
this.ShowMessageAsync("Caution",
Expand Down Expand Up @@ -541,8 +562,37 @@ private void APIServerToggleSwitch_Toggled(object sender, RoutedEventArgs e)

private void Selector_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
context.Settings.SelectedAccent = ((UserDataContext.AccentColorMenuData)e.AddedItems[0]).Name;
ThemeManager.Current.ChangeThemeColorScheme(this, ((UserDataContext.AccentColorMenuData)e.AddedItems[0]).Name);

}

private async void AccentColorPicker_OnSelectedColorChanged(object sender, RoutedPropertyChangedEventArgs<System.Windows.Media.Color?> e)
{
if (e.NewValue.HasValue)
{

string BaseColor;
if (context.Settings.DarkMode)
{
BaseColor = ThemeManager.BaseColorDark;
}
else
{
BaseColor = ThemeManager.BaseColorLight;
}

context.Settings.SelectedAccent = JsonConvert.SerializeObject(e.NewValue.Value);

Theme newTheme = new Theme(name: "CustomTheme",
displayName: "CustomTheme",
baseColorScheme: BaseColor,
colorScheme: "CustomAccent",
primaryAccentColor: e.NewValue.Value,
showcaseBrush: new SolidColorBrush(e.NewValue.Value),
isRuntimeGenerated: true,
isHighContrast: false);

ThemeManager.Current.ChangeTheme(this, newTheme);
}
}
}
}

0 comments on commit 5b1d308

Please sign in to comment.