moly_kit/widgets/
citation_list.rs

1use makepad_widgets::*;
2
3use super::citation::CitationWidgetRefExt;
4
5script_mod! {
6    use mod.prelude.widgets.*
7    use mod.widgets.*
8
9    mod.widgets.CitationListBase = #(CitationList::register_widget(vm))
10    mod.widgets.CitationList = set_type_default() do mod.widgets.CitationListBase {
11        width: Fill
12        height: Fit
13        list := PortalList {
14            flow: Right
15            width: Fill
16            // Fit doesn't work here.
17            height: 50
18            Citation := Citation {
19                // spacing on parent doesn't work
20                margin: Inset { right: 8 }
21            }
22        }
23    }
24}
25
26#[derive(Script, ScriptHook, Widget)]
27pub struct CitationList {
28    #[source]
29    source: ScriptObjectRef,
30
31    #[deref]
32    deref: View,
33
34    #[rust]
35    pub urls: Vec<String>,
36}
37
38impl Widget for CitationList {
39    fn draw_walk(&mut self, cx: &mut Cx2d, scope: &mut Scope, walk: Walk) -> DrawStep {
40        let list_uid = self.portal_list(cx, ids!(list)).widget_uid();
41        while let Some(widget) = self.deref.draw_walk(cx, scope, walk).step() {
42            if widget.widget_uid() == list_uid {
43                self.draw_list(cx, &mut *widget.as_portal_list().borrow_mut().unwrap());
44            }
45        }
46
47        DrawStep::done()
48    }
49
50    fn handle_event(&mut self, cx: &mut Cx, event: &Event, scope: &mut Scope) {
51        self.deref.handle_event(cx, event, scope)
52    }
53}
54
55impl CitationList {
56    fn draw_list(&mut self, cx: &mut Cx2d, list: &mut PortalList) {
57        list.set_item_range(cx, 0, self.urls.len());
58        while let Some(index) = list.next_visible_item(cx) {
59            if index >= self.urls.len() {
60                continue;
61            }
62
63            let item = list.item(cx, index, id!(Citation));
64            item.as_citation()
65                .borrow_mut()
66                .unwrap()
67                .set_url_once(cx, self.urls[index].clone());
68            item.draw_all(cx, &mut Scope::empty());
69        }
70    }
71}