Skip to content

Commit

Permalink
Update and Repaint on resize. (dotnet#1979)
Browse files Browse the repository at this point in the history
* Repaint on resize/drag.

* Remove API changes.

* Use the same `onRender` action for Glfw's refresh events, but prevent reentrancy into DoEvents.

* Move protected _onFrame field into the GlfwWindow class rather than the base ViewImplementationBase class.
  • Loading branch information
otac0n committed Mar 20, 2024
1 parent 8506e63 commit 4f76a9f
Showing 1 changed file with 46 additions and 6 deletions.
52 changes: 46 additions & 6 deletions src/Windowing/Silk.NET.Windowing.Glfw/GlfwWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ internal unsafe class GlfwWindow : WindowImplementationBase, IVkSurface
private GlfwCallbacks.WindowPosCallback? _onMove;
private GlfwCallbacks.WindowSizeCallback? _onResize;
private GlfwCallbacks.FramebufferSizeCallback? _onFramebufferResize;
private GlfwCallbacks.WindowRefreshCallback? _onRefresh;
private GlfwCallbacks.DropCallback? _onFileDrop;
private GlfwCallbacks.WindowCloseCallback? _onClosing;
private GlfwCallbacks.WindowFocusCallback? _onFocusChanged;
Expand All @@ -37,6 +38,16 @@ internal unsafe class GlfwWindow : WindowImplementationBase, IVkSurface
private string _localTitleCache; // glfw doesn't let us get the window title.
private GlfwContext? _glContext;
private string _windowClass;
private bool _inDoEvents;

/// <summary>
/// The action passed to <see cref="Run"/>.
/// </summary>
/// <remarks>
/// May be called on repaint from within <see cref="DoEvents"/>.
/// For example, during modal operations such as resizing on Windows.
/// </remarks>
protected Action? _onFrame;

public GlfwWindow(WindowOptions optionsCache, GlfwWindow? parent, GlfwMonitor? monitor) : base(optionsCache)
{
Expand Down Expand Up @@ -67,6 +78,19 @@ protected override Rectangle<int> CoreBorderSize

protected override nint CoreHandle => (nint) _glfwWindow;

public override void Run(Action onFrame)
{
try
{
_onFrame = onFrame;
base.Run(onFrame);
}
finally
{
_onFrame = null;
}
}

protected override void CoreReset()
{
if (_glfwWindow == null)
Expand Down Expand Up @@ -598,13 +622,24 @@ public override Vector2D<int> FramebufferSize

public override void DoEvents()
{
if (IsEventDriven)
if (!_inDoEvents)
{
_glfw.WaitEvents();
}
else
{
_glfw.PollEvents();
try
{
_inDoEvents = true;
if (IsEventDriven)
{
_glfw.WaitEvents();
}
else
{
_glfw.PollEvents();
}
}
finally
{
_inDoEvents = false;
}
}
}

Expand Down Expand Up @@ -658,6 +693,8 @@ protected override void RegisterCallbacks()
FramebufferResize?.Invoke(new(width, height));
};

_onRefresh = (window) => _onFrame?.Invoke();

_onClosing = window => Closing?.Invoke();

_onFocusChanged = (window, isFocused) => FocusChanged?.Invoke(isFocused);
Expand Down Expand Up @@ -744,6 +781,7 @@ protected override void RegisterCallbacks()
_glfw.SetWindowIconifyCallback(_glfwWindow, _onMinimized);
_glfw.SetWindowMaximizeCallback(_glfwWindow, _onMaximized);
_glfw.SetFramebufferSizeCallback(_glfwWindow, _onFramebufferResize);
_glfw.SetWindowRefreshCallback(_glfwWindow, _onRefresh);
_glfw.SetDropCallback(_glfwWindow, _onFileDrop);
GLFW.Glfw.ThrowExceptions();
}
Expand All @@ -761,6 +799,7 @@ protected override void UnregisterCallbacks()
_glfw.GcUtility.Unpin(_onMove);
_glfw.GcUtility.Unpin(_onResize);
_glfw.GcUtility.Unpin(_onFramebufferResize);
_glfw.GcUtility.Unpin(_onRefresh);
_glfw.GcUtility.Unpin(_onFileDrop);
_glfw.GcUtility.Unpin(_onFocusChanged);

Expand All @@ -770,6 +809,7 @@ protected override void UnregisterCallbacks()
_onMove = null;
_onResize = null;
_onFramebufferResize = null;
_onRefresh = null;
_onFileDrop = null;
_onFocusChanged = null;
}
Expand Down

0 comments on commit 4f76a9f

Please sign in to comment.