Browse Source

support running in window, without system tray

Thomas Buck 3 years ago
parent
commit
3e39a38fa8
2 changed files with 49 additions and 14 deletions
  1. 3
    1
      README.md
  2. 46
    13
      src/octotray.py

+ 3
- 1
README.md View File

@@ -9,7 +9,9 @@ Automatic builds are provided for Linux, Windows and macOS.
9 9
 
10 10
 For more [take a look at OctoTray on my website](https://www.xythobuz.de/octotray.html).
11 11
 
12
-## Building
12
+If the system tray is not available (or when passing the '-w' parameter) the main menu will instead be shown in a window.
13
+
14
+## Building / Running
13 15
 
14 16
 You have different options of building and running OctoTray:
15 17
 

+ 46
- 13
src/octotray.py View File

@@ -337,6 +337,24 @@ class CamWindow(QWidget):
337 337
             else:
338 338
                 print("Error loading image: " + reply.errorString())
339 339
 
340
+class MainWindow(QWidget):
341
+    def __init__(self, parent, *args, **kwargs):
342
+        super(MainWindow, self).__init__(*args, **kwargs)
343
+        self.parent = parent
344
+
345
+        self.mainLayout = QVBoxLayout()
346
+        self.setLayout(self.mainLayout)
347
+        self.mainLayout.addWidget(self.parent.menu)
348
+
349
+        self.parent.menu.aboutToHide.connect(self.aboutToHide)
350
+
351
+    def aboutToHide(self):
352
+        self.parent.menu.show()
353
+
354
+    def closeEvent(self, event):
355
+        self.parent.exit()
356
+        event.accept()
357
+
340 358
 class OctoTray():
341 359
     name = "OctoTray"
342 360
     vendor = "xythobuz"
@@ -367,13 +385,10 @@ class OctoTray():
367 385
     camWindows = []
368 386
     settingsWindow = None
369 387
 
370
-    def __init__(self, app):
388
+    def __init__(self, app, inSysTray):
371 389
         QCoreApplication.setApplicationName(self.name)
372 390
         self.app = app
373
-
374
-        if not QSystemTrayIcon.isSystemTrayAvailable():
375
-            self.showDialog("OctoTray Error", "System Tray is not available on this platform!", "", False, False, True)
376
-            sys.exit(0)
391
+        self.inSysTray = inSysTray
377 392
 
378 393
         self.manager = QtNetwork.QNetworkAccessManager()
379 394
         self.menu = QMenu()
@@ -483,11 +498,22 @@ class OctoTray():
483 498
         self.pic.load(self.iconPathName)
484 499
         self.icon = QIcon(self.pic)
485 500
 
486
-        self.trayIcon = QSystemTrayIcon(self.icon)
487
-        self.trayIcon.setToolTip(self.name + " " + self.version)
488
-        self.trayIcon.setContextMenu(self.menu)
489
-        self.trayIcon.activated.connect(self.showHide)
490
-        self.trayIcon.setVisible(True)
501
+        if self.inSysTray:
502
+            self.trayIcon = QSystemTrayIcon(self.icon)
503
+            self.trayIcon.setToolTip(self.name + " " + self.version)
504
+            self.trayIcon.setContextMenu(self.menu)
505
+            self.trayIcon.activated.connect(self.showHide)
506
+            self.trayIcon.setVisible(True)
507
+        else:
508
+            self.mainWindow = MainWindow(self)
509
+            self.mainWindow.show()
510
+            self.mainWindow.activateWindow()
511
+            screenGeometry = QDesktopWidget().screenGeometry()
512
+            x = (screenGeometry.width() - self.mainWindow.width()) / 2
513
+            y = (screenGeometry.height() - self.mainWindow.height()) / 2
514
+            x += screenGeometry.x()
515
+            y += screenGeometry.y()
516
+            self.mainWindow.setGeometry(int(x), int(y), int(self.mainWindow.width()), int(self.mainWindow.height()))
491 517
 
492 518
     def showHide(self, activationReason):
493 519
         if activationReason == QSystemTrayIcon.Trigger:
@@ -868,18 +894,25 @@ class OctoTray():
868 894
         if self.settingsWindow != None:
869 895
             self.settingsWindow.close()
870 896
 
871
-        self.trayIcon.setVisible(False)
897
+        if self.inSysTray:
898
+            self.trayIcon.setVisible(False)
899
+        else:
900
+            self.mainWindow.setVisible(False)
872 901
 
873 902
 if __name__ == "__main__":
874 903
     app = QApplication(sys.argv)
875 904
     app.setQuitOnLastWindowClosed(False)
876 905
 
877
-    tray = OctoTray(app)
906
+    inSysTray = QSystemTrayIcon.isSystemTrayAvailable()
907
+    if ("windowed" in sys.argv) or ("--windowed" in sys.argv) or ("-w" in sys.argv):
908
+        inSysTray = False
909
+
910
+    tray = OctoTray(app, inSysTray)
878 911
     rc = app.exec_()
879 912
 
880 913
     while rc == 42:
881 914
         tray.closeAll()
882
-        tray = OctoTray(app)
915
+        tray = OctoTray(app, inSysTray)
883 916
         rc = app.exec_()
884 917
 
885 918
     sys.exit(rc)

Loading…
Cancel
Save