Skip to content

Commit

Permalink
nullable enable in avalonianative window / windowbase managed side.
Browse files Browse the repository at this point in the history
  • Loading branch information
danwalmsley committed Apr 22, 2022
1 parent 582389e commit d06464d
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 78 deletions.
39 changes: 25 additions & 14 deletions src/Avalonia.Native/AvaloniaNativePlatform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@
using Avalonia.Platform;
using Avalonia.Rendering;

#nullable enable

namespace Avalonia.Native
{
class AvaloniaNativePlatform : IPlatformSettings, IWindowingPlatform
{
private readonly IAvaloniaNativeFactory _factory;
private AvaloniaNativePlatformOptions _options;
private AvaloniaNativePlatformOpenGlInterface _platformGl;
private AvaloniaNativePlatformOpenGlInterface? _platformGl;

[DllImport("libAvaloniaNative")]
static extern IntPtr CreateAvaloniaNative();
Expand All @@ -28,8 +30,8 @@ class AvaloniaNativePlatform : IPlatformSettings, IWindowingPlatform

public static AvaloniaNativePlatform Initialize(IntPtr factory, AvaloniaNativePlatformOptions options)
{
var result = new AvaloniaNativePlatform(MicroComRuntime.CreateProxyFor<IAvaloniaNativeFactory>(factory, true));
result.DoInitialize(options);
var result = new AvaloniaNativePlatform(MicroComRuntime.CreateProxyFor<IAvaloniaNativeFactory>(factory, true), options);
result.DoInitialize();

return result;
}
Expand Down Expand Up @@ -62,15 +64,20 @@ public void SetupApplicationMenuExporter ()

public void SetupApplicationName ()
{
if(!string.IsNullOrWhiteSpace(Application.Current.Name))
if(!string.IsNullOrWhiteSpace(Application.Current?.Name))
{
_factory.MacOptions.SetApplicationTitle(Application.Current?.Name);
}
else
{
_factory.MacOptions.SetApplicationTitle(Application.Current.Name);
_factory.MacOptions.SetApplicationTitle("");
}
}

private AvaloniaNativePlatform(IAvaloniaNativeFactory factory)
private AvaloniaNativePlatform(IAvaloniaNativeFactory factory, AvaloniaNativePlatformOptions options)
{
_factory = factory;
_options = options;
}

class GCHandleDeallocator : CallbackBase, IAvnGCHandleDeallocatorCallback
Expand All @@ -81,10 +88,8 @@ public void FreeGCHandle(IntPtr handle)
}
}

void DoInitialize(AvaloniaNativePlatformOptions options)
void DoInitialize()
{
_options = options;

var applicationPlatform = new AvaloniaNativeApplicationPlatform();

_factory.Initialize(new GCHandleDeallocator(), applicationPlatform);
Expand Down Expand Up @@ -114,11 +119,17 @@ void DoInitialize(AvaloniaNativePlatformOptions options)
.Bind<INativeApplicationCommands>().ToConstant(new MacOSNativeMenuCommands(_factory.CreateApplicationCommands()));

var hotkeys = AvaloniaLocator.Current.GetService<PlatformHotkeyConfiguration>();
hotkeys.MoveCursorToTheStartOfLine.Add(new KeyGesture(Key.Left, hotkeys.CommandModifiers));
hotkeys.MoveCursorToTheStartOfLineWithSelection.Add(new KeyGesture(Key.Left, hotkeys.CommandModifiers | hotkeys.SelectionModifiers));
hotkeys.MoveCursorToTheEndOfLine.Add(new KeyGesture(Key.Right, hotkeys.CommandModifiers));
hotkeys.MoveCursorToTheEndOfLineWithSelection.Add(new KeyGesture(Key.Right, hotkeys.CommandModifiers | hotkeys.SelectionModifiers));


if (hotkeys != null)
{
hotkeys.MoveCursorToTheStartOfLine.Add(new KeyGesture(Key.Left, hotkeys.CommandModifiers));
hotkeys.MoveCursorToTheStartOfLineWithSelection.Add(new KeyGesture(Key.Left,
hotkeys.CommandModifiers | hotkeys.SelectionModifiers));
hotkeys.MoveCursorToTheEndOfLine.Add(new KeyGesture(Key.Right, hotkeys.CommandModifiers));
hotkeys.MoveCursorToTheEndOfLineWithSelection.Add(new KeyGesture(Key.Right,
hotkeys.CommandModifiers | hotkeys.SelectionModifiers));
}

if (_options.UseGpu)
{
try
Expand Down
27 changes: 13 additions & 14 deletions src/Avalonia.Native/WindowImpl.cs
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
using System;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Platform;
using Avalonia.Input;
using Avalonia.Input.Raw;
using Avalonia.Native.Interop;
using Avalonia.OpenGL;
using Avalonia.Platform;
using Avalonia.Platform.Interop;

#nullable enable

namespace Avalonia.Native
{
internal class WindowImpl : WindowBaseImpl, IWindowImpl, ITopLevelImplWithNativeMenuExporter
{
private readonly IAvaloniaNativeFactory _factory;
private readonly AvaloniaNativePlatformOptions _opts;
private readonly AvaloniaNativePlatformOpenGlInterface _glFeature;
private readonly AvaloniaNativePlatformOpenGlInterface? _glFeature;
IAvnWindow _native;
private double _extendTitleBarHeight = -1;
private DoubleClickHelper _doubleClickHelper;


internal WindowImpl(IAvaloniaNativeFactory factory, AvaloniaNativePlatformOptions opts,
AvaloniaNativePlatformOpenGlInterface glFeature) : base(opts, glFeature)
AvaloniaNativePlatformOpenGlInterface? glFeature) : base(opts, glFeature)
{
_factory = factory;
_opts = opts;
Expand Down Expand Up @@ -82,12 +81,12 @@ public void SetSystemDecorations(Controls.SystemDecorations enabled)
_native.SetDecorations((Interop.SystemDecorations)enabled);
}

public void SetTitleBarColor(Avalonia.Media.Color color)
public void SetTitleBarColor(Media.Color color)
{
_native.SetTitleBarColor(new AvnColor { Alpha = color.A, Red = color.R, Green = color.G, Blue = color.B });
}

public void SetTitle(string title)
public void SetTitle(string? title)
{
_native.SetTitle(title ?? "");
}
Expand All @@ -98,9 +97,9 @@ public WindowState WindowState
set => _native.SetWindowState((AvnWindowState)value);
}

public Action<WindowState> WindowStateChanged { get; set; }
public Action<WindowState>? WindowStateChanged { get; set; }

public Action<bool> ExtendClientAreaToDecorationsChanged { get; set; }
public Action<bool>? ExtendClientAreaToDecorationsChanged { get; set; }

public Thickness ExtendedMargins { get; private set; }

Expand All @@ -113,9 +112,9 @@ protected override bool ChromeHitTest (RawPointerEventArgs e)
{
if(_isExtended)
{
if(e.Type == RawPointerEventType.LeftButtonDown)
if(e.Type == RawPointerEventType.LeftButtonDown && _inputRoot is Window window)
{
var visual = (_inputRoot as Window).Renderer.HitTestFirst(e.Position, _inputRoot as Window, x =>
var visual = window.Renderer.HitTestFirst(e.Position, _inputRoot as Window, x =>
{
if (x is IInputElement ie && (!ie.IsHitTestVisible || !ie.IsVisible))
{
Expand Down Expand Up @@ -203,16 +202,16 @@ public void SetIcon(IWindowIconImpl icon)
// NO OP on OSX
}

public Func<bool> Closing { get; set; }
public Func<bool>? Closing { get; set; }

public ITopLevelNativeMenuExporter NativeMenuExporter { get; }

public void Move(PixelPoint point) => Position = point;

public override IPopupImpl CreatePopup() =>
public override IPopupImpl? CreatePopup() =>
_opts.OverlayPopups ? null : new PopupImpl(_factory, _opts, _glFeature, this);

public Action GotInputWhenDisabled { get; set; }
public Action? GotInputWhenDisabled { get; set; }

public void SetParent(IWindowImpl parent)
{
Expand Down
Loading

0 comments on commit d06464d

Please sign in to comment.