Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refact: float content interface #724

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 9 additions & 18 deletions tui/src/confirmation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@ use std::borrow::Cow;
use crate::{float::FloatContent, hint::Shortcut};

use crossterm::event::{KeyCode, KeyEvent};
use ratatui::{
layout::Alignment,
prelude::*,
widgets::{Block, Borders, Clear, List},
};
use ratatui::{prelude::*, widgets::List};

pub enum ConfirmStatus {
Confirm,
Expand Down Expand Up @@ -57,19 +53,15 @@ impl ConfirmPrompt {
}

impl FloatContent for ConfirmPrompt {
fn draw(&mut self, frame: &mut Frame, area: Rect) {
let block = Block::default()
.borders(Borders::ALL)
.title(" Confirm selections ")
.title_bottom(" [y] to continue, [n] to abort ")
.title_alignment(Alignment::Center)
.title_style(Style::default().bold())
.style(Style::default());

frame.render_widget(block.clone(), area);
fn top_title(&self) -> Option<Line<'_>> {
Some(Line::from(" Confirm selections ").style(Style::default().bold()))
}

let inner_area = block.inner(area);
fn bottom_title(&self) -> Option<Line<'_>> {
Some(Line::from(" [y] to continue, [n] to abort ").italic())
}

fn draw(&mut self, frame: &mut Frame, area: Rect) {
let paths_text = self
.names
.iter()
Expand All @@ -80,8 +72,7 @@ impl FloatContent for ConfirmPrompt {
})
.collect::<Text>();

frame.render_widget(Clear, inner_area);
frame.render_widget(List::new(paths_text), inner_area);
frame.render_widget(List::new(paths_text), area);
}

fn handle_key_event(&mut self, key: &KeyEvent) -> bool {
Expand Down
24 changes: 22 additions & 2 deletions tui/src/float.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
use crossterm::event::{KeyCode, KeyEvent};
use ratatui::{
layout::{Constraint, Direction, Layout, Rect},
layout::{Alignment, Constraint, Direction, Layout, Rect},
style::{Style, Stylize},
text::Line,
widgets::{Block, Borders, Clear},
Frame,
};

use crate::hint::Shortcut;

pub trait FloatContent {
fn draw(&mut self, frame: &mut Frame, area: Rect);
fn top_title(&self) -> Option<Line<'_>>;
fn bottom_title(&self) -> Option<Line<'_>>;
fn handle_key_event(&mut self, key: &KeyEvent) -> bool;
fn is_finished(&self) -> bool;
fn get_shortcut_list(&self) -> (&str, Box<[Shortcut]>);
Expand Down Expand Up @@ -50,7 +55,22 @@ impl<Content: FloatContent + ?Sized> Float<Content> {

pub fn draw(&mut self, frame: &mut Frame, parent_area: Rect) {
let popup_area = self.floating_window(parent_area);
self.content.draw(frame, popup_area);
let mut block = Block::new()
.borders(Borders::ALL)
.title_alignment(Alignment::Center)
.style(Style::new().reset());

if let Some(top_title) = self.content.top_title() {
block = block.title_top(top_title);
}

if let Some(bottom_title) = self.content.bottom_title() {
block = block.title_bottom(bottom_title);
}

frame.render_widget(Clear, popup_area);
frame.render_widget(&block, popup_area);
self.content.draw(frame, block.inner(popup_area));
}

// Returns true if the floating window is finished.
Expand Down
42 changes: 21 additions & 21 deletions tui/src/floating_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use ratatui::{
layout::Rect,
style::{Style, Stylize},
text::Line,
widgets::{Block, Borders, Clear, List},
widgets::{Block, List},
Frame,
};

Expand All @@ -29,8 +29,8 @@ pub struct FloatingText {
max_line_width: usize,
v_scroll: usize,
h_scroll: usize,
mode_title: String,
frame_height: usize,
title: String,
}

macro_rules! style {
Expand Down Expand Up @@ -125,7 +125,7 @@ fn get_lines_owned(s: &str) -> Vec<String> {
}

impl FloatingText {
pub fn new(text: String, title: &str) -> Self {
pub fn new(text: String, title: String) -> Self {
let src = get_lines(&text)
.into_iter()
.map(|s| s.to_string())
Expand All @@ -134,7 +134,7 @@ impl FloatingText {
let max_line_width = max_width!(src);
Self {
src,
mode_title: title.to_string(),
title,
max_line_width,
v_scroll: 0,
h_scroll: 0,
Expand All @@ -148,6 +148,7 @@ impl FloatingText {
// just apply highlights directly
(max_width!(get_lines(cmd)), Some(cmd.clone()))
}

Command::LocalFile { file, .. } => {
// have to read from tmp dir to get cmd src
let raw = std::fs::read_to_string(file)
Expand All @@ -165,7 +166,7 @@ impl FloatingText {

Some(Self {
src,
mode_title: title,
title,
max_line_width,
h_scroll: 0,
v_scroll: 0,
Expand Down Expand Up @@ -200,24 +201,23 @@ impl FloatingText {
}

impl FloatContent for FloatingText {
fn draw(&mut self, frame: &mut Frame, area: Rect) {
self.frame_height = area.height as usize;
fn top_title(&self) -> Option<Line<'_>> {
let title_text = format!(" {} ", self.title);

let title_line = Line::from(title_text)
.centered()
.style(Style::default().reversed());

// Define the Block with a border and background color
let block = Block::default()
.borders(Borders::ALL)
.title(self.mode_title.clone())
.title_alignment(ratatui::layout::Alignment::Center)
.title_style(Style::default().reversed())
.style(Style::default());
Some(title_line)
}

frame.render_widget(Clear, area);
fn bottom_title(&self) -> Option<Line<'_>> {
None
}

frame.render_widget(block.clone(), area);
fn draw(&mut self, frame: &mut Frame, area: Rect) {
let Rect { height, .. } = area;

// Calculate the inner area to ensure text is not drawn over the border
let inner_area = block.inner(area);
let Rect { height, .. } = inner_area;
let lines = self
.src
.iter()
Expand Down Expand Up @@ -261,7 +261,7 @@ impl FloatContent for FloatingText {
.highlight_style(Style::default().reversed());

// Render the list inside the bordered area
frame.render_widget(list, inner_area);
frame.render_widget(list, area);
}

fn handle_key_event(&mut self, key: &KeyEvent) -> bool {
Expand All @@ -282,7 +282,7 @@ impl FloatContent for FloatingText {

fn get_shortcut_list(&self) -> (&str, Box<[Shortcut]>) {
(
&self.mode_title,
&self.title,
Box::new([
Shortcut::new("Scroll down", ["j", "Down"]),
Shortcut::new("Scroll up", ["k", "Up"]),
Expand Down
Loading