dioxus/packages/tui/src/widget.rs

102 lines
2.5 KiB
Rust
Raw Normal View History

2022-02-17 22:06:28 +00:00
use tui::{
buffer::Buffer,
layout::Rect,
style::{Color, Modifier},
widgets::Widget,
};
use crate::{
style::{convert, RinkColor, RinkStyle},
Config,
};
pub struct RinkBuffer<'a> {
buf: &'a mut Buffer,
cfg: Config,
}
impl<'a> RinkBuffer<'a> {
fn new(buf: &'a mut Buffer, cfg: Config) -> RinkBuffer<'a> {
Self { buf, cfg }
}
2022-03-23 19:18:17 +00:00
pub fn set(&mut self, x: u16, y: u16, new: RinkCell) {
2022-03-18 15:43:43 +00:00
let area = self.buf.area();
if x < area.x || x >= area.width + area.x || y < area.y || y >= area.height + area.y {
// panic!("({x}, {y}) is not in {area:?}");
return;
2022-03-18 15:43:43 +00:00
}
2022-02-17 22:06:28 +00:00
let mut cell = self.buf.get_mut(x, y);
cell.bg = convert(self.cfg.rendering_mode, new.bg.blend(cell.bg));
2022-02-23 01:41:45 +00:00
if new.symbol.is_empty() {
if !cell.symbol.is_empty() {
2022-02-17 22:06:28 +00:00
// allows text to "shine through" transparent backgrounds
cell.fg = convert(self.cfg.rendering_mode, new.bg.blend(cell.fg));
}
} else {
cell.modifier = new.modifier;
2022-03-23 19:18:17 +00:00
cell.symbol = new.symbol;
2022-02-17 22:06:28 +00:00
cell.fg = convert(self.cfg.rendering_mode, new.fg.blend(cell.bg));
}
}
}
pub trait RinkWidget {
2022-05-04 18:35:30 +00:00
fn render(self, area: Rect, buf: RinkBuffer);
2022-02-17 22:06:28 +00:00
}
pub struct WidgetWithContext<T: RinkWidget> {
widget: T,
config: Config,
}
impl<T: RinkWidget> WidgetWithContext<T> {
pub fn new(widget: T, config: Config) -> WidgetWithContext<T> {
WidgetWithContext { widget, config }
}
}
impl<T: RinkWidget> Widget for WidgetWithContext<T> {
fn render(self, area: Rect, buf: &mut Buffer) {
2022-05-04 18:35:30 +00:00
self.widget.render(area, RinkBuffer::new(buf, self.config));
2022-02-17 22:06:28 +00:00
}
}
#[derive(Clone)]
pub struct RinkCell {
pub symbol: String,
pub bg: RinkColor,
pub fg: RinkColor,
pub modifier: Modifier,
}
impl Default for RinkCell {
fn default() -> Self {
Self {
symbol: "".to_string(),
fg: RinkColor {
color: Color::Rgb(0, 0, 0),
2022-03-18 15:43:43 +00:00
alpha: 0,
2022-02-17 22:06:28 +00:00
},
bg: RinkColor {
color: Color::Rgb(0, 0, 0),
2022-03-18 15:43:43 +00:00
alpha: 0,
2022-02-17 22:06:28 +00:00
},
modifier: Modifier::empty(),
}
}
}
impl RinkCell {
pub fn set_style(&mut self, style: RinkStyle) {
if let Some(c) = style.fg {
self.fg = c;
}
if let Some(c) = style.bg {
self.bg = c;
}
self.modifier = style.add_modifier;
self.modifier.remove(style.sub_modifier);
}
}