File: menu.py

package info (click to toggle)
python-pywebview 6.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 33,452 kB
  • sloc: python: 10,921; javascript: 3,250; java: 522; cs: 130; sh: 15; makefile: 3; xml: 1
file content (97 lines) | stat: -rw-r--r-- 2,693 bytes parent folder | download
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
"""Create an application menu."""

import webview
from webview.menu import Menu, MenuAction, MenuSeparator


def change_active_window_content():
    active_window = webview.active_window()
    if active_window:
        active_window.load_html('<h1>You changed this window!</h1>')


def click_me():
    active_window = webview.active_window()
    if active_window:
        active_window.load_html('<h1>You clicked me!</h1>')


def test():
    active_window = webview.active_window()
    if active_window:
        active_window.load_html('<h1>This is a test!</h1>')


def do_nothing():
    pass


def say_this_is_window_2():
    active_window = webview.active_window()
    if active_window:
        active_window.load_html('<h1>This is window 2</h2>')


def open_save_file_dialog():
    active_window = webview.active_window()
    active_window.create_file_dialog(
        webview.FileDialog.SAVE, directory='/', save_filename='test.file'
    )


def open_preferences():
    active_window = webview.active_window()
    if active_window:
        active_window.load_html('<h1>Preferences</h1><p>App preferences would open here (macOS app menu)</p>')


def check_for_updates():
    active_window = webview.active_window()
    if active_window:
        active_window.load_html('<h1>Check for Updates</h1><p>Checking for updates... (macOS app menu)</p>')


if __name__ == '__main__':
    # App menu items (macOS only - appears between About and Services)
    # On other platforms, this menu is ignored
    macos_app_menu = Menu('__app__', [
        MenuAction('Preferences...', open_preferences),
        MenuSeparator(),
        MenuAction('Check for Updates', check_for_updates),
    ])

    window_menu = [Menu(
        'Window', [
            MenuAction('Test', test)
        ]
    )]

    app_menu = [
        macos_app_menu,  # macOS app menu items
        Menu(
            'Menu 1',
            [
                MenuAction('Change Active Window Content', change_active_window_content),
                MenuSeparator(),
                Menu(
                    'Random',
                    [
                        MenuAction('Click Me', click_me),
                        MenuAction('File Dialog', open_save_file_dialog),
                    ],
                ),
            ],
        ),
        Menu('Menu 2', [MenuAction('This will do nothing', do_nothing)]),
    ]

    window_1 = webview.create_window(
        'Application Menu Example', 'https://pywebview.flowrl.com/hello'
    )
    window_2 = webview.create_window(
        'Window Menu Example',
        html='<h1>Another window to test application menu</h1>',
        menu=window_menu,
    )

    webview.start(menu=app_menu)