deja-vu/src/components/popup.rs
2Shirt 7d4f28f950
Move fast, break things
Data structures and logic was changing very fast.  Don't have the time
to break downt the individual sections ATM.  Sorry future reader (me).
2024-11-03 17:49:30 -08:00

101 lines
3.1 KiB
Rust

use clap::command;
// This file is part of Deja-vu.
//
// Deja-vu is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Deja-vu is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Deja-vu. If not, see <https://www.gnu.org/licenses/>.
//
use color_eyre::Result;
use ratatui::{prelude::*, widgets::*};
use tokio::sync::mpsc::UnboundedSender;
use super::Component;
use crate::{
action::Action,
app::Mode,
config::Config,
system::disk::{get_disks, Disk},
};
#[derive(Default)]
pub struct Popup {
command_tx: Option<UnboundedSender<Action>>,
config: Config,
disks: Vec<Disk>,
popup_text: String,
}
impl Popup {
pub fn new() -> Self {
Self {
popup_text: String::from("Scanning Disks..."),
..Default::default()
}
}
}
impl Component for Popup {
fn register_action_handler(&mut self, tx: UnboundedSender<Action>) -> Result<()> {
self.command_tx = Some(tx);
Ok(())
}
fn register_config_handler(&mut self, config: Config) -> Result<()> {
self.config = config;
Ok(())
}
fn update(&mut self, action: Action) -> Result<Option<Action>> {
match action {
Action::Tick => {
// add any logic here that should run on every tick
}
Action::Render => {
// 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::ScanDisks => {
if let Some(command_tx) = self.command_tx.clone() {
self.disks = get_disks();
command_tx.send(Action::NextScreen)?;
}
}
Action::SetMode(Mode::SelectDisks) => {
self.popup_text.clear();
if let Some(command_tx) = self.command_tx.clone() {
command_tx.send(Action::UpdateDiskList(self.disks.clone()))?;
}
}
_ => {}
}
Ok(None)
}
fn draw(&mut self, frame: &mut Frame, area: Rect) -> Result<()> {
if self.popup_text.is_empty() {
// Only show popup if popup_text is set
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))])
.block(popup_block)
.centered()
.wrap(Wrap { trim: false });
frame.render_widget(Clear, area);
frame.render_widget(popup, area);
Ok(())
}
}