-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathmod.rs
More file actions
165 lines (141 loc) · 4.64 KB
/
mod.rs
File metadata and controls
165 lines (141 loc) · 4.64 KB
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
mod chat_action_bar;
mod chat_history;
mod chat_history_item;
mod chat_history_model;
mod chat_history_row;
mod chat_info_window;
mod event_row;
mod message_row;
mod send_photo_dialog;
use self::chat_action_bar::ChatActionBar;
use self::chat_history::ChatHistory;
use self::chat_history_item::{ChatHistoryItem, ChatHistoryItemType};
use self::chat_history_model::{ChatHistoryError, ChatHistoryModel};
use self::chat_history_row::ChatHistoryRow;
use self::chat_info_window::ChatInfoWindow;
use self::event_row::EventRow;
use self::message_row::MessageRow;
use self::send_photo_dialog::SendPhotoDialog;
use glib::clone;
use gtk::glib;
use gtk::prelude::*;
use gtk::subclass::prelude::*;
use crate::tdlib::Chat;
use crate::utils::spawn;
mod imp {
use super::*;
use adw::subclass::prelude::BinImpl;
use gtk::CompositeTemplate;
use once_cell::sync::Lazy;
use std::cell::{Cell, RefCell};
#[derive(Debug, Default, CompositeTemplate)]
#[template(resource = "/com/github/melix99/telegrand/ui/content.ui")]
pub(crate) struct Content {
pub(super) compact: Cell<bool>,
pub(super) chat: RefCell<Option<Chat>>,
#[template_child]
pub(super) stack: TemplateChild<gtk::Stack>,
#[template_child]
pub(super) unselected_chat: TemplateChild<gtk::Box>,
#[template_child]
pub(super) chat_history: TemplateChild<ChatHistory>,
}
#[glib::object_subclass]
impl ObjectSubclass for Content {
const NAME: &'static str = "Content";
type Type = super::Content;
type ParentType = adw::Bin;
fn class_init(klass: &mut Self::Class) {
ChatHistory::static_type();
klass.bind_template();
}
fn instance_init(obj: &glib::subclass::InitializingObject<Self>) {
obj.init_template();
}
}
impl ObjectImpl for Content {
fn properties() -> &'static [glib::ParamSpec] {
static PROPERTIES: Lazy<Vec<glib::ParamSpec>> = Lazy::new(|| {
vec![
glib::ParamSpecBoolean::builder("compact").build(),
glib::ParamSpecObject::builder::<Chat>("chat")
.explicit_notify()
.build(),
]
});
PROPERTIES.as_ref()
}
fn set_property(&self, _id: usize, value: &glib::Value, pspec: &glib::ParamSpec) {
let obj = self.obj();
match pspec.name() {
"compact" => {
let compact = value.get().unwrap();
self.compact.set(compact);
}
"chat" => {
let chat = value.get().unwrap();
obj.set_chat(chat);
}
_ => unimplemented!(),
}
}
fn property(&self, _id: usize, pspec: &glib::ParamSpec) -> glib::Value {
let obj = self.obj();
match pspec.name() {
"compact" => self.compact.get().to_value(),
"chat" => obj.chat().to_value(),
_ => unimplemented!(),
}
}
}
impl WidgetImpl for Content {}
impl BinImpl for Content {}
}
glib::wrapper! {
pub(crate) struct Content(ObjectSubclass<imp::Content>)
@extends gtk::Widget, adw::Bin;
}
impl Default for Content {
fn default() -> Self {
Self::new()
}
}
impl Content {
pub(crate) fn new() -> Self {
glib::Object::new()
}
pub(crate) fn handle_paste_action(&self) {
self.imp().chat_history.handle_paste_action();
}
pub(crate) fn chat(&self) -> Option<Chat> {
self.imp().chat.borrow().clone()
}
fn set_chat(&self, chat: Option<Chat>) {
if self.chat() == chat {
return;
}
let imp = self.imp();
if chat.is_some() {
imp.stack.set_visible_child(&imp.chat_history.get());
} else {
imp.stack.set_visible_child(&imp.unselected_chat.get());
}
// Mark the previous chat as closed, if any
if let Some(old_chat) = imp.chat.replace(chat) {
spawn(clone!(@weak old_chat => async move {
if let Err(e) = old_chat.close().await {
log::warn!("Error closing a chat: {e:?}");
}
}));
}
// Mark the new chat as opened, if any
if let Some(chat) = imp.chat.borrow().as_ref() {
spawn(clone!(@weak chat => async move {
if let Err(e) = chat.open().await {
log::warn!("Error opening a chat: {e:?}");
}
}));
}
self.notify("chat");
}
}