Isolate code
Some checks failed
Cargo CI / Build and Test (pull_request) Failing after 1m58s
Cargo CI / Lint (pull_request) Successful in 1m6s

This commit is contained in:
Wojciech Kozlowski 2024-09-14 23:26:50 +02:00
parent 18bbf78a92
commit 694242e386
2 changed files with 25 additions and 9 deletions

20
src/tui/ui/input.rs Normal file
View File

@ -0,0 +1,20 @@
use ratatui::{layout::Rect, widgets::Paragraph, Frame};
use crate::tui::app::InputPublic;
pub struct InputOverlay;
impl InputOverlay {
pub fn paragraph<'a>(text: &str) -> Paragraph<'a> {
Paragraph::new(format!(" {text}"))
}
pub fn place_cursor(input: InputPublic, area: Rect, frame: &mut Frame) {
let width = area.width.max(4) - 4; // keep 2 for borders, 1 for left-pad, and 1 for cursor
let scroll = input.visual_scroll(width as usize);
frame.set_cursor_position((
area.x + ((input.visual_cursor()).max(scroll) - scroll) as u16 + 2,
area.y + 1,
))
}
}

View File

@ -3,6 +3,7 @@ mod display;
mod error_state;
mod fetch_state;
mod info_state;
mod input;
mod match_state;
mod minibuffer;
mod overlay;
@ -25,6 +26,7 @@ use crate::tui::{
error_state::ErrorOverlay,
fetch_state::FetchOverlay,
info_state::{AlbumOverlay, ArtistOverlay},
input::InputOverlay,
match_state::MatchOverlay,
minibuffer::Minibuffer,
overlay::{OverlayBuilder, OverlaySize},
@ -152,15 +154,9 @@ impl Ui {
.with_width(OverlaySize::MarginFactor(4))
.with_height(OverlaySize::Value(3))
.build(frame.area());
let text_area = format!(" {}", input.value());
UiWidget::render_overlay_widget("Input", Paragraph::new(text_area), area, false, frame);
let width = area.width.max(4) - 4; // keep 2 for borders, 1 for left-pad, and 1 for cursor
let scroll = input.visual_scroll(width as usize);
frame.set_cursor_position((
area.x + ((input.visual_cursor()).max(scroll) - scroll) as u16 + 2,
area.y + 1,
))
let input_text = InputOverlay::paragraph(input.value());
UiWidget::render_overlay_widget("Input", input_text, area, false, frame);
InputOverlay::place_cursor(input, area, frame);
}
fn render_error_overlay<S: AsRef<str>>(title: S, msg: S, frame: &mut Frame) {