diff --git a/src/Avalonia.Controls.ColorPicker/ColorView/AlphaComponentPosition.cs b/src/Avalonia.Controls.ColorPicker/AlphaComponentPosition.cs similarity index 100% rename from src/Avalonia.Controls.ColorPicker/ColorView/AlphaComponentPosition.cs rename to src/Avalonia.Controls.ColorPicker/AlphaComponentPosition.cs diff --git a/src/Avalonia.Controls.ColorPicker/ColorView/ColorView.Properties.cs b/src/Avalonia.Controls.ColorPicker/ColorView/ColorView.Properties.cs index e334a1d323..532e87a9fc 100644 --- a/src/Avalonia.Controls.ColorPicker/ColorView/ColorView.Properties.cs +++ b/src/Avalonia.Controls.ColorPicker/ColorView/ColorView.Properties.cs @@ -48,7 +48,7 @@ namespace Avalonia.Controls public static readonly StyledProperty HexInputAlphaPositionProperty = AvaloniaProperty.Register( nameof(HexInputAlphaPosition), - AlphaComponentPosition.Trailing); // Match CSS (and default slider order) instead of XAML/WinUI + AlphaComponentPosition.Leading); // By default match XAML and the WinUI control /// /// Defines the property. diff --git a/src/Avalonia.Controls.ColorPicker/ColorView/ColorView.cs b/src/Avalonia.Controls.ColorPicker/ColorView/ColorView.cs index 274e7f5851..7674b74b6a 100644 --- a/src/Avalonia.Controls.ColorPicker/ColorView/ColorView.cs +++ b/src/Avalonia.Controls.ColorPicker/ColorView/ColorView.cs @@ -61,7 +61,11 @@ namespace Avalonia.Controls { if (_hexTextBox != null) { - _hexTextBox.Text = ColorToHexConverter.ToHexString(Color, HexInputAlphaPosition); + _hexTextBox.Text = ColorToHexConverter.ToHexString( + Color, + HexInputAlphaPosition, + includeAlpha: (IsAlphaEnabled && IsAlphaVisible), + includeSymbol: false); } } diff --git a/src/Avalonia.Controls.ColorPicker/Converters/ColorToHexConverter.cs b/src/Avalonia.Controls.ColorPicker/Converters/ColorToHexConverter.cs index 8798f874f4..8257499d70 100644 --- a/src/Avalonia.Controls.ColorPicker/Converters/ColorToHexConverter.cs +++ b/src/Avalonia.Controls.ColorPicker/Converters/ColorToHexConverter.cs @@ -11,6 +11,18 @@ namespace Avalonia.Controls.Converters /// public class ColorToHexConverter : IValueConverter { + /// + /// Gets or sets a value indicating whether the alpha component is visible in the Hex formatted text. + /// + /// + /// When hidden the existing alpha component value is maintained. Also when hidden the user is still + /// able to input an 8-digit number with alpha. Alpha will be processed but then removed when displayed. + /// + /// Because this property only controls whether alpha is displayed (and it is still processed regardless) + /// it is termed 'Visible' instead of 'Enabled'. + /// + public bool IsAlphaVisible { get; set; } = true; + /// /// Gets or sets the position of a color's alpha component relative to all other components. /// @@ -48,7 +60,7 @@ namespace Avalonia.Controls.Converters return AvaloniaProperty.UnsetValue; } - return ToHexString(color, AlphaPosition, includeSymbol); + return ToHexString(color, AlphaPosition, IsAlphaVisible, includeSymbol); } /// @@ -67,26 +79,40 @@ namespace Avalonia.Controls.Converters /// /// The color to represent as a hex value string. /// The output position of the alpha component. + /// Whether the alpha component will be included in the hex string. /// Whether the hex symbol '#' will be added. /// The input color converted to its hex value string. public static string ToHexString( Color color, AlphaComponentPosition alphaPosition, + bool includeAlpha = true, bool includeSymbol = false) { uint intColor; - if (alphaPosition == AlphaComponentPosition.Trailing) + string hexColor; + + if (includeAlpha) { - intColor = ((uint)color.R << 24) | ((uint)color.G << 16) | ((uint)color.B << 8) | (uint)color.A; + if (alphaPosition == AlphaComponentPosition.Trailing) + { + intColor = ((uint)color.R << 24) | ((uint)color.G << 16) | ((uint)color.B << 8) | (uint)color.A; + } + else + { + // Default is Leading alpha (same as XAML) + intColor = ((uint)color.A << 24) | ((uint)color.R << 16) | ((uint)color.G << 8) | (uint)color.B; + } + + hexColor = intColor.ToString("x8", CultureInfo.InvariantCulture).ToUpperInvariant(); } else { - // Default is Leading alpha - intColor = ((uint)color.A << 24) | ((uint)color.R << 16) | ((uint)color.G << 8) | (uint)color.B; + // In this case the alpha position no longer matters + // Both cases are calculated the same + intColor = ((uint)color.R << 16) | ((uint)color.G << 8) | (uint)color.B; + hexColor = intColor.ToString("x6", CultureInfo.InvariantCulture).ToUpperInvariant(); } - string hexColor = intColor.ToString("x8", CultureInfo.InvariantCulture).ToUpperInvariant(); - if (includeSymbol) { hexColor = '#' + hexColor; diff --git a/src/Avalonia.Controls.ColorPicker/Themes/Fluent/ColorPicker.xaml b/src/Avalonia.Controls.ColorPicker/Themes/Fluent/ColorPicker.xaml index a9f52b93c7..b3c7cd9f9c 100644 --- a/src/Avalonia.Controls.ColorPicker/Themes/Fluent/ColorPicker.xaml +++ b/src/Avalonia.Controls.ColorPicker/Themes/Fluent/ColorPicker.xaml @@ -6,6 +6,8 @@ + + diff --git a/src/Avalonia.Controls.ColorPicker/Themes/Fluent/ColorPreviewer.xaml b/src/Avalonia.Controls.ColorPicker/Themes/Fluent/ColorPreviewer.xaml index 3a88d25ef1..e05fa5a907 100644 --- a/src/Avalonia.Controls.ColorPicker/Themes/Fluent/ColorPreviewer.xaml +++ b/src/Avalonia.Controls.ColorPicker/Themes/Fluent/ColorPreviewer.xaml @@ -64,6 +64,7 @@ diff --git a/src/Avalonia.Controls.ColorPicker/Themes/Fluent/ColorView.xaml b/src/Avalonia.Controls.ColorPicker/Themes/Fluent/ColorView.xaml index f72fb11bbe..acd2c7ff15 100644 --- a/src/Avalonia.Controls.ColorPicker/Themes/Fluent/ColorView.xaml +++ b/src/Avalonia.Controls.ColorPicker/Themes/Fluent/ColorView.xaml @@ -295,6 +295,8 @@ + + diff --git a/src/Avalonia.Controls.ColorPicker/Themes/Simple/ColorPicker.xaml b/src/Avalonia.Controls.ColorPicker/Themes/Simple/ColorPicker.xaml index 7639073775..ff4e1d93a8 100644 --- a/src/Avalonia.Controls.ColorPicker/Themes/Simple/ColorPicker.xaml +++ b/src/Avalonia.Controls.ColorPicker/Themes/Simple/ColorPicker.xaml @@ -6,6 +6,8 @@ + + diff --git a/src/Avalonia.Controls.ColorPicker/Themes/Simple/ColorPreviewer.xaml b/src/Avalonia.Controls.ColorPicker/Themes/Simple/ColorPreviewer.xaml index 0e51a0519a..a39dd91f52 100644 --- a/src/Avalonia.Controls.ColorPicker/Themes/Simple/ColorPreviewer.xaml +++ b/src/Avalonia.Controls.ColorPicker/Themes/Simple/ColorPreviewer.xaml @@ -64,6 +64,7 @@ diff --git a/src/Avalonia.Controls.ColorPicker/Themes/Simple/ColorView.xaml b/src/Avalonia.Controls.ColorPicker/Themes/Simple/ColorView.xaml index 4e219a98af..a26d3179b5 100644 --- a/src/Avalonia.Controls.ColorPicker/Themes/Simple/ColorView.xaml +++ b/src/Avalonia.Controls.ColorPicker/Themes/Simple/ColorView.xaml @@ -257,6 +257,8 @@ + + diff --git a/src/Avalonia.Controls/SplitButton/ToggleSplitButton.cs b/src/Avalonia.Controls/SplitButton/ToggleSplitButton.cs index 70d2983b9b..509b58833f 100644 --- a/src/Avalonia.Controls/SplitButton/ToggleSplitButton.cs +++ b/src/Avalonia.Controls/SplitButton/ToggleSplitButton.cs @@ -70,7 +70,7 @@ namespace Avalonia.Controls /// protected void Toggle() { - IsChecked = !IsChecked; + SetCurrentValue(IsCheckedProperty, !IsChecked); } /// diff --git a/src/Avalonia.Diagnostics/Diagnostics/Controls/BrushEditor.cs b/src/Avalonia.Diagnostics/Diagnostics/Controls/BrushEditor.cs index b7579ed31b..ff05614667 100644 --- a/src/Avalonia.Diagnostics/Diagnostics/Controls/BrushEditor.cs +++ b/src/Avalonia.Diagnostics/Diagnostics/Controls/BrushEditor.cs @@ -34,7 +34,11 @@ namespace Avalonia.Diagnostics.Controls { case ISolidColorBrush scb: { - var colorView = new ColorView { Color = scb.Color }; + var colorView = new ColorView + { + HexInputAlphaPosition = AlphaComponentPosition.Leading, // Always match XAML + Color = scb.Color, + }; colorView.ColorChanged += (_, e) => Brush = new ImmutableSolidColorBrush(e.NewColor); diff --git a/src/Avalonia.Diagnostics/Diagnostics/Views/PropertyValueEditorView.cs b/src/Avalonia.Diagnostics/Diagnostics/Views/PropertyValueEditorView.cs index 2811d895c1..7b58671996 100644 --- a/src/Avalonia.Diagnostics/Diagnostics/Views/PropertyValueEditorView.cs +++ b/src/Avalonia.Diagnostics/Diagnostics/Views/PropertyValueEditorView.cs @@ -130,7 +130,10 @@ namespace Avalonia.Diagnostics.Views IsEnabled = !Property.IsReadonly }; - var cv = new ColorView(); + var cv = new ColorView + { + HexInputAlphaPosition = AlphaComponentPosition.Leading, // Always match XAML + }; cv.Bind( ColorView.ColorProperty,