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

Maui.Core Gtk enhancements #5

Merged
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
138 changes: 137 additions & 1 deletion src/Core/src/Fonts/FontManager.Linux.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,148 @@
namespace Microsoft.Maui
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Maui.Graphics;
using Pango;

namespace Microsoft.Maui
{

// see: https://developer.gnome.org/pygtk/stable/class-pangofontdescription.html
/*
public enum Pango.Weight
{
Thin = 100, // 0x00000064
Ultralight = 200, // 0x000000C8
Light = 300, // 0x0000012C
Semilight = 350, // 0x0000015E
Book = 380, // 0x0000017C
Normal = 400, // 0x00000190
Medium = 500, // 0x000001F4
Semibold = 600, // 0x00000258
Bold = 700, // 0x000002BC
Ultrabold = 800, // 0x00000320
Heavy = 900, // 0x00000384
Ultraheavy = 1000, // 0x000003E8
}
public enum Stretch
{
UltraCondensed,
ExtraCondensed,
Condensed,
SemiCondensed,
Normal,
SemiExpanded,
Expanded,
ExtraExpanded,
UltraExpanded,
}
public enum Style { Normal, Oblique, Italic }
public enum Variant
{
Normal,
SmallCaps,
}
public enum Gravity
{
South,
East,
North,
West,
Auto,
}
*/

public class FontManager : IFontManager
{

readonly IFontRegistrar _fontRegistrar;

static Pango.Context? _systemContext;

Pango.Context SystemContext => _systemContext ??= Gdk.PangoHelper.ContextGet();

public FontManager(IFontRegistrar fontRegistrar)
{
_fontRegistrar = fontRegistrar;
}

FontDescription? _defaultFontFamily;

public FontDescription DefaultFontFamily
{
get => _defaultFontFamily ??= GetFontFamily(default);
}

double? _defaultFontSize;

public double DefaultFontSize => _defaultFontSize ??= DefaultFontFamily?.GetSize() ?? 0;

public FontDescription GetFontFamily(Font font) =>
font == default ? SystemContext.FontDescription : font.ToFontDescription();

public double GetFontSize(Font font)
{
if (font.UseNamedSize)
return GetFontSize(font.NamedSize);

return font.FontSize;
}

public double GetFontSize(NamedSize namedSize)
{
// TODO: Hmm, maybe we need to revisit this, since we no longer support Windows Phone OR WinRT.
// These are values pulled from the mapped sizes on Windows Phone, WinRT has no equivalent sizes, only intents.

return namedSize switch
{
NamedSize.Default => DefaultFontSize,
NamedSize.Micro => 15.667,
NamedSize.Small => 18.667,
NamedSize.Medium => 22.667,
NamedSize.Large => 32,
NamedSize.Body => 14,
NamedSize.Caption => 12,
NamedSize.Header => 46,
NamedSize.Subtitle => 20,
NamedSize.Title => 24,
_ => throw new ArgumentOutOfRangeException(nameof(namedSize)),
};
}

private IEnumerable<(Pango.FontFamily family, Pango.FontDescription description)> GetAvailableFamilyFaces(Pango.FontFamily family)
{

if (family != default)
{
foreach (var face in family.Faces)
yield return (family, face.Describe());
}

yield break;
}

private FontDescription[] GetAvailableFontStyles()
{
var fontFamilies = SystemContext.FontMap?.Families.ToArray();

var styles = new List<FontDescription>();

if (fontFamilies != null)
{
styles.AddRange(fontFamilies.SelectMany(GetAvailableFamilyFaces).Select(font => font.description)
.OrderBy(d=>d.Family));
}


return styles.ToArray();
}

}

}
11 changes: 10 additions & 1 deletion src/Core/src/Fonts/IFontManager.Linux.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
namespace Microsoft.Maui
using Pango;

namespace Microsoft.Maui
{
public interface IFontManager
{

FontDescription DefaultFontFamily { get; }

double DefaultFontSize { get; }

FontDescription GetFontFamily(Font font);

double GetFontSize(Font font);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,23 @@

namespace Microsoft.Maui.Handlers
{
public partial class ActivityIndicatorHandler : ViewHandler<ICheckBox, Spinner>
public partial class ActivityIndicatorHandler : ViewHandler<IActivityIndicator, Spinner>
{
protected override Spinner CreateNativeView()
{
return new Spinner();
return new();
}

public static void MapIsRunning(ActivityIndicatorHandler handler, IActivityIndicator activityIndicator)
{
handler.NativeView?.UpdateIsRunning(activityIndicator);
}

[MissingMapper]
public static void MapColor(ActivityIndicatorHandler handler, IActivityIndicator activityIndicator) { }

public static void MapColor(ActivityIndicatorHandler handler, IActivityIndicator activityIndicator)
{
handler.NativeView?.SetForegroundColor(activityIndicator.Color);

}
}
}
28 changes: 19 additions & 9 deletions src/Core/src/Handlers/Button/ButtonHandler.Linux.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@

namespace Microsoft.Maui.Handlers
{

public partial class ButtonHandler : ViewHandler<IButton, Button>
{

protected override Button CreateNativeView()
{
return new Button();
Expand All @@ -29,31 +31,39 @@ public static void MapText(ButtonHandler handler, IButton button)
handler.NativeView?.UpdateText(button);
}

[MissingMapper]
public static void MapTextColor(ButtonHandler handler, IButton button) { }
public static void MapTextColor(ButtonHandler handler, IButton button)
{
handler.NativeView?.UpdateTextColor(button.TextColor);
}

[MissingMapper]
public static void MapCharacterSpacing(ButtonHandler handler, IButton button) { }

[MissingMapper]
public static void MapFont(ButtonHandler handler, IButton button) { }
public static void MapFont(ButtonHandler handler, IButton button)
{
handler.MapFont(button);
}

[MissingMapper]
public static void MapPadding(ButtonHandler handler, IButton button) { }
public static void MapPadding(ButtonHandler handler, IButton button)
{
handler.NativeView.WithMargin(button.Padding);
}

void OnButtonPressEvent(object? o, ButtonPressEventArgs args)
{
VirtualView?.Pressed();
InvokeEvent(() => VirtualView?.Pressed());
}

void OnButtonReleaseEvent(object? o, ButtonReleaseEventArgs args)
{
VirtualView?.Released();
InvokeEvent(() => VirtualView?.Released());
}

void OnButtonClicked(object? sender, EventArgs e)
{
VirtualView?.Clicked();
InvokeEvent(() => VirtualView?.Clicked());
}

}

}
31 changes: 25 additions & 6 deletions src/Core/src/Handlers/CheckBox/CheckBoxHandler.Linux.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,36 @@
using Gtk;
using System;
using Gtk;

namespace Microsoft.Maui.Handlers
{

public partial class CheckBoxHandler : ViewHandler<ICheckBox, CheckButton>
{
protected override CheckButton CreateNativeView()
{
return new CheckButton();
}

protected override CheckButton CreateNativeView() => new();

public static void MapIsChecked(CheckBoxHandler handler, ICheckBox check)
{
handler.NativeView?.UpdateIsChecked(check);
}

protected override void ConnectHandler(CheckButton nativeView)
{
nativeView.Toggled += OnToggledEvent;
}

protected override void DisconnectHandler(CheckButton nativeView)
{
nativeView.Toggled -= OnToggledEvent;
}

protected void OnToggledEvent(object? sender, EventArgs e)
{
if (sender is CheckButton nativeView && VirtualView != null)
VirtualView.IsChecked = nativeView.Active;

}

}
}

}
34 changes: 23 additions & 11 deletions src/Core/src/Handlers/Editor/EditorHandler.Linux.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

namespace Microsoft.Maui.Handlers
{

public partial class EditorHandler : ViewHandler<IEditor, TextView>
{

protected override TextView CreateNativeView()
{
return new TextView();
return new();
}

public static void MapText(EditorHandler handler, IEditor editor)
Expand All @@ -15,27 +17,37 @@ public static void MapText(EditorHandler handler, IEditor editor)
}

[MissingMapper]
public static void MapPlaceholder(IViewHandler handler, IEditor editor) { }
public static void MapPlaceholder(EditorHandler handler, IEditor editor) { }

[MissingMapper]
public static void MapPlaceholderColor(IViewHandler handler, IEditor editor) { }
public static void MapPlaceholderColor(EditorHandler handler, IEditor editor) { }

[MissingMapper]
public static void MapCharacterSpacing(IViewHandler handler, IEditor editor) { }
public static void MapCharacterSpacing(EditorHandler handler, IEditor editor) { }

[MissingMapper]
public static void MapMaxLength(IViewHandler handler, IEditor editor) { }
public static void MapMaxLength(EditorHandler handler, IEditor editor) { }

[MissingMapper]
public static void MapIsTextPredictionEnabled(EditorHandler handler, IEditor editor) { }

[MissingMapper]
public static void MapFont(IViewHandler handler, IEditor editor) { }
public static void MapFont(EditorHandler handler, IEditor editor)
{
handler.MapFont(editor);

[MissingMapper]
public static void MapIsReadOnly(IViewHandler handler, IEditor editor) { }
}

public static void MapIsReadOnly(EditorHandler handler, IEditor editor)
{
if (handler.NativeView is { } nativeView)
nativeView.Editable = editor.IsReadOnly;
}

public static void MapTextColor(EditorHandler handler, IEditor editor)
{
handler.NativeView?.UpdateTextColor(editor.TextColor);
}

[MissingMapper]
public static void MapTextColor(EditorHandler handler, IEditor editor) { }
}

}
Loading