logo
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
use gtk::prelude::ButtonExt;
use relm4::factory::{DynamicIndex, FactoryPrototype, FactoryVecDeque};
use relm4::{gtk, send, WidgetPlus};

use super::OpenButtonMsg;

use std::path::PathBuf;

#[derive(Debug)]
pub(crate) struct FileListItem {
    pub(crate) path: PathBuf,
}

#[derive(Debug)]
pub(crate) struct FileListItemWidgets {
    label: gtk::Button,
    row: gtk::ListBoxRow,
}

impl FactoryPrototype for FileListItem {
    type Factory = FactoryVecDeque<Self>;
    type View = gtk::Box;
    type Widgets = FileListItemWidgets;
    type Root = gtk::ListBoxRow;
    type Msg = OpenButtonMsg;

    fn init_view(&self, key: &DynamicIndex, sender: relm4::Sender<Self::Msg>) -> Self::Widgets {
        let label = gtk::Button::with_label(
            self.path
                .iter()
                .last()
                .expect("Empty path")
                .to_str()
                .expect("Couldn't convert path to string"),
        );
        let row = gtk::ListBoxRow::builder().child(&label).build();

        label.inline_css(b"margin: 0;");

        let key = key.clone();
        label.connect_clicked(move |_| {
            send!(sender, OpenButtonMsg::OpenRecent(key.clone()));
        });
        FileListItemWidgets { label, row }
    }

    fn view(&self, _key: &DynamicIndex, widgets: &Self::Widgets) {
        widgets.label.set_label(
            self.path
                .iter()
                .last()
                .expect("Empty path")
                .to_str()
                .expect("Couldn't convert path to string"),
        );
    }

    fn position(&self, _key: &DynamicIndex) {}

    fn root_widget(widgets: &Self::Widgets) -> &Self::Root {
        &widgets.row
    }
}