From 9adac7b737b2a4160b0a1ffbec78d7638b55aa98 Mon Sep 17 00:00:00 2001 From: Roy Nieterau Date: Tue, 14 Jan 2025 00:03:00 +0100 Subject: [PATCH] Show logged-in user with their avatar in tray menu --- client/ayon_core/tools/tray/ui/tray.py | 33 ++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/client/ayon_core/tools/tray/ui/tray.py b/client/ayon_core/tools/tray/ui/tray.py index aad89b6081..6293599dd1 100644 --- a/client/ayon_core/tools/tray/ui/tray.py +++ b/client/ayon_core/tools/tray/ui/tray.py @@ -48,6 +48,22 @@ ) +def _crop_pixmap_to_circle(pixmap: QtGui.QPixmap) -> QtGui.QPixmap: + size = min(pixmap.width(), pixmap.height()) + cropped_pixmap = QtGui.QPixmap(size, size) + cropped_pixmap.fill(QtCore.Qt.transparent) + + painter = QtGui.QPainter(cropped_pixmap) + painter.setRenderHint(QtGui.QPainter.Antialiasing) + path = QtGui.QPainterPath() + path.addEllipse(QtCore.QRectF(0, 0, size, size)) + painter.setClipPath(path) + painter.drawPixmap(0, 0, pixmap) + painter.end() + + return cropped_pixmap + + class TrayManager: """Cares about context of application. @@ -471,7 +487,20 @@ def _startup_validations(self): def _add_version_item(self): tray_menu = self.tray_widget.menu - login_action = QtWidgets.QAction("Login", self.tray_widget) + + # Show logged-in user's name and icon + user = ayon_api.get_user() + username = user["name"] + response = ayon_api.get(f"users/{username}/avatar") + image = response.content + pixmap = QtGui.QPixmap() + pixmap.loadFromData(image) + pixmap = _crop_pixmap_to_circle(pixmap) + icon = QtGui.QIcon(pixmap) + name = user.get("attrib", {}).get("fullName") or user["name"] + + login_action = QtWidgets.QAction(f"Login ({name})", self.tray_widget) + login_action.setIcon(icon) login_action.triggered.connect(self._on_ayon_login) tray_menu.addAction(login_action) version_string = os.getenv("AYON_VERSION", "AYON Info") @@ -771,4 +800,4 @@ def main(): u"ayon_tray" ) - sys.exit(app.exec_()) + sys.exit(app.exec_()) \ No newline at end of file