-
Notifications
You must be signed in to change notification settings - Fork 0
/
text_widget.rs
102 lines (81 loc) · 2.4 KB
/
text_widget.rs
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
use streaming_iterator::StreamingIterator;
use unicode_width::UnicodeWidthStr;
use crate::config::theme::Theme;
use crate::experiments::screenspace::Screenspace;
use crate::io::input_event::InputEvent;
use crate::io::output::Output;
use crate::primitives::printable::Printable;
use crate::primitives::xy::XY;
use crate::widget::any_msg::AnyMsg;
use crate::widget::fill_policy::SizePolicy;
use crate::widget::widget::{get_new_widget_id, Widget, WID};
pub struct TextWidget {
wid: WID,
text: Box<dyn Printable>,
size_policy: SizePolicy,
}
impl TextWidget {
pub const TYPENAME: &'static str = "text_widget";
pub fn new(text: Box<dyn Printable>) -> Self {
Self {
wid: get_new_widget_id(),
text,
size_policy: SizePolicy::SELF_DETERMINED,
}
}
pub fn with_size_policy(self, size_policy: SizePolicy) -> Self {
Self { size_policy, ..self }
}
pub fn text_size(&self) -> XY {
let mut size = XY::ZERO;
let _debug_text = self.text.to_owned_string();
let mut line_it = self.text.lines();
while let Some(line) = line_it.next() {
size.x = size.x.max(line.width() as u16);
size.y += 1;
}
size
}
pub fn get_text(&self) -> String {
self.text.to_owned_string()
}
pub fn set_text(&mut self, text: Box<dyn Printable>) {
self.text = text;
}
}
impl Widget for TextWidget {
fn id(&self) -> WID {
self.wid
}
fn typename(&self) -> &'static str {
Self::TYPENAME
}
fn static_typename() -> &'static str
where
Self: Sized,
{
Self::TYPENAME
}
fn size_policy(&self) -> SizePolicy {
self.size_policy
}
fn full_size(&self) -> XY {
self.text_size()
}
fn layout(&mut self, _screenspace: Screenspace) {}
fn on_input(&self, _input_event: InputEvent) -> Option<Box<dyn AnyMsg>> {
None
}
fn update(&mut self, _msg: Box<dyn AnyMsg>) -> Option<Box<dyn AnyMsg>> {
None
}
fn render(&self, theme: &Theme, focused: bool, output: &mut dyn Output) {
let text = theme.default_text(focused);
let mut line_idx = 0;
let mut line_it = self.text.lines();
while let Some(line) = line_it.next() {
output.print_at(XY::new(0, line_idx), text, line); //TODO leaks etc
line_idx += 1;
}
}
}