Skip to content

Commit

Permalink
Merge pull request #10600 from AvaloniaUI/fixes/x11window-nre
Browse files Browse the repository at this point in the history
Add nullable reference checking to X11Window.
  • Loading branch information
maxkatz6 authored and grokys committed Mar 9, 2023
1 parent afd25c8 commit a674362
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/Avalonia.Controls/Platform/IPopupImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Avalonia.Platform
/// </summary>
public interface IPopupImpl : IWindowBaseImpl
{
IPopupPositioner PopupPositioner { get; }
IPopupPositioner? PopupPositioner { get; }

void SetWindowManagerAddShadowHint(bool enabled);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Avalonia.Controls/Primitives/PopupRoot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public PopupRoot(TopLevel parent, IPopupImpl impl, IAvaloniaDependencyResolver d

private void UpdatePosition()
{
PlatformImpl?.PopupPositioner.Update(_positionerParameters);
PlatformImpl?.PopupPositioner?.Update(_positionerParameters);
}

public void ConfigurePosition(IVisual target, PlacementMode placement, Point offset,
Expand Down
66 changes: 38 additions & 28 deletions src/Avalonia.X11/X11Window.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
using static Avalonia.X11.XLib;
// ReSharper disable IdentifierTypo
// ReSharper disable StringLiteralTypo

#nullable enable

namespace Avalonia.X11
{
unsafe partial class X11Window : IWindowImpl, IPopupImpl, IXI2Client,
Expand All @@ -36,7 +39,7 @@ unsafe partial class X11Window : IWindowImpl, IPopupImpl, IXI2Client,
private XConfigureEvent? _configure;
private PixelPoint? _configurePoint;
private bool _triggeredExpose;
private IInputRoot _inputRoot;
private IInputRoot? _inputRoot;
private readonly MouseDevice _mouse;
private readonly TouchDevice _touch;
private readonly IKeyboardDevice _keyboard;
Expand All @@ -52,8 +55,8 @@ unsafe partial class X11Window : IWindowImpl, IPopupImpl, IXI2Client,
private bool _wasMappedAtLeastOnce = false;
private double? _scalingOverride;
private bool _disabled;
private TransparencyHelper _transparencyHelper;
private RawEventGrouper _rawEventGrouper;
private TransparencyHelper? _transparencyHelper;
private RawEventGrouper? _rawEventGrouper;
private bool _useRenderWindow = false;

enum XSyncState
Expand All @@ -63,7 +66,7 @@ enum XSyncState
WaitPaint
}

public X11Window(AvaloniaX11Platform platform, IWindowImpl popupParent)
public X11Window(AvaloniaX11Platform platform, IWindowImpl? popupParent)
{
_platform = platform;
_popup = popupParent != null;
Expand Down Expand Up @@ -193,7 +196,7 @@ public X11Window(AvaloniaX11Platform platform, IWindowImpl popupParent)

XFlush(_x11.Display);
if(_popup)
PopupPositioner = new ManagedPopupPositioner(new ManagedPopupPositionerPopupImplHelper(popupParent, MoveResize));
PopupPositioner = new ManagedPopupPositioner(new ManagedPopupPositionerPopupImplHelper(popupParent!, MoveResize));
if (platform.Options.UseDBusMenu)
NativeMenuExporter = DBusMenuExporter.TryCreateTopLevelNativeMenu(_handle);
NativeControlHost = new X11NativeControlHost(_platform, this);
Expand Down Expand Up @@ -339,17 +342,17 @@ public double RenderScaling
public double DesktopScaling => RenderScaling;

public IEnumerable<object> Surfaces { get; }
public Action<RawInputEventArgs> Input { get; set; }
public Action<Rect> Paint { get; set; }
public Action<Size, PlatformResizeReason> Resized { get; set; }
public Action<RawInputEventArgs>? Input { get; set; }
public Action<Rect>? Paint { get; set; }
public Action<Size, PlatformResizeReason>? Resized { get; set; }
//TODO
public Action<double> ScalingChanged { get; set; }
public Action Deactivated { get; set; }
public Action Activated { get; set; }
public Func<bool> Closing { get; set; }
public Action<WindowState> WindowStateChanged { get; set; }
public Action<double>? ScalingChanged { get; set; }
public Action? Deactivated { get; set; }
public Action? Activated { get; set; }
public Func<bool>? Closing { get; set; }
public Action<WindowState>? WindowStateChanged { get; set; }

public Action<WindowTransparencyLevel> TransparencyLevelChanged
public Action<WindowTransparencyLevel>? TransparencyLevelChanged
{
get => _transparencyHelper?.TransparencyLevelChanged;
set
Expand All @@ -359,17 +362,17 @@ public Action<WindowTransparencyLevel> TransparencyLevelChanged
}
}

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

public Thickness ExtendedMargins { get; } = new Thickness();

public Thickness OffScreenMargin { get; } = new Thickness();

public bool IsClientAreaExtendedToDecorations { get; }

public Action Closed { get; set; }
public Action<PixelPoint> PositionChanged { get; set; }
public Action LostFocus { get; set; }
public Action? Closed { get; set; }
public Action<PixelPoint>? PositionChanged { get; set; }
public Action? LostFocus { get; set; }

public IRenderer CreateRenderer(IRenderRoot root)
{
Expand All @@ -389,6 +392,9 @@ public IRenderer CreateRenderer(IRenderRoot root)

void OnEvent(ref XEvent ev)
{
if (_inputRoot is null)
return;

if (ev.type == XEventName.MapNotify)
{
_mapped = true;
Expand Down Expand Up @@ -435,7 +441,8 @@ void OnEvent(ref XEvent ev)
2 => RawPointerEventType.MiddleButtonDown,
3 => RawPointerEventType.RightButtonDown,
8 => RawPointerEventType.XButton1Down,
9 => RawPointerEventType.XButton2Down
9 => RawPointerEventType.XButton2Down,
_ => throw new NotSupportedException("Unexepected RawPointerEventType.")
},
ref ev, ev.ButtonEvent.state);
else
Expand Down Expand Up @@ -463,7 +470,8 @@ void OnEvent(ref XEvent ev)
2 => RawPointerEventType.MiddleButtonUp,
3 => RawPointerEventType.RightButtonUp,
8 => RawPointerEventType.XButton1Up,
9 => RawPointerEventType.XButton2Up
9 => RawPointerEventType.XButton2Up,
_ => throw new NotSupportedException("Unexepected RawPointerEventType.")
},
ref ev, ev.ButtonEvent.state);
}
Expand Down Expand Up @@ -619,7 +627,7 @@ private void OnPropertyChange(IntPtr atom, bool hasValue)
{
// Occurs once the window has been mapped, which is the earliest the extents
// can be retrieved, so invoke event to force update of TopLevel.FrameSize.
Resized.Invoke(ClientSize, PlatformResizeReason.Unspecified);
Resized?.Invoke(ClientSize, PlatformResizeReason.Unspecified);
}

if (atom == _x11.Atoms._NET_WM_STATE)
Expand Down Expand Up @@ -739,11 +747,13 @@ private void ScheduleInput(RawInputEventArgs args)
if (args is RawDragEvent drag)
drag.Location = drag.Location / RenderScaling;

_rawEventGrouper.HandleEvent(args);
_rawEventGrouper?.HandleEvent(args);
}

void MouseEvent(RawPointerEventType type, ref XEvent ev, XModifierMask mods)
{
if (_inputRoot is null)
return;
var mev = new RawPointerEventArgs(
_mouse, (ulong)ev.ButtonEvent.time.ToInt64(), _inputRoot,
type, new Point(ev.ButtonEvent.x, ev.ButtonEvent.y), TranslateModifiers(mods));
Expand Down Expand Up @@ -778,7 +788,7 @@ public void Invalidate(Rect rect)

}

public IInputRoot InputRoot => _inputRoot;
public IInputRoot? InputRoot => _inputRoot;

public void SetInputRoot(IInputRoot inputRoot)
{
Expand Down Expand Up @@ -923,7 +933,7 @@ public void CanResize(bool value)
UpdateSizeHints(null);
}

public void SetCursor(ICursorImpl cursor)
public void SetCursor(ICursorImpl? cursor)
{
if (cursor == null)
XDefineCursor(_x11.Display, _handle, _x11.DefaultCursor);
Expand Down Expand Up @@ -960,7 +970,7 @@ public PixelPoint Position
public IMouseDevice MouseDevice => _mouse;
public TouchDevice TouchDevice => _touch;

public IPopupImpl CreatePopup()
public IPopupImpl? CreatePopup()
=> _platform.Options.OverlayPopups ? null : new X11Window(_platform, this);

public void Activate()
Expand Down Expand Up @@ -1046,7 +1056,7 @@ public void BeginResizeDrag(WindowEdge edge, PointerPressedEventArgs e)
BeginMoveResize(side, e);
}

public void SetTitle(string title)
public void SetTitle(string? title)
{
if (string.IsNullOrEmpty(title))
{
Expand Down Expand Up @@ -1125,9 +1135,9 @@ public void SetExtendClientAreaTitleBarHeightHint(double titleBarHeight)
{
}

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

public void SetIcon(IWindowIconImpl icon)
public void SetIcon(IWindowIconImpl? icon)
{
if (icon != null)
{
Expand Down

0 comments on commit a674362

Please sign in to comment.