Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Tizen] Add Label TextType #11103

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
22 changes: 19 additions & 3 deletions src/Controls/src/Core/Handlers/Shell/Tizen/ShellView.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#nullable enable

using System;
using System.Collections.Generic;
using Microsoft.Maui.Controls.Handlers;
using Microsoft.Maui.Controls.Handlers.Items;
using Microsoft.Maui.Graphics;
Expand Down Expand Up @@ -34,6 +33,8 @@ public class ShellView : ViewGroup, IAppearanceObserver, IFlyoutBehaviorObserver

ShellItemHandler? _currentItemHandler;

WrapperView? _backdropView;

bool _isOpen;

protected Shell? Element { get; set; }
Expand All @@ -44,7 +45,9 @@ public class ShellView : ViewGroup, IAppearanceObserver, IFlyoutBehaviorObserver

protected bool HeaderOnMenu => _headerBehavior == FlyoutHeaderBehavior.Scroll || _headerBehavior == FlyoutHeaderBehavior.CollapseOnScroll;

protected NColor DefaultBackgroundCorlor = NColor.White;
public readonly NColor DefaultBackgroundColor = NColor.White;

public readonly NColor DefaultBackdropColor = new NColor(0.1f, 0.1f, 0.1f, 0.5f);

public event EventHandler? Toggled;

Expand Down Expand Up @@ -123,7 +126,7 @@ public void UpdateFlyout(IView? flyout)

public void UpdateBackgroundColor(GColor? color)
{
_navigationView.BackgroundColor = color?.ToNUIColor() ?? DefaultBackgroundCorlor;
_navigationView.BackgroundColor = color?.ToNUIColor() ?? DefaultBackgroundColor;
}

public void UpdateCurrentItem(ShellItem newItem, bool animate = true)
Expand Down Expand Up @@ -200,6 +203,19 @@ public void UpdateItems()

public void UpdateFlyoutBackDrop(Brush backdrop)
{
if (_backdropView == null)
{
_backdropView = new WrapperView()
{
WidthSpecification = LayoutParamPolicies.MatchParent,
HeightSpecification = LayoutParamPolicies.MatchParent,
BackgroundColor = DefaultBackdropColor
};
_navigationDrawer.Backdrop = _backdropView;
}

if (!backdrop.IsEmpty)
_backdropView.UpdateBackground(backdrop);
}

public void SetToolbar(MauiToolbar toolbar)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using Microsoft.Maui.Controls.Internals;
using TFormattedString = Tizen.UIExtensions.Common.FormattedString;
using TSpan = Tizen.UIExtensions.Common.Span;

namespace Microsoft.Maui.Controls.Platform
{
public static class FormattedStringExtensions
{
public static TFormattedString ToFormattedString(this Label label)
=> ToFormattedText(
label.FormattedText,
label.TextColor,
label.RequireFontManager(),
label.ToFont(),
label.TextTransform,
label.TextDecorations);

internal static TFormattedString ToFormattedText(
this FormattedString formattedString,
Graphics.Color defaultColor,
IFontManager fontManager,
Font? defaultFont = null,
TextTransform defaultTextTransform = TextTransform.Default,
TextDecorations defaultTextDecorations = TextDecorations.None)
{
if (formattedString == null)
return new TFormattedString();

var defaultFontSize = defaultFont?.Size ?? fontManager.DefaultFontSize;
var formattedText = new TFormattedString();

for (int i = 0; i < formattedString.Spans.Count; i++)
{
Span span = formattedString.Spans[i];
var transform = span.TextTransform != TextTransform.Default ? span.TextTransform : defaultTextTransform;
var text = TextTransformUtilites.GetTransformedText(span.Text, transform);

if (text == null)
continue;

var nativeSpan = new TSpan() { Text = text };
var textColor = span.TextColor ?? defaultColor;

if (textColor is not null)
nativeSpan.ForegroundColor = textColor.ToPlatform();

if (span.BackgroundColor is not null)
nativeSpan.BackgroundColor = span.BackgroundColor.ToPlatform();

var font = span.ToFont(defaultFontSize);
if (font.IsDefault && defaultFont.HasValue)
font = defaultFont.Value;

if (!font.IsDefault)
{
nativeSpan.FontSize = font.Size.ToScaledPoint();
nativeSpan.FontFamily = fontManager.GetFontFamily(span.FontFamily);
}

nativeSpan.LineHeight = span.LineHeight;

var textDecorations = span.IsSet(Span.TextDecorationsProperty)
? span.TextDecorations
: defaultTextDecorations;
if (textDecorations.HasFlag(TextDecorations.Strikethrough) || textDecorations.HasFlag(TextDecorations.Underline))
nativeSpan.TextDecorations = span.TextDecorations.ToPlatform();

formattedText.Spans.Add(nativeSpan);
}
return formattedText;
}
}
}
33 changes: 26 additions & 7 deletions src/Controls/src/Core/Platform/Tizen/Extensions/TextExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Maui.Platform;
using Microsoft.Maui.Controls.Internals;
using Microsoft.Maui.Controls.Internals;
using Tizen.UIExtensions.NUI;
using TEntry = Tizen.UIExtensions.NUI.Entry;
using TEditor = Tizen.UIExtensions.NUI.Editor;
using TLabel = Tizen.UIExtensions.NUI.Label;
using TFormattedString = Tizen.UIExtensions.Common.FormattedString;
using TSpan = Tizen.UIExtensions.Common.Span;

namespace Microsoft.Maui.Controls.Platform
{
Expand All @@ -22,7 +21,7 @@ public static void UpdateLineBreakMode(this TLabel platformLabel, Label label)
{
platformLabel.LineBreakMode = label.LineBreakMode.ToPlatform();
}

public static void UpdateText(this TEditor editor, InputView inputView)
{
var text = TextTransformUtilites.GetTransformedText(inputView.Text, inputView.TextTransform);
Expand All @@ -32,7 +31,27 @@ public static void UpdateText(this TEditor editor, InputView inputView)

public static void UpdateText(this TLabel platformLabel, Label label)
{
platformLabel.Text = TextTransformUtilites.GetTransformedText(label.Text, label.TextTransform);
switch (label.TextType)
{
case TextType.Text:
if (label.FormattedText != null)
platformLabel.FormattedText = label.ToFormattedString();
else
platformLabel.Text = TextTransformUtilites.GetTransformedText(label.Text, label.TextTransform);
break;
case TextType.Html:
platformLabel.UpdateTextHtml(label);
break;
}
}

static void UpdateTextHtml(this TLabel platformLabel, Label label)
{
var formattedText = new TFormattedString();
var htmlSpan = new TSpan() { Text = label.Text };
formattedText.Spans.Add(htmlSpan);
platformLabel.EnableMarkup = true;
platformLabel.Text = formattedText.ToMarkupText();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1619,6 +1619,7 @@ Microsoft.Maui.Controls.Platform.ElementChangedEventArgs<TElement>.ElementChange
Microsoft.Maui.Controls.Platform.ElementChangedEventArgs<TElement>.NewElement.get -> TElement?
Microsoft.Maui.Controls.Platform.ElementChangedEventArgs<TElement>.OldElement.get -> TElement?
Microsoft.Maui.Controls.Platform.FontExtensions
Microsoft.Maui.Controls.Platform.FormattedStringExtensions
Microsoft.Maui.Controls.Platform.GestureHandler
Microsoft.Maui.Controls.Platform.GestureHandler.Attach(Microsoft.Maui.IViewHandler! handler) -> void
Microsoft.Maui.Controls.Platform.GestureHandler.Detach() -> void
Expand Down Expand Up @@ -1695,7 +1696,6 @@ Microsoft.Maui.Controls.Platform.ShellSectionView.MauiContext.set -> void
Microsoft.Maui.Controls.Platform.ShellSectionView.ShellSection.get -> Microsoft.Maui.Controls.ShellSection!
Microsoft.Maui.Controls.Platform.ShellSectionView.ShellSectionView(Microsoft.Maui.Controls.ShellSection! section, Microsoft.Maui.IMauiContext! context) -> void
Microsoft.Maui.Controls.Platform.ShellView
Microsoft.Maui.Controls.Platform.ShellView.DefaultBackgroundCorlor -> Tizen.NUI.Color!
Microsoft.Maui.Controls.Platform.ShellView.Element.get -> Microsoft.Maui.Controls.Shell?
Microsoft.Maui.Controls.Platform.ShellView.Element.set -> void
Microsoft.Maui.Controls.Platform.ShellView.HeaderOnMenu.get -> bool
Expand Down Expand Up @@ -3153,6 +3153,8 @@ override sealed Microsoft.Maui.Controls.PlatformBehavior<TView, TPlatformView>.O
override sealed Microsoft.Maui.Controls.PlatformBehavior<TView, TPlatformView>.OnAttachedTo(TView! bindable) -> void
override sealed Microsoft.Maui.Controls.PlatformBehavior<TView, TPlatformView>.OnDetachingFrom(Microsoft.Maui.Controls.BindableObject! bindable) -> void
override sealed Microsoft.Maui.Controls.PlatformBehavior<TView, TPlatformView>.OnDetachingFrom(TView! bindable) -> void
readonly Microsoft.Maui.Controls.Platform.ShellView.DefaultBackdropColor -> Tizen.NUI.Color!
readonly Microsoft.Maui.Controls.Platform.ShellView.DefaultBackgroundColor -> Tizen.NUI.Color!
static Microsoft.Maui.Controls.AbsoluteLayout.AutoSize -> double
static Microsoft.Maui.Controls.Application.AccentColor.get -> Microsoft.Maui.Graphics.Color?
static Microsoft.Maui.Controls.Application.AccentColor.set -> void
Expand Down Expand Up @@ -5775,6 +5777,7 @@ static Microsoft.Maui.Controls.Handlers.ShellHandler.MapItems(Microsoft.Maui.Con
~static Microsoft.Maui.Controls.Platform.ButtonExtensions.UpdateText(this Tizen.UIExtensions.NUI.Button platformButton, Microsoft.Maui.Controls.Button button) -> void
~static Microsoft.Maui.Controls.Platform.CollectionViewExtensions.ToLayoutManager(this Microsoft.Maui.Controls.IItemsLayout layout, Microsoft.Maui.Controls.ItemSizingStrategy sizing = Microsoft.Maui.Controls.ItemSizingStrategy.MeasureFirstItem) -> Tizen.UIExtensions.NUI.ICollectionViewLayoutManager
~static Microsoft.Maui.Controls.Platform.FontExtensions.ToNativeFontFamily(this string self, Microsoft.Maui.IFontManager fontManager) -> string
~static Microsoft.Maui.Controls.Platform.FormattedStringExtensions.ToFormattedString(this Microsoft.Maui.Controls.Label label) -> Tizen.UIExtensions.Common.FormattedString
~static Microsoft.Maui.Controls.Platform.ImageExtensions.LoadImage(this Tizen.NUI.BaseComponents.ImageView image, Microsoft.Maui.Controls.ImageSource source) -> System.Threading.Tasks.Task
~static Microsoft.Maui.Controls.Platform.ImageExtensions.LoadImageAsync(this Tizen.NUI.BaseComponents.ImageView image, Microsoft.Maui.Controls.FileImageSource imageSource) -> System.Threading.Tasks.Task<bool>
~static Microsoft.Maui.Controls.Platform.ImageExtensions.LoadImageAsync(this Tizen.NUI.BaseComponents.ImageView image, Microsoft.Maui.Controls.StreamImageSource imageSource) -> System.Threading.Tasks.Task<bool>
Expand Down
12 changes: 12 additions & 0 deletions src/Core/src/Fonts/EmbeddedFontLoader.Tizen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ public partial class EmbeddedFontLoader : IEmbeddedFontLoader
/// <inheritdoc/>
public string? LoadFont(EmbeddedFont font)
{
var fontResourcePath = IOPath.Combine(TApplication.Current.DirectoryInfo.Resource, _fontCacheFolderName);
var fontResourceFilePath = IOPath.Combine(fontResourcePath, font.FontName!);
if (File.Exists(fontResourceFilePath))
{
return IOPath.GetFileNameWithoutExtension(fontResourceFilePath);
}

if (FontCacheDirectory == null)
{
FontCacheDirectory = Directory.CreateDirectory(IOPath.Combine(TApplication.Current.DirectoryInfo.Data, _fontCacheFolderName));
Expand All @@ -28,7 +35,10 @@ public partial class EmbeddedFontLoader : IEmbeddedFontLoader
var filePath = IOPath.Combine(FontCacheDirectory.FullName, font.FontName!);
var name = IOPath.GetFileNameWithoutExtension(filePath);
if (File.Exists(filePath))
{
return name;
}

try
{
using (var fileStream = File.Create(filePath))
Expand All @@ -38,6 +48,8 @@ public partial class EmbeddedFontLoader : IEmbeddedFontLoader

font.ResourceStream.CopyTo(fileStream);
}
Tizen.NUI.FontClient.Instance.AddCustomFontDirectory(FontCacheDirectory.FullName);

return name;
}
catch (Exception ex)
Expand Down
8 changes: 3 additions & 5 deletions src/Core/src/Fonts/FontManager.Tizen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ public string GetFontFamily(string? fontFamliy)
if (index != -1)
{
string font = cleansedFont.Substring(0, index);
string style = cleansedFont.Substring(index + 1);
return $"{font}:style={style}";
return $"{font}";
}
else
{
Expand All @@ -79,8 +78,7 @@ string GetNativeFontFamily((string? family, float size, FontSlant slant) fontKey
if (index != -1)
{
string font = cleansedFont.Substring(0, index);
string style = cleansedFont.Substring(index + 1);
return $"{font}:style={style}";
return $"{font}";
}
else
{
Expand Down Expand Up @@ -115,4 +113,4 @@ string GetNativeFontFamily((string? family, float size, FontSlant slant) fontKey
return fontFile.PostScriptName;
}
}
}
}
7 changes: 7 additions & 0 deletions src/Core/src/Platform/Tizen/MauiApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@
using Microsoft.Maui.LifecycleEvents;
using Tizen.Applications;
using Tizen.NUI;
using IOPath = System.IO.Path;
using TApplication = Tizen.Applications.Application;
using NView = Tizen.NUI.BaseComponents.View;

namespace Microsoft.Maui
{
public abstract class MauiApplication : NUIApplication, IPlatformApplication
{
const string _fontCacheFolderName = "fonts";

internal Func<bool>? _handleBackButtonPressed;

IMauiContext _applicationContext = null!;
Expand All @@ -28,6 +32,9 @@ protected override void OnPreCreate()
FocusManager.Instance.EnableDefaultAlgorithm(true);
NView.SetDefaultGrabTouchAfterLeave(true);

var fontResourcePath = IOPath.Combine(TApplication.Current.DirectoryInfo.Resource, _fontCacheFolderName);
FontClient.Instance.AddCustomFontDirectory(fontResourcePath);

var mauiApp = CreateMauiApp();
var rootContext = new MauiContext(mauiApp.Services);

Expand Down
20 changes: 15 additions & 5 deletions src/Core/src/Platform/iOS/ApplicationExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Runtime.Versioning;
using Foundation;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -57,14 +58,23 @@ public static void CreatePlatformWindow(this IUIWindowSceneDelegate sceneDelegat
dicts.Add(session.UserInfo);
if (session.StateRestorationActivity?.UserInfo is not null)
dicts.Add(session.StateRestorationActivity.UserInfo);
if (connectionOptions.UserActivities is not null)
try
{
foreach (var u in connectionOptions.UserActivities)
using var activities = connectionOptions.UserActivities;
if (activities is not null)
{
if (u is NSUserActivity userActivity && userActivity.UserInfo is not null)
dicts.Add(userActivity.UserInfo);
foreach (var u in activities)
{
if (u is NSUserActivity userActivity && userActivity.UserInfo is not null)
dicts.Add(userActivity.UserInfo);
}
}
}
catch (InvalidCastException)
{
// HACK: Workaround for https://github.com/xamarin/xamarin-macios/issues/13704
// This only throws if the collection is empty.
}

var window = CreatePlatformWindow(application, scene as UIWindowScene, dicts.ToArray());
if (window is not null)
Expand Down
30 changes: 24 additions & 6 deletions src/Essentials/src/Platform/WindowStateManager.ios.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,18 @@ public void Init(Func<UIViewController?>? getCurrentUIViewController) =>
// if we have scene support, use that
if (OperatingSystem.IsIOSVersionAtLeast(13) || OperatingSystem.IsMacCatalystVersionAtLeast(13))
{
var scenes = UIApplication.SharedApplication.ConnectedScenes;
var windowScene = scenes.ToArray<UIWindowScene>().FirstOrDefault();
return windowScene?.Windows.FirstOrDefault();
try
{
using var scenes = UIApplication.SharedApplication.ConnectedScenes;
var windowScene = scenes.ToArray<UIWindowScene>().FirstOrDefault();
return windowScene?.Windows.FirstOrDefault();
}
catch (InvalidCastException)
{
// HACK: Workaround for https://github.com/xamarin/xamarin-macios/issues/13704
// This only throws if the collection is empty.
return null;
}
}

// use the windows property (up to 13.0)
Expand All @@ -115,9 +124,18 @@ public void Init(Func<UIViewController?>? getCurrentUIViewController) =>
// if we have scene support, use that
if (OperatingSystem.IsIOSVersionAtLeast(13) || OperatingSystem.IsMacCatalystVersionAtLeast(13))
{
var scenes = UIApplication.SharedApplication.ConnectedScenes;
var windowScene = scenes.ToArray<UIWindowScene>().FirstOrDefault();
return windowScene?.Windows;
try
{
using var scenes = UIApplication.SharedApplication.ConnectedScenes;
var windowScene = scenes.ToArray<UIWindowScene>().FirstOrDefault();
return windowScene?.Windows;
}
catch (InvalidCastException)
{
// HACK: Workaround for https://github.com/xamarin/xamarin-macios/issues/13704
// This only throws if the collection is empty.
return null;
}
}

// use the windows property (up to 15.0)
Expand Down