DatGuy1 / Windows-Toasts

Python library used to send toast notifications on Windows machines
Apache License 2.0
101 stars 6 forks source link

Struggling to understand how to add a HERO image #167

Closed NebularNerd closed 1 week ago

NebularNerd commented 1 week ago

I'm trying to port my Burnt Toast notifiers to python to save making subprocess calls but I'm struggling to understand how to make a hero image.

In-progress minimal working code:

from windows_toasts import Toast, ToastDisplayImage, InteractableWindowsToaster
from pathlib import Path

basedir = Path(__file__).parent.absolute()  # Maps basedir to location of this file
src = f"{basedir}/data/toast"
herodir = f"{src}/HERO"
imgdir = f"{src}/IMG"
icodir = f"{src}/ICO"

toasts = {
    "IC": {"appid": "ZZZ_ImageConverter", "icon": rf"{icodir}/picture.ico", "appname": "Image converter"},
    "NT": {
        "appid": "ZZZ_Notification",
        "icon": rf"{icodir}/IconGroup268.ico",
        "appname": "Notification",
    },
    "CZ": {
        "appid": "ZZZ_ChineseZipConverter",
        "icon": rf"{icodir}/chinese_language.ico",
        "appname": "Chinese Zip Converter",
    },
    "PDF": {
        "appid": "ZZZ_PDFConverter",
        "icon": rf"{icodir}/pdf.ico",
        "appname": "PDF Converter",
    },
}

heros = {
    "error": f"{herodir}/error.gif",
    "check": f"{herodir}/checkmark.gif",
    "announce": f"{herodir}/announce.gif",
    "install": f"{herodir}/installing.gif",
    "code": f"{herodir}/binarycode.gif",
    "chineselandscape": f"{herodir}/chinese_zip.gif",
}

logos = {
    "chinesehappy": rf"{imgdir}/chinese_happiness.png",
    "download": rf"{imgdir}/download.png",
    "check": rf"{imgdir}/checkmark.png",
    "error": rf"{imgdir}/error.png",
    "info": rf"{imgdir}/info.png",
    "pdf": rf"{imgdir}/pdf.png",
}

def toastme(d: dict):
    toaster = InteractableWindowsToaster("Test Notif", d["a"])
    newToast = Toast([d["t"]])
    # str or PathLike
    newToast.AddImage(ToastDisplayImage.fromPath(Path(d["i"])))
    newToast.AddImage(ToastDisplayImage.fromPath(Path(d["h"])))

    toaster.show_toast(newToast)

data = {
    "t": "This is a most exciting notification!",
    "i": logos["info"],
    "h": heros["announce"],
    "a": toasts["NT"]["appid"],
}
toastme(data)

How can I assign logo and hero to the .AddImage lines, I've been reading through the docs and maybe I'm just being a bit dim, but I can't seem to suss it out.

NebularNerd commented 1 week ago

Figured it out with a little help from Google Gemini.

First change: from windows_toasts import Toast, ToastDisplayImage, InteractableWindowsToaster to from windows_toasts import Toast, ToastDisplayImage, InteractableWindowsToaster, ToastImagePosition

Then ammend the .AddImage lines as required:

newToast.AddImage(ToastDisplayImage.fromPath('logo.jpg'), position=ToastImagePosition.AppLogo))
newToast.AddImage(ToastDisplayImage.fromPath('hero.jpg'), position=ToastImagePosition.Hero))
newToast.AddImage(ToastDisplayImage.fromPath('inline.jpg'), position=ToastImagePosition.Inline))

And we now get: image

@DatGuy1: It may be worth adding an example like you have with the progress bars and such, as the documentation while comprehensive could benefit from a quick example. Loving this by the way, I was using your register_hkey_aumid.py with my old Burnt Toast version to handle the custom titles but now I can finally move to a full Python implementation. 😎

DatGuy1 commented 1 week ago

I'll add a section on it to https://windows-toasts.readthedocs.io/en/latest/advanced_usage.html