CoreLib
Update to v0.1.4 Add `is_function_in_file` and if the key is "<ORIGINAL>", the `TranslationManager.translate()` will return it directly. (For some strings that don't need to translate in specific language) Launcher Update to 0.1.0a0 Add plugin system support and add some useful apis for plugin. Add "Refresh avatar cache" to account page.
This commit is contained in:
1 parent
deb06f0190
commit
c3662d4cc6
34 files changed
+2192
-57
No files matched your search
+44
-11
@@ -20,7 +20,7 @@ from PySide6.QtWidgets import (
|
||||
from constant import MICROSOFT_VERIFY_ENDPOINT, CLIENT_ID
|
||||
|
||||
from core_lib.qt.hack import move_window_to_center
|
||||
from manager.account import AccountManager, AccountSummary, AccountLoginStatus
|
||||
from manager.account import AccountLoginStatus, AccountSummary, AccountManager
|
||||
from manager.settings_models import AccountSettings
|
||||
|
||||
|
||||
@@ -195,12 +195,17 @@ class AccountPage(QWidget):
|
||||
self.refresh_button = QPushButton(self.app.tr_text("Refresh all"))
|
||||
self.reload_button = QPushButton(self.app.tr_text("Reload accounts"))
|
||||
self.delete_all_button = QPushButton(self.app.tr_text("Delete All"))
|
||||
self.refresh_avatar_cache_button = QPushButton(self.app.tr_text("Refresh avatar cache"))
|
||||
|
||||
# Button click handle
|
||||
self.add_account_button.clicked.connect(self.start_login_page)
|
||||
self.refresh_button.clicked.connect(self.refresh_accounts)
|
||||
self.reload_button.clicked.connect(self.reload_accounts)
|
||||
self.delete_all_button.clicked.connect(self.account_manager.delete_all_accounts)
|
||||
self.refresh_avatar_cache_button.clicked.connect(self.refresh_accounts_head)
|
||||
|
||||
# Size
|
||||
self.avatar_size = QSize(32, 32)
|
||||
|
||||
# Bind widget
|
||||
main_layout.addWidget(self.account_list)
|
||||
@@ -208,6 +213,7 @@ class AccountPage(QWidget):
|
||||
right_layout.addWidget(self.refresh_button)
|
||||
right_layout.addWidget(self.reload_button)
|
||||
right_layout.addWidget(self.delete_all_button)
|
||||
right_layout.addWidget(self.refresh_avatar_cache_button)
|
||||
right_layout.addStretch()
|
||||
|
||||
layout.addLayout(main_layout, 1)
|
||||
@@ -298,12 +304,11 @@ class AccountPage(QWidget):
|
||||
list_item = QListWidgetItem()
|
||||
list_item.setData(Qt.ItemDataRole.UserRole, account.id)
|
||||
|
||||
head_size = QSize(32, 32)
|
||||
widget = AccountWidget(
|
||||
self.app,
|
||||
account.id,
|
||||
account.display_name,
|
||||
self.account_manager.get_head_pixmap(account.avatar, head_size),
|
||||
self.account_manager.get_head_pixmap(account.avatar, self.avatar_size),
|
||||
self.app.tr_text("Current" if is_current else "Available"),
|
||||
self,
|
||||
delete_account_callback=lambda: self._delete_account(account.id),
|
||||
@@ -317,11 +322,31 @@ class AccountPage(QWidget):
|
||||
self.account_list.setItemWidget(list_item, widget)
|
||||
|
||||
if not self.account_manager.has_saved_head(account.avatar):
|
||||
self.account_manager.request_account_head(
|
||||
account.id,
|
||||
account.minecraft_name or account.username,
|
||||
max(head_size.width(), head_size.height()),
|
||||
)
|
||||
self._update_account_head(account.id, account.minecraft_name or account.username)
|
||||
|
||||
def refresh_accounts_head(self):
|
||||
for row in range(self.account_list.count()):
|
||||
item = self.account_list.item(row)
|
||||
widget = self.account_list.itemWidget(item)
|
||||
if isinstance(widget, AccountWidget):
|
||||
related_account = self.account_manager.get_account(widget.account_id)
|
||||
|
||||
if not related_account:
|
||||
continue
|
||||
|
||||
self._update_account_head(
|
||||
related_account.id,
|
||||
related_account.minecraft_name or related_account.username,
|
||||
force=True,
|
||||
)
|
||||
|
||||
def _update_account_head(self, account_id: str, username: str, *, force: bool = False):
|
||||
self.account_manager.request_account_head(
|
||||
account_id,
|
||||
username,
|
||||
max(self.avatar_size.width(), self.avatar_size.height()),
|
||||
force=force,
|
||||
)
|
||||
|
||||
def _account_head_updated(self, account_id: str):
|
||||
account = self.account_manager.get_account(account_id)
|
||||
@@ -383,9 +408,9 @@ class AccountPage(QWidget):
|
||||
def start_login_page(self):
|
||||
# Active existing login dialog
|
||||
if self.login_dialog is not None:
|
||||
self.login_dialog.raise_()
|
||||
self.login_dialog.activateWindow()
|
||||
return
|
||||
self.app.logger.info("Destroying existing login dialog...")
|
||||
self.login_dialog.close()
|
||||
self.login_dialog.deleteLater()
|
||||
|
||||
# Create login dialog
|
||||
self.add_account_button.setEnabled(False)
|
||||
@@ -427,6 +452,14 @@ class AccountPage(QWidget):
|
||||
if created_id is not None:
|
||||
self.account_authenticated.emit({"account_id": created_id})
|
||||
|
||||
account = self.account_manager.get_account(created_id)
|
||||
|
||||
if not account:
|
||||
return
|
||||
|
||||
# Update head image
|
||||
self._update_account_head(created_id, account.username)
|
||||
|
||||
def _login_failed(self, message: str):
|
||||
if self.login_dialog is not None:
|
||||
self.login_dialog.set_error_message(message)
|
||||
|
||||
@@ -5,6 +5,7 @@ from PySide6.QtGui import QIcon, QMouseEvent, QHideEvent, QPixmap, QPainter
|
||||
from PySide6.QtWidgets import (QWidget, QVBoxLayout, QLabel, QHBoxLayout, QPushButton, QScrollArea, QFrame,
|
||||
QSizePolicy, QApplication)
|
||||
|
||||
from core_lib.qt.item_button import ItemButton
|
||||
from manager.game import LaunchManager, LaunchResult
|
||||
from core_lib.game.version_info import LATEST_VERSION_ALIASES
|
||||
from manager.profile import ProfileSummary, LaunchProfile, ProfileManager
|
||||
@@ -86,7 +87,8 @@ class ProfileButton(QFrame):
|
||||
self.clicked.emit()
|
||||
super().mousePressEvent(event)
|
||||
|
||||
class ProfileListItem(QPushButton):
|
||||
|
||||
class ProfileListItem(ItemButton):
|
||||
selected = Signal(object)
|
||||
|
||||
def __init__(self, profile: ProfileSummary, parent: QWidget | None = None) -> None:
|
||||
@@ -475,6 +477,7 @@ class LaunchProfilePage(QWidget):
|
||||
|
||||
if not request.wait(60):
|
||||
self.widget_mgr.signal.show_error_message.emit(
|
||||
+
|
||||
self.app.tr_text("Time out waiting for request for launch.")
|
||||
)
|
||||
return
|
||||
|
||||
+89
-3
@@ -1,20 +1,22 @@
|
||||
import shutil
|
||||
from pathlib import Path
|
||||
from typing import Callable
|
||||
|
||||
from PySide6.QtCore import Qt, QUrl, Signal
|
||||
from PySide6.QtGui import QDesktopServices, QIcon
|
||||
from PySide6.QtWidgets import (
|
||||
QFileDialog, QFormLayout, QFrame, QHBoxLayout, QLabel, QLineEdit,
|
||||
QMessageBox, QPushButton, QScrollArea, QSizePolicy, QSpinBox,
|
||||
QVBoxLayout, QWidget, QMenu, QComboBox,
|
||||
QVBoxLayout, QWidget, QMenu, QComboBox, QTabWidget,
|
||||
)
|
||||
|
||||
from core_lib.qt.item_button import ItemButton
|
||||
from manager.profile import ProfileDetail, ProfileManager, ProfileSummary, UpdateProfileRequest
|
||||
from manager.game import AvailableVersion
|
||||
from pages.add_modded_version import AddModdedVersionDialog
|
||||
|
||||
|
||||
class ManageProfileItem(QPushButton):
|
||||
class ManageProfileItem(ItemButton):
|
||||
selected = Signal(object)
|
||||
|
||||
def __init__(self, app, profile: ProfileSummary,
|
||||
@@ -94,6 +96,8 @@ class ManageProfilePage(QWidget):
|
||||
Profile manage page
|
||||
"""
|
||||
|
||||
selected_profile_changed = Signal(object)
|
||||
|
||||
VERSION_CATEGORIES = (
|
||||
("Vanilla", "vanilla"),
|
||||
("Modded", "modded"),
|
||||
@@ -136,12 +140,14 @@ class ManageProfilePage(QWidget):
|
||||
self.options_layout.setContentsMargins(20, 16, 20, 20)
|
||||
self.options_layout.setSpacing(14)
|
||||
self.options_scroll.setWidget(self.options_content)
|
||||
columns.addWidget(self.options_scroll, 3)
|
||||
|
||||
# Tabs
|
||||
self.profile_tabs = QTabWidget()
|
||||
self.empty_label = QLabel(self.app.tr_text("Select a profile from the list to edit its settings."))
|
||||
self.empty_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
|
||||
self.empty_label.setWordWrap(True)
|
||||
self.options_layout.addWidget(self.empty_label, 1)
|
||||
columns.addWidget(self.profile_tabs, 3)
|
||||
|
||||
self.editor = QFrame()
|
||||
self.editor.setObjectName("profileEditor")
|
||||
@@ -222,6 +228,21 @@ class ManageProfilePage(QWidget):
|
||||
self._load_type_dropdown()
|
||||
self.refresh_profiles()
|
||||
|
||||
self._tabs = {
|
||||
"Profile settings": self.options_scroll
|
||||
}
|
||||
|
||||
self.app.i18n.language_changed.connect(self.load_profile_page_for_tabs)
|
||||
self.load_profile_page_for_tabs()
|
||||
|
||||
def load_profile_page_for_tabs(self, *_args):
|
||||
for tab_name, page in self._tabs.items():
|
||||
index = self.profile_tabs.indexOf(page)
|
||||
if index < 0:
|
||||
self.profile_tabs.addTab(page, self.app.tr_text(tab_name))
|
||||
else:
|
||||
self.profile_tabs.setTabText(index, self.app.tr_text(tab_name))
|
||||
|
||||
@staticmethod
|
||||
def _scroll_area(name: str) -> QScrollArea:
|
||||
area = QScrollArea()
|
||||
@@ -303,6 +324,7 @@ class ManageProfilePage(QWidget):
|
||||
else:
|
||||
self.editor.hide()
|
||||
self.empty_label.show()
|
||||
self.selected_profile_changed.emit(None)
|
||||
|
||||
def select_profile(self, profile: ProfileSummary) -> None:
|
||||
detail = self.profile_manager.get_profile(profile.id)
|
||||
@@ -318,6 +340,7 @@ class ManageProfilePage(QWidget):
|
||||
self._set_detail(detail)
|
||||
self.empty_label.hide()
|
||||
self.editor.show()
|
||||
self.selected_profile_changed.emit(detail)
|
||||
|
||||
def _select_version(self, version_id: str) -> None:
|
||||
for index in range(self.version_dropdown.count()):
|
||||
@@ -496,3 +519,66 @@ class ManageProfilePage(QWidget):
|
||||
if answer == QMessageBox.StandardButton.Yes:
|
||||
if self.profile_manager.remove_profile(profile_id):
|
||||
self.selected_profile_id = None
|
||||
|
||||
def add_page(self, page_name: str, page: QScrollArea) -> Callable[[], None]:
|
||||
"""Add a plugin tab and return an idempotent cleanup callback."""
|
||||
if not isinstance(page_name, str) or not page_name.strip():
|
||||
raise ValueError("Invalid page name")
|
||||
|
||||
if not isinstance(page, QScrollArea):
|
||||
raise ValueError("Page must be a QScrollArea")
|
||||
|
||||
if page_name in self._tabs:
|
||||
raise ValueError("Page name already exists")
|
||||
if page in self._tabs.values():
|
||||
raise ValueError("Page is already registered")
|
||||
|
||||
self._tabs[page_name] = page
|
||||
self.load_profile_page_for_tabs()
|
||||
|
||||
def remove_page():
|
||||
# An old cleanup callback must not remove a replacement tab.
|
||||
if self._tabs.get(page_name) is page:
|
||||
self.remove_page(page_name)
|
||||
|
||||
return remove_page
|
||||
|
||||
def remove_page(self, page_name: str) -> bool:
|
||||
if page_name == "Profile settings":
|
||||
raise ValueError("The profile settings page cannot be removed")
|
||||
page = self._tabs.pop(page_name, None)
|
||||
if page is None:
|
||||
return False
|
||||
index = self.profile_tabs.indexOf(page)
|
||||
if index >= 0:
|
||||
self.profile_tabs.removeTab(index)
|
||||
page.hide()
|
||||
page.deleteLater()
|
||||
return True
|
||||
|
||||
def register_plugin_apis(self, plugin_manager):
|
||||
plugin_manager.register_api("launcher.manage_profile_page.add_page", self.add_page)
|
||||
plugin_manager.register_api("launcher.manage_profile_page.remove_page", self.remove_page)
|
||||
plugin_manager.register_api("launcher.manage_profile_page.subscribe_profile", self.subscribe_profile)
|
||||
|
||||
def subscribe_profile(self, callback: Callable) -> Callable[[], None]:
|
||||
"""Send saved details for the editor selection, including the initial state."""
|
||||
if not callable(callback):
|
||||
raise TypeError("Profile callback must be callable")
|
||||
self.selected_profile_changed.connect(callback)
|
||||
connected = True
|
||||
|
||||
def cleanup():
|
||||
nonlocal connected
|
||||
if connected:
|
||||
self.selected_profile_changed.disconnect(callback)
|
||||
connected = False
|
||||
|
||||
try:
|
||||
detail = (self.profile_manager.get_profile(self.selected_profile_id)
|
||||
if self.selected_profile_id is not None else None)
|
||||
callback(detail)
|
||||
except BaseException:
|
||||
cleanup()
|
||||
raise
|
||||
return cleanup
|
||||
@@ -0,0 +1,413 @@
|
||||
from pathlib import Path
|
||||
|
||||
from PySide6.QtCore import Qt, QSize, QUrl
|
||||
from PySide6.QtGui import QDesktopServices, QIcon, QPixmap, QMovie
|
||||
from PySide6.QtWidgets import (
|
||||
QWidget, QDialog, QVBoxLayout, QHBoxLayout, QLabel, QLineEdit,
|
||||
QListWidget, QListWidgetItem, QPushButton, QComboBox, QSplitter,
|
||||
QStackedWidget, QScrollArea, QFormLayout, QDialogButtonBox, QSizePolicy, QTextBrowser, QFrame,
|
||||
)
|
||||
|
||||
from manager.plugins import Plugin
|
||||
|
||||
|
||||
class PluginsDialog(QDialog):
|
||||
def __init__(self, app, plugin_manager, parent=None):
|
||||
super().__init__(parent)
|
||||
self.app = app
|
||||
self.plugin_manager = plugin_manager
|
||||
self.icon_movies: list[QMovie] = []
|
||||
self.default_plugin_icon = self.app.get_icon(Path(self.app.icon_dir, "plugin.png"))
|
||||
self.setWindowIcon(self.default_plugin_icon)
|
||||
self.setObjectName("pluginsDialog")
|
||||
self.resize(960, 620)
|
||||
self.setMinimumSize(720, 480)
|
||||
|
||||
layout = QVBoxLayout(self)
|
||||
layout.setContentsMargins(20, 16, 20, 16)
|
||||
layout.setSpacing(14)
|
||||
|
||||
header = QHBoxLayout()
|
||||
self.title_label = QLabel()
|
||||
self.title_label.setObjectName("pluginsTitle")
|
||||
self.reload_button = QPushButton()
|
||||
self.reload_button.setIcon(self.app.get_icon(Path(self.app.icon_dir, "refresh.png")))
|
||||
self.folder_button = QPushButton()
|
||||
self.folder_button.setIcon(self.app.get_icon(Path(self.app.icon_dir, "file.png")))
|
||||
|
||||
header.addWidget(self.title_label)
|
||||
header.addStretch()
|
||||
header.addWidget(self.folder_button)
|
||||
header.addWidget(self.reload_button)
|
||||
layout.addLayout(header)
|
||||
|
||||
# Split plugin list and details
|
||||
splitter = QSplitter(Qt.Orientation.Horizontal)
|
||||
splitter.setChildrenCollapsible(False)
|
||||
|
||||
sidebar = QWidget()
|
||||
sidebar.setMinimumWidth(260)
|
||||
sidebar_layout = QVBoxLayout(sidebar)
|
||||
sidebar_layout.setContentsMargins(0, 0, 12, 0)
|
||||
sidebar_layout.setSpacing(10)
|
||||
|
||||
# Label
|
||||
self.installed_label = QLabel()
|
||||
self.installed_label.setObjectName("pluginsSectionTitle")
|
||||
|
||||
self.search_edit = QLineEdit()
|
||||
self.search_edit.setClearButtonEnabled(True)
|
||||
self.filter_combo = QComboBox()
|
||||
# Plugin state filter
|
||||
for state in ("all", "enabled", "disabled"):
|
||||
self.filter_combo.addItem("", state)
|
||||
|
||||
# Plugin list
|
||||
self.plugin_list = QListWidget()
|
||||
self.plugin_list.setObjectName("pluginList")
|
||||
self.plugin_list.setIconSize(QSize(32, 32))
|
||||
self.plugin_list.setSpacing(3)
|
||||
self.plugin_list.setWordWrap(True)
|
||||
self.plugin_list.setHorizontalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAlwaysOff)
|
||||
|
||||
self.count_label = QLabel()
|
||||
self.count_label.setObjectName("pluginsMuted")
|
||||
|
||||
# Bind widgets
|
||||
sidebar_layout.addWidget(self.installed_label)
|
||||
sidebar_layout.addWidget(self.search_edit)
|
||||
sidebar_layout.addWidget(self.filter_combo)
|
||||
sidebar_layout.addWidget(self.plugin_list, 1)
|
||||
sidebar_layout.addWidget(self.count_label)
|
||||
splitter.addWidget(sidebar)
|
||||
|
||||
# Stack widget (For plugin details)
|
||||
self.details_stack = QStackedWidget()
|
||||
|
||||
empty_page = QWidget()
|
||||
empty_layout = QVBoxLayout(empty_page)
|
||||
empty_layout.addStretch()
|
||||
|
||||
self.empty_title = QLabel()
|
||||
self.empty_title.setObjectName("pluginsEmptyTitle")
|
||||
self.empty_title.setAlignment(Qt.AlignmentFlag.AlignCenter)
|
||||
self.empty_text = QLabel()
|
||||
self.empty_text.setObjectName("pluginsMuted")
|
||||
self.empty_text.setWordWrap(True)
|
||||
self.empty_text.setAlignment(Qt.AlignmentFlag.AlignCenter)
|
||||
|
||||
empty_layout.addWidget(self.empty_title)
|
||||
empty_layout.addWidget(self.empty_text)
|
||||
empty_layout.addStretch()
|
||||
self.details_stack.addWidget(empty_page)
|
||||
|
||||
# Scroll area for plugin details
|
||||
scroll = QScrollArea()
|
||||
scroll.setWidgetResizable(True)
|
||||
scroll.setHorizontalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAlwaysOff)
|
||||
scroll.setFrameShape(QScrollArea.Shape.NoFrame)
|
||||
|
||||
details = QWidget()
|
||||
details_layout = QVBoxLayout(details)
|
||||
details_layout.setContentsMargins(24, 20, 20, 20)
|
||||
details_layout.setSpacing(18)
|
||||
|
||||
# Plugin identity inside the details layout
|
||||
identity = QHBoxLayout()
|
||||
self.plugin_icon = QLabel()
|
||||
self.plugin_icon.setObjectName("pluginIcon")
|
||||
self.plugin_icon.setAlignment(Qt.AlignmentFlag.AlignCenter)
|
||||
self.plugin_icon.setFixedSize(56, 56)
|
||||
self.plugin_icon.setPixmap(self.default_plugin_icon.pixmap(QSize(48, 48)))
|
||||
|
||||
identity.addWidget(self.plugin_icon)
|
||||
identity_text = QVBoxLayout()
|
||||
|
||||
self.name_label = QLabel()
|
||||
self.name_label.setObjectName("pluginName")
|
||||
self.name_label.setWordWrap(True)
|
||||
self.group_label = QLabel()
|
||||
self.group_label.setObjectName("pluginsMuted")
|
||||
self.group_label.setWordWrap(True)
|
||||
|
||||
identity_text.addWidget(self.name_label)
|
||||
identity_text.addWidget(self.group_label)
|
||||
identity.addLayout(identity_text, 1)
|
||||
details_layout.addLayout(identity)
|
||||
|
||||
# Status of the plugin
|
||||
self.status_label = QLabel()
|
||||
self.status_label.setObjectName("pluginsMuted")
|
||||
details_layout.addWidget(self.status_label)
|
||||
|
||||
# Plugin description
|
||||
self.overview_label = QLabel()
|
||||
self.overview_label.setObjectName("pluginsSectionTitle")
|
||||
self.description_browser = QTextBrowser() # use QTextBrowser for Markdown support
|
||||
self.description_browser.setFrameShape(QFrame.Shape.NoFrame)
|
||||
details_layout.addWidget(self.overview_label)
|
||||
details_layout.addWidget(self.description_browser)
|
||||
|
||||
# Metadata
|
||||
metadata = QFormLayout()
|
||||
metadata.setHorizontalSpacing(24)
|
||||
metadata.setVerticalSpacing(14)
|
||||
metadata.setFieldGrowthPolicy(QFormLayout.FieldGrowthPolicy.AllNonFixedFieldsGrow)
|
||||
metadata.setRowWrapPolicy(QFormLayout.RowWrapPolicy.WrapLongRows)
|
||||
|
||||
self.field_labels = {}
|
||||
self.field_values = {}
|
||||
|
||||
for key in ("Version", "Plugin ID", "Group", "Required API version", "Location"):
|
||||
title = QLabel()
|
||||
title.setObjectName("pluginsMuted")
|
||||
value = QLabel()
|
||||
value.setWordWrap(True)
|
||||
value.setMinimumWidth(0)
|
||||
value.setSizePolicy(QSizePolicy.Policy.Ignored, QSizePolicy.Policy.Preferred)
|
||||
value.setTextFormat(Qt.TextFormat.PlainText)
|
||||
value.setTextInteractionFlags(Qt.TextInteractionFlag.TextSelectableByMouse)
|
||||
metadata.addRow(title, value)
|
||||
self.field_labels[key] = title
|
||||
self.field_values[key] = value
|
||||
|
||||
details_layout.addLayout(metadata)
|
||||
|
||||
# Buttons
|
||||
self.open_plugin_button = QPushButton()
|
||||
self.open_plugin_button.setIcon(self.app.get_icon(Path(self.app.icon_dir, "file.png")))
|
||||
self.toggle_plugin_button = QPushButton()
|
||||
|
||||
details_layout.addWidget(self.open_plugin_button, 0, Qt.AlignmentFlag.AlignLeft)
|
||||
details_layout.addWidget(self.toggle_plugin_button, 0, Qt.AlignmentFlag.AlignLeft)
|
||||
details_layout.addStretch()
|
||||
|
||||
scroll.setWidget(details)
|
||||
self.details_stack.addWidget(scroll)
|
||||
|
||||
splitter.addWidget(self.details_stack)
|
||||
splitter.setStretchFactor(0, 0)
|
||||
splitter.setStretchFactor(1, 1)
|
||||
splitter.setSizes([310, 610])
|
||||
layout.addWidget(splitter, 1)
|
||||
self.buttons = QDialogButtonBox(QDialogButtonBox.StandardButton.Close)
|
||||
self.buttons.rejected.connect(self.reject)
|
||||
layout.addWidget(self.buttons)
|
||||
|
||||
# Plugin metadata is plain text, never HTML.
|
||||
for label in (self.name_label, self.group_label, self.plugin_icon):
|
||||
label.setTextFormat(Qt.TextFormat.PlainText)
|
||||
|
||||
# Bind events
|
||||
self.search_edit.textChanged.connect(self.refresh_plugins)
|
||||
self.filter_combo.currentIndexChanged.connect(self.refresh_plugins)
|
||||
self.plugin_list.currentItemChanged.connect(self._show_plugin_details)
|
||||
self.reload_button.clicked.connect(self.reload_plugins)
|
||||
self.folder_button.clicked.connect(self._open_plugins_directory)
|
||||
self.open_plugin_button.clicked.connect(self._open_selected_directory)
|
||||
self.toggle_plugin_button.clicked.connect(self._toggle_selected_plugin)
|
||||
self.plugin_manager.signals.plugins_loaded.connect(self.refresh_plugins)
|
||||
self.plugin_manager.signals.plugin_state_changed.connect(self.refresh_plugins)
|
||||
self.app.i18n.language_changed.connect(self.retranslate_ui)
|
||||
|
||||
# Apply qss
|
||||
self.app.apply_qss(Path("plugins.qss"), self.setStyleSheet)
|
||||
|
||||
# Apply translations
|
||||
self.retranslate_ui()
|
||||
|
||||
def retranslate_ui(self, *_args):
|
||||
tr = self.app.tr_text
|
||||
|
||||
self.setWindowTitle(tr("Plugins"))
|
||||
|
||||
self.title_label.setText(tr("Plugins"))
|
||||
self.installed_label.setText(tr("Installed"))
|
||||
self.reload_button.setText(tr("Reload plugins"))
|
||||
self.folder_button.setText(tr("Plugins folder"))
|
||||
|
||||
self.search_edit.setPlaceholderText(tr("Search plugins…"))
|
||||
self.search_edit.setAccessibleName(tr("Search plugins…"))
|
||||
self.filter_combo.setAccessibleName(tr("Filter plugins"))
|
||||
|
||||
for index, text in enumerate(("All plugins", "Enabled", "Disabled")):
|
||||
self.filter_combo.setItemText(index, tr(text))
|
||||
|
||||
self.overview_label.setText(tr("Overview"))
|
||||
self.description_browser.setText(tr("This plugin does not provide a description."))
|
||||
|
||||
self.open_plugin_button.setText(tr("Open plugin folder"))
|
||||
self.buttons.button(QDialogButtonBox.StandardButton.Close).setText(tr("Close"))
|
||||
|
||||
for key, label in self.field_labels.items():
|
||||
label.setText(tr(key))
|
||||
|
||||
self.refresh_plugins()
|
||||
|
||||
def refresh_plugins(self, *_args):
|
||||
current = self.plugin_list.currentItem()
|
||||
|
||||
selected_path = current.data(Qt.ItemDataRole.UserRole).path if current else None
|
||||
query = self.search_edit.text().strip().casefold()
|
||||
state = self.filter_combo.currentData()
|
||||
|
||||
plugins = sorted(self.plugin_manager.plugins, key=lambda entry: entry.name.casefold())
|
||||
for movie in self.icon_movies:
|
||||
movie.stop()
|
||||
movie.frameChanged.disconnect()
|
||||
movie.error.disconnect()
|
||||
movie.deleteLater()
|
||||
self.icon_movies.clear()
|
||||
self.plugin_list.blockSignals(True)
|
||||
self.plugin_list.clear()
|
||||
|
||||
selected_item = None
|
||||
for plugin in plugins:
|
||||
if query and query not in f"{plugin.name} {plugin.group} {plugin.id}".casefold():
|
||||
continue
|
||||
if (state == "enabled" and not plugin.enabled) or (state == "disabled" and plugin.enabled):
|
||||
continue
|
||||
item = QListWidgetItem(f"{plugin.name}\n{plugin.group} · {plugin.version}")
|
||||
item.setIcon(self.default_plugin_icon)
|
||||
item.setSizeHint(QSize(240, 70))
|
||||
item.setData(Qt.ItemDataRole.UserRole, plugin)
|
||||
self.plugin_list.addItem(item)
|
||||
self._load_plugin_icon(item, plugin)
|
||||
if plugin.path == selected_path:
|
||||
selected_item = item
|
||||
|
||||
if self.plugin_list.count():
|
||||
self.plugin_list.setCurrentItem(selected_item or self.plugin_list.item(0))
|
||||
|
||||
self.plugin_list.blockSignals(False)
|
||||
self.count_label.setText(self.app.tr_text(
|
||||
"{visible} of {total} plugins", visible=self.plugin_list.count(), total=len(plugins),
|
||||
))
|
||||
self._show_plugin_details(self.plugin_list.currentItem())
|
||||
|
||||
def _show_plugin_details(self, item, _previous=None):
|
||||
tr = self.app.tr_text
|
||||
if item is None:
|
||||
self.toggle_plugin_button.setEnabled(False)
|
||||
self.details_stack.setCurrentIndex(0)
|
||||
if self.plugin_manager.plugins:
|
||||
self.empty_title.setText(tr("No matching plugins"))
|
||||
self.empty_text.setText(tr("Try another search or change the filter."))
|
||||
else:
|
||||
self.empty_title.setText(tr("No plugins loaded"))
|
||||
self.empty_text.setText(tr("Add plugins to your plugins folder, then click Reload plugins."))
|
||||
return
|
||||
|
||||
plugin: Plugin = item.data(Qt.ItemDataRole.UserRole)
|
||||
|
||||
if plugin.description:
|
||||
self.description_browser.setMarkdown(plugin.description)
|
||||
else:
|
||||
self.description_browser.setText(tr("This plugin does not provide a description."))
|
||||
|
||||
self.toggle_plugin_button.setText(tr("Disable plugin") if plugin.enabled else tr("Enable plugin"))
|
||||
self.toggle_plugin_button.setEnabled(not plugin.is_changing_state)
|
||||
self.details_stack.setCurrentIndex(1)
|
||||
self.plugin_icon.setPixmap(item.icon().pixmap(QSize(48, 48)))
|
||||
self.name_label.setText(plugin.name)
|
||||
self.group_label.setText(plugin.group)
|
||||
self.status_label.setText(tr("Enabled") if plugin.enabled else tr("Disabled"))
|
||||
values = {
|
||||
"Version": plugin.version,
|
||||
"Plugin ID": plugin.id,
|
||||
"Group": plugin.group,
|
||||
"Required API version": str(plugin.api_version_range),
|
||||
"Location": str(plugin.path),
|
||||
}
|
||||
|
||||
for key, value in values.items():
|
||||
self.field_values[key].setText(value)
|
||||
|
||||
def _toggle_selected_plugin(self):
|
||||
item: QListWidgetItem | None = self.plugin_list.currentItem()
|
||||
if item is None:
|
||||
return
|
||||
plugin: Plugin = item.data(Qt.ItemDataRole.UserRole)
|
||||
self.toggle_plugin_button.setEnabled(False)
|
||||
try:
|
||||
self.plugin_manager.set_plugin_enabled(plugin, not plugin.enabled)
|
||||
finally:
|
||||
self._show_plugin_details(self.plugin_list.currentItem())
|
||||
|
||||
def _load_plugin_icon(self, item: QListWidgetItem, plugin: Plugin):
|
||||
path = plugin.icon_path
|
||||
if path is None or path.suffix.lower() not in (".gif", ".png", ".jpg", ".jpeg", ".bmp"):
|
||||
return
|
||||
|
||||
# Load custom artwork directly to preserve its original colors.
|
||||
pixmap = QPixmap(str(path))
|
||||
if pixmap.isNull():
|
||||
return
|
||||
item.setIcon(QIcon(pixmap))
|
||||
if path.suffix.lower() != ".gif":
|
||||
return
|
||||
|
||||
movie = QMovie(str(path), parent=self)
|
||||
if not movie.isValid():
|
||||
movie.deleteLater()
|
||||
return
|
||||
movie.setScaledSize(pixmap.size().scaled(QSize(48, 48), Qt.AspectRatioMode.KeepAspectRatio))
|
||||
movie.frameChanged.connect(lambda _frame, target=item, animation=movie:
|
||||
self._update_plugin_movie(target, animation))
|
||||
movie.error.connect(lambda _error, target=item, animation=movie:
|
||||
self._stop_plugin_movie(target, animation))
|
||||
self.icon_movies.append(movie)
|
||||
movie.jumpToFrame(0)
|
||||
if self.isVisible():
|
||||
movie.start()
|
||||
|
||||
def _update_plugin_movie(self, item: QListWidgetItem, movie: QMovie):
|
||||
pixmap = movie.currentPixmap()
|
||||
if pixmap.isNull():
|
||||
return
|
||||
item.setIcon(QIcon(pixmap))
|
||||
if self.plugin_list.currentItem() is item:
|
||||
self.plugin_icon.setPixmap(pixmap)
|
||||
|
||||
def _stop_plugin_movie(self, item: QListWidgetItem, movie: QMovie):
|
||||
movie.stop()
|
||||
item.setIcon(self.default_plugin_icon)
|
||||
if self.plugin_list.currentItem() is item:
|
||||
self.plugin_icon.setPixmap(self.default_plugin_icon.pixmap(QSize(48, 48)))
|
||||
|
||||
def showEvent(self, event):
|
||||
super().showEvent(event)
|
||||
for movie in self.icon_movies:
|
||||
movie.start()
|
||||
|
||||
def hideEvent(self, event):
|
||||
for movie in self.icon_movies:
|
||||
movie.stop()
|
||||
super().hideEvent(event)
|
||||
|
||||
def reload_plugins(self):
|
||||
self.reload_button.setEnabled(False)
|
||||
try:
|
||||
self.plugin_manager.signals.reload_plugins.emit()
|
||||
finally:
|
||||
self.reload_button.setEnabled(True)
|
||||
|
||||
def _open_directory(self, path: Path):
|
||||
try:
|
||||
path.mkdir(parents=True, exist_ok=True)
|
||||
if not QDesktopServices.openUrl(QUrl.fromLocalFile(str(path.resolve()))):
|
||||
raise OSError(self.app.tr_text("Unable to open folder: {path}", path=path))
|
||||
except OSError as e:
|
||||
self.app.widget_manager.signal.show_and_print_error_message.emit({
|
||||
"title": self.app.tr_text("Plugin Error"),
|
||||
"message": str(e),
|
||||
"error": str(e),
|
||||
})
|
||||
|
||||
def _open_plugins_directory(self):
|
||||
self._open_directory(self.app.plugins_dir)
|
||||
|
||||
def _open_selected_directory(self):
|
||||
item = self.plugin_list.currentItem()
|
||||
if item:
|
||||
self._open_directory(item.data(Qt.ItemDataRole.UserRole).path)
|
||||
+15
-2
@@ -15,9 +15,10 @@ from PySide6.QtWidgets import (
|
||||
QWidget, QScrollArea, QSizePolicy, QSpinBox,
|
||||
)
|
||||
|
||||
from constant import LAUNCHER_DESCRIPTION, LAUNCHER_NAME, LAUNCHER_VERSION
|
||||
from constant import LAUNCHER_DESCRIPTION, LAUNCHER_NAME, LAUNCHER_VERSION, LAUNCHER_DESCRIPTION_KEYWORD
|
||||
from core_lib.qt import messagebox
|
||||
from manager.settings_models import AccountSettings, AppearanceSettings, GeneralSettings
|
||||
from pages.plugins import PluginsDialog
|
||||
|
||||
LANGUAGE_LABELS = {
|
||||
"en": "English",
|
||||
@@ -201,10 +202,20 @@ class SettingsPage(QWidget):
|
||||
)
|
||||
scroll_layout.addWidget(button)
|
||||
|
||||
plugins_button = SettingsItem(tr("Plugins"), tr("Browse installed plugins"))
|
||||
plugins_button.clicked.connect(self._open_plugins_dialog)
|
||||
scroll_layout.addWidget(plugins_button)
|
||||
scroll_layout.addStretch()
|
||||
layout.addWidget(scroll_area)
|
||||
return page
|
||||
|
||||
def _open_plugins_dialog(self):
|
||||
dialog = PluginsDialog(self.app, self.app.plugin_manager, self)
|
||||
try:
|
||||
dialog.exec()
|
||||
finally:
|
||||
dialog.deleteLater()
|
||||
|
||||
def _create_general_page(self) -> QWidget:
|
||||
tr = self.app.tr_text
|
||||
page = SettingsSubPage(tr("General"), self._go_back)
|
||||
@@ -309,7 +320,9 @@ class SettingsPage(QWidget):
|
||||
brand_label.setObjectName("settingsBrand")
|
||||
version_label = QLabel(tr("Version: {version}", version=LAUNCHER_VERSION))
|
||||
version_label.setObjectName("settingsVersion")
|
||||
description = QPlainTextEdit(LAUNCHER_DESCRIPTION)
|
||||
|
||||
description_text = self.app.tr_text(LAUNCHER_DESCRIPTION_KEYWORD)
|
||||
description = QPlainTextEdit(description_text if description_text != LAUNCHER_DESCRIPTION_KEYWORD else LAUNCHER_DESCRIPTION)
|
||||
description.setObjectName("settingsDescription")
|
||||
description.setReadOnly(True)
|
||||
|
||||
|
||||
Reference in new issue
Block a user