Skip to content

Commit d7ec5fe

Browse files
committed
wip
1 parent 35d0db2 commit d7ec5fe

7 files changed

Lines changed: 53 additions & 54 deletions

File tree

examples/open_parented/src/main.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use baseview::dpi::LogicalSize;
22
use baseview::{
3-
Event, EventStatus, HandlerError, WindowContext, WindowHandle, WindowHandler,
4-
WindowOpenOptions, WindowSize,
3+
Event, EventStatus, HandlerError, Window, WindowContext, WindowHandler, WindowOpenOptions,
4+
WindowSize,
55
};
66
use std::cell::{Cell, RefCell};
77
use std::num::NonZeroU32;
@@ -10,7 +10,7 @@ struct ParentWindowHandler {
1010
surface: RefCell<softbuffer::Surface<WindowContext, WindowContext>>,
1111
damaged: Cell<bool>,
1212

13-
child_window: WindowHandle,
13+
child_window: Window,
1414
}
1515

1616
impl ParentWindowHandler {
@@ -25,7 +25,7 @@ impl ParentWindowHandler {
2525
.with_parent(&window)
2626
.with_title("baseview child");
2727

28-
let child_window = baseview::create_window(window_open_options, ChildWindowHandler::new)?;
28+
let child_window = Window::create(window_open_options, ChildWindowHandler::new)?;
2929

3030
Ok(Self { surface: surface.into(), damaged: true.into(), child_window })
3131
}
@@ -130,7 +130,7 @@ impl WindowHandler for ChildWindowHandler {
130130
fn main() -> Result<(), baseview::Error> {
131131
let window_open_options = WindowOpenOptions::new().with_size(LogicalSize::new(512.0, 512.0));
132132

133-
baseview::create_window(window_open_options, ParentWindowHandler::new)?.run_until_closed()?;
133+
Window::create(window_open_options, ParentWindowHandler::new)?.run_until_closed()?;
134134

135135
Ok(())
136136
}

examples/open_window/src/main.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ use rtrb::{Consumer, RingBuffer};
88
use baseview::copy_to_clipboard;
99
use baseview::dpi::{LogicalSize, PhysicalPosition};
1010
use baseview::{
11-
Event, EventStatus, HandlerError, MouseEvent, WindowContext, WindowHandler, WindowOpenOptions,
12-
WindowSize,
11+
Event, EventStatus, HandlerError, MouseEvent, Window, WindowContext, WindowHandler,
12+
WindowOpenOptions, WindowSize,
1313
};
1414

1515
#[derive(Debug, Clone)]
@@ -152,7 +152,7 @@ fn main() -> Result<(), baseview::Error> {
152152
}
153153
});
154154

155-
baseview::create_window(window_open_options, |window| {
155+
Window::create(window_open_options, |window| {
156156
let ctx = softbuffer::Context::new(window.clone())?;
157157
let mut surface = softbuffer::Surface::new(&ctx, window.clone())?;
158158
let size = window.size().physical;

examples/plugin_clack/src/gui.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::ExamplePluginMainThread;
33
use baseview::dpi::*;
44
use baseview::gl::GlConfig;
55
use baseview::host::{Host, HostCallbacks, HostMainThreadCaller};
6-
use baseview::{HandlerError, WindowHandle, WindowOpenOptions, WindowSize};
6+
use baseview::{HandlerError, Window, WindowOpenOptions, WindowSize};
77
use clack_extensions::gui::{
88
AspectRatioStrategy, GuiApiType, GuiConfiguration, GuiResizeHints, GuiSize, HostGui,
99
PluginGuiImpl, Window as ClapWindow,
@@ -14,7 +14,7 @@ use clack_plugin::prelude::{HostMainThreadHandle, HostSharedHandle};
1414
use raw_window_handle::HasRawWindowHandle;
1515

1616
pub struct ExamplePluginGui {
17-
pub handle: WindowHandle,
17+
pub handle: Window,
1818
}
1919

2020
impl PluginGuiImpl for ExamplePluginMainThread<'_> {
@@ -45,7 +45,7 @@ impl PluginGuiImpl for ExamplePluginMainThread<'_> {
4545
});
4646
}
4747

48-
let window = baseview::create_window_with_host(options, OpenWindowExample::new, host)?;
48+
let window = Window::create_with_host(options, OpenWindowExample::new, host)?;
4949

5050
self.gui = Some(ExamplePluginGui { handle: window });
5151
Ok(())

examples/render_femtovg/src/main.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use baseview::dpi::{LogicalSize, PhysicalPosition};
22
use baseview::gl::{GlConfig, GlContext};
33
use baseview::{
4-
Event, EventStatus, HandlerError, MouseEvent, WindowContext, WindowHandler, WindowOpenOptions,
5-
WindowSize,
4+
Event, EventStatus, HandlerError, MouseEvent, Window, WindowContext, WindowHandler,
5+
WindowOpenOptions, WindowSize,
66
};
77
use femtovg::renderer::OpenGl;
88
use femtovg::{Canvas, Color};
@@ -123,7 +123,7 @@ fn main() -> Result<(), baseview::Error> {
123123
.with_size(LogicalSize::new(512, 512))
124124
.with_gl_config(GlConfig { alpha_bits: 8, ..GlConfig::default() });
125125

126-
baseview::create_window(window_open_options, FemtovgExample::new)?.run_until_closed()?;
126+
Window::create(window_open_options, FemtovgExample::new)?.run_until_closed()?;
127127
Ok(())
128128
}
129129

examples/render_wgpu/src/main.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use baseview::dpi::{LogicalSize, PhysicalSize};
22
use baseview::{
3-
Event, EventStatus, HandlerError, WindowContext, WindowHandler, WindowOpenOptions, WindowSize,
3+
Event, EventStatus, HandlerError, Window, WindowContext, WindowHandler, WindowOpenOptions,
4+
WindowSize,
45
};
56

67
use log::LevelFilter;
@@ -214,7 +215,7 @@ fn main() -> Result<(), baseview::Error> {
214215
.with_title("WGPU on Baseview")
215216
.with_size(LogicalSize::new(512, 512));
216217

217-
baseview::create_window(window_open_options, |c| pollster::block_on(WgpuExample::new(c)))?
218+
Window::create(window_open_options, |c| pollster::block_on(WgpuExample::new(c)))?
218219
.run_until_closed()?;
219220

220221
Ok(())

src/host.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::cell::RefCell;
33

44
/// A special handler for the Window thread to wake up and call methods on the main thread.
55
///
6-
/// [`WindowHandle::host_main_thread_callback`](crate::WindowHandle::host_main_thread_callback)
6+
/// [`WindowHandle::host_main_thread_callback`](crate::Window::host_main_thread_callback)
77
/// should be called as a response to this.
88
///
99
/// # Platform compatibility notes
@@ -12,7 +12,7 @@ use std::cell::RefCell;
1212
pub trait HostMainThreadCaller: Send + 'static {
1313
/// Schedules a callback on the main thread.
1414
///
15-
/// [`WindowHandle::host_main_thread_callback`](crate::WindowHandle::host_main_thread_callback)
15+
/// [`WindowHandle::host_main_thread_callback`](crate::Window::host_main_thread_callback)
1616
/// should be called as a response to this.
1717
///
1818
/// # Platform compatibility notes
@@ -50,7 +50,7 @@ pub trait HostCallbacks: 'static {
5050
///
5151
/// It also brings the additional safety guarantee that all handlers given to this types will be
5252
/// destroyed alongside with the window, guaranteeing callbacks cannot be fired after the
53-
/// [`WindowHandle`](crate::WindowHandle) is dropped. (or after this [`Host`] object is dropped, if
53+
/// [`WindowHandle`](crate::Window) is dropped. (or after this [`Host`] object is dropped, if
5454
/// it never made it to a [`create_window_with_host`](crate::create_window_with_host) call).
5555
pub struct Host {
5656
#[cfg(target_os = "linux")]

src/window.rs

Lines changed: 33 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,39 +5,57 @@ use crate::*;
55
use dpi::{LogicalSize, PhysicalSize, Pixel, Size};
66
use std::marker::PhantomData;
77

8-
pub struct WindowHandle {
9-
window_handle: platform::WindowHandle,
8+
pub struct Window {
9+
inner: platform::WindowHandle,
1010
// so that WindowHandle is !Send on all platforms
1111
phantom: PhantomData<*mut ()>,
1212
}
1313

14-
impl WindowHandle {
14+
impl Window {
1515
#[inline]
16-
fn new(window_handle: platform::WindowHandle) -> Self {
17-
Self { window_handle, phantom: PhantomData }
16+
pub fn create<H: WindowHandler>(
17+
builder: WindowOpenOptions,
18+
handler: impl FnOnce(WindowContext) -> Result<H, HandlerError> + Send + 'static,
19+
) -> Result<Window, Error> {
20+
Self::create_with_host(builder, handler, None)
21+
}
22+
23+
pub fn create_with_host<H: WindowHandler>(
24+
builder: WindowOpenOptions,
25+
handler: impl FnOnce(WindowContext) -> Result<H, HandlerError> + Send + 'static,
26+
host: impl Into<Option<Host>>,
27+
) -> Result<Window, Error> {
28+
Ok(Self {
29+
inner: platform::WindowHandle::create_window(
30+
builder,
31+
WindowHandlerBuilder::new(handler),
32+
host.into().unwrap_or_else(Host::default),
33+
)?,
34+
phantom: PhantomData,
35+
})
1836
}
1937

2038
/// Blocks the thread and runs an event loop until the window is closed.
2139
///
2240
/// The window is shown automatically if it wasn't already.
2341
#[inline]
2442
pub fn run_until_closed(self) -> Result<(), Error> {
25-
self.window_handle.run_until_closed()?;
43+
self.inner.run_until_closed()?;
2644
Ok(())
2745
}
2846

2947
/// The current size of the window.
3048
#[inline]
3149
pub fn size(&self) -> WindowSize {
32-
self.window_handle.size()
50+
self.inner.size()
3351
}
3452

3553
/// Resizes the window to the given [`Size`].
3654
///
3755
/// The `size` can be provided in either physical or logical pixels.
3856
#[inline]
3957
pub fn resize(&self, size: Size) -> Result<(), Error> {
40-
self.window_handle.resize(size)?;
58+
self.inner.resize(size)?;
4159
Ok(())
4260
}
4361

@@ -57,7 +75,7 @@ impl WindowHandle {
5775
/// On macOS, this function is always a no-op.
5876
#[inline]
5977
pub fn suggest_fallback_scale_factor(&self, scale_factor: f64) -> Result<(), Error> {
60-
self.window_handle.suggest_scale_factor(scale_factor)?;
78+
self.inner.suggest_scale_factor(scale_factor)?;
6179
Ok(())
6280
}
6381

@@ -68,7 +86,7 @@ impl WindowHandle {
6886
/// It is guaranteed that no other objects (e.g. the parent window) are used by this window after
6987
/// this call.
7088
///
71-
/// Calling this method is more explicit, but otherwise identical to just dropping this [`WindowHandle`].
89+
/// Calling this method is more explicit, but otherwise identical to just dropping this [`Window`].
7290
#[inline]
7391
pub fn close(self) {
7492
drop(self)
@@ -78,7 +96,7 @@ impl WindowHandle {
7896
/// if the window was closed/dropped.
7997
#[inline]
8098
pub fn is_open(&self) -> bool {
81-
self.window_handle.is_open()
99+
self.inner.is_open()
82100
}
83101

84102
/// Performs the work the window thread had scheduled for the main thread.
@@ -92,22 +110,22 @@ impl WindowHandle {
92110
/// On Windows and macOS, this is always a no-op.
93111
#[inline]
94112
pub fn host_main_thread_callback(&mut self) {
95-
self.window_handle.handle_main_thread_callback()
113+
self.inner.handle_main_thread_callback()
96114
}
97115

98116
/// Reparents this window using the given `parent`.
99117
///
100118
/// If the window was a floating window, it will become parented.
101119
#[inline]
102120
pub fn set_parent(&self, parent: impl Into<ParentWindowHandle>) -> Result<(), Error> {
103-
self.window_handle.set_parent(parent.into().inner)?;
121+
self.inner.set_parent(parent.into().inner)?;
104122
Ok(())
105123
}
106124

107125
/// Shows the window to the screen.
108126
#[inline]
109127
pub fn show(&self) -> Result<(), Error> {
110-
self.window_handle.show()?;
128+
self.inner.show()?;
111129
Ok(())
112130
}
113131

@@ -117,31 +135,11 @@ impl WindowHandle {
117135
/// paused and the user will not be able to see or interact with it.
118136
#[inline]
119137
pub fn hide(&self) -> Result<(), Error> {
120-
self.window_handle.hide()?;
138+
self.inner.hide()?;
121139
Ok(())
122140
}
123141
}
124142

125-
#[inline]
126-
pub fn create_window<H: WindowHandler>(
127-
builder: WindowOpenOptions,
128-
handler: impl FnOnce(WindowContext) -> Result<H, HandlerError> + Send + 'static,
129-
) -> Result<WindowHandle, Error> {
130-
create_window_with_host(builder, handler, None)
131-
}
132-
133-
pub fn create_window_with_host<H: WindowHandler>(
134-
builder: WindowOpenOptions,
135-
handler: impl FnOnce(WindowContext) -> Result<H, HandlerError> + Send + 'static,
136-
host: impl Into<Option<Host>>,
137-
) -> Result<WindowHandle, Error> {
138-
Ok(WindowHandle::new(platform::WindowHandle::create_window(
139-
builder,
140-
WindowHandlerBuilder::new(handler),
141-
host.into().unwrap_or_else(Host::default),
142-
)?))
143-
}
144-
145143
/// A window's size, which can be read in either logical or physical pixels.
146144
///
147145
/// Methods that produce this type in baseview guarantee that either the physical or the logical

0 commit comments

Comments
 (0)