1use gtk::prelude::*;
2use relm4::{
3 Component, ComponentController, ComponentParts, ComponentSender, Controller, RelmApp,
4 SimpleComponent, gtk,
5};
6use relm4_components::web_image::{WebImage, WebImageMsg};
7
8const IMAGES: &[&str] = &[
9 "https://raw.githubusercontent.com/Relm4/Relm4/main/assets/Relm_logo_with_text.png",
10 "https://raw.githubusercontent.com/Relm4/Relm4/main/assets/Relm_logo.png",
11 "https://raw.githubusercontent.com/gtk-rs/gtk-rs.github.io/master/logo/gtk-rs.ico",
12 "https://avatars.githubusercontent.com/u/5430905",
13];
14
15#[derive(Debug)]
16enum AppMsg {
17 Next,
18 Unload,
19}
20
21struct App {
22 image: Controller<WebImage>,
23 idx: usize,
24}
25
26#[relm4::component]
27impl SimpleComponent for App {
28 type Init = ();
29 type Input = AppMsg;
30 type Output = ();
31
32 view! {
33 gtk::ApplicationWindow {
34 set_default_size: (300, 300),
35
36 #[wrap(Some)]
37 set_titlebar = >k::HeaderBar {
38 pack_start = >k::Button {
39 set_label: "Next image",
40 connect_clicked => AppMsg::Next,
41 },
42 pack_start = >k::Button {
43 set_label: "Unload image",
44 connect_clicked => AppMsg::Unload,
45 }
46 },
47
48 gtk::Box {
49 #[local_ref]
50 image -> gtk::Box {}
51 }
52 }
53 }
54
55 fn update(&mut self, msg: Self::Input, _: ComponentSender<Self>) {
56 match msg {
57 AppMsg::Next => {
58 self.idx = (self.idx + 1) % IMAGES.len();
59 self.image
60 .emit(WebImageMsg::LoadImage(IMAGES[self.idx].to_owned()));
61 }
62 AppMsg::Unload => self.image.emit(WebImageMsg::Unload),
63 }
64 }
65
66 fn init(
67 _: Self::Init,
68 root: Self::Root,
69 sender: ComponentSender<Self>,
70 ) -> ComponentParts<Self> {
71 let image = WebImage::builder().launch(IMAGES[0].to_owned()).detach();
72 let model = App { image, idx: 0 };
73
74 let image = model.image.widget();
75 let widgets = view_output!();
76
77 ComponentParts { model, widgets }
78 }
79}
80
81fn main() {
82 let app = RelmApp::new("relm4.example.open_button");
83 app.run::<App>(());
84}