// SPDX-FileCopyrightText: 2026 Matvey Shcherbinin // SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL import QtQuick import QtQuick.Effects import org.kde.kirigami as Kirigami import org.kde.kirigamiaddons.components as Components import koutnet.app // like Components.Avatar but plays an animated GIF instead of freezing on frame one. // keeps the same shape and initials fallback; still images go through the normal Avatar. Item { id: root // same properties as Components.Avatar, so it is a drop-in property string name: "" property string source: "" property string iconSource: "" // kept for compatibility; the AnimatedImage below is always async anyway property bool asynchronous: true // A list builds and rebuilds its rows, and a row built twice is an avatar // fetched twice. So the source is asked of the cache on disk first: an // mxc:// url is a request to the homeserver, and the file is a disk read // that costs the same whichever row asked for it. Fetching it into the // cache happens either way - below, in the background - so the second pass // over the same list reads nothing from the network. readonly property string cachedSource: { if (root.source.length === 0) return "" const local = MediaCache.localPathFor(root.source) return local.length > 0 ? FsUtils.toSourceUrl(local) : root.source } Component.onCompleted: MediaCache.fetch(root.source) onSourceChanged: MediaCache.fetch(root.source) // AnimatedImage knows it is animated once frameCount > 1; until then the plain Avatar draws readonly property bool animated: probe.status === AnimatedImage.Ready && probe.frameCount > 1 implicitWidth: Kirigami.Units.iconSizes.large implicitHeight: Kirigami.Units.iconSizes.large // the real Avatar, used whenever the source is not an animation Components.Avatar { anchors.fill: parent visible: !root.animated name: root.name source: root.animated ? "" : root.cachedSource iconSource: root.iconSource asynchronous: root.asynchronous } // the animated path. AnimatedImage cannot round its corners, so it is masked to a circle AnimatedImage { id: probe anchors.fill: parent source: root.cachedSource cache: true fillMode: Image.PreserveAspectCrop // hidden; the user sees the masked copy, but it keeps playing so the mask has frames visible: false asynchronous: true sourceSize: appSettings.avatarDecodeLimit > 0 ? Qt.size(appSettings.avatarDecodeLimit, appSettings.avatarDecodeLimit) : undefined playing: !appSettings.freezeAnimatedImages // re-arm playback on load, an empty source at startup clears the flag onStatusChanged: { if (status === AnimatedImage.Ready) playing = !appSettings.freezeAnimatedImages } onPlayingChanged: { if (appSettings.freezeAnimatedImages && playing) playing = false } } MultiEffect { anchors.fill: parent visible: root.animated source: probe maskEnabled: true maskSource: circleMask } // the circular mask; a layer item, never drawn to screen, just sampled by the effect Item { id: circleMask anchors.fill: parent layer.enabled: true visible: false Rectangle { anchors.fill: parent radius: width / 2 color: "black" } } }