Skip to content

Commit

Permalink
Merge branch 'main' into housekeeping/fix-codeformatting
Browse files Browse the repository at this point in the history
  • Loading branch information
jsuarezruiz authored Mar 13, 2021
2 parents 8a9936c + 64df5c5 commit b2c4a4b
Show file tree
Hide file tree
Showing 31 changed files with 431 additions and 11 deletions.
1 change: 1 addition & 0 deletions src/Compatibility/Core/src/Android/ButtonLayoutManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
UpdateLineBreakMode();
}

[PortHandler]
void UpdatePadding()
{
AButton view = View;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static void SetLineBreakMode(this TextView textView, Label label)
public static void SetLineBreakMode(this TextView textView, Button button) =>
SetLineBreak(textView, button.LineBreakMode);


[PortHandler]
public static int SetLineBreak(TextView textView, LineBreakMode lineBreakMode)
{
int maxLines = Int32.MaxValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@ void UpdateCharacterSpacing()
}
}

[PortHandler]
void UpdateLineBreakMode()
{
this.SetLineBreakMode(Element);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ async Task UpdateImageAsync()
}
}

[PortHandler]
void UpdatePadding()
{
if (_disposed || _renderer == null || _element == null)
Expand Down
1 change: 1 addition & 0 deletions src/Compatibility/Core/src/iOS/Renderers/LabelRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ void UpdateHorizontalTextAlignment()
#endif
}

[PortHandler]
void UpdateLineBreakMode()
{
#if __MOBILE__
Expand Down
25 changes: 24 additions & 1 deletion src/Controls/samples/Controls.Sample/Pages/MainPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

namespace Maui.Controls.Sample.Pages
{

public class MainPage : ContentPage, IPage
{
MainPageViewModel _viewModel;
Expand All @@ -24,6 +23,15 @@ public MainPage(MainPageViewModel viewModel)

void SetupMauiLayout()
{
const string loremIpsum =
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. " +
"Quisque ut dolor metus. Duis vel iaculis mauris, sit amet finibus mi. " +
"Etiam congue ornare risus, in facilisis libero tempor eget. " +
"Phasellus mattis mollis libero ut semper. In sit amet sapien odio. " +
"Sed interdum ullamcorper dui eu rutrum. Vestibulum non sagittis justo. " +
"Cras rutrum scelerisque elit, et porta est lobortis ac. " +
"Pellentesque eu ornare tortor. Sed bibendum a nisl at laoreet.";

var verticalStack = new VerticalStackLayout() { Spacing = 5, BackgroundColor = Color.AntiqueWhite };
var horizontalStack = new HorizontalStackLayout() { Spacing = 2, BackgroundColor = Color.CornflowerBlue };

Expand All @@ -35,10 +43,25 @@ void SetupMauiLayout()
verticalStack.Add(new Label { Text = "This should be BOLD text!", FontAttributes = FontAttributes.Bold });
verticalStack.Add(new Label { Text = "This should be a CUSTOM font!", FontFamily = "Dokdo" });
verticalStack.Add(new Label { Text = "This should have padding", Padding = new Thickness(40), BackgroundColor = Color.LightBlue });
verticalStack.Add(new Label { Text = loremIpsum });
verticalStack.Add(new Label { Text = loremIpsum, MaxLines = 2 });
verticalStack.Add(new Label { Text = loremIpsum, LineBreakMode = LineBreakMode.TailTruncation });
verticalStack.Add(new Label { Text = loremIpsum, MaxLines = 2, LineBreakMode = LineBreakMode.TailTruncation });


var paddingButton = new Button
{
Padding = new Thickness(40),
Text = "This button has a padding!!",
BackgroundColor = Color.Purple,
};

verticalStack.Add(paddingButton);

var underlineLabel = new Label { Text = "underline", TextDecorations = TextDecorations.Underline };
verticalStack.Add(underlineLabel);


var button = new Button() { Text = _viewModel.Text, WidthRequest = 200 };
var button2 = new Button()
{
Expand Down
2 changes: 1 addition & 1 deletion src/Core/src/Core/IButton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ namespace Microsoft.Maui
/// <summary>
/// Represents a View that reacts to touch events.
/// </summary>
public interface IButton : IView, IText
public interface IButton : IView, IText, IPadding
{
/// <summary>
/// Occurs when the Button is pressed.
Expand Down
10 changes: 5 additions & 5 deletions src/Core/src/Core/ILabel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ namespace Microsoft.Maui
/// <summary>
/// Represents a View that displays text.
/// </summary>
public interface ILabel : IView, IText
public interface ILabel : IView, IText, IPadding
{
/// <summary>
/// Gets the maximum number of lines allowed in the Label.
/// Gets the option for line breaking.
/// </summary>
int MaxLines { get; }
LineBreakMode LineBreakMode { get; }

/// <summary>
/// Gets the space between the text of the Label and it's border.
/// Gets the maximum number of lines allowed in the Label.
/// </summary>
Thickness Padding { get; }
int MaxLines { get; }

/// <summary>
/// Gets the text decoration applied to the Label.
Expand Down
13 changes: 13 additions & 0 deletions src/Core/src/Core/IPadding.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace Microsoft.Maui
{
/// <summary>
/// Provides functionality to be able to customize Padding.
/// </summary>
public interface IPadding
{
/// <summary>
/// Gets the Padding
/// </summary>
Thickness Padding { get; }
}
}
5 changes: 5 additions & 0 deletions src/Core/src/Handlers/Button/ButtonHandler.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ public static void MapTextColor(ButtonHandler handler, IButton button)
handler.TypedNativeView?.UpdateTextColor(button);
}

public static void MapPadding(ButtonHandler handler, IButton button)
{
handler.TypedNativeView?.UpdatePadding(button);
}

public bool OnTouch(IButton? button, AView? v, MotionEvent? e)
{
switch (e?.ActionMasked)
Expand Down
1 change: 1 addition & 0 deletions src/Core/src/Handlers/Button/ButtonHandler.Standard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ public partial class ButtonHandler : AbstractViewHandler<IButton, object>

public static void MapText(ButtonHandler handler, IButton button) { }
public static void MapTextColor(ButtonHandler handler, IButton button) { }
public static void MapPadding(ButtonHandler handler, IButton button) { }
}
}
4 changes: 2 additions & 2 deletions src/Core/src/Handlers/Button/ButtonHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ public partial class ButtonHandler
public static PropertyMapper<IButton, ButtonHandler> ButtonMapper = new PropertyMapper<IButton, ButtonHandler>(ViewHandler.ViewMapper)
{
[nameof(IButton.Text)] = MapText,
[nameof(IButton.TextColor)] = MapTextColor
[nameof(IButton.TextColor)] = MapTextColor,
[nameof(IButton.Padding)] = MapPadding,
};

public ButtonHandler() : base(ButtonMapper)
Expand All @@ -15,7 +16,6 @@ public ButtonHandler() : base(ButtonMapper)

public ButtonHandler(PropertyMapper mapper) : base(mapper ?? ButtonMapper)
{

}
}
}
4 changes: 4 additions & 0 deletions src/Core/src/Handlers/Button/ButtonHandler.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ public static void MapTextColor(ButtonHandler handler, IButton button)
{
handler.TypedNativeView?.UpdateTextColor(button, ButtonTextColorDefaultNormal, ButtonTextColorDefaultHighlighted, ButtonTextColorDefaultDisabled);
}
public static void MapPadding(ButtonHandler handler, IButton button)
{
handler.TypedNativeView?.UpdatePadding(button);
}

void SetControlPropertiesFromProxy()
{
Expand Down
5 changes: 5 additions & 0 deletions src/Core/src/Handlers/Label/LabelHandler.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ public static void MapCharacterSpacing(LabelHandler handler, ILabel label)
handler.TypedNativeView?.UpdateCharacterSpacing(label);
}

public static void MapLineBreakMode(LabelHandler handler, ILabel label)
{
handler.TypedNativeView?.UpdateLineBreakMode(label);
}

public static void MapMaxLines(LabelHandler handler, ILabel label)
{
handler.TypedNativeView?.UpdateMaxLines(label);
Expand Down
1 change: 1 addition & 0 deletions src/Core/src/Handlers/Label/LabelHandler.Standard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public static void MapText(IViewHandler handler, ILabel label) { }
public static void MapTextColor(IViewHandler handler, ILabel label) { }
public static void MapCharacterSpacing(IViewHandler handler, ILabel label) { }
public static void MapFont(LabelHandler handler, ILabel label) { }
public static void MapLineBreakMode(LabelHandler handler, ILabel label) { }
public static void MapTextDecorations(LabelHandler handler, ILabel label) { }
public static void MapMaxLines(IViewHandler handler, ILabel label) { }
public static void MapPadding(LabelHandler handler, ILabel label) { }
Expand Down
1 change: 1 addition & 0 deletions src/Core/src/Handlers/Label/LabelHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public partial class LabelHandler
[nameof(ILabel.CharacterSpacing)] = MapCharacterSpacing,
[nameof(ILabel.MaxLines)] = MapMaxLines,
[nameof(ILabel.Font)] = MapFont,
[nameof(ILabel.LineBreakMode)] = MapLineBreakMode,
[nameof(ILabel.Padding)] = MapPadding,
[nameof(ILabel.TextDecorations)] = MapTextDecorations
};
Expand Down
5 changes: 5 additions & 0 deletions src/Core/src/Handlers/Label/LabelHandler.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ public static void MapCharacterSpacing(LabelHandler handler, ILabel label)
handler.TypedNativeView?.UpdateCharacterSpacing(label);
}

public static void MapLineBreakMode(LabelHandler handler, ILabel label)
{
handler.TypedNativeView?.UpdateLineBreakMode(label);
}

public static void MapMaxLines(LabelHandler handler, ILabel label)
{
handler.TypedNativeView?.UpdateMaxLines(label);
Expand Down
17 changes: 17 additions & 0 deletions src/Core/src/Platform/Android/ButtonExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,23 @@ public static void UpdateTextColor(this AppCompatButton button, XColor color, Co
public static void UpdateTextColor(this AppCompatButton appCompatButton, IButton button, XColor defaultColor) =>
appCompatButton.SetTextColor(button.TextColor.Cleanse(defaultColor).ToNative());

public static void UpdatePadding(this AppCompatButton appCompatButton, IButton button)
{
var context = appCompatButton.Context;

if (context == null)
{
return;
}
var padding = button.Padding;

appCompatButton.SetPadding(
(int)context.ToPixels(padding.Left),
(int)context.ToPixels(padding.Top),
(int)context.ToPixels(padding.Right),
(int)context.ToPixels(padding.Bottom));
}

static XColor Cleanse(this XColor color, XColor defaultColor) => color.IsDefault ? defaultColor : color;
}
}
48 changes: 48 additions & 0 deletions src/Core/src/Platform/Android/LabelExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Android.Graphics;
using Android.Text;
using Android.Util;
using Android.Widget;

Expand Down Expand Up @@ -39,6 +40,11 @@ public static void UpdateFont(this TextView textView, ILabel label, IFontManager
textView.SetTextSize(ComplexUnitType.Sp, sp);
}

public static void UpdateLineBreakMode(this TextView textView, ILabel label)
{
textView.SetLineBreakMode(label);
}

public static void UpdateMaxLines(this TextView textView, ILabel label)
{
int maxLinex = label.MaxLines;
Expand Down Expand Up @@ -76,5 +82,47 @@ public static void UpdateTextDecorations(this TextView textView, ILabel label)
else
textView.PaintFlags |= PaintFlags.UnderlineText;
}

internal static void SetLineBreakMode(this TextView textView, ILabel label)
{
var lineBreakMode = label.LineBreakMode;

int maxLines = label.MaxLines;
if (maxLines <= 0)
maxLines = int.MaxValue;

bool singleLine = false;

switch (lineBreakMode)
{
case LineBreakMode.NoWrap:
maxLines = 1;
textView.Ellipsize = null;
break;
case LineBreakMode.WordWrap:
textView.Ellipsize = null;
break;
case LineBreakMode.CharacterWrap:
textView.Ellipsize = null;
break;
case LineBreakMode.HeadTruncation:
maxLines = 1;
singleLine = true; // Workaround for bug in older Android API versions (https://bugzilla.xamarin.com/show_bug.cgi?id=49069)
textView.Ellipsize = TextUtils.TruncateAt.Start;
break;
case LineBreakMode.TailTruncation:
maxLines = 1;
textView.Ellipsize = TextUtils.TruncateAt.End;
break;
case LineBreakMode.MiddleTruncation:
maxLines = 1;
singleLine = true; // Workaround for bug in older Android API versions (https://bugzilla.xamarin.com/show_bug.cgi?id=49069)
textView.Ellipsize = TextUtils.TruncateAt.Middle;
break;
}

textView.SetSingleLine(singleLine);
textView.SetMaxLines(maxLines);
}
}
}
10 changes: 10 additions & 0 deletions src/Core/src/Platform/iOS/ButtonExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,15 @@ public static void UpdateTextColor(this UIButton nativeButton, IButton button, U
nativeButton.TintColor = color;
}
}

public static void UpdatePadding(this UIButton nativeButton, IButton button)
{
nativeButton.ContentEdgeInsets = new UIEdgeInsets(
(float)button.Padding.Top,
(float)button.Padding.Left,
(float)button.Padding.Bottom,
(float)button.Padding.Right);
}

}
}
40 changes: 40 additions & 0 deletions src/Core/src/Platform/iOS/LabelExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ public static void UpdateFont(this UILabel nativeLabel, ILabel label, IFontManag
nativeLabel.UpdateCharacterSpacing(label);
}

public static void UpdateLineBreakMode(this UILabel nativeLabel, ILabel label)
{
SetLineBreakMode(nativeLabel, label);
}

public static void UpdateMaxLines(this UILabel nativeLabel, ILabel label)
{
int maxLines = label.MaxLines;
Expand All @@ -66,6 +71,41 @@ public static void UpdatePadding(this MauiLabel nativeLabel, ILabel label)
(float)label.Padding.Right);
}

internal static void SetLineBreakMode(this UILabel nativeLabel, ILabel label)
{
int maxLines = label.MaxLines;
if (maxLines < 0)
maxLines = 0;

switch (label.LineBreakMode)
{
case LineBreakMode.NoWrap:
nativeLabel.LineBreakMode = UILineBreakMode.Clip;
maxLines = 1;
break;
case LineBreakMode.WordWrap:
nativeLabel.LineBreakMode = UILineBreakMode.WordWrap;
break;
case LineBreakMode.CharacterWrap:
nativeLabel.LineBreakMode = UILineBreakMode.CharacterWrap;
break;
case LineBreakMode.HeadTruncation:
nativeLabel.LineBreakMode = UILineBreakMode.HeadTruncation;
maxLines = 1;
break;
case LineBreakMode.MiddleTruncation:
nativeLabel.LineBreakMode = UILineBreakMode.MiddleTruncation;
maxLines = 1;
break;
case LineBreakMode.TailTruncation:
nativeLabel.LineBreakMode = UILineBreakMode.TailTruncation;
maxLines = 1;
break;
}

nativeLabel.Lines = maxLines;
}

public static void UpdateTextDecorations(this UILabel nativeLabel, ILabel label)
{
if (nativeLabel.AttributedText != null && !(nativeLabel.AttributedText?.Length > 0))
Expand Down
Loading

0 comments on commit b2c4a4b

Please sign in to comment.