-
Notifications
You must be signed in to change notification settings - Fork 1
/
MinimizeToTray2.py
104 lines (92 loc) · 3.14 KB
/
MinimizeToTray2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#-*- coding: utf-8 -*-
# Copyright: Simone Gaiarin <[email protected]>
# License: GNU GPL, version 3 or later; http://www.gnu.org/copyleft/gpl.html
# Name: Minimize to Tray 2
# Version: 0.2
# Description: Minimize anki to tray when the X button is pressed (Anki 2 version)
# Homepage: https://github.com/simgunz/anki-plugins
# Report any problem in the github issues section
from PyQt4.QtGui import *
from PyQt4 import QtCore
import aqt
from anki.hooks import addHook
# Set this variable to True to hide all windows on startup, otherwise set it to False
HIDE_ON_STARTUP = False
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
_fromUtf8 = lambda s: s
def focusChanged(old, now):
if now == None:
self.last_focus = old
def showAll():
for w in self.tray_hidden:
if w.isWindow() and w.isHidden():
w.showNormal()
active = self.last_focus
active.raise_()
active.activateWindow()
self.anki_visible = True
self.tray_hidden = []
def hideAll():
self.tray_hidden = []
activeWindow = QApplication.activeModalWidget()
for w in QApplication.topLevelWidgets():
if w.isWindow() and not w.isHidden():
if not w.children():
continue
w.hide()
self.tray_hidden.append(w)
self.anki_visible = False
def trayActivated(reason):
if reason == QSystemTrayIcon.Trigger:
if self.anki_visible:
hideAll()
else:
showAll()
def createSysTray():
# Check if self (i.e., mw.aqt) already has a trayIcon
if hasattr(self, 'trayIcon'):
return
self.anki_visible = True
self.last_focus = self
self.trayIcon = QSystemTrayIcon(self)
ankiLogo = QIcon()
ankiLogo.addPixmap(QPixmap(_fromUtf8(":/icons/anki.png")), QIcon.Normal, QIcon.Off)
self.trayIcon.setIcon(ankiLogo)
#trayMenu = QMenu(self)
#self.trayIcon.setContextMenu(trayMenu)
#trayMenu.addAction(self.form.actionExit)
self.connect(self.trayIcon, QtCore.SIGNAL("activated(QSystemTrayIcon::ActivationReason)"), trayActivated)
self.connect(self.app, QtCore.SIGNAL("focusChanged(QWidget*,QWidget*)"), focusChanged)
self.trayIcon.show()
def myOnClose():
"Called from a shortcut key. Close current active window."
aw = self.app.activeWindow()
if not aw or aw == self:
self.unloadProfile(browser=False)
self.trayIcon.hide()
self.app.quit()
else:
aw.close()
def myCloseEvent(event):
"User hit the X button"
if self.anki_visible:
self.col.save()
trayActivated(QSystemTrayIcon.Trigger)
event.ignore();
else:
myOnClose()
def setCloseEventAction():
self.disconnect(self.form.actionExit, QtCore.SIGNAL("triggered()"), self, QtCore.SLOT("close()"))
self.connect(self.form.actionExit, QtCore.SIGNAL("triggered()"), self.onClose)
if __name__ == "__main__":
print "Don't run me. I'm a plugin."
else:
self = aqt.mw
addHook("profileLoaded", setCloseEventAction)
addHook("profileLoaded", createSysTray)
if HIDE_ON_STARTUP:
addHook("profileLoaded", hideAll)
self.closeEvent = myCloseEvent
self.onClose = myOnClose