mirror of
https://github.com/ClementTsang/bottom.git
synced 2025-04-08 17:05:59 +02:00
Temp before ctx
This commit is contained in:
parent
7e4139fcd4
commit
190615cd35
@ -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,
|
||||
};
|
||||
|
@ -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`].
|
||||
|
@ -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()
|
||||
|
@ -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();
|
||||
|
@ -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,
|
||||
|
@ -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)]
|
||||
|
@ -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> {
|
||||
|
@ -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;
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user