Skip to content

Commit 4d73dce

Browse files
committed
Fix red button hover state
1 parent 90cf085 commit 4d73dce

3 files changed

Lines changed: 154 additions & 2 deletions

File tree

src/SwitchifyPc.App/MainWindow.xaml

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,41 @@
6868
</Setter>
6969
</Style>
7070

71-
<Style x:Key="PrimaryButton" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
71+
<Style x:Key="PrimaryButton" TargetType="Button">
72+
<Setter Property="MinHeight" Value="40" />
73+
<Setter Property="Padding" Value="16,0" />
7274
<Setter Property="BorderBrush" Value="{StaticResource BrandPrimary}" />
75+
<Setter Property="BorderThickness" Value="1" />
7376
<Setter Property="Background" Value="{StaticResource BrandPrimary}" />
7477
<Setter Property="Foreground" Value="White" />
78+
<Setter Property="FontWeight" Value="Bold" />
79+
<Setter Property="Cursor" Value="Hand" />
80+
<Setter Property="Template">
81+
<Setter.Value>
82+
<ControlTemplate TargetType="Button">
83+
<Border x:Name="Root"
84+
MinHeight="{TemplateBinding MinHeight}"
85+
Padding="{TemplateBinding Padding}"
86+
Background="{TemplateBinding Background}"
87+
BorderBrush="{TemplateBinding BorderBrush}"
88+
BorderThickness="{TemplateBinding BorderThickness}"
89+
CornerRadius="10">
90+
<ContentPresenter HorizontalAlignment="Center"
91+
VerticalAlignment="Center"
92+
RecognizesAccessKey="True" />
93+
</Border>
94+
<ControlTemplate.Triggers>
95+
<Trigger Property="IsMouseOver" Value="True">
96+
<Setter TargetName="Root" Property="Background" Value="{StaticResource BrandPrimaryHover}" />
97+
<Setter TargetName="Root" Property="BorderBrush" Value="{StaticResource BrandPrimaryHover}" />
98+
</Trigger>
99+
<Trigger Property="IsEnabled" Value="False">
100+
<Setter TargetName="Root" Property="Opacity" Value="0.55" />
101+
</Trigger>
102+
</ControlTemplate.Triggers>
103+
</ControlTemplate>
104+
</Setter.Value>
105+
</Setter>
75106
</Style>
76107

77108
<Style x:Key="MainCard" TargetType="Border">

src/SwitchifyPc.App/SettingsWindow.xaml

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
2121

2222
<SolidColorBrush x:Key="BrandPrimary" Color="#D32F2F" />
23+
<SolidColorBrush x:Key="BrandPrimaryHover" Color="#B3261E" />
2324
<SolidColorBrush x:Key="Surface" Color="#FFFFFF" />
2425
<SolidColorBrush x:Key="SurfaceMuted" Color="#F3F2F6" />
2526
<SolidColorBrush x:Key="Text" Color="#1C1B1F" />
@@ -63,10 +64,41 @@
6364
</Setter>
6465
</Style>
6566

66-
<Style x:Key="PrimaryButton" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
67+
<Style x:Key="PrimaryButton" TargetType="Button">
68+
<Setter Property="MinHeight" Value="38" />
69+
<Setter Property="Padding" Value="14,0" />
6770
<Setter Property="BorderBrush" Value="{StaticResource BrandPrimary}" />
71+
<Setter Property="BorderThickness" Value="1" />
6872
<Setter Property="Background" Value="{StaticResource BrandPrimary}" />
6973
<Setter Property="Foreground" Value="White" />
74+
<Setter Property="FontWeight" Value="Bold" />
75+
<Setter Property="Cursor" Value="Hand" />
76+
<Setter Property="Template">
77+
<Setter.Value>
78+
<ControlTemplate TargetType="Button">
79+
<Border x:Name="Root"
80+
MinHeight="{TemplateBinding MinHeight}"
81+
Padding="{TemplateBinding Padding}"
82+
Background="{TemplateBinding Background}"
83+
BorderBrush="{TemplateBinding BorderBrush}"
84+
BorderThickness="{TemplateBinding BorderThickness}"
85+
CornerRadius="10">
86+
<ContentPresenter HorizontalAlignment="Center"
87+
VerticalAlignment="Center"
88+
RecognizesAccessKey="True" />
89+
</Border>
90+
<ControlTemplate.Triggers>
91+
<Trigger Property="IsMouseOver" Value="True">
92+
<Setter TargetName="Root" Property="Background" Value="{StaticResource BrandPrimaryHover}" />
93+
<Setter TargetName="Root" Property="BorderBrush" Value="{StaticResource BrandPrimaryHover}" />
94+
</Trigger>
95+
<Trigger Property="IsEnabled" Value="False">
96+
<Setter TargetName="Root" Property="Opacity" Value="0.55" />
97+
</Trigger>
98+
</ControlTemplate.Triggers>
99+
</ControlTemplate>
100+
</Setter.Value>
101+
</Setter>
70102
</Style>
71103

72104
<Style x:Key="PanelBorder" TargetType="Border">
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
using System.Threading;
2+
using System.Windows;
3+
using System.Windows.Media;
4+
using SwitchifyPc.App;
5+
using WpfBorder = System.Windows.Controls.Border;
6+
using WpfControl = System.Windows.Controls.Control;
7+
using WpfControlTemplate = System.Windows.Controls.ControlTemplate;
8+
9+
namespace SwitchifyPc.Tests;
10+
11+
public sealed class ButtonStyleTests
12+
{
13+
[Fact]
14+
public void MainWindowPrimaryButtonStyleUsesRedHoverBrush()
15+
{
16+
RunOnSta(() =>
17+
{
18+
MainWindow window = new();
19+
try
20+
{
21+
AssertPrimaryButtonStyleUsesRedHoverBrush(window);
22+
}
23+
finally
24+
{
25+
window.Close();
26+
}
27+
});
28+
}
29+
30+
[Fact]
31+
public void SettingsWindowPrimaryButtonStyleUsesRedHoverBrush()
32+
{
33+
RunOnSta(() =>
34+
{
35+
SettingsWindow window = new();
36+
try
37+
{
38+
AssertPrimaryButtonStyleUsesRedHoverBrush(window);
39+
}
40+
finally
41+
{
42+
window.Close();
43+
}
44+
});
45+
}
46+
47+
private static void AssertPrimaryButtonStyleUsesRedHoverBrush(FrameworkElement element)
48+
{
49+
Style style = Assert.IsType<Style>(element.FindResource("PrimaryButton"));
50+
Assert.Null(style.BasedOn);
51+
52+
SolidColorBrush hoverBrush = Assert.IsType<SolidColorBrush>(element.FindResource("BrandPrimaryHover"));
53+
WpfControlTemplate template = Assert.IsType<WpfControlTemplate>(
54+
Assert.Single(style.Setters.OfType<Setter>(), setter => setter.Property == WpfControl.TemplateProperty).Value);
55+
Trigger hoverTrigger = Assert.IsType<Trigger>(
56+
Assert.Single(template.Triggers.OfType<Trigger>(), trigger => trigger.Property == UIElement.IsMouseOverProperty));
57+
58+
Assert.Contains(hoverTrigger.Setters.OfType<Setter>(), setter =>
59+
setter.TargetName == "Root" &&
60+
setter.Property == WpfBorder.BackgroundProperty &&
61+
ReferenceEquals(setter.Value, hoverBrush));
62+
Assert.Contains(hoverTrigger.Setters.OfType<Setter>(), setter =>
63+
setter.TargetName == "Root" &&
64+
setter.Property == WpfBorder.BorderBrushProperty &&
65+
ReferenceEquals(setter.Value, hoverBrush));
66+
}
67+
68+
private static void RunOnSta(Action action)
69+
{
70+
Exception? exception = null;
71+
Thread thread = new(() =>
72+
{
73+
try
74+
{
75+
action();
76+
}
77+
catch (Exception error)
78+
{
79+
exception = error;
80+
}
81+
});
82+
83+
thread.SetApartmentState(ApartmentState.STA);
84+
thread.Start();
85+
thread.Join();
86+
87+
if (exception is not null) throw exception;
88+
}
89+
}

0 commit comments

Comments
 (0)