qt-chat-app

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

Avatar.qml (2062B)


      1 import QtQuick
      2 import "../styles"
      3 
      4 Item {
      5     id: root
      6     width: size
      7     height: size
      8 
      9     property int size: TelegramStyle.avatarSize
     10     property string imageSource: ""
     11     property string name: ""
     12     property int chatId: 0
     13     property bool showOnlineIndicator: false
     14 
     15     // Background circle with color based on name
     16     Rectangle {
     17         id: circle
     18         anchors.fill: parent
     19         radius: size / 2
     20         color: getColorForName(name)
     21         clip: true
     22 
     23         // Initials
     24         Text {
     25             visible: imageSource === ""
     26             anchors.centerIn: parent
     27             text: getInitials(name)
     28             color: "white"
     29             font.pixelSize: size / 2.5
     30             font.bold: true
     31         }
     32 
     33         // Image overlay (if available)
     34         Image {
     35             visible: imageSource !== ""
     36             anchors.fill: parent
     37             source: imageSource
     38             fillMode: Image.PreserveAspectCrop
     39             smooth: true
     40         }
     41     }
     42 
     43     // Online indicator
     44     Rectangle {
     45         visible: showOnlineIndicator
     46         width: size / 4
     47         height: size / 4
     48         radius: width / 2
     49         color: TelegramStyle.onlineIndicator
     50         border.color: "white"
     51         border.width: 2
     52         anchors.right: parent.right
     53         anchors.bottom: parent.bottom
     54     }
     55 
     56     function getInitials(name) {
     57         if (!name || name === "") {
     58             return chatId > 0 ? chatId.toString() : "?"
     59         }
     60         var parts = name.split(" ")
     61         if (parts.length >= 2) {
     62             return (parts[0][0] + parts[1][0]).toUpperCase()
     63         }
     64         return name.substring(0, Math.min(2, name.length)).toUpperCase()
     65     }
     66 
     67     function getColorForName(name) {
     68         var colors = ["#e57373", "#ba68c8", "#64b5f6",
     69                       "#4db6ac", "#81c784", "#ffb74d"]
     70         if (!name || name === "") return colors[0]
     71 
     72         var hash = 0
     73         for (var i = 0; i < name.length; i++) {
     74             hash = name.charCodeAt(i) + ((hash << 5) - hash)
     75         }
     76         return colors[Math.abs(hash) % colors.length]
     77     }
     78 }