Rename view_context to build_context

This commit is contained in:
ClementTsang 2022-01-02 22:47:50 -05:00
parent 40a984e137
commit 11a4d24d52
23 changed files with 42 additions and 48 deletions

View File

@ -30,7 +30,7 @@ use crate::{
canvas::Painter, canvas::Painter,
constants, constants,
data_conversion::ConvertedData, data_conversion::ConvertedData,
tuine::{Application, Element, Status, ViewContext}, tuine::{Application, Element, Status, BuildContext},
units::data_units::DataUnit, units::data_units::DataUnit,
Pid, Pid,
}; };
@ -251,7 +251,7 @@ impl Application for AppState {
self.terminator.load(SeqCst) self.terminator.load(SeqCst)
} }
fn view<'b>(&mut self, ctx: &mut ViewContext<'_>) -> Element<Self::Message> { fn view<'b>(&mut self, ctx: &mut BuildContext<'_>) -> Element<Self::Message> {
match self.current_screen { match self.current_screen {
CurrentScreen::Main => { CurrentScreen::Main => {
// The main screen. // The main screen.

View File

@ -150,7 +150,7 @@ pub struct WidgetLayoutRoot {
impl WidgetLayoutRoot { impl WidgetLayoutRoot {
pub fn build<Message>( pub fn build<Message>(
&self, ctx: &mut ViewContext<'_>, app_state: &AppState, data: &mut ConvertedData<'_>, &self, ctx: &mut BuildContext<'_>, app_state: &AppState, data: &mut ConvertedData<'_>,
) -> Element<Message> { ) -> Element<Message> {
Flex::column_with_children( Flex::column_with_children(
self.children self.children
@ -186,7 +186,7 @@ pub enum WidgetLayoutNode {
impl WidgetLayoutNode { impl WidgetLayoutNode {
fn new_row<Message>( fn new_row<Message>(
ctx: &mut ViewContext<'_>, app_state: &AppState, data: &mut ConvertedData<'_>, ctx: &mut BuildContext<'_>, app_state: &AppState, data: &mut ConvertedData<'_>,
children: &[WidgetLayoutNode], parent_ratio: u16, children: &[WidgetLayoutNode], parent_ratio: u16,
) -> FlexElement<Message> { ) -> FlexElement<Message> {
FlexElement::with_flex( FlexElement::with_flex(
@ -201,7 +201,7 @@ impl WidgetLayoutNode {
} }
fn new_col<Message>( fn new_col<Message>(
ctx: &mut ViewContext<'_>, app_state: &AppState, data: &mut ConvertedData<'_>, ctx: &mut BuildContext<'_>, app_state: &AppState, data: &mut ConvertedData<'_>,
children: &[WidgetLayoutNode], parent_ratio: u16, children: &[WidgetLayoutNode], parent_ratio: u16,
) -> FlexElement<Message> { ) -> FlexElement<Message> {
FlexElement::with_flex( FlexElement::with_flex(
@ -216,7 +216,7 @@ impl WidgetLayoutNode {
} }
fn new_carousel<Message>( fn new_carousel<Message>(
ctx: &mut ViewContext<'_>, app_state: &AppState, data: &mut ConvertedData<'_>, ctx: &mut BuildContext<'_>, app_state: &AppState, data: &mut ConvertedData<'_>,
children: &[BottomWidgetType], selected: bool, children: &[BottomWidgetType], selected: bool,
) -> FlexElement<Message> { ) -> FlexElement<Message> {
// FIXME: Carousel! // FIXME: Carousel!
@ -231,7 +231,7 @@ impl WidgetLayoutNode {
} }
fn make_element<Message>( fn make_element<Message>(
ctx: &mut ViewContext<'_>, app_state: &AppState, data: &mut ConvertedData<'_>, ctx: &mut BuildContext<'_>, app_state: &AppState, data: &mut ConvertedData<'_>,
widget_type: &BottomWidgetType, selected: bool, widget_type: &BottomWidgetType, selected: bool,
) -> Element<Message> { ) -> Element<Message> {
let painter = &app_state.painter; let painter = &app_state.painter;
@ -270,7 +270,7 @@ impl WidgetLayoutNode {
} }
pub fn build<Message>( pub fn build<Message>(
&self, ctx: &mut ViewContext<'_>, app_state: &AppState, data: &mut ConvertedData<'_>, &self, ctx: &mut BuildContext<'_>, app_state: &AppState, data: &mut ConvertedData<'_>,
) -> FlexElement<Message> { ) -> FlexElement<Message> {
match self { match self {
WidgetLayoutNode::Row { WidgetLayoutNode::Row {

View File

@ -4,7 +4,7 @@ use tui::Terminal;
use super::{ use super::{
runtime::{self, RuntimeEvent}, runtime::{self, RuntimeEvent},
Element, Event, Status, ViewContext, Element, Event, Status, BuildContext,
}; };
/// An alias to the [`tui::backend::CrosstermBackend`] writing to [`std::io::Stdout`]. /// An alias to the [`tui::backend::CrosstermBackend`] writing to [`std::io::Stdout`].
@ -22,7 +22,7 @@ pub trait Application: Sized {
fn is_terminated(&self) -> bool; fn is_terminated(&self) -> bool;
/// Creates the user interface. /// Creates the user interface.
fn view<'b>(&mut self, ctx: &mut ViewContext<'_>) -> Element<Self::Message>; fn view<'b>(&mut self, ctx: &mut BuildContext<'_>) -> Element<Self::Message>;
/// To run upon stopping the application. /// To run upon stopping the application.
fn destructor(&mut self) {} fn destructor(&mut self) {}

View File

@ -24,3 +24,6 @@ pub use padding::*;
pub mod time_graph; pub mod time_graph;
pub use time_graph::TimeGraph; pub use time_graph::TimeGraph;
pub mod focus;
pub use focus::*;

View File

@ -145,7 +145,7 @@ where
type ComponentState = ShortcutState; type ComponentState = ShortcutState;
fn build(ctx: &mut crate::tuine::ViewContext<'_>, props: Self::Properties) -> Self { fn build(ctx: &mut crate::tuine::BuildContext<'_>, props: Self::Properties) -> Self {
let (key, state) = let (key, state) =
ctx.register_and_mut_state::<_, Self::ComponentState>(Location::caller()); ctx.register_and_mut_state::<_, Self::ComponentState>(Location::caller());
let mut forest: FxHashMap<Vec<Event>, bool> = FxHashMap::default(); let mut forest: FxHashMap<Vec<Event>, bool> = FxHashMap::default();

View File

@ -211,7 +211,7 @@ impl<Message> StatefulComponent<Message> for TextTable<Message> {
type ComponentState = TextTableState; type ComponentState = TextTableState;
fn build(ctx: &mut crate::tuine::ViewContext<'_>, mut props: Self::Properties) -> Self { fn build(ctx: &mut crate::tuine::BuildContext<'_>, mut props: Self::Properties) -> Self {
let sort = props.sort; let sort = props.sort;
let (key, state) = ctx.register_and_mut_state_with_default::<_, Self::ComponentState, _>( let (key, state) = ctx.register_and_mut_state_with_default::<_, Self::ComponentState, _>(
Location::caller(), Location::caller(),
@ -372,15 +372,15 @@ impl<Message> TmpComponent<Message> for TextTable<Message> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::tuine::{ use crate::tuine::{
text_table::SortType, StateMap, StatefulComponent, TextTableProps, ViewContext, text_table::SortType, StateMap, StatefulComponent, TextTableProps, BuildContext,
}; };
use super::{DataRow, TextTable}; use super::{DataRow, TextTable};
type Message = (); type Message = ();
fn ctx<'a>(map: &'a mut StateMap) -> ViewContext<'a> { fn ctx<'a>(map: &'a mut StateMap) -> BuildContext<'a> {
ViewContext::new(map) BuildContext::new(map)
} }
#[test] #[test]

View File

@ -22,15 +22,6 @@ use super::{Bounds, DrawContext, Element, Event, LayoutNode, Size, StateContext,
#[allow(unused_variables)] #[allow(unused_variables)]
#[enum_dispatch] #[enum_dispatch]
pub trait TmpComponent<Message> { pub trait TmpComponent<Message> {
/// Builds as component into an [`Element`](super::Element).
#[track_caller]
fn build(self, ctx: ()) -> Element<Message>
where
Self: Sized,
{
todo!()
}
/// Draws the component. /// Draws the component.
fn draw<Backend>( fn draw<Backend>(
&mut self, state_ctx: &mut StateContext<'_>, draw_ctx: &DrawContext<'_>, &mut self, state_ctx: &mut StateContext<'_>, draw_ctx: &DrawContext<'_>,

View File

@ -1,4 +1,4 @@
use crate::tuine::{State, StateContext, ViewContext}; use crate::tuine::{State, StateContext, BuildContext};
use super::TmpComponent; use super::TmpComponent;
@ -11,5 +11,5 @@ pub trait StatefulComponent<Message>: TmpComponent<Message> {
type ComponentState: State; type ComponentState: State;
#[track_caller] #[track_caller]
fn build(ctx: &mut ViewContext<'_>, props: Self::Properties) -> Self; fn build(ctx: &mut BuildContext<'_>, props: Self::Properties) -> Self;
} }

View File

@ -7,7 +7,7 @@ pub struct BatteryTable {}
impl super::AppWidget for BatteryTable { impl super::AppWidget for BatteryTable {
fn build( fn build(
ctx: &mut crate::tuine::ViewContext<'_>, painter: &crate::canvas::Painter, ctx: &mut crate::tuine::BuildContext<'_>, painter: &crate::canvas::Painter,
config: &crate::app::AppConfig, data: &mut crate::data_conversion::ConvertedData<'_>, config: &crate::app::AppConfig, data: &mut crate::data_conversion::ConvertedData<'_>,
) -> Self { ) -> Self {
Self {} Self {}

View File

@ -8,7 +8,7 @@ pub struct CpuGraph {}
impl super::AppWidget for CpuGraph { impl super::AppWidget for CpuGraph {
fn build( fn build(
ctx: &mut crate::tuine::ViewContext<'_>, painter: &crate::canvas::Painter, ctx: &mut crate::tuine::BuildContext<'_>, painter: &crate::canvas::Painter,
config: &crate::app::AppConfig, data: &mut crate::data_conversion::ConvertedData<'_>, config: &crate::app::AppConfig, data: &mut crate::data_conversion::ConvertedData<'_>,
) -> Self { ) -> Self {
Self {} Self {}

View File

@ -9,7 +9,7 @@ pub struct CpuSimple {}
impl super::AppWidget for CpuSimple { impl super::AppWidget for CpuSimple {
fn build( fn build(
ctx: &mut crate::tuine::ViewContext<'_>, painter: &crate::canvas::Painter, ctx: &mut crate::tuine::BuildContext<'_>, painter: &crate::canvas::Painter,
config: &crate::app::AppConfig, data: &mut crate::data_conversion::ConvertedData<'_>, config: &crate::app::AppConfig, data: &mut crate::data_conversion::ConvertedData<'_>,
) -> Self { ) -> Self {
Self {} Self {}

View File

@ -4,7 +4,7 @@ use crate::{
data_conversion::ConvertedData, data_conversion::ConvertedData,
tuine::{ tuine::{
Bounds, DrawContext, LayoutNode, SimpleTable, Size, StateContext, Status, TmpComponent, Bounds, DrawContext, LayoutNode, SimpleTable, Size, StateContext, Status, TmpComponent,
ViewContext, BuildContext,
}, },
}; };
@ -21,7 +21,7 @@ impl<Message> DiskTable<Message> {}
impl<Message> AppWidget for DiskTable<Message> { impl<Message> AppWidget for DiskTable<Message> {
fn build( fn build(
ctx: &mut ViewContext<'_>, painter: &Painter, config: &AppConfig, ctx: &mut BuildContext<'_>, painter: &Painter, config: &AppConfig,
data: &mut ConvertedData<'_>, data: &mut ConvertedData<'_>,
) -> Self { ) -> Self {
let style = simple_table::StyleSheet { let style = simple_table::StyleSheet {

View File

@ -7,7 +7,7 @@ pub struct MemGraph {}
impl super::AppWidget for MemGraph { impl super::AppWidget for MemGraph {
fn build( fn build(
ctx: &mut crate::tuine::ViewContext<'_>, painter: &crate::canvas::Painter, ctx: &mut crate::tuine::BuildContext<'_>, painter: &crate::canvas::Painter,
config: &crate::app::AppConfig, data: &mut crate::data_conversion::ConvertedData<'_>, config: &crate::app::AppConfig, data: &mut crate::data_conversion::ConvertedData<'_>,
) -> Self { ) -> Self {
Self {} Self {}

View File

@ -9,7 +9,7 @@ pub struct MemSimple {}
impl super::AppWidget for MemSimple { impl super::AppWidget for MemSimple {
fn build( fn build(
ctx: &mut crate::tuine::ViewContext<'_>, painter: &crate::canvas::Painter, ctx: &mut crate::tuine::BuildContext<'_>, painter: &crate::canvas::Painter,
config: &crate::app::AppConfig, data: &mut crate::data_conversion::ConvertedData<'_>, config: &crate::app::AppConfig, data: &mut crate::data_conversion::ConvertedData<'_>,
) -> Self { ) -> Self {
Self {} Self {}

View File

@ -31,11 +31,11 @@ pub use mem_simple::*;
pub mod net_simple; pub mod net_simple;
pub use net_simple::*; pub use net_simple::*;
use crate::{app::AppConfig, canvas::Painter, data_conversion::ConvertedData, tuine::ViewContext}; use crate::{app::AppConfig, canvas::Painter, data_conversion::ConvertedData, tuine::BuildContext};
pub trait AppWidget { pub trait AppWidget {
fn build( fn build(
ctx: &mut ViewContext<'_>, painter: &Painter, config: &AppConfig, ctx: &mut BuildContext<'_>, painter: &Painter, config: &AppConfig,
data: &mut ConvertedData<'_>, data: &mut ConvertedData<'_>,
) -> Self; ) -> Self;
} }

View File

@ -7,7 +7,7 @@ pub struct NetGraph {}
impl super::AppWidget for NetGraph { impl super::AppWidget for NetGraph {
fn build( fn build(
ctx: &mut crate::tuine::ViewContext<'_>, painter: &crate::canvas::Painter, ctx: &mut crate::tuine::BuildContext<'_>, painter: &crate::canvas::Painter,
config: &crate::app::AppConfig, data: &mut crate::data_conversion::ConvertedData<'_>, config: &crate::app::AppConfig, data: &mut crate::data_conversion::ConvertedData<'_>,
) -> Self { ) -> Self {
Self {} Self {}

View File

@ -9,7 +9,7 @@ pub struct NetSimple {}
impl super::AppWidget for NetSimple { impl super::AppWidget for NetSimple {
fn build( fn build(
ctx: &mut crate::tuine::ViewContext<'_>, painter: &crate::canvas::Painter, ctx: &mut crate::tuine::BuildContext<'_>, painter: &crate::canvas::Painter,
config: &crate::app::AppConfig, data: &mut crate::data_conversion::ConvertedData<'_>, config: &crate::app::AppConfig, data: &mut crate::data_conversion::ConvertedData<'_>,
) -> Self { ) -> Self {
Self {} Self {}

View File

@ -7,7 +7,7 @@ pub struct ProcessTable {}
impl super::AppWidget for ProcessTable { impl super::AppWidget for ProcessTable {
fn build( fn build(
ctx: &mut crate::tuine::ViewContext<'_>, painter: &crate::canvas::Painter, ctx: &mut crate::tuine::BuildContext<'_>, painter: &crate::canvas::Painter,
config: &crate::app::AppConfig, data: &mut crate::data_conversion::ConvertedData<'_>, config: &crate::app::AppConfig, data: &mut crate::data_conversion::ConvertedData<'_>,
) -> Self { ) -> Self {
Self {} Self {}

View File

@ -5,7 +5,7 @@ use crate::tuine::{
self, block, self, block,
shortcut::ShortcutProps, shortcut::ShortcutProps,
text_table::{self, DataRow, SortType, TextTableProps, TextTableState}, text_table::{self, DataRow, SortType, TextTableProps, TextTableState},
Block, Event, Shortcut, StatefulComponent, Status, TextTable, TmpComponent, ViewContext, Block, BuildContext, Event, Shortcut, StatefulComponent, Status, TextTable, TmpComponent,
}; };
/// A set of styles for a [`SimpleTable`]. /// A set of styles for a [`SimpleTable`].
@ -28,7 +28,7 @@ pub struct SimpleTable<Message> {
impl<Message> SimpleTable<Message> { impl<Message> SimpleTable<Message> {
#[track_caller] #[track_caller]
pub fn build<C: Into<std::borrow::Cow<'static, str>>, R: Into<DataRow>>( pub fn build<C: Into<std::borrow::Cow<'static, str>>, R: Into<DataRow>>(
ctx: &mut ViewContext<'_>, style: StyleSheet, columns: Vec<C>, data: Vec<R>, ctx: &mut BuildContext<'_>, style: StyleSheet, columns: Vec<C>, data: Vec<R>,
sort_index: usize, sort_index: usize,
) -> Self { ) -> Self {
let text_table = TextTable::build( let text_table = TextTable::build(

View File

@ -4,7 +4,7 @@ use crate::{
data_conversion::ConvertedData, data_conversion::ConvertedData,
tuine::{ tuine::{
Bounds, DrawContext, LayoutNode, SimpleTable, Size, StateContext, Status, TmpComponent, Bounds, DrawContext, LayoutNode, SimpleTable, Size, StateContext, Status, TmpComponent,
ViewContext, BuildContext,
}, },
}; };
@ -21,7 +21,7 @@ impl<Message> TempTable<Message> {}
impl<Message> AppWidget for TempTable<Message> { impl<Message> AppWidget for TempTable<Message> {
fn build( fn build(
ctx: &mut ViewContext<'_>, painter: &Painter, config: &AppConfig, ctx: &mut BuildContext<'_>, painter: &Painter, config: &AppConfig,
data: &mut ConvertedData<'_>, data: &mut ConvertedData<'_>,
) -> Self { ) -> Self {
let style = simple_table::StyleSheet { let style = simple_table::StyleSheet {

View File

@ -2,12 +2,12 @@ use crate::tuine::{Caller, Key, State, StateMap};
use super::StateContext; use super::StateContext;
pub struct ViewContext<'a> { pub struct BuildContext<'a> {
key_counter: usize, key_counter: usize,
state_context: StateContext<'a>, state_context: StateContext<'a>,
} }
impl<'a> ViewContext<'a> { impl<'a> BuildContext<'a> {
pub fn new(state_map: &'a mut StateMap) -> Self { pub fn new(state_map: &'a mut StateMap) -> Self {
Self { Self {
key_counter: 0, key_counter: 0,

View File

@ -4,8 +4,8 @@ pub use state_map::StateMap;
pub mod draw_context; pub mod draw_context;
pub use draw_context::DrawContext; pub use draw_context::DrawContext;
pub mod view_context; pub mod build_context;
pub use view_context::ViewContext; pub use build_context::BuildContext;
pub mod state_context; pub mod state_context;
pub use state_context::StateContext; pub use state_context::StateContext;

View File

@ -6,7 +6,7 @@ use crate::tuine::Status;
use super::{ use super::{
build_layout_tree, Application, DrawContext, Element, Event, LayoutNode, StateContext, build_layout_tree, Application, DrawContext, Element, Event, LayoutNode, StateContext,
StateMap, TmpComponent, ViewContext, StateMap, TmpComponent, BuildContext,
}; };
#[derive(Clone, Copy, Debug)] #[derive(Clone, Copy, Debug)]
@ -118,7 +118,7 @@ fn create_user_interface<A>(application: &mut A, app_data: &mut AppData) -> Elem
where where
A: Application + 'static, A: Application + 'static,
{ {
let mut ctx = ViewContext::new(&mut app_data.state_map); let mut ctx = BuildContext::new(&mut app_data.state_map);
application.view(&mut ctx) application.view(&mut ctx)
} }