Skip to content

Commit 63a0d2d

Browse files
committed
fix tab widths on android
1 parent 18b8687 commit 63a0d2d

File tree

1 file changed

+38
-11
lines changed

1 file changed

+38
-11
lines changed

Vapolia.SegmentedViews/Platforms/Android/SegmentedViewHandler.cs

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,35 @@ protected override void DisconnectHandler(MaterialButtonToggleGroup platformView
8585
base.DisconnectHandler(platformView);
8686
}
8787

88-
// /// <summary>
89-
// /// Invokes "IView.Frame" after calling PlatformView.Layout
90-
// /// </summary>
91-
// public override void PlatformArrange(Rect frame)
92-
// {
93-
// base.PlatformArrange(frame);
94-
// }
88+
/// <summary>
89+
/// Fix tabs won't use the full container's width
90+
/// </summary>
91+
/// <remarks>
92+
/// Credits: yurkinh
93+
/// </remarks>
94+
public override void PlatformArrange(Rect frame)
95+
{
96+
var platformView = PlatformView;
97+
var virtualView = VirtualView;
98+
99+
if (frame.Width <= 0 || frame.Height <= 0 || platformView == null! || virtualView == null!)
100+
return;
101+
102+
var needsExactMeasure = (virtualView.VerticalLayoutAlignment == Microsoft.Maui.Primitives.LayoutAlignment.Fill
103+
|| virtualView.HorizontalLayoutAlignment == Microsoft.Maui.Primitives.LayoutAlignment.Fill)
104+
// If the Width and Height are both explicit, then we've already done MeasureSpecMode.Exactly in both dimensions; no need to do it again
105+
// Otherwise we're going to need a second measurement pass so TextView can properly handle alignments
106+
&& virtualView is not { Width: >= 0, Height: >= 0 };
107+
108+
// Depending on our layout situation, the TextView may need an additional measurement pass at the final size
109+
// in order to properly handle any TextAlignment properties and some internal bookkeeping
110+
if (needsExactMeasure)
111+
platformView.Measure(MakeExact(frame.Width), MakeExact(frame.Height));
112+
113+
base.PlatformArrange(frame);
114+
115+
int MakeExact(double size) => MeasureSpecMode.Exactly.MakeMeasureSpec((int)platformView.Context.ToPixels(size));
116+
}
95117

96118
void PlatformView_CheckedChange(object? sender, MaterialButtonToggleGroup.ButtonCheckedEventArgs e)
97119
{
@@ -170,8 +192,7 @@ static void ConfigureRadioButton(SegmentedViewHandler handler, ISegmentedView co
170192
rb.StrokeColor = handler.BorderColor;
171193
rb.Enabled = virtualView.IsEnabled;
172194

173-
var fontManager = handler.Services?.GetRequiredService<IFontManager>()!;
174-
rb.UpdateFont(control, fontManager);
195+
UpdateFont(rb, handler, control);
175196

176197
var padding = handler.Context.ToPixels(control.ItemPadding);
177198
rb.SetPadding((int)padding.Left, (int)padding.Top, (int)padding.Right, (int)padding.Bottom);
@@ -242,7 +263,7 @@ static void SetTextColor(MaterialButton button, ISegmentedView virtualView)
242263

243264
static void MapBorderColor(SegmentedViewHandler handler, ISegmentedView virtualView)
244265
{
245-
handler.BorderColor = new ColorStateList(
266+
handler.BorderColor = new (
246267
[
247268
[-global::Android.Resource.Attribute.StateEnabled],
248269
[],
@@ -265,7 +286,13 @@ static void MapCharacterSpacing(SegmentedViewHandler handler, ITextStyle control
265286
static void MapFont(SegmentedViewHandler handler, ITextStyle control)
266287
{
267288
var fontManager = handler.Services?.GetRequiredService<IFontManager>()!;
268-
DoForAllChildren(handler, v => v.UpdateFont(control, fontManager));
289+
DoForAllChildren(handler, v => UpdateFont(v, handler, control, fontManager));
290+
}
291+
292+
static void UpdateFont(MaterialButton button, SegmentedViewHandler handler, ITextStyle control, IFontManager? fontManager = null)
293+
{
294+
fontManager ??= handler.Services?.GetRequiredService<IFontManager>()!;
295+
button.UpdateFont(control, fontManager);
269296
}
270297

271298
private static void MapIsSelectionRequired(SegmentedViewHandler handler, ISegmentedView control)

0 commit comments

Comments
 (0)