Skip to content

Commit d46a857

Browse files
committed
Refactor slider helper into controls
1 parent 94b5ffd commit d46a857

5 files changed

Lines changed: 66 additions & 62 deletions

File tree

src/Wpf.Ui.Violeta.Gallery/Pages/BasicInput/SliderPage.xaml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
77
xmlns:ui="http://schemas.lepo.co/wpfui/2022/xaml"
88
xmlns:vio="http://schemas.lepo.co/wpfui/2022/xaml/violeta"
9-
xmlns:hotfix="clr-namespace:Wpf.Ui.Violeta.Hotfix;assembly=Wpf.Ui.Violeta"
109
xmlns:lang="clr-namespace:Wpf.Ui.Violeta.Gallery.Globalization"
1110
Title="{I18N {x:Static lang:LangKeys.Gallery_Page_Slider}}"
1211
d:Background="#1C1C1C"
@@ -211,8 +210,8 @@
211210
AutoToolTipPlacement="TopLeft"
212211
Maximum="100"
213212
Minimum="0"
214-
hotfix:SliderAutoToolTipHelper.Prefix="Volume: "
215-
hotfix:SliderAutoToolTipHelper.Suffix=" %"
213+
ui:ControlHelper.Prefix="Volume: "
214+
ui:ControlHelper.Suffix=" %"
216215
Value="35" />
217216
<StackPanel Grid.Column="1" Margin="16,0,0,0" VerticalAlignment="Center">
218217
<TextBlock Foreground="{DynamicResource TextFillColorTertiaryBrush}" Text="{I18N {x:Static lang:LangKeys.Sample_b1282d58a4}}" />

src/Wpf.Ui.Violeta/Controls/Primitives/ControlHelper.cs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,4 +1068,54 @@ private static void TryFocusOnLoaded(UIElement element)
10681068
element.Focus();
10691069
}
10701070
}
1071+
1072+
// -- Prefix ----------------------------------------------------------
1073+
1074+
/// <summary>
1075+
/// Identifies the Prefix attached property.
1076+
/// Optional text prepended to a value shown by a control (e.g. the Slider AutoToolTip).
1077+
/// </summary>
1078+
public static readonly DependencyProperty PrefixProperty =
1079+
DependencyProperty.RegisterAttached(
1080+
"Prefix",
1081+
typeof(string),
1082+
typeof(ControlHelper),
1083+
new PropertyMetadata(null));
1084+
1085+
/// <summary>
1086+
/// Gets the text prepended to the displayed value.
1087+
/// </summary>
1088+
public static string? GetPrefix(DependencyObject element) =>
1089+
(string?)element.GetValue(PrefixProperty);
1090+
1091+
/// <summary>
1092+
/// Sets the text prepended to the displayed value.
1093+
/// </summary>
1094+
public static void SetPrefix(DependencyObject element, string? value) =>
1095+
element.SetValue(PrefixProperty, value);
1096+
1097+
// -- Suffix ----------------------------------------------------------
1098+
1099+
/// <summary>
1100+
/// Identifies the Suffix attached property.
1101+
/// Optional text appended to a value shown by a control (e.g. the Slider AutoToolTip).
1102+
/// </summary>
1103+
public static readonly DependencyProperty SuffixProperty =
1104+
DependencyProperty.RegisterAttached(
1105+
"Suffix",
1106+
typeof(string),
1107+
typeof(ControlHelper),
1108+
new PropertyMetadata(null));
1109+
1110+
/// <summary>
1111+
/// Gets the text appended to the displayed value.
1112+
/// </summary>
1113+
public static string? GetSuffix(DependencyObject element) =>
1114+
(string?)element.GetValue(SuffixProperty);
1115+
1116+
/// <summary>
1117+
/// Sets the text appended to the displayed value.
1118+
/// </summary>
1119+
public static void SetSuffix(DependencyObject element, string? value) =>
1120+
element.SetValue(SuffixProperty, value);
10711121
}

src/Wpf.Ui.Violeta/Hotfix/Slider.xaml renamed to src/Wpf.Ui.Violeta/Controls/Slider/Slider.xaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<ResourceDictionary
22
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
33
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4-
xmlns:hotfix="clr-namespace:Wpf.Ui.Violeta.Hotfix"
4+
xmlns:local="clr-namespace:Wpf.Ui.Violeta.Controls"
55
xmlns:sys="clr-namespace:System;assembly=mscorlib">
66

77
<!-- Closely based on WPF-UI Slider, plus WinUI AutoToolTip + accent decrease fill + thumb motion. -->
@@ -67,7 +67,7 @@
6767
BasedOn="{StaticResource {x:Type ToolTip}}"
6868
TargetType="{x:Type ToolTip}">
6969
<Setter Property="FontFamily" Value="{DynamicResource ContentControlThemeFontFamily}" />
70-
<Setter Property="hotfix:SliderAutoToolTipHelper.IsEnabled" Value="True" />
70+
<Setter Property="local:SliderHelper.IsEnabled" Value="True" />
7171
<Setter Property="HorizontalContentAlignment" Value="Center" />
7272
<Setter Property="VerticalContentAlignment" Value="Center" />
7373
</Style>
@@ -81,7 +81,7 @@
8181
<Setter Property="Foreground" Value="{DynamicResource SliderThumbBackground}" />
8282
<Setter Property="BorderThickness" Value="1" />
8383
<Setter Property="Background" Value="{DynamicResource SliderOuterThumbBackground}" />
84-
<Setter Property="hotfix:SliderAutoToolTipHelper.Attach" Value="True" />
84+
<Setter Property="local:SliderHelper.Attach" Value="True" />
8585
<Setter Property="Template">
8686
<Setter.Value>
8787
<ControlTemplate TargetType="{x:Type Thumb}">

src/Wpf.Ui.Violeta/Hotfix/SliderAutoToolTipHelper.cs renamed to src/Wpf.Ui.Violeta/Controls/Slider/SliderHelper.cs

Lines changed: 10 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@
66
using System.Windows.Controls.Primitives;
77
using System.Windows.Media;
88
using System.Windows.Threading;
9+
using Wpf.Ui.Controls;
910

10-
namespace Wpf.Ui.Violeta.Hotfix;
11+
namespace Wpf.Ui.Violeta.Controls;
1112

1213
/// <summary>
13-
/// WinUI-style Slider AutoToolTip placement and Fluent style attachment for Hotfix Slider.
14+
/// WinUI-style Slider AutoToolTip placement and Fluent style attachment.
1415
/// </summary>
15-
public static class SliderAutoToolTipHelper
16+
public static class SliderHelper
1617
{
1718
private const string AutoToolTipStyleKey = "SliderAutoToolTipStyle";
1819

@@ -35,7 +36,7 @@ public static void SetAttach(Thumb thumb, bool value)
3536
DependencyProperty.RegisterAttached(
3637
"Attach",
3738
typeof(bool),
38-
typeof(SliderAutoToolTipHelper),
39+
typeof(SliderHelper),
3940
new PropertyMetadata(false, OnAttachChanged));
4041

4142
private static void OnAttachChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
@@ -89,52 +90,6 @@ private static void ApplyFluentStyle(ToolTip toolTip, FrameworkElement resourceH
8990

9091
#endregion Attach
9192

92-
#region Prefix / Suffix
93-
94-
/// <summary>
95-
/// Optional text prepended to the AutoToolTip value, e.g. "Volume: ".
96-
/// Attach to the <see cref="Slider"/> (or to the ToolTip itself).
97-
/// </summary>
98-
public static string? GetPrefix(DependencyObject element)
99-
{
100-
return (string?)element.GetValue(PrefixProperty);
101-
}
102-
103-
public static void SetPrefix(DependencyObject element, string? value)
104-
{
105-
element.SetValue(PrefixProperty, value);
106-
}
107-
108-
public static readonly DependencyProperty PrefixProperty =
109-
DependencyProperty.RegisterAttached(
110-
"Prefix",
111-
typeof(string),
112-
typeof(SliderAutoToolTipHelper),
113-
new PropertyMetadata(null));
114-
115-
/// <summary>
116-
/// Optional text appended to the AutoToolTip value, e.g. "%".
117-
/// Attach to the <see cref="Slider"/> (or to the ToolTip itself).
118-
/// </summary>
119-
public static string? GetSuffix(DependencyObject element)
120-
{
121-
return (string?)element.GetValue(SuffixProperty);
122-
}
123-
124-
public static void SetSuffix(DependencyObject element, string? value)
125-
{
126-
element.SetValue(SuffixProperty, value);
127-
}
128-
129-
public static readonly DependencyProperty SuffixProperty =
130-
DependencyProperty.RegisterAttached(
131-
"Suffix",
132-
typeof(string),
133-
typeof(SliderAutoToolTipHelper),
134-
new PropertyMetadata(null));
135-
136-
#endregion Prefix / Suffix
137-
13893
#region IsEnabled
13994

14095
public static bool GetIsEnabled(ToolTip toolTip)
@@ -151,7 +106,7 @@ public static void SetIsEnabled(ToolTip toolTip, bool value)
151106
DependencyProperty.RegisterAttached(
152107
"IsEnabled",
153108
typeof(bool),
154-
typeof(SliderAutoToolTipHelper),
109+
typeof(SliderHelper),
155110
new PropertyMetadata(OnIsEnabledChanged));
156111

157112
private static void OnIsEnabledChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
@@ -231,8 +186,8 @@ thumb.TemplatedParent is not Slider slider ||
231186
}
232187

233188
// When attached to the ToolTip itself, honor the local value over the slider's.
234-
string? prefix = GetPrefix(toolTip) ?? GetPrefix(slider);
235-
string? suffix = GetSuffix(toolTip) ?? GetSuffix(slider);
189+
string? prefix = ControlHelper.GetPrefix(toolTip) ?? ControlHelper.GetPrefix(slider);
190+
string? suffix = ControlHelper.GetSuffix(toolTip) ?? ControlHelper.GetSuffix(slider);
236191

237192
if (string.IsNullOrEmpty(prefix) && string.IsNullOrEmpty(suffix))
238193
{
@@ -260,7 +215,7 @@ thumb.TemplatedParent is not Slider slider ||
260215
DependencyProperty.RegisterAttached(
261216
"IsFormattingContent",
262217
typeof(bool),
263-
typeof(SliderAutoToolTipHelper));
218+
typeof(SliderHelper));
264219

265220
private static bool GetIsFormattingContent(ToolTip toolTip)
266221
{
@@ -280,7 +235,7 @@ private static void SetIsFormattingContent(ToolTip toolTip, bool value)
280235
DependencyProperty.RegisterAttached(
281236
"OriginalCustomPopupPlacementCallback",
282237
typeof(CustomPopupPlacementCallback),
283-
typeof(SliderAutoToolTipHelper));
238+
typeof(SliderHelper));
284239

285240
private static CustomPopupPlacementCallback GetOriginalCustomPopupPlacementCallback(ToolTip toolTip)
286241
{

src/Wpf.Ui.Violeta/Resources/Wpf.Ui.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@
121121
<ResourceDictionary Source="pack://application:,,,/Wpf.Ui.Violeta;component/Hotfix/PasswordBox.xaml" />
122122
<ResourceDictionary Source="pack://application:,,,/Wpf.Ui.Violeta;component/Hotfix/ToggleSwitch.xaml" />
123123
<ResourceDictionary Source="pack://application:,,,/Wpf.Ui.Violeta;component/Hotfix/ToolTip.xaml" />
124-
<ResourceDictionary Source="pack://application:,,,/Wpf.Ui.Violeta;component/Hotfix/Slider.xaml" />
124+
<ResourceDictionary Source="pack://application:,,,/Wpf.Ui.Violeta;component/Controls/Slider/Slider.xaml" />
125125
<ResourceDictionary Source="pack://application:,,,/Wpf.Ui.Violeta;component/Hotfix/ContextMenu.xaml" />
126126
<ResourceDictionary Source="pack://application:,,,/Wpf.Ui.Violeta;component/Hotfix/MenuItem.xaml" />
127127
<ResourceDictionary Source="pack://application:,,,/Wpf.Ui.Violeta;component/Hotfix/ListViewItem.xaml" />

0 commit comments

Comments
 (0)