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
51 changes: 24 additions & 27 deletions src/platform/win/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ use super::drop_target::DropTarget;
use super::*;
use crate::handler::WindowHandlerBuilder;
use crate::host::Host;
use crate::platform::win::window_state::{WindowSharedState, WindowState};
use crate::platform::win::window_state::{RedrawTimer, WindowSharedState, WindowState};
use crate::platform::PlatformError;
use crate::utils::SizingStrategy;
use crate::window::WindowInitializer;
use crate::wrappers::win32::cursor::SystemCursor;
use crate::wrappers::win32::window::*;
use crate::wrappers::win32::{
ole_initialize, run_thread_message_loop_until, Dpi, DpiAwarenessGuard, LibraryModule, Rect,
TimerId, WindowStyle,
TimerId, TimerType, WindowStyle,
};
use crate::{Event, MouseButton, MouseEvent, ScrollDelta, WindowEvent, WindowSize};

Expand Down Expand Up @@ -359,19 +359,33 @@ impl BaseviewWindow {
handler.poll();
}

pub(crate) fn handle_timer(&self, timer: &TimerHandle) {
let Some(handler) = self.handler.get() else { return };

handler.on_timer(timer.into());
}

pub(crate) fn handle_event(&self, event: Event) -> EventStatus {
let Some(handler) = self.handler.get() else {
return EventStatus::Ignored;
};

handler.on_event(event)
}

pub(crate) fn handle_delayed_redraw_wm_timer(&self, id: TimerId) {
match self.shared_state.delayed_redraw_timers.remove_if_exists(id) {
Ok(false) => {}
Err(e) => {
warn!("Could not remove timer: {}", e);
}
Ok(true) => {
self.window_state.request_redraw();
}
}
}

pub(crate) fn handle_user_wm_timer(&self, id: TimerId) {
if self.shared_state.user_timers.exists(id) {
let Some(handler) = self.handler.get() else { return };

handler.on_timer((&id).into());
}
}
}

impl Drop for BaseviewWindow {
Expand Down Expand Up @@ -626,32 +640,15 @@ unsafe fn wnd_proc_inner(
WM_TIMER => {
let timer_id = TimerId::from_raw(wparam)?;

if window_state.redraw_timer.matches_id(timer_id) {
if timer_id == RedrawTimer::ID {
if !window_state.redraw_timer.is_running() {
return None;
};
window.invalidate_window();
return Some(0);
}

match window_state.shared.delayed_redraw_timers.remove_if_exists(window, timer_id) {
Ok(false) => {}
Err(e) => {
warn!("Could not remove timer: {}", e);
return None;
}
Ok(true) => {
window_state.request_redraw();
return Some(0);
}
}

if window_state.shared.user_timers.exists(timer_id) {
window_bv.handle_timer(&timer_id);
Some(0)
} else {
None
}
None
}
WM_CLOSE => {
window_bv.handle_event(Event::Window(WindowEvent::WillClose));
Expand Down
34 changes: 15 additions & 19 deletions src/platform/win/window_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ use crate::dpi::{PhysicalSize, Size};
use crate::platform::win::dpi::DpiScalingStrategy;
use crate::platform::win::keyboard::KeyboardState;
use crate::platform::win::waker::WindowWakerSource;
use crate::platform::{PlatformHandle, TimerHandle, WindowWaker};
use crate::platform::{BaseviewWindow, PlatformHandle, TimerHandle, WindowWaker};
use crate::utils::SizingStrategy;
use crate::window::WindowInitializer;
use crate::wrappers::win32::cursor::SystemCursor;
use crate::wrappers::win32::h_instance::HInstance;
use crate::wrappers::win32::window::{HWnd, PostMessageExt};
use crate::wrappers::win32::{
Dpi, DpiAwarenessGuard, ExtendedUser32, LibraryModule, TimerList, TimerSlot,
Dpi, DpiAwarenessGuard, ExtendedUser32, LibraryModule, TimerId, TimerList, TimerSlot, TimerType,
};
use crate::{MouseCursor, WindowSize};
use raw_window_handle::{DisplayHandle, Win32WindowHandle};
Expand All @@ -18,7 +18,12 @@ use std::num::NonZeroIsize;
use std::rc::Rc;
use std::time::Duration;

const REDRAW_TIMER_DELAY_MSEC: u32 = 15;
pub(crate) struct RedrawTimer;

impl TimerType for RedrawTimer {
const ID: TimerId = TimerId::from_const(0x42691);
const TIMEOUT_MSEC: u32 = 15;
}

/// All data associated with the window.
pub(crate) struct WindowState {
Expand All @@ -31,7 +36,7 @@ pub(crate) struct WindowState {

pub user32: LibraryModule<ExtendedUser32>,
pub shared: Rc<WindowSharedState>,
pub(crate) redraw_timer: TimerSlot,
pub(crate) redraw_timer: TimerSlot<RedrawTimer>,
pub redraw_requested: Cell<bool>,

#[cfg(feature = "opengl")]
Expand Down Expand Up @@ -131,13 +136,13 @@ impl WindowState {
pub fn request_redraw(&self) {
self.redraw_requested.set(true);

self.redraw_timer.start_if_not_running(REDRAW_TIMER_DELAY_MSEC);
self.redraw_timer.start_if_not_running();
}

pub fn setup_redraw_request_for_next_frame(&self) {
let should_redraw_next_frame = self.redraw_requested.take();

self.redraw_timer.set_running(should_redraw_next_frame, REDRAW_TIMER_DELAY_MSEC);
self.redraw_timer.set_running(should_redraw_next_frame);
}

pub fn request_redraw_after(&self, duration: Duration) {
Expand Down Expand Up @@ -180,22 +185,22 @@ impl WindowSharedState {
let state = Self {
sizing_strategy: SizingStrategy::from_settings(&init.settings),
user32,
hwnd: None.into(),

// Internal state
is_alive: true.into(),
resize_host_originated: false.into(),
destroy_host_originated: false.into(),
delayed_redraw_timers: TimerList::new(BaseviewWindow::handle_delayed_redraw_wm_timer),
user_timers: TimerList::new(BaseviewWindow::handle_user_wm_timer),
window_waker_source: WindowWakerSource::new(),

// Overriden by init() below
parented: false.into(),
current_dpi: Dpi::default().into(),
current_size: init.settings.size.to_physical(1.0).into(),
fallback_scale_factor: init.settings.fallback_scale_factor.into(),
dpi_scaling_strategy: DpiScalingStrategy::default().into(),
delayed_redraw_timers: TimerList::new(),
user_timers: TimerList::new(),
window_waker_source: WindowWakerSource::new(),
hwnd: None.into(),
};

state.init(init);
Expand Down Expand Up @@ -265,15 +270,6 @@ impl WindowSharedState {
}
}

impl Drop for WindowSharedState {
fn drop(&mut self) {
if let Some(hwnd) = self.hwnd.get() {
self.user_timers.stop_and_destroy_all(hwnd);
self.delayed_redraw_timers.stop_and_destroy_all(hwnd);
}
}
}

struct Guard<'a>(&'a Cell<bool>);
impl<'a> Drop for Guard<'a> {
fn drop(&mut self) {
Expand Down
4 changes: 4 additions & 0 deletions src/wrappers/win32/dpi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,10 @@ impl<'a> DpiAwarenessGuard<'a> {
}
}

pub fn dummy() -> Self {
Self { inner: None }
}

pub fn with_guard_optional<T>(
user32: &'a ExtendedUser32, strategy: DpiScalingStrategy, handler: impl FnOnce() -> T,
) -> T {
Expand Down
4 changes: 4 additions & 0 deletions src/wrappers/win32/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,8 @@ impl WindowStyle {

style
}

pub const fn dummy() -> Self {
Self { style: 0, style_ex: 0 }
}
}
Loading
Loading