Skip to content

Commit

Permalink
Add the LabelHandler for WinUI (dotnet#655)
Browse files Browse the repository at this point in the history
* Add the LabelHandler for WinUI

* Windows is beautiful

* ws

* my bad. it does work.

* Revert "my bad. it does work."

This reverts commit 3c985b5.

* Revert "Revert "my bad. it does work.""

This reverts commit 4b4e0b3.

* not this now

* Don't remove all the xaml on non-Windows

Android and iOS are non-Windows and thus they lose all XAML files. THis only came in with WinUI and is only noticeable since we started using the App.xaml as the main entry point.

* - add build step to winui cake

* Moving toys

Co-authored-by: Shane Neuville <[email protected]>
  • Loading branch information
mattleibow and PureWeen committed Apr 6, 2021
1 parent 5a2aa3a commit 6a71d58
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 18 deletions.
7 changes: 7 additions & 0 deletions build.cake
Original file line number Diff line number Diff line change
Expand Up @@ -901,6 +901,13 @@ Task("VS-WINUI")
DotNetCoreBuild("./src/DotNet/Dotnet.csproj");
var ext = IsRunningOnWindows() ? ".exe" : "";
DotNetCoreBuild("./Microsoft.Maui.BuildTasks-net6.sln", new DotNetCoreBuildSettings { ToolPath = $"./bin/dotnet/dotnet{ext}" });
MSBuild("Microsoft.Maui.WinUI.sln",
GetMSBuildSettings()
.WithRestore()
.WithProperty("MauiPlatforms", "net6.0-windows10.0.19041.0")
);
StartVisualStudioForDotNet6("./Microsoft.Maui.WinUI.sln");
});

Expand Down
2 changes: 1 addition & 1 deletion src/Compatibility/Core/src/AppHostBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ public static class AppHostBuilderExtensions
typeof(Button),
typeof(ContentPage),
typeof(Page),
typeof(Label),
#if !WINDOWS
typeof(ActivityIndicator),
typeof(CheckBox),
typeof(DatePicker),
typeof(Editor),
typeof(Entry),
typeof(Label),
typeof(Picker),
typeof(ProgressBar),
typeof(SearchBar),
Expand Down
43 changes: 34 additions & 9 deletions src/Core/src/Handlers/Label/LabelHandler.Windows.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,48 @@
using System;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;

namespace Microsoft.Maui.Handlers
{
public partial class LabelHandler : AbstractViewHandler<ILabel, TextBlock>
public partial class LabelHandler : AbstractViewHandler<ILabel, FrameworkElement>
{
protected override TextBlock CreateNativeView() => new TextBlock();
protected TextBlock? TextBlock { get; set; }

public static void MapText(IViewHandler handler, ILabel label) =>
(handler as LabelHandler)?.TypedNativeView?.UpdateText(label);
protected override FrameworkElement CreateNativeView()
{
TextBlock = new TextBlock();
return new Border { Child = TextBlock };
}

public static void MapText(LabelHandler handler, ILabel label) =>
handler.TextBlock?.UpdateText(label);

public static void MapTextColor(LabelHandler handler, ILabel label) =>
handler.TextBlock?.UpdateTextColor(label);

public static void MapCharacterSpacing(LabelHandler handler, ILabel label) { }

public static void MapFont(LabelHandler handler, ILabel label)
{
_ = handler.Services ?? throw new InvalidOperationException($"{nameof(Services)} should have been set by base class.");

var fontManager = handler.Services.GetRequiredService<IFontManager>();

handler.TextBlock?.UpdateFont(label, fontManager);
}

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 MapHorizontalTextAlignment(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) { }

public static void MapMaxLines(LabelHandler handler, ILabel label) { }

public static void MapPadding(LabelHandler handler, ILabel label) =>
handler.TextBlock?.UpdatePadding(label);

public static void MapLineHeight(LabelHandler handler, ILabel label) { }
}
}
3 changes: 0 additions & 3 deletions src/Core/src/Platform/Windows/ControlExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ public static void UpdateIsEnabled(this Control nativeControl, bool isEnabled) =
public static void UpdateForegroundColor(this Control nativeControl, Color color, UI.Xaml.Media.Brush? defaultBrush = null) =>
nativeControl.Foreground = color.IsDefault && defaultBrush != null ? defaultBrush : color.ToNative();

public static void UpdateBackgroundColor(this Control nativeControl, Color color, UI.Xaml.Media.Brush? defaultBrush = null) =>
nativeControl.Background = color.IsDefault && defaultBrush != null ? defaultBrush : color.ToNative();

public static void UpdatePadding(this Control nativeControl, Thickness padding, UI.Xaml.Thickness? defaultThickness = null)
{
// TODO: have a way to reset the padding
Expand Down
18 changes: 18 additions & 0 deletions src/Core/src/Platform/Windows/PrimitiveExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using WPoint = Windows.Foundation.Point;
using WThickness = Microsoft.UI.Xaml.Thickness;

namespace Microsoft.Maui
{
public static class PrimitiveExtensions
{
public static WPoint ToNative(this Point point) =>
new WPoint(point.X, point.Y);

public static WThickness ToNative(this Thickness thickness) =>
new WThickness(
thickness.Left,
thickness.Top,
thickness.Right,
thickness.Bottom);
}
}
13 changes: 10 additions & 3 deletions src/Core/src/Platform/Windows/TextBlockExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,16 @@ public static void UpdateFont(this TextBlock nativeControl, Font font, IFontMana
nativeControl.FontWeight = font.FontAttributes.ToFontWeight();
}

public static void UpdateText(this TextBlock nativeControl, IText text)
{
public static void UpdateFont(this TextBlock nativeControl, IText text, IFontManager fontManager) =>
nativeControl.UpdateFont(text.Font, fontManager);

public static void UpdateText(this TextBlock nativeControl, IText text) =>
nativeControl.Text = text.Text;
}

public static void UpdateTextColor(this TextBlock nativeControl, IText text) =>
nativeControl.UpdateProperty(TextBlock.ForegroundProperty, text.TextColor);

public static void UpdatePadding(this TextBlock nativeControl, ILabel label) =>
nativeControl.UpdateProperty(TextBlock.PaddingProperty, label.Padding.ToNative());
}
}
36 changes: 34 additions & 2 deletions src/Core/src/Platform/Windows/ViewExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,42 @@ public static class ViewExtensions
public static void UpdateIsEnabled(this FrameworkElement nativeView, IView view) =>
(nativeView as Control)?.UpdateIsEnabled(view.IsEnabled);

public static void UpdateBackgroundColor(this FrameworkElement nativeView, IView view) =>
(nativeView as Control)?.UpdateBackgroundColor(view.BackgroundColor);
public static void UpdateBackgroundColor(this FrameworkElement nativeView, IView view)
{
if (nativeView is Control control)
control.UpdateBackgroundColor(view.BackgroundColor);
else if (nativeView is Border border)
border.UpdateBackgroundColor(view.BackgroundColor);
else if (nativeView is Panel panel)
panel.UpdateBackgroundColor(view.BackgroundColor);
}

public static void UpdateBackgroundColor(this Control nativeControl, Color color, UI.Xaml.Media.Brush? defaultBrush = null) =>
nativeControl.Background = color.IsDefault && defaultBrush != null ? defaultBrush : color.ToNative();

public static void UpdateBackgroundColor(this Border nativeControl, Color color, UI.Xaml.Media.Brush? defaultBrush = null) =>
nativeControl.Background = color.IsDefault && defaultBrush != null ? defaultBrush : color.ToNative();

public static void UpdateBackgroundColor(this Panel nativeControl, Color color, UI.Xaml.Media.Brush? defaultBrush = null) =>
nativeControl.Background = color.IsDefault && defaultBrush != null ? defaultBrush : color.ToNative();

public static void UpdateAutomationId(this FrameworkElement nativeView, IView view) =>
AutomationProperties.SetAutomationId(nativeView, view.AutomationId);

internal static void UpdateProperty(this FrameworkElement nativeControl, DependencyProperty property, Color color)
{
if (color.IsDefault)
nativeControl.ClearValue(property);
else
nativeControl.SetValue(property, color.ToNative());
}

internal static void UpdateProperty(this FrameworkElement nativeControl, DependencyProperty property, object? value)
{
if (value == null)
nativeControl.ClearValue(property);
else
nativeControl.SetValue(property, value);
}
}
}

0 comments on commit 6a71d58

Please sign in to comment.