Skip to content

Commit

Permalink
Merge pull request #62 from WPFDevelopersOrg/dev
Browse files Browse the repository at this point in the history
master pull request  dev
  • Loading branch information
yanjinhuagood authored Aug 16, 2023
2 parents d033b41 + cbb102c commit dab5cdb
Show file tree
Hide file tree
Showing 44 changed files with 1,777 additions and 191 deletions.
3 changes: 2 additions & 1 deletion src/WPFDevelopers.Net40/Themes/Generic.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<Setter.Value>
<ControlTemplate TargetType="{x:Type wd:Window}">
<Border
Name="PART_Border"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Expand Down Expand Up @@ -161,7 +162,7 @@
<Trigger Property="WindowState" Value="Maximized">
<Setter TargetName="PART_RestoreButton" Property="Visibility" Value="Visible" />
<Setter TargetName="PART_MaximizeButton" Property="Visibility" Value="Collapsed" />
<Setter TargetName="LayoutRoot" Property="Margin" Value="7" />
<Setter TargetName="PART_Border" Property="Margin" Value="7" />
</Trigger>
<Trigger Property="WindowStyle" Value="ToolWindow">
<Setter TargetName="PART_MinAndMax" Property="Visibility" Value="Collapsed" />
Expand Down
287 changes: 245 additions & 42 deletions src/WPFDevelopers.Net40/Themes/Theme.xaml

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion src/WPFDevelopers.Net45x/Themes/Generic.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
<Setter.Value>
<ControlTemplate TargetType="{x:Type wd:Window}">
<Border
Name="PART_Border"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Expand Down Expand Up @@ -167,7 +168,7 @@
<Trigger Property="WindowState" Value="Maximized">
<Setter TargetName="PART_RestoreButton" Property="Visibility" Value="Visible" />
<Setter TargetName="PART_MaximizeButton" Property="Visibility" Value="Collapsed" />
<Setter TargetName="LayoutRoot" Property="Margin" Value="7" />
<Setter TargetName="PART_Border" Property="Margin" Value="7" />
</Trigger>
<Trigger Property="WindowStyle" Value="ToolWindow">
<Setter TargetName="PART_MinAndMax" Property="Visibility" Value="Collapsed" />
Expand Down
287 changes: 245 additions & 42 deletions src/WPFDevelopers.Net45x/Themes/Theme.xaml

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/WPFDevelopers.Samples.Shared/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<!-- 需要注意ws:Resources 必须再配色主题后,Theme="Dark" 为黑色皮肤 -->
<wd:Resources Theme="Light" />
<ResourceDictionary Source="pack://application:,,,/WPFDevelopers;component/Themes/Theme.xaml" />
<!-- 下方不用拷贝 -->
<ResourceDictionary Source="pack://application:,,,/WPFDevelopers.Samples;component/ExampleViews/LoginWindow/Generic.xaml" />
<ResourceDictionary Source="pack://application:,,,/WPFDevelopers.Samples;component/Controls/CodeViewer/CodeViewer.xaml" />
<ResourceDictionary Source="pack://application:,,,/WPFDevelopers.Samples;component/Controls/TimeLine/TimeLine.xaml" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<UserControl
x:Class="WPFDevelopers.Samples.ExampleViews.ColorPickerExample"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="clr-namespace:WPFDevelopers.Samples.Controls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WPFDevelopers.Samples.ExampleViews"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:wd="https://github.com/WPFDevelopersOrg/WPFDevelopers"
d:DesignHeight="450"
d:DesignWidth="800"
mc:Ignorable="d">
<controls:CodeViewer>
<wd:ColorPicker />
<controls:CodeViewer.SourceCodes>
<controls:SourceCodeModel CodeSource="/WPFDevelopers.SamplesCode;component/ExampleViews/ColorPickerExample.xaml" CodeType="Xaml" />
<controls:SourceCodeModel CodeSource="/WPFDevelopers.SamplesCode;component/ExampleViews/ColorPickerExample.xaml.cs" CodeType="CSharp" />
</controls:CodeViewer.SourceCodes>
</controls:CodeViewer>
</UserControl>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Windows.Controls;

namespace WPFDevelopers.Samples.ExampleViews
{
/// <summary>
/// BarrageExample.xaml 的交互逻辑
/// </summary>
public partial class ColorPickerExample : UserControl
{
public ColorPickerExample()
{
InitializeComponent();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
Rows="1">
<wd:NumericBox
Width="100"
Height="30"
Maximum="100"
Minimum="0" />
<wd:NumericBox
Width="100"
Height="40"
Margin="10,0"
Maximum="1000"
Minimum="100"
Expand Down
1 change: 1 addition & 0 deletions src/WPFDevelopers.Samples.Shared/Helpers/MenuEnum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public enum MenuEnum
Message,
PathIcon,
NumericBox,
ColorPicker,
VirtualizingWrapPanel,
AcrylicBlur,
TaskbarInfo
Expand Down
3 changes: 3 additions & 0 deletions src/WPFDevelopers.Samples.Shared/ViewModels/MainVM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,9 @@ void MenuItemSelection(string _menuName)
case MenuEnum.NumericBox:
ControlPanel = new NumericBoxExample();
break;
case MenuEnum.ColorPicker:
ControlPanel = new ColorPickerExample();
break;
case MenuEnum.VirtualizingWrapPanel:
ControlPanel = new VirtualizingWrapPanel();
new VirtualizingWrapPanelExample().MaskShowDialog();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@
<SubType>Code</SubType>
<DependentUpon>CircularProgressBarExample.xaml</DependentUpon>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)ExampleViews\ColorPickerExample.xaml.cs">
<DependentUpon>ColorPickerExample.xaml</DependentUpon>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)ExampleViews\CountdownTimerExample.xaml.cs">
<SubType>Code</SubType>
<DependentUpon>CountdownTimerExample.xaml</DependentUpon>
Expand Down Expand Up @@ -556,6 +559,10 @@
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
<Page Include="$(MSBuildThisFileDirectory)ExampleViews\ColorPickerExample.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
<Page Include="$(MSBuildThisFileDirectory)ExampleViews\CountdownTimerExample.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
Expand Down
2 changes: 1 addition & 1 deletion src/WPFDevelopers.Samples40/WPFDevelopers.Samples40.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
</BootstrapperPackage>
</ItemGroup>
<ItemGroup>
<PackageReference Include="WPFDevelopers" Version="1.1.0.2-preview1" />
<PackageReference Include="WPFDevelopers" Version="1.1.0.2-preview2" />
</ItemGroup>
<ItemGroup>
<Resource Include="..\WPFDevelopers.Samples.Shared\WPFDevelopers.ico">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<ApplicationIcon>..\WPFDevelopers.Samples.Shared\WPFDevelopers.ico</ApplicationIcon>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="WPFDevelopers" Version="1.1.0.2-preview1" />
<PackageReference Include="WPFDevelopers" Version="1.1.0.2-preview2" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@
<Resource Include="..\WPFDevelopers.Samples.Shared\Controls\ScrollViewer\ScrollViewerAnimation.cs" Link="Controls\ScrollViewer\ScrollViewerAnimation.cs" />
<Resource Include="..\WPFDevelopers.Samples.Shared\Controls\ScrollViewer\ScrollViewerBehavior.cs" Link="Controls\ScrollViewer\ScrollViewerBehavior.cs" />
<Resource Include="..\WPFDevelopers.Samples.Shared\Controls\SongWords\SongWords.cs" Link="Controls\SongWords\SongWords.cs" />
<Resource Include="..\WPFDevelopers.Samples.Shared\Controls\CountdownTimer\CountdownTimer.cs" Link="Controls\CountdownTimer\CountdownTimer.cs" />
<Resource Include="..\WPFDevelopers.Samples.Shared\Controls\ElementAdorner\ElementAdorner.cs" Link="Controls\ElementAdorner\ElementAdorner.cs" />
<Resource Include="..\WPFDevelopers.Samples.Shared\Controls\ElementAdorner\ThumbAngle.cs" Link="Controls\ElementAdorner\ThumbAngle.cs" />
<Resource Include="..\WPFDevelopers.Samples.Shared\Controls\CropControl\CropControl.cs" Link="Controls\CropControl\CropControl.cs" />
Expand All @@ -137,6 +138,7 @@
<Resource Include="..\WPFDevelopers.Samples.Shared\ExampleViews\NoneNoChromeWindow.xaml.cs" Link="ExampleViews\NoneNoChromeWindow.xaml.cs" DependentUpon="NoneNoChromeWindow.xaml" />
<Resource Include="..\WPFDevelopers.Samples.Shared\ExampleViews\PathIconExample.xaml.cs" Link="ExampleViews\PathIconExample.xaml.cs" DependentUpon="PathIconExample.xaml" />
<Resource Include="..\WPFDevelopers.Samples.Shared\ExampleViews\NumericBoxExample.xaml.cs" Link="ExampleViews\NumericBoxExample.xaml.cs" DependentUpon="NumericBoxExample.xaml" />
<Resource Include="..\WPFDevelopers.Samples.Shared\ExampleViews\ColorPickerExample.xaml.cs" Link="ExampleViews\ColorPickerExample.xaml.cs" DependentUpon="ColorPickerExample.xaml" />
</ItemGroup>
<ItemGroup>
<Resource Include="..\WPFDevelopers.Samples.Shared\ExampleViews\AboutWindow.xaml">
Expand Down Expand Up @@ -463,5 +465,8 @@
<Resource Include="..\WPFDevelopers.Samples.Shared\ExampleViews\NumericBoxExample.xaml">
<Link>ExampleViews\NumericBoxExample.xaml</Link>
</Resource>
<Resource Include="..\WPFDevelopers.Samples.Shared\ExampleViews\ColorPickerExample.xaml">
<Link>ExampleViews\ColorPickerExample.xaml</Link>
</Resource>
</ItemGroup>
</Project>
223 changes: 223 additions & 0 deletions src/WPFDevelopers.Shared/Controls/ColorPicker/ColorPicker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Media;
using WPFDevelopers.Utilities;

namespace WPFDevelopers.Controls
{
[TemplatePart(Name = HueSliderColorTemplateName, Type = typeof(Slider))]
[TemplatePart(Name = CanvasTemplateName, Type = typeof(Canvas))]
[TemplatePart(Name = ThumbTemplateName, Type = typeof(Thumb))]
[TemplatePart(Name = ButtonTemplateName, Type = typeof(Button))]
public class ColorPicker : Control
{
private const string HueSliderColorTemplateName = "PART_HueSlider";

private const string CanvasTemplateName = "PART_Canvas";

private const string ThumbTemplateName = "PART_Thumb";

private const string ButtonTemplateName = "PART_Button";

private static readonly DependencyPropertyKey HueColorPropertyKey =
DependencyProperty.RegisterReadOnly("HueColor", typeof(Color), typeof(ColorPicker),
new PropertyMetadata(Colors.Red));

public static readonly DependencyProperty HueColorProperty = HueColorPropertyKey.DependencyProperty;

public static readonly DependencyProperty SelectedColorProperty =
DependencyProperty.Register("SelectedColor", typeof(Color), typeof(ColorPicker),
new FrameworkPropertyMetadata(Colors.Red, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
OnSelectedColorChanged));

private static readonly DependencyPropertyKey HSBPropertyKey =
DependencyProperty.RegisterReadOnly("HSB", typeof(HSB), typeof(ColorPicker),
new PropertyMetadata(new HSB()));

public static readonly DependencyProperty HSBHProperty = HSBPropertyKey.DependencyProperty;

public static readonly DependencyProperty ColorTypeProperty =
DependencyProperty.Register("ColorType", typeof(ColorTypeEnum), typeof(ColorPicker),
new PropertyMetadata(ColorTypeEnum.RGB));

private Button _button;

private Canvas _canvas;

private Slider _hueSliderColor;

private bool _isInnerUpdateSelectedColor;

private Thumb _thumb;

private ColorTypeEnum[] colorTypeEnums;

private int currentGridStateIndex;


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

public Color HueColor => (Color) GetValue(HueColorProperty);

public Color SelectedColor
{
get => (Color) GetValue(SelectedColorProperty);
set => SetValue(SelectedColorProperty, value);
}

public HSB HSB => (HSB) GetValue(HSBHProperty);


public ColorTypeEnum ColorType
{
get => (ColorTypeEnum) GetValue(ColorTypeProperty);
set => SetValue(ColorTypeProperty, value);
}

private static void OnSelectedColorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var ctrl = d as ColorPicker;
if (ctrl._isInnerUpdateSelectedColor)
{
ctrl._isInnerUpdateSelectedColor = false;
return;
}

var color = (Color) e.NewValue;
double h = 0, s = 0, b = 0;
ColorUtil.HsbFromColor(color, ref h, ref s, ref b);
var hsb = new HSB {H = h, S = s, B = b};
ctrl.SetValue(HueColorPropertyKey, ColorUtil.ColorFromHsb(hsb.H, 1, 1));
ctrl.SetValue(HSBPropertyKey, hsb);
Canvas.SetLeft(ctrl._thumb, s * ctrl._canvas.ActualWidth - ctrl._thumb.ActualWidth / 2);
Canvas.SetTop(ctrl._thumb, (1 - b) * ctrl._canvas.ActualHeight - ctrl._thumb.ActualHeight / 2);
ctrl._hueSliderColor.Value = 1 - h;
}

public override void OnApplyTemplate()
{
base.OnApplyTemplate();
if (_hueSliderColor != null)
_hueSliderColor.ValueChanged -= HueSliderColor_OnValueChanged;
_canvas = GetTemplateChild(CanvasTemplateName) as Canvas;
if (_canvas != null)
{
_canvas.Loaded += Canvas_Loaded;
_canvas.MouseUp += Canvas_MouseUp;
}

_thumb = GetTemplateChild(ThumbTemplateName) as Thumb;
if (_thumb != null)
_thumb.DragDelta += Thumb_DragDelta;
_hueSliderColor = GetTemplateChild(HueSliderColorTemplateName) as Slider;
if (_hueSliderColor != null)
_hueSliderColor.ValueChanged += HueSliderColor_OnValueChanged;

_button = GetTemplateChild(ButtonTemplateName) as Button;
currentGridStateIndex = 0;
colorTypeEnums = (ColorTypeEnum[]) Enum.GetValues(typeof(ColorTypeEnum));
if (_button != null)
_button.Click += Button_Click;
}

private void Button_Click(object sender, RoutedEventArgs e)
{
currentGridStateIndex = (currentGridStateIndex + 1) % colorTypeEnums.Length;
ColorType = colorTypeEnums[currentGridStateIndex];
}

private void Canvas_MouseUp(object sender, MouseButtonEventArgs e)
{
var canvasPosition = e.GetPosition(_canvas);
GetHSB(canvasPosition);
}

private void Canvas_MouseDown(object sender, MouseButtonEventArgs e)
{
var canvasPosition = e.GetPosition(_canvas);
GetHSB(canvasPosition);
}

private void GetHSB(Point point, bool isMove = true)
{
var newLeft = point.X - _thumb.ActualWidth / 2;
var newTop = point.Y - _thumb.ActualHeight / 2;
var thumbW = _thumb.ActualWidth / 2;
var thumbH = _thumb.ActualHeight / 2;
var canvasRight = _canvas.ActualWidth - thumbW;
var canvasBottom = _canvas.ActualHeight - thumbH;
if (newLeft < -thumbW)
newLeft = -thumbW;
else if (newLeft > canvasRight)
newLeft = canvasRight;
if (newTop < -thumbH)
newTop = -thumbH;
else if (newTop > canvasBottom)
newTop = canvasBottom;

if (isMove)
{
Canvas.SetLeft(_thumb, newLeft);
Canvas.SetTop(_thumb, newTop);
}

var hsb = new HSB
{
H = HSB.H, S = (newLeft + thumbW) / _canvas.ActualWidth,
B = 1 - (newTop + thumbH) / _canvas.ActualHeight
};
SetValue(HSBPropertyKey, hsb);
var currentColor = ColorUtil.ColorFromAhsb(1, HSB.H, HSB.S, HSB.B);
if (SelectedColor != currentColor)
{
_isInnerUpdateSelectedColor = true;
SelectedColor = currentColor;
}
}

private void Thumb_DragDelta(object sender, DragDeltaEventArgs e)
{
var point = Mouse.GetPosition(_canvas);
GetHSB(point);
}

private void Canvas_Loaded(object sender, RoutedEventArgs e)
{
var width = (int) _canvas.ActualWidth;
var height = (int) _canvas.ActualHeight;
var point = new Point(width - _thumb.ActualWidth / 2, -_thumb.ActualHeight / 2);
Canvas.SetLeft(_thumb, point.X);
Canvas.SetTop(_thumb, point.Y);
var hsb = new HSB {H = _hueSliderColor.Value, S = HSB.S, B = HSB.B};
SetValue(HSBPropertyKey, hsb);
}

private void HueSliderColor_OnValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
if (DoubleUtil.AreClose(HSB.H, e.NewValue))
return;
var hsb = new HSB {H = 1 - e.NewValue, S = HSB.S, B = HSB.B};
SetValue(HSBPropertyKey, hsb);
SetValue(HueColorPropertyKey, ColorUtil.ColorFromHsb(HSB.H, 1, 1));

var newLeft = Canvas.GetLeft(_thumb);
var newTop = Canvas.GetTop(_thumb);
var point = new Point(newLeft, newTop);
GetHSB(point, false);
}
}

public enum ColorTypeEnum
{
RGB,
HSL,
HEX
}
}
Loading

0 comments on commit dab5cdb

Please sign in to comment.