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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
//! Reusable and easily configurable component for loading images from the web.

use std::collections::VecDeque;
use std::fmt::Debug;

use relm4::gtk::prelude::Cast;
use relm4::gtk::traits::{BoxExt, WidgetExt};
use relm4::{gtk, ComponentSender};
use relm4::{Component, ComponentParts};

#[derive(Debug, Clone, PartialEq, Eq)]
/// Reusable component for loading images from the web.
pub struct WebImage {
    current_id: usize,
    current_widget: gtk::Widget,
}

#[derive(Debug, Clone, PartialEq, Eq)]
/// Load or unload a web image.
pub enum WebImageMsg {
    /// Load an image from an url.
    LoadImage(String),
    /// Unload the current image.
    Unload,
}

impl Component for WebImage {
    type CommandOutput = Option<(usize, VecDeque<u8>)>;
    type Input = WebImageMsg;
    type Output = ();
    type Init = String;
    type Root = gtk::Box;
    type Widgets = ();

    fn init_root() -> Self::Root {
        gtk::Box::default()
    }

    fn init(
        url: Self::Init,
        root: &Self::Root,
        sender: ComponentSender<Self>,
    ) -> ComponentParts<Self> {
        let widget = gtk::Box::default();
        root.append(&widget);
        let current_widget = Self::set_spinner(root, widget.upcast_ref());

        let model = Self {
            current_id: 0,
            current_widget,
        };

        model.load_image(&sender, url);

        ComponentParts { model, widgets: () }
    }

    fn update(&mut self, input: Self::Input, sender: ComponentSender<Self>, root: &Self::Root) {
        self.current_widget = Self::set_spinner(root, &self.current_widget);
        self.current_id = self.current_id.wrapping_add(1);

        match input {
            WebImageMsg::LoadImage(url) => {
                self.load_image(&sender, url);
            }
            WebImageMsg::Unload => (),
        }
    }

    fn update_cmd(
        &mut self,
        message: Self::CommandOutput,
        sender: ComponentSender<Self>,
        root: &Self::Root,
    ) {
        if let Some((id, data)) = message {
            if id == self.current_id {
                if let Some(img) = Self::generate_image(data) {
                    self.current_widget = Self::set_image(root, &self.current_widget, &img);
                    sender.output(()).ok();
                }
            }
        }
    }
}

impl WebImage {
    #[must_use]
    fn set_spinner(root: &<Self as Component>::Root, widget: &gtk::Widget) -> gtk::Widget {
        root.remove(widget);
        relm4::view! {
            #[local_ref]
            root -> gtk::Box {
                set_halign: gtk::Align::Center,
                set_valign: gtk::Align::Center,

                #[name(spinner)]
                gtk::Spinner {
                    start: (),
                    set_hexpand: true,
                    set_vexpand: true,
                }
            }
        }
        spinner.upcast()
    }

    #[must_use]
    fn set_image(
        root: &<Self as Component>::Root,
        widget: &gtk::Widget,
        img: &gtk::Image,
    ) -> gtk::Widget {
        root.remove(widget);
        relm4::view! {
            #[local_ref]
            root -> gtk::Box {
                set_halign: gtk::Align::Fill,
                set_valign: gtk::Align::Fill,

                #[local_ref]
                img -> gtk::Image {
                    set_hexpand: true,
                    set_vexpand: true,
                }
            }
        }
        img.clone().upcast()
    }

    fn load_image(&self, sender: &ComponentSender<Self>, url: String) {
        sender.oneshot_command(Self::get_img_data(self.current_id, url));
    }

    fn generate_image(data: VecDeque<u8>) -> Option<gtk::Image> {
        let pixbuf = gtk::gdk_pixbuf::Pixbuf::from_read(data).ok()?;
        Some(gtk::Image::from_pixbuf(Some(&pixbuf)))
    }

    async fn get_img_data(id: usize, url: String) -> Option<(usize, VecDeque<u8>)> {
        let response = reqwest::get(url).await.ok()?;
        let bytes = response.bytes().await.ok()?;
        Some((id, bytes.into_iter().collect()))
    }
}