Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pop-up bubble/balloon notification (feature request) #46

Open
WanderingApps opened this issue Jan 19, 2022 · 1 comment
Open

Pop-up bubble/balloon notification (feature request) #46

WanderingApps opened this issue Jan 19, 2022 · 1 comment

Comments

@WanderingApps
Copy link

FEATURE REQUEST

I use this module to call some background scripting that is hidden to the user. I am working on having the tray icon color changing while the script is in progress and then back when it finishes, audio feedback, etc. It would be great to have the option of a pop-up notification/balloon where it could show the user the background script had completed.

Really like the module, thanks for all the hard work.

@Damian-2001
Copy link

It is not so difficult to add this option, I share in case it can help someone.
*Note that on windows >= 10 , "balloon notifications" is equal to "toasted notifications" and they can be implemented in addition to Shell_NotifyIcon, with WinRT api

traybar.py

    def __init__(self, ...):
        ...
        self._balloon_callback = None
        self._balloon_hicon = None

    def show_toast_notification(self, text, title="", icon=None, onclick=None):
        if not self._notify_id:
            self._refresh_icon()
        self._balloon_callback = None
        if onclick and "function" in repr(onclick):
            self._balloon_callback = onclick
        if icon is not None and os.path.isfile(icon):
            if self._balloon_hicon:
                DestroyIcon(self._balloon_hicon)
            icon = encode_for_locale(icon)
            hicon = self._hicon = LoadImage(0, icon, IMAGE_ICON, 128, 128, LR_LOADFROMFILE)
            self._balloon_hicon = hicon
        else:
            hicon = 0
        _notify_id = NotifyBalloonData(hWnd = self._notify_id.hWnd,
                                      uID = self._notify_id.uID,
                                      text = text, 
                                      title = title,
                                      hicon = hicon)
        if not Shell_NotifyIcon(NIM_MODIFY, ctypes.byref(_notify_id)):
            raise Exception("show_toast_notification failed: %s" % ctypes.WinError())

    def _notify(self, hwnd, msg, wparam, lparam):
        if lparam == WM_LBUTTONDBLCLK:
            self._execute_menu_option(self._default_menu_index + SysTrayIcon.FIRST_ID)
        elif lparam == WM_RBUTTONUP:
            self._show_menu()
        elif lparam == WM_LBUTTONUP:
            pass
        elif lparam & 0xffff == NIN_BALLOONUSERCLICK:
            if self._balloon_callback:
                self._balloon_callback()
        return True

win32_adapter.py

NIF_INFO = 16
NIIF_INFO = 1
NIIF_USER = 4
NIIF_ICON_MASK = 15
NIIF_NOSOUND = 16
NIIF_LARGE_ICON = 32
NIN_BALLOONUSERCLICK = (WM_USER + 5)

def NotifyBalloonData(hWnd=0, uID=0, hicon=0, text="", title=""):
    text = encode_for_locale(text)[:256]
    title = encode_for_locale(title)[:64]
    res = NOTIFYICONDATA()
    res.cbSize = ctypes.sizeof(res)
    res.hWnd = hWnd
    res.uID = uID
    res.uFlags = NIF_INFO
    res.szInfo = text
    res.szInfoTitle = title
    if hicon and "hBalloonIcon" in NOTIFYICONDATA._fields_[-1]:
        res.dwInfoFlags = NIIF_USER | NIIF_LARGE_ICON | NIIF_ICON_MASK | NIIF_NOSOUND
        res.hBalloonIcon = hicon
    else:
        res.dwInfoFlags = NIIF_INFO | NIIF_NOSOUND
    return res

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants