Skip to content

Commit 9f14c2d

Browse files
refactor: extract loading rendering logic to crate::ui::loading_screen
The loading logic has particularities like a constant and a static mut variable along with some unsafe code and a `terminal.draw()` call that violates the main call at the start of the app loop. Due to these reasons, extract all the loading rendering logic to a dedicated module `crate::ui::loading_screen`. Co-developed-by: Gabriel Alves <alvesgabriel@usp.br> Signed-off-by: David Tadokoro <davidbtadokoro@usp.br>
1 parent 5d3e450 commit 9f14c2d

File tree

3 files changed

+60
-55
lines changed

3 files changed

+60
-55
lines changed

src/ui.rs

Lines changed: 3 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,20 @@
1-
use std::fmt::Display;
2-
31
use patch_hub::patch::Patch;
42
use ratatui::{
53
layout::{Alignment, Constraint, Direction, Layout, Rect},
6-
prelude::Backend,
74
style::{Color, Modifier, Style},
85
text::{Line, Span, Text},
9-
widgets::{Block, Borders, HighlightSpacing, List, ListItem, ListState, Paragraph, Wrap},
10-
Frame, Terminal,
6+
widgets::{Block, Borders, HighlightSpacing, List, ListItem, ListState, Paragraph},
7+
Frame,
118
};
129

1310
use crate::app::{self, App};
1411
use app::screens::{bookmarked::BookmarkedPatchsetsState, CurrentScreen};
1512

1613
mod details_actions;
1714
mod edit_config;
15+
pub mod loading_screen;
1816
mod render_patchset;
1917

20-
const SPINNER: [char; 8] = [
21-
'\u{1F311}',
22-
'\u{1F312}',
23-
'\u{1F313}',
24-
'\u{1F314}',
25-
'\u{1F315}',
26-
'\u{1F316}',
27-
'\u{1F317}',
28-
'\u{1F318}',
29-
];
30-
static mut SPINNER_TICK: usize = 1;
31-
3218
pub fn draw_ui(f: &mut Frame, app: &App) {
3319
let chunks = Layout::default()
3420
.direction(Direction::Vertical)
@@ -54,42 +40,6 @@ pub fn draw_ui(f: &mut Frame, app: &App) {
5440
render_navi_bar(f, app, chunks[2]);
5541
}
5642

57-
/// This function renders a loading screen taking a `terminal` instance and a
58-
/// `title`.
59-
pub fn render_loading_screen<B: Backend>(
60-
mut terminal: Terminal<B>,
61-
title: impl Display,
62-
) -> Terminal<B> {
63-
let _ = terminal.draw(|f| draw_loading_screen(f, title));
64-
terminal
65-
}
66-
67-
/// Gets the current spinner state and updates the tick.
68-
fn spinner() -> char {
69-
let spinner_state = SPINNER[unsafe { SPINNER_TICK }];
70-
unsafe {
71-
SPINNER_TICK = (SPINNER_TICK + 1) % 8;
72-
}
73-
spinner_state
74-
}
75-
76-
/// The actual implementation of the loading screen rendering. Currently the
77-
/// loading notification is static.
78-
fn draw_loading_screen(f: &mut Frame, title: impl Display) {
79-
let loading_text = format!("{} {}", title, spinner());
80-
81-
let loading_par = Paragraph::new(Line::from(Span::styled(
82-
loading_text,
83-
Style::default().fg(Color::Green),
84-
)))
85-
.block(Block::default().borders(Borders::ALL))
86-
.centered()
87-
.wrap(Wrap { trim: true });
88-
89-
let loading_area = centered_rect(30, 10, f.area());
90-
f.render_widget(loading_par, loading_area);
91-
}
92-
9343
fn render_title(f: &mut Frame, chunk: Rect) {
9444
let title_block = Block::default()
9545
.borders(Borders::ALL)
@@ -367,7 +317,6 @@ fn render_navi_bar(f: &mut Frame, app: &App, chunk: Rect) {
367317
f.render_widget(keys_hint_footer, footer_chunks[1]);
368318
}
369319

370-
#[allow(dead_code)]
371320
/// helper function to create a centered rect using up certain percentage of the available rect `r`
372321
fn centered_rect(percent_x: u16, percent_y: u16, r: Rect) -> Rect {
373322
// Cut the given rectangle into three vertical pieces

src/ui/loading_screen.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
use std::fmt::Display;
2+
3+
use ratatui::{
4+
prelude::Backend,
5+
style::{Color, Style},
6+
text::{Line, Span},
7+
widgets::{Block, Borders, Paragraph, Wrap},
8+
Frame, Terminal,
9+
};
10+
11+
use super::centered_rect;
12+
13+
const SPINNER: [char; 8] = [
14+
'\u{1F311}',
15+
'\u{1F312}',
16+
'\u{1F313}',
17+
'\u{1F314}',
18+
'\u{1F315}',
19+
'\u{1F316}',
20+
'\u{1F317}',
21+
'\u{1F318}',
22+
];
23+
static mut SPINNER_TICK: usize = 1;
24+
25+
/// This function renders a loading screen taking a `terminal` instance and a
26+
/// `title`.
27+
pub fn render<B: Backend>(mut terminal: Terminal<B>, title: impl Display) -> Terminal<B> {
28+
let _ = terminal.draw(|f| draw_loading_screen(f, title));
29+
terminal
30+
}
31+
32+
/// Gets the current spinner state and updates the tick.
33+
fn spinner() -> char {
34+
let char_to_ret = SPINNER[unsafe { SPINNER_TICK }];
35+
unsafe {
36+
SPINNER_TICK = (SPINNER_TICK + 1) % 8;
37+
}
38+
char_to_ret
39+
}
40+
41+
/// The actual implementation of the loading screen rendering. Currently the
42+
/// loading notification is static.
43+
fn draw_loading_screen(f: &mut Frame, title: impl Display) {
44+
let loading_text = format!("{} {}", title, spinner());
45+
46+
let loading_par = Paragraph::new(Line::from(Span::styled(
47+
loading_text,
48+
Style::default().fg(Color::Green),
49+
)))
50+
.block(Block::default().borders(Borders::ALL))
51+
.centered()
52+
.wrap(Wrap { trim: true });
53+
54+
let loading_area = centered_rect(30, 10, f.area());
55+
f.render_widget(loading_par, loading_area);
56+
}

src/utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ macro_rules! loading_screen {
9696

9797
let handle = std::thread::spawn(move || {
9898
while loading_clone.load(std::sync::atomic::Ordering::Relaxed) {
99-
terminal = $crate::ui::render_loading_screen(terminal, $title);
99+
terminal = $crate::ui::loading_screen::render(terminal, $title);
100100
std::thread::sleep(std::time::Duration::from_millis(200));
101101
}
102102

0 commit comments

Comments
 (0)