Skip to content

Commit

Permalink
feat: Support for more Font properties in Skia Overlay TextBox
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinZikmund committed Mar 7, 2023
1 parent 1593db7 commit e6976b1
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 72 deletions.
31 changes: 31 additions & 0 deletions src/Uno.UI.Runtime.Skia.Gtk/Extensions/TypeConversionExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#nullable enable

using Silk.NET.OpenGL;

namespace Uno.UI.Runtime.Skia.GTK.Extensions;

internal static class TypeConversionExtensions
{
public static Pango.Stretch ToGtkFontStretch(this Windows.UI.Text.FontStretch fontStretch) =>
fontStretch switch
{
Windows.UI.Text.FontStretch.UltraCondensed => Pango.Stretch.UltraCondensed,
Windows.UI.Text.FontStretch.ExtraCondensed => Pango.Stretch.ExtraCondensed,
Windows.UI.Text.FontStretch.Condensed => Pango.Stretch.Condensed,
Windows.UI.Text.FontStretch.SemiCondensed => Pango.Stretch.SemiCondensed,
Windows.UI.Text.FontStretch.SemiExpanded => Pango.Stretch.SemiExpanded,
Windows.UI.Text.FontStretch.Expanded => Pango.Stretch.Expanded,
Windows.UI.Text.FontStretch.ExtraExpanded => Pango.Stretch.ExtraExpanded,
Windows.UI.Text.FontStretch.UltraExpanded => Pango.Stretch.UltraExpanded,
_ => Pango.Stretch.Normal
};

public static Pango.Style ToGtkFontStyle(this Windows.UI.Text.FontStyle fontStyle) =>
fontStyle switch
{
Windows.UI.Text.FontStyle.Normal => Pango.Style.Normal,
Windows.UI.Text.FontStyle.Italic => Pango.Style.Italic,
Windows.UI.Text.FontStyle.Oblique => Pango.Style.Oblique,
_ => Pango.Style.Normal
};
}
11 changes: 7 additions & 4 deletions src/Uno.UI.Runtime.Skia.Gtk/UI/Xaml/Controls/GtkTextBoxView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
using Uno.UI.Runtime.Skia.GTK.Extensions;
using static Windows.UI.Xaml.Shapes.BorderLayerRenderer;
using GtkWindow = Gtk.Window;

Expand Down Expand Up @@ -78,7 +79,7 @@ public void RemoveFromTextInputLayer()

public virtual void UpdateProperties(TextBox textBox)
{
SetFont(textBox.FontWeight, textBox.FontSize);
SetFont(textBox);
SetForeground(textBox.Foreground);
SetSelectionHighlightColor(textBox.SelectionHighlightColor);
RootWidget.Opacity = textBox.Opacity;
Expand All @@ -103,12 +104,14 @@ public void SetPosition(double x, double y)
internal static Fixed? GetOverlayLayer(XamlRoot xamlRoot) =>
XamlRootMap.GetHostForRoot(xamlRoot)?.NativeOverlayLayer;

private void SetFont(FontWeight fontWeight, double fontSize)
private void SetFont(TextBox textBox)
{
var fontDescription = new FontDescription
{
Weight = fontWeight.ToPangoWeight(),
AbsoluteSize = fontSize * Pango.Scale.PangoScale,
Weight = textBox.FontWeight.ToPangoWeight(),
Style = textBox.FontStyle.ToGtkFontStyle(),
Stretch = textBox.FontStretch.ToGtkFontStretch(),
AbsoluteSize = textBox.FontSize * Pango.Scale.PangoScale,
};
#pragma warning disable CS0612 // Type or member is obsolete
InputWidget.OverrideFont(fontDescription);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ internal class MultilineTextBoxView : GtkTextBoxView
public MultilineTextBoxView()
{
_scrolledWindow.Add(_textView);
_scrolledWindow.
}

protected override Widget RootWidget => _scrolledWindow;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#nullable enable

using System.Linq;
using System.Diagnostics;
using System.Linq;

namespace Uno.UI.Runtime.Skia.Wpf.Extensions.UI.Xaml.Controls;
namespace Uno.UI.Runtime.Skia.Wpf.Extensions;

internal static class Extensions
internal static class TypeConversionExtensions
{
public static System.Windows.Media.Brush? ToWpfBrush(this Windows.UI.Xaml.Media.Brush brush)
{
Expand Down Expand Up @@ -62,30 +62,33 @@ private static System.Windows.Media.GradientStop ToWpfGradientStop(this Windows.
return null;
}

private static System.Windows.Media.AlignmentX ToWpfAlignmentX(this Windows.UI.Xaml.Media.AlignmentX alignment)
{
Debug.Assert((int)System.Windows.Media.AlignmentX.Left == (int)Windows.UI.Xaml.Media.AlignmentX.Left);
Debug.Assert((int)System.Windows.Media.AlignmentX.Right == (int)Windows.UI.Xaml.Media.AlignmentX.Right);
Debug.Assert((int)System.Windows.Media.AlignmentX.Center == (int)Windows.UI.Xaml.Media.AlignmentX.Center);
return (System.Windows.Media.AlignmentX)alignment;
}

private static System.Windows.Media.AlignmentY ToWpfAlignmentY(this Windows.UI.Xaml.Media.AlignmentY alignment)
{
Debug.Assert((int)System.Windows.Media.AlignmentY.Top == (int)Windows.UI.Xaml.Media.AlignmentY.Top);
Debug.Assert((int)System.Windows.Media.AlignmentY.Center == (int)Windows.UI.Xaml.Media.AlignmentY.Center);
Debug.Assert((int)System.Windows.Media.AlignmentY.Bottom == (int)Windows.UI.Xaml.Media.AlignmentY.Bottom);
return (System.Windows.Media.AlignmentY)alignment;
}

private static System.Windows.Media.Stretch ToWpfStretch(this Windows.UI.Xaml.Media.Stretch stretch)
{
Debug.Assert((int)System.Windows.Media.Stretch.None == (int)Windows.UI.Xaml.Media.Stretch.None);
Debug.Assert((int)System.Windows.Media.Stretch.Fill == (int)Windows.UI.Xaml.Media.Stretch.Fill);
Debug.Assert((int)System.Windows.Media.Stretch.Uniform == (int)Windows.UI.Xaml.Media.Stretch.Uniform);
Debug.Assert((int)System.Windows.Media.Stretch.UniformToFill == (int)Windows.UI.Xaml.Media.Stretch.UniformToFill);
return (System.Windows.Media.Stretch)stretch;
}
private static System.Windows.Media.AlignmentX ToWpfAlignmentX(this Windows.UI.Xaml.Media.AlignmentX alignment) =>
alignment switch
{
Windows.UI.Xaml.Media.AlignmentX.Left => System.Windows.Media.AlignmentX.Left,
Windows.UI.Xaml.Media.AlignmentX.Center => System.Windows.Media.AlignmentX.Center,
Windows.UI.Xaml.Media.AlignmentX.Right => System.Windows.Media.AlignmentX.Right,
_ => throw new System.InvalidOperationException(),
};

private static System.Windows.Media.AlignmentY ToWpfAlignmentY(this Windows.UI.Xaml.Media.AlignmentY alignment) =>
alignment switch
{
Windows.UI.Xaml.Media.AlignmentY.Top => System.Windows.Media.AlignmentY.Top,
Windows.UI.Xaml.Media.AlignmentY.Center => System.Windows.Media.AlignmentY.Center,
Windows.UI.Xaml.Media.AlignmentY.Bottom => System.Windows.Media.AlignmentY.Bottom,
_ => throw new System.InvalidOperationException(),
};

private static System.Windows.Media.Stretch ToWpfStretch(this Windows.UI.Xaml.Media.Stretch stretch) =>
stretch switch
{
Windows.UI.Xaml.Media.Stretch.None => System.Windows.Media.Stretch.None,
Windows.UI.Xaml.Media.Stretch.Fill => System.Windows.Media.Stretch.Fill,
Windows.UI.Xaml.Media.Stretch.Uniform => System.Windows.Media.Stretch.Uniform,
Windows.UI.Xaml.Media.Stretch.UniformToFill => System.Windows.Media.Stretch.UniformToFill,
_ => throw new System.InvalidOperationException(),
};

private static System.Windows.Media.Transform? ToWpfTransform(this Windows.UI.Xaml.Media.Transform transform)
{
Expand Down Expand Up @@ -137,11 +140,38 @@ private static System.Windows.Media.Stretch ToWpfStretch(this Windows.UI.Xaml.Me
return null;
}

private static System.Windows.Media.BrushMappingMode ToWpfBrushMappingMode(this Windows.UI.Xaml.Media.BrushMappingMode mappingMode)
{
Debug.Assert((int)System.Windows.Media.BrushMappingMode.Absolute == (int)Windows.UI.Xaml.Media.BrushMappingMode.Absolute);
Debug.Assert((int)System.Windows.Media.BrushMappingMode.RelativeToBoundingBox == (int)Windows.UI.Xaml.Media.BrushMappingMode.RelativeToBoundingBox);
private static System.Windows.Media.BrushMappingMode ToWpfBrushMappingMode(this Windows.UI.Xaml.Media.BrushMappingMode mappingMode) =>
mappingMode switch
{
Windows.UI.Xaml.Media.BrushMappingMode.Absolute => System.Windows.Media.BrushMappingMode.Absolute,
Windows.UI.Xaml.Media.BrushMappingMode.RelativeToBoundingBox => System.Windows.Media.BrushMappingMode.RelativeToBoundingBox,
_ => throw new System.InvalidOperationException(),
};

return (System.Windows.Media.BrushMappingMode)mappingMode;
}
public static System.Windows.FontStretch ToWpfFontStretch(this Windows.UI.Text.FontStretch fontStretch) =>
fontStretch switch
{
Windows.UI.Text.FontStretch.Condensed => System.Windows.FontStretches.Condensed,
Windows.UI.Text.FontStretch.Expanded => System.Windows.FontStretches.Expanded,
Windows.UI.Text.FontStretch.ExtraCondensed => System.Windows.FontStretches.ExtraCondensed,
Windows.UI.Text.FontStretch.ExtraExpanded => System.Windows.FontStretches.ExtraExpanded,
Windows.UI.Text.FontStretch.Normal => System.Windows.FontStretches.Normal,
Windows.UI.Text.FontStretch.SemiCondensed => System.Windows.FontStretches.SemiCondensed,
Windows.UI.Text.FontStretch.SemiExpanded => System.Windows.FontStretches.SemiExpanded,
Windows.UI.Text.FontStretch.UltraCondensed => System.Windows.FontStretches.UltraCondensed,
Windows.UI.Text.FontStretch.UltraExpanded => System.Windows.FontStretches.UltraExpanded,
_ => System.Windows.FontStretches.Normal
};

public static System.Windows.FontStyle ToWpfFontStyle(this Windows.UI.Text.FontStyle fontStyle) =>
fontStyle switch
{
Windows.UI.Text.FontStyle.Normal => System.Windows.FontStyles.Normal,
Windows.UI.Text.FontStyle.Italic => System.Windows.FontStyles.Italic,
Windows.UI.Text.FontStyle.Oblique => System.Windows.FontStyles.Oblique,
_ => System.Windows.FontStyles.Normal
};

public static System.Windows.Media.FontFamily ToWpfFontFamily(this Windows.UI.Xaml.Media.FontFamily fontFamily) =>
new System.Windows.Media.FontFamily(fontFamily.Source);
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,38 +42,10 @@ public override IDisposable ObserveTextChanges(EventHandler onChanged)
return Disposable.Create(() => _textBox.TextChanged -= OnTextChanged);
}

public override void UpdateProperties(Windows.UI.Xaml.Controls.TextBox textBox)
public override void UpdateProperties(Windows.UI.Xaml.Controls.TextBox winUITextBox)
{
SetFont(_textBox, winUITextBox);
SetForegroundAndHighlightColor(_textBox, winUITextBox);
//TODO:MZ:
}

// public override string Text { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }

// protected override WpfElement RootElement => _textBox;

// protected override WpfElement InputElement => _textBox;


// public override (int start, int end) GetSelectionBounds() => throw new NotImplementedException();
// public override bool IsCompatible(TextBox textBox) => throw new NotImplementedException();
// public override IDisposable ObserveTextChanges(EventHandler onChanged)
// {
// InputElement.TextChanged += WpfTextViewTextChanged;
// disposable.Add(Disposable.Create(() => _currentTextBoxInputWidget.TextChanged -= WpfTextViewTextChanged));
// }

// if (_currentPasswordBoxInputWidget is not null)
// {
// _currentPasswordBoxInputWidget.PasswordChanged += PasswordBoxViewPasswordChanged;
// disposable.Add(Disposable.Create(() => _currentPasswordBoxInputWidget.PasswordChanged -= PasswordBoxViewPasswordChanged));
// }
//_textChangedDisposable.Disposable = disposable;
// }
// public override void SetSelectionBounds(int start, int end) => throw new NotImplementedException();

//private WpfTextViewTextBox CreateInputControl()
//{
// var textView = new WpfTextViewTextBox();
// return textView;
//}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,9 @@ textBox is not Windows.UI.Xaml.Controls.PasswordBox ?

public void AddToTextInputLayer(XamlRoot xamlRoot)
{
if (GetOverlayLayer(xamlRoot) is { } layer && RootElement.GetParent() != layer)
if (GetOverlayLayer(xamlRoot) is { } layer && RootElement.Parent != layer)
{
layer.Children.Add(RootElement);

}
}

Expand Down Expand Up @@ -100,8 +99,10 @@ protected void SetFont(WpfControl wpfControl, Windows.UI.Xaml.Controls.TextBox s
{
wpfControl.FontSize = source.FontSize;
wpfControl.FontWeight = WpfFontWeight.FromOpenTypeWeight(source.FontWeight.Weight);
//TODO:MZ: Font, FontStretch, etc.
}
wpfControl.FontStretch = source.FontStretch.ToWpfFontStretch();
wpfControl.FontStyle = source.FontStyle.ToWpfFontStyle();
wpfControl.FontFamily = source.FontFamily.ToWpfFontFamily();
}

protected void SetForegroundAndHighlightColor(WpfControl wpfControl, Windows.UI.Xaml.Controls.TextBox source)
{
Expand Down

0 comments on commit e6976b1

Please sign in to comment.