abacus/abacus-ui/src/output_block.rs

118 lines
4.5 KiB
Rust

use abacus_core::Output;
use druid::{
widget::{Flex, Label, Padding, ViewSwitcher},
Color, FontDescriptor, FontFamily, FontWeight, Widget, WidgetExt,
};
use crate::data::Block;
const OUTPUT_FONT_SIZE: f64 = 16.0;
pub fn output_block() -> impl Widget<Block> {
ViewSwitcher::new(
|data: &Block, _env| data.clone(),
|selector: &Block, _data, _env| match &selector.output {
Output::Scalar(v) => Box::new(Padding::new(
25.0,
Label::new(v.to_string())
.with_font(
FontDescriptor::new(FontFamily::MONOSPACE).with_weight(FontWeight::BOLD),
)
.with_text_size(OUTPUT_FONT_SIZE)
.padding(10.0)
.background(Color::rgb8(30, 30, 30))
.rounded(4.0)
.expand_width(),
)),
Output::Error(e) => Box::new(Padding::new(
25.0,
Label::new(e.to_string())
.with_font(
FontDescriptor::new(FontFamily::MONOSPACE).with_weight(FontWeight::BOLD),
)
.with_text_alignment(druid::TextAlignment::Center)
.with_line_break_mode(druid::widget::LineBreaking::WordWrap)
.with_text_size(OUTPUT_FONT_SIZE)
.padding(10.0)
.background(Color::rgb8(30, 10, 10))
.expand_width(),
)),
Output::Series(series) => {
let mut flex = Flex::row();
for v in series.iter() {
flex.add_child(
Label::new(v.to_string())
.with_font(
FontDescriptor::new(FontFamily::MONOSPACE)
.with_weight(FontWeight::BOLD),
)
.with_text_size(OUTPUT_FONT_SIZE)
.padding(3.0)
.border(Color::rgb8(60, 60, 60), 1.0),
);
}
Box::new(Padding::new(
25.0,
flex.padding(10.0)
.background(Color::rgb8(30, 30, 30))
.rounded(4.0)
.expand_width(),
))
}
Output::DataFrame(frame) => {
let mut flex = Flex::row();
for series in frame.iter() {
let mut col = Flex::column();
col.add_child(
Label::new(series.name())
.with_font(
FontDescriptor::new(FontFamily::MONOSPACE)
.with_weight(FontWeight::BLACK),
)
.with_text_size(OUTPUT_FONT_SIZE)
.expand_width()
.padding(3.0)
.border(Color::rgb8(60, 60, 60), 1.0),
);
for v in series.iter() {
col.add_child(
Label::new(v.to_string())
.with_font(
FontDescriptor::new(FontFamily::MONOSPACE)
.with_weight(FontWeight::MEDIUM),
)
.with_text_size(OUTPUT_FONT_SIZE)
.expand_width()
.padding(3.0)
.border(Color::rgb8(60, 60, 60), 1.0),
);
}
flex.add_flex_child(col, 1.0);
}
Box::new(Padding::new(
25.0,
flex.padding(10.0)
.background(Color::rgb8(30, 30, 30))
.rounded(4.0)
.expand_width(),
))
}
_ => Box::new(Padding::new(
0.0,
Label::new("")
.with_font(
FontDescriptor::new(FontFamily::MONOSPACE).with_weight(FontWeight::BOLD),
)
.with_text_size(0.0)
.padding(0.0)
.background(Color::TRANSPARENT),
)),
},
)
}