Skip to content
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
7 changes: 7 additions & 0 deletions src/Skyline.Apple/AppKitWindowBackend.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ internal sealed class AppKitWindowBackend : IWindowBackend

public event Action<(int Width, int Height)>? FramebufferResized;
public event Action<bool>? MinimizedChanged;
public event Action<bool>? FocusChanged;
public event Action<double>? Render;
public event Action<PointerEvent>? Pointer;
public event Action<KeyEvent>? Key;
Expand Down Expand Up @@ -162,6 +163,8 @@ public void Run()

internal void OnMinimizedChanged(bool minimized) => MinimizedChanged?.Invoke(minimized);

internal void OnFocusChanged(bool focused) => FocusChanged?.Invoke(focused);

public void Dispose()
{
_window.Delegate = null;
Expand All @@ -181,5 +184,9 @@ private sealed class WindowDelegate(AppKitWindowBackend backend) : NSWindowDeleg
public override void DidMiniaturize(NSNotification notification) => backend.OnMinimizedChanged(true);

public override void DidDeminiaturize(NSNotification notification) => backend.OnMinimizedChanged(false);

public override void DidBecomeKey(NSNotification notification) => backend.OnFocusChanged(true);

public override void DidResignKey(NSNotification notification) => backend.OnFocusChanged(false);
}
}
7 changes: 7 additions & 0 deletions src/Skyline/AppWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ public AppWindow(AppWindowOptions? options = null)
_backend.Pointer += e => PointerInput?.Invoke(e);
_backend.Key += e => KeyInput?.Invoke(e);
_backend.Text += e => TextInput?.Invoke(e);
_backend.FocusChanged += focused => FocusChanged?.Invoke(focused);
}

internal void RaisePointer(PointerEventKind kind, float x, float y, int button, float wheelDx, float wheelDy) =>
Expand Down Expand Up @@ -139,6 +140,12 @@ internal void RaiseText(char character) =>
public event Action<KeyEvent>? KeyInput;
public event Action<TextEvent>? TextInput;

/// <summary>
/// The window gained or lost focus (true = active). Pause animation,
/// timers, and media while blurred; resume on focus.
/// </summary>
public event Action<bool>? FocusChanged;

/// <summary>
/// When set and returning false, the frame is skipped and the loop
/// sleeps briefly instead of rendering. Continuous-render apps leave
Expand Down
2 changes: 2 additions & 0 deletions src/Skyline/GlfwWindowBackend.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ internal sealed class GlfwWindowBackend : IWindowBackend

public event Action<(int Width, int Height)>? FramebufferResized;
public event Action<bool>? MinimizedChanged;
public event Action<bool>? FocusChanged;
public event Action<double>? Render;
public event Action<PointerEvent>? Pointer;
public event Action<KeyEvent>? Key;
Expand Down Expand Up @@ -51,6 +52,7 @@ public unsafe GlfwWindowBackend(AppWindowOptions options)

_window.FramebufferResize += sz => FramebufferResized?.Invoke((sz.X, sz.Y));
_window.StateChanged += state => MinimizedChanged?.Invoke(state == WindowState.Minimized);
_window.FocusChanged += focused => FocusChanged?.Invoke(focused);
_window.Render += delta => Render?.Invoke(delta);

_input = _window.CreateInput();
Expand Down
3 changes: 3 additions & 0 deletions src/Skyline/IWindowBackend.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ internal interface IWindowBackend : IDisposable
/// <summary>Minimized state changed; true when minimized.</summary>
event Action<bool>? MinimizedChanged;

/// <summary>Focus changed; true when the window became active.</summary>
event Action<bool>? FocusChanged;

/// <summary>A frame tick from the backend's own loop, with the delta seconds.</summary>
event Action<double>? Render;

Expand Down
1 change: 1 addition & 0 deletions tests/Skyline.Tests/WindowBackendFactoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ private sealed class FakeBackend : IWindowBackend

public event Action<(int Width, int Height)>? FramebufferResized;
public event Action<bool>? MinimizedChanged;
public event Action<bool>? FocusChanged;
public event Action<double>? Render;
public event Action<PointerEvent>? Pointer;
public event Action<KeyEvent>? Key;
Expand Down