Skip to content

Commit

Permalink
Implement WindowEvent::Minimized and WindowEvent::Restored for Wi…
Browse files Browse the repository at this point in the history
…ndows
  • Loading branch information
rhysd committed Oct 30, 2023
1 parent 3c9f9da commit 6c5dc6d
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 10 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ Unreleased` header.

# Unreleased

- On Windows, implement `WindowEvent::Minimized` and `WindowEvent::Restored`.

# 0.29.3

- On Wayland, apply correct scale to `PhysicalSize` passed in `WindowBuilder::with_inner_size` when possible.
Expand Down
13 changes: 8 additions & 5 deletions examples/util/fill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ pub(super) fn fill_window(window: &Window) {
}

GC.with(|gc| {
let size = window.inner_size();
let (Some(width), Some(height)) =
(NonZeroU32::new(size.width), NonZeroU32::new(size.height))
else {
return;
};

// Either get the last context used or create a new one.
let mut gc = gc.borrow_mut();
let surface = gc
Expand All @@ -61,13 +68,9 @@ pub(super) fn fill_window(window: &Window) {

// Fill a buffer with a solid color.
const DARK_GRAY: u32 = 0xFF181818;
let size = window.inner_size();

surface
.resize(
NonZeroU32::new(size.width).expect("Width must be greater than zero"),
NonZeroU32::new(size.height).expect("Height must be greater than zero"),
)
.resize(width, height)
.expect("Failed to resize the softbuffer surface");

let mut buffer = surface
Expand Down
14 changes: 14 additions & 0 deletions src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,20 @@ pub enum WindowEvent {
/// Winit will aggregate duplicate redraw requests into a single event, to
/// help avoid duplicating rendering work.
RedrawRequested,

/// Emitted when a window is minimized.
///
/// ## Platform-specific
///
/// - Only available on **Windows**.
Minimized,

/// Emitted when a window is restored from minimized state.
///
/// ## Platform-specific
///
/// - Only available on **Windows**.
Restored,
}

/// Identifier of an input device.
Expand Down
20 changes: 15 additions & 5 deletions src/platform_impl/windows/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -964,6 +964,19 @@ unsafe fn lose_active_focus<T>(window: HWND, userdata: &WindowData<T>) {
});
}

unsafe fn minimize_or_restore<T>(window: HWND, userdata: &WindowData<T>, minimized: bool) {
use crate::event::WindowEvent::{Minimized, Restored};

userdata
.window_state_lock()
.set_window_flags_in_place(|f| f.set(WindowFlags::MINIMIZED, minimized));
let event = if minimized { Minimized } else { Restored };
userdata.send_event(Event::WindowEvent {
window_id: RootWindowId(WindowId(window)),
event,
});
}

/// Any window whose callback is configured to this function will have its events propagated
/// through the events loop of the thread the window was created in.
//
Expand Down Expand Up @@ -1426,14 +1439,11 @@ unsafe fn public_window_callback_inner<T: 'static>(
// this is necessary for us to maintain minimize/restore state
WM_SYSCOMMAND => {
if wparam == SC_RESTORE as usize {
let mut w = userdata.window_state_lock();
w.set_window_flags_in_place(|f| f.set(WindowFlags::MINIMIZED, false));
unsafe { minimize_or_restore(window, userdata, false) };
}
if wparam == SC_MINIMIZE as usize {
let mut w = userdata.window_state_lock();
w.set_window_flags_in_place(|f| f.set(WindowFlags::MINIMIZED, true));
unsafe { minimize_or_restore(window, userdata, true) };
}
// Send `WindowEvent::Minimized` here if we decide to implement one

if wparam == SC_SCREENSAVE as usize {
let window_state = userdata.window_state_lock();
Expand Down

0 comments on commit 6c5dc6d

Please sign in to comment.