use super::view::{write_line_to_terminal, View};
use crate::core::draw::DrawBuffer;
use crate::core::event::Event;
use crate::core::geometry::Rect;
use crate::core::palette::{LABEL_NORMAL, LABEL_SHORTCUT};
use crate::terminal::Terminal;
pub struct Label {
bounds: Rect,
text: String,
link: Option<usize>, owner: Option<*const dyn View>,
owner_type: super::view::OwnerType,
}
impl Label {
pub fn new(bounds: Rect, text: &str) -> Self {
Self {
bounds,
text: text.to_string(),
link: None,
owner: None,
owner_type: super::view::OwnerType::Dialog, }
}
pub fn set_link(&mut self, link_index: usize) {
self.link = Some(link_index);
}
}
impl View for Label {
fn bounds(&self) -> Rect {
self.bounds
}
fn set_bounds(&mut self, bounds: Rect) {
self.bounds = bounds;
}
fn draw(&mut self, terminal: &mut Terminal) {
let width = self.bounds.width() as usize;
let mut buf = DrawBuffer::new(width);
let normal_attr = self.map_color(LABEL_NORMAL);
let shortcut_attr = self.map_color(LABEL_SHORTCUT);
buf.move_char(0, ' ', normal_attr, width);
buf.move_str_with_shortcut(0, &self.text, normal_attr, shortcut_attr);
write_line_to_terminal(terminal, self.bounds.a.x, self.bounds.a.y, &buf);
}
fn handle_event(&mut self, _event: &mut Event) {
}
fn label_link(&self) -> Option<usize> {
self.link
}
fn set_owner(&mut self, owner: *const dyn View) {
self.owner = Some(owner);
}
fn get_owner(&self) -> Option<*const dyn View> {
self.owner
}
fn get_owner_type(&self) -> super::view::OwnerType {
self.owner_type
}
fn set_owner_type(&mut self, owner_type: super::view::OwnerType) {
self.owner_type = owner_type;
}
fn get_palette(&self) -> Option<crate::core::palette::Palette> {
use crate::core::palette::{palettes, Palette};
Some(Palette::from_slice(palettes::CP_LABEL))
}
}