Skip to content

Commit

Permalink
feat(NumberBox): Header and HeaderTemplate support
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinZikmund committed Nov 9, 2020
1 parent b0e942a commit 6e14000
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:contract5Present="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:xamarin="http://uno.ui/xamarin"
mc:Ignorable="d xamarin"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">

<xamarin:Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
Expand Down Expand Up @@ -167,6 +166,6 @@
</StackPanel>
</Grid>

</xamarin:Grid>
</Grid>

</UserControl>
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,9 @@ public sealed partial class MUX_Test : UserControl
public MUX_Test()
{
this.InitializeComponent();
#if HAS_UNO
TestNumberBox.RegisterPropertyChangedCallback(NumberBox.TextProperty, new DependencyPropertyChangedCallback(TextPropertyChanged));
#endif
}

#if HAS_UNO
private void SpinMode_Changed(object sender, RoutedEventArgs e)
{
if (TestNumberBox != null)
Expand Down Expand Up @@ -130,6 +127,5 @@ private void TextPropertyChanged(DependencyObject o, DependencyProperty p)
{
TextTextBox.Text = TestNumberBox.Text;
}
#endif
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public object Header
}

public static DependencyProperty HeaderProperty { get ; } =
DependencyProperty.Register(nameof(Header), typeof(object), typeof(NumberBox), new FrameworkPropertyMetadata(null));
DependencyProperty.Register(nameof(Header), typeof(object), typeof(NumberBox), new FrameworkPropertyMetadata(null, (s, e) => (s as NumberBox)?.OnHeaderPropertyChanged(e)));

public DataTemplate HeaderTemplate
{
Expand All @@ -99,7 +99,7 @@ public DataTemplate HeaderTemplate
}

public static DependencyProperty HeaderTemplateProperty { get; } =
DependencyProperty.Register(nameof(HeaderTemplate), typeof(DataTemplate), typeof(NumberBox), new FrameworkPropertyMetadata(null, options: FrameworkPropertyMetadataOptions.ValueDoesNotInheritDataContext));
DependencyProperty.Register(nameof(HeaderTemplate), typeof(DataTemplate), typeof(NumberBox), new FrameworkPropertyMetadata(null, options: FrameworkPropertyMetadataOptions.ValueDoesNotInheritDataContext, (s, e) => (s as NumberBox)?.OnHeaderTemplatePropertyChanged(e)));

public string PlaceholderText
{
Expand Down
72 changes: 66 additions & 6 deletions src/Uno.UI/Microsoft/UI/Xaml/Controls/NumberBox/NumberBox.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Globalization;
using System.Linq;
using Uno.Disposables;
using Uno.UI.Helpers.WinUI;
using Windows.Foundation.Metadata;
using Windows.Globalization.NumberFormatting;
using Windows.System;
using Windows.UI.Xaml;
Expand All @@ -11,10 +13,6 @@
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.Foundation.Metadata;
using System.Globalization;
using Uno.Disposables;

namespace Microsoft.UI.Xaml.Controls
{
Expand All @@ -28,6 +26,8 @@ public partial class NumberBox : Control
TextBox m_textBox;
Windows.UI.Xaml.Controls.Primitives.Popup m_popup;

private ContentPresenter m_headerPresenter;

SerialDisposable _eventSubscriptions = new SerialDisposable();

static string c_numberBoxDownButtonName = "DownSpinButton";
Expand All @@ -37,6 +37,8 @@ public partial class NumberBox : Control
static string c_numberBoxPopupName = "UpDownPopup";
static string c_numberBoxPopupDownButtonName = "PopupDownSpinButton";
static string c_numberBoxPopupUpButtonName = "PopupUpSpinButton";

static string c_numberBoxHeaderName = "HeaderContentPresenter";
// UNO TODO static string c_numberBoxPopupContentRootName= "PopupContentRoot";

// UNO TODO static double c_popupShadowDepth = 16.0;
Expand Down Expand Up @@ -283,6 +285,16 @@ private void UpdateValueToText()
}
}

private void OnHeaderPropertyChanged(DependencyPropertyChangedEventArgs args)
{
UpdateHeaderPresenterState();
}

private void OnHeaderTemplatePropertyChanged(DependencyPropertyChangedEventArgs args)
{
UpdateHeaderPresenterState();
}

private void OnValidationModePropertyChanged(DependencyPropertyChangedEventArgs args)
{
ValidateInput();
Expand Down Expand Up @@ -598,5 +610,53 @@ private bool IsInBounds(double value)
{
return (value >= Minimum && value <= Maximum);
}

private void UpdateHeaderPresenterState()
{
bool shouldShowHeader = false;

// Load header presenter as late as possible

// To enable lightweight styling, collapse header presenter if there is no header specified
var header = Header;
if (header != null)
{
// Check if header is string or not
var headerAsString = header as string;
if (headerAsString != null)
{
if (headerAsString != string.Empty)
{
// Header is not empty string
shouldShowHeader = true;
}
}
else
{
// Header is not a string, so let's show header presenter
shouldShowHeader = true;
}
}
var headerTemplate = HeaderTemplate;
if (headerTemplate != null)
{
shouldShowHeader = true;
}

if (shouldShowHeader && m_headerPresenter == null)
{
var headerPresenter = GetTemplateChild<ContentPresenter>(c_numberBoxHeaderName);
if (headerPresenter != null)
{
// Set presenter to enable lightweight styling of the headers margin
m_headerPresenter = headerPresenter;
}
}

if (m_headerPresenter != null)
{
m_headerPresenter.Visibility = shouldShowHeader ? Visibility.Visible : Visibility.Collapsed;
}
}
}
}

0 comments on commit 6e14000

Please sign in to comment.