| 1 |
// SPDX-FileCopyrightText: Nheko Contributors |
| 2 |
// |
| 3 |
// SPDX-License-Identifier: GPL-3.0-or-later |
| 4 |
|
| 5 |
import "./ui" |
| 6 |
import QtQuick 2.15 |
| 7 |
import QtQuick.Controls 2.15 |
| 8 |
import QtQuick.Window 2.15 |
| 9 |
import im.nheko 1.0 |
| 10 |
|
| 11 |
AbstractButton { |
| 12 |
id: avatar |
| 13 |
|
| 14 |
property alias color: bg.color |
| 15 |
property bool crop: true |
| 16 |
property string displayName |
| 17 |
property string roomid |
| 18 |
property alias textColor: label.color |
| 19 |
property string url |
| 20 |
property string userid |
| 21 |
|
| 22 |
height: 48 |
| 23 |
width: 48 |
| 24 |
|
| 25 |
background: Rectangle { |
| 26 |
id: bg |
| 27 |
|
| 28 |
color: palette.alternateBase |
| 29 |
radius: Settings.avatarCircles ? height / 2 : height / 8 |
| 30 |
} |
| 31 |
|
| 32 |
Label { |
| 33 |
id: label |
| 34 |
|
| 35 |
anchors.fill: parent |
| 36 |
color: palette.text |
| 37 |
enabled: false |
| 38 |
font.pixelSize: avatar.height / 2 |
| 39 |
horizontalAlignment: Text.AlignHCenter |
| 40 |
text: TimelineManager.escapeEmoji(avatar.displayName ? String.fromCodePoint(avatar.displayName.codePointAt(0)) : "") |
| 41 |
textFormat: Text.RichText |
| 42 |
verticalAlignment: Text.AlignVCenter |
| 43 |
visible: img.status != Image.Ready && !Settings.useIdenticon |
| 44 |
} |
| 45 |
Image { |
| 46 |
id: identicon |
| 47 |
|
| 48 |
anchors.fill: parent |
| 49 |
source: Settings.useIdenticon ? ("image://jdenticon/" + (avatar.userid !== "" ? avatar.userid : avatar.roomid) + "?radius=" + (Settings.avatarCircles ? 100 : 25)) : "" |
| 50 |
visible: Settings.useIdenticon && img.status != Image.Ready |
| 51 |
} |
| 52 |
Image { |
| 53 |
id: img |
| 54 |
|
| 55 |
anchors.fill: parent |
| 56 |
asynchronous: true |
| 57 |
fillMode: avatar.crop ? Image.PreserveAspectCrop : Image.PreserveAspectFit |
| 58 |
source: if (avatar.url.startsWith('image://colorimage')) { |
| 59 |
return avatar.url + "&radius=" + (Settings.avatarCircles ? 100 : 25) + ((avatar.crop) ? "" : "&scale"); |
| 60 |
} else if (avatar.url.startsWith('image://')) { |
| 61 |
return avatar.url + "?radius=" + (Settings.avatarCircles ? 100 : 25) + ((avatar.crop) ? "" : "&scale"); |
| 62 |
} else if (avatar.url.startsWith(':/')) { |
| 63 |
return "image://colorimage/" + avatar.url + "?" + label.color; |
| 64 |
} else { |
| 65 |
return ""; |
| 66 |
} |
| 67 |
sourceSize.height: if (!avatar.url.startsWith('image://MxcImage/') && avatar.url.endsWith('.svg')){ |
| 68 |
return avatar.height |
| 69 |
} else { |
| 70 |
return avatar.height * Screen.devicePixelRatio |
| 71 |
} |
| 72 |
sourceSize.width: if (!avatar.url.startsWith('image://MxcImage/') && avatar.url.endsWith('.svg')){ |
| 73 |
return avatar.width |
| 74 |
} else { |
| 75 |
return avatar.width * Screen.devicePixelRatio |
| 76 |
} |
| 77 |
} |
| 78 |
Rectangle { |
| 79 |
id: onlineIndicator |
| 80 |
|
| 81 |
function updatePresence() { |
| 82 |
switch (Presence.userPresence(avatar.userid)) { |
| 83 |
case "online": |
| 84 |
return Nheko.theme.online; |
| 85 |
case "unavailable": |
| 86 |
return Nheko.theme.unavailable; |
| 87 |
case "offline": |
| 88 |
default: |
| 89 |
// return "#a82353" don't show anything if offline, since it is confusing, if presence is disabled |
| 90 |
return "transparent"; |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
anchors.bottom: avatar.bottom |
| 95 |
anchors.right: avatar.right |
| 96 |
color: updatePresence() |
| 97 |
height: avatar.height / 6 |
| 98 |
radius: Settings.avatarCircles ? height / 2 : height / 8 |
| 99 |
visible: !!avatar.userid |
| 100 |
width: height |
| 101 |
|
| 102 |
Connections { |
| 103 |
function onPresenceChanged(id) { |
| 104 |
if (id == avatar.userid) |
| 105 |
onlineIndicator.color = onlineIndicator.updatePresence(); |
| 106 |
} |
| 107 |
|
| 108 |
target: Presence |
| 109 |
} |
| 110 |
} |
| 111 |
NhekoCursorShape { |
| 112 |
anchors.fill: parent |
| 113 |
cursorShape: Qt.PointingHandCursor |
| 114 |
} |
| 115 |
Ripple { |
| 116 |
color: Qt.rgba(palette.alternateBase.r, palette.alternateBase.g, palette.alternateBase.b, 0.5) |
| 117 |
} |
| 118 |
} |