diff options
author | ClementTsang <cjhtsang@uwaterloo.ca> | 2021-12-14 23:31:36 -0500 |
---|---|---|
committer | ClementTsang <cjhtsang@uwaterloo.ca> | 2021-12-14 23:31:36 -0500 |
commit | c002a3fa3a3276464502e0b1ea2bd29a1b8e315b (patch) | |
tree | 7cbfea422cc9b9102019365ceb7eb462547287f8 | |
parent | c094efa74c6154440483f18cb056a9f2a7da2447 (diff) |
temp
-rw-r--r-- | src/tuice/component.rs | 3 | ||||
-rw-r--r-- | src/tuice/component/base/flex.rs | 18 | ||||
-rw-r--r-- | src/tuice/component/properties.rs | 3 | ||||
-rw-r--r-- | src/tuice/context.rs | 4 | ||||
-rw-r--r-- | src/tuice/key.rs | 26 | ||||
-rw-r--r-- | src/tuice/mod.rs | 19 | ||||
-rw-r--r-- | src/tuice/screen.rs | 1 | ||||
-rw-r--r-- | src/tuice/state.rs | 25 |
8 files changed, 94 insertions, 5 deletions
diff --git a/src/tuice/component.rs b/src/tuice/component.rs index 42cc7d53..753f36ba 100644 --- a/src/tuice/component.rs +++ b/src/tuice/component.rs @@ -4,6 +4,9 @@ pub use base::*; pub mod widget; pub use widget::*; +pub mod properties; +pub use properties::*; + use enum_dispatch::enum_dispatch; use tui::{layout::Rect, Frame}; diff --git a/src/tuice/component/base/flex.rs b/src/tuice/component/base/flex.rs index 439705ba..1aefca0e 100644 --- a/src/tuice/component/base/flex.rs +++ b/src/tuice/component/base/flex.rs @@ -92,7 +92,9 @@ impl<'a, Message> TmpComponent<Message> for Flex<'a, Message> { .iter_mut() .zip(context.children()) .for_each(|(child, child_node)| { - child.draw(child_node, frame); + if child_node.should_draw() { + child.draw(child_node, frame); + } }); } @@ -123,10 +125,16 @@ impl<'a, Message> TmpComponent<Message> for Flex<'a, Message> { .zip(children.iter_mut()) .enumerate() .for_each(|(index, (child, child_node))| { - if child.flex == 0 && remaining_bounds.has_space() { - let size = child.child_layout(remaining_bounds, child_node); - current_size += size; - remaining_bounds.shrink_size(size); + if child.flex == 0 { + let size = if remaining_bounds.has_space() { + let size = child.child_layout(remaining_bounds, child_node); + current_size += size; + remaining_bounds.shrink_size(size); + + size + } else { + Size::default() + }; sizes.push(size); } else { diff --git a/src/tuice/component/properties.rs b/src/tuice/component/properties.rs new file mode 100644 index 00000000..d44ab368 --- /dev/null +++ b/src/tuice/component/properties.rs @@ -0,0 +1,3 @@ +/// A trait that the properties of a [`Component`](super::Component) +/// should implement. +pub trait Properties: PartialEq {} diff --git a/src/tuice/context.rs b/src/tuice/context.rs index 0de909cb..05dad611 100644 --- a/src/tuice/context.rs +++ b/src/tuice/context.rs @@ -24,6 +24,10 @@ impl<'a> DrawContext<'_> { rect } + pub(crate) fn should_draw(&self) -> bool { + self.current_node.rect.area() != 0 + } + pub(crate) fn children(&self) -> impl Iterator<Item = DrawContext<'_>> { let new_offset = ( self.current_offset.0 + self.current_node.rect.x, diff --git a/src/tuice/key.rs b/src/tuice/key.rs new file mode 100644 index 00000000..11e00ee5 --- /dev/null +++ b/src/tuice/key.rs @@ -0,0 +1,26 @@ +//! Code here is based on crochet's implementation - see +//! [here](https://github.com/raphlinus/crochet/blob/master/src/key.rs) for more details! + +use std::hash::Hash; +use std::panic::Location; + +/// A newtype around [`Location`]. +#[derive(Clone, Copy, Hash, Debug, PartialEq, Eq, PartialOrd, Ord)] +pub struct Caller(&'static Location<'static>); + +/// A unique key built around using the [`Location`] given by +/// `#[track_caller]` and a sequence index. +#[derive(Clone, Copy, Hash, Debug, PartialEq, Eq, PartialOrd, Ord)] +pub struct Key { + pub(crate) caller: Caller, + pub(crate) index: usize, +} + +impl Key { + pub fn new(caller: impl Into<Caller>, index: usize) -> Self { + Self { + caller: caller.into(), + index, + } + } +} diff --git a/src/tuice/mod.rs b/src/tuice/mod.rs index aeb40e1d..ff5fe5b1 100644 --- a/src/tuice/mod.rs +++ b/src/tuice/mod.rs @@ -1,3 +1,13 @@ +//! tuice is a wrapper around tui-rs that expands upon state management and +//! event handling. +//! +//! tuice is inspired by a **ton** of other libraries and frameworks, like: +//! - Iced +//! - [crochet](https://github.com/raphlinus/crochet) +//! - Flutter +//! - React +//! - Yew + mod tui_rs; pub mod component; @@ -20,3 +30,12 @@ pub use element::*; pub mod context; pub use context::*; + +pub mod screen; +pub use screen::*; + +pub mod key; +pub use key::*; + +pub mod state; +pub use state::*; diff --git a/src/tuice/screen.rs b/src/tuice/screen.rs new file mode 100644 index 00000000..31289c1d --- /dev/null +++ b/src/tuice/screen.rs @@ -0,0 +1 @@ +//! Screens are a system to preserve states while moving between two "screens". diff --git a/src/tuice/state.rs b/src/tuice/state.rs new file mode 100644 index 00000000..144c129f --- /dev/null +++ b/src/tuice/state.rs @@ -0,0 +1,25 @@ +//! State code is heavily inspired by crochet's work - see +//! [here](https://github.com/raphlinus/crochet/blob/master/src/state.rs) for the original. + +use std::any::Any; + +/// A trait that any sort of [`Component`](crate::tuice::Component) state should implement. +pub trait State { + fn as_any(&self) -> &dyn Any; + + fn are_equal(&self, other: &dyn State) -> bool; +} + +impl<S: PartialEq + 'static> State for S { + fn as_any(&self) -> &dyn Any { + self + } + + fn are_equal(&self, other: &dyn State) -> bool { + other + .as_any() + .downcast_ref() + .map(|other| self == other) + .unwrap_or(false) + } +} |