Skip to content

Commit 62336dd

Browse files
committed
rlottie: Recolor stickers using color matrix
1 parent ff0ab78 commit 62336dd

File tree

3 files changed

+29
-20
lines changed

3 files changed

+29
-20
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
.chat-list row .unread-count-muted {
116
background-color: @dark_2;
127
}

src/components/sticker.rs

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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,23 @@ 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+
use gtk::graphene::*;
93+
let color = self.obj().color();
94+
let color_offset = Vec4::new(color.red(), color.green(), color.blue(), 0.0);
95+
let mut matrix = [0.0; 16];
96+
matrix[15] = color.alpha();
97+
let color_matrix = Matrix::from_float(matrix);
98+
99+
snapshot.push_color_matrix(&color_matrix, &color_offset);
100+
self.parent_snapshot(snapshot);
101+
snapshot.pop();
102+
} else {
103+
self.parent_snapshot(snapshot);
104+
}
105+
}
87106
}
88107

89108
impl Sticker {
@@ -108,11 +127,15 @@ impl Sticker {
108127
return;
109128
}
110129

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

113132
let aspect_ratio = sticker.width as f64 / sticker.height as f64;
114133
imp.aspect_ratio.set(aspect_ratio);
115134

135+
let recolor = matches!(&sticker.full_type,
136+
tdlib::enums::StickerFullType::CustomEmoji(data) if data.needs_repainting);
137+
imp.recolor.set(recolor);
138+
116139
let format = sticker.format;
117140

118141
spawn(clone!(@weak self as obj, @weak session => async move {
@@ -185,18 +208,17 @@ impl Sticker {
185208

186209
// Skip if widget was recycled by ListView
187210
if self.imp().file_id.get() == file_id {
188-
self.set_child(Some(widget));
211+
self.set_child(widget, true);
189212
}
190213
}
191214

192-
fn set_child(&self, child: Option<gtk::Widget>) {
215+
fn set_child(&self, child: gtk::Widget, is_loaded: bool) {
193216
let imp = self.imp();
217+
imp.is_loaded.set(is_loaded);
194218

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

199-
if let Some(old) = imp.child.replace(child) {
221+
if let Some(old) = imp.child.replace(Some(child)) {
200222
old.unparent()
201223
}
202224
}

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)