Skip to content

Commit 9195097

Browse files
committed
WIP: misc changes
1 parent ee5ffee commit 9195097

File tree

7 files changed

+378
-4
lines changed

7 files changed

+378
-4
lines changed

src/session/content/chat_history.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use crate::session::content::ChatHistoryError;
2121
use crate::session::content::ChatHistoryModel;
2222
use crate::session::content::ChatHistoryRow;
2323
use crate::session::content::ChatInfoWindow;
24+
use crate::strings::ChatSubtitleString;
2425
use crate::tdlib::Chat;
2526
use crate::tdlib::ChatType;
2627
use crate::tdlib::SponsoredMessage;
@@ -52,6 +53,7 @@ mod imp {
5253
pub(super) list_view: TemplateChild<gtk::ListView>,
5354
#[template_child]
5455
pub(super) chat_action_bar: TemplateChild<ChatActionBar>,
56+
pub binding: RefCell<Option<gtk::ExpressionWatch>>,
5557
}
5658

5759
#[glib::object_subclass]
@@ -373,6 +375,18 @@ impl ChatHistory {
373375
imp.list_view.set_model(Some(&selection));
374376

375377
imp.model.replace(Some(model));
378+
if let Some(binding) = imp.binding.take() {
379+
binding.unwatch()
380+
}
381+
382+
// TODO: Make the subtitle label using accent color when user is online or if there is a chat action
383+
384+
// Bind subtitle
385+
imp.binding.replace(Some(
386+
gtk::ConstantExpression::new(ChatSubtitleString::new(chat.clone()))
387+
.chain_property::<ChatSubtitleString>("subtitle")
388+
.bind(&*imp.window_title, "subtitle", Some(self)),
389+
));
376390
}
377391

378392
imp.chat.replace(chat);

src/session/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,9 @@ impl Session {
253253
Update::ChatUnreadMentionCount(ref data) => {
254254
self.chat(data.chat_id).handle_update(update)
255255
}
256+
Update::ChatOnlineMemberCount(ref data) => {
257+
self.chat(data.chat_id).handle_update(update)
258+
}
256259
Update::ChatIsBlocked(ref data) => self.chat(data.chat_id).handle_update(update),
257260
Update::ChatIsMarkedAsUnread(ref data) => self.chat(data.chat_id).handle_update(update),
258261
Update::DeleteMessages(ref data) => self.chat(data.chat_id).handle_update(update),

src/strings/chat_subtitle_string.rs

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
use gettextrs::gettext;
2+
use glib::clone;
3+
use gtk::glib;
4+
use gtk::prelude::*;
5+
use gtk::subclass::prelude::*;
6+
7+
use super::UserStatusString;
8+
use crate::i18n::*;
9+
use crate::strings;
10+
use crate::tdlib::Chat;
11+
use crate::tdlib::ChatType;
12+
13+
mod imp {
14+
use once_cell::sync::Lazy;
15+
use once_cell::unsync::OnceCell;
16+
17+
use super::*;
18+
19+
#[derive(Debug, Default)]
20+
pub(crate) struct ChatSubtitleString {
21+
pub(super) chat: OnceCell<Chat>,
22+
pub(super) user_status_string: OnceCell<UserStatusString>,
23+
}
24+
25+
#[glib::object_subclass]
26+
impl ObjectSubclass for ChatSubtitleString {
27+
const NAME: &'static str = "ChatSubtitleString";
28+
type Type = super::ChatSubtitleString;
29+
}
30+
31+
impl ObjectImpl for ChatSubtitleString {
32+
fn properties() -> &'static [glib::ParamSpec] {
33+
static PROPERTIES: Lazy<Vec<glib::ParamSpec>> = Lazy::new(|| {
34+
vec![glib::ParamSpecString::builder("subtitle")
35+
.read_only()
36+
.build()]
37+
});
38+
PROPERTIES.as_ref()
39+
}
40+
41+
fn property(&self, _id: usize, pspec: &glib::ParamSpec) -> glib::Value {
42+
let obj = self.obj();
43+
44+
match pspec.name() {
45+
"subtitle" => obj.subtitle().to_value(),
46+
_ => unimplemented!(),
47+
}
48+
}
49+
}
50+
}
51+
52+
glib::wrapper! {
53+
pub(crate) struct ChatSubtitleString(ObjectSubclass<imp::ChatSubtitleString>);
54+
}
55+
56+
impl ChatSubtitleString {
57+
pub(crate) fn new(chat: Chat) -> ChatSubtitleString {
58+
let obj: ChatSubtitleString = glib::Object::builder().build();
59+
60+
chat.actions()
61+
.connect_items_changed(clone!(@weak obj => move |_, _, _, _| {
62+
obj.notify("subtitle");
63+
}));
64+
65+
chat.connect_notify_local(
66+
Some("online-member-count"),
67+
clone!(@weak obj => move |_, _| {
68+
obj.notify("subtitle");
69+
}),
70+
);
71+
match chat.type_() {
72+
ChatType::BasicGroup(basic) => {
73+
basic.connect_notify_local(
74+
Some("member-count"),
75+
clone!(@weak obj => move |_, _| {
76+
obj.notify("subtitle");
77+
}),
78+
);
79+
}
80+
ChatType::Supergroup(group) => {
81+
group.connect_notify_local(
82+
Some("member-count"),
83+
clone!(@weak obj => move |_, _| {
84+
obj.notify("subtitle");
85+
}),
86+
);
87+
}
88+
ChatType::Private(user) if !chat.is_own_chat() => {
89+
let user_status_string = UserStatusString::new(user.clone());
90+
user_status_string.connect_notify_local(
91+
Some("string"),
92+
clone!(@weak obj => move |_, _| {
93+
obj.notify("subtitle");
94+
}),
95+
);
96+
obj.imp()
97+
.user_status_string
98+
.set(user_status_string)
99+
.unwrap();
100+
}
101+
ChatType::Secret(secret) => {
102+
let user = secret.user();
103+
let user_status_string = UserStatusString::new(user.clone());
104+
user_status_string.connect_notify_local(
105+
Some("string"),
106+
clone!(@weak obj => move |_, _| {
107+
obj.notify("subtitle");
108+
}),
109+
);
110+
obj.imp()
111+
.user_status_string
112+
.set(user_status_string)
113+
.unwrap();
114+
}
115+
_ => (),
116+
}
117+
118+
obj.imp().chat.set(chat).unwrap();
119+
obj
120+
}
121+
122+
pub(crate) fn subtitle(&self) -> String {
123+
let chat = self.imp().chat.get().unwrap();
124+
let action = chat.actions().last();
125+
if let Some(action) = action {
126+
strings::chat_action(&action)
127+
} else {
128+
format!(
129+
"{}{}",
130+
if !chat.is_own_chat() {
131+
match chat.type_() {
132+
ChatType::Private(_) | ChatType::Secret(_) => {
133+
if !chat.is_own_chat() {
134+
self.imp().user_status_string.get().unwrap().string()
135+
} else {
136+
String::new()
137+
}
138+
}
139+
ChatType::BasicGroup(basic) => {
140+
let m_count = basic.member_count();
141+
match m_count {
142+
0 => gettext("group"),
143+
_ => ngettext_f(
144+
"{num} member",
145+
"{num} members",
146+
m_count as u32,
147+
&[("num", &m_count.to_string())],
148+
),
149+
}
150+
}
151+
ChatType::Supergroup(data) if data.is_channel() => {
152+
let m_count = data.member_count();
153+
match m_count {
154+
0 => gettext("channel"),
155+
_ => ngettext_f(
156+
"{num} subscriber",
157+
"{num} subscribers",
158+
m_count as u32,
159+
&[("num", &m_count.to_string())],
160+
),
161+
}
162+
}
163+
ChatType::Supergroup(data) => {
164+
let m_count = data.member_count();
165+
match m_count {
166+
0 => gettext("group"),
167+
_ => ngettext_f(
168+
"{num} member",
169+
"{num} members",
170+
m_count as u32,
171+
&[("num", &m_count.to_string())],
172+
),
173+
}
174+
}
175+
}
176+
} else {
177+
String::new()
178+
},
179+
if chat.online_member_count() > 1 {
180+
format!(
181+
", {}",
182+
ngettext_f(
183+
"{num} online",
184+
"{num} online",
185+
chat.online_member_count() as u32,
186+
&[("num", &chat.online_member_count().to_string())]
187+
)
188+
)
189+
} else {
190+
String::new()
191+
}
192+
)
193+
}
194+
}
195+
}

src/strings.rs renamed to src/strings/mod.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
mod chat_subtitle_string;
2+
mod user_status_string;
3+
4+
pub(crate) use chat_subtitle_string::ChatSubtitleString;
15
use ellipse::Ellipse;
26
use gettextrs::gettext;
37
use gtk::glib;
@@ -6,6 +10,7 @@ use tdlib::enums::UserStatus;
610
use tdlib::enums::UserType;
711
use tdlib::types::MessageGame;
812
use tdlib::types::MessageGameScore;
13+
pub(crate) use user_status_string::UserStatusString;
914

1015
use crate::i18n::gettext_f;
1116
use crate::i18n::ngettext_f;
@@ -842,3 +847,18 @@ fn message_contact_registered(sender: &MessageSender) -> String {
842847
let sender = message_sender(sender, true);
843848
gettext_f("{sender} joined Telegram", &[("sender", &sender)])
844849
}
850+
851+
/*pub(crate) fn group_subtitle(member_count: i32, online_count: i32) -> String {
852+
format!(
853+
"{}{}",
854+
match member_count {
855+
1 => gettext("1 member"),
856+
_ => gettext!("{} members", member_count),
857+
},
858+
if online_count > 1 {
859+
gettext!(", {} online", online_count)
860+
} else {
861+
String::new()
862+
}
863+
)
864+
}*/

0 commit comments

Comments
 (0)