use crate::component::Component;
use crate::components::slotted_bar::SlotContent;
use crate::context::RenderContext;
use crate::event::EventHandler;
use crate::layout::Rect;
use crate::render::Renderer;
use anyhow::Result;
pub struct Logo {
text: String,
dirty: bool,
}
impl Logo {
pub fn new(text: impl Into<String>) -> Self {
Logo {
text: text.into(),
dirty: true,
}
}
}
impl EventHandler for Logo {}
impl Component for Logo {
fn render(
&mut self,
renderer: &mut Renderer,
bounds: Rect,
_ctx: &RenderContext,
) -> Result<()> {
if self.text.is_empty() || bounds.width == 0 {
return Ok(());
}
let padded = format!(" {} ", self.text);
let content_len = padded.len() as u16;
let x = bounds
.x
.saturating_add(bounds.width.saturating_sub(content_len));
renderer.move_cursor(x, bounds.y)?;
renderer.write_styled(&padded, "\x1b[47;30m")?;
renderer.write_text("\x1b[0m")?;
self.dirty = false;
Ok(())
}
fn min_size(&self) -> (u16, u16) {
((self.text.len() + 2) as u16, 1)
}
fn mark_dirty(&mut self) {
self.dirty = true;
}
fn is_dirty(&self) -> bool {
self.dirty
}
fn name(&self) -> &str {
"Logo"
}
}
impl SlotContent for Logo {
fn responsive_sizes(&self) -> Vec<crate::components::slotted_bar::SlotSize> {
use crate::components::slotted_bar::SlotSize;
vec![SlotSize::Blocks((self.text.len() + 2) as u16)]
}
fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
self
}
}