Skip to content

Commit

Permalink
app window to the foreground when starting a new instance of the app
Browse files Browse the repository at this point in the history
  • Loading branch information
clesssalvein committed Feb 19, 2023
1 parent 447f3cc commit 89ee312
Show file tree
Hide file tree
Showing 10 changed files with 161 additions and 34 deletions.
114 changes: 86 additions & 28 deletions GlpiClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@
QGridLayout, QMainWindow, QDesktopWidget, QTableWidget, QDateTimeEdit, QAbstractItemView, QTableWidgetItem, \
QAbstractScrollArea, QHeaderView, QMessageBox, QPlainTextEdit, QApplication, QFileDialog, QComboBox, QVBoxLayout
from PyQt5.QtCore import Qt, QCoreApplication, QTimer, QDate
from PyQt5.QtGui import QIcon, QFont, QPixmap
from PyQt5.QtGui import QIcon, QFont, QPixmap, QWindow

# spinner module (from file "waitingspinnerwidget.py")
from waitingspinnerwidget import QtWaitingSpinner

# check if app already running (from file "singleinstance.py")
from singleinstance import singleinstance
#from singleinstance import singleinstance
from sys import exit


Expand Down Expand Up @@ -143,9 +143,22 @@ def AuthWinInitUI(self):

# check if another instance of the same program running
if myAppAlreadyRunning.alreadyrunning():
print("Another instance of this program is already running")
QMessageBox.about(self, appName, _("The program is already running"))
exit(0)

# if checked checkbox hideAppWindowToTrayOnClose(1) in config file
# MessageBox "The program is already running"
if hideAppWindowToTrayOnClose == "1":
print("Another instance of this program is already running")
QMessageBox.about(self, appName, _("The program is already running"))
exit(0)

# if checked checkbox hideAppWindowToTrayOnClose(0) in config file
# maximize app from panel
if hideAppWindowToTrayOnClose == "0":
w = WindowMgr()
w.find_window_wildcard("GlpiClient")
w.set_foreground()
exit(0)

# no app running, safe to continue...
print("No another instance is running, can continue here")

Expand Down Expand Up @@ -284,11 +297,16 @@ def AuthWinInitUI(self):
# show mainwin
self.show()

# HIDE (MINIMIZE) APP TO TRAY AT STARTUP
# if checked checkbox hideAppWindowToTrayAtStartup(1) in config file
if hideAppWindowToTrayAtStartup == "1":
# hide (minimize) appWindow at startup
self.hide()

# hide (minimize) appWindow to tray at startup
if hideAppWindowToTrayOnClose == "1":
self.hide()

# minimize window to windows panel
if hideAppWindowToTrayOnClose == "0":
self.setWindowState(self.windowState() | QWindow.Minimized)

def center(self):
qr = self.frameGeometry()
Expand Down Expand Up @@ -471,13 +489,20 @@ def keyPressEvent(self, event):

def closeEvent(self, event):
event.ignore()
self.hide()
self.tray_icon.showMessage(
appName,
appName + " " + "is minimized to the system tray",
#QSystemTrayIcon.Information,
2000
)

# HIDE (MINIMIZE) APP TO TRAY ON CLOSE
# if checked checkbox hideAppWindowToTrayOnClose(1) in config file
if hideAppWindowToTrayOnClose == "1":
self.hide()
self.tray_icon.showMessage(
appName,
appName + " " + "is minimized to the system tray",
#QSystemTrayIcon.Information,
2000
)
# minimize window to windows panel
if hideAppWindowToTrayOnClose == "0":
self.setWindowState(self.windowState() | QWindow.Minimized)

# about button
def aboutWinShow(self):
Expand Down Expand Up @@ -1182,13 +1207,19 @@ def settingsWinShow(self):

def closeEvent(self, event):
event.ignore()
self.hide()
self.tray_icon.showMessage(
appName,
appName + " " + _("is minimized to the system tray"),
#QSystemTrayIcon.Information,
2000
)

# HIDE (MINIMIZE) APP TO TRAY ON CLOSE
if hideAppWindowToTrayOnClose == "1":
self.hide()
self.tray_icon.showMessage(
appName,
appName + " " + _("is minimized to the system tray"),
#QSystemTrayIcon.Information,
2000
)
# minimize window to windows panel
if hideAppWindowToTrayOnClose == "0":
self.setWindowState(self.windowState() | QWindow.Minimized)

# app close func
def appClose(self):
Expand Down Expand Up @@ -1300,7 +1331,7 @@ def __init__(self):

def initUI(self):
# create Settings win
self.setFixedSize(500, 240)
self.setFixedSize(600, 240)
self.center()
self.setWindowTitle(_("Settings"))
self.setWindowIcon(QIcon('img\ico.png'))
Expand Down Expand Up @@ -1341,7 +1372,7 @@ def initUI(self):
# add checkbox autoupdate
self.settingsCheckboxAutoUpdate = QCheckBox(self)

# if checked checkbox REMEMBER LOGIN IS TRUE(1) in config file
# if checked checkbox autoupdate IS TRUE(1) in config file
if appAutoUpdate == "1":
# check checkboxRememberLogin
self.settingsCheckboxAutoUpdate.setChecked(True)
Expand All @@ -1352,9 +1383,9 @@ def initUI(self):
# hideAppWindowToTrayAtStartup option
self.settingsHideAppWindowToTrayAtStartup = QLabel(self)
self.settingsHideAppWindowToTrayAtStartup.setFont(QFont("Decorative", 9))
self.settingsHideAppWindowToTrayAtStartup.setText(_("Hide app window to tray at startup") + ':')
self.settingsHideAppWindowToTrayAtStartup.setText(_("Hide app window at startup") + ':')

# add checkbox autoupdate
# add checkbox hideAppWindowToTrayAtStartup
self.settingsCheckboxHideAppWindowToTrayAtStartup = QCheckBox(self)

# if checked checkbox hideAppWindowToTrayAtStartup(1) in config file
Expand All @@ -1365,6 +1396,22 @@ def initUI(self):
# UNcheck checkboxRememberLogin
self.settingsCheckboxHideAppWindowToTrayAtStartup.setChecked(False)

# option
self.settingsHideAppWindowToTrayOnClose = QLabel(self)
self.settingsHideAppWindowToTrayOnClose.setFont(QFont("Decorative", 9))
self.settingsHideAppWindowToTrayOnClose.setText(_("Hide app window to tray on App close") + ':')

# add checkbox hideAppWindowToTrayAtStartup
self.settingsCheckboxHideAppWindowToTrayOnClose = QCheckBox(self)

# if checked checkbox hideAppWindowToTrayAtStartup(1) in config file
if hideAppWindowToTrayOnClose == "1":
# check checkboxRememberLogin
self.settingsCheckboxHideAppWindowToTrayOnClose.setChecked(True)
if hideAppWindowToTrayOnClose == "0":
# UNcheck checkboxRememberLogin
self.settingsCheckboxHideAppWindowToTrayOnClose.setChecked(False)

# OK button create
self.settingsOkButton = QPushButton(_("OK"), self)
self.settingsOkButton.setFixedSize(80, 30)
Expand All @@ -1383,8 +1430,10 @@ def initUI(self):
gridSettings.addWidget(self.settingsCheckboxAutoUpdate, 2, 1, 1, 1, alignment=Qt.AlignCenter)
gridSettings.addWidget(self.settingsHideAppWindowToTrayAtStartup, 3, 0, 1, 1, alignment=Qt.AlignCenter)
gridSettings.addWidget(self.settingsCheckboxHideAppWindowToTrayAtStartup, 3, 1, 1, 1, alignment=Qt.AlignCenter)
gridSettings.addWidget(self.settingsOkButton, 4, 0, 1, 1, alignment=Qt.AlignRight)
gridSettings.addWidget(self.settingsExitButton, 4, 1, 1, 1, alignment=Qt.AlignLeft)
gridSettings.addWidget(self.settingsHideAppWindowToTrayOnClose, 4, 0, 1, 1, alignment=Qt.AlignCenter)
gridSettings.addWidget(self.settingsCheckboxHideAppWindowToTrayOnClose, 4, 1, 1, 1, alignment=Qt.AlignCenter)
gridSettings.addWidget(self.settingsOkButton, 5, 0, 1, 1, alignment=Qt.AlignRight)
gridSettings.addWidget(self.settingsExitButton, 5, 1, 1, 1, alignment=Qt.AlignLeft)
self.setLayout(gridSettings)

self.show()
Expand Down Expand Up @@ -1427,6 +1476,15 @@ def settingsSave(self):
else:
config.set("main", "hideappwindowtotrayatstartup", "0")

# if checkbox hideAppWindowToTrayOnClose is checked - write to config file
if self.settingsCheckboxHideAppWindowToTrayOnClose.isChecked():
# remember in config file
config.set("main", "hideappwindowtotrayonclose", "1")

# if checkbox settingsCheckboxHideAppWindowToTrayOnClose is UNchecked - write to config file
else:
config.set("main", "hideappwindowtotrayonclose", "0")

# write settings to config file
with open(configPath, "w", encoding="utf-8") as config_file:
config.write(config_file)
Expand Down
1 change: 1 addition & 0 deletions config/config.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ apptoken = hHFrpHKt0lMTIPxM2vsGlCShnRatQPeF5D6c1QF5
lang = en
autoupdate = 0
hideappwindowtotrayatstartup = 0
hideappwindowtotrayonclose = 0

[ftp]
ftpserverhost = test.ru
Expand Down
58 changes: 58 additions & 0 deletions functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,61 @@ def sessionKillCommon():
pass

return


# detect already running app

from win32event import CreateMutex
from win32api import CloseHandle, GetLastError
from winerror import ERROR_ALREADY_EXISTS

class singleinstance:
""" Limits application to single instance """

def __init__(self):
self.mutexname = "testmutex_{D0E858DF-985E-4907-B7FB-8D732C3FC3B9}"
self.mutex = CreateMutex(None, False, self.mutexname)
self.lasterror = GetLastError()

def alreadyrunning(self):
return (self.lasterror == ERROR_ALREADY_EXISTS)

def __del__(self):
if self.mutex:
CloseHandle(self.mutex)


# unwrap already opened app's window

import win32gui
import re


# maximize app window,
# if you run new app and another app already run

class WindowMgr:
#Encapsulates some calls to the winapi for window management

def __init__ (self):
# Constructor
self._handle = None

def find_window(self, class_name, window_name=None):
# find a window by its class_name
self._handle = win32gui.FindWindow(class_name, window_name)

def _window_enum_callback(self, hwnd, wildcard):
# Pass to win32gui.EnumWindows() to check all the opened windows
if re.match(wildcard, str(win32gui.GetWindowText(hwnd))) is not None:
self._handle = hwnd

def find_window_wildcard(self, wildcard):
# find a window whose title matches the wildcard regex
self._handle = None
win32gui.EnumWindows(self._window_enum_callback, wildcard)

def set_foreground(self):
# put the window in the foreground
win32gui.SetForegroundWindow(self._handle)
win32gui.ShowWindow(self._handle, 4)
Binary file modified lang/i18n/en/LC_MESSAGES/glpiClient.mo
Binary file not shown.
7 changes: 5 additions & 2 deletions lang/i18n/en/LC_MESSAGES/glpiClient.po
Original file line number Diff line number Diff line change
Expand Up @@ -293,8 +293,11 @@ msgstr "This window will close automatically..."
msgid "App autoupdate"
msgstr "App autoupdate"

msgid "Hide app window to tray at startup"
msgstr "Hide app window to tray at startup"
msgid "Hide app window at startup"
msgstr "Hide app window at startup"

msgid "Hide app window to tray on App close"
msgstr "Hide app window to tray on App close"

msgid "Author Contact"
msgstr "Author Contact"
Expand Down
Binary file modified lang/i18n/it/LC_MESSAGES/glpiClient.mo
Binary file not shown.
5 changes: 4 additions & 1 deletion lang/i18n/it/LC_MESSAGES/glpiClient.po
Original file line number Diff line number Diff line change
Expand Up @@ -314,9 +314,12 @@ msgstr "Questa finestra si chiuderà automaticamente..."
msgid "App autoupdate"
msgstr "Aggiornamento automatico app"

msgid "Hide app window to tray at startup"
msgid "Hide app window at startup"
msgstr "Nascondi la finestra del programma nella barra delle applicazioni all'avvio"

msgid "Hide app window to tray on App close"
msgstr "Hide app window to tray on App close"

msgid "Author Contact"
msgstr "Autore Contatto"

Expand Down
Binary file modified lang/i18n/ru/LC_MESSAGES/glpiClient.mo
Binary file not shown.
7 changes: 5 additions & 2 deletions lang/i18n/ru/LC_MESSAGES/glpiClient.po
Original file line number Diff line number Diff line change
Expand Up @@ -293,8 +293,11 @@ msgstr "Это окно закроется автоматически..."
msgid "App autoupdate"
msgstr "Автообновление программы"

msgid "Hide app window to tray at startup"
msgstr "Скрыть окно программы в системном лотке при запуске"
msgid "Hide app window at startup"
msgstr "Скрыть окно программы при запуске"

msgid "Hide app window to tray on App close"
msgstr "Свернуть программу в трей при закрытии"

msgid "Author Contact"
msgstr "Связаться с автором"
Expand Down
3 changes: 2 additions & 1 deletion vars.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
appName = "GlpiClient"
appExeFile = "GlpiClient.exe"
appDirName = "GlpiClient"
appVersion = "0.304"
appVersion = "0.305"
authorEmail = "clesssalvein@gmail.com"
appHomepage = "https://github.com/clesssalvein/GlpiClient"
sessionToken = None
Expand All @@ -30,6 +30,7 @@
appToken = config.get("main", "appToken")
appLang = config.get("main", "lang")
hideAppWindowToTrayAtStartup = config.get("main", "hideappwindowtotrayatstartup")
hideAppWindowToTrayOnClose = config.get("main", "hideappwindowtotrayonclose")

# gettext multilang
langDirsPath = "lang/i18n"
Expand Down

0 comments on commit 89ee312

Please sign in to comment.