Skip to content

Commit

Permalink
0.1.0
Browse files Browse the repository at this point in the history
add events
add docs
  • Loading branch information
Mo-Dabao committed May 16, 2022
1 parent d338706 commit d20a131
Show file tree
Hide file tree
Showing 19 changed files with 578 additions and 172 deletions.
159 changes: 21 additions & 138 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,156 +8,39 @@ or suddenly realize the script has stopped for a long while.

It'll be reassuring that the script can stop with a friendly gesture.


## Usage
## Features

winsdk_toast can pop up Windows Toast Notifications that contain

- [x] Text
- [x] Image
- [x] Input
- [x] Text
- [x] Selection
- [x] Audio
- [x] Buit-in
- [ ] Custom
- [x] Progress
- [x] Still
- [ ] Live
- [x] Group
- [x] Event

## Minimal Example

```python
from os.path import abspath
from winsdk_toast import Notifier, Toast

path_pic = abspath('./example/resource/python.ico')

notifier = Notifier('程序名 applicationId')

# %% minimal example
toast = Toast()
toast.add_text('第一行 1st line')
notifier.show(toast)
# %% which is equivalent to
xml = """
<toast activationType="background">
<visual>
<binding template="ToastGeneric">
<text>第一行 1st line</text>
</binding>
</visual>
</toast>
"""
toast = Toast(xml)
notifier.show(toast)


# %% simple example
toast = Toast()
toast.add_text('第一行 1st line', hint_align='center', hint_style='caption')
toast.add_text('第二行 2nd line')
toast.add_text('第三行 3rd line', placement='attribution')
toast.add_image(path_pic, placement='appLogoOverride')
toast.add_action('关闭 Close')
toast.set_audio(silent='true')
notifier.show(toast)
# %% which is equivalent to
xml = f"""
<toast activationType="background">
<visual>
<binding template="ToastGeneric">
<text hint-style="caption" hint-align="center">第一行 1st line</text>
<text>第二行 2nd line</text>
<text placement="attribution">第三行 3rd line</text>
<image src="{path_pic}" placement="appLogoOverride" />
</binding>
</visual>
<actions>
<action content="关闭 Close" arguments="dismiss" activationType="system" />
</actions><audio loop="false" silent="true" />
</toast>
"""
toast = Toast(xml)
notifier.show(toast)

# %% example for control freak
toast = Toast()
element_toast = toast.set_toast(
launch='blah', duration='long', displayTimeStamp='2022-04-01T12:00:00Z', scenario='default',
useButtonStyle='false', activationType='background'
)
element_visual = toast.set_visual(
version='1', lang='zh-CN', baseUri='ms-appx:///', branding='none', addImageQuery='false'
)
element_binding = toast.set_binding(
template='ToastGeneric', fallback='2ndtemplate', lang='zh-CN', addImageQuery='false',
baseUri='ms-appx:///', branding='none'
)
element_text = toast.add_text(
text='第一行 1st line for control freak', id_='1', lang='zh-CN', placement=None,
hint_maxLines='1', hint_style='title', hint_align='center', hint_wrap='false',
element_parent=element_binding
)
element_group = toast.add_group()
element_subgroup_left = toast.add_subgroup(element_parent=element_group)
element_text = toast.add_text(
text='第二行 2nd line for control freak', id_='2', lang='zh-CN', placement=None,
hint_maxLines='1', hint_style='captionSubtle ', hint_align='left', hint_wrap='false',
element_parent=element_subgroup_left
)
element_subgroup_right = toast.add_subgroup(element_parent=element_group)
element_text = toast.add_text(
text='第三行 3rd line for control freak', id_='3', lang='zh-CN', placement='attribution',
hint_maxLines='1', hint_style='captionSubtle', hint_align='left', hint_wrap='false',
element_parent=element_subgroup_right
)
toast.add_image(
path_pic, id_=None, alt='', addImageQuery='false',
placement='appLogoOverride', hint_crop='circle'
)
toast.set_actions()
toast.add_action(
'关闭 Close', arguments='dismiss', activationType='system', placement=None,
imageUri=None, hint_inputId=None, hint_buttonStyle=None, hint_toolTip='tip close'
)
notifier.show(toast)
# %% which is equivalent to
xml = f"""
<toast launch="blah" duration="long" displayTimeStamp="2022-04-01T12:00:00Z" scenario="default" useButtonStyle="false" activationType="background">
<visual version="1" lang="zh-CN" baseUri="ms-appx:///" branding="none" addImageQuery="false">
<binding template="ToastGeneric" fallback="2ndtemplate" lang="zh-CN" addImageQuery="false" baseUri="ms-appx:///" branding="none">
<text id="1" lang="zh-CN" hint-maxLines="1" hint-style="title" hint-align="center" hint-wrap="false">第一行 1st line for control freak</text>
<group>
<subgroup>
<text id="2" lang="zh-CN" hint-maxLines="1" hint-style="captionSubtle " hint-align="left" hint-wrap="false">第二行 2nd line for control freak</text>
</subgroup>
<subgroup>
<text id="3" lang="zh-CN" placement="attribution" hint-maxLines="1" hint-style="captionSubtle" hint-align="left" hint-wrap="false">第三行 3rd line for control freak</text>
</subgroup>
</group>
<image src="{path_pic}" alt="" addImageQuery="false" placement="appLogoOverride" hint-crop="circle"/>
</binding>
</visual>
<actions>
<action content="关闭 Close" arguments="dismiss" activationType="system" hint-toolTip="tip close"/>
</actions>
</toast>
"""
toast = Toast(xml)
notifier.show(toast)
```
The corresponding effects are like:

![minimal_example.gif](doc/pic/minimal_example.gif)

![simple_example.gif](doc/pic/simple_example.gif)

![example_for_control_freak.gif](doc/pic/example_for_control_freak.gif)


## Todo

- Events and callbacks.
- Documentation.
- Costume audio. According to [Microsoft Docs], this might be tricky.
- ...

[Microsoft Docs]: https://docs.microsoft.com/en-us/windows/apps/design/shell/tiles-and-notifications/custom-audio-on-toasts

## else
![minimal_example.gif](docs/source/Examples/pics/minimal.gif)

When I almost make it work, I found another package [windows_toast]
which has the same dependency and more features.
Luckily our 'styles' are quite different, and I'm on vacation,
so I decide to finish it any way.
More examples and documents are on the way.

If you need more features now, please use [windows_toast] instead,
maybe give this one a try later.

[winsdk]: https://pypi.org/project/winsdk
[windows_toast]: https://github.com/DatGuy1/Windows-Toasts
20 changes: 20 additions & 0 deletions docs/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Minimal makefile for Sphinx documentation
#

# You can set these variables from the command line, and also
# from the environment for the first two.
SPHINXOPTS ?=
SPHINXBUILD ?= sphinx-build
SOURCEDIR = source
BUILDDIR = build

# Put it first so that "make" without argument is like "make help".
help:
@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)

.PHONY: help Makefile

# Catch-all target: route all unknown targets to Sphinx using the new
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
%: Makefile
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
35 changes: 35 additions & 0 deletions docs/make.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
@ECHO OFF

pushd %~dp0

REM Command file for Sphinx documentation

if "%SPHINXBUILD%" == "" (
set SPHINXBUILD=sphinx-build
)
set SOURCEDIR=source
set BUILDDIR=build

%SPHINXBUILD% >NUL 2>NUL
if errorlevel 9009 (
echo.
echo.The 'sphinx-build' command was not found. Make sure you have Sphinx
echo.installed, then set the SPHINXBUILD environment variable to point
echo.to the full path of the 'sphinx-build' executable. Alternatively you
echo.may add the Sphinx directory to PATH.
echo.
echo.If you don't have Sphinx installed, grab it from
echo.https://www.sphinx-doc.org/
exit /b 1
)

if "%1" == "" goto help

%SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%
goto end

:help
%SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%

:end
popd
94 changes: 94 additions & 0 deletions docs/source/Examples/events.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# Events

A Toast Notification ends up with an event among `Activated`, `Dissmissed` and `Failed`.

By default, `Notifier` handles `Activated` by printing the argument of the Action(button) and the inputs of Inputs, handes `Dissmissed` by printing the reason of `Dissmissed`, handles `Failed` by raising error_code.

```python
from winsdk_toast import Notifier, Toast


# Step1. Create Notifier with applicationId
notifier = Notifier('程序名 applicationId')

# Step2. Create Toast which contains the message to be shown
toast = Toast()
toast.add_text('第一行 1st line', hint_align='center', hint_style='caption')
toast.add_input(type_='text', id_='input_name')
toast.add_action('关闭 Close')
toast.set_audio(silent='true') # Mute

# Step3. Show the Toast
notifier.show(toast)
```

If click the `X` at the top-right, `Dissmissed` event happens and prints

```
Dismissed reason: UserCanceled
```

If wait till it disappears, `Dissmissed` event happens and prints

```
Dismissed reason: TimeOut
```

If click the `关闭 Close` button, `Activated` event happens and prints

```
Activated with
argument: dismiss
inputs: {'input_name': ''}
```

If type `test input` in the input box and click on `关闭 Close` button, `Activated` event happens and prints

```
Activated with
argument: dismiss
inputs: {'input_name': 'test input'}
```

I haven't figured out how the `activationType` and `arguments` of `action` work, so just leave them to dismiss the Toast Notification.

## Custom `handle`

Just define the functions that handle the `EventArgsActivated`, `EventArgsDismissed`, `EventArgsFailed` respectively, and pass them to `notifier.show`.

```python
from winsdk_toast import Notifier, Toast
from winsdk_toast.event import EventArgsActivated, EventArgsDismissed, EventArgsFailed


def handle_activated(event_args_activated: EventArgsActivated):
print('activated')
print('inputs:', event_args_activated.inputs)
print('argument:', event_args_activated.argument)


def handle_dismissed(event_args_dismissed: EventArgsDismissed):
print('dismissed')
print('reason:', event_args_dismissed.reason)


def handle_failed(event_args_failed: EventArgsFailed):
print('failed')
print('error_code:', event_args_failed.error_code)


# Step1. Create Notifier with applicationId
notifier = Notifier('程序名 applicationId')

# Step2. Create Toast which contains the message to be shown
toast = Toast()
toast.add_text('第一行 1st line', hint_align='center', hint_style='caption')
toast.add_input(type_='text', id_='input_name')
toast.add_action('关闭 Close')
toast.set_audio(silent='true') # Mute

# Step3. Show the Toast
notifier.show(
toast, handle_activated=handle_activated, handle_dismissed=handle_dismissed, handle_failed=handle_failed
)
```
9 changes: 9 additions & 0 deletions docs/source/Examples/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Examples
========================================

.. toctree::
:maxdepth: 2

minimal.md
pic_and_button.md
events.md
43 changes: 43 additions & 0 deletions docs/source/Examples/minimal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Minimal

```python
from winsdk_toast import Notifier, Toast


# Step1. Create Notifier with applicationId
notifier = Notifier('程序名 applicationId')

# Step2. Create Toast which contains the message to be shown
toast = Toast()
toast.add_text('第一行 1st line')

# Step3. Show the Toast
notifier.show(toast)
```

![minimal.gif](pics/minimal.gif)

Which can also be achieved by

```python
from winsdk_toast import Notifier, Toast


# Step1. Create Notifier with applicationId
notifier = Notifier('程序名 applicationId')

# Step2. Create Toast which contains the message to be shown
xml = """
<toast activationType="background">
<visual>
<binding template="ToastGeneric">
<text>第一行 1st line</text>
</binding>
</visual>
</toast>
"""
toast = Toast(xml)

# Step3. Show the Toast
notifier.show(toast)
```
Loading

0 comments on commit d20a131

Please sign in to comment.