Skip to content

Commit

Permalink
perf(scrollbar): const creation (#963)
Browse files Browse the repository at this point in the history
A bunch of `const fn` allow for more performance and `Default` now uses the `const` new implementations.
  • Loading branch information
EdJoPaTo committed Feb 24, 2024
1 parent d0067c8 commit 65e7923
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 145 deletions.
28 changes: 9 additions & 19 deletions examples/scrollbar.rs
Expand Up @@ -133,10 +133,7 @@ fn ui(f: &mut Frame, app: &mut App) {
Line::from("This is a line".reset()),
Line::from(vec![
Span::raw("Masked text: "),
Span::styled(
Masked::new("password", '*'),
Style::default().fg(Color::Red),
),
Span::styled(Masked::new("password", '*'), Style::new().fg(Color::Red)),
]),
Line::from("This is a line "),
Line::from("This is a line ".red()),
Expand All @@ -146,20 +143,17 @@ fn ui(f: &mut Frame, app: &mut App) {
Line::from("This is a line".reset()),
Line::from(vec![
Span::raw("Masked text: "),
Span::styled(
Masked::new("password", '*'),
Style::default().fg(Color::Red),
),
Span::styled(Masked::new("password", '*'), Style::new().fg(Color::Red)),
]),
];
app.vertical_scroll_state = app.vertical_scroll_state.content_length(text.len());
app.horizontal_scroll_state = app.horizontal_scroll_state.content_length(long_line.len());

let create_block = |title: &'static str| Block::bordered().gray().title(title.bold());

let title = Block::default()
.title("Use h j k l or ◄ ▲ ▼ ► to scroll ".bold())
.title_alignment(Alignment::Center);
let title = Block::new()
.title_alignment(Alignment::Center)
.title("Use h j k l or ◄ ▲ ▼ ► to scroll ".bold());
f.render_widget(title, chunks[0]);

let paragraph = Paragraph::new(text.clone())
Expand All @@ -168,8 +162,7 @@ fn ui(f: &mut Frame, app: &mut App) {
.scroll((app.vertical_scroll as u16, 0));
f.render_widget(paragraph, chunks[1]);
f.render_stateful_widget(
Scrollbar::default()
.orientation(ScrollbarOrientation::VerticalRight)
Scrollbar::new(ScrollbarOrientation::VerticalRight)
.begin_symbol(Some("↑"))
.end_symbol(Some("↓")),
chunks[1],
Expand All @@ -184,8 +177,7 @@ fn ui(f: &mut Frame, app: &mut App) {
.scroll((app.vertical_scroll as u16, 0));
f.render_widget(paragraph, chunks[2]);
f.render_stateful_widget(
Scrollbar::default()
.orientation(ScrollbarOrientation::VerticalLeft)
Scrollbar::new(ScrollbarOrientation::VerticalLeft)
.symbols(scrollbar::VERTICAL)
.begin_symbol(None)
.track_symbol(None)
Expand All @@ -205,8 +197,7 @@ fn ui(f: &mut Frame, app: &mut App) {
.scroll((0, app.horizontal_scroll as u16));
f.render_widget(paragraph, chunks[3]);
f.render_stateful_widget(
Scrollbar::default()
.orientation(ScrollbarOrientation::HorizontalBottom)
Scrollbar::new(ScrollbarOrientation::HorizontalBottom)
.thumb_symbol("🬋")
.end_symbol(None),
chunks[3].inner(&Margin {
Expand All @@ -224,8 +215,7 @@ fn ui(f: &mut Frame, app: &mut App) {
.scroll((0, app.horizontal_scroll as u16));
f.render_widget(paragraph, chunks[4]);
f.render_stateful_widget(
Scrollbar::default()
.orientation(ScrollbarOrientation::HorizontalBottom)
Scrollbar::new(ScrollbarOrientation::HorizontalBottom)
.thumb_symbol("░")
.track_symbol(Some("─")),
chunks[4].inner(&Margin {
Expand Down

0 comments on commit 65e7923

Please sign in to comment.