use taffy instead of stretch2

This commit is contained in:
Evan Almloff committed 2022-06-10 17:23:30 -05:00
1 parent a5b9200e47
commit dcfe60c164
8 files changed
+49 -62

No files matched your search

+17 -17
View File
@@ -13,12 +13,12 @@ use futures::{
channel::mpsc::{UnboundedReceiver, UnboundedSender},
pin_mut, StreamExt,
};
use layout::StretchLayout;
use layout::TaffyLayout;
use std::cell::RefCell;
use std::rc::Rc;
use std::{io, time::Duration};
use stretch2::{geometry::Point, prelude::Size, Stretch};
use style_attributes::StyleModifier;
use taffy::{geometry::Point, prelude::Size, Taffy};
use tui::{backend::CrosstermBackend, layout::Rect, Terminal};
mod config;
@@ -37,8 +37,8 @@ type Node = dioxus_native_core::real_dom::Node<NodeState>;
#[derive(Debug, Clone, State, Default)]
struct NodeState {
#[child_dep_state(layout, RefCell<Stretch>)]
layout: StretchLayout,
#[child_dep_state(layout, RefCell<Taffy>)]
layout: TaffyLayout,
// depends on attributes, the C component of it's parent and a u8 context
#[parent_dep_state(style)]
style: StyleModifier,
@@ -88,9 +88,9 @@ pub fn launch_cfg(app: Component<()>, cfg: Config) {
let mut rdom: Dom = RealDom::new();
let mutations = dom.rebuild();
let to_update = rdom.apply_mutations(vec![mutations]);
let stretch = Rc::new(RefCell::new(Stretch::new()));
let taffy = Rc::new(RefCell::new(Taffy::new()));
let mut any_map = AnyMap::new();
any_map.insert(stretch.clone());
any_map.insert(taffy.clone());
let _to_rerender = rdom.update_state(&dom, to_update, any_map).unwrap();
render_vdom(
@@ -99,7 +99,7 @@ pub fn launch_cfg(app: Component<()>, cfg: Config) {
handler,
cfg,
rdom,
stretch,
taffy,
register_event,
)
.unwrap();
@@ -111,7 +111,7 @@ fn render_vdom(
handler: RinkInputHandler,
cfg: Config,
mut rdom: Dom,
stretch: Rc<RefCell<Stretch>>,
taffy: Rc<RefCell<Taffy>>,
mut register_event: impl FnMut(crossterm::event::Event),
) -> Result<()> {
tokio::runtime::Builder::new_current_thread()
@@ -146,17 +146,17 @@ fn render_vdom(
if !to_rerender.is_empty() || resized {
resized = false;
fn resize(dims: Rect, stretch: &mut Stretch, rdom: &Dom) {
fn resize(dims: Rect, taffy: &mut Taffy, rdom: &Dom) {
let width = dims.width;
let height = dims.height;
let root_node = rdom[0].state.layout.node.unwrap();
stretch
taffy
.compute_layout(
root_node,
Size {
width: stretch2::prelude::Number::Defined((width - 1) as f32),
height: stretch2::prelude::Number::Defined((height - 1) as f32),
width: taffy::prelude::Number::Defined((width - 1) as f32),
height: taffy::prelude::Number::Defined((height - 1) as f32),
},
)
.unwrap();
@@ -164,11 +164,11 @@ fn render_vdom(
if let Some(terminal) = &mut terminal {
terminal.draw(|frame| {
// size is guaranteed to not change when rendering
resize(frame.size(), &mut stretch.borrow_mut(), &rdom);
resize(frame.size(), &mut taffy.borrow_mut(), &rdom);
let root = &rdom[0];
render::render_vnode(
frame,
&stretch.borrow(),
&taffy.borrow(),
&rdom,
root,
cfg,
@@ -183,7 +183,7 @@ fn render_vdom(
width: 300,
height: 300,
},
&mut stretch.borrow_mut(),
&mut taffy.borrow_mut(),
&rdom,
);
}
@@ -223,7 +223,7 @@ fn render_vdom(
}
{
let evts = handler.get_events(&stretch.borrow(), &mut rdom);
let evts = handler.get_events(&taffy.borrow(), &mut rdom);
for e in evts {
vdom.handle_message(SchedulerMsg::Event(e));
}
@@ -232,7 +232,7 @@ fn render_vdom(
let to_update = rdom.apply_mutations(mutations);
// update the style and layout
let mut any_map = AnyMap::new();
any_map.insert(stretch.clone());
any_map.insert(taffy.clone());
to_rerender = rdom.update_state(vdom, to_update, any_map).unwrap();
}
}