refactor(tui): child widget can provide constraint to container
All checks were successful
Rust / build (push) Successful in 7m13s
ci/woodpecker/push/cron-docker-builder Pipeline was successful
ci/woodpecker/push/push-next Pipeline was successful
ci/woodpecker/push/tag-created Pipeline was successful
Release Please / Release-plz (push) Successful in 1m16s

This commit is contained in:
Paul Campbell 2024-08-28 07:53:56 +01:00
parent 4517fe62e4
commit f85cbce4c6
3 changed files with 49 additions and 34 deletions

View file

@ -1,13 +1,21 @@
use git_next_core::git::graph::Log; use git_next_core::git::graph::Log;
// //
use ratatui::{ use ratatui::{
prelude::Constraint,
text::{Line, Text}, text::{Line, Text},
widgets::{Paragraph, Widget}, widgets::{Paragraph, Widget},
}; };
use super::HeightConstraint;
pub struct CommitLog<'a> { pub struct CommitLog<'a> {
pub log: &'a Log, pub log: &'a Log,
} }
impl<'a> HeightConstraint for CommitLog<'a> {
fn height_constraint(&self) -> Constraint {
Constraint::Length(u16::try_from(self.log.len()).unwrap_or(u16::MAX))
}
}
impl<'a> Widget for CommitLog<'a> { impl<'a> Widget for CommitLog<'a> {
fn render(self, area: ratatui::prelude::Rect, buf: &mut ratatui::prelude::Buffer) fn render(self, area: ratatui::prelude::Rect, buf: &mut ratatui::prelude::Buffer)
where where

View file

@ -6,3 +6,11 @@ mod repo;
pub use configured_app::ConfiguredAppWidget; pub use configured_app::ConfiguredAppWidget;
pub use history::CommitLog; pub use history::CommitLog;
use ratatui::layout::Constraint;
pub trait HeightConstraint {
fn height_constraint(&self) -> Constraint;
}
// pub trait WidthConstraints {
// fn width_constraint(&self) -> Constraint;
// }

View file

@ -14,34 +14,30 @@ use identity::Identity;
use ratatui::{ use ratatui::{
buffer::Buffer, buffer::Buffer,
layout::{Constraint, Direction, Layout, Rect}, layout::{Constraint, Rect},
widgets::{Block, Borders, Widget}, widgets::{Block, Borders, Widget},
}; };
use super::HeightConstraint;
pub struct RepoWidget<'a> { pub struct RepoWidget<'a> {
pub repo_state: &'a RepoState, pub repo_state: &'a RepoState,
} }
impl<'a> Widget for RepoWidget<'a> { impl<'a> RepoWidget<'a> {
fn render(self, area: Rect, buf: &mut Buffer) fn inner(&self) -> InnerRepoWidget {
where
Self: Sized,
{
match self.repo_state { match self.repo_state {
RepoState::Identified { RepoState::Identified {
repo_alias, repo_alias,
message, message,
alert, alert,
} => { } => InnerRepoWidget {
InnerRepoWidget { label: "identified",
label: "identified", repo_alias,
repo_alias, message,
message, alert: alert.as_ref().map(String::as_str),
alert: alert.as_ref().map(String::as_str), branches: None,
branches: None, log: None,
log: None, },
}
.render(area, buf);
}
RepoState::Configured { RepoState::Configured {
repo_alias, repo_alias,
@ -56,8 +52,7 @@ impl<'a> Widget for RepoWidget<'a> {
alert: alert.as_ref().map(String::as_str), alert: alert.as_ref().map(String::as_str),
branches: Some(branches), branches: Some(branches),
log: Some(log), log: Some(log),
} },
.render(area, buf),
RepoState::Ready { RepoState::Ready {
repo_alias, repo_alias,
@ -81,12 +76,18 @@ impl<'a> Widget for RepoWidget<'a> {
// main, // main,
// next, // next,
// dev, // dev,
} },
.render(area, buf), }
}; }
}
impl<'a> Widget for RepoWidget<'a> {
fn render(self, area: Rect, buf: &mut Buffer)
where
Self: Sized,
{
self.inner().render(area, buf);
} }
} }
//
struct InnerRepoWidget<'a> { struct InnerRepoWidget<'a> {
pub label: &'a str, pub label: &'a str,
@ -100,6 +101,13 @@ struct InnerRepoWidget<'a> {
// pub next: &'a Commit, // pub next: &'a Commit,
// pub dev: &'a Commit, // pub dev: &'a Commit,
} }
impl<'a> HeightConstraint for InnerRepoWidget<'a> {
fn height_constraint(&self) -> ratatui::prelude::Constraint {
self.log
.map(|log| CommitLog { log })
.map_or(Constraint::Length(0), |w| w.height_constraint())
}
}
impl<'a> Widget for InnerRepoWidget<'a> { impl<'a> Widget for InnerRepoWidget<'a> {
fn render(self, area: Rect, buf: &mut Buffer) fn render(self, area: Rect, buf: &mut Buffer)
where where
@ -114,18 +122,9 @@ impl<'a> Widget for InnerRepoWidget<'a> {
self.branches, self.branches,
)) ))
.borders(Borders::ALL); .borders(Borders::ALL);
let log_len: u16 = self.log.map_or_else(
|| 0,
|log| u16::try_from(log.len()).map_or(u16::MAX, |len| len),
);
let layout_repo = Layout::default()
.direction(Direction::Vertical)
.constraints(vec![Constraint::Length(log_len)])
.split(block.inner(area));
block.render(area, buf);
if let Some(log) = self.log { if let Some(log) = self.log {
CommitLog { log }.render(layout_repo[0], buf); (CommitLog { log }).render(block.inner(area), buf);
} }
block.render(area, buf);
} }
} }