Skip to content

Commit

Permalink
refactor: auto-fit width for Outline (close #12)
Browse files Browse the repository at this point in the history
This also removes max_width from Scroll which means if a widget
need max_width as a state, add it as a field to the inner lines.
  • Loading branch information
zjp-CN committed Mar 22, 2024
1 parent 1ab5f69 commit 1fe8633
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 36 deletions.
9 changes: 6 additions & 3 deletions src/bin/dashboard/ui/version_features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ impl VersionFeatures {
}
let [outer, pkg_toml] = ver_feat_toml::split_for_pkg_toml(outer);
self.pkg_toml.update_area(pkg_toml);
let [ver, feat] = split_ver_feat(outer, self.versions.inner.max_width);
let [ver, feat] = split_ver_feat(outer, self.versions.inner.lines.max_width);
self.features.update_area(feat);
self.versions.update_area(ver);
}
Expand Down Expand Up @@ -211,9 +211,11 @@ impl Versions {
let border = Surround::new(Block::new().title("Version").borders(Borders::ALL), area);
Self {
inner: Scroll {
lines: VersionsInner { all: all_verions },
lines: VersionsInner {
all: all_verions,
max_width,
},
area: border.inner(),
max_width,
..Default::default()
},
border,
Expand Down Expand Up @@ -260,6 +262,7 @@ impl Versions {
struct VersionsInner {
/// TODO: add cached status
all: Vec<PkgInfo>,
max_width: u16,
}

impl std::ops::Deref for VersionsInner {
Expand Down
2 changes: 1 addition & 1 deletion src/bin/page/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use ratatui::{

impl Page {
fn layout(&self) -> Layout {
let outline_width = self.outline.display_ref().max_width + 1;
let outline_width = self.outline.max_width() + 1;
Layout::default()
.direction(Direction::Horizontal)
.constraints([
Expand Down
13 changes: 10 additions & 3 deletions src/bin/page/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub struct Page {
impl Page {
pub fn new(pkg_key: PkgKey, doc: CrateDoc, area: Rect) -> Result<Self> {
let mut page = Page {
outline: Outline::new(&doc),
outline: Outline::new(&doc, area.height),
content: Content {
inner: content::ContentInner::new(&doc),
..Default::default()
Expand Down Expand Up @@ -93,9 +93,9 @@ struct Outline {
}

impl Outline {
fn new(doc: &CrateDoc) -> Self {
fn new(doc: &CrateDoc, height: u16) -> Self {
Outline {
inner: outline::OutlineInner::new(doc),
inner: outline::OutlineInner::new(doc, height),
..Default::default()
}
}
Expand Down Expand Up @@ -133,6 +133,13 @@ impl Outline {
fn is_module_tree(&self) -> bool {
self.inner.is_module_tree()
}

fn max_width(&self) -> u16 {
self.display_ref()
.visible_lines()
.and_then(|lines| lines.iter().map(|l| l.width()).max())
.unwrap_or(0)
}
}

#[derive(Default, Debug)]
Expand Down
5 changes: 2 additions & 3 deletions src/bin/page/outline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ impl std::fmt::Debug for OutlineInner {
}

impl OutlineInner {
pub fn new(doc: &CrateDoc) -> Self {
let modules = match ScrollTreeLines::new_tree_lines(doc.clone().into()) {
pub fn new(doc: &CrateDoc, height: u16) -> Self {
let modules = match ScrollTreeLines::new_tree_lines(doc.clone().into(), height) {
Ok(lines) => lines,
Err(err) => {
error!("Failed to init module Outline:\n{err}");
Expand Down Expand Up @@ -135,7 +135,6 @@ impl Setu {
error!("{path} generated unexpected empty TreeLines");
}
// self.update_area(modules.area);
self.display.update_maxwidth();
self.display.start = 0;
self.display.cursor.y = 0;
Some(())
Expand Down
1 change: 0 additions & 1 deletion src/bin/page/page_fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ impl Page {
}

fn update_after_folding_outline(&mut self) {
self.outline().update_maxwidth();
self.update_area_inner(self.area);

let outline = self.outline();
Expand Down
22 changes: 21 additions & 1 deletion src/bin/page/page_scroll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ use crate::ui::scrollable::{ScrollOffset, ScrollText, ScrollTreeLines};
macro_rules! current {
($self:ident: $outline:block; $content:block $(;)?) => {
match $self.current {
Some(Panel::Outline) => $outline,
Some(Panel::Outline) => {
let old_max_width = $self.outline.max_width();
$outline
$self.update_area_due_to_outline_max_width(old_max_width);
}
Some(Panel::Content) => $content,
_ => (),
};
Expand Down Expand Up @@ -76,28 +80,44 @@ impl Page {
/// to implement content cursor movement.
impl Page {
pub fn move_forward_cursor(&mut self) {
let old_max_width = self.outline.max_width();
self.outline().move_forward_cursor();
self.update_content();
self.update_area_due_to_outline_max_width(old_max_width);
}

pub fn move_backward_cursor(&mut self) {
let old_max_width = self.outline.max_width();
self.outline().move_backward_cursor();
self.update_content();
self.update_area_due_to_outline_max_width(old_max_width);
}

pub fn move_top_cursor(&mut self) {
let old_max_width = self.outline.max_width();
self.outline().move_top_cursor();
self.update_content();
self.update_area_due_to_outline_max_width(old_max_width);
}

pub fn move_bottom_cursor(&mut self) {
let old_max_width = self.outline.max_width();
self.outline().move_bottom_cursor();
self.update_content();
self.update_area_due_to_outline_max_width(old_max_width);
}

pub fn move_middle_cursor(&mut self) {
let old_max_width = self.outline.max_width();
self.outline().move_middle_cursor();
self.update_content();
self.update_area_due_to_outline_max_width(old_max_width);
}

fn update_area_due_to_outline_max_width(&mut self, old_max_width: u16) {
if self.outline.max_width() != old_max_width {
self.update_area_inner(self.area);
}
}

/// update content's StyledLines and Headings aftet setting the cursor
Expand Down
9 changes: 0 additions & 9 deletions src/bin/ui/scrollable/markdown/heading.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use crate::{
};
use ratatui::prelude::Buffer;
use term_rustdoc::{tree::Text, util::XString};
use unicode_width::UnicodeWidthStr;

pub type ScrollHeading = Scroll<Headings>;

Expand Down Expand Up @@ -77,15 +76,7 @@ impl Headings {

impl ScrollHeading {
pub fn update_headings(&mut self, headings: Headings) {
// NOTE: max_width is the real maximum width of all lines,
// and the display width can be smaller then max_width to truncate heading lines.
let max_width = headings
.iter()
.map(|h| h.as_str().width() as u16)
.max()
.unwrap_or(0);
self.lines = headings;
self.max_width = max_width;
}

pub fn render(&self, buf: &mut Buffer, content_start: usize, content_end: usize) {
Expand Down
23 changes: 8 additions & 15 deletions src/bin/ui/scrollable/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{err, Result};
use crate::Result;
use ratatui::buffer::Cell;
use ratatui::prelude::{Buffer, Rect};
use std::fmt;
Expand Down Expand Up @@ -45,8 +45,6 @@ pub struct Scroll<Ls: Lines> {
pub start: usize,
/// The row position where cursor was last time
pub cursor: Cursor<<Ls::Line as LineState>::State>,
/// The maximum width among all lines
pub max_width: u16,
/// The widget area, usually not the full screen
pub area: Rect,
}
Expand Down Expand Up @@ -133,12 +131,11 @@ where
<Ls::Line as LineState>::State: Default,
{
fn default() -> Self where {
let (lines, start, cursor, max_windth, area) = Default::default();
let (lines, start, cursor, area) = Default::default();
Scroll {
lines,
start,
cursor,
max_width: max_windth,
area,
}
}
Expand All @@ -148,13 +145,14 @@ impl<Ls> Scroll<Ls>
where
Ls: Default + Lines<Line = TreeLine>,
{
pub fn new_tree_lines(lines: Ls) -> Result<Self> {
let w = lines.iter().map(TreeLine::width).max();
let max_windth = w.ok_or_else(|| err!("The documentation is empty with no items."))?;

// TODO: rm me
pub fn new_tree_lines(lines: Ls, height: u16) -> Result<Self> {
Ok(Self {
lines,
max_width: max_windth,
area: Rect {
height, // for getting correct visible_lines and basic layout
..Default::default()
},
..Default::default()
})
}
Expand All @@ -166,10 +164,6 @@ where
.get(self.cursor.y as usize + self.start)
.and_then(|l| l.id.as_deref())
}

pub fn update_maxwidth(&mut self) {
self.max_width = self.lines.iter().map(TreeLine::width).max().unwrap();
}
}

impl<Ls: Lines> fmt::Debug for Scroll<Ls> {
Expand All @@ -178,7 +172,6 @@ impl<Ls: Lines> fmt::Debug for Scroll<Ls> {
s.field("lines.len", &self.total_len())
.field("start", &self.start)
.field("cursor.y", &self.cursor.y)
.field("max_windth", &self.max_width)
.field("area", &self.area);
s.finish()
}
Expand Down

0 comments on commit 1fe8633

Please sign in to comment.