duat_base/widgets/
info.rs1use std::sync::Once;
7
8use duat_core::{
9 context::Handle,
10 data::Pass,
11 hook::{self, OnMouseEvent, WidgetOpened},
12 mode::MouseEventKind,
13 opts::PrintOpts,
14 text::{Text, TextMut},
15 ui::Widget,
16};
17
18pub fn add_info_hooks() {
20 use MouseEventKind::{ScrollDown, ScrollUp};
21
22 hook::add::<OnMouseEvent<Info>>(|pa, event| match event.kind {
23 ScrollDown | ScrollUp => {
24 let (info, area) = event.handle.write_with_area(pa);
25 let scroll = if let ScrollDown = event.kind { 3 } else { -3 };
26 area.scroll_ver(&info.text, scroll, info.print_opts());
27 }
28 _ => {}
29 });
30
31 hook::add::<WidgetOpened<Info>>(|pa, info| {
32 let (info, area) = info.write_with_area(pa);
33 let size = area.size_of_text(info.print_opts(), &info.text).unwrap();
34 _ = area.set_width(size.x);
35 _ = area.set_height(size.y);
36 });
37}
38
39pub struct Info {
50 text: Text,
52}
53
54impl Info {
55 pub fn new(text: Text) -> Self {
63 static ONCE: Once = Once::new();
64 ONCE.call_once(|| {});
65
66 Self { text }
67 }
68
69 pub fn set_text(pa: &mut Pass, info: &Handle<Self>, func: impl FnOnce(&mut Text)) {
74 let (info, area) = info.write_with_area(pa);
75 func(&mut info.text);
76
77 let size = area.size_of_text(info.print_opts(), &info.text).unwrap();
78 _ = area.set_width(size.x);
79 _ = area.set_height(size.y);
80 }
81}
82
83impl Widget for Info {
84 fn text(&self) -> &Text {
85 &self.text
86 }
87
88 fn text_mut(&mut self) -> TextMut<'_> {
89 self.text.as_mut()
90 }
91
92 fn print_opts(&self) -> PrintOpts {
93 let mut opts = PrintOpts::new();
94 opts.wrap_lines = true;
95 opts.tabstop = 2;
96 opts
97 }
98}