Debian Codegraph @

plyer/platforms/win/libs/balloontip.py

raw symbols SCIP
1 # -- coding: utf-8 --
2 '''
3 Module of Windows API for creating taskbar balloon tip
4 notification in the taskbar's tray notification area.
5 '''
6
7 __all__ = ('WindowsBalloonTip', 'balloon_tip')
8
9
10 import time
11 import ctypes
12 import atexit
13 from threading import RLock
14
15 from plyer.platforms.win.libs import win_api_defs
16
17
18 WS_OVERLAPPED = 0x00000000
19 WS_SYSMENU = 0x00080000
20 WM_DESTROY = 2
21 CW_USEDEFAULT = 8
22
23 LR_LOADFROMFILE = 16
24 LR_DEFAULTSIZE = 0x0040
25 IDI_APPLICATION = 32512
26 IMAGE_ICON = 1
27
28 NOTIFYICON_VERSION_4 = 4
29 NIM_ADD = 0
30 NIM_MODIFY = 1
31 NIM_DELETE = 2
32 NIM_SETVERSION = 4
33 NIF_MESSAGE = 1
34 NIF_ICON = 2
35 NIF_TIP = 4
36 NIF_INFO = 0x10
37 NIIF_USER = 4
38 NIIF_LARGE_ICON = 0x20
39
40
41 class WindowsBalloonTip:
42 '''
43 Implementation of balloon tip notifications through Windows API.
44
45 * Register Window class name:
46 https://msdn.microsoft.com/en-us/library/windows/desktop/ms632596.aspx
47 * Create an overlapped window using the registered class.
48 - It's hidden everywhere in GUI unless ShowWindow(handle, SW_SHOW)
49 function is called.
50 * Show/remove a tray icon and a balloon tip notification.
51
52 Each instance is a separate notification with different parameters.
53 Can be used with Threads.
54 '''
55
56 _class_atom = 0
57 _wnd_class_ex = None
58 _hwnd = None
59 _hicon = None
60 _balloon_icon = None
61 _notify_data = None
62 _count = 0
63 _lock = RLock()
64
65 @staticmethod
66 def _get_unique_id():
67 '''
68 Keep track of each created balloon tip notification names,
69 so that they can be easily identified even from outside.
70
71 Make sure the count is shared between all the instances
72 i.e. use a lock, so that _count class variable is incremented
73 safely when using balloon tip notifications with Threads.
74 '''
75
76 WindowsBalloonTip._lock.acquire()
77 val = WindowsBalloonTip._count
78 WindowsBalloonTip._count += 1
79 WindowsBalloonTip._lock.release()
80 return val
81
82 def __init__(self, title, message, app_name, app_icon='',
83 timeout=10, **kwargs):
84 '''
85 The app_icon parameter, if given, is an .ICO file.
86 '''
87 atexit.register(self.__del__)
88
89 wnd_class_ex = win_api_defs.get_WNDCLASSEXW()
90 class_name = 'PlyerTaskbar' + str(WindowsBalloonTip._get_unique_id())
91
92 wnd_class_ex.lpszClassName = class_name
93
94 # keep ref to it as long as window is alive
95 wnd_class_ex.lpfnWndProc = win_api_defs.WindowProc(
96 win_api_defs.DefWindowProcW
97 )
98 wnd_class_ex.hInstance = win_api_defs.GetModuleHandleW(None)
99 if wnd_class_ex.hInstance is None:
100 raise Exception('Could not get windows module instance.')
101
102 class_atom = win_api_defs.RegisterClassExW(wnd_class_ex)
103 if class_atom == 0:
104 raise Exception('Could not register the PlyerTaskbar class.')
105
106 self._class_atom = class_atom
107 self._wnd_class_ex = wnd_class_ex
108
109 # create window
110 self._hwnd = win_api_defs.CreateWindowExW(
111 # dwExStyle, lpClassName, lpWindowName, dwStyle
112 0, class_atom, '', WS_OVERLAPPED,
113 # x, y, nWidth, nHeight
114 0, 0, CW_USEDEFAULT, CW_USEDEFAULT,
115 # hWndParent, hMenu, hInstance, lpParam
116 None, None, wnd_class_ex.hInstance, None
117 )
118 if self._hwnd is None:
119 raise Exception('Could not get create window.')
120 win_api_defs.UpdateWindow(self._hwnd)
121
122 # load .ICO file for as balloon tip and tray icon
123 if app_icon:
124 icon_flags = LR_LOADFROMFILE | LR_DEFAULTSIZE
125 hicon = win_api_defs.LoadImageW(
126 None, app_icon, IMAGE_ICON, 0, 0, icon_flags
127 )
128
129 if hicon is None:
130 raise Exception('Could not load icon {}'.format(app_icon))
131 self._balloon_icon = self._hicon = hicon
132 else:
133 self._hicon = win_api_defs.LoadIconW(
134 None,
135 ctypes.cast(IDI_APPLICATION, win_api_defs.LPCWSTR)
136 )
137
138 # show the notification
139 self.notify(title, message, app_name)
140 if timeout:
141 time.sleep(timeout)
142
143 def __del__(self):
144 '''
145 Clean visible parts of the notification object, then free all resources
146 allocated for creating the nofitication Window and icon.
147 '''
148 self.remove_notify()
149 if self._hicon is not None:
150 win_api_defs.DestroyIcon(self._hicon)
151 if self._wnd_class_ex is not None:
152 win_api_defs.UnregisterClassW(
153 self._class_atom,
154 self._wnd_class_ex.hInstance
155 )
156 if self._hwnd is not None:
157 win_api_defs.DestroyWindow(self._hwnd)
158
159 def notify(self, title, message, app_name):
160 '''
161 Displays a balloon in the systray. Can be called multiple times
162 with different parameter values.
163 '''
164 # remove previous visible balloon tip nofitication if available
165 self.remove_notify()
166
167 # add icon and messages to window
168 hicon = self._hicon
169 flags = NIF_TIP | NIF_INFO
170 icon_flag = 0
171
172 if hicon is not None:
173 flags |= NIF_ICON
174
175 # if icon is default app's one, don't display it in message
176 if self._balloon_icon is not None:
177 icon_flag = NIIF_USER | NIIF_LARGE_ICON
178
179 notify_data = win_api_defs.get_NOTIFYICONDATAW(
180 0, self._hwnd,
181 id(self), flags, 0, hicon, app_name, 0, 0, message,
182 NOTIFYICON_VERSION_4, title, icon_flag, win_api_defs.GUID(),
183 self._balloon_icon
184 )
185
186 self._notify_data = notify_data
187 if not win_api_defs.Shell_NotifyIconW(NIM_ADD, notify_data):
188 raise Exception('Shell_NotifyIconW failed.')
189 if not win_api_defs.Shell_NotifyIconW(NIM_SETVERSION,
190 notify_data):
191 raise Exception('Shell_NotifyIconW failed.')
192
193 def remove_notify(self):
194 '''
195 Removes the notify balloon, if displayed.
196 '''
197 if self._notify_data is not None:
198 win_api_defs.Shell_NotifyIconW(NIM_DELETE, self._notify_data)
199 self._notify_data = None
200
201
202 def balloon_tip(**kwargs):
203 '''
204 Instance for balloon tip notification implementation.
205 '''
206 WindowsBalloonTip(**kwargs)