Skip to content

Commit b12ddea

Browse files
authored
Merge pull request Ellendar#393 from initsu/colors-update-and-spoiler-warning
Change material colors & add red UI indicator if spoiler is on
2 parents 80f3d30 + 6b64b8c commit b12ddea

7 files changed

Lines changed: 119 additions & 31 deletions

File tree

CrossPlatformUI/App.axaml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@
77
xmlns:avalonia="clr-namespace:Material.Icons.Avalonia;assembly=Material.Icons.Avalonia"
88
xmlns:assists="clr-namespace:Material.Styles.Assists;assembly=Material.Styles"
99
xmlns:behaviors="clr-namespace:CrossPlatformUI.Behaviors"
10-
x:Class="CrossPlatformUI.App"
11-
RequestedThemeVariant="Default">
10+
x:Class="CrossPlatformUI.App">
1211
<!-- "Default" ThemeVariant follows system theme variant. "Dark" or "Light" are other available options. -->
1312
<Application.Styles>
14-
<themes:MaterialTheme PrimaryColor="DeepPurple" SecondaryColor="LightBlue" />
13+
<themes:MaterialTheme />
1514
<avalonia:MaterialIconStyles />
1615
<dialogHostAvalonia:DialogHostStyles />
1716

CrossPlatformUI/App.axaml.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@ public override void OnFrameworkInitializationCompleted()
139139
var state = !TransitionAssist.GetDisableTransitions(TopLevel!);
140140
TransitionAssist.SetDisableTransitions(TopLevel!, state);
141141

142+
ThemeHelper.SetTheme(main.RandomizerViewModel.ThemeVariantName);
143+
142144
base.OnFrameworkInitializationCompleted();
143145
}
144146

CrossPlatformUI/ThemeHelper.cs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using Avalonia.Media;
2+
using Material.Styles.Themes;
3+
4+
namespace CrossPlatformUI;
5+
6+
/// Material Avalonia's dynamic theming system leaves a lot to be desired.
7+
/// Probably base Avalonia is better for this & better documented.
8+
public class ThemeHelper
9+
{
10+
private static readonly Theme DarkMaterialTheme;
11+
private static readonly Theme LightMaterialTheme;
12+
13+
private static IBrush DarkNonStandardFlagEnabledBackground = SolidColorBrush.Parse("#80b10600");
14+
private static IBrush LightNonStandardFlagEnabledBackground = SolidColorBrush.Parse("#d2b30000");
15+
16+
static ThemeHelper() {
17+
DarkMaterialTheme = Theme.Create(Theme.Dark, Color.Parse("#ffbc33"), Color.Parse("#969696"));
18+
DarkMaterialTheme.Paper = Color.Parse("#1a1a1a");
19+
20+
LightMaterialTheme = Theme.Create(Theme.Light, Color.Parse("#002a88"), Color.Parse("#696969"));
21+
}
22+
23+
public static string SetTheme(string name)
24+
{
25+
var app = App.Current;
26+
if (app == null)
27+
{
28+
return "Light";
29+
}
30+
var themeBootstrap = app.LocateMaterialTheme<MaterialTheme>();
31+
switch (name)
32+
{
33+
case "Dark":
34+
themeBootstrap.CurrentTheme = DarkMaterialTheme;
35+
return "Dark";
36+
case "Light":
37+
default:
38+
themeBootstrap.CurrentTheme = LightMaterialTheme;
39+
return "Light";
40+
}
41+
}
42+
43+
public static IBrush GetFlagAlertBackgroundBrush(string theme, bool alert)
44+
{
45+
if (alert)
46+
{
47+
return theme == "Dark" ? DarkNonStandardFlagEnabledBackground : LightNonStandardFlagEnabledBackground;
48+
}
49+
else
50+
{
51+
return Brushes.Transparent;
52+
}
53+
}
54+
}

CrossPlatformUI/ViewModels/RandomizerViewModel.cs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
using System.Reactive.Subjects;
99
using System.Text.Json.Serialization;
1010
using Microsoft.Extensions.DependencyInjection;
11+
using Material.Colors;
1112
using Avalonia.Controls;
12-
using Avalonia.Styling;
13+
using Avalonia.Media;
14+
using Material.Styles.Themes;
1315
using ReactiveUI;
1416
using ReactiveUI.Validation.Extensions;
1517
using ReactiveUI.Validation.Helpers;
@@ -56,22 +58,17 @@ public string Seed
5658
}
5759
}
5860

59-
private string themeVariantName = "";
61+
[JsonIgnore]
62+
public BehaviorSubject<string> ThemeVariantSubject = new("");
6063
public string ThemeVariantName
6164
{
6265
get
6366
{
64-
return themeVariantName;
67+
return ThemeVariantSubject.Value;
6568
}
6669
set
6770
{
68-
App.Current!.RequestedThemeVariant = value switch
69-
{
70-
"Light" => ThemeVariant.Light,
71-
"Dark" => ThemeVariant.Dark,
72-
_ => ThemeVariant.Default
73-
};
74-
themeVariantName = value;
71+
ThemeVariantSubject.OnNext(ThemeHelper.SetTheme(value));
7572
}
7673
}
7774
private int currentTabIndex;
@@ -94,6 +91,7 @@ public RandomizerViewModel(MainViewModel main)
9491
BiomesViewModel = new(Main);
9592
PalacesViewModel = new(Main);
9693
ItemsViewModel = new(Main);
94+
HintsViewModel = new(Main, this);
9795
CustomizeViewModel = new(Main);
9896
Activator = new ViewModelActivator();
9997

@@ -132,15 +130,14 @@ public RandomizerViewModel(MainViewModel main)
132130

133131
ToggleTheme = ReactiveCommand.Create(() =>
134132
{
135-
if(App.Current!.ActualThemeVariant == ThemeVariant.Dark)
133+
if(ThemeVariantName == "Dark")
136134
{
137-
App.Current!.RequestedThemeVariant = ThemeVariant.Light;
135+
ThemeVariantName = "Light";
138136
}
139-
else if (App.Current!.ActualThemeVariant == ThemeVariant.Light)
137+
else
140138
{
141-
App.Current!.RequestedThemeVariant = ThemeVariant.Dark;
139+
ThemeVariantName = "Dark";
142140
}
143-
ThemeVariantName = App.Current.RequestedThemeVariant?.Key.ToString() ?? "Default";
144141
});
145142

146143
var seedValidObservable = this.WhenAnyValue(x => x.Main.Config.Seed, seed => !string.IsNullOrWhiteSpace(seed));
@@ -318,6 +315,7 @@ private void AddValidationRules()
318315
public BiomesViewModel BiomesViewModel { get; }
319316
public PalacesViewModel PalacesViewModel { get; }
320317
public ItemsViewModel ItemsViewModel { get; }
318+
public HintsViewModel HintsViewModel { get; }
321319
public CustomizeViewModel CustomizeViewModel { get; }
322320

323321
[JsonIgnore]
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System;
2+
using System.Diagnostics.CodeAnalysis;
3+
using System.Reactive.Disposables;
4+
using System.Reactive.Linq;
5+
using Avalonia.Media;
6+
using ReactiveUI;
7+
8+
namespace CrossPlatformUI.ViewModels.Tabs;
9+
10+
[RequiresUnreferencedCode("ReactiveUI uses reflection")]
11+
public class HintsViewModel : ReactiveObject, IActivatableViewModel
12+
{
13+
public ViewModelActivator Activator { get; }
14+
public MainViewModel Main { get; }
15+
public IObservable<IBrush> HeaderBackgroundObservable { get; }
16+
17+
public HintsViewModel(MainViewModel main, RandomizerViewModel randomizerViewModel)
18+
{
19+
Main = main;
20+
Activator = new();
21+
22+
var alertFlags = Main.FlagsChanged
23+
.Select(_ => Main.Config.GenerateSpoiler)
24+
.DistinctUntilChanged();
25+
26+
HeaderBackgroundObservable = randomizerViewModel.ThemeVariantSubject.CombineLatest(alertFlags)
27+
.Select(pair => ThemeHelper.GetFlagAlertBackgroundBrush(pair.First, pair.Second));
28+
29+
this.WhenActivated(OnActivate);
30+
}
31+
32+
internal void OnActivate(CompositeDisposable disposables)
33+
{
34+
}
35+
}

CrossPlatformUI/Views/RandomizerView.axaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,8 @@
202202
<TabItem Header="Drops">
203203
<tabs:DropsView DataContext="{Binding Main}"/>
204204
</TabItem>
205-
<TabItem Header="Hints">
206-
<tabs:HintsView DataContext="{Binding Main}"/>
205+
<TabItem Header="Hints" Background="{Binding HintsViewModel.HeaderBackgroundObservable^}">
206+
<tabs:HintsView DataContext="{Binding HintsViewModel}"/>
207207
</TabItem>
208208
<TabItem Header="Customize">
209209
<tabs:CustomizeView DataContext="{Binding CustomizeViewModel}" />

CrossPlatformUI/Views/Tabs/HintsView.axaml

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66
xmlns:rc="clr-namespace:Z2Randomizer.RandomizerCore;assembly=RandomizerCore"
77
xmlns:ui="clr-namespace:CrossPlatformUI"
88
xmlns:vm="clr-namespace:CrossPlatformUI.ViewModels"
9+
xmlns:vmt="clr-namespace:CrossPlatformUI.ViewModels.Tabs"
910
xmlns:lang="clr-namespace:Z2Randomizer.CrossPlatformUI.Lang"
1011
mc:Ignorable="d" d:DesignWidth="900" d:DesignHeight="600"
11-
x:DataType="vm:MainViewModel"
12-
x:Class="CrossPlatformUI.Views.Tabs.HintsView">
12+
x:Class="CrossPlatformUI.Views.Tabs.HintsView"
13+
x:DataType="vmt:HintsViewModel">
1314
<Grid ColumnDefinitions="*, *">
1415
<StackPanel Grid.Column="0">
1516
<HyperlinkButton
@@ -24,36 +25,35 @@
2425

2526
<CheckBox
2627
IsThreeState="True"
27-
IsChecked="{Binding Config.EnableHelpfulHints}"
28+
IsChecked="{Binding Main.Config.EnableHelpfulHints}"
2829
Content="Enable Helpful Hints"
2930
>
3031
<ToolTip.Tip><TextBlock Text="{x:Static lang:Resources.EnableHelpfulHintsToolTip}"/></ToolTip.Tip>
3132
</CheckBox>
3233
<CheckBox
3334
IsThreeState="True"
34-
IsChecked="{Binding Config.EnableSpellItemHints}"
35+
IsChecked="{Binding Main.Config.EnableSpellItemHints}"
3536
Content="Enable Spell Item Hints"
3637
>
3738
<ToolTip.Tip><TextBlock Text="{x:Static lang:Resources.EnableSpellItemHintsToolTip}"/></ToolTip.Tip>
3839
</CheckBox>
3940
<CheckBox
4041
IsThreeState="True"
41-
IsChecked="{Binding Config.EnableTownNameHints}"
42+
IsChecked="{Binding Main.Config.EnableTownNameHints}"
4243
Content="Enable Town Name Hints"
4344
>
4445
<ToolTip.Tip><TextBlock Text="{x:Static lang:Resources.EnableTownNameHintsToolTip}"/></ToolTip.Tip>
4546
</CheckBox>
4647
</StackPanel>
4748
<StackPanel Grid.Column="1">
48-
<CheckBox
49-
IsThreeState="False"
50-
IsChecked="{Binding Config.GenerateSpoiler}"
51-
Content="Generate Spoiler"
52-
>
49+
<CheckBox IsChecked="{Binding Main.Config.GenerateSpoiler}"
50+
Margin="14,4,4,4" Padding="0,4,0,4"
51+
Background="{Binding HeaderBackgroundObservable^}"
52+
Content="Generate Spoiler">
5353
<ToolTip.Tip><TextBlock Text="{x:Static lang:Resources.GenerateSpoilerToolTip}"/></ToolTip.Tip>
5454
</CheckBox>
5555
<CheckBox
56-
IsChecked="{Binding Config.RevealWalkthroughWalls}"
56+
IsChecked="{Binding Main.Config.RevealWalkthroughWalls}"
5757
Content="Reveal Walkthrough Walls"
5858
>
5959
<ToolTip.Tip><TextBlock Text="{x:Static lang:Resources.RevealWalkthroughWallsToolTip}"/></ToolTip.Tip>

0 commit comments

Comments
 (0)