Skip to content
Merged
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
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