mirror of
https://github.com/ChrisTitusTech/linutil.git
synced 2024-11-05 21:28:48 +00:00
Added FloatingText class. It shows text in a float
This commit is contained in:
parent
6e0540c68a
commit
ab2cc1340f
|
@ -1,5 +1,5 @@
|
|||
use crate::{
|
||||
float::Float, preview_content::PreviewContent, running_command::Command, state::AppState,
|
||||
float::Float, preview_content::FloatingText, running_command::Command, state::AppState,
|
||||
};
|
||||
use crossterm::event::{KeyCode, KeyEvent, KeyEventKind};
|
||||
use ego_tree::{tree, NodeId};
|
||||
|
@ -33,7 +33,7 @@ pub struct CustomList {
|
|||
// This stores the filtered tree
|
||||
filtered_items: Vec<ListNode>,
|
||||
// This is the preview window for the commands
|
||||
preview_float: Float<PreviewContent>,
|
||||
preview_float: Float<FloatingText>,
|
||||
}
|
||||
|
||||
impl CustomList {
|
||||
|
@ -416,7 +416,7 @@ impl CustomList {
|
|||
};
|
||||
|
||||
self.preview_float
|
||||
.set_content(Some(PreviewContent::new(lines)));
|
||||
.set_content(Some(FloatingText::new(lines)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
82
src/preview_content.rs
Normal file
82
src/preview_content.rs
Normal file
|
@ -0,0 +1,82 @@
|
|||
use crate::float::FloatContent;
|
||||
use crossterm::event::{KeyCode, KeyEvent};
|
||||
use ratatui::{
|
||||
layout::Rect,
|
||||
style::{Style, Stylize},
|
||||
text::Line,
|
||||
widgets::{Block, Borders, List},
|
||||
Frame,
|
||||
};
|
||||
|
||||
pub struct FloatingText {
|
||||
text: Vec<String>,
|
||||
scroll: usize,
|
||||
}
|
||||
|
||||
impl FloatingText {
|
||||
pub fn new(text: Vec<String>) -> Self {
|
||||
Self { text, scroll: 0 }
|
||||
}
|
||||
|
||||
fn scroll_down(&mut self) {
|
||||
if self.scroll + 1 < self.text.len() {
|
||||
self.scroll += 1;
|
||||
}
|
||||
}
|
||||
|
||||
fn scroll_up(&mut self) {
|
||||
if self.scroll > 0 {
|
||||
self.scroll -= 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl FloatContent for FloatingText {
|
||||
fn draw(&mut self, frame: &mut Frame, area: Rect) {
|
||||
// Define the Block with a border and background color
|
||||
let block = Block::default()
|
||||
.borders(Borders::ALL)
|
||||
.style(Style::default());
|
||||
|
||||
// Draw the Block first
|
||||
frame.render_widget(block.clone(), area);
|
||||
|
||||
// Calculate the inner area to ensure text is not drawn over the border
|
||||
let inner_area = block.inner(area);
|
||||
|
||||
// Create the list of lines to be displayed
|
||||
let lines: Vec<Line> = self
|
||||
.text
|
||||
.iter()
|
||||
.skip(self.scroll)
|
||||
.take(inner_area.height as usize)
|
||||
.map(|line| Line::from(line.as_str()))
|
||||
.collect();
|
||||
|
||||
// Create list widget
|
||||
let list = List::new(lines)
|
||||
.block(Block::default())
|
||||
.highlight_style(Style::default().reversed());
|
||||
|
||||
// Render the list inside the bordered area
|
||||
frame.render_widget(list, inner_area);
|
||||
}
|
||||
|
||||
fn handle_key_event(&mut self, key: &KeyEvent) -> bool {
|
||||
match key.code {
|
||||
KeyCode::Down | KeyCode::Char('j') => {
|
||||
self.scroll_down();
|
||||
true
|
||||
}
|
||||
KeyCode::Up | KeyCode::Char('k') => {
|
||||
self.scroll_up();
|
||||
true
|
||||
}
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
fn is_finished(&self) -> bool {
|
||||
true
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user