fractal/session/view/content/explore/
servers_popover.rs

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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
use adw::subclass::prelude::*;
use gtk::{glib, glib::clone, prelude::*, CompositeTemplate};
use ruma::ServerName;

use super::{ExploreServerRow, Server, ServerList};
use crate::session::model::Session;

mod imp {
    use std::cell::RefCell;

    use glib::subclass::InitializingObject;

    use super::*;

    #[derive(Debug, Default, CompositeTemplate, glib::Properties)]
    #[template(resource = "/org/gnome/Fractal/ui/session/view/content/explore/servers_popover.ui")]
    #[properties(wrapper_type = super::ExploreServersPopover)]
    pub struct ExploreServersPopover {
        /// The current session.
        #[property(get, set = Self::set_session, explicit_notify)]
        pub session: glib::WeakRef<Session>,
        /// The server list.
        #[property(get)]
        pub server_list: RefCell<Option<ServerList>>,
        #[template_child]
        pub listbox: TemplateChild<gtk::ListBox>,
        #[template_child]
        pub server_entry: TemplateChild<gtk::Entry>,
    }

    #[glib::object_subclass]
    impl ObjectSubclass for ExploreServersPopover {
        const NAME: &'static str = "ContentExploreServersPopover";
        type Type = super::ExploreServersPopover;
        type ParentType = gtk::Popover;

        fn class_init(klass: &mut Self::Class) {
            Self::bind_template(klass);

            klass.install_action("explore-servers-popover.add-server", None, |obj, _, _| {
                obj.add_server();
            });
            klass.install_action(
                "explore-servers-popover.remove-server",
                Some(&String::static_variant_type()),
                |obj, _, variant| {
                    if let Some(variant) = variant.and_then(String::from_variant) {
                        obj.remove_server(&variant);
                    }
                },
            );
        }

        fn instance_init(obj: &InitializingObject<Self>) {
            obj.init_template();
        }
    }

    #[glib::derived_properties]
    impl ObjectImpl for ExploreServersPopover {
        fn constructed(&self) {
            self.parent_constructed();
            let obj = self.obj();

            self.server_entry.connect_changed(clone!(
                #[weak]
                obj,
                move |_| obj.update_add_server_state()
            ));
            self.server_entry.connect_activate(clone!(
                #[weak]
                obj,
                move |_| obj.add_server()
            ));

            obj.update_add_server_state();
        }
    }

    impl WidgetImpl for ExploreServersPopover {}
    impl PopoverImpl for ExploreServersPopover {}

    impl ExploreServersPopover {
        /// Set the current session.
        fn set_session(&self, session: Option<&Session>) {
            if session == self.session.upgrade().as_ref() {
                return;
            }

            self.session.set(session);
            self.obj().notify_session();
        }
    }
}

glib::wrapper! {
    /// A popover that lists the servers that can be explored.
    pub struct ExploreServersPopover(ObjectSubclass<imp::ExploreServersPopover>)
        @extends gtk::Widget, gtk::Popover, @implements gtk::Accessible;
}

impl ExploreServersPopover {
    pub fn new(session: &Session) -> Self {
        glib::Object::builder().property("session", session).build()
    }

    /// Initialize the list of servers.
    pub fn init(&self) {
        let Some(session) = &self.session() else {
            return;
        };

        let imp = self.imp();
        let server_list = ServerList::new(session);

        imp.listbox.bind_model(Some(&server_list), |obj| {
            ExploreServerRow::new(obj.downcast_ref::<Server>().unwrap()).upcast()
        });

        // Select the first server by default.
        imp.listbox.select_row(imp.listbox.row_at_index(0).as_ref());

        imp.server_list.replace(Some(server_list));
        self.notify_server_list();
    }

    /// The server that is currently selected, if any.
    pub fn selected_server(&self) -> Option<Server> {
        self.imp()
            .listbox
            .selected_row()
            .and_downcast::<ExploreServerRow>()
            .and_then(|row| row.server())
    }

    pub fn connect_selected_server_changed<F: Fn(&Self, Option<Server>) + 'static>(
        &self,
        f: F,
    ) -> glib::SignalHandlerId {
        self.imp().listbox.connect_row_selected(clone!(
            #[weak(rename_to = obj)]
            self,
            move |_, row| {
                f(
                    &obj,
                    row.and_then(|row| row.downcast_ref::<ExploreServerRow>())
                        .and_then(ExploreServerRow::server),
                );
            }
        ))
    }

    /// Whether the server currently in the text entry can be added.
    fn can_add_server(&self) -> bool {
        let server = self.imp().server_entry.text();
        ServerName::parse(server.as_str()).is_ok()
            // Don't allow duplicates
            && self
                .server_list()
                .filter(|l| !l.contains_matrix_server(&server))
                .is_some()
    }

    /// Update the state of the action to add a server according to the current
    /// state.
    fn update_add_server_state(&self) {
        self.action_set_enabled("explore-servers-popover.add-server", self.can_add_server());
    }

    /// Add the server currently in the text entry.
    fn add_server(&self) {
        if !self.can_add_server() {
            return;
        }
        let Some(server_list) = self.server_list() else {
            return;
        };

        let imp = self.imp();

        let server = imp.server_entry.text();
        imp.server_entry.set_text("");

        server_list.add_custom_matrix_server(server.into());

        let index = i32::try_from(server_list.n_items()).unwrap_or(i32::MAX);
        let row = imp.listbox.row_at_index(index - 1);
        imp.listbox.select_row(row.as_ref());
    }

    /// Remove the given server.
    fn remove_server(&self, server: &str) {
        let Some(server_list) = self.server_list() else {
            return;
        };

        let imp = self.imp();

        // If the selected server is gonna be removed, select the first one.
        if self.selected_server().and_then(|s| s.server()).as_deref() == Some(server) {
            imp.listbox.select_row(imp.listbox.row_at_index(0).as_ref());
        }

        server_list.remove_custom_matrix_server(server);
    }
}