Skip to content

Commit

Permalink
Create windows with UserEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
matthunz committed Nov 22, 2023
1 parent a012aa4 commit 8def8d2
Show file tree
Hide file tree
Showing 9 changed files with 210 additions and 32 deletions.
101 changes: 101 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ lazy_static = "1.4.0"
slotmap = "1.0.6"
vello = { git = "https://github.com/linebender/vello", version = "0.0.1" }
winit = "0.29.3"
anyhow = "1.0.75"
clap = "4.4.8"

[package.metadata.docs.rs]
features = ["full"]
Expand Down
6 changes: 3 additions & 3 deletions examples/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@ async fn main() {
let ui = UserInterface::new();

// Window A
let tree = ui.insert(LocalTree::builder(Window::default()));
let tree = ui.insert(LocalTree::builder(Window::new(&ui)));

let a = tree.insert(Text::new("Window A"));
tree.root().push_child(a.key);

// Window B
let sub_tree = ui.insert(LocalTree::builder(Window::default()));
let sub_tree = ui.insert(LocalTree::builder(Window::new(&ui)));
tree.insert(sub_tree.tree.clone());

let b = sub_tree.insert(Text::new("Window B"));
sub_tree.root().push_child(b.key);

// Window C
let window_c = sub_tree.insert(Window::default());
let window_c = sub_tree.insert(Window::new(&ui));
sub_tree.root().push_child(window_c.key);

let a = sub_tree.insert(Text::new("Window C"));
Expand Down
11 changes: 10 additions & 1 deletion src/any_element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@ use std::any::Any;

use vello::SceneBuilder;

use crate::Element;
use crate::{
element::{Lifecycle, LifecycleContext},
Element,
};

pub trait AnyElement {
fn as_any(&self) -> &dyn Any;

fn as_any_mut(&mut self) -> &mut dyn Any;

fn lifecycle_any(&mut self, cx: LifecycleContext, lifecycle: Lifecycle);

fn handle_any(&mut self, msg: Box<dyn Any>);

fn render_any(&mut self, scene: SceneBuilder);
Expand All @@ -26,6 +31,10 @@ where
self
}

fn lifecycle_any(&mut self, cx: LifecycleContext, lifecycle: Lifecycle) {
self.lifecycle(cx, lifecycle)
}

fn handle_any(&mut self, msg: Box<dyn Any>) {
self.handle(*msg.downcast().unwrap())
}
Expand Down
26 changes: 16 additions & 10 deletions src/element/mod.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
use slotmap::DefaultKey;
use vello::SceneBuilder;

mod text;
pub use text::{Text, TextMessage};

pub trait Element {
type Message;
mod window;
pub use window::Window;

fn handle(&mut self, msg: Self::Message);
use crate::TreeKey;

fn render(&mut self, scene: SceneBuilder);
pub struct LifecycleContext {
pub tree_key: TreeKey,
pub key: DefaultKey,
}

pub enum Lifecycle {
Build,
}

#[derive(Default)]
pub struct Window {}
pub trait Element {
type Message;

impl Element for Window {
type Message = ();
fn lifecycle(&mut self, _cx: LifecycleContext, _lifecycle: Lifecycle) {}

fn handle(&mut self, _msg: Self::Message) {}
fn handle(&mut self, msg: Self::Message);

fn render(&mut self, _scene: SceneBuilder) {}
fn render(&mut self, scene: SceneBuilder);
}
3 changes: 1 addition & 2 deletions src/element/text.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::borrow::Cow;

use crate::{Element, LocalElementRef};
use std::borrow::Cow;

pub enum TextMessage {
SetContent(Cow<'static, str>),
Expand Down
26 changes: 26 additions & 0 deletions src/element/window.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use crate::{Element, UserInterface};
use vello::SceneBuilder;

use super::LifecycleContext;

pub struct Window {
ui: UserInterface,
}

impl Window {
pub fn new(ui: &UserInterface) -> Self {
Self { ui: ui.clone() }
}
}

impl Element for Window {
type Message = ();

fn lifecycle(&mut self, cx: LifecycleContext, _lifecycle: super::Lifecycle) {
self.ui.insert_window(cx.tree_key, cx.key)
}

fn handle(&mut self, _msg: Self::Message) {}

fn render(&mut self, _scene: SceneBuilder) {}
}
19 changes: 12 additions & 7 deletions src/tree/local.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use super::{TreeBuilder, TreeMessage};
use crate::{element::Window, AnyElement, Element, LocalElementRef, TreeKey, UserInterface};
use crate::{
element::LifecycleContext, AnyElement, Element, LocalElementRef, TreeKey, UserInterface,
};
use slotmap::{DefaultKey, SlotMap, SparseSecondaryMap};
use std::{cell::RefCell, marker::PhantomData, rc::Rc};
use vello::{Scene, SceneBuilder};
Expand Down Expand Up @@ -40,19 +42,22 @@ impl<E> LocalTree<E> {
let element: Rc<RefCell<Box<dyn AnyElement>>> = Rc::new(RefCell::new(Box::new(element)));
let key = self.inner.borrow_mut().elements.insert(element.clone());

// TODO
element.borrow_mut().lifecycle_any(
LifecycleContext {
tree_key: self.inner.borrow().key,
key,
},
crate::element::Lifecycle::Build,
);

LocalElementRef {
element,
tree: self.clone(),
key,
_marker: PhantomData,
}
}

pub fn insert_window(&self) -> LocalElementRef<E, Window> {
let window = self.insert(Window {});
self.ui.insert_window(self.inner.borrow().key, window.key);
window
}
}

impl<E: Element> Element for LocalTree<E> {
Expand Down
Loading

0 comments on commit 8def8d2

Please sign in to comment.