Add theme change support for AssetViewer

Signed-off-by: Loren Eteval <loren.eteval@proton.me>
This commit is contained in:
Loren Eteval
2023-08-11 18:24:57 +08:00
parent e5038cd9e2
commit 5a4ffa69f4
2 changed files with 43 additions and 24 deletions
+20 -20
View File
@@ -206,26 +206,6 @@ def getAbsolutePath(path):
return path if os.path.isabs(path) else str(ROOT_DIR / path)
def swapListItem(listOrTuple, index0, index1):
swap = listOrTuple[index0]
listOrTuple[index0] = listOrTuple[index1]
listOrTuple[index1] = swap
def moveToCenter(widget, parent=None):
geometry = widget.geometry()
if parent is None:
center = QApplication.primaryScreen().availableGeometry().center()
else:
center = parent.geometry().center()
geometry.moveCenter(center)
widget.move(geometry.topLeft())
def getUbuntuRelease():
try:
result = subprocess.run(
@@ -248,3 +228,23 @@ def getUbuntuRelease():
# Any non-exit exceptions
return ''
def swapListItem(listOrTuple, index0, index1):
swap = listOrTuple[index0]
listOrTuple[index0] = listOrTuple[index1]
listOrTuple[index1] = swap
def moveToCenter(widget, parent=None):
geometry = widget.geometry()
if parent is None:
center = QApplication.primaryScreen().availableGeometry().center()
else:
center = parent.geometry().center()
geometry.moveCenter(center)
widget.move(geometry.topLeft())
+23 -4
View File
@@ -7,6 +7,7 @@ from Furious.Utility.Utility import (
SupportThemeChangedCallback,
bootstrapIcon,
bootstrapIconWhite,
getUbuntuRelease,
moveToCenter,
)
from Furious.Utility.Translator import Translatable, gettext as _
@@ -168,6 +169,11 @@ class AssetViewerWidget(
self.listWidget.setSelectionMode(QListWidget.SelectionMode.ExtendedSelection)
self.listWidget.setIconSize(QtCore.QSize(64, 64))
if PLATFORM == 'Linux' and getUbuntuRelease() == '20.04':
self.initialTheme = darkdetect.theme()
else:
self.initialTheme = None
self.flushItem()
self.setCentralWidget(self.listWidget)
@@ -238,14 +244,14 @@ class AssetViewerWidget(
for index in indexes:
os.remove(AssetViewerWidget.AssetDir / self.listWidget.item(index).text())
def flushItem(self):
def flushItemByTheme(self, theme):
self.listWidget.clear()
for filename in os.listdir(AssetViewerWidget.AssetDir):
if os.path.isfile(AssetViewerWidget.AssetDir / filename):
item = QListWidgetItem(filename)
if darkdetect.theme() == 'Dark':
if theme == 'Dark':
if PLATFORM == 'Windows':
# Windows. Always use black icon
item.setIcon(bootstrapIcon('file-earmark.svg'))
@@ -256,6 +262,15 @@ class AssetViewerWidget(
self.listWidget.addItem(item)
def flushItem(self):
if PLATFORM == 'Linux' and getUbuntuRelease() == '20.04':
assert self.initialTheme is not None
# Ubuntu 20.04. Flush by initial theme
self.flushItemByTheme(self.initialTheme)
else:
self.flushItemByTheme(darkdetect.theme())
def closeEvent(self, event):
event.ignore()
@@ -268,8 +283,12 @@ class AssetViewerWidget(
self.setWindowIcon(bootstrapIcon('rocket-takeoff-window.svg'))
def themeChangedCallback(self, theme):
# TODO
pass
if PLATFORM == 'Linux' and getUbuntuRelease() == '20.04':
# Ubuntu 20.04 system dark theme does not
# change menu color. Do nothing
pass
else:
self.flushItemByTheme(theme)
def retranslate(self):
with StateContext(self):