Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 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
44 changes: 40 additions & 4 deletions NiceEntry/NiceButton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Microsoft.Maui.Handlers;
using Microsoft.Maui.Layouts;
using System.Windows.Input;
using Microsoft.Maui;
Comment thread
nord- marked this conversation as resolved.
Outdated

namespace NiceEntry;

Expand Down Expand Up @@ -68,6 +69,7 @@ public override void OnInitializeAccessibilityNodeInfo(Android.Views.View host,
private Brush? _userBackgroundBrush;
private bool _commandEnabled = true;
private bool _tapInFlight;
internal bool WrapsText;

private readonly Border _border;
private readonly Grid _contentHost;
Expand Down Expand Up @@ -98,7 +100,6 @@ public NiceButton()
VerticalTextAlignment = TextAlignment.Center,
HorizontalOptions = LayoutOptions.Center,
VerticalOptions = LayoutOptions.Center,
LineBreakMode = LineBreakMode.TailTruncation,
FontSize = FontSize
};

Expand Down Expand Up @@ -197,6 +198,10 @@ private static void NeutralizeInheritedVisualStates(VisualElement label)
nameof(FontAttributes), typeof(FontAttributes), typeof(NiceButton),
FontAttributes.None, propertyChanged: FontAttributesChanged);

public static readonly BindableProperty LineBreakModeProperty = BindableProperty.Create(
nameof(LineBreakMode), typeof(Microsoft.Maui.LineBreakMode?), typeof(NiceButton), null,
propertyChanged: LayoutAffectingChanged);
Comment thread
nord- marked this conversation as resolved.
Outdated

public static readonly BindableProperty IconSizeProperty = BindableProperty.Create(
nameof(IconSize), typeof(double), typeof(NiceButton), 20.0, propertyChanged: IconSizeChanged);

Expand Down Expand Up @@ -249,6 +254,11 @@ private static void NeutralizeInheritedVisualStates(VisualElement label)
public double FontSize { get => (double)GetValue(FontSizeProperty); set => SetValue(FontSizeProperty, value); }
public string FontFamily { get => (string)GetValue(FontFamilyProperty); set => SetValue(FontFamilyProperty, value); }
public FontAttributes FontAttributes { get => (FontAttributes)GetValue(FontAttributesProperty); set => SetValue(FontAttributesProperty, value); }
public Microsoft.Maui.LineBreakMode? LineBreakMode
{
get => (Microsoft.Maui.LineBreakMode?)GetValue(LineBreakModeProperty);
set => SetValue(LineBreakModeProperty, value);
}
public double IconSize { get => (double)GetValue(IconSizeProperty); set => SetValue(IconSizeProperty, value); }
public ButtonShape ButtonShape { get => (ButtonShape)GetValue(ButtonShapeProperty); set => SetValue(ButtonShapeProperty, value); }
public double CornerRadius { get => (double)GetValue(CornerRadiusProperty); set => SetValue(CornerRadiusProperty, value); }
Expand Down Expand Up @@ -360,6 +370,15 @@ private static void ShapeChanged(BindableObject b, object o, object n)
private void UpdateFontSizeView() => _textLabel.FontSize = FontSize;
private void UpdateFontFamilyView() => _textLabel.FontFamily = FontFamily;
private void UpdateFontAttributesView() => _textLabel.FontAttributes = FontAttributes;

internal Microsoft.Maui.LineBreakMode EffectiveLineBreakMode =>
LineBreakMode ?? (Orientation == ButtonContentOrientation.Vertical
? Microsoft.Maui.LineBreakMode.WordWrap
: Microsoft.Maui.LineBreakMode.TailTruncation);

private void UpdateLineBreakModeView()
=> _textLabel.LineBreakMode = EffectiveLineBreakMode;

private void UpdateIconSizeView() => _iconLabel.FontSize = IconSize;

private void UpdateShapeView()
Expand Down Expand Up @@ -522,7 +541,16 @@ private void RebuildContent()
_iconLabel.IsVisible = hasIcon;
_textLabel.IsVisible = hasText;

if (!hasIcon && !hasText) return;
WrapsText = hasText && EffectiveLineBreakMode == Microsoft.Maui.LineBreakMode.WordWrap;

if (!hasIcon && !hasText)
{
UpdateLineBreakModeView();
return;
}
Comment thread
nord- marked this conversation as resolved.

var textColumnWidth = WrapsText ? GridLength.Star : GridLength.Auto;
_contentHost.HorizontalOptions = WrapsText ? LayoutOptions.Fill : LayoutOptions.Center;

if (hasIcon && hasText)
{
Expand All @@ -534,13 +562,17 @@ private void RebuildContent()
{
_contentHost.ColumnSpacing = Spacing;
_contentHost.RowSpacing = 0;
_contentHost.ColumnDefinitions.Add(new ColumnDefinition(GridLength.Auto));
_contentHost.ColumnDefinitions.Add(new ColumnDefinition(GridLength.Auto));
_contentHost.ColumnDefinitions.Add(
new ColumnDefinition(iconFirst ? GridLength.Auto : textColumnWidth));
_contentHost.ColumnDefinitions.Add(
new ColumnDefinition(iconFirst ? textColumnWidth : GridLength.Auto));
_contentHost.Add(first, 0, 0);
_contentHost.Add(second, 1, 0);
}
else
{
// Vertical: single column (Star for wrap, Auto otherwise), two rows
_contentHost.ColumnDefinitions.Add(new ColumnDefinition(textColumnWidth));
_contentHost.RowSpacing = Spacing;
_contentHost.ColumnSpacing = 0;
_contentHost.RowDefinitions.Add(new RowDefinition(GridLength.Auto));
Expand All @@ -551,10 +583,14 @@ private void RebuildContent()
}
else
{
if (hasText)
_contentHost.ColumnDefinitions.Add(new ColumnDefinition(textColumnWidth));

var only = hasIcon ? (View)_iconLabel : _textLabel;
_contentHost.Add(only, 0, 0);
}

UpdateLineBreakModeView();
InvalidateMeasure();
}
}
19 changes: 18 additions & 1 deletion NiceEntry/NiceButtonLayoutManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,24 @@ public Size Measure(double widthConstraint, double heightConstraint)
foreach (var child in _button)
{
if (child.Visibility == Visibility.Collapsed) continue;
var size = child.Measure(widthConstraint, heightConstraint);

Size size;
if (_button.WrapsText
&& !double.IsPositiveInfinity(widthConstraint))
{
// Pass 1: natural size — Star columns act like Auto at infinite width
var natural = child.Measure(double.PositiveInfinity, heightConstraint);
// Pass 2 only when content overflows the real constraint
size = natural.Width <= widthConstraint
? natural
: child.Measure(widthConstraint, heightConstraint);
}
else
{
// Non-wrap: unchanged — measure with real constraint
size = child.Measure(widthConstraint, heightConstraint);
}

desired = new Size(Math.Max(desired.Width, size.Width), Math.Max(desired.Height, size.Height));
}

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ Without this call, `Icon` glyphs render as empty boxes.
| Text | `FontSize` | `double` | `NiceButton.DefaultFontSize` (14.0) |
| Text | `FontFamily` | `string` | – |
| Text | `FontAttributes` | `FontAttributes` | `None` |
| Text | `LineBreakMode` | `LineBreakMode?` | `null` (auto: `WordWrap` for Vertical, `TailTruncation` for Horizontal) |
| Icon | `IconSize` | `double` | `20` |
| Shadow | `HasShadow` | `bool` | `false` |
| Shadow | `CustomShadow` | `Shadow` | – |
Expand Down
Loading
Loading