use crate::core::geometry::Rect;
use crate::core::event::Event;
use crate::core::command::{CM_OK, CM_CANCEL};
use crate::core::palette::Attr;
use crate::terminal::Terminal;
use super::dialog::Dialog;
use super::color_selector::ColorSelector;
use super::button::Button;
use super::static_text::StaticText;
use super::View;
pub struct ColorDialog {
dialog: Dialog,
_fg_selector_idx: usize,
_bg_selector_idx: usize,
initial_attr: Attr,
selected_attr: Option<Attr>,
}
impl ColorDialog {
pub fn new(bounds: Rect, title: &str, initial_attr: Attr) -> Self {
let mut dialog = Dialog::new(bounds, title);
dialog.add(Box::new(StaticText::new(
Rect::new(2, 2, bounds.width() - 4, 3),
"Select foreground and background colors:"
)));
dialog.add(Box::new(StaticText::new(
Rect::new(2, 4, 20, 5),
"Foreground:"
)));
let fg_selector = ColorSelector::new(Rect::new(2, 5, 26, 8));
let fg_selector_idx = dialog.add(Box::new(fg_selector));
dialog.add(Box::new(StaticText::new(
Rect::new(2, 9, 20, 10),
"Background:"
)));
let bg_selector = ColorSelector::new(Rect::new(2, 10, 26, 13));
let bg_selector_idx = dialog.add(Box::new(bg_selector));
dialog.add(Box::new(StaticText::new(
Rect::new(28, 5, bounds.width() - 4, 6),
"Preview:"
)));
dialog.add(Box::new(StaticText::new(
Rect::new(28, 6, bounds.width() - 4, 8),
"Sample text with\nselected colors"
)));
dialog.add(Box::new(Button::new(
Rect::new(bounds.width() - 24, bounds.height() - 4, bounds.width() - 14, bounds.height() - 2),
"OK",
CM_OK,
true
)));
dialog.add(Box::new(Button::new(
Rect::new(bounds.width() - 12, bounds.height() - 4, bounds.width() - 2, bounds.height() - 2),
"Cancel",
CM_CANCEL,
false
)));
Self {
dialog,
_fg_selector_idx: fg_selector_idx,
_bg_selector_idx: bg_selector_idx,
initial_attr,
selected_attr: None,
}
}
pub fn execute(&mut self, app: &mut crate::app::Application) -> Option<Attr> {
let result = self.dialog.execute(app);
if result == CM_OK {
Some(self.initial_attr)
} else {
None
}
}
pub fn get_selected_attr(&self) -> Option<Attr> {
self.selected_attr
}
}
impl View for ColorDialog {
fn bounds(&self) -> Rect {
self.dialog.bounds()
}
fn set_bounds(&mut self, bounds: Rect) {
self.dialog.set_bounds(bounds);
}
fn draw(&mut self, terminal: &mut Terminal) {
self.dialog.draw(terminal);
}
fn handle_event(&mut self, event: &mut Event) {
self.dialog.handle_event(event);
}
fn can_focus(&self) -> bool {
true
}
fn state(&self) -> crate::core::state::StateFlags {
self.dialog.state()
}
fn set_state(&mut self, state: crate::core::state::StateFlags) {
self.dialog.set_state(state);
}
}