Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion MaterialDesignThemes.UITests/WPF/TextBoxes/TextBoxTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace MaterialDesignThemes.UITests.WPF.TextBoxes
{
public class TextBoxTests : TestBase
{
public TextBoxTests(ITestOutputHelper output)
public TextBoxTests(ITestOutputHelper output)
: base(output)
{
}
Expand Down Expand Up @@ -283,5 +283,27 @@ public async Task HelperText_CanSetFontColorWithAttachedStyle()

recorder.Success();
}

[Fact]
[Description("Issue 2362")]
public async Task FloatingOffset_ValuesGetApproprietlyApplied()
{
await using var recorder = new TestRecorder(App);

var textBox = await LoadXaml<TextBox>(@"
<TextBox Style=""{StaticResource MaterialDesignFloatingHintTextBox}""
materialDesign:HintAssist.Hint=""Hint with offset""
materialDesign:HintAssist.FloatingOffset=""1,-42""
Margin=""100"" VerticalAlignment=""Center""
Text=""Something"" />
");
var hint = await textBox.GetElement<SmartHint>("Hint");
Point offset = await hint.GetFloatingOffset();

Assert.Equal(1, offset.X);
Assert.Equal(-42, offset.Y);

recorder.Success();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,25 @@ namespace MaterialDesignThemes.Wpf.Converters
{
internal class FloatingHintOffsetCalculationConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object? parameter, CultureInfo culture)
public object Convert(object?[] values, Type targetType, object? parameter, CultureInfo culture)
{
var fontFamily = (FontFamily)values[0];
double fontSize = (double)values[1];
double floatingScale = (double)values[2];
if (values.Length > 3 &&
values[3] is Point floatingOffset &&
floatingOffset != HintAssist.DefaultFloatingOffset)
{
return floatingOffset;
}

var fontFamily = (FontFamily)values[0]!;
double fontSize = (double)values[1]!;
double floatingScale = (double)values[2]!;

double hintHeight = fontFamily.LineSpacing * fontSize;
double floatingHintHeight = hintHeight * floatingScale;

double offset = (values.Length > 3 ? values[3] : null) switch
double offset = (values.Length > 4 ? values[4] : null) switch
{
Thickness padding => floatingHintHeight / 2 + padding.Top,
Thickness padding => (floatingHintHeight / 2) + padding.Top,
double parentHeight => (parentHeight - hintHeight + floatingHintHeight) / 2,
_ => floatingHintHeight
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Diagnostics;
using System.Globalization;
using System.Windows;
using System.Windows.Data;
Expand All @@ -18,15 +17,18 @@ public object Convert(object value, Type targetType, object parameter, CultureIn
? new Thickness(Constants.TextBoxInnerButtonSpacing, padding.Top, padding.Right, padding.Bottom)
: Binding.DoNothing;

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => throw new NotSupportedException();
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
=> throw new NotSupportedException();

/// <summary>
/// Adds the width of the inner picker button to the right of inner padding
/// </summary>
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (!(values[0] is Thickness padding))
if (values[0] is not Thickness padding)
{
return Binding.DoNothing;
}

switch (values.Length)
{
Expand All @@ -40,13 +42,16 @@ public object Convert(object[] values, Type targetType, object parameter, Cultur

// calculate padding for picker-button (considering floating hint offset)
case 4:
var offsetResult = _offsetCalculation.Convert(new[] { values[1], values[2], values[3] }, typeof(Thickness), null, culture);
var offsetResult = _offsetCalculation.Convert(new[] { values[1], values[2], values[3], null }, typeof(Thickness), null, culture);
if (offsetResult is Thickness offset)
{
return new Thickness(
padding.Left + offset.Left,
padding.Top + offset.Top,
padding.Right + offset.Right,
padding.Bottom + offset.Bottom);
}

break;
}
return Binding.DoNothing;
Expand Down
2 changes: 1 addition & 1 deletion MaterialDesignThemes.Wpf/HintAssist.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public static class HintAssist
{
private const double DefaultFloatingScale = 0.74;
private const double DefaultHintOpacity = 0.56;
private static readonly Point DefaultFloatingOffset = new Point(0, -16);
internal static readonly Point DefaultFloatingOffset = new(0, -16);
private static readonly Brush DefaultBackground = new SolidColorBrush(Colors.Transparent);
private static readonly double DefaultHelperTextFontSize = 10;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,7 @@
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="FontFamily" />
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="FontSize" />
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="(wpf:HintAssist.FloatingScale)" />
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="(wpf:HintAssist.FloatingOffset)" />
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="ActualHeight" />
</MultiBinding>
</Setter.Value>
Expand Down Expand Up @@ -659,6 +660,7 @@
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="FontFamily" />
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="FontSize" />
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="(wpf:HintAssist.FloatingScale)" />
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="(wpf:HintAssist.FloatingOffset)" />
</MultiBinding>
</Setter.Value>
</Setter>
Expand All @@ -668,6 +670,7 @@
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="FontFamily" />
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="FontSize" />
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="(wpf:HintAssist.FloatingScale)" />
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="(wpf:HintAssist.FloatingOffset)" />
</MultiBinding>
</Setter.Value>
</Setter>
Expand Down Expand Up @@ -1012,7 +1015,6 @@
<Style x:Key="MaterialDesignFilledComboBox" TargetType="{x:Type ComboBox}" BasedOn="{StaticResource MaterialDesignFloatingHintComboBox}">
<Setter Property="wpf:TextFieldAssist.HasFilledTextField" Value="True" />
<Setter Property="wpf:TextFieldAssist.TextFieldCornerRadius" Value="4,4,0,0" />
<Setter Property="wpf:HintAssist.FloatingOffset" Value="0,-8" />
<Setter Property="wpf:ComboBoxAssist.ShowSelectedItem" Value="False" />
<Setter Property="VerticalContentAlignment" Value="Stretch" />
<Setter Property="VerticalAlignment" Value="Stretch" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="FontFamily" />
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="FontSize" />
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="(wpf:HintAssist.FloatingScale)" />
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="(wpf:HintAssist.FloatingOffset)" />
</MultiBinding>
</Setter.Value>
</Setter>
Expand All @@ -249,6 +250,7 @@
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="FontFamily" />
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="FontSize" />
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="(wpf:HintAssist.FloatingScale)" />
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="(wpf:HintAssist.FloatingOffset)" />
</MultiBinding>
</Setter.Value>
</Setter>
Expand All @@ -272,6 +274,7 @@
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="FontFamily" />
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="FontSize" />
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="(wpf:HintAssist.FloatingScale)" />
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="(wpf:HintAssist.FloatingOffset)" />
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="ActualHeight" />
</MultiBinding>
</Setter.Value>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="FontFamily" />
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="FontSize" />
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="(wpf:HintAssist.FloatingScale)" />
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="(wpf:HintAssist.FloatingOffset)" />
</MultiBinding>
</Setter.Value>
</Setter>
Expand All @@ -218,6 +219,7 @@
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="FontFamily" />
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="FontSize" />
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="(wpf:HintAssist.FloatingScale)" />
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="(wpf:HintAssist.FloatingOffset)" />
</MultiBinding>
</Setter.Value>
</Setter>
Expand All @@ -243,6 +245,7 @@
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="FontFamily" />
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="FontSize" />
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="(wpf:HintAssist.FloatingScale)" />
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="(wpf:HintAssist.FloatingOffset)" />
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="ActualHeight" />
</MultiBinding>
</Setter.Value>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="FontFamily" />
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="FontSize" />
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="(wpf:HintAssist.FloatingScale)" />
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="(wpf:HintAssist.FloatingOffset)" />
</MultiBinding>
</Setter.Value>
</Setter>
Expand All @@ -295,6 +296,7 @@
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="FontFamily" />
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="FontSize" />
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="(wpf:HintAssist.FloatingScale)" />
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="(wpf:HintAssist.FloatingOffset)" />
</MultiBinding>
</Setter.Value>
</Setter>
Expand All @@ -321,6 +323,7 @@
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="FontFamily" />
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="FontSize" />
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="(wpf:HintAssist.FloatingScale)" />
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="(wpf:HintAssist.FloatingOffset)" />
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="Padding" />
</MultiBinding>
</Setter.Value>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="FontFamily" />
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="FontSize" />
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="(wpf:HintAssist.FloatingScale)" />
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="(wpf:HintAssist.FloatingOffset)" />
</MultiBinding>
</Setter.Value>
</Setter>
Expand All @@ -247,6 +248,7 @@
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="FontFamily" />
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="FontSize" />
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="(wpf:HintAssist.FloatingScale)" />
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="(wpf:HintAssist.FloatingOffset)" />
</MultiBinding>
</Setter.Value>
</Setter>
Expand All @@ -270,6 +272,7 @@
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="FontFamily" />
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="FontSize" />
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="(wpf:HintAssist.FloatingScale)" />
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="(wpf:HintAssist.FloatingOffset)" />
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="ActualHeight" />
</MultiBinding>
</Setter.Value>
Expand Down