mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-08 04:07:40 +02:00
Add RGB hex color preview to Advanced Paste clipboard history (#43990)
<!-- Enter a brief description/summary of your PR here. What does it fix/what does it change/how was it tested (even manually, if necessary)? --> ## Summary of the Pull Request This pull request adds support for recognizing and displaying clipboard items that are valid RGB hex color codes (such as `#FFBFAB` or `#abc`) in the Advanced Paste module. It introduces logic to detect hex color strings, converts them to color values, and updates the UI to show a color preview for these items. The changes also include comprehensive unit tests for the new functionality. <!-- Please review the items on the PR checklist before submitting--> ## PR Checklist - [x] Closes: #43538 <!-- - [ ] Closes: #yyy (add separate lines for additional resolved issues) --> - [x] **Communication:** I've discussed this with core contributors already. If the work hasn't been agreed, this work might be rejected - [x] **Tests:** Added/updated and all pass - [ ] **Localization:** All end-user-facing strings can be localized - [ ] **Dev docs:** Added/updated - [ ] **New binaries:** Added on the required places - [ ] [JSON for signing](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ESRPSigning_core.json) for new binaries - [ ] [WXS for installer](https://github.com/microsoft/PowerToys/blob/main/installer/PowerToysSetup/Product.wxs) for new binaries and localization folder - [ ] [YML for CI pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ci/templates/build-powertoys-steps.yml) for new test projects - [ ] [YML for signed pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/release.yml) - [ ] **Documentation updated:** If checked, please file a pull request on [our docs repo](https://github.com/MicrosoftDocs/windows-uwp/tree/docs/hub/powertoys) and link it here: #xxx <!-- Provide a more detailed description of the PR, other things fixed, or any additional comments/features here --> ## Detailed Description of the Pull Request / Additional comments **Clipboard color detection and conversion:** * Added `ClipboardItemHelper.IsRgbHexColor` method using a compiled regex to identify valid hex color strings in clipboard text. [[1]](diffhunk://#diff-7429196ad30cd0bce57b102669da4dc13d43a09579e99ceac7cc0f7dc101cd2bR62-R86) [[2]](diffhunk://#diff-7429196ad30cd0bce57b102669da4dc13d43a09579e99ceac7cc0f7dc101cd2bR112-R114) * Introduced `HexColorConverterHelper.ConvertHexColorToRgb` utility to convert hex color strings to `Windows.UI.Color`, handling both 3-digit and 6-digit formats. **UI enhancements for color previews:** * Updated `ClipboardHistoryItemPreviewControl` to include a color preview grid that displays an ellipse filled with the detected color and the color code as text, using the new `HexColorToBrushConverter`. [[1]](diffhunk://#diff-2ed6014d4c17037b9cd0ab397e40b9069b1e7fe47a700673f34e8217d78124d5R29-R48) [[2]](diffhunk://#diff-2ed6014d4c17037b9cd0ab397e40b9069b1e7fe47a700673f34e8217d78124d5R14) [[3]](diffhunk://#diff-0c26c92697f6bb38fa40160fc8b18f0876ddc8d828a510034411001aa2e05063R1-R28) * Modified logic in `ClipboardHistoryItemPreviewControl.xaml.cs` to ensure color previews are shown only for detected color items and to adjust visibility of text and glyph previews accordingly. **Unit tests for color detection and conversion:** * Added unit tests for hex color conversion (`HexColorToColorConverterTests.cs`) and color detection logic (`ClipboardItemHelperTests.cs`) to verify correct behavior for valid, invalid, and edge-case inputs. [[1]](diffhunk://#diff-d81d997d5fb414f1563c31c38681113aaa9c847ef05bb77662d30bd1310d6b8eR1-R61) [[2]](diffhunk://#diff-185e8954ca6f061bf5d60d0c61ac6cfd87bd1a48ebda11a8172e3496a050fe85R1-R36) <!-- Describe how you validated the behavior. Add automated tests wherever possible, but list manual validation steps taken as well --> ## Validation Steps Performed * Copied to the clipboard a color encoded text like: #FFBFAB * Opened Advanced Paste and noticed the color: <img width="467" height="309" alt="image" src="https://github.com/user-attachments/assets/6cedce89-9833-4efb-abf9-3cfe8e8f32f0" /> --------- Co-authored-by: Copilot <198982749+Copilot@users.noreply.github.com> Co-authored-by: crramirez <8397379+crramirez@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
parent
e68526b8d8
commit
121c6c0712
@@ -0,0 +1,56 @@
|
|||||||
|
// Copyright (c) Microsoft Corporation
|
||||||
|
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||||
|
// See the LICENSE file in the project root for more information.
|
||||||
|
|
||||||
|
using AdvancedPaste.Converters;
|
||||||
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||||
|
using Windows.UI;
|
||||||
|
|
||||||
|
namespace AdvancedPaste.UnitTests.ConvertersTests;
|
||||||
|
|
||||||
|
[TestClass]
|
||||||
|
public sealed class HexColorToColorConverterTests
|
||||||
|
{
|
||||||
|
[TestMethod]
|
||||||
|
public void TestConvert_ValidSixDigitHex_ReturnsColor()
|
||||||
|
{
|
||||||
|
Color? result = HexColorConverterHelper.ConvertHexColorToRgb("#FFBFAB");
|
||||||
|
Assert.IsNotNull(result);
|
||||||
|
|
||||||
|
var color = (Windows.UI.Color)result;
|
||||||
|
Assert.AreEqual(255, color.R);
|
||||||
|
Assert.AreEqual(191, color.G);
|
||||||
|
Assert.AreEqual(171, color.B);
|
||||||
|
Assert.AreEqual(255, color.A);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void TestConvert_ValidThreeDigitHex_ReturnsColor()
|
||||||
|
{
|
||||||
|
Color? result = HexColorConverterHelper.ConvertHexColorToRgb("#abc");
|
||||||
|
Assert.IsNotNull(result);
|
||||||
|
|
||||||
|
var color = (Windows.UI.Color)result;
|
||||||
|
|
||||||
|
// #abc should expand to #aabbcc
|
||||||
|
Assert.AreEqual(170, color.R); // 0xaa
|
||||||
|
Assert.AreEqual(187, color.G); // 0xbb
|
||||||
|
Assert.AreEqual(204, color.B); // 0xcc
|
||||||
|
Assert.AreEqual(255, color.A);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void TestConvert_NullOrEmpty_ReturnsNull()
|
||||||
|
{
|
||||||
|
Assert.IsNull(HexColorConverterHelper.ConvertHexColorToRgb(null));
|
||||||
|
Assert.IsNull(HexColorConverterHelper.ConvertHexColorToRgb(string.Empty));
|
||||||
|
Assert.IsNull(HexColorConverterHelper.ConvertHexColorToRgb(" "));
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void TestConvert_InvalidHex_ReturnsNull()
|
||||||
|
{
|
||||||
|
Assert.IsNull(HexColorConverterHelper.ConvertHexColorToRgb("#GGGGGG"));
|
||||||
|
Assert.IsNull(HexColorConverterHelper.ConvertHexColorToRgb("#12345"));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
// Copyright (c) Microsoft Corporation
|
||||||
|
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||||
|
// See the LICENSE file in the project root for more information.
|
||||||
|
|
||||||
|
using AdvancedPaste.Helpers;
|
||||||
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||||
|
|
||||||
|
namespace AdvancedPaste.UnitTests.HelpersTests;
|
||||||
|
|
||||||
|
[TestClass]
|
||||||
|
public sealed class ClipboardItemHelperTests
|
||||||
|
{
|
||||||
|
[TestMethod]
|
||||||
|
[DataRow("#FFBFAB", true)]
|
||||||
|
[DataRow("#000000", true)]
|
||||||
|
[DataRow("#FFFFFF", true)]
|
||||||
|
[DataRow("#fff", true)]
|
||||||
|
[DataRow("#abc", true)]
|
||||||
|
[DataRow("#123456", true)]
|
||||||
|
[DataRow("#AbCdEf", true)]
|
||||||
|
[DataRow("FFBFAB", false)] // Missing #
|
||||||
|
[DataRow("#GGGGGG", false)] // Invalid hex characters
|
||||||
|
[DataRow("#12345", false)] // Wrong length
|
||||||
|
[DataRow("#1234567", false)] // Too long
|
||||||
|
[DataRow("", false)]
|
||||||
|
[DataRow(null, false)]
|
||||||
|
[DataRow(" #FFF ", true)] // Whitespace should be trimmed
|
||||||
|
[DataRow("Not a color", false)]
|
||||||
|
[DataRow("#", false)]
|
||||||
|
[DataRow("##FFFFFF", false)]
|
||||||
|
public void TestIsRgbHexColor(string input, bool expected)
|
||||||
|
{
|
||||||
|
bool result = ClipboardItemHelper.IsRgbHexColor(input);
|
||||||
|
Assert.AreEqual(expected, result, $"IsRgbHexColor(\"{input}\") should return {expected}");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -11,6 +11,7 @@
|
|||||||
mc:Ignorable="d">
|
mc:Ignorable="d">
|
||||||
<UserControl.Resources>
|
<UserControl.Resources>
|
||||||
<converters:DateTimeToFriendlyStringConverter x:Key="DateTimeToFriendlyStringConverter" />
|
<converters:DateTimeToFriendlyStringConverter x:Key="DateTimeToFriendlyStringConverter" />
|
||||||
|
<converters:HexColorToBrushConverter x:Key="HexColorToBrushConverter" />
|
||||||
<tkconverters:BoolToVisibilityConverter x:Name="BoolToVisibilityConverter" />
|
<tkconverters:BoolToVisibilityConverter x:Name="BoolToVisibilityConverter" />
|
||||||
</UserControl.Resources>
|
</UserControl.Resources>
|
||||||
<Grid ColumnSpacing="12">
|
<Grid ColumnSpacing="12">
|
||||||
@@ -25,6 +26,26 @@
|
|||||||
Source="{x:Bind ClipboardItem.Image, Mode=OneWay}"
|
Source="{x:Bind ClipboardItem.Image, Mode=OneWay}"
|
||||||
Stretch="UniformToFill"
|
Stretch="UniformToFill"
|
||||||
Visibility="{x:Bind HasImage, Mode=OneWay, Converter={StaticResource BoolToVisibilityConverter}}" />
|
Visibility="{x:Bind HasImage, Mode=OneWay, Converter={StaticResource BoolToVisibilityConverter}}" />
|
||||||
|
<!-- Color preview with text -->
|
||||||
|
<Grid Visibility="{x:Bind HasColor, Mode=OneWay, Converter={StaticResource BoolToVisibilityConverter}}">
|
||||||
|
<Grid.ColumnDefinitions>
|
||||||
|
<ColumnDefinition Width="Auto" />
|
||||||
|
<ColumnDefinition Width="*" />
|
||||||
|
</Grid.ColumnDefinitions>
|
||||||
|
<Ellipse
|
||||||
|
Grid.Column="0"
|
||||||
|
Width="8"
|
||||||
|
Height="8"
|
||||||
|
Margin="8,0,8,0"
|
||||||
|
Fill="{x:Bind ClipboardItem.Content, Mode=OneWay, Converter={StaticResource HexColorToBrushConverter}}" />
|
||||||
|
<TextBlock
|
||||||
|
Grid.Column="1"
|
||||||
|
VerticalAlignment="Center"
|
||||||
|
FontSize="10"
|
||||||
|
Foreground="{ThemeResource TextFillColorSecondaryBrush}"
|
||||||
|
Text="{x:Bind ClipboardItem.Content, Mode=OneWay}"
|
||||||
|
TextWrapping="NoWrap" />
|
||||||
|
</Grid>
|
||||||
<!-- Text preview -->
|
<!-- Text preview -->
|
||||||
<TextBlock
|
<TextBlock
|
||||||
Margin="8,0,0,0"
|
Margin="8,0,0,0"
|
||||||
|
|||||||
@@ -38,9 +38,11 @@ namespace AdvancedPaste.Controls
|
|||||||
|
|
||||||
public bool HasImage => ContentImage is not null;
|
public bool HasImage => ContentImage is not null;
|
||||||
|
|
||||||
public bool HasText => !string.IsNullOrEmpty(ContentText) && !HasImage;
|
public bool HasText => !string.IsNullOrEmpty(ContentText) && !HasImage && !HasColor;
|
||||||
|
|
||||||
public bool HasGlyph => !HasImage && !HasText && !string.IsNullOrEmpty(IconGlyph);
|
public bool HasGlyph => !HasImage && !HasText && !HasColor && !string.IsNullOrEmpty(IconGlyph);
|
||||||
|
|
||||||
|
public bool HasColor => ClipboardItemHelper.IsRgbHexColor(ContentText);
|
||||||
|
|
||||||
public ClipboardHistoryItemPreviewControl()
|
public ClipboardHistoryItemPreviewControl()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -0,0 +1,39 @@
|
|||||||
|
// Copyright (c) Microsoft Corporation
|
||||||
|
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||||
|
// See the LICENSE file in the project root for more information.
|
||||||
|
|
||||||
|
namespace AdvancedPaste.Converters
|
||||||
|
{
|
||||||
|
public static class HexColorConverterHelper
|
||||||
|
{
|
||||||
|
public static Windows.UI.Color? ConvertHexColorToRgb(string hexColor)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// Remove # if present
|
||||||
|
var cleanHex = hexColor.TrimStart('#');
|
||||||
|
|
||||||
|
// Expand 3-digit hex to 6-digit (#ABC -> #AABBCC)
|
||||||
|
if (cleanHex.Length == 3)
|
||||||
|
{
|
||||||
|
cleanHex = $"{cleanHex[0]}{cleanHex[0]}{cleanHex[1]}{cleanHex[1]}{cleanHex[2]}{cleanHex[2]}";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (cleanHex.Length == 6)
|
||||||
|
{
|
||||||
|
var r = System.Convert.ToByte(cleanHex.Substring(0, 2), 16);
|
||||||
|
var g = System.Convert.ToByte(cleanHex.Substring(2, 2), 16);
|
||||||
|
var b = System.Convert.ToByte(cleanHex.Substring(4, 2), 16);
|
||||||
|
|
||||||
|
return Windows.UI.Color.FromArgb(255, r, g, b);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
// Invalid color format - return null
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
// Copyright (c) Microsoft Corporation
|
||||||
|
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||||
|
// See the LICENSE file in the project root for more information.
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using Microsoft.UI.Xaml.Data;
|
||||||
|
using Microsoft.UI.Xaml.Media;
|
||||||
|
|
||||||
|
namespace AdvancedPaste.Converters
|
||||||
|
{
|
||||||
|
public sealed partial class HexColorToBrushConverter : IValueConverter
|
||||||
|
{
|
||||||
|
public object ConvertBack(object value, Type targetType, object parameter, string language)
|
||||||
|
=> throw new NotSupportedException();
|
||||||
|
|
||||||
|
public object Convert(object value, Type targetType, object parameter, string language)
|
||||||
|
{
|
||||||
|
if (value is not string hexColor || string.IsNullOrWhiteSpace(hexColor))
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
Windows.UI.Color? color = HexColorConverterHelper.ConvertHexColorToRgb(hexColor);
|
||||||
|
|
||||||
|
return color != null ? new SolidColorBrush((Windows.UI.Color)color) : null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,6 +3,7 @@
|
|||||||
// See the LICENSE file in the project root for more information.
|
// See the LICENSE file in the project root for more information.
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using AdvancedPaste.Models;
|
using AdvancedPaste.Models;
|
||||||
using Microsoft.UI.Xaml.Media.Imaging;
|
using Microsoft.UI.Xaml.Media.Imaging;
|
||||||
@@ -10,8 +11,11 @@ using Windows.ApplicationModel.DataTransfer;
|
|||||||
|
|
||||||
namespace AdvancedPaste.Helpers
|
namespace AdvancedPaste.Helpers
|
||||||
{
|
{
|
||||||
internal static class ClipboardItemHelper
|
internal static partial class ClipboardItemHelper
|
||||||
{
|
{
|
||||||
|
// Compiled regex for better performance when checking multiple clipboard items
|
||||||
|
private static readonly Regex HexColorRegex = HexColorCompiledRegex();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates a ClipboardItem from current clipboard data.
|
/// Creates a ClipboardItem from current clipboard data.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -55,6 +59,31 @@ namespace AdvancedPaste.Helpers
|
|||||||
return clipboardItem;
|
return clipboardItem;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Checks if text is a valid RGB hex color (e.g., #FFBFAB or #fff).
|
||||||
|
/// </summary>
|
||||||
|
public static bool IsRgbHexColor(string text)
|
||||||
|
{
|
||||||
|
if (text == null)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
string trimmedText = text.Trim();
|
||||||
|
if (trimmedText.Length > 7)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (string.IsNullOrWhiteSpace(trimmedText))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Match #RGB or #RRGGBB format (case-insensitive)
|
||||||
|
return HexColorRegex.IsMatch(trimmedText);
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates a BitmapImage from clipboard data.
|
/// Creates a BitmapImage from clipboard data.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -80,5 +109,8 @@ namespace AdvancedPaste.Helpers
|
|||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[GeneratedRegex(@"^#([0-9A-Fa-f]{3}|[0-9A-Fa-f]{6})$")]
|
||||||
|
private static partial Regex HexColorCompiledRegex();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user