implement macro and update tui
This commit is contained in:
1 parent
9e971fbe11
commit
bc2925240e
11 files changed
+233
-800
No files matched your search
+25
-10
@@ -3,16 +3,13 @@ use crate::{node::PreventDefault, TuiDom};
|
||||
use dioxus_native_core::{
|
||||
tree::TreeView,
|
||||
utils::{ElementProduced, PersistantElementIter},
|
||||
NodeId,
|
||||
NodeId, Pass,
|
||||
};
|
||||
use dioxus_native_core_macro::sorted_str_slice;
|
||||
|
||||
use std::{cmp::Ordering, num::NonZeroU16};
|
||||
|
||||
use dioxus_native_core::{
|
||||
node_ref::{AttributeMask, NodeMask, NodeView},
|
||||
state::NodeDepState,
|
||||
};
|
||||
use dioxus_native_core::node_ref::{AttributeMask, NodeMask, NodeView};
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
|
||||
pub(crate) enum FocusLevel {
|
||||
@@ -64,15 +61,33 @@ pub(crate) struct Focus {
|
||||
pub level: FocusLevel,
|
||||
}
|
||||
|
||||
impl NodeDepState for Focus {
|
||||
type DepState = ();
|
||||
impl Pass for Focus {
|
||||
type Ctx = ();
|
||||
const NODE_MASK: NodeMask =
|
||||
NodeMask::new_with_attrs(AttributeMask::Static(FOCUS_ATTRIBUTES)).with_listeners();
|
||||
|
||||
fn reduce(&mut self, node: NodeView<'_>, _sibling: (), _: &Self::Ctx) -> bool {
|
||||
type ParentDependencies = ();
|
||||
type ChildDependencies = ();
|
||||
type NodeDependencies = ();
|
||||
|
||||
fn pass<'a>(
|
||||
&mut self,
|
||||
node_view: NodeView,
|
||||
node: <Self::NodeDependencies as dioxus_native_core::Dependancy>::ElementBorrowed<'a>,
|
||||
parent: Option<
|
||||
<Self::ParentDependencies as dioxus_native_core::Dependancy>::ElementBorrowed<'a>,
|
||||
>,
|
||||
children: Option<
|
||||
impl Iterator<
|
||||
Item = <Self::ChildDependencies as dioxus_native_core::Dependancy>::ElementBorrowed<
|
||||
'a,
|
||||
>,
|
||||
>,
|
||||
>,
|
||||
context: <Self::Ctx as dioxus_native_core::Dependancy>::ElementBorrowed<'a>,
|
||||
) -> bool {
|
||||
let new = Focus {
|
||||
level: if let Some(a) = node
|
||||
level: if let Some(a) = node_view
|
||||
.attributes()
|
||||
.and_then(|mut a| a.find(|a| a.attribute.name == "tabindex"))
|
||||
{
|
||||
@@ -91,7 +106,7 @@ impl NodeDepState for Focus {
|
||||
} else {
|
||||
FocusLevel::Unfocusable
|
||||
}
|
||||
} else if node
|
||||
} else if node_view
|
||||
.listeners()
|
||||
.and_then(|mut listeners| {
|
||||
listeners
|
||||
|
||||
+31
-23
@@ -3,7 +3,7 @@ use std::sync::{Arc, Mutex};
|
||||
use dioxus_native_core::layout_attributes::apply_layout_attributes;
|
||||
use dioxus_native_core::node::OwnedAttributeView;
|
||||
use dioxus_native_core::node_ref::{AttributeMask, NodeMask, NodeView};
|
||||
use dioxus_native_core::state::ChildDepState;
|
||||
use dioxus_native_core::Pass;
|
||||
use dioxus_native_core_macro::sorted_str_slice;
|
||||
use taffy::prelude::*;
|
||||
|
||||
@@ -40,29 +40,35 @@ pub(crate) struct TaffyLayout {
|
||||
pub node: PossiblyUninitalized<Node>,
|
||||
}
|
||||
|
||||
impl ChildDepState for TaffyLayout {
|
||||
type Ctx = Arc<Mutex<Taffy>>;
|
||||
type DepState = (Self,);
|
||||
// use tag to force this to be called when a node is built
|
||||
const NODE_MASK: NodeMask =
|
||||
NodeMask::new_with_attrs(AttributeMask::Static(SORTED_LAYOUT_ATTRS))
|
||||
.with_text()
|
||||
.with_tag();
|
||||
impl Pass for TaffyLayout {
|
||||
type Ctx = (Arc<Mutex<Taffy>>,);
|
||||
type ChildDependencies = (Self,);
|
||||
type ParentDependencies = ();
|
||||
type NodeDependencies = ();
|
||||
|
||||
/// Setup the layout
|
||||
fn reduce<'a>(
|
||||
const NODE_MASK: NodeMask =
|
||||
NodeMask::new_with_attrs(AttributeMask::Static(SORTED_LAYOUT_ATTRS)).with_text();
|
||||
|
||||
fn pass<'a>(
|
||||
&mut self,
|
||||
node: NodeView,
|
||||
children: impl Iterator<Item = (&'a Self,)>,
|
||||
ctx: &Self::Ctx,
|
||||
) -> bool
|
||||
where
|
||||
Self::DepState: 'a,
|
||||
{
|
||||
node_view: NodeView,
|
||||
node: <Self::NodeDependencies as dioxus_native_core::Dependancy>::ElementBorrowed<'a>,
|
||||
parent: Option<
|
||||
<Self::ParentDependencies as dioxus_native_core::Dependancy>::ElementBorrowed<'a>,
|
||||
>,
|
||||
children: Option<
|
||||
impl Iterator<
|
||||
Item = <Self::ChildDependencies as dioxus_native_core::Dependancy>::ElementBorrowed<
|
||||
'a,
|
||||
>,
|
||||
>,
|
||||
>,
|
||||
(taffy,): <Self::Ctx as dioxus_native_core::Dependancy>::ElementBorrowed<'a>,
|
||||
) -> bool {
|
||||
let mut changed = false;
|
||||
let mut taffy = ctx.lock().expect("poisoned taffy");
|
||||
let mut taffy = taffy.lock().expect("poisoned taffy");
|
||||
let mut style = Style::default();
|
||||
if let Some(text) = node.text() {
|
||||
if let Some(text) = node_view.text() {
|
||||
let char_len = text.chars().count();
|
||||
|
||||
style = Style {
|
||||
@@ -85,7 +91,7 @@ impl ChildDepState for TaffyLayout {
|
||||
}
|
||||
} else {
|
||||
// gather up all the styles from the attribute list
|
||||
if let Some(attributes) = node.attributes() {
|
||||
if let Some(attributes) = node_view.attributes() {
|
||||
for OwnedAttributeView {
|
||||
attribute, value, ..
|
||||
} in attributes
|
||||
@@ -101,8 +107,10 @@ impl ChildDepState for TaffyLayout {
|
||||
|
||||
// Set all direct nodes as our children
|
||||
let mut child_layout = vec![];
|
||||
for (l,) in children {
|
||||
child_layout.push(l.node.unwrap());
|
||||
if let Some(children) = children {
|
||||
for (l,) in children {
|
||||
child_layout.push(l.node.unwrap());
|
||||
}
|
||||
}
|
||||
|
||||
fn scale_dimention(d: Dimension) -> Dimension {
|
||||
|
||||
@@ -113,10 +113,10 @@ pub fn launch_cfg_with_props<Props: 'static>(app: Component<Props>, props: Props
|
||||
{
|
||||
let mut rdom = rdom.borrow_mut();
|
||||
let mutations = dom.rebuild();
|
||||
let (to_update, _) = rdom.apply_mutations(mutations);
|
||||
rdom.apply_mutations(mutations);
|
||||
let mut any_map = SendAnyMap::new();
|
||||
any_map.insert(taffy.clone());
|
||||
let _to_rerender = rdom.update_state(to_update, any_map);
|
||||
let _ = rdom.update_state(any_map);
|
||||
}
|
||||
|
||||
render_vdom(
|
||||
@@ -271,11 +271,12 @@ fn render_vdom(
|
||||
let mutations = vdom.render_immediate();
|
||||
handler.prune(&mutations, &rdom);
|
||||
// updates the dom's nodes
|
||||
let (to_update, dirty) = rdom.apply_mutations(mutations);
|
||||
rdom.apply_mutations(mutations);
|
||||
// update the style and layout
|
||||
let mut any_map = SendAnyMap::new();
|
||||
any_map.insert(taffy.clone());
|
||||
to_rerender = rdom.update_state(to_update, any_map);
|
||||
let (new_to_rerender, dirty) = rdom.update_state(any_map);
|
||||
to_rerender = new_to_rerender;
|
||||
for (id, mask) in dirty {
|
||||
if mask.overlaps(&NodeMask::new().with_text()) {
|
||||
to_rerender.insert(id);
|
||||
|
||||
+19
-14
@@ -1,22 +1,20 @@
|
||||
use crate::focus::Focus;
|
||||
use crate::layout::TaffyLayout;
|
||||
use crate::style_attributes::StyleModifier;
|
||||
use dioxus_native_core::{real_dom::RealDom, state::*};
|
||||
use dioxus_native_core_macro::{sorted_str_slice, State};
|
||||
use dioxus_native_core::{real_dom::RealDom, Dependancy, Pass};
|
||||
use dioxus_native_core_macro::{sorted_str_slice, AnyMapLike, State};
|
||||
|
||||
pub(crate) type TuiDom = RealDom<NodeState>;
|
||||
pub(crate) type TuiNode = dioxus_native_core::node::Node<NodeState>;
|
||||
|
||||
#[derive(Debug, Clone, State, Default)]
|
||||
#[derive(Debug, Clone, State, AnyMapLike, Default)]
|
||||
pub(crate) struct NodeState {
|
||||
#[child_dep_state(layout, Mutex<Stretch>)]
|
||||
#[skip_clone]
|
||||
pub layout: TaffyLayout,
|
||||
#[parent_dep_state(style)]
|
||||
pub style: StyleModifier,
|
||||
#[node_dep_state()]
|
||||
pub prevent_default: PreventDefault,
|
||||
#[node_dep_state()]
|
||||
pub focus: Focus,
|
||||
#[skip]
|
||||
pub focused: bool,
|
||||
}
|
||||
|
||||
@@ -45,8 +43,11 @@ impl Default for PreventDefault {
|
||||
}
|
||||
}
|
||||
|
||||
impl NodeDepState for PreventDefault {
|
||||
type DepState = ();
|
||||
impl Pass for PreventDefault {
|
||||
type ParentDependencies = ();
|
||||
type ChildDependencies = ();
|
||||
type NodeDependencies = ();
|
||||
|
||||
type Ctx = ();
|
||||
|
||||
const NODE_MASK: dioxus_native_core::node_ref::NodeMask =
|
||||
@@ -57,13 +58,17 @@ impl NodeDepState for PreventDefault {
|
||||
)
|
||||
.with_listeners();
|
||||
|
||||
fn reduce(
|
||||
fn pass<'a>(
|
||||
&mut self,
|
||||
node: dioxus_native_core::node_ref::NodeView,
|
||||
_sibling: (),
|
||||
_ctx: &Self::Ctx,
|
||||
node_view: dioxus_native_core::node_ref::NodeView,
|
||||
node: <Self::NodeDependencies as Dependancy>::ElementBorrowed<'a>,
|
||||
parent: Option<<Self::ParentDependencies as Dependancy>::ElementBorrowed<'a>>,
|
||||
children: Option<
|
||||
impl Iterator<Item = <Self::ChildDependencies as Dependancy>::ElementBorrowed<'a>>,
|
||||
>,
|
||||
context: <Self::Ctx as Dependancy>::ElementBorrowed<'a>,
|
||||
) -> bool {
|
||||
let new = match node.attributes().and_then(|mut attrs| {
|
||||
let new = match node_view.attributes().and_then(|mut attrs| {
|
||||
attrs
|
||||
.find(|a| a.attribute.name == "dioxus-prevent-default")
|
||||
.and_then(|a| a.value.as_text())
|
||||
|
||||
@@ -45,7 +45,7 @@ pub(crate) fn render_vnode(
|
||||
let height = layout_to_screen_space(fy + height).round() as u16 - y;
|
||||
|
||||
match &node.node_data.node_type {
|
||||
NodeType::Text { text } => {
|
||||
NodeType::Text(text) => {
|
||||
#[derive(Default)]
|
||||
struct Label<'a> {
|
||||
text: &'a str,
|
||||
|
||||
@@ -33,7 +33,7 @@ use dioxus_native_core::{
|
||||
layout_attributes::parse_value,
|
||||
node::OwnedAttributeView,
|
||||
node_ref::{AttributeMask, NodeMask, NodeView},
|
||||
state::ParentDepState,
|
||||
Pass,
|
||||
};
|
||||
use dioxus_native_core_macro::sorted_str_slice;
|
||||
use taffy::prelude::*;
|
||||
@@ -46,22 +46,40 @@ pub struct StyleModifier {
|
||||
pub modifier: TuiModifier,
|
||||
}
|
||||
|
||||
impl ParentDepState for StyleModifier {
|
||||
impl Pass for StyleModifier {
|
||||
type Ctx = ();
|
||||
type DepState = (Self,);
|
||||
type ParentDependencies = (Self,);
|
||||
type ChildDependencies = ();
|
||||
type NodeDependencies = ();
|
||||
|
||||
// todo: seperate each attribute into it's own class
|
||||
const NODE_MASK: NodeMask =
|
||||
NodeMask::new_with_attrs(AttributeMask::Static(SORTED_STYLE_ATTRS)).with_element();
|
||||
|
||||
fn reduce(&mut self, node: NodeView, parent: Option<(&Self,)>, _: &Self::Ctx) -> bool {
|
||||
fn pass<'a>(
|
||||
&mut self,
|
||||
node_view: NodeView,
|
||||
node: <Self::NodeDependencies as dioxus_native_core::Dependancy>::ElementBorrowed<'a>,
|
||||
parent: Option<
|
||||
<Self::ParentDependencies as dioxus_native_core::Dependancy>::ElementBorrowed<'a>,
|
||||
>,
|
||||
children: Option<
|
||||
impl Iterator<
|
||||
Item = <Self::ChildDependencies as dioxus_native_core::Dependancy>::ElementBorrowed<
|
||||
'a,
|
||||
>,
|
||||
>,
|
||||
>,
|
||||
context: <Self::Ctx as dioxus_native_core::Dependancy>::ElementBorrowed<'a>,
|
||||
) -> bool {
|
||||
let mut new = StyleModifier::default();
|
||||
if parent.is_some() {
|
||||
new.core.fg = None;
|
||||
}
|
||||
|
||||
// handle text modifier elements
|
||||
if node.namespace().is_none() {
|
||||
if let Some(tag) = node.tag() {
|
||||
if node_view.namespace().is_none() {
|
||||
if let Some(tag) = node_view.tag() {
|
||||
match tag {
|
||||
"b" => apply_style_attributes("font-weight", "bold", &mut new),
|
||||
"strong" => apply_style_attributes("font-weight", "bold", &mut new),
|
||||
@@ -79,7 +97,7 @@ impl ParentDepState for StyleModifier {
|
||||
}
|
||||
|
||||
// gather up all the styles from the attribute list
|
||||
if let Some(attrs) = node.attributes() {
|
||||
if let Some(attrs) = node_view.attributes() {
|
||||
for OwnedAttributeView {
|
||||
attribute, value, ..
|
||||
} in attrs
|
||||
|
||||
Reference in new issue
Block a user