From 8695377245bb9201bc8bd03c45f628702fa084cd Mon Sep 17 00:00:00 2001 From: Loren Eteval Date: Tue, 29 Aug 2023 12:44:36 +0800 Subject: [PATCH] Refactor settings and widget object name Signed-off-by: Loren Eteval --- Furious/Action/Connect.py | 17 +++++--------- Furious/Action/EditConfiguration.py | 2 +- Furious/Action/Export.py | 10 ++++----- Furious/Action/Import.py | 16 ++++++------- Furious/Action/Routing.py | 14 +++++------- Furious/Action/Settings.py | 2 +- Furious/Utility/Settings.py | 18 ++++++++------- Furious/Utility/Utility.py | 6 ++--- Furious/Widget/Application.py | 13 +++++------ Furious/Widget/EditConfiguration.py | 32 +++++++++++++------------- Furious/Widget/EditRouting.py | 12 +++++----- Furious/Widget/LogViewer.py | 35 ++++++++++++++++++++--------- Furious/Widget/SystemTrayIcon.py | 2 +- 13 files changed, 91 insertions(+), 88 deletions(-) diff --git a/Furious/Action/Connect.py b/Furious/Action/Connect.py index 53d603e..befaadf 100644 --- a/Furious/Action/Connect.py +++ b/Furious/Action/Connect.py @@ -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) diff --git a/Furious/Action/EditConfiguration.py b/Furious/Action/EditConfiguration.py index a5dc862..4dd99b9 100644 --- a/Furious/Action/EditConfiguration.py +++ b/Furious/Action/EditConfiguration.py @@ -12,4 +12,4 @@ class EditConfigurationAction(Action): ) def triggeredCallback(self, checked): - APP().MainWidget.show() + APP().ServerWidget.show() diff --git a/Furious/Action/Export.py b/Furious/Action/Export.py index d2f8ac9..6f1ab2e 100644 --- a/Furious/Action/Export.py +++ b/Furious/Action/Export.py @@ -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 ) ) diff --git a/Furious/Action/Import.py b/Furious/Action/Import.py index 681f739..6434a57 100644 --- a/Furious/Action/Import.py +++ b/Furious/Action/Import.py @@ -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 ) diff --git a/Furious/Action/Routing.py b/Furious/Action/Routing.py index fbdae1b..0f18e98 100644 --- a/Furious/Action/Routing.py +++ b/Furious/Action/Routing.py @@ -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) ), ), ) diff --git a/Furious/Action/Settings.py b/Furious/Action/Settings.py index 695571d..b40d389 100644 --- a/Furious/Action/Settings.py +++ b/Furious/Action/Settings.py @@ -31,7 +31,7 @@ class SettingsChildAction(Action): else: APP().ShowTabAndSpacesInEditor = Switch.OFF - APP().MainWidget.showTabAndSpacesIfNecessary() + APP().ServerWidget.showTabAndSpacesIfNecessary() class SettingsAction(Action): diff --git a/Furious/Utility/Settings.py b/Furious/Utility/Settings.py index 22b137d..fc90c80 100644 --- a/Furious/Utility/Settings.py +++ b/Furious/Utility/Settings.py @@ -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 diff --git a/Furious/Utility/Utility.py b/Furious/Utility/Utility.py index 167637f..03f6d55 100644 --- a/Furious/Utility/Utility.py +++ b/Furious/Utility/Utility.py @@ -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) diff --git a/Furious/Widget/Application.py b/Furious/Widget/Application.py index 4e04a18..fcfaeef 100644 --- a/Furious/Widget/Application.py +++ b/Furious/Widget/Application.py @@ -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) diff --git a/Furious/Widget/EditConfiguration.py b/Furious/Widget/EditConfiguration.py index 7c0a4e0..3fb2b64 100644 --- a/Furious/Widget/EditConfiguration.py +++ b/Furious/Widget/EditConfiguration.py @@ -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): diff --git a/Furious/Widget/EditRouting.py b/Furious/Widget/EditRouting.py index a4757e7..4802e2c 100644 --- a/Furious/Widget/EditRouting.py +++ b/Furious/Widget/EditRouting.py @@ -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 = [] diff --git a/Furious/Widget/LogViewer.py b/Furious/Widget/LogViewer.py index 50316a9..897adda 100644 --- a/Furious/Widget/LogViewer.py +++ b/Furious/Widget/LogViewer.py @@ -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) diff --git a/Furious/Widget/SystemTrayIcon.py b/Furious/Widget/SystemTrayIcon.py index 4704106..c4ca202 100644 --- a/Furious/Widget/SystemTrayIcon.py +++ b/Furious/Widget/SystemTrayIcon.py @@ -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}')