mirror of
https://github.com/danbulant/mangui
synced 2026-07-06 11:20:34 +00:00
start working on events
This commit is contained in:
parent
a953e771da
commit
19df12a8c8
3 changed files with 73 additions and 8 deletions
70
src/main.rs
70
src/main.rs
|
|
@ -1,3 +1,4 @@
|
||||||
|
use std::collections::HashMap;
|
||||||
use std::num::NonZeroU32;
|
use std::num::NonZeroU32;
|
||||||
use std::ops::Deref;
|
use std::ops::Deref;
|
||||||
use std::sync::{Arc, RwLock, Weak};
|
use std::sync::{Arc, RwLock, Weak};
|
||||||
|
|
@ -32,7 +33,10 @@ mod nodes;
|
||||||
|
|
||||||
type TNode<T> = dyn Node<T>;
|
type TNode<T> = dyn Node<T>;
|
||||||
type SharedTNode<T> = Arc<RwLock<TNode<T>>>;
|
type SharedTNode<T> = Arc<RwLock<TNode<T>>>;
|
||||||
type TaffyMap<T> = PtrWeakKeyHashMap<Weak<RwLock<TNode<T>>>, taffy::node::Node>;
|
type WeakTNode<T> = Weak<RwLock<TNode<T>>>;
|
||||||
|
type TNodePtr<T> = Option<Vec<WeakTNode<T>>>;
|
||||||
|
type NodeLayoutMap<T> = PtrWeakKeyHashMap<Weak<RwLock<TNode<T>>>, taffy::node::Node>;
|
||||||
|
type LayoutNodeMap<T> = HashMap<taffy::node::Node, Weak<RwLock<TNode<T>>>>;
|
||||||
type CurrentRenderer = OpenGl;
|
type CurrentRenderer = OpenGl;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
|
@ -45,7 +49,7 @@ fn main() {
|
||||||
let canvas = Canvas::new(renderer).expect("Cannot create canvas");
|
let canvas = Canvas::new(renderer).expect("Cannot create canvas");
|
||||||
|
|
||||||
let mut taffy = Taffy::new();
|
let mut taffy = Taffy::new();
|
||||||
let mut taffy_map = TaffyMap::new();
|
let mut taffy_map = NodeLayoutMap::new();
|
||||||
|
|
||||||
let mut root = Layout::<CurrentRenderer>::new();
|
let mut root = Layout::<CurrentRenderer>::new();
|
||||||
root.style.layout.display = taffy::style::Display::Flex;
|
root.style.layout.display = taffy::style::Display::Flex;
|
||||||
|
|
@ -64,6 +68,53 @@ fn main() {
|
||||||
color: Color::rgb(255, 0, 0),
|
color: Color::rgb(255, 0, 0),
|
||||||
radius: 10.
|
radius: 10.
|
||||||
})));
|
})));
|
||||||
|
root.children.push(Arc::new(RwLock::new(Layout {
|
||||||
|
style: Style {
|
||||||
|
overflow: nodes::Overflow::Visible,
|
||||||
|
layout: TaffyStyle {
|
||||||
|
min_size: Size {
|
||||||
|
width: Dimension::Points(100.),
|
||||||
|
height: Dimension::Points(100.)
|
||||||
|
},
|
||||||
|
flex_grow: 1.,
|
||||||
|
display: taffy::style::Display::Flex,
|
||||||
|
flex_direction: taffy::style::FlexDirection::Column,
|
||||||
|
..Default::default()
|
||||||
|
}
|
||||||
|
},
|
||||||
|
children: vec![
|
||||||
|
Arc::new(RwLock::new(nodes::primitives::Rectangle {
|
||||||
|
style: Style {
|
||||||
|
overflow: nodes::Overflow::Visible,
|
||||||
|
layout: TaffyStyle {
|
||||||
|
min_size: Size {
|
||||||
|
width: Dimension::Points(50.),
|
||||||
|
height: Dimension::Points(50.)
|
||||||
|
},
|
||||||
|
flex_grow: 1.,
|
||||||
|
..Default::default()
|
||||||
|
}
|
||||||
|
},
|
||||||
|
color: Color::rgb(0, 255, 0),
|
||||||
|
radius: 5.
|
||||||
|
})),
|
||||||
|
Arc::new(RwLock::new(nodes::primitives::Rectangle {
|
||||||
|
style: Style {
|
||||||
|
overflow: nodes::Overflow::Visible,
|
||||||
|
layout: TaffyStyle {
|
||||||
|
min_size: Size {
|
||||||
|
width: Dimension::Points(50.),
|
||||||
|
height: Dimension::Points(50.)
|
||||||
|
},
|
||||||
|
..Default::default()
|
||||||
|
}
|
||||||
|
},
|
||||||
|
color: Color::rgb(0, 255, 255),
|
||||||
|
radius: 5.
|
||||||
|
}))
|
||||||
|
]
|
||||||
|
|
||||||
|
})));
|
||||||
root.children.push(Arc::new(RwLock::new(nodes::primitives::Rectangle {
|
root.children.push(Arc::new(RwLock::new(nodes::primitives::Rectangle {
|
||||||
style: Style {
|
style: Style {
|
||||||
overflow: nodes::Overflow::Visible,
|
overflow: nodes::Overflow::Visible,
|
||||||
|
|
@ -75,7 +126,7 @@ fn main() {
|
||||||
..Default::default()
|
..Default::default()
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
color: Color::rgb(0, 255, 0),
|
color: Color::rgb(0, 0, 255),
|
||||||
radius: 0.
|
radius: 0.
|
||||||
})));
|
})));
|
||||||
let groot: Arc<RwLock<Layout<CurrentRenderer>>> = Arc::new(RwLock::new(root));
|
let groot: Arc<RwLock<Layout<CurrentRenderer>>> = Arc::new(RwLock::new(root));
|
||||||
|
|
@ -92,7 +143,10 @@ fn main() {
|
||||||
let mut context = RenderContext {
|
let mut context = RenderContext {
|
||||||
canvas,
|
canvas,
|
||||||
node_layout: taffy_map,
|
node_layout: taffy_map,
|
||||||
taffy
|
layout_node: LayoutNodeMap::new(),
|
||||||
|
taffy,
|
||||||
|
mouse: None,
|
||||||
|
keyboard_focus: None
|
||||||
};
|
};
|
||||||
|
|
||||||
// let mut width: u32 = 0;
|
// let mut width: u32 = 0;
|
||||||
|
|
@ -135,12 +189,20 @@ fn main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (node, taffy_node) in context.node_layout.iter() {
|
for (node, taffy_node) in context.node_layout.iter() {
|
||||||
|
// context.layout_node.insert(*taffy_node, Arc::downgrade(&node));
|
||||||
let node = node.read().unwrap();
|
let node = node.read().unwrap();
|
||||||
let node_style = node.style();
|
let node_style = node.style();
|
||||||
context.taffy.set_style(*taffy_node, node_style.layout.to_owned()).unwrap();
|
context.taffy.set_style(*taffy_node, node_style.layout.to_owned()).unwrap();
|
||||||
}
|
}
|
||||||
|
// context.layout_node.retain(|_, v| v.upgrade().is_some());
|
||||||
context.taffy.compute_layout(*context.node_layout.get(&root).unwrap(), Size::MAX_CONTENT).unwrap();
|
context.taffy.compute_layout(*context.node_layout.get(&root).unwrap(), Size::MAX_CONTENT).unwrap();
|
||||||
should_recompute = false;
|
should_recompute = false;
|
||||||
|
// Additional optimizations could be done here
|
||||||
|
// - When setting styles, check that the styles aren't the same (taffy doesn't do that and instead always mark it as dirty)
|
||||||
|
// - taffy seems to always recompute (maybe internally checks dirtyness, I didn't look into it that much)
|
||||||
|
// - the weakmap dance (src_nodes, dst_nodes) could be avoided by changing the weakmap used
|
||||||
|
// (weakmap removes keys when you attempt to read them, we could change it so that we could iterate on them and remove them in one go)
|
||||||
|
// could perhaps be a significant boost regarding memory usage (and performance) during large layout changes
|
||||||
// dbg!("recomputed");
|
// dbg!("recomputed");
|
||||||
}
|
}
|
||||||
// dbg!(&root);
|
// dbg!(&root);
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,7 @@ pub struct Layout<T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Renderer> Layout<T> {
|
impl<T: Renderer> Layout<T> {
|
||||||
pub(crate) fn new() -> Layout<T> {
|
pub fn new() -> Layout<T> {
|
||||||
Layout {
|
Layout {
|
||||||
style: Style {
|
style: Style {
|
||||||
layout: TaffyStyle::default(),
|
layout: TaffyStyle::default(),
|
||||||
|
|
|
||||||
|
|
@ -7,14 +7,17 @@ use femtovg::{Canvas, Renderer, Color};
|
||||||
use taffy::layout::Layout;
|
use taffy::layout::Layout;
|
||||||
pub use taffy::style::Style as TaffyStyle;
|
pub use taffy::style::Style as TaffyStyle;
|
||||||
use taffy::Taffy;
|
use taffy::Taffy;
|
||||||
use crate::TaffyMap;
|
use crate::{NodeLayoutMap, LayoutNodeMap, TNodePtr};
|
||||||
|
|
||||||
type SharedTNode<T> = Arc<RwLock<dyn Node<T>>>;
|
type SharedTNode<T> = Arc<RwLock<dyn Node<T>>>;
|
||||||
|
|
||||||
pub struct RenderContext<T: Renderer> {
|
pub struct RenderContext<T: Renderer> {
|
||||||
pub canvas: Canvas<T>,
|
pub canvas: Canvas<T>,
|
||||||
pub node_layout: TaffyMap<T>,
|
pub node_layout: NodeLayoutMap<T>,
|
||||||
pub taffy: Taffy
|
pub layout_node: LayoutNodeMap<T>,
|
||||||
|
pub taffy: Taffy,
|
||||||
|
pub mouse: TNodePtr<T>,
|
||||||
|
pub keyboard_focus: TNodePtr<T>
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Renderer> RenderContext<T> {
|
impl<T: Renderer> RenderContext<T> {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue