Skip to content
This repository has been archived by the owner on May 11, 2023. It is now read-only.

Commit

Permalink
terminal-tui: Support context component
Browse files Browse the repository at this point in the history
Signed-off-by: Erik Kundt <[email protected]>
  • Loading branch information
erak committed Nov 6, 2022
1 parent 0db1e8c commit f8b80aa
Showing 1 changed file with 91 additions and 0 deletions.
91 changes: 91 additions & 0 deletions terminal-tui/src/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -635,3 +635,94 @@ impl TabState {
}
}
}

/// A single-line component, that displays multiple spans containing
/// context information.
pub struct ContextBar {
attributes: Props,
context: HighlightedLabel,
id: HighlightedLabel,
title: HighlightedLabel,
author: HighlightedLabel,
count: HighlightedLabel,
}

impl ContextBar {
pub fn new(context: &str, id: &str, title: &str, author: &str, count: &str) -> Self {
Self {
attributes: Props::default(),
context: HighlightedLabel::new(context).background(Color::Rgb(238, 111, 248)),
id: HighlightedLabel::new(id)
.foreground(Color::Rgb(117, 113, 249))
.background(Color::Rgb(40, 40, 40)),
title: HighlightedLabel::new(title)
.foreground(Color::Rgb(70, 70, 70))
.background(Color::Rgb(40, 40, 40)),
author: HighlightedLabel::new(author)
.foreground(Color::Rgb(117, 113, 249))
.background(Color::Rgb(40, 40, 40)),
count: HighlightedLabel::new(count)
.foreground(Color::Rgb(70, 70, 70))
.background(Color::Rgb(50, 50, 50)),
}
.height(1)
}

fn height(mut self, h: u16) -> Self {
self.attr(Attribute::Height, AttrValue::Size(h));
self
}
}

impl MockComponent for ContextBar {
fn view(&mut self, frame: &mut Frame, area: Rect) {
let display = self
.attributes
.get_or(Attribute::Display, AttrValue::Flag(true))
.unwrap_flag();

let context_w = self.context.query(Attribute::Width).unwrap().unwrap_size();
let id_w = self.id.query(Attribute::Width).unwrap().unwrap_size();
let author_w = self.author.query(Attribute::Width).unwrap().unwrap_size();
let count_w = self.count.query(Attribute::Width).unwrap().unwrap_size();

if display {
let layout = HorizontalLayout::new(
vec![
Box::new(self.context.clone()),
Box::new(self.id.clone()),
Box::new(
self.title.clone().width(
area.width
.saturating_sub(context_w + id_w + author_w + count_w),
),
),
Box::new(self.author.clone()),
Box::new(self.count.clone()),
],
area,
)
.build();

for (mut component, area) in layout {
component.view(frame, area);
}
}
}

fn query(&self, attr: Attribute) -> Option<AttrValue> {
self.attributes.get(attr)
}

fn attr(&mut self, attr: Attribute, value: AttrValue) {
self.attributes.set(attr, value)
}

fn state(&self) -> State {
State::None
}

fn perform(&mut self, _cmd: Cmd) -> CmdResult {
CmdResult::None
}
}

0 comments on commit f8b80aa

Please sign in to comment.