use tuirealm::command::{Cmd, CmdResult};
use tuirealm::component::Component;
use tuirealm::props::{
AttrValue, Attribute, Borders, Color, PropPayload, PropValue, Props, QueryResult, Shape, Style,
TextModifiers, Title,
};
use tuirealm::ratatui::Frame;
use tuirealm::ratatui::layout::Rect;
use tuirealm::ratatui::symbols::Marker;
use tuirealm::ratatui::text::{Line as Spans, Span};
use tuirealm::ratatui::widgets::canvas::{Canvas as TuiCanvas, Context, Points};
use tuirealm::state::State;
use super::props::{CANVAS_X_BOUNDS, CANVAS_Y_BOUNDS};
use crate::prop_ext::CommonProps;
#[derive(Default)]
#[must_use]
pub struct Canvas {
common: CommonProps,
props: Props,
}
impl Canvas {
pub fn foreground(mut self, fg: Color) -> Self {
self.attr(Attribute::Foreground, AttrValue::Color(fg));
self
}
pub fn background(mut self, bg: Color) -> Self {
self.attr(Attribute::Background, AttrValue::Color(bg));
self
}
pub fn modifiers(mut self, m: TextModifiers) -> Self {
self.attr(Attribute::TextProps, AttrValue::TextModifiers(m));
self
}
pub fn style(mut self, style: Style) -> Self {
self.attr(Attribute::Style, AttrValue::Style(style));
self
}
pub fn inactive(mut self, s: Style) -> Self {
self.attr(Attribute::UnfocusedBorderStyle, AttrValue::Style(s));
self
}
pub fn borders(mut self, b: Borders) -> Self {
self.attr(Attribute::Borders, AttrValue::Borders(b));
self
}
pub fn title<T: Into<Title>>(mut self, title: T) -> Self {
self.attr(Attribute::Title, AttrValue::Title(title.into()));
self
}
pub fn data(mut self, data: impl IntoIterator<Item = Shape>) -> Self {
self.attr(
Attribute::Shape,
AttrValue::Payload(PropPayload::Vec(
data.into_iter().map(PropValue::Shape).collect(),
)),
);
self
}
pub fn x_bounds(mut self, bounds: (f64, f64)) -> Self {
self.attr(
Attribute::Custom(CANVAS_X_BOUNDS),
AttrValue::Payload(PropPayload::Pair((
PropValue::F64(bounds.0),
PropValue::F64(bounds.1),
))),
);
self
}
pub fn y_bounds(mut self, bounds: (f64, f64)) -> Self {
self.attr(
Attribute::Custom(CANVAS_Y_BOUNDS),
AttrValue::Payload(PropPayload::Pair((
PropValue::F64(bounds.0),
PropValue::F64(bounds.1),
))),
);
self
}
pub fn marker(mut self, marker: Marker) -> Self {
self.attr(Attribute::Marker, AttrValue::Marker(marker));
self
}
fn draw_shape(ctx: &mut Context, shape: &Shape) {
match shape {
Shape::Label((x, y, label, color)) => {
let span = Span::styled(label.to_string(), Style::default().fg(*color));
ctx.print(*x, *y, Spans::from(vec![span]));
}
Shape::Layer => ctx.layer(),
Shape::Line(line) => ctx.draw(line),
Shape::Map(map) => ctx.draw(map),
Shape::Points((coords, color)) => ctx.draw(&Points {
coords,
color: *color,
}),
Shape::Rectangle(rectangle) => ctx.draw(rectangle),
}
}
}
impl Component for Canvas {
fn view(&mut self, render: &mut Frame, area: Rect) {
if !self.common.display {
return;
}
let x_bounds: [f64; 2] = self
.props
.get(Attribute::Custom(CANVAS_X_BOUNDS))
.and_then(AttrValue::as_payload)
.and_then(PropPayload::as_pair)
.and_then(|(a, b)| Some([a.as_f64()?, b.as_f64()?]))
.unwrap_or_default();
let y_bounds: [f64; 2] = self
.props
.get(Attribute::Custom(CANVAS_Y_BOUNDS))
.and_then(AttrValue::as_payload)
.and_then(PropPayload::as_pair)
.and_then(|(a, b)| Some([a.as_f64()?, b.as_f64()?]))
.unwrap_or_default();
let shapes: Vec<Shape> = self
.props
.get(Attribute::Shape)
.and_then(AttrValue::as_payload)
.and_then(PropPayload::as_vec)
.map(|v| v.iter().filter_map(PropValue::as_shape).cloned().collect())
.unwrap_or_default();
let marker = self
.props
.get(Attribute::Marker)
.and_then(AttrValue::as_marker)
.unwrap_or(Marker::Braille);
let mut widget = TuiCanvas::default()
.marker(marker)
.x_bounds(x_bounds)
.y_bounds(y_bounds)
.paint(|ctx| shapes.iter().for_each(|x| Self::draw_shape(ctx, x)));
if let Some(block) = self.common.get_block() {
widget = widget.block(block);
}
if let Some(color) = self.common.style.bg {
widget = widget.background_color(color);
}
render.render_widget(widget, area);
}
fn query<'a>(&'a self, attr: Attribute) -> Option<QueryResult<'a>> {
if let Some(value) = self.common.get_for_query(attr) {
return Some(value);
}
self.props.get_for_query(attr)
}
fn attr(&mut self, attr: Attribute, value: AttrValue) {
if let Some(value) = self.common.set(attr, value) {
self.props.set(attr, value);
}
}
fn state(&self) -> State {
State::None
}
fn perform(&mut self, cmd: Cmd) -> CmdResult {
CmdResult::Invalid(cmd)
}
}
#[cfg(test)]
mod test {
use pretty_assertions::assert_eq;
use tuirealm::props::HorizontalAlignment;
use tuirealm::ratatui::widgets::canvas::{Line, Map, MapResolution, Rectangle};
use super::*;
#[test]
fn test_component_canvas_with_shapes() {
let component: Canvas = Canvas::default()
.background(Color::Black)
.title(Title::from("playing risiko").alignment(HorizontalAlignment::Center))
.borders(Borders::default())
.marker(Marker::Dot)
.x_bounds((-180.0, 180.0))
.y_bounds((-90.0, 90.0))
.data([
Shape::Map(Map {
resolution: MapResolution::High,
color: Color::Rgb(240, 240, 240),
}),
Shape::Layer,
Shape::Line(Line {
x1: 0.0,
y1: 10.0,
x2: 10.0,
y2: 10.0,
color: Color::Red,
}),
Shape::Rectangle(Rectangle {
x: 60.0,
y: 20.0,
width: 70.0,
height: 22.0,
color: Color::Cyan,
}),
Shape::Points((
vec![
(21.0, 13.0),
(66.0, 77.0),
(34.0, 69.0),
(45.0, 76.0),
(120.0, 55.0),
(-32.0, -50.0),
(-4.0, 2.0),
(-32.0, -48.0),
],
Color::Green,
)),
]);
assert_eq!(component.state(), State::None);
}
}