Add the LabelHandler for WinUI (#655)

* Add the LabelHandler for WinUI

* Windows is beautiful

* ws

* my bad. it does work.

* Revert "my bad. it does work."

This reverts commit 3c985b530dc2a84d53ef7900e7d7db715fb03a18.

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

This reverts commit 4b4e0b3be1c5b19255b0ffccad87c11b25ecb0a3.

* 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 <shneuvil@microsoft.com>
This commit is contained in:
Matthew Leibowitz 2021-04-06 17:56:33 +02:00 коммит произвёл GitHub
Родитель 5a2aa3ae5d
Коммит 6a71d58824
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
7 изменённых файлов: 104 добавлений и 18 удалений

Просмотреть файл

@ -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");
});

Просмотреть файл

@ -12,13 +12,13 @@ namespace Microsoft.Maui.Controls.Compatibility
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),

Просмотреть файл

@ -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) { }
}
}

Просмотреть файл

@ -18,9 +18,6 @@ namespace Microsoft.Maui
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

Просмотреть файл

@ -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);
}
}

Просмотреть файл

@ -12,9 +12,16 @@ namespace Microsoft.Maui
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());
}
}

Просмотреть файл

@ -9,10 +9,42 @@ namespace Microsoft.Maui
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);
}
}
}