Skip to content

Commit

Permalink
tui: Constify functions, shrink Margin representation
Browse files Browse the repository at this point in the history
  • Loading branch information
archseer authored and Schuyler Mortimer committed Jul 10, 2024
1 parent 87e0423 commit 7851ce0
Show file tree
Hide file tree
Showing 14 changed files with 102 additions and 112 deletions.
4 changes: 2 additions & 2 deletions helix-term/src/ui/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -532,8 +532,8 @@ impl Component for Completion {
surface.clear_with(doc_area, background);

if cx.editor.popup_border() {
use tui::widgets::{Block, Borders, Widget};
Widget::render(Block::default().borders(Borders::ALL), doc_area, surface);
use tui::widgets::{Block, Widget};
Widget::render(Block::bordered(), doc_area, surface);
}

markdown_doc.render(doc_area, surface, cx);
Expand Down
7 changes: 3 additions & 4 deletions helix-term/src/ui/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use helix_view::graphics::{Margin, Rect};
use helix_view::info::Info;
use tui::buffer::Buffer as Surface;
use tui::text::Text;
use tui::widgets::{Block, Borders, Paragraph, Widget};
use tui::widgets::{Block, Paragraph, Widget};

impl Component for Info {
fn render(&mut self, viewport: Rect, surface: &mut Surface, cx: &mut Context) {
Expand All @@ -23,13 +23,12 @@ impl Component for Info {
));
surface.clear_with(area, popup_style);

let block = Block::default()
let block = Block::bordered()
.title(self.title.as_str())
.borders(Borders::ALL)
.border_style(popup_style);

let margin = Margin::horizontal(1);
let inner = block.inner(area).inner(&margin);
let inner = block.inner(area).inner(margin);
block.render(area, surface);

Paragraph::new(&Text::from(self.text.as_str()))
Expand Down
4 changes: 2 additions & 2 deletions helix-term/src/ui/lsp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ impl Component for SignatureHelp {

let (_, sig_text_height) = crate::ui::text::required_size(&sig_text, area.width);
let sig_text_area = area.clip_top(1).with_height(sig_text_height);
let sig_text_area = sig_text_area.inner(&margin).intersection(surface.area);
let sig_text_area = sig_text_area.inner(margin).intersection(surface.area);
let sig_text_para = Paragraph::new(&sig_text).wrap(Wrap { trim: false });
sig_text_para.render(sig_text_area, surface);

Expand All @@ -153,7 +153,7 @@ impl Component for SignatureHelp {
let sig_doc_para = Paragraph::new(&sig_doc)
.wrap(Wrap { trim: false })
.scroll((cx.scroll.unwrap_or_default() as u16, 0));
sig_doc_para.render(sig_doc_area.inner(&margin), surface);
sig_doc_para.render(sig_doc_area.inner(margin), surface);
}

fn required_size(&mut self, viewport: (u16, u16)) -> Option<(u16, u16)> {
Expand Down
2 changes: 1 addition & 1 deletion helix-term/src/ui/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ impl Component for Markdown {
.scroll((cx.scroll.unwrap_or_default() as u16, 0));

let margin = Margin::all(1);
par.render(area.inner(&margin), surface);
par.render(area.inner(margin), surface);
}

fn required_size(&mut self, viewport: (u16, u16)) -> Option<(u16, u16)> {
Expand Down
20 changes: 9 additions & 11 deletions helix-term/src/ui/picker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use tui::{
buffer::Buffer as Surface,
layout::Constraint,
text::{Span, Spans},
widgets::{Block, BorderType, Borders, Cell, Table},
widgets::{Block, BorderType, Cell, Table},
};

use tui::widgets::Widget;
Expand Down Expand Up @@ -555,13 +555,12 @@ impl<T: Item + 'static> Picker<T> {
let background = cx.editor.theme.get("ui.background");
surface.clear_with(area, background);

// don't like this but the lifetime sucks
let block = Block::default().borders(Borders::ALL);
const BLOCK: Block<'_> = Block::bordered();

// calculate the inner area inside the box
let inner = block.inner(area);
let inner = BLOCK.inner(area);

block.render(area, surface);
BLOCK.render(area, surface);

// -- Render the input bar:

Expand Down Expand Up @@ -706,15 +705,14 @@ impl<T: Item + 'static> Picker<T> {
let text = cx.editor.theme.get("ui.text");
surface.clear_with(area, background);

// don't like this but the lifetime sucks
let block = Block::default().borders(Borders::ALL);
const BLOCK: Block<'_> = Block::bordered();

// calculate the inner area inside the box
let inner = block.inner(area);
let inner = BLOCK.inner(area);
// 1 column gap on either side
let margin = Margin::horizontal(1);
let inner = inner.inner(&margin);
block.render(area, surface);
let inner = inner.inner(margin);
BLOCK.render(area, surface);

if let Some((path, range)) = self.current_file(cx.editor) {
let preview = self.get_preview(path, cx.editor);
Expand Down Expand Up @@ -937,7 +935,7 @@ impl<T: Item + 'static + Send + Sync> Component for Picker<T> {
}

fn cursor(&self, area: Rect, editor: &Editor) -> (Option<Position>, CursorKind) {
let block = Block::default().borders(Borders::ALL);
let block = Block::bordered();
// calculate the inner area inside the box
let inner = block.inner(area);

Expand Down
6 changes: 3 additions & 3 deletions helix-term/src/ui/popup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
};
use tui::{
buffer::Buffer as Surface,
widgets::{Block, Borders, Widget},
widgets::{Block, Widget},
};

use helix_core::Position;
Expand Down Expand Up @@ -323,8 +323,8 @@ impl<T: Component> Component for Popup<T> {

let mut inner = area;
if render_borders {
inner = area.inner(&Margin::all(1));
Widget::render(Block::default().borders(Borders::ALL), area, surface);
inner = area.inner(Margin::all(1));
Widget::render(Block::bordered(), area, surface);
}
let border = usize::from(render_borders);

Expand Down
7 changes: 3 additions & 4 deletions helix-term/src/ui/prompt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use helix_view::keyboard::KeyCode;
use std::sync::Arc;
use std::{borrow::Cow, ops::RangeFrom};
use tui::buffer::Buffer as Surface;
use tui::widgets::{Block, Borders, Widget};
use tui::widgets::{Block, Widget};

use helix_core::{
unicode::segmentation::GraphemeCursor, unicode::width::UnicodeWidthStr, Position,
Expand Down Expand Up @@ -457,12 +457,11 @@ impl Prompt {
let background = theme.get("ui.help");
surface.clear_with(area, background);

let block = Block::default()
let block = Block::bordered()
// .title(self.title.as_str())
.borders(Borders::ALL)
.border_style(background);

let inner = block.inner(area).inner(&Margin::horizontal(1));
let inner = block.inner(area).inner(Margin::horizontal(1));

block.render(area, surface);
text.render(inner, surface, cx);
Expand Down
16 changes: 7 additions & 9 deletions helix-tui/src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,24 +83,22 @@ impl Layout {
self
}

pub fn margin(mut self, margin: u16) -> Layout {
pub const fn margin(mut self, margin: u16) -> Layout {
self.margin = Margin::all(margin);
self
}

pub fn horizontal_margin(mut self, horizontal: u16) -> Layout {
self.margin.left = horizontal;
self.margin.right = horizontal;
pub const fn horizontal_margin(mut self, horizontal: u16) -> Layout {
self.margin.horizontal = horizontal;
self
}

pub fn vertical_margin(mut self, vertical: u16) -> Layout {
self.margin.top = vertical;
self.margin.bottom = vertical;
pub const fn vertical_margin(mut self, vertical: u16) -> Layout {
self.margin.vertical = vertical;
self
}

pub fn direction(mut self, direction: Direction) -> Layout {
pub const fn direction(mut self, direction: Direction) -> Layout {
self.direction = direction;
self
}
Expand Down Expand Up @@ -191,7 +189,7 @@ fn split(area: Rect, layout: &Layout) -> Vec<Rect> {
.map(|_| Rect::default())
.collect::<Vec<Rect>>();

let dest_area = area.inner(&layout.margin);
let dest_area = area.inner(layout.margin);
for (i, e) in elements.iter().enumerate() {
vars.insert(e.x, (i, 0));
vars.insert(e.y, (i, 1));
Expand Down
48 changes: 25 additions & 23 deletions helix-tui/src/widgets/block.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
buffer::Buffer,
symbols::line,
text::{Span, Spans},
text::Spans,
widgets::{Borders, Widget},
};
use helix_view::graphics::{Rect, Style};
Expand Down Expand Up @@ -58,6 +58,22 @@ pub struct Block<'a> {
}

impl<'a> Block<'a> {
pub const fn new() -> Self {
Self {
title: None,
borders: Borders::empty(),
border_style: Style::new(),
border_type: BorderType::Plain,
style: Style::new(),
}
}

pub const fn bordered() -> Self {
let mut block = Self::new();
block.borders = Borders::ALL;
block
}

pub fn title<T>(mut self, title: T) -> Block<'a>
where
T: Into<Spans<'a>>,
Expand All @@ -66,34 +82,22 @@ impl<'a> Block<'a> {
self
}

#[deprecated(
since = "0.10.0",
note = "You should use styling capabilities of `text::Spans` given as argument of the `title` method to apply styling to the title."
)]
pub fn title_style(mut self, style: Style) -> Block<'a> {
if let Some(t) = self.title {
let title = String::from(&t);
self.title = Some(Spans::from(Span::styled(title, style)));
}
self
}

pub fn border_style(mut self, style: Style) -> Block<'a> {
pub const fn border_style(mut self, style: Style) -> Block<'a> {
self.border_style = style;
self
}

pub fn style(mut self, style: Style) -> Block<'a> {
pub const fn style(mut self, style: Style) -> Block<'a> {
self.style = style;
self
}

pub fn borders(mut self, flag: Borders) -> Block<'a> {
pub const fn borders(mut self, flag: Borders) -> Block<'a> {
self.borders = flag;
self
}

pub fn border_type(mut self, border_type: BorderType) -> Block<'a> {
pub const fn border_type(mut self, border_type: BorderType) -> Block<'a> {
self.border_type = border_type;
self
}
Expand Down Expand Up @@ -413,9 +417,7 @@ mod tests {

// All borders
assert_eq!(
Block::default()
.borders(Borders::ALL)
.inner(Rect::default()),
Block::bordered().inner(Rect::default()),
Rect {
x: 0,
y: 0,
Expand All @@ -425,7 +427,7 @@ mod tests {
"all borders, width=0, height=0"
);
assert_eq!(
Block::default().borders(Borders::ALL).inner(Rect {
Block::bordered().inner(Rect {
x: 0,
y: 0,
width: 1,
Expand All @@ -440,7 +442,7 @@ mod tests {
"all borders, width=1, height=1"
);
assert_eq!(
Block::default().borders(Borders::ALL).inner(Rect {
Block::bordered().inner(Rect {
x: 0,
y: 0,
width: 2,
Expand All @@ -455,7 +457,7 @@ mod tests {
"all borders, width=2, height=2"
);
assert_eq!(
Block::default().borders(Borders::ALL).inner(Rect {
Block::bordered().inner(Rect {
x: 0,
y: 0,
width: 3,
Expand Down
2 changes: 1 addition & 1 deletion helix-tui/src/widgets/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ impl<'a> ListItem<'a> {
/// # use helix_tui::style::{Style, Color, Modifier};
/// let items = [ListItem::new("Item 1"), ListItem::new("Item 2"), ListItem::new("Item 3")];
/// List::new(items)
/// .block(Block::default().title("List").borders(Borders::ALL))
/// .block(Block::bordered().title("List"))
/// .style(Style::default().fg(Color::White))
/// .highlight_style(Style::default().add_modifier(Modifier::ITALIC))
/// .highlight_symbol(">>");
Expand Down
2 changes: 1 addition & 1 deletion helix-tui/src/widgets/paragraph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ fn get_line_offset(line_width: u16, text_area_width: u16, alignment: Alignment)
/// Spans::from(Span::styled("Second line", Style::default().fg(Color::Red))),
/// ]);
/// Paragraph::new(&text)
/// .block(Block::default().title("Paragraph").borders(Borders::ALL))
/// .block(Block::bordered().title("Paragraph"))
/// .style(Style::default().fg(Color::White).bg(Color::Black))
/// .alignment(Alignment::Center)
/// .wrap(Wrap { trim: true });
Expand Down
10 changes: 5 additions & 5 deletions helix-tui/tests/widgets_paragraph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
// let size = f.size();
// let text = Text::from(SAMPLE_STRING);
// let paragraph = Paragraph::new(&text)
// .block(Block::default().borders(Borders::ALL))
// .block(Block::bordered())
// .alignment(alignment)
// .wrap(Wrap { trim: true });
// f.render_widget(paragraph, size);
Expand Down Expand Up @@ -90,7 +90,7 @@
// let size = f.size();
// let text = Text::from(s);
// let paragraph = Paragraph::new(&text)
// .block(Block::default().borders(Borders::ALL))
// .block(Block::bordered())
// .wrap(Wrap { trim: true });
// f.render_widget(paragraph, size);
// })
Expand Down Expand Up @@ -122,7 +122,7 @@
// let size = f.size();
// let text = Text::from(s);
// let paragraph = Paragraph::new(&text)
// .block(Block::default().borders(Borders::ALL))
// .block(Block::bordered())
// .wrap(Wrap { trim: true });
// f.render_widget(paragraph, size);
// })
Expand Down Expand Up @@ -156,7 +156,7 @@
// .draw(|f| {
// let size = f.size();
// let text = Text::from(line);
// let paragraph = Paragraph::new(&text).block(Block::default().borders(Borders::ALL));
// let paragraph = Paragraph::new(&text).block(Block::bordered());
// f.render_widget(paragraph, size);
// })
// .unwrap();
Expand All @@ -175,7 +175,7 @@
// "段落现在可以水平滚动了!\nParagraph can scroll horizontally!\nShort line",
// );
// let paragraph = Paragraph::new(&text)
// .block(Block::default().borders(Borders::ALL))
// .block(Block::bordered())
// .alignment(alignment)
// .scroll(scroll);
// f.render_widget(paragraph, size);
Expand Down
Loading

0 comments on commit 7851ce0

Please sign in to comment.