Refactor settings and widget object name

Signed-off-by: Loren Eteval <loren.eteval@proton.me>
This commit is contained in:
Loren Eteval
2023-08-29 12:44:36 +08:00
parent dad42496ab
commit 8695377245
13 changed files with 91 additions and 88 deletions
+6 -11
View File
@@ -272,11 +272,6 @@ class ConnectAction(Action):
self.connectingFlag = False
@property
def MainWidget(self):
# Handy reference
return APP().MainWidget
@property
def activatedServer(self):
try:
@@ -285,7 +280,7 @@ class ConnectAction(Action):
if activatedIndex < 0:
return None
else:
return self.MainWidget.ServerList[activatedIndex]['config']
return APP().ServerWidget.ServerList[activatedIndex]['config']
except Exception:
# Any non-exit exceptions
@@ -442,9 +437,9 @@ class ConnectAction(Action):
self.coreJSON['routing'] = self.XrayRouting
else:
try:
routingWidget = APP().editRoutingWidget
routesWidget = APP().RoutesWidget
route = routingWidget.RoutesList[int(routing)]
route = routesWidget.RoutesList[int(routing)]
logger.info(f'routing is {route["remark"]}')
logger.info(f'RoutingObject: {route[XrayCore.name()]}')
@@ -517,9 +512,9 @@ class ConnectAction(Action):
)
else:
try:
routingWidget = APP().editRoutingWidget
routesWidget = APP().RoutesWidget
route = routingWidget.RoutesList[int(routing)]
route = routesWidget.RoutesList[int(routing)]
logger.info(f'routing is {route["remark"]}')
logger.info(f'RoutingObject: {route[Hysteria.name()]}')
@@ -615,7 +610,7 @@ class ConnectAction(Action):
# Connecting
self.connectingFlag = True
if not APP().Configuration or len(self.MainWidget.ServerList) == 0:
if not APP().Configuration or len(APP().ServerWidget.ServerList) == 0:
APP().Connect = Switch.OFF
self.setChecked(False)
+1 -1
View File
@@ -12,4 +12,4 @@ class EditConfigurationAction(Action):
)
def triggeredCallback(self, checked):
APP().MainWidget.show()
APP().ServerWidget.show()
+5 -5
View File
@@ -111,7 +111,7 @@ class ExportJSONResultBox(MessageBox):
def exportLink(selectedIndex):
serverList = APP().MainWidget.ServerList
serverList = APP().ServerWidget.ServerList
serverLink = []
successStr = []
failureStr = []
@@ -141,7 +141,7 @@ class ExportLinkAction(Action):
self.exportLinkResult = ExportLinkResultBox('', '', isQRCodeExport=False)
def triggeredCallback(self, checked):
selectedIndex = APP().MainWidget.selectedIndex
selectedIndex = APP().ServerWidget.selectedIndex
if len(selectedIndex) == 0:
# Nothing selected. Do nothing
@@ -167,7 +167,7 @@ class ExportQRCodeAction(Action):
self.exportLinkResult = ExportLinkResultBox('', '', isQRCodeExport=True)
def triggeredCallback(self, checked):
selectedIndex = APP().MainWidget.selectedIndex
selectedIndex = APP().ServerWidget.selectedIndex
if len(selectedIndex) == 0:
# Nothing selected. Do nothing
@@ -195,7 +195,7 @@ class ExportJSONAction(Action):
self.exportJSONResult = ExportJSONResultBox()
def triggeredCallback(self, checked):
selectedIndex = APP().MainWidget.selectedIndex
selectedIndex = APP().ServerWidget.selectedIndex
if len(selectedIndex) == 0:
# Nothing selected. Do nothing
@@ -204,7 +204,7 @@ class ExportJSONAction(Action):
try:
QApplication.clipboard().setText(
'\n'.join(
APP().MainWidget.ServerList[index]['config']
APP().ServerWidget.ServerList[index]['config']
for index in selectedIndex
)
)
+8 -8
View File
@@ -146,7 +146,7 @@ class ImportLinkAction(Action):
if choice == MessageBox.ButtonRole.AcceptRole.value:
# Go to edit
APP().MainWidget.show()
APP().ServerWidget.show()
else:
# OK. Do nothing
pass
@@ -187,7 +187,7 @@ class ImportLinkAction(Action):
remark = urllib.parse.unquote(getOrDefault('ps'))
APP().MainWidget.importServer(
APP().ServerWidget.importServer(
remark,
ujson.dumps(
myJSON,
@@ -247,7 +247,7 @@ class ImportLinkAction(Action):
)
)
APP().MainWidget.importServer(
APP().ServerWidget.importServer(
remark,
ujson.dumps(
myJSON, indent=2, ensure_ascii=False, escape_forward_slashes=False
@@ -298,7 +298,7 @@ class ImportLinkAction(Action):
)
)
APP().MainWidget.importServer(
APP().ServerWidget.importServer(
remark,
ujson.dumps(
myJSON, indent=2, ensure_ascii=False, escape_forward_slashes=False
@@ -324,7 +324,7 @@ class ImportLinkAction(Action):
ProxyOutboundObjectSS(*re.split(r'[@:]', myData))
)
APP().MainWidget.importServer(
APP().ServerWidget.importServer(
'sslegacy',
ujson.dumps(
myJSON, indent=2, ensure_ascii=False, escape_forward_slashes=False
@@ -389,7 +389,7 @@ class ImportLinkAction(Action):
else:
successRemark = []
rowCount = APP().MainWidget.rowCount
rowCount = APP().ServerWidget.rowCount
for shareLink in splitByNewLine:
remark, result = self.parseShareLink(shareLink)
@@ -446,7 +446,7 @@ class ImportJSONAction(Action):
if choice == MessageBox.ButtonRole.AcceptRole.value:
# Go to edit
APP().MainWidget.show()
APP().ServerWidget.show()
else:
# OK. Do nothing
pass
@@ -461,7 +461,7 @@ class ImportJSONAction(Action):
self.showJSONErrorBox()
else:
APP().MainWidget.importServer(
APP().ServerWidget.importServer(
_('Untitled'), self.clipboard, syncStorage=True
)
+6 -8
View File
@@ -147,12 +147,10 @@ class BuiltinRoutingChildAction(Action):
if APP().Routing != textEnglish:
# De-activate
APP().editRoutingWidget.activateItemByIndex(
routingToIndex(), activate=False
)
APP().RoutesWidget.activateItemByIndex(routingToIndex(), activate=False)
APP().Routing = textEnglish
# Activate
APP().editRoutingWidget.activateItemByIndex(routingToIndex(), activate=True)
APP().RoutesWidget.activateItemByIndex(routingToIndex(), activate=True)
if APP().tray.ConnectAction.isConnected():
# Connected. Re-configure connection
@@ -181,15 +179,15 @@ class RoutingChildAction(Action):
# Found action
if APP().Routing != str(index):
route = APP().editRoutingWidget.RoutesList[index]
route = APP().RoutesWidget.RoutesList[index]
# De-activate
APP().editRoutingWidget.activateItemByIndex(
APP().RoutesWidget.activateItemByIndex(
routingToIndex(), activate=False
)
APP().Routing = str(index)
# Activate
APP().editRoutingWidget.activateItemByIndex(
APP().RoutesWidget.activateItemByIndex(
routingToIndex(), activate=True
)
@@ -236,7 +234,7 @@ class RoutingAction(Action):
checkable=True,
checked=APP().Routing == str(index),
)
for index, route in enumerate(APP().editRoutingWidget.RoutesList)
for index, route in enumerate(APP().RoutesWidget.RoutesList)
),
),
)
+1 -1
View File
@@ -31,7 +31,7 @@ class SettingsChildAction(Action):
else:
APP().ShowTabAndSpacesInEditor = Switch.OFF
APP().MainWidget.showTabAndSpacesIfNecessary()
APP().ServerWidget.showTabAndSpacesIfNecessary()
class SettingsAction(Action):
+10 -8
View File
@@ -65,18 +65,20 @@ SUPPORTED_SETTINGS = (
Settings('Configuration'),
# User Activated Server Index
Settings('ActivatedItemIndex'),
# Main Widget Window Size
Settings('MainWidgetWindowSize'),
# Edit Routing Widget Window Size
# Server Widget Window Size
Settings('ServerWidgetWindowSize'),
# Routes Widget Window Size
Settings('RoutesWidgetWindowSize'),
# Main Widget Section Size
# Server Widget Section Size
Settings('ServerWidgetSectionSizeTable'),
# Edit Routing Widget Section Size
# Routes Widget Section Size
Settings('RoutesWidgetSectionSizeTable'),
# Main Widget Font Point Size
Settings('EditorWidgetPointSize'),
# Server Widget Font Point Size
Settings('ServerWidgetPointSize'),
# Log Viewer Widget Font Point Size
Settings('ViewerWidgetPointSize'),
Settings('LogViewerWidgetPointSize'),
# Tor Viewer Widget Font Point Size
Settings('TorViewerWidgetPointSize'),
# User selected language
Settings('Language', SUPPORTED_LANGUAGE, SMART_CHOSEN_LANGUAGE),
# Startup On Boot
+2 -4
View File
@@ -57,7 +57,7 @@ class ServerStorage:
def sync(ob=None):
if ob is None:
# Object is up-to-date
APP().Configuration = ServerStorage.toStorage(APP().MainWidget.StorageObj)
APP().Configuration = ServerStorage.toStorage(APP().ServerWidget.StorageObj)
else:
# Object is up-to-date
APP().Configuration = ServerStorage.toStorage(ob)
@@ -94,9 +94,7 @@ class RoutesStorage:
def sync(ob=None):
if ob is None:
# Object is up-to-date
APP().CustomRouting = RoutesStorage.toStorage(
APP().editRoutingWidget.StorageObj
)
APP().CustomRouting = RoutesStorage.toStorage(APP().RoutesWidget.StorageObj)
else:
# Object is up-to-date
APP().CustomRouting = RoutesStorage.toStorage(ob)
+6 -7
View File
@@ -141,8 +141,6 @@ class Application(SingletonApplication):
self.customFontEnabled = False
self.customFontName = ''
self.editRoutingWidget = None
self.logViewerWidget = None
self.logViewerHandle = None
self.logStreamHandle = None
@@ -150,7 +148,8 @@ class Application(SingletonApplication):
self.themeDetector = None
self.themeListenerThread = None
self.MainWidget = None
self.ServerWidget = None
self.RoutesWidget = None
def __getattr__(self, key):
try:
@@ -246,8 +245,8 @@ class Application(SingletonApplication):
self.aboutToQuit.connect(self.cleanup)
self.MainWidget = EditConfigurationWidget()
self.editRoutingWidget = EditRoutingWidget()
self.ServerWidget = EditConfigurationWidget()
self.RoutesWidget = EditRoutingWidget()
self.tray = SystemTrayIcon()
self.tray.show()
@@ -267,8 +266,8 @@ class Application(SingletonApplication):
NeedSyncSettings.syncAll()
def exit(self, exitcode=0):
if self.MainWidget is not None:
if self.MainWidget.questionSave():
if self.ServerWidget is not None:
if self.ServerWidget.questionSave():
super().exit(exitcode)
else:
super().exit(exitcode)
+16 -16
View File
@@ -224,7 +224,7 @@ class ScrollToActivatedServerAction(Action):
)
def triggeredCallback(self, checked):
if APP().MainWidget.modified:
if APP().ServerWidget.modified:
self.parent().saveChangeFirst.exec()
return
@@ -605,7 +605,7 @@ class RoutingAction(Action):
super().__init__(_('Edit Routing...'), **kwargs)
def triggeredCallback(self, checked):
APP().editRoutingWidget.show()
APP().RoutesWidget.show()
class ShowLogAction(Action):
@@ -878,7 +878,7 @@ class NormalServerHorizontalHeader(HeaderView):
@QtCore.Slot(int)
def handleSectionClicked(self, clickedIndex):
if APP().MainWidget.modified:
if APP().ServerWidget.modified:
self.parent().saveChangeFirst.exec()
return
@@ -1322,7 +1322,7 @@ class NormalServerWidget(Translatable, SupportConnectedCallback, TableWidget):
self.swapItem(index, index - 1)
def moveUpSelectedItem(self):
if APP().MainWidget.modified:
if APP().ServerWidget.modified:
self.saveChangeFirst.exec()
return
@@ -1365,7 +1365,7 @@ class NormalServerWidget(Translatable, SupportConnectedCallback, TableWidget):
self.swapItem(index, index + 1)
def moveDownSelectedItem(self):
if APP().MainWidget.modified:
if APP().ServerWidget.modified:
self.saveChangeFirst.exec()
return
@@ -1401,7 +1401,7 @@ class NormalServerWidget(Translatable, SupportConnectedCallback, TableWidget):
)
def duplicateSelectedItem(self):
if APP().MainWidget.modified:
if APP().ServerWidget.modified:
self.saveChangeFirst.exec()
return
@@ -1414,13 +1414,13 @@ class NormalServerWidget(Translatable, SupportConnectedCallback, TableWidget):
for index in indexes:
if 0 <= index < len(self.ServerList):
APP().MainWidget.importServer(**self.ServerList[index])
APP().ServerWidget.importServer(**self.ServerList[index])
# Sync it
ServerStorage.sync()
def deleteSelectedItem(self):
if APP().MainWidget.modified:
if APP().ServerWidget.modified:
self.saveChangeFirst.exec()
return
@@ -1519,12 +1519,12 @@ class NormalServerWidget(Translatable, SupportConnectedCallback, TableWidget):
self.saveScrollBarValue(prevRow)
if APP().MainWidget is not None and APP().MainWidget.modified:
if APP().ServerWidget is not None and APP().ServerWidget.modified:
choice = self.questionSaveBox.exec()
if choice == MessageBox.ButtonRole.AcceptRole.value:
# Save
if APP().MainWidget.SaveAsServerAction.save(
if APP().ServerWidget.SaveAsServerAction.save(
successCallback=lambda: self.switchContext(currRow),
):
pass
@@ -1536,7 +1536,7 @@ class NormalServerWidget(Translatable, SupportConnectedCallback, TableWidget):
# Discard
self.switchContext(currRow)
APP().MainWidget.markAsSaved() # Fake saved
APP().ServerWidget.markAsSaved() # Fake saved
elif choice == MessageBox.ButtonRole.RejectRole.value:
# Cancel. Do not switch
@@ -1559,7 +1559,7 @@ class NormalServerWidget(Translatable, SupportConnectedCallback, TableWidget):
# Same item activated. Do nothing
return
if APP().MainWidget is not None and APP().MainWidget.modified:
if APP().ServerWidget is not None and APP().ServerWidget.modified:
self.saveChangeFirst.exec()
return
@@ -1680,7 +1680,7 @@ class EditConfigurationWidget(MainWindow):
try:
font = self.plainTextEdit.font()
font.setPointSize(int(APP().EditorWidgetPointSize))
font.setPointSize(int(APP().ServerWidgetPointSize))
self.plainTextEdit.setFont(font)
except Exception:
@@ -1846,7 +1846,7 @@ class EditConfigurationWidget(MainWindow):
self.setGeometry(
100,
100,
*list(int(size) for size in APP().MainWidgetWindowSize.split(',')),
*list(int(size) for size in APP().ServerWidgetWindowSize.split(',')),
)
except Exception:
# Any non-exit exceptions
@@ -1966,7 +1966,7 @@ class EditConfigurationWidget(MainWindow):
self.questionSave()
def syncSettings(self):
APP().MainWidgetWindowSize = (
APP().ServerWidgetWindowSize = (
f'{self.geometry().width()},{self.geometry().height()}'
)
APP().ServerWidgetSectionSizeTable = ujson.dumps(
@@ -1974,7 +1974,7 @@ class EditConfigurationWidget(MainWindow):
ensure_ascii=False,
escape_forward_slashes=False,
)
APP().EditorWidgetPointSize = str(self.plainTextEdit.font().pointSize())
APP().ServerWidgetPointSize = str(self.plainTextEdit.font().pointSize())
def retranslate(self):
with StateContext(self):
+5 -7
View File
@@ -250,7 +250,7 @@ class ImportAssetFileAction(Action):
super().__init__(_('Import Asset File...'), **kwargs)
def triggeredCallback(self, checked):
APP().editRoutingWidget.assetViewer.show()
APP().RoutesWidget.assetViewer.show()
def questionFastReconnect(saveConfInfo):
@@ -291,9 +291,7 @@ class SaveAction(Action):
def getParentIndex(self):
routingEditor = self.parent()
routingEditorRef = (
APP().editRoutingWidget.editRoutingTableWidget.routingEditorRef
)
routingEditorRef = APP().RoutesWidget.editRoutingTableWidget.routingEditorRef
for index, editor in enumerate(routingEditorRef[len(BUILTIN_ROUTING) :]):
if id(routingEditor) == id(editor):
@@ -349,7 +347,7 @@ class SaveAction(Action):
if parentIndex >= 0:
for core, json in zip(coreList, jsonList):
APP().editRoutingWidget.RoutesList[parentIndex][core] = json
APP().RoutesWidget.RoutesList[parentIndex][core] = json
# Sync it
RoutesStorage.sync()
@@ -830,14 +828,14 @@ class EditRoutingTableWidget(Translatable, SupportConnectedCallback, TableWidget
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.editRoutingWidget = kwargs.get('parent')
self.RoutesWidget = kwargs.get('parent')
self.questionDeleteBox = QuestionDeleteBox(
icon=MessageBox.Icon.Question, parent=self.parent()
)
# Handy reference
self.RoutesList = self.editRoutingWidget.RoutesList
self.RoutesList = self.RoutesWidget.RoutesList
# Currently only has Xray-Core
self.routingEditorRef = []
+24 -11
View File
@@ -150,16 +150,7 @@ class LogViewerWidget(MainWindow):
)
)
try:
# Restore point size
font = self.textBrowser.font()
font.setPointSize(int(APP().ViewerWidgetPointSize))
self.textBrowser.setFont(font)
except Exception:
# Any non-exit exceptions
pass
self.restorePointSize()
self.setCentralWidget(self.textBrowser)
@@ -199,5 +190,27 @@ class LogViewerWidget(MainWindow):
def log(self):
return self.textBrowser.toPlainText()
def pointSizeSetting(self):
return f'{self.__class__.__name__}PointSize'
def restorePointSize(self):
try:
# Restore point size
font = self.textBrowser.font()
font.setPointSize(int(getattr(APP(), self.pointSizeSetting())))
self.textBrowser.setFont(font)
except Exception:
# Any non-exit exceptions
pass
def syncSettings(self):
APP().ViewerWidgetPointSize = str(self.textBrowser.font().pointSize())
setattr(
APP(), self.pointSizeSetting(), str(self.textBrowser.font().pointSize())
)
class TorViewerWidget(LogViewerWidget):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
+1 -1
View File
@@ -89,7 +89,7 @@ class SystemTrayIcon(Translatable, QSystemTrayIcon):
@QtCore.Slot(QSystemTrayIcon.ActivationReason)
def handleActivated(self, reason):
if reason == QSystemTrayIcon.ActivationReason.DoubleClick:
APP().MainWidget.show()
APP().ServerWidget.show()
def setApplicationToolTip(self):
self.setToolTip(f'{_(APPLICATION_NAME)} {APPLICATION_VERSION}')