1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
//! Canvas example -- general-purpose drawing surface.
//!
//! Demonstrates the Canvas component with various shape primitives
//! including lines, rectangles, circles, points, and labels.
//!
//! Run with: cargo run --example canvas --features display-components
use envision::prelude::*;
/// Application marker type.
struct CanvasApp;
/// Application state with a canvas.
#[derive(Clone)]
struct State {
canvas: CanvasState,
}
/// Application messages.
#[derive(Clone, Debug)]
enum Msg {
Quit,
}
impl App for CanvasApp {
type State = State;
type Message = Msg;
fn init() -> (State, Command<Msg>) {
let canvas = CanvasState::new()
.with_title("Canvas Drawing")
.with_bounds(0.0, 100.0, 0.0, 100.0)
.with_marker(CanvasMarker::Braille)
.with_shapes(vec![
// Diagonal line across the canvas
CanvasShape::Line {
x1: 0.0,
y1: 0.0,
x2: 100.0,
y2: 100.0,
color: Color::Red,
},
// Border rectangle
CanvasShape::Rectangle {
x: 5.0,
y: 5.0,
width: 90.0,
height: 90.0,
color: Color::Blue,
},
// Center circle
CanvasShape::Circle {
x: 50.0,
y: 50.0,
radius: 20.0,
color: Color::Green,
},
// Corner points
CanvasShape::Points {
coords: vec![
(10.0, 10.0),
(90.0, 10.0),
(10.0, 90.0),
(90.0, 90.0),
(50.0, 50.0),
],
color: Color::Yellow,
},
// Center label
CanvasShape::Label {
x: 50.0,
y: 50.0,
text: "Center".to_string(),
color: Color::Cyan,
},
// Small circle in upper-right
CanvasShape::Circle {
x: 80.0,
y: 80.0,
radius: 8.0,
color: Color::Magenta,
},
]);
(State { canvas }, Command::none())
}
fn update(_state: &mut State, msg: Msg) -> Command<Msg> {
match msg {
Msg::Quit => Command::quit(),
}
}
fn view(state: &State, frame: &mut Frame) {
let theme = Theme::default();
Canvas::view(
&state.canvas,
&mut RenderContext::new(frame, frame.area(), &theme),
);
}
fn handle_event(event: &Event) -> Option<Msg> {
if let Some(key) = event.as_key() {
match key.code {
Key::Char('q') | Key::Esc => Some(Msg::Quit),
_ => None,
}
} else {
None
}
}
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut vt = Runtime::<CanvasApp, _>::virtual_builder(70, 25).build()?;
println!("=== Canvas Example ===\n");
vt.tick()?;
println!("Canvas with various shapes (line, rectangle, circle, points, label):");
println!("{}\n", vt.display());
Ok(())
}