Skip to content

Commit

Permalink
Create WindowBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
matthunz committed Dec 12, 2023
1 parent c88a9b7 commit ee96548
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 7 deletions.
2 changes: 1 addition & 1 deletion examples/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ fn main() {
let ui = UserInterface::new();
let _guard = ui.enter();

let window = Window {}.spawn();
let window = Window::builder().title("Window A").build().spawn();
let app = App.spawn();

window.cursor_moved().bind(&app, App::cursor_moved);
Expand Down
2 changes: 1 addition & 1 deletion src/any_object.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use slotmap::DefaultKey;
use crate::{HandleState, Object};
use slotmap::DefaultKey;
use std::any::Any;

/// A dynamic reactive object.
Expand Down
6 changes: 3 additions & 3 deletions src/ui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ mod window;
pub use window::{Window, WindowHandle};

struct Inner {
pending_windows: Vec<Handle<Window>>,
pending_windows: Vec<(winit::window::WindowBuilder, Handle<Window>)>,
windows: HashMap<WindowId, (winit::window::Window, Handle<Window>)>,
}

Expand Down Expand Up @@ -83,8 +83,8 @@ impl UserInterface {
self.rt.try_run();

let mut cx = self.context.inner.borrow_mut();
while let Some(handle) = cx.pending_windows.pop() {
let window = winit::window::Window::new(&event_loop).unwrap();
while let Some((builder, handle)) = cx.pending_windows.pop() {
let window = builder.build(event_loop).unwrap();
cx.windows.insert(window.id(), (window, handle));
}
drop(cx);
Expand Down
68 changes: 66 additions & 2 deletions src/ui/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,83 @@ use kurbo::Point;
use winit::{
dpi::PhysicalSize,
event::{ElementState, MouseButton, MouseScrollDelta, TouchPhase},
window::Theme,
};

pub struct WindowBuilder {
window: Option<winit::window::WindowBuilder>,
}

impl Default for WindowBuilder {
fn default() -> Self {
Self {
window: Some(Default::default()),
}
}
}

impl WindowBuilder {
pub fn build(&mut self) -> Window {
Window {
window: self.window.take(),
}
}

pub fn title(&mut self, title: impl Into<String>) -> &mut Self {
self.window = Some(self.window.take().unwrap().with_title(title));
self
}

pub fn is_resizable(&mut self, is_resizable: bool) -> &mut Self {
self.attr(|builder| builder.with_resizable(is_resizable))
}

pub fn is_active(&mut self, is_active: bool) -> &mut Self {
self.attr(|builder| builder.with_active(is_active))
}

pub fn decorations(&mut self, decorations: bool) -> &mut Self {
self.attr(|builder| builder.with_decorations(decorations))
}

pub fn theme(&mut self, theme: Option<Theme>) -> &mut Self {
self.attr(|builder| builder.with_theme(theme))
}

fn attr(
&mut self,
f: impl FnOnce(winit::window::WindowBuilder) -> winit::window::WindowBuilder,
) -> &mut Self {
self.window = Some(f(self.window.take().unwrap()));
self
}
}

/// Window on the native platform.
pub struct Window {}
pub struct Window {
window: Option<winit::window::WindowBuilder>,
}

impl Default for Window {
fn default() -> Self {
Self {
window: Some(Default::default()),
}
}
}

#[object]
impl Window {
pub fn builder() -> WindowBuilder {
WindowBuilder::default()
}

fn start(&mut self, handle: Handle<Self>) {
Context::current()
.inner
.borrow_mut()
.pending_windows
.push(handle.clone());
.push((self.window.take().unwrap(), handle.clone()));
}

/// Signal for the cursor movement event.
Expand Down

0 comments on commit ee96548

Please sign in to comment.