mirror of
https://github.com/storytold/pdfcraft.git
synced 2026-10-25 10:39:01 -06:00
* Issue #324: vendor winit 0.30.13 as published The crate as released on crates.io, without its asset files (docs/res/*.svg and examples/data/*.png, AGENTS.md §1). Not used yet: the next commit patches it and adds it to [patch.crates-io]. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Issue #324: on Windows 11, a window dragged to another monitor takes its scale winit 0.30.13 handles WM_DPICHANGED by checking that the new window rect is on the monitor `MonitorFromWindow` names. During a drag that is still the monitor being left, so winit nudged the window back onto it, which sent another WM_DPICHANGED: the window grew and stayed on the wrong monitor at the wrong scale. From Windows 11 (build 22000) on, apply the rect Windows suggests, as winit master does since 488c036a (rust-windowing/winit#4341); no 0.30 release has it. Windows 10 keeps the old adjustment, as on winit master. The vendored copy goes in [patch.crates-io]; a test fails if winit resolves from crates.io again or the vendored copy loses the patch. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
118 lines
4.1 KiB
Rust
118 lines
4.1 KiB
Rust
//! Fill the window buffer with a solid color.
|
|
//!
|
|
//! Launching a window without drawing to it has unpredictable results varying from platform to
|
|
//! platform. In order to have well-defined examples, this module provides an easy way to
|
|
//! fill the window buffer with a solid color.
|
|
//!
|
|
//! The `softbuffer` crate is used, largely because of its ease of use. `glutin` or `wgpu` could
|
|
//! also be used to fill the window buffer, but they are more complicated to use.
|
|
|
|
#[allow(unused_imports)]
|
|
pub use platform::cleanup_window;
|
|
pub use platform::fill_window;
|
|
|
|
#[cfg(all(feature = "rwh_05", not(any(target_os = "android", target_os = "ios"))))]
|
|
mod platform {
|
|
use std::cell::RefCell;
|
|
use std::collections::HashMap;
|
|
use std::mem;
|
|
use std::mem::ManuallyDrop;
|
|
use std::num::NonZeroU32;
|
|
|
|
use softbuffer::{Context, Surface};
|
|
use winit::window::{Window, WindowId};
|
|
|
|
thread_local! {
|
|
// NOTE: You should never do things like that, create context and drop it before
|
|
// you drop the event loop. We do this for brevity to not blow up examples. We use
|
|
// ManuallyDrop to prevent destructors from running.
|
|
//
|
|
// A static, thread-local map of graphics contexts to open windows.
|
|
static GC: ManuallyDrop<RefCell<Option<GraphicsContext>>> = const { ManuallyDrop::new(RefCell::new(None)) };
|
|
}
|
|
|
|
/// The graphics context used to draw to a window.
|
|
struct GraphicsContext {
|
|
/// The global softbuffer context.
|
|
context: RefCell<Context<&'static Window>>,
|
|
|
|
/// The hash map of window IDs to surfaces.
|
|
surfaces: HashMap<WindowId, Surface<&'static Window, &'static Window>>,
|
|
}
|
|
|
|
impl GraphicsContext {
|
|
fn new(w: &Window) -> Self {
|
|
Self {
|
|
context: RefCell::new(
|
|
Context::new(unsafe { mem::transmute::<&'_ Window, &'static Window>(w) })
|
|
.expect("Failed to create a softbuffer context"),
|
|
),
|
|
surfaces: HashMap::new(),
|
|
}
|
|
}
|
|
|
|
fn create_surface(
|
|
&mut self,
|
|
window: &Window,
|
|
) -> &mut Surface<&'static Window, &'static Window> {
|
|
self.surfaces.entry(window.id()).or_insert_with(|| {
|
|
Surface::new(&self.context.borrow(), unsafe {
|
|
mem::transmute::<&'_ Window, &'static Window>(window)
|
|
})
|
|
.expect("Failed to create a softbuffer surface")
|
|
})
|
|
}
|
|
|
|
fn destroy_surface(&mut self, window: &Window) {
|
|
self.surfaces.remove(&window.id());
|
|
}
|
|
}
|
|
|
|
pub 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.get_or_insert_with(|| GraphicsContext::new(window)).create_surface(window);
|
|
|
|
// Fill a buffer with a solid color.
|
|
const DARK_GRAY: u32 = 0xff181818;
|
|
|
|
surface.resize(width, height).expect("Failed to resize the softbuffer surface");
|
|
|
|
let mut buffer = surface.buffer_mut().expect("Failed to get the softbuffer buffer");
|
|
buffer.fill(DARK_GRAY);
|
|
buffer.present().expect("Failed to present the softbuffer buffer");
|
|
})
|
|
}
|
|
|
|
#[allow(dead_code)]
|
|
pub fn cleanup_window(window: &Window) {
|
|
GC.with(|gc| {
|
|
let mut gc = gc.borrow_mut();
|
|
if let Some(context) = gc.as_mut() {
|
|
context.destroy_surface(window);
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
#[cfg(not(all(feature = "rwh_05", not(any(target_os = "android", target_os = "ios")))))]
|
|
mod platform {
|
|
pub fn fill_window(_window: &winit::window::Window) {
|
|
// No-op on mobile platforms.
|
|
}
|
|
|
|
#[allow(dead_code)]
|
|
pub fn cleanup_window(_window: &winit::window::Window) {
|
|
// No-op on mobile platforms.
|
|
}
|
|
}
|