Skip to content

Commit

Permalink
fix: 修复取色时,只点击后会崩溃的bug
Browse files Browse the repository at this point in the history
  • Loading branch information
SweetSmellFox committed Oct 12, 2024
1 parent efeb62a commit 3186d1b
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 27 deletions.
47 changes: 47 additions & 0 deletions Controls/PinButton.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace MFATools.Controls;

public class PinButton : Button
{
public static readonly DependencyProperty IsCheckedProperty =
DependencyProperty.Register(nameof(IsChecked), typeof(bool), typeof(PinButton),
new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
OnIsCheckedChanged));

public bool IsChecked
{
get => (bool)GetValue(IsCheckedProperty);
set => SetValue(IsCheckedProperty, value);
}

public event RoutedPropertyChangedEventHandler<bool> CheckedChanged;

private static void OnIsCheckedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is PinButton pinButton)
{
pinButton.OnCheckedChanged(pinButton.IsChecked, (bool)e.NewValue);
}
}

private void OnCheckedChanged(bool oldValue, bool newValue)
{
CheckedChanged?.Invoke(this, new RoutedPropertyChangedEventArgs<bool>(oldValue, newValue));
}

static PinButton()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(PinButton), new FrameworkPropertyMetadata(typeof(PinButton)));
}

// Constructor
public PinButton()
{
// Set the default content to 📌
Content = "📌";
Click += (_, _) => { IsChecked = !IsChecked; };
}
}
21 changes: 12 additions & 9 deletions Res/Style/MainStyle.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
xmlns:controls="clr-namespace:MFATools.Controls"
xmlns:hc="https://handyorg.github.io/handycontrol">
<!-- Start: Button | Minimize | Maximize | Close -->
<Style x:Key="IconButtonsStyle" TargetType="{x:Type Button}">
<Style x:Key="IconButtonsStyle" TargetType="{x:Type Button}">
<Setter Property="Background" Value="Transparent" />
<Setter Property="Cursor" Value="Hand" />
<Setter Property="BorderThickness" Value="0" />
Expand Down Expand Up @@ -58,17 +58,20 @@
</Setter.Value>
</Setter>
</Style>

<Style x:Key="PushpinButtonStyle" TargetType="{x:Type Button}" BasedOn="{StaticResource ButtonDefault}">

<Style TargetType="{x:Type controls:PinButton}" BasedOn="{StaticResource ButtonDefault}">
<Setter Property="Foreground" Value="{DynamicResource ActionIconColor}"/>
<Setter Property="Background" Value="Transparent" />
<Setter Property="Cursor" Value="Hand" />
<Setter Property="BorderThickness" Value="0" />
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="{DynamicResource GrayColor9}" />
<Setter Property="Background" Value="{DynamicResource GrayColor11}" />
</Trigger>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Foreground" Value="{DynamicResource PrimaryBrush}"/>
</Trigger>
</Style.Triggers>

</Style>
<!-- End: Button | Minimize | Maximize | Close -->

Expand Down Expand Up @@ -191,7 +194,7 @@
</Trigger>
</Style.Triggers>
</Style>

<Style x:Key="textBoxButton" TargetType="Button" BasedOn="{StaticResource ButtonIcon}">
<Setter Property="Cursor" Value="Arrow" />
<Setter Property="hc:VisualElement.HighlightBackground" Value="Transparent" />
Expand Down Expand Up @@ -242,12 +245,12 @@
</Setter.Value>
</Setter>
</Style>

<Style TargetType="controls:AttributeButton" x:Key="AttributeButtonStyle" BasedOn="{StaticResource ButtonDefault}">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="LightBlue"/>
<Setter Property="BorderBrush" Value="Blue"/>
<Setter Property="Background" Value="LightBlue" />
<Setter Property="BorderBrush" Value="Blue" />
</Trigger>
</Style.Triggers>
</Style>
Expand Down
3 changes: 2 additions & 1 deletion Res/Theme/DarkTheme.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@
<SolidColorBrush x:Key="GrayColor5" Color="LightGray" />
<SolidColorBrush x:Key="GrayColor9" Color="#e9e9e9" />
<SolidColorBrush x:Key="GrayColor10" Color="#FF212121" />

<SolidColorBrush x:Key="GrayColor11" Color="#646465" />

<SolidColorBrush x:Key="PurpleColor1" Color="#6F59D4" />

<SolidColorBrush x:Key="RedColor1" Color="#F72626" />
Expand Down
3 changes: 2 additions & 1 deletion Res/Theme/LightTheme.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@
<SolidColorBrush x:Key="GrayColor5" Color="LightGray" />
<SolidColorBrush x:Key="GrayColor9" Color="#e9e9e9" />
<SolidColorBrush x:Key="GrayColor10" Color="LightGray" />

<SolidColorBrush x:Key="GrayColor11" Color="#e9e9e9" />

<SolidColorBrush x:Key="PurpleColor1" Color="#6F59D4" />

<SolidColorBrush x:Key="RedColor1" Color="#F72626" />
Expand Down
4 changes: 2 additions & 2 deletions Views/ColorExtractionDialog.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,8 @@ private void SaveButton_Click(object sender, RoutedEventArgs e)

private void GetColorRange(double x, double y, double width, double height)
{
if (width < 1) width = 1;
if (height < 1) height = 1;
if (width < 1 || !double.IsNormal(width)) width = 1;
if (height < 1|| !double.IsNormal(height)) height = 1;
// 创建BitmapImage对象

if (image.Source is BitmapImage bitmapImage)
Expand Down
12 changes: 5 additions & 7 deletions Views/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,18 @@
<StackPanel Grid.Column="0"
HorizontalAlignment="Left" Orientation="Horizontal">
<TextBlock VerticalAlignment="Center" Margin="6 0 4 0" Text="{lex:Loc AppTitle}"
IsHitTestVisible="False"
Foreground="{DynamicResource PrimaryTextBrush}">

</TextBlock>
<TextBlock VerticalAlignment="Center" Foreground="{StaticResource GrayColor1}"
<TextBlock VerticalAlignment="Center" Foreground="{StaticResource GrayColor1}" IsHitTestVisible="False"
Margin="2 0 4 0" x:Name="version" Text="Debug">
</TextBlock>
</StackPanel>

<Button Grid.Column="1" x:Name="btnPin" Padding="0 -2 0 0" Width="42"
Content="📌" Click="ToggleWindowTopMost"
Style="{StaticResource PushpinButtonStyle}"
Foreground="{Binding WindowTopMostButtonForeground}">

</Button>
<controls:PinButton Grid.Column="1" x:Name="btnPin" Padding="0 -2 0 0" Width="42"
Content="📌" CheckedChanged="ToggleWindowTopMost">
</controls:PinButton>
</Grid>
</hc:Window.NonClientAreaContent>
<Grid>
Expand Down
9 changes: 2 additions & 7 deletions Views/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,9 @@ private bool InitializeData()
}


private void ToggleWindowTopMost(object sender, RoutedEventArgs e)
private void ToggleWindowTopMost(object sender, RoutedPropertyChangedEventArgs<bool> e)
{
if (Data == null) return;
Topmost = !Topmost;
if (Topmost)
Data.WindowTopMostButtonForeground = FindResource("PrimaryBrush") as Brush ?? Brushes.DarkGray;
else
Data.WindowTopMostButtonForeground = FindResource("ActionIconColor") as Brush ?? Brushes.DarkGray;
Topmost = e.NewValue;
}

private void TabControl_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
Expand Down

0 comments on commit 3186d1b

Please sign in to comment.