azalea_shell/factory/bluetooth/
device.rs

1use gtk::prelude::{BoxExt, ButtonExt, WidgetExt};
2use relm4::{FactorySender, prelude::FactoryComponent};
3
4use crate::{icon, service::dbus::bluez::Device};
5
6#[derive(Debug)]
7pub struct Model {
8    pub device: Device,
9}
10
11#[derive(Debug)]
12pub enum Input {
13    Connect,
14}
15
16#[derive(Debug)]
17pub enum Output {
18    Connect(Device, bool),
19}
20
21#[relm4::factory(pub)]
22impl FactoryComponent for Model {
23    type Index = String;
24    type Init = Device;
25    type Input = Input;
26    type Output = Output;
27    type CommandOutput = ();
28    type ParentWidget = gtk::Box;
29
30    view! {
31        #[root]
32        gtk::Box {
33            set_spacing: 12,
34
35            gtk::Image {
36                set_icon_name: Some(self.device.icon.as_deref().unwrap_or(icon::BLUETOOTH)),
37            },
38
39            gtk::Label {
40                set_halign: gtk::Align::Start,
41                set_hexpand: true,
42                set_label: self.device.name.as_ref().unwrap_or(&String::from("unknown")),
43            },
44
45            gtk::Button {
46                set_halign: gtk::Align::End,
47
48                #[watch]
49                set_icon_name: if self.device.is_connected {
50                    icon::PLUG_CONNECTED
51                } else {
52                    icon::PLUG_DISCONNECTED
53                },
54
55                #[watch]
56                set_css_classes: if self.device.is_connected {
57                    &[ "azalea-primary-container" ]
58                } else {
59                    &[]
60                },
61
62                connect_clicked => Input::Connect
63            }
64        }
65    }
66
67    fn init_model(device: Self::Init, _index: &String, _sender: FactorySender<Self>) -> Self {
68        Self { device }
69    }
70
71    fn update(&mut self, message: Self::Input, sender: FactorySender<Self>) {
72        match message {
73            Input::Connect => drop(sender.output(Output::Connect(
74                self.device.clone(),
75                !self.device.is_connected,
76            ))),
77        }
78    }
79}