mirror of
https://github.com/telegramdesktop/tdesktop
synced 2025-08-31 14:38:15 +00:00
Reuse SizeTag::Large emoji instances.
This commit is contained in:
@@ -648,84 +648,4 @@ void Object::repaint() {
|
||||
_repaint();
|
||||
}
|
||||
|
||||
SeparateInstance::SeparateInstance(
|
||||
std::unique_ptr<Loader> loader,
|
||||
Fn<void(not_null<Instance*>, RepaintRequest)> repaintLater,
|
||||
Fn<void()> repaint)
|
||||
: emoji(Loading(std::move(loader), Preview()), std::move(repaintLater))
|
||||
, object(&emoji, std::move(repaint)) {
|
||||
}
|
||||
|
||||
SimpleManager::SimpleManager()
|
||||
: _repaintTimer([=] { invokeRepaints(); }) {
|
||||
}
|
||||
|
||||
std::unique_ptr<SeparateInstance> SimpleManager::make(
|
||||
std::unique_ptr<Loader> loader,
|
||||
Fn<void()> repaint) {
|
||||
auto repaintLater = [=](
|
||||
not_null<Instance*> instance,
|
||||
RepaintRequest request) {
|
||||
if (!request.when) {
|
||||
return;
|
||||
}
|
||||
auto &when = _repaints[request.duration];
|
||||
if (when < request.when) {
|
||||
when = request.when;
|
||||
}
|
||||
if (_repaintTimerScheduled) {
|
||||
return;
|
||||
}
|
||||
scheduleRepaintTimer();
|
||||
};
|
||||
_simpleRepaint = repaint;
|
||||
return std::make_unique<SeparateInstance>(
|
||||
std::move(loader),
|
||||
std::move(repaintLater),
|
||||
std::move(repaint));
|
||||
}
|
||||
|
||||
void SimpleManager::scheduleRepaintTimer() {
|
||||
_repaintTimerScheduled = true;
|
||||
Ui::PostponeCall(this, [=] {
|
||||
_repaintTimerScheduled = false;
|
||||
|
||||
auto next = crl::time();
|
||||
for (const auto &[duration, when] : _repaints) {
|
||||
if (!next || next > when) {
|
||||
next = when;
|
||||
}
|
||||
}
|
||||
if (next && (!_repaintNext || _repaintNext > next)) {
|
||||
const auto now = crl::now();
|
||||
if (now >= next) {
|
||||
_repaintNext = 0;
|
||||
_repaintTimer.cancel();
|
||||
invokeRepaints();
|
||||
} else {
|
||||
_repaintNext = next;
|
||||
_repaintTimer.callOnce(next - now);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void SimpleManager::invokeRepaints() {
|
||||
_repaintNext = 0;
|
||||
auto invoke = false;
|
||||
const auto now = crl::now();
|
||||
for (auto i = begin(_repaints); i != end(_repaints);) {
|
||||
if (i->second > now) {
|
||||
++i;
|
||||
continue;
|
||||
}
|
||||
invoke = true;
|
||||
i = _repaints.erase(i);
|
||||
}
|
||||
if (invoke && _simpleRepaint) {
|
||||
_simpleRepaint();
|
||||
}
|
||||
scheduleRepaintTimer();
|
||||
}
|
||||
|
||||
} // namespace Ui::CustomEmoji
|
||||
|
@@ -267,34 +267,4 @@ private:
|
||||
|
||||
};
|
||||
|
||||
struct SeparateInstance {
|
||||
SeparateInstance(
|
||||
std::unique_ptr<Loader> loader,
|
||||
Fn<void(not_null<Instance*>, RepaintRequest)> repaintLater,
|
||||
Fn<void()> repaint);
|
||||
|
||||
Instance emoji;
|
||||
Object object;
|
||||
};
|
||||
|
||||
class SimpleManager final : public base::has_weak_ptr {
|
||||
public:
|
||||
SimpleManager();
|
||||
|
||||
[[nodiscard]] std::unique_ptr<SeparateInstance> make(
|
||||
std::unique_ptr<Loader> loader,
|
||||
Fn<void()> repaint);
|
||||
|
||||
private:
|
||||
void scheduleRepaintTimer();
|
||||
void invokeRepaints();
|
||||
|
||||
base::flat_map<crl::time, crl::time> _repaints;
|
||||
bool _repaintTimerScheduled = false;
|
||||
crl::time _repaintNext = 0;
|
||||
base::Timer _repaintTimer;
|
||||
Fn<void()> _simpleRepaint;
|
||||
|
||||
};
|
||||
|
||||
} // namespace Ui::CustomEmoji
|
||||
|
Reference in New Issue
Block a user