Skip to content

Commit 14882ab

Browse files
committed
Split out Surface from Window
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.
1 parent c913cda commit 14882ab

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+1712
-1624
lines changed

examples/child_window.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@ fn main() -> Result<(), impl std::error::Error> {
55

66
use winit::application::ApplicationHandler;
77
use winit::dpi::{LogicalPosition, LogicalSize, Position};
8-
use winit::event::{ElementState, KeyEvent, WindowEvent};
8+
use winit::event::{ElementState, KeyEvent, SurfaceEvent};
99
use winit::event_loop::{ActiveEventLoop, EventLoop};
1010
use winit::raw_window_handle::HasRawWindowHandle;
11-
use winit::window::{Window, WindowAttributes, WindowId};
11+
use winit::window::{SurfaceId, Window, WindowAttributes};
1212

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

1616
#[derive(Default)]
1717
struct Application {
18-
parent_window_id: Option<WindowId>,
19-
windows: HashMap<WindowId, Box<dyn Window>>,
18+
parent_window_id: Option<SurfaceId>,
19+
windows: HashMap<SurfaceId, Box<dyn Window>>,
2020
}
2121

2222
impl ApplicationHandler for Application {
@@ -36,23 +36,23 @@ fn main() -> Result<(), impl std::error::Error> {
3636
fn window_event(
3737
&mut self,
3838
event_loop: &dyn ActiveEventLoop,
39-
window_id: winit::window::WindowId,
40-
event: WindowEvent,
39+
window_id: winit::window::SurfaceId,
40+
event: SurfaceEvent,
4141
) {
4242
match event {
43-
WindowEvent::CloseRequested => {
43+
SurfaceEvent::CloseRequested => {
4444
self.windows.clear();
4545
event_loop.exit();
4646
},
47-
WindowEvent::PointerEntered { device_id: _, .. } => {
47+
SurfaceEvent::PointerEntered { device_id: _, .. } => {
4848
// On x11, println when the cursor entered in a window even if the child window
4949
// is created by some key inputs.
5050
// the child windows are always placed at (0, 0) with size (200, 200) in the
5151
// parent window, so we also can see this log when we move
5252
// the cursor around (200, 200) in parent window.
5353
println!("cursor entered in the window {window_id:?}");
5454
},
55-
WindowEvent::KeyboardInput {
55+
SurfaceEvent::KeyboardInput {
5656
event: KeyEvent { state: ElementState::Pressed, .. },
5757
..
5858
} => {
@@ -62,7 +62,7 @@ fn main() -> Result<(), impl std::error::Error> {
6262
println!("Child window created with id: {child_id:?}");
6363
self.windows.insert(child_id, child_window);
6464
},
65-
WindowEvent::RedrawRequested => {
65+
SurfaceEvent::RedrawRequested => {
6666
if let Some(window) = self.windows.get(&window_id) {
6767
fill::fill_window(window.as_ref());
6868
}

examples/control_flow.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ use ::tracing::{info, warn};
88
#[cfg(web_platform)]
99
use web_time as time;
1010
use winit::application::ApplicationHandler;
11-
use winit::event::{ElementState, KeyEvent, StartCause, WindowEvent};
11+
use winit::event::{ElementState, KeyEvent, StartCause, SurfaceEvent};
1212
use winit::event_loop::{ActiveEventLoop, ControlFlow, EventLoop};
1313
use winit::keyboard::{Key, NamedKey};
14-
use winit::window::{Window, WindowAttributes, WindowId};
14+
use winit::window::{SurfaceId, Window, WindowAttributes};
1515

1616
#[path = "util/fill.rs"]
1717
mod fill;
@@ -75,16 +75,16 @@ impl ApplicationHandler for ControlFlowDemo {
7575
fn window_event(
7676
&mut self,
7777
_event_loop: &dyn ActiveEventLoop,
78-
_window_id: WindowId,
79-
event: WindowEvent,
78+
_window_id: SurfaceId,
79+
event: SurfaceEvent,
8080
) {
8181
info!("{event:?}");
8282

8383
match event {
84-
WindowEvent::CloseRequested => {
84+
SurfaceEvent::CloseRequested => {
8585
self.close_requested = true;
8686
},
87-
WindowEvent::KeyboardInput {
87+
SurfaceEvent::KeyboardInput {
8888
event: KeyEvent { logical_key: key, state: ElementState::Pressed, .. },
8989
..
9090
} => match key.as_ref() {
@@ -111,7 +111,7 @@ impl ApplicationHandler for ControlFlowDemo {
111111
},
112112
_ => (),
113113
},
114-
WindowEvent::RedrawRequested => {
114+
SurfaceEvent::RedrawRequested => {
115115
let window = self.window.as_ref().unwrap();
116116
window.pre_present_notify();
117117
fill::fill_window(window.as_ref());

examples/pump_events.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ fn main() -> std::process::ExitCode {
88
use std::time::Duration;
99

1010
use winit::application::ApplicationHandler;
11-
use winit::event::WindowEvent;
11+
use winit::event::SurfaceEvent;
1212
use winit::event_loop::{ActiveEventLoop, EventLoop};
1313
use winit::platform::pump_events::{EventLoopExtPumpEvents, PumpStatus};
14-
use winit::window::{Window, WindowAttributes, WindowId};
14+
use winit::window::{SurfaceId, Window, WindowAttributes};
1515

1616
#[path = "util/fill.rs"]
1717
mod fill;
@@ -30,8 +30,8 @@ fn main() -> std::process::ExitCode {
3030
fn window_event(
3131
&mut self,
3232
event_loop: &dyn ActiveEventLoop,
33-
_window_id: WindowId,
34-
event: WindowEvent,
33+
_window_id: SurfaceId,
34+
event: SurfaceEvent,
3535
) {
3636
println!("{event:?}");
3737

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

4343
match event {
44-
WindowEvent::CloseRequested => event_loop.exit(),
45-
WindowEvent::RedrawRequested => {
44+
SurfaceEvent::CloseRequested => event_loop.exit(),
45+
SurfaceEvent::RedrawRequested => {
4646
fill::fill_window(window.as_ref());
4747
window.request_redraw();
4848
},

examples/run_on_demand.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,18 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
66
use std::time::Duration;
77

88
use winit::application::ApplicationHandler;
9-
use winit::event::WindowEvent;
9+
use winit::event::SurfaceEvent;
1010
use winit::event_loop::{ActiveEventLoop, EventLoop};
1111
use winit::platform::run_on_demand::EventLoopExtRunOnDemand;
12-
use winit::window::{Window, WindowAttributes, WindowId};
12+
use winit::window::{SurfaceId, Window, WindowAttributes};
1313

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

1717
#[derive(Default)]
1818
struct App {
1919
idx: usize,
20-
window_id: Option<WindowId>,
20+
window_id: Option<SurfaceId>,
2121
window: Option<Box<dyn Window>>,
2222
}
2323

@@ -40,10 +40,10 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
4040
fn window_event(
4141
&mut self,
4242
event_loop: &dyn ActiveEventLoop,
43-
window_id: WindowId,
44-
event: WindowEvent,
43+
window_id: SurfaceId,
44+
event: SurfaceEvent,
4545
) {
46-
if event == WindowEvent::Destroyed && self.window_id == Some(window_id) {
46+
if event == SurfaceEvent::Destroyed && self.window_id == Some(window_id) {
4747
println!(
4848
"--------------------------------------------------------- Window {} Destroyed",
4949
self.idx
@@ -59,7 +59,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
5959
};
6060

6161
match event {
62-
WindowEvent::CloseRequested => {
62+
SurfaceEvent::CloseRequested => {
6363
println!(
6464
"--------------------------------------------------------- Window {} \
6565
CloseRequested",
@@ -68,7 +68,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
6868
fill::cleanup_window(window.as_ref());
6969
self.window = None;
7070
},
71-
WindowEvent::RedrawRequested => {
71+
SurfaceEvent::RedrawRequested => {
7272
fill::fill_window(window.as_ref());
7373
},
7474
_ => (),

examples/util/fill.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ mod platform {
2020
use std::num::NonZeroU32;
2121

2222
use softbuffer::{Context, Surface};
23-
use winit::window::{Window, WindowId};
23+
use winit::window::{SurfaceId, Window};
2424

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

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

4343
impl GraphicsContext {

0 commit comments

Comments
 (0)