use ratatui::buffer::Buffer;
use ratatui::layout::Rect;
use ratatui::prelude::{Span, Style};
use ratatui::widgets::{Widget, WidgetRef};
#[derive(Clone, Debug, PartialEq)]
pub struct GraphicBlock<'a> {
pub(crate) position: Position,
image: Span<'a>,
pub enabled: bool,
}
#[derive(Clone, Debug, PartialEq)]
pub struct Position {
pub x: u16,
pub y: u16,
}
impl<'a> GraphicBlock<'a> {
#[must_use]
pub fn new(position: Position, image: &'a str, style: Style) -> GraphicBlock<'a> {
GraphicBlock {
position,
image: Span::styled(image, style),
enabled: true,
}
}
pub fn set_position(&mut self, position: Position) {
self.position = position;
}
pub fn enable(&mut self) {
self.enabled = true;
}
pub fn disable(&mut self) {
self.enabled = false;
}
#[must_use]
pub fn get_position(&self) -> &Position {
&self.position
}
#[must_use]
pub fn get_content(&self) -> &str {
&self.image.content
}
}
impl WidgetRef for GraphicBlock<'_> {
fn render_ref(&self, area: Rect, buf: &mut Buffer) {
if self.enabled
&& area.y + self.position.y < area.height
&& area.x + self.position.x < area.width
{
buf.set_span(
area.x + self.position.x,
area.y + self.position.y,
&self.image,
area.width,
);
}
}
}
impl Widget for GraphicBlock<'_> {
fn render(self, area: Rect, buf: &mut Buffer) {
self.render_ref(area, buf);
}
}