diff --git a/examples/open_parented/src/main.rs b/examples/open_parented/src/main.rs index 4125bd1a..f4cfba9a 100644 --- a/examples/open_parented/src/main.rs +++ b/examples/open_parented/src/main.rs @@ -1,6 +1,6 @@ use baseview::{ Event, EventStatus, PhySize, Window, WindowEvent, WindowHandle, WindowHandler, - WindowScalePolicy, + WindowOpenOptions, }; use std::num::NonZeroU32; @@ -19,12 +19,9 @@ impl ParentWindowHandler { let mut surface = unsafe { softbuffer::Surface::new(&ctx, window) }.unwrap(); surface.resize(NonZeroU32::new(512).unwrap(), NonZeroU32::new(512).unwrap()).unwrap(); - let window_open_options = baseview::WindowOpenOptions { - title: "baseview child".into(), - size: baseview::Size::new(256.0, 256.0), - scale: WindowScalePolicy::SystemScaleFactor, - gl_config: None, - }; + let window_open_options = + WindowOpenOptions::new().with_size(256.0, 256.0).with_title("baseview child"); + let child_window = Window::open_parented(window, window_open_options, ChildWindowHandler::new); @@ -124,14 +121,7 @@ impl WindowHandler for ChildWindowHandler { } fn main() { - let window_open_options = baseview::WindowOpenOptions { - title: "baseview".into(), - size: baseview::Size::new(512.0, 512.0), - scale: WindowScalePolicy::SystemScaleFactor, - - // TODO: Add an example that uses the OpenGL context - gl_config: None, - }; + let window_open_options = WindowOpenOptions::new().with_size(512.0, 512.0); Window::open_blocking(window_open_options, ParentWindowHandler::new); } diff --git a/examples/open_window/src/main.rs b/examples/open_window/src/main.rs index f91facd3..c943317a 100644 --- a/examples/open_window/src/main.rs +++ b/examples/open_window/src/main.rs @@ -6,7 +6,7 @@ use rtrb::{Consumer, RingBuffer}; #[cfg(target_os = "macos")] use baseview::{copy_to_clipboard, MouseEvent}; use baseview::{ - Event, EventStatus, PhySize, Window, WindowEvent, WindowHandler, WindowScalePolicy, + Event, EventStatus, PhySize, Window, WindowEvent, WindowHandler, WindowOpenOptions, }; #[derive(Debug, Clone)] @@ -63,12 +63,7 @@ impl WindowHandler for OpenWindowExample { } fn main() { - let window_open_options = baseview::WindowOpenOptions { - title: "baseview".into(), - size: baseview::Size::new(512.0, 512.0), - scale: WindowScalePolicy::SystemScaleFactor, - gl_config: None, - }; + let window_open_options = WindowOpenOptions::new().with_size(512.0, 512.0); let (mut tx, rx) = RingBuffer::new(128); diff --git a/examples/render_femtovg/src/main.rs b/examples/render_femtovg/src/main.rs index 0f5504b7..643f033d 100644 --- a/examples/render_femtovg/src/main.rs +++ b/examples/render_femtovg/src/main.rs @@ -1,7 +1,7 @@ use baseview::gl::GlConfig; use baseview::{ Event, EventStatus, MouseEvent, PhyPoint, Size, Window, WindowEvent, WindowHandler, WindowInfo, - WindowOpenOptions, WindowScalePolicy, + WindowOpenOptions, }; use femtovg::renderer::OpenGl; use femtovg::{Canvas, Color}; @@ -95,13 +95,10 @@ impl WindowHandler for FemtovgExample { } fn main() { - let window_open_options = WindowOpenOptions { - title: "Femtovg on Baseview".into(), - size: Size::new(512.0, 512.0), - scale: WindowScalePolicy::SystemScaleFactor, - - gl_config: Some(GlConfig { alpha_bits: 8, ..GlConfig::default() }), - }; + let window_open_options = WindowOpenOptions::new() + .with_title("Femtovg on Baseview") + .with_size(512.0, 512.0) + .with_gl_config(GlConfig { alpha_bits: 8, ..GlConfig::default() }); Window::open_blocking(window_open_options, FemtovgExample::new); } diff --git a/src/window_open_options.rs b/src/window_open_options.rs index caadcc54..753edb90 100644 --- a/src/window_open_options.rs +++ b/src/window_open_options.rs @@ -30,11 +30,43 @@ pub struct WindowOpenOptions { /// If provided, then an OpenGL context will be created for this window. You'll be able to /// access this context through [crate::Window::gl_context]. /// - /// By default this is set to `None`. + /// By default, this is set to `None`. #[cfg(feature = "opengl")] pub gl_config: Option, } +impl WindowOpenOptions { + #[inline] + pub fn new() -> Self { + Self::default() + } + + #[inline] + pub fn with_title(mut self, title: impl Into) -> Self { + self.title = title.into(); + self + } + + #[inline] + pub fn with_size(mut self, width: f64, height: f64) -> Self { + self.size = Size::new(width, height); + self + } + + #[inline] + pub fn with_scale_policy(mut self, scale: WindowScalePolicy) -> Self { + self.scale = scale; + self + } + + #[cfg(feature = "opengl")] + #[inline] + pub fn with_gl_config(mut self, gl_config: impl Into>) -> Self { + self.gl_config = gl_config.into(); + self + } +} + impl Default for WindowOpenOptions { fn default() -> Self { Self {