Add multiple lines and color support for popups

This commit is contained in:
2Shirt 2024-11-03 18:06:16 -08:00
parent c62c6c751c
commit c8faa283c8
Signed by: 2Shirt
GPG key ID: 152FAC923B0E132C
3 changed files with 38 additions and 11 deletions

View file

@ -16,7 +16,7 @@
use serde::{Deserialize, Serialize};
use strum::Display;
use crate::{app::Mode, system::disk::Disk};
use crate::{app::Mode, components::popup::Type, system::disk::Disk};
#[derive(Debug, Clone, PartialEq, Eq, Display, Serialize, Deserialize)]
pub enum Action {
@ -28,7 +28,7 @@ pub enum Action {
Quit,
ClearScreen,
DismissPopup,
DisplayPopup(String),
DisplayPopup(Type, String),
Error(String),
Help,
KeyUp,

View file

@ -32,7 +32,12 @@ use tracing::{debug, info};
use crate::{
action::Action,
components::{
footer::Footer, fps::FpsCounter, left::Left, popup::Popup, right::Right, title::Title,
footer::Footer,
fps::FpsCounter,
left::Left,
popup::{Popup, Type},
right::Right,
title::Title,
Component,
},
config::Config,
@ -253,9 +258,10 @@ impl App {
self.action_tx.send(Action::UpdateDiskList(disks.clone()))?;
}
Mode::Done => {
self.action_tx.send(Action::DisplayPopup(String::from(
"COMPLETE\n\nThank you for using this tool!",
)))?;
self.action_tx.send(Action::DisplayPopup(
Type::Success,
String::from("COMPLETE\n\n\nThank you for using this tool!"),
))?;
}
_ => {}
}

View file

@ -15,15 +15,26 @@
//
use color_eyre::Result;
use ratatui::{prelude::*, widgets::*};
use serde::{Deserialize, Serialize};
use strum::Display;
use tokio::sync::mpsc::UnboundedSender;
use super::Component;
use crate::{action::Action, app::Mode, config::Config};
#[derive(Default, Debug, Clone, PartialEq, Eq, Display, Serialize, Deserialize)]
pub enum Type {
#[default]
Info,
Error,
Success,
}
#[derive(Default)]
pub struct Popup {
command_tx: Option<UnboundedSender<Action>>,
config: Config,
popup_type: Type,
popup_text: String,
}
@ -56,7 +67,10 @@ impl Component for Popup {
// add any logic here that should run on every render
}
Action::DismissPopup => self.popup_text.clear(),
Action::DisplayPopup(new_text) => self.popup_text = String::from(new_text),
Action::DisplayPopup(new_type, new_text) => {
self.popup_type = new_type;
self.popup_text = String::from(new_text);
}
Action::SetMode(Mode::ScanDisks) => self.popup_text = String::from("Scanning Disks..."),
_ => {}
}
@ -69,10 +83,17 @@ impl Component for Popup {
return Ok(());
}
let popup_block = Block::default()
.borders(Borders::ALL)
.style(Style::default().cyan().bold());
let popup = Paragraph::new(vec![Line::from(Span::raw(&self.popup_text))])
// Set style/color
let mut style = Style::default().bold();
match self.popup_type {
Type::Info => style = style.cyan(),
Type::Error => style = style.red(),
Type::Success => style = style.green(),
}
// Build block
let popup_block = Block::default().borders(Borders::ALL).style(style);
let popup = Paragraph::new(String::from(&self.popup_text))
.block(popup_block)
.centered()
.wrap(Wrap { trim: false });