Skip to content

Commit

Permalink
WIP frame in context
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasPickering committed Nov 2, 2023
1 parent 88c2cd4 commit e175fe7
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 121 deletions.
39 changes: 10 additions & 29 deletions src/tui/view/component/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{
},
state::Notification,
util::{layout, ButtonBrick, ToTui},
Frame, RenderContext,
DrawContext,
},
},
};
Expand Down Expand Up @@ -58,26 +58,20 @@ impl Component for ErrorModal {
}

impl Draw for ErrorModal {
fn draw(
&self,
context: &RenderContext,
_: (),
frame: &mut Frame,
chunk: Rect,
) {
fn draw(&self, context: &mut DrawContext, _: (), chunk: Rect) {
let [content_chunk, footer_chunk] = layout(
chunk,
Direction::Vertical,
[Constraint::Min(0), Constraint::Length(1)],
);

frame.render_widget(
context.frame.render_widget(
Paragraph::new(self.0.to_tui(context)).wrap(Wrap::default()),
content_chunk,
);

// Prompt the user to get out of here
frame.render_widget(
context.frame.render_widget(
Paragraph::new(
ButtonBrick {
text: "OK",
Expand Down Expand Up @@ -181,14 +175,8 @@ impl Component for PromptModal {
}

impl Draw for PromptModal {
fn draw(
&self,
_context: &RenderContext,
_: (),
frame: &mut Frame,
chunk: Rect,
) {
frame.render_widget(self.text_area.widget(), chunk);
fn draw(&self, context: &mut DrawContext, _: (), chunk: Rect) {
context.frame.render_widget(self.text_area.widget(), chunk);
}
}

Expand All @@ -213,9 +201,8 @@ pub struct HelpTextProps {
impl Draw<HelpTextProps> for HelpText {
fn draw(
&self,
context: &RenderContext,
context: &mut DrawContext,
props: HelpTextProps,
frame: &mut Frame,
chunk: Rect,
) {
// Decide which actions to show based on context. This is definitely
Expand Down Expand Up @@ -255,7 +242,7 @@ impl Draw<HelpTextProps> for HelpText {
.unwrap_or_else(|| "???".into())
})
.join(" / ");
frame.render_widget(Paragraph::new(text), chunk);
context.frame.render_widget(Paragraph::new(text), chunk);
}
}

Expand All @@ -271,14 +258,8 @@ impl NotificationText {
}

impl Draw for NotificationText {
fn draw(
&self,
context: &RenderContext,
_: (),
frame: &mut Frame,
chunk: Rect,
) {
frame.render_widget(
fn draw(&self, context: &mut DrawContext, _: (), chunk: Rect) {
context.frame.render_widget(
Paragraph::new(self.notification.to_tui(context)),
chunk,
);
Expand Down
16 changes: 7 additions & 9 deletions src/tui/view/component/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,20 +92,18 @@ impl UpdateContext {
/// attaching a lifetime to the associated type makes using this in a trait
/// object very difficult (maybe impossible?). This is an easy shortcut.
pub trait Draw<Props = ()> {
fn draw(
&self,
context: &RenderContext,
props: Props,
frame: &mut Frame,
chunk: Rect,
);
fn draw(&self, context: &mut DrawContext, props: Props, chunk: Rect);
}

/// Global readonly data that various components need during rendering
/// Global data that various components need during rendering. A mutable
/// reference to this is passed around to give access to the frame, but please
/// don't modify anything :)
#[derive(Debug)]
pub struct RenderContext<'a> {
pub struct DrawContext<'a, 'f> {
pub input_engine: &'a InputEngine,
pub theme: &'a Theme,
// TODO refcell?
pub frame: &'a mut Frame<'f>,
}

/// A trigger for state change in the view. Events are handled by
Expand Down
20 changes: 8 additions & 12 deletions src/tui/view/component/modal.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
use crate::tui::{
input::Action,
view::{
component::{Component, Draw, Event, UpdateContext, UpdateOutcome},
component::{
Component, Draw, DrawContext, Event, UpdateContext, UpdateOutcome,
},
util::centered_rect,
},
};
use derive_more::Display;
use ratatui::{
prelude::Constraint,
prelude::{Constraint, Rect},
widgets::{Block, BorderType, Borders, Clear},
};
use std::{collections::VecDeque, ops::DerefMut};
Expand Down Expand Up @@ -138,13 +140,7 @@ impl Component for ModalQueue {
}

impl Draw for ModalQueue {
fn draw(
&self,
context: &crate::tui::view::RenderContext,
_: (),
frame: &mut crate::tui::view::Frame,
chunk: ratatui::prelude::Rect,
) {
fn draw(&self, context: &mut DrawContext, _: (), chunk: Rect) {
if let Some(modal) = self.queue.front() {
let (x, y) = modal.dimensions();
let chunk = centered_rect(x, y, chunk);
Expand All @@ -155,11 +151,11 @@ impl Draw for ModalQueue {
let inner_chunk = block.inner(chunk);

// Draw the outline of the modal
frame.render_widget(Clear, chunk);
frame.render_widget(block, chunk);
context.frame.render_widget(Clear, chunk);
context.frame.render_widget(block, chunk);

// Render the actual content
modal.draw(context, (), frame, inner_chunk);
modal.draw(context, (), inner_chunk);
}
}
}
19 changes: 6 additions & 13 deletions src/tui/view/component/primary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::{
},
state::{FixedSelect, RequestState, StatefulList, StatefulSelect},
util::{layout, BlockBrick, ListBrick, ToTui},
Frame, RenderContext,
DrawContext,
},
},
};
Expand Down Expand Up @@ -164,9 +164,8 @@ impl Component for PrimaryView {
impl<'a> Draw<PrimaryViewProps<'a>> for PrimaryView {
fn draw(
&self,
context: &RenderContext,
context: &mut DrawContext,
props: PrimaryViewProps<'a>,
frame: &mut Frame,
chunk: Rect,
) {
// Split the main pane horizontally
Expand Down Expand Up @@ -197,15 +196,13 @@ impl<'a> Draw<PrimaryViewProps<'a>> for PrimaryView {
ListPaneProps {
is_selected: panes.is_selected(&PrimaryPane::ProfileList),
},
frame,
profiles_chunk,
);
self.recipe_list_pane.draw(
context,
ListPaneProps {
is_selected: panes.is_selected(&PrimaryPane::RecipeList),
},
frame,
recipes_chunk,
);
self.request_pane.draw(
Expand All @@ -214,7 +211,6 @@ impl<'a> Draw<PrimaryViewProps<'a>> for PrimaryView {
is_selected: panes.is_selected(&PrimaryPane::Request),
selected_recipe: self.selected_recipe(),
},
frame,
request_chunk,
);
self.response_pane.draw(
Expand All @@ -223,7 +219,6 @@ impl<'a> Draw<PrimaryViewProps<'a>> for PrimaryView {
is_selected: panes.is_selected(&PrimaryPane::Response),
active_request: props.active_request,
},
frame,
response_chunk,
);
}
Expand Down Expand Up @@ -276,9 +271,8 @@ impl Component for ProfileListPane {
impl Draw<ListPaneProps> for ProfileListPane {
fn draw(
&self,
context: &RenderContext,
context: &mut DrawContext,
props: ListPaneProps,
frame: &mut Frame,
chunk: Rect,
) {
let list = ListBrick {
Expand All @@ -288,7 +282,7 @@ impl Draw<ListPaneProps> for ProfileListPane {
},
list: &self.profiles,
};
frame.render_stateful_widget(
context.frame.render_stateful_widget(
list.to_tui(context),
chunk,
&mut self.profiles.state_mut(),
Expand Down Expand Up @@ -356,9 +350,8 @@ impl Component for RecipeListPane {
impl Draw<ListPaneProps> for RecipeListPane {
fn draw(
&self,
context: &RenderContext,
context: &mut DrawContext,
props: ListPaneProps,
frame: &mut Frame,
chunk: Rect,
) {
let pane_kind = PrimaryPane::RecipeList;
Expand All @@ -369,7 +362,7 @@ impl Draw<ListPaneProps> for RecipeListPane {
},
list: &self.recipes,
};
frame.render_stateful_widget(
context.frame.render_stateful_widget(
list.to_tui(context),
chunk,
&mut self.recipes.state_mut(),
Expand Down
20 changes: 11 additions & 9 deletions src/tui/view/component/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
},
state::{FixedSelect, StatefulSelect},
util::{layout, BlockBrick, TabBrick, ToTui},
Frame, RenderContext,
DrawContext,
},
},
};
Expand Down Expand Up @@ -76,9 +76,8 @@ impl Component for RequestPane {
impl<'a> Draw<RequestPaneProps<'a>> for RequestPane {
fn draw(
&self,
context: &RenderContext,
context: &mut DrawContext,
props: RequestPaneProps<'a>,
frame: &mut Frame,
chunk: Rect,
) {
// Render outermost block
Expand All @@ -89,7 +88,7 @@ impl<'a> Draw<RequestPaneProps<'a>> for RequestPane {
};
let block = block.to_tui(context);
let inner_chunk = block.inner(chunk);
frame.render_widget(block, chunk);
context.frame.render_widget(block, chunk);

// Render request contents
if let Some(recipe) = props.selected_recipe {
Expand All @@ -104,31 +103,34 @@ impl<'a> Draw<RequestPaneProps<'a>> for RequestPane {
);

// URL
frame.render_widget(
context.frame.render_widget(
Paragraph::new(format!("{} {}", recipe.method, recipe.url)),
url_chunk,
);

// Navigation tabs
let tabs = TabBrick { tabs: &self.tabs };
frame.render_widget(tabs.to_tui(context), tabs_chunk);
context
.frame
.render_widget(tabs.to_tui(context), tabs_chunk);

// Request content
match self.tabs.selected() {
RequestTab::Body => {
if let Some(body) = recipe.body.as_deref() {
frame
context
.frame
.render_widget(Paragraph::new(body), content_chunk);
}
}
RequestTab::Query => {
frame.render_widget(
context.frame.render_widget(
Paragraph::new(recipe.query.to_tui(context)),
content_chunk,
);
}
RequestTab::Headers => {
frame.render_widget(
context.frame.render_widget(
Paragraph::new(recipe.headers.to_tui(context)),
content_chunk,
);
Expand Down
Loading

0 comments on commit e175fe7

Please sign in to comment.