Skip to content

Commit

Permalink
[Tizen] Add handing Label.TextType
Browse files Browse the repository at this point in the history
  • Loading branch information
JoonghyunCho committed Nov 28, 2022
1 parent aa79dc6 commit 90e70a1
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 12 deletions.
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 @@ -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 @@ -5776,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
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@ override Microsoft.Maui.Controls.TemplatedView.MeasureOverride(double widthConst
~override Microsoft.Maui.Controls.Handlers.Compatibility.TableViewRenderer.OnElementChanged(Microsoft.Maui.Controls.Platform.ElementChangedEventArgs<Microsoft.Maui.Controls.TableView> e) -> void
~static Microsoft.Maui.Controls.Handlers.Compatibility.CellContentFactory.CreateContent(object data, Microsoft.Maui.Controls.BindableObject container = null) -> Microsoft.Maui.Controls.View
~static Microsoft.Maui.Controls.Handlers.Compatibility.ListViewRenderer.CommandMapper -> Microsoft.Maui.CommandMapper<Microsoft.Maui.Controls.ListView, Microsoft.Maui.Controls.Handlers.Compatibility.ListViewRenderer>
~static Microsoft.Maui.Controls.Handlers.Compatibility.ListViewRenderer.Mapper -> Microsoft.Maui.PropertyMapper<Microsoft.Maui.Controls.ListView, Microsoft.Maui.Controls.Handlers.Compatibility.ListViewRenderer>
~static Microsoft.Maui.Controls.Handlers.Compatibility.ListViewRenderer.Mapper -> Microsoft.Maui.PropertyMapper<Microsoft.Maui.Controls.ListView, Microsoft.Maui.Controls.Handlers.Compatibility.ListViewRenderer>
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

0 comments on commit 90e70a1

Please sign in to comment.