Skip to content

Commit d24e160

Browse files
committed
rlottie: Recolor stickers using color matrix
1 parent d2aa231 commit d24e160

File tree

3 files changed

+24
-21
lines changed

3 files changed

+24
-21
lines changed

data/resources/style-dark.css

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,6 @@
22
background-color: @dark_2;
33
}
44

5-
/* sticker must be repainted to a text color in messages */
6-
messagesticker.needs-repainting > overlay > widget > widget {
7-
filter: invert(1);
8-
}
9-
105
vectorpath {
116
color: alpha(#404040, 0.8);
127
}

src/components/sticker.rs

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use tdlib::types::Sticker as TdSticker;
88

99
use super::VectorPath;
1010
use crate::session::Session;
11-
use crate::utils::{decode_image_from_path, spawn};
11+
use crate::utils::{color_matrix_from_color, decode_image_from_path, spawn};
1212

1313
mod imp {
1414
use super::*;
@@ -19,6 +19,8 @@ mod imp {
1919
pub(crate) struct Sticker {
2020
pub(super) file_id: Cell<i32>,
2121
pub(super) aspect_ratio: Cell<f64>,
22+
pub(super) recolor: Cell<bool>,
23+
pub(super) is_loaded: Cell<bool>,
2224
pub(super) child: RefCell<Option<gtk::Widget>>,
2325

2426
#[property(get, set = Self::set_longer_side_size)]
@@ -84,6 +86,17 @@ mod imp {
8486
child.allocate(width, height, baseline, None);
8587
}
8688
}
89+
90+
fn snapshot(&self, snapshot: &gtk::Snapshot) {
91+
if self.recolor.get() && self.is_loaded.get() {
92+
let (color_matrix, color_offset) = color_matrix_from_color(self.obj().color());
93+
snapshot.push_color_matrix(&color_matrix, &color_offset);
94+
self.parent_snapshot(snapshot);
95+
snapshot.pop();
96+
} else {
97+
self.parent_snapshot(snapshot);
98+
}
99+
}
87100
}
88101

89102
impl Sticker {
@@ -108,11 +121,15 @@ impl Sticker {
108121
return;
109122
}
110123

111-
self.set_child(Some(VectorPath::new(&sticker.outline).upcast()));
124+
self.set_child(VectorPath::new(&sticker.outline).upcast(), false);
112125

113126
let aspect_ratio = sticker.width as f64 / sticker.height as f64;
114127
imp.aspect_ratio.set(aspect_ratio);
115128

129+
let recolor = matches!(&sticker.full_type,
130+
tdlib::enums::StickerFullType::CustomEmoji(data) if data.needs_repainting);
131+
imp.recolor.set(recolor);
132+
116133
let format = sticker.format;
117134

118135
spawn(clone!(@weak self as obj, @weak session => async move {
@@ -185,18 +202,17 @@ impl Sticker {
185202

186203
// Skip if widget was recycled by ListView
187204
if self.imp().file_id.get() == file_id {
188-
self.set_child(Some(widget));
205+
self.set_child(widget, true);
189206
}
190207
}
191208

192-
fn set_child(&self, child: Option<gtk::Widget>) {
209+
fn set_child(&self, child: gtk::Widget, is_loaded: bool) {
193210
let imp = self.imp();
211+
imp.is_loaded.set(is_loaded);
194212

195-
if let Some(ref child) = child {
196-
child.set_parent(self);
197-
}
213+
child.set_parent(self);
198214

199-
if let Some(old) = imp.child.replace(child) {
215+
if let Some(old) = imp.child.replace(Some(child)) {
200216
old.unparent()
201217
}
202218
}

src/session/content/message_row/sticker.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -162,14 +162,6 @@ impl MessageBaseExt for MessageSticker {
162162
_ => unreachable!(),
163163
};
164164

165-
// TODO: that should be handled a bit better in the future
166-
match &sticker.full_type {
167-
StickerFullType::CustomEmoji(data) if data.needs_repainting => {
168-
self.add_css_class("needs-repainting")
169-
}
170-
_ => self.remove_css_class("needs-repainting"),
171-
}
172-
173165
let (size, margin_bottom) = if is_emoji {
174166
(EMOJI_SIZE, 8)
175167
} else {

0 commit comments

Comments
 (0)