diff --git a/manager/account.py b/manager/account.py index 1d15919..e09119d 100644 --- a/manager/account.py +++ b/manager/account.py @@ -591,6 +591,9 @@ class AccountManager(QObject): account_id = self.get_current_account_id() return self.get_account(account_id) if account_id is not None else None + def is_current_account(self, account_id: str | None) -> bool: + return self.get_current_account_id() == account_id + def get_launch_account(self, account_id: str | None = None) -> LaunchAccount | None: """ Get launch used account. diff --git a/pages/account.py b/pages/account.py index 664e7d3..d613c67 100644 --- a/pages/account.py +++ b/pages/account.py @@ -4,7 +4,7 @@ import webbrowser from pathlib import Path from PySide6.QtCore import QSize, Qt, Signal, QTimer -from PySide6.QtGui import QPixmap, QFontDatabase +from PySide6.QtGui import QPixmap, QFontDatabase, QIcon from PySide6.QtWidgets import ( QHBoxLayout, QLabel, @@ -14,7 +14,7 @@ from PySide6.QtWidgets import ( QMessageBox, QPushButton, QVBoxLayout, - QWidget, QDialog, QLineEdit, QApplication, + QWidget, QDialog, QLineEdit, QApplication, QToolButton, ) from constant import CLIENT_ID, MICROSOFT_VERIFY_ENDPOINT @@ -192,6 +192,8 @@ class AccountPage(QWidget): layout.addLayout(main_layout, 1) layout.addLayout(right_layout) + self.related_button : QToolButton = None + self.account_manager.accounts_changed.connect(self._render_accounts) self.account_manager.current_account_changed.connect(self._current_account_changed) self.account_manager.login_signal.device_code_ready.connect(self._show_device_code) @@ -201,6 +203,39 @@ class AccountPage(QWidget): self.account_manager.head_signal.updated.connect(self._account_head_updated) self._render_accounts() + def set_related_button(self, button: QToolButton): + self.related_button = button + self._update_related_button(None) + + def _update_related_button(self, account_id: str | None): + """ + Update related button's icon to current (active) account avatar + :param account_id: + :return: + """ + if self.related_button is None: + return + + if account_id is None: + account_id = self.account_manager.get_current_account_id() + + if not self.account_manager.is_current_account(account_id): + return + + account = self.account_manager.get_account(account_id) + if account is None: + self.related_button.setIcon( + self.app.get_icon(Path(self.app.icon_dir, "head.png"), True) + ) + return + + pixmap = self.account_manager.get_head_pixmap( + account.avatar, + QSize(32, 32), + ) + # Apply avatar to open account page button + self.related_button.setIcon(QIcon(pixmap)) + def _render_accounts(self): self.account_list.clear() @@ -254,11 +289,15 @@ class AccountPage(QWidget): continue widget = self.account_list.itemWidget(item) if isinstance(widget, AccountWidget): + pixmap = self.account_manager.get_head_pixmap(account.avatar, QSize(32, 32)) widget.head_icon_label.setPixmap( - self.account_manager.get_head_pixmap(account.avatar, QSize(32, 32)) + pixmap, ) + break + self._update_related_button(account_id) + def _delete_account(self, account_id: str): if self.account_manager.remove_account(account_id): self.account_manager.save() diff --git a/window.py b/window.py index 16fa54f..168ba5b 100644 --- a/window.py +++ b/window.py @@ -199,6 +199,8 @@ class LauncherMainWindow(QMainWindow): # Account btn (will be moved to the bottom (or last one item) of the toolbar self.open_account_page = self.toolbar.add_button(icon=self.app.get_icon(Path(self.app.icon_dir, "head.png"), True)) + self.account_page.set_related_button(self.open_account_page) # AccountPage require this to set button icon to + # current account's avatar # Profile btn self.open_create_profile_page = self.toolbar.add_button(icon=self.app.get_icon(Path(self.app.icon_dir, "add_temp.png")))