Temp before ctx

This commit is contained in:
ClementTsang 2021-12-20 19:32:12 -05:00
parent 7e4139fcd4
commit 190615cd35
8 changed files with 54 additions and 11 deletions

View File

@ -29,7 +29,7 @@ use frozen_state::FrozenState;
use crate::{
canvas::Painter,
constants,
tuine::{Application, Element, Flex},
tuine::{Application, ComponentContext, Element, Flex},
units::data_units::DataUnit,
Pid,
};

View File

@ -4,7 +4,7 @@ use tui::Terminal;
use super::{
runtime::{self, RuntimeEvent},
Element, Event,
ComponentContext, Element, Event,
};
/// An alias to the [`tui::backend::CrosstermBackend`] writing to [`std::io::Stdout`].

View File

@ -1,7 +1,7 @@
pub mod table_column;
mod table_scroll_state;
use std::{borrow::Cow, cmp::min};
use std::{borrow::Cow, cmp::min, panic::Location};
use tui::{
backend::Backend,
@ -14,7 +14,7 @@ use unicode_segmentation::UnicodeSegmentation;
use crate::{
constants::TABLE_GAP_HEIGHT_LIMIT,
tuine::{DrawContext, Event, Status, TmpComponent},
tuine::{ComponentContext, DrawContext, Event, Status, TmpComponent},
};
pub use self::table_column::{TextColumn, TextColumnConstraint};
@ -27,8 +27,6 @@ pub struct StyleSheet {
table_header: Style,
}
pub enum TextTableMsg {}
/// A sortable, scrollable table for text data.
pub struct TextTable<'a, Message> {
state: TextTableState,
@ -45,9 +43,12 @@ pub struct TextTable<'a, Message> {
}
impl<'a, Message> TextTable<'a, Message> {
#[track_caller]
pub fn new<S: Into<Cow<'static, str>>>(columns: Vec<S>) -> Self {
let state = TextTableState::default();
Self {
state: TextTableState::default(),
state,
column_widths: vec![0; columns.len()],
columns: columns
.into_iter()

View File

@ -16,7 +16,7 @@ impl Default for ScrollDirection {
}
/// We save the previous window index for future reference, but we must invalidate if the area changes.
#[derive(Default)]
#[derive(PartialEq, Default)]
struct WindowIndex {
index: usize,
cached_area: Rect,
@ -39,6 +39,16 @@ pub struct ScrollState {
tui_state: TableState,
}
impl PartialEq for ScrollState {
fn eq(&self, other: &Self) -> bool {
self.current_index == other.current_index
&& self.window_index == other.window_index
&& self.scroll_direction == other.scroll_direction
&& self.num_items == other.num_items
&& (self.tui_state.selected() == other.tui_state.selected())
}
}
impl Default for ScrollState {
fn default() -> Self {
let mut tui_state = TableState::default();

View File

@ -1,6 +1,32 @@
use std::panic::Location;
use rustc_hash::FxHashMap;
use tui::layout::Rect;
use super::LayoutNode;
use super::{Key, LayoutNode, State};
#[derive(Default)]
pub struct ComponentContext {
key_counter: usize,
state_map: FxHashMap<Key, Box<dyn State>>,
stale_map: FxHashMap<Key, bool>,
}
impl ComponentContext {
pub fn access_or_new<S: State + Default + 'static>(
&mut self, location: &'static Location<'static>,
) -> &mut Box<dyn State> {
let key = Key::new(location, self.key_counter);
self.key_counter += 1;
*(self.stale_map.entry(key).or_insert(true)) = true;
self.state_map
.entry(key)
.or_insert_with(|| Box::new(S::default()))
}
pub fn cycle(&mut self) {}
}
pub struct DrawContext<'a> {
current_node: &'a LayoutNode,

View File

@ -8,6 +8,12 @@ use std::panic::Location;
#[derive(Clone, Copy, Hash, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct Caller(&'static Location<'static>);
impl From<&'static Location<'static>> for Caller {
fn from(location: &'static Location<'static>) -> Self {
Caller(location)
}
}
/// 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)]

View File

@ -4,7 +4,7 @@ use tui::{backend::Backend, layout::Rect, Terminal};
use crate::tuine::Status;
use super::{build_layout_tree, Application, Element, Event, TmpComponent};
use super::{build_layout_tree, Application, ComponentContext, Element, Event, TmpComponent};
#[derive(Clone, Copy, Debug)]
pub enum RuntimeEvent<Message> {

View File

@ -1,5 +1,5 @@
//! State code is heavily inspired by crochet's work - see
//! [here](https://github.com/raphlinus/crochet/blob/master/src/state.rs) for the original.
//! [here](https://github.com/raphlinus/crochet/blob/master/src/state.rs) for the original work.
use std::any::Any;