use crate::render::Cell;
use crate::style::Color;
use crate::widget::traits::{RenderContext, View, WidgetProps};
use crate::{impl_props_builders, impl_styled_view};
pub struct ZenMode {
content: Box<dyn View>,
enabled: bool,
padding_x: u16,
padding_y: u16,
bg_color: Color,
dim_opacity: f32,
center_vertical: bool,
props: WidgetProps,
}
impl ZenMode {
pub fn new(content: impl View + 'static) -> Self {
Self {
content: Box::new(content),
enabled: false,
padding_x: 4,
padding_y: 2,
bg_color: Color::rgb(15, 15, 25),
dim_opacity: 0.0,
center_vertical: false,
props: WidgetProps::new(),
}
}
pub fn padding(mut self, padding: u16) -> Self {
self.padding_x = padding;
self.padding_y = padding;
self
}
pub fn padding_x(mut self, padding: u16) -> Self {
self.padding_x = padding;
self
}
pub fn padding_y(mut self, padding: u16) -> Self {
self.padding_y = padding;
self
}
pub fn bg(mut self, color: Color) -> Self {
self.bg_color = color;
self
}
pub fn dim(mut self, opacity: f32) -> Self {
self.dim_opacity = opacity.clamp(0.0, 1.0);
self
}
pub fn center(mut self) -> Self {
self.center_vertical = true;
self
}
pub fn enable(&mut self) {
self.enabled = true;
}
pub fn disable(&mut self) {
self.enabled = false;
}
pub fn toggle(&mut self) {
self.enabled = !self.enabled;
}
pub fn is_enabled(&self) -> bool {
self.enabled
}
pub fn set_enabled(&mut self, enabled: bool) {
self.enabled = enabled;
}
pub fn content(&self) -> &dyn View {
self.content.as_ref()
}
pub fn content_mut(&mut self) -> &mut dyn View {
self.content.as_mut()
}
#[doc(hidden)]
pub fn get_padding_x(&self) -> u16 {
self.padding_x
}
#[doc(hidden)]
pub fn get_padding_y(&self) -> u16 {
self.padding_y
}
#[doc(hidden)]
pub fn get_bg_color(&self) -> Color {
self.bg_color
}
#[doc(hidden)]
pub fn get_dim_opacity(&self) -> f32 {
self.dim_opacity
}
#[doc(hidden)]
pub fn get_center_vertical(&self) -> bool {
self.center_vertical
}
}
impl Default for ZenMode {
fn default() -> Self {
Self::new(super::text(""))
}
}
impl View for ZenMode {
fn render(&self, ctx: &mut RenderContext) {
let area = ctx.area;
if self.enabled {
for y in 0..area.height {
for x in 0..area.width {
let mut cell = Cell::new(' ');
cell.bg = Some(self.bg_color);
ctx.set(x, y, cell);
}
}
let content_width = area.width.saturating_sub(self.padding_x * 2);
let content_height = area.height.saturating_sub(self.padding_y * 2);
if content_width > 0 && content_height > 0 {
let content_area = ctx.sub_area(
self.padding_x,
self.padding_y,
content_width,
content_height,
);
let mut sub_ctx = RenderContext::new(ctx.buffer, content_area);
self.content.render(&mut sub_ctx);
}
} else {
self.content.render(ctx);
}
}
crate::impl_view_meta!("ZenMode");
}
impl_styled_view!(ZenMode);
impl_props_builders!(ZenMode);
pub fn zen(content: impl View + 'static) -> ZenMode {
ZenMode::new(content)
}
pub fn zen_dark(content: impl View + 'static) -> ZenMode {
ZenMode::new(content).bg(Color::rgb(15, 15, 25)).padding(4)
}
pub fn zen_light(content: impl View + 'static) -> ZenMode {
ZenMode::new(content)
.bg(Color::rgb(250, 250, 250))
.padding(4)
}