moved stretch layout to native-core

This commit is contained in:
Evan Almloff committed 2022-03-26 20:28:29 -05:00
1 parent 26d92b6e51
commit 6adfa8805c
5 files changed
+35 -36

No files matched your search

+7 -7
View File
@@ -4,7 +4,7 @@ use crossterm::event::{
use dioxus_core::*;
use dioxus_html::{on::*, KeyCode};
use dioxus_native_core::{Tree, TreeNode};
use dioxus_native_core::{layout::StretchLayout, Tree, TreeNode};
use futures::{channel::mpsc::UnboundedReceiver, StreamExt};
use std::{
any::Any,
@@ -16,7 +16,7 @@ use std::{
};
use stretch2::{prelude::Layout, Stretch};
use crate::{style_attributes::StyleModifier, RinkLayout};
use crate::style_attributes::StyleModifier;
// a wrapper around the input state for easier access
// todo: fix loop
@@ -166,7 +166,7 @@ impl InnerInputState {
evts: &mut Vec<EventCore>,
resolved_events: &mut Vec<UserEvent>,
layout: &Stretch,
tree: &mut Tree<RinkLayout, StyleModifier>,
tree: &mut Tree<StretchLayout, StyleModifier>,
) {
let previous_mouse = self
.mouse
@@ -191,7 +191,7 @@ impl InnerInputState {
previous_mouse: Option<(MouseData, Vec<u16>)>,
resolved_events: &mut Vec<UserEvent>,
layout: &Stretch,
tree: &mut Tree<RinkLayout, StyleModifier>,
tree: &mut Tree<StretchLayout, StyleModifier>,
) {
struct Data<'b> {
new_pos: (i32, i32),
@@ -215,8 +215,8 @@ impl InnerInputState {
data: Arc<dyn Any + Send + Sync>,
will_bubble: &mut HashSet<ElementId>,
resolved_events: &mut Vec<UserEvent>,
node: &TreeNode<RinkLayout, StyleModifier>,
tree: &Tree<RinkLayout, StyleModifier>,
node: &TreeNode<StretchLayout, StyleModifier>,
tree: &Tree<StretchLayout, StyleModifier>,
) {
// only trigger event if the event was not triggered already by a child
if will_bubble.insert(node.id) {
@@ -543,7 +543,7 @@ impl RinkInputHandler {
pub fn get_events<'a>(
&self,
layout: &Stretch,
tree: &mut Tree<RinkLayout, StyleModifier>,
tree: &mut Tree<StretchLayout, StyleModifier>,
) -> Vec<UserEvent> {
let mut resolved_events = Vec::new();
-90
View File
@@ -1,90 +0,0 @@
use dioxus_core::*;
use dioxus_native_core::BubbledUpState;
use stretch2::prelude::*;
use dioxus_native_core::layout_attributes::apply_layout_attributes;
/*
The layout system uses the lineheight as one point.
stretch uses fractional points, so we can rasterize if we need too, but not with characters
this means anything thats "1px" is 1 lineheight. Unfortunately, text cannot be smaller or bigger
*/
#[derive(Clone, PartialEq, Default, Debug)]
pub struct RinkLayout {
pub style: Style,
pub node: Option<Node>,
}
impl BubbledUpState for RinkLayout {
type Ctx = Stretch;
// Although we pass in the parent, the state of RinkLayout must only depend on the parent for optimiztion purposes
fn reduce<'a, I>(&mut self, children: I, vnode: &VNode, stretch: &mut Self::Ctx)
where
I: Iterator<Item = &'a Self>,
Self: 'a,
{
match vnode {
VNode::Text(t) => {
let char_len = t.text.chars().count();
let style = Style {
size: Size {
// characters are 1 point tall
height: Dimension::Points(1.0),
// text is as long as it is declared
width: Dimension::Points(char_len as f32),
},
..Default::default()
};
if let Some(n) = self.node {
if self.style != style {
stretch.set_style(n, style).unwrap();
}
} else {
self.node = Some(stretch.new_node(style, &[]).unwrap());
}
self.style = style;
}
VNode::Element(el) => {
// gather up all the styles from the attribute list
let mut style = Style::default();
for &Attribute { name, value, .. } in el.attributes {
apply_layout_attributes(name, value, &mut style);
}
// todo: move
if el.id.get() == Some(ElementId(0)) {
apply_layout_attributes("width", "100%", &mut style);
apply_layout_attributes("height", "100%", &mut style);
}
// Set all direct nodes as our children
let mut child_layout = vec![];
for l in children {
child_layout.push(l.node.unwrap());
}
child_layout.reverse();
if let Some(n) = self.node {
if &stretch.children(n).unwrap() != &child_layout {
stretch.set_children(n, &child_layout).unwrap();
}
if self.style != style {
stretch.set_style(n, style).unwrap();
}
} else {
self.node = Some(stretch.new_node(style, &child_layout).unwrap());
}
self.style = style;
}
_ => (),
}
}
}
+18 -20
View File
@@ -10,7 +10,7 @@ use crossterm::{
use dioxus_core::exports::futures_channel::mpsc::unbounded;
use dioxus_core::*;
use dioxus_html::on::{KeyboardData, MouseData, PointerData, TouchData, WheelData};
use dioxus_native_core::Tree;
use dioxus_native_core::{layout::StretchLayout, Tree};
use futures::{channel::mpsc::UnboundedSender, pin_mut, StreamExt};
use std::collections::HashSet;
use std::{io, time::Duration};
@@ -21,7 +21,6 @@ use tui::{backend::CrosstermBackend, Terminal};
mod config;
mod hooks;
mod layout;
mod render;
mod style;
mod style_attributes;
@@ -29,7 +28,6 @@ mod widget;
pub use config::*;
pub use hooks::*;
pub use layout::*;
pub use render::*;
pub fn launch(app: Component<()>) {
@@ -46,7 +44,7 @@ pub fn launch_cfg(app: Component<()>, cfg: Config) {
cx.provide_root_context(state);
let mut tree: Tree<RinkLayout, StyleModifier> = Tree::new();
let mut tree: Tree<StretchLayout, StyleModifier> = Tree::new();
let mutations = dom.rebuild();
let to_update = tree.apply_mutations(vec![mutations]);
let mut stretch = Stretch::new();
@@ -62,7 +60,7 @@ pub fn render_vdom(
ctx: UnboundedSender<TermEvent>,
handler: RinkInputHandler,
cfg: Config,
mut tree: Tree<RinkLayout, StyleModifier>,
mut tree: Tree<StretchLayout, StyleModifier>,
mut stretch: Stretch,
) -> Result<()> {
// Setup input handling
@@ -148,21 +146,21 @@ pub fn render_vdom(
// resolve events before rendering
// todo: events do not trigger update?
for e in handler.get_events(&stretch, &mut tree) {
let tname = if e.data.is::<PointerData>() {
"PointerData"
} else if e.data.is::<WheelData>() {
"WheelData"
} else if e.data.is::<MouseData>() {
"MouseData"
} else if e.data.is::<KeyboardData>() {
"KeyboardData"
} else if e.data.is::<TouchData>() {
"TouchData"
} else if e.data.is::<(u16, u16)>() {
"(u16, u16)"
} else {
panic!()
};
// let tname = if e.data.is::<PointerData>() {
// "PointerData"
// } else if e.data.is::<WheelData>() {
// "WheelData"
// } else if e.data.is::<MouseData>() {
// "MouseData"
// } else if e.data.is::<KeyboardData>() {
// "KeyboardData"
// } else if e.data.is::<TouchData>() {
// "TouchData"
// } else if e.data.is::<(u16, u16)>() {
// "(u16, u16)"
// } else {
// panic!()
// };
// println!("{tname}: {e:?}");
vdom.handle_message(SchedulerMsg::Event(e));
}
+5 -5
View File
@@ -1,4 +1,4 @@
use dioxus_native_core::{layout_attributes::UnitSystem, Tree, TreeNode};
use dioxus_native_core::{layout::StretchLayout, layout_attributes::UnitSystem, Tree, TreeNode};
use std::io::Stdout;
use stretch2::{
geometry::Point,
@@ -11,7 +11,7 @@ use crate::{
style::{RinkColor, RinkStyle},
style_attributes::{BorderEdge, BorderStyle},
widget::{RinkBuffer, RinkCell, RinkWidget, WidgetWithContext},
Config, RinkLayout, StyleModifier,
Config, StyleModifier,
};
const RADIUS_MULTIPLIER: [f32; 2] = [1.0, 0.5];
@@ -19,8 +19,8 @@ const RADIUS_MULTIPLIER: [f32; 2] = [1.0, 0.5];
pub fn render_vnode<'a>(
frame: &mut tui::Frame<CrosstermBackend<Stdout>>,
layout: &Stretch,
tree: &Tree<RinkLayout, StyleModifier>,
node: &TreeNode<RinkLayout, StyleModifier>,
tree: &Tree<StretchLayout, StyleModifier>,
node: &TreeNode<StretchLayout, StyleModifier>,
cfg: Config,
) {
match &node.node_type {
@@ -80,7 +80,7 @@ pub fn render_vnode<'a>(
}
}
impl RinkWidget for &TreeNode<RinkLayout, StyleModifier> {
impl RinkWidget for &TreeNode<StretchLayout, StyleModifier> {
fn render(self, area: Rect, mut buf: RinkBuffer<'_>) {
use tui::symbols::line::*;