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
mod public_room;
mod public_room_list;
mod public_room_row;
mod server;
mod server_list;
mod server_row;
mod servers_popover;

use adw::subclass::prelude::*;
use gtk::{glib, glib::clone, prelude::*, CompositeTemplate};

pub use self::{
    public_room::PublicRoom, public_room_list::PublicRoomList, public_room_row::PublicRoomRow,
    servers_popover::ExploreServersPopover,
};
use self::{server::Server, server_list::ServerList, server_row::ExploreServerRow};
use crate::{components::Spinner, 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/mod.ui")]
    #[properties(wrapper_type = super::Explore)]
    pub struct Explore {
        /// The current session.
        #[property(get, set = Self::set_session, explicit_notify)]
        pub session: glib::WeakRef<Session>,
        #[template_child]
        pub stack: TemplateChild<gtk::Stack>,
        #[template_child]
        pub search_entry: TemplateChild<gtk::SearchEntry>,
        #[template_child]
        pub servers_button: TemplateChild<gtk::MenuButton>,
        #[template_child]
        pub servers_popover: TemplateChild<ExploreServersPopover>,
        #[template_child]
        pub listview: TemplateChild<gtk::ListView>,
        #[template_child]
        pub scrolled_window: TemplateChild<gtk::ScrolledWindow>,
        pub public_room_list: RefCell<Option<PublicRoomList>>,
    }

    #[glib::object_subclass]
    impl ObjectSubclass for Explore {
        const NAME: &'static str = "ContentExplore";
        type Type = super::Explore;
        type ParentType = adw::Bin;

        fn class_init(klass: &mut Self::Class) {
            PublicRoom::ensure_type();
            PublicRoomList::ensure_type();
            PublicRoomRow::ensure_type();
            Spinner::ensure_type();

            Self::bind_template(klass);

            klass.set_accessible_role(gtk::AccessibleRole::Group);
        }

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

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

            adj.connect_value_changed(clone!(@weak self as imp => move |adj| {
                if adj.upper() - adj.value() < adj.page_size() * 2.0 {
                    if let Some(public_room_list) = &*imp.public_room_list.borrow() {
                        public_room_list.load_public_rooms(false);
                    }
                }
            }));

            self.search_entry
                .connect_search_changed(clone!(@weak obj => move |_| {
                    obj.trigger_search();
                }));

            self.servers_popover.connect_selected_server_changed(
                clone!(@weak obj => move |_, server| {
                    if let Some(server) = server {
                        obj.imp().servers_button.set_label(&server.name());
                        obj.trigger_search();
                    }
                }),
            );
        }
    }

    impl WidgetImpl for Explore {}
    impl BinImpl for Explore {}

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

            if let Some(session) = &session {
                let public_room_list = PublicRoomList::new(session);
                self.listview
                    .set_model(Some(&gtk::NoSelection::new(Some(public_room_list.clone()))));

                public_room_list.connect_loading_notify(clone!(@weak obj => move |_| {
                    obj.update_visible_child();
                }));

                public_room_list.connect_empty_notify(clone!(@weak obj => move |_| {
                    obj.update_visible_child();
                }));

                self.public_room_list.replace(Some(public_room_list));
                obj.update_visible_child();
            }

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

glib::wrapper! {
    /// A view to explore rooms in the public directory of homeservers.
    pub struct Explore(ObjectSubclass<imp::Explore>)
        @extends gtk::Widget, adw::Bin, @implements gtk::Accessible;
}

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

    pub fn init(&self) {
        let imp = self.imp();

        imp.servers_popover.init();

        if let Some(server) = imp.servers_popover.selected_server() {
            imp.servers_button.set_label(&server.name());
        }

        if let Some(public_room_list) = &*imp.public_room_list.borrow() {
            public_room_list.init();
        }

        self.imp().search_entry.grab_focus();
    }

    /// Update the visible child according to the current state.
    fn update_visible_child(&self) {
        let imp = self.imp();
        if let Some(public_room_list) = &*imp.public_room_list.borrow() {
            if public_room_list.loading() {
                imp.stack.set_visible_child_name("loading");
            } else if public_room_list.empty() {
                imp.stack.set_visible_child_name("empty");
            } else {
                imp.stack.set_visible_child_name("results");
            }
        }
    }

    fn trigger_search(&self) {
        let imp = self.imp();
        if let Some(public_room_list) = &*imp.public_room_list.borrow() {
            let text = imp.search_entry.text().as_str().to_string();
            let server = imp.servers_popover.selected_server().unwrap();
            public_room_list.search(Some(text), server);
        };
    }
}