This commit is contained in:
ClementTsang 2021-12-14 23:31:36 -05:00
parent c094efa74c
commit c002a3fa3a
8 changed files with 94 additions and 5 deletions

View File

@ -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};

View File

@ -92,7 +92,9 @@ impl<'a, Message> TmpComponent<Message> for Flex<'a, Message> {
.iter_mut()
.zip(context.children())
.for_each(|(child, child_node)| {
if child_node.should_draw() {
child.draw(child_node, frame);
}
});
}
@ -123,11 +125,17 @@ 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() {
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 {
total_flex += child.flex;

View File

@ -0,0 +1,3 @@
/// A trait that the properties of a [`Component`](super::Component)
/// should implement.
pub trait Properties: PartialEq {}

View File

@ -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,

26
src/tuice/key.rs Normal file
View File

@ -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,
}
}
}

View File

@ -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::*;

1
src/tuice/screen.rs Normal file
View File

@ -0,0 +1 @@
//! Screens are a system to preserve states while moving between two "screens".

25
src/tuice/state.rs Normal file
View File

@ -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)
}
}