Skip to content

Commit

Permalink
Update dev-dependencies (#3641)
Browse files Browse the repository at this point in the history
  • Loading branch information
daxpedda committed Apr 18, 2024
1 parent c15fa6e commit 4f47a4e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 14 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,13 @@ smol_str = "0.2.0"
tracing = { version = "0.1.40", default_features = false }

[dev-dependencies]
image = { version = "0.24.0", default-features = false, features = ["png"] }
image = { version = "0.25.0", default-features = false, features = ["png"] }
tracing = { version = "0.1.40", default_features = false, features = ["log"] }
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
winit = { path = ".", features = ["rwh_05"] }

[target.'cfg(not(any(target_os = "android", target_os = "ios")))'.dev-dependencies]
softbuffer = { version = "0.3.0", default-features = false, features = [
softbuffer = { version = "0.4.0", default-features = false, features = [
"x11",
"x11-dlopen",
"wayland",
Expand Down
21 changes: 15 additions & 6 deletions examples/util/fill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub use platform::fill_window;
mod platform {
use std::cell::RefCell;
use std::collections::HashMap;
use std::mem;
use std::mem::ManuallyDrop;
use std::num::NonZeroU32;

Expand All @@ -34,24 +35,32 @@ mod platform {
/// The graphics context used to draw to a window.
struct GraphicsContext {
/// The global softbuffer context.
context: Context,
context: RefCell<Context<&'static Window>>,

/// The hash map of window IDs to surfaces.
surfaces: HashMap<WindowId, Surface>,
surfaces: HashMap<WindowId, Surface<&'static Window, &'static Window>>,
}

impl GraphicsContext {
fn new(w: &Window) -> Self {
Self {
context: unsafe { Context::new(w) }.expect("Failed to create a softbuffer context"),
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 {
fn create_surface(
&mut self,
window: &Window,
) -> &mut Surface<&'static Window, &'static Window> {
self.surfaces.entry(window.id()).or_insert_with(|| {
unsafe { Surface::new(&self.context, window) }
.expect("Failed to create a softbuffer surface")
Surface::new(&self.context.borrow(), unsafe {
mem::transmute::<&'_ Window, &'static Window>(window)
})
.expect("Failed to create a softbuffer surface")
})
}

Expand Down
22 changes: 16 additions & 6 deletions examples/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ use std::fmt::Debug;
use std::mem;
#[cfg(not(any(android_platform, ios_platform)))]
use std::num::NonZeroU32;
use std::sync::Arc;

use ::tracing::{error, info};
use cursor_icon::CursorIcon;
#[cfg(not(any(android_platform, ios_platform)))]
use rwh_05::HasRawDisplayHandle;
use rwh_06::{DisplayHandle, HasDisplayHandle};
#[cfg(not(any(android_platform, ios_platform)))]
use softbuffer::{Context, Surface};

Expand Down Expand Up @@ -83,14 +84,21 @@ struct Application {
///
/// With OpenGL it could be EGLDisplay.
#[cfg(not(any(android_platform, ios_platform)))]
context: Option<Context>,
context: Option<Context<DisplayHandle<'static>>>,
}

impl Application {
fn new<T>(event_loop: &EventLoop<T>) -> Self {
// SAFETY: we drop the context right before the event loop is stopped, thus making it safe.
#[cfg(not(any(android_platform, ios_platform)))]
let context = Some(unsafe { Context::from_raw(event_loop.raw_display_handle()).unwrap() });
let context = Some(
Context::new(unsafe {
std::mem::transmute::<DisplayHandle<'_>, DisplayHandle<'static>>(
event_loop.display_handle().unwrap(),
)
})
.unwrap(),
);

// You'll have to choose an icon size at your own discretion. On X11, the desired size varies
// by WM, and on Windows, you still have to account for screen scaling. Here we use 32px,
Expand Down Expand Up @@ -494,9 +502,9 @@ struct WindowState {
///
/// NOTE: This surface must be dropped before the `Window`.
#[cfg(not(any(android_platform, ios_platform)))]
surface: Surface,
surface: Surface<DisplayHandle<'static>, Arc<Window>>,
/// The actual winit Window.
window: Window,
window: Arc<Window>,
/// The window theme we're drawing with.
theme: Theme,
/// Cursor position over the window.
Expand All @@ -523,10 +531,12 @@ struct WindowState {

impl WindowState {
fn new(app: &Application, window: Window) -> Result<Self, Box<dyn Error>> {
let window = Arc::new(window);

// SAFETY: the surface is dropped before the `window` which provided it with handle, thus
// it doesn't outlive it.
#[cfg(not(any(android_platform, ios_platform)))]
let surface = unsafe { Surface::new(app.context.as_ref().unwrap(), &window)? };
let surface = Surface::new(app.context.as_ref().unwrap(), Arc::clone(&window))?;

let theme = window.theme().unwrap_or(Theme::Dark);
info!("Theme: {theme:?}");
Expand Down

0 comments on commit 4f47a4e

Please sign in to comment.