From 6adfa8805c308bee1addb776225393f3413827e1 Mon Sep 17 00:00:00 2001 From: Evan Almloff Date: Wed, 23 Mar 2022 14:33:06 -0500 Subject: [PATCH] moved stretch layout to native-core --- packages/{tui => native-core}/src/layout.rs | 8 ++--- packages/native-core/src/lib.rs | 1 + packages/tui/src/hooks.rs | 14 ++++---- packages/tui/src/lib.rs | 38 ++++++++++----------- packages/tui/src/render.rs | 10 +++--- 5 files changed, 35 insertions(+), 36 deletions(-) rename packages/{tui => native-core}/src/layout.rs (94%) diff --git a/packages/tui/src/layout.rs b/packages/native-core/src/layout.rs similarity index 94% rename from packages/tui/src/layout.rs rename to packages/native-core/src/layout.rs index ced248dc9..9e6dd90ea 100644 --- a/packages/tui/src/layout.rs +++ b/packages/native-core/src/layout.rs @@ -1,8 +1,8 @@ +use crate::BubbledUpState; use dioxus_core::*; -use dioxus_native_core::BubbledUpState; use stretch2::prelude::*; -use dioxus_native_core::layout_attributes::apply_layout_attributes; +use crate::layout_attributes::apply_layout_attributes; /* The layout system uses the lineheight as one point. @@ -11,12 +11,12 @@ stretch uses fractional points, so we can rasterize if we need too, but not with this means anything thats "1px" is 1 lineheight. Unfortunately, text cannot be smaller or bigger */ #[derive(Clone, PartialEq, Default, Debug)] -pub struct RinkLayout { +pub struct StretchLayout { pub style: Style, pub node: Option, } -impl BubbledUpState for RinkLayout { +impl BubbledUpState for StretchLayout { type Ctx = Stretch; // Although we pass in the parent, the state of RinkLayout must only depend on the parent for optimiztion purposes diff --git a/packages/native-core/src/lib.rs b/packages/native-core/src/lib.rs index 88dde28fc..67e888500 100644 --- a/packages/native-core/src/lib.rs +++ b/packages/native-core/src/lib.rs @@ -1,6 +1,7 @@ use std::collections::{HashMap, HashSet, VecDeque}; use dioxus_core::{ElementId, Mutations, VNode, VirtualDom}; +pub mod layout; pub mod layout_attributes; /// A tree that can sync with dioxus mutations backed by a hashmap. diff --git a/packages/tui/src/hooks.rs b/packages/tui/src/hooks.rs index d65123d0c..2184dcd06 100644 --- a/packages/tui/src/hooks.rs +++ b/packages/tui/src/hooks.rs @@ -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, resolved_events: &mut Vec, layout: &Stretch, - tree: &mut Tree, + tree: &mut Tree, ) { let previous_mouse = self .mouse @@ -191,7 +191,7 @@ impl InnerInputState { previous_mouse: Option<(MouseData, Vec)>, resolved_events: &mut Vec, layout: &Stretch, - tree: &mut Tree, + tree: &mut Tree, ) { struct Data<'b> { new_pos: (i32, i32), @@ -215,8 +215,8 @@ impl InnerInputState { data: Arc, will_bubble: &mut HashSet, resolved_events: &mut Vec, - node: &TreeNode, - tree: &Tree, + node: &TreeNode, + tree: &Tree, ) { // 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, + tree: &mut Tree, ) -> Vec { let mut resolved_events = Vec::new(); diff --git a/packages/tui/src/lib.rs b/packages/tui/src/lib.rs index 3893ab423..f3828aac1 100644 --- a/packages/tui/src/lib.rs +++ b/packages/tui/src/lib.rs @@ -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 = Tree::new(); + let mut tree: Tree = 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, handler: RinkInputHandler, cfg: Config, - mut tree: Tree, + mut tree: Tree, 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" - } else if e.data.is::() { - "WheelData" - } else if e.data.is::() { - "MouseData" - } else if e.data.is::() { - "KeyboardData" - } else if e.data.is::() { - "TouchData" - } else if e.data.is::<(u16, u16)>() { - "(u16, u16)" - } else { - panic!() - }; + // let tname = if e.data.is::() { + // "PointerData" + // } else if e.data.is::() { + // "WheelData" + // } else if e.data.is::() { + // "MouseData" + // } else if e.data.is::() { + // "KeyboardData" + // } else if e.data.is::() { + // "TouchData" + // } else if e.data.is::<(u16, u16)>() { + // "(u16, u16)" + // } else { + // panic!() + // }; // println!("{tname}: {e:?}"); vdom.handle_message(SchedulerMsg::Event(e)); } diff --git a/packages/tui/src/render.rs b/packages/tui/src/render.rs index 66ee1fd91..7be17ff82 100644 --- a/packages/tui/src/render.rs +++ b/packages/tui/src/render.rs @@ -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>, layout: &Stretch, - tree: &Tree, - node: &TreeNode, + tree: &Tree, + node: &TreeNode, cfg: Config, ) { match &node.node_type { @@ -80,7 +80,7 @@ pub fn render_vnode<'a>( } } -impl RinkWidget for &TreeNode { +impl RinkWidget for &TreeNode { fn render(self, area: Rect, mut buf: RinkBuffer<'_>) { use tui::symbols::line::*;