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 handing Label.TextType #11646

Merged
merged 1 commit into from
Nov 28, 2022
Merged
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
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;
}
}
}
31 changes: 25 additions & 6 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 @@ -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 @@ -1642,6 +1642,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 @@ -5888,6 +5889,7 @@ virtual Microsoft.Maui.Controls.Window.OnStopped() -> void
~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 GetFont(Font font)
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