Skip to content

Commit

Permalink
Split out Surface from Window
Browse files Browse the repository at this point in the history
This PR splits some methods of Window into a new supertrait Surface, laying groundwork for the implementation of rust-windowing#3928.

As stated there, this split is needed because popups and subsurfaces may not necessarily allow the same operations as windows.
  • Loading branch information
jgcodes2020 committed Oct 24, 2024
1 parent c913cda commit 14882ab
Show file tree
Hide file tree
Showing 51 changed files with 1,712 additions and 1,624 deletions.
20 changes: 10 additions & 10 deletions examples/child_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ fn main() -> Result<(), impl std::error::Error> {

use winit::application::ApplicationHandler;
use winit::dpi::{LogicalPosition, LogicalSize, Position};
use winit::event::{ElementState, KeyEvent, WindowEvent};
use winit::event::{ElementState, KeyEvent, SurfaceEvent};
use winit::event_loop::{ActiveEventLoop, EventLoop};
use winit::raw_window_handle::HasRawWindowHandle;
use winit::window::{Window, WindowAttributes, WindowId};
use winit::window::{SurfaceId, Window, WindowAttributes};

#[path = "util/fill.rs"]
mod fill;

#[derive(Default)]
struct Application {
parent_window_id: Option<WindowId>,
windows: HashMap<WindowId, Box<dyn Window>>,
parent_window_id: Option<SurfaceId>,
windows: HashMap<SurfaceId, Box<dyn Window>>,
}

impl ApplicationHandler for Application {
Expand All @@ -36,23 +36,23 @@ fn main() -> Result<(), impl std::error::Error> {
fn window_event(
&mut self,
event_loop: &dyn ActiveEventLoop,
window_id: winit::window::WindowId,
event: WindowEvent,
window_id: winit::window::SurfaceId,
event: SurfaceEvent,
) {
match event {
WindowEvent::CloseRequested => {
SurfaceEvent::CloseRequested => {
self.windows.clear();
event_loop.exit();
},
WindowEvent::PointerEntered { device_id: _, .. } => {
SurfaceEvent::PointerEntered { device_id: _, .. } => {
// On x11, println when the cursor entered in a window even if the child window
// is created by some key inputs.
// the child windows are always placed at (0, 0) with size (200, 200) in the
// parent window, so we also can see this log when we move
// the cursor around (200, 200) in parent window.
println!("cursor entered in the window {window_id:?}");
},
WindowEvent::KeyboardInput {
SurfaceEvent::KeyboardInput {
event: KeyEvent { state: ElementState::Pressed, .. },
..
} => {
Expand All @@ -62,7 +62,7 @@ fn main() -> Result<(), impl std::error::Error> {
println!("Child window created with id: {child_id:?}");
self.windows.insert(child_id, child_window);
},
WindowEvent::RedrawRequested => {
SurfaceEvent::RedrawRequested => {
if let Some(window) = self.windows.get(&window_id) {
fill::fill_window(window.as_ref());
}
Expand Down
14 changes: 7 additions & 7 deletions examples/control_flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ use ::tracing::{info, warn};
#[cfg(web_platform)]
use web_time as time;
use winit::application::ApplicationHandler;
use winit::event::{ElementState, KeyEvent, StartCause, WindowEvent};
use winit::event::{ElementState, KeyEvent, StartCause, SurfaceEvent};
use winit::event_loop::{ActiveEventLoop, ControlFlow, EventLoop};
use winit::keyboard::{Key, NamedKey};
use winit::window::{Window, WindowAttributes, WindowId};
use winit::window::{SurfaceId, Window, WindowAttributes};

#[path = "util/fill.rs"]
mod fill;
Expand Down Expand Up @@ -75,16 +75,16 @@ impl ApplicationHandler for ControlFlowDemo {
fn window_event(
&mut self,
_event_loop: &dyn ActiveEventLoop,
_window_id: WindowId,
event: WindowEvent,
_window_id: SurfaceId,
event: SurfaceEvent,
) {
info!("{event:?}");

match event {
WindowEvent::CloseRequested => {
SurfaceEvent::CloseRequested => {
self.close_requested = true;
},
WindowEvent::KeyboardInput {
SurfaceEvent::KeyboardInput {
event: KeyEvent { logical_key: key, state: ElementState::Pressed, .. },
..
} => match key.as_ref() {
Expand All @@ -111,7 +111,7 @@ impl ApplicationHandler for ControlFlowDemo {
},
_ => (),
},
WindowEvent::RedrawRequested => {
SurfaceEvent::RedrawRequested => {
let window = self.window.as_ref().unwrap();
window.pre_present_notify();
fill::fill_window(window.as_ref());
Expand Down
12 changes: 6 additions & 6 deletions examples/pump_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ fn main() -> std::process::ExitCode {
use std::time::Duration;

use winit::application::ApplicationHandler;
use winit::event::WindowEvent;
use winit::event::SurfaceEvent;
use winit::event_loop::{ActiveEventLoop, EventLoop};
use winit::platform::pump_events::{EventLoopExtPumpEvents, PumpStatus};
use winit::window::{Window, WindowAttributes, WindowId};
use winit::window::{SurfaceId, Window, WindowAttributes};

#[path = "util/fill.rs"]
mod fill;
Expand All @@ -30,8 +30,8 @@ fn main() -> std::process::ExitCode {
fn window_event(
&mut self,
event_loop: &dyn ActiveEventLoop,
_window_id: WindowId,
event: WindowEvent,
_window_id: SurfaceId,
event: SurfaceEvent,
) {
println!("{event:?}");

Expand All @@ -41,8 +41,8 @@ fn main() -> std::process::ExitCode {
};

match event {
WindowEvent::CloseRequested => event_loop.exit(),
WindowEvent::RedrawRequested => {
SurfaceEvent::CloseRequested => event_loop.exit(),
SurfaceEvent::RedrawRequested => {
fill::fill_window(window.as_ref());
window.request_redraw();
},
Expand Down
16 changes: 8 additions & 8 deletions examples/run_on_demand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
use std::time::Duration;

use winit::application::ApplicationHandler;
use winit::event::WindowEvent;
use winit::event::SurfaceEvent;
use winit::event_loop::{ActiveEventLoop, EventLoop};
use winit::platform::run_on_demand::EventLoopExtRunOnDemand;
use winit::window::{Window, WindowAttributes, WindowId};
use winit::window::{SurfaceId, Window, WindowAttributes};

#[path = "util/fill.rs"]
mod fill;

#[derive(Default)]
struct App {
idx: usize,
window_id: Option<WindowId>,
window_id: Option<SurfaceId>,
window: Option<Box<dyn Window>>,
}

Expand All @@ -40,10 +40,10 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
fn window_event(
&mut self,
event_loop: &dyn ActiveEventLoop,
window_id: WindowId,
event: WindowEvent,
window_id: SurfaceId,
event: SurfaceEvent,
) {
if event == WindowEvent::Destroyed && self.window_id == Some(window_id) {
if event == SurfaceEvent::Destroyed && self.window_id == Some(window_id) {
println!(
"--------------------------------------------------------- Window {} Destroyed",
self.idx
Expand All @@ -59,7 +59,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
};

match event {
WindowEvent::CloseRequested => {
SurfaceEvent::CloseRequested => {
println!(
"--------------------------------------------------------- Window {} \
CloseRequested",
Expand All @@ -68,7 +68,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
fill::cleanup_window(window.as_ref());
self.window = None;
},
WindowEvent::RedrawRequested => {
SurfaceEvent::RedrawRequested => {
fill::fill_window(window.as_ref());
},
_ => (),
Expand Down
4 changes: 2 additions & 2 deletions examples/util/fill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ mod platform {
use std::num::NonZeroU32;

use softbuffer::{Context, Surface};
use winit::window::{Window, WindowId};
use winit::window::{SurfaceId, Window};

thread_local! {
// NOTE: You should never do things like that, create context and drop it before
Expand All @@ -37,7 +37,7 @@ mod platform {
context: RefCell<Context<&'static dyn Window>>,

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

impl GraphicsContext {
Expand Down
Loading

0 comments on commit 14882ab

Please sign in to comment.