forked from lleino/joplin-ui-tests
-
Notifications
You must be signed in to change notification settings - Fork 0
/
menu.py
215 lines (197 loc) · 5.88 KB
/
menu.py
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
"""Helper functions for accessing menus of the joplin app."""
import dataclasses
import logging
from typing import Dict, List, Optional, Sequence
import pyautogui
@dataclasses.dataclass
class Entry:
"""Represents an arbitrary menu entry."""
name: str
subentries: Sequence = ()
NOTEBOOK_MENU_LAYOUT = (
Entry("New"),
Entry("Delete"),
Entry("Rename"),
Entry(
"Export", (Entry("JEX"), Entry("RAW"), Entry("MD"), Entry("HTML"), Entry("PDF"))
),
)
# Strings don't have to match the ui exactly, but should still be associable.
TOP_MENU_LAYOUT = (
Entry(
"File",
(
Entry("New note"),
Entry("New to-do"),
Entry("New notebook"),
Entry("New sub-notebook"),
Entry(
"Import",
(
Entry("JEX"),
Entry("MD (File)"),
Entry("MD (Directory)"),
Entry("RAW"),
Entry("ENEX (Markdown)"),
Entry("ENEX (HTML)"),
),
),
Entry(
"Export all",
(
Entry("JEX"),
Entry("RAW"),
Entry("MD"),
Entry("HTML"),
Entry("PDF"),
),
),
Entry("Synchronize"),
Entry("Print"),
Entry("Quit"),
),
),
Entry(
"Edit",
(
Entry("Copy"),
Entry("Cut"),
Entry("Paste"),
Entry("Select all"),
Entry("Undo"),
Entry("Redo"),
Entry("Bold"),
Entry("Italic"),
Entry("Hyperlink"),
Entry("Code"),
Entry("Insert Date Time"),
Entry("Attach file"),
Entry("Delete line"),
Entry("Duplicate line"),
Entry("Toggle comment"),
Entry("Sort selected lines"),
Entry("Indent less"),
Entry("Indent more"),
Entry("Swap line down"),
Entry("Swap line up"),
Entry("Search all notes"),
Entry("Search current note"),
),
),
Entry(
"View",
(
Entry("Change application layout"),
Entry("Toggle sidebar"),
Entry("Toggle note list"),
Entry("Toggle editor layout"),
Entry(
"Layout button sequence",
(
Entry("Editor / Viewer / Split view"),
Entry("Editor / Viewer"),
Entry("Editor / Split view"),
Entry("Viewer / Split view"),
),
),
Entry(
"Sort notes by",
(
Entry("Updated"),
Entry("Created"),
Entry("Title"),
Entry("Custom"),
Entry("Reverse"),
),
),
Entry(
"Sort notebooks by",
(Entry("Title"), Entry("Updated"), Entry("Reverse")),
),
Entry("Show note counts"),
Entry("Uncompleted todos on top"),
Entry("Show completed todos"),
Entry("Actual size"),
Entry("Zoom in"),
Entry("Zoom out"),
),
),
Entry(
"Go",
(
Entry("Back"),
Entry("Forward"),
Entry(
"Focus",
(
Entry("Sidebar"),
Entry("Note list"),
Entry("Note title"),
Entry("Note body"),
),
),
Entry("Goto anything"),
),
),
Entry("Notebook", (Entry("Share"),)),
Entry(
"Note",
(
Entry("Toggle external editing"),
Entry("Tags"),
Entry("Publish note"),
Entry("Statistics"),
),
),
Entry(
"Tools",
(
Entry("Options"),
Entry("Note attachments"),
Entry(
"Spell checker",
(Entry("Use"), Entry("[Language]"), Entry("Choose language")),
),
Entry("Command palette"),
),
),
Entry(
"Help",
(
Entry("Website and docs"),
Entry("Forum"),
Entry("Donation"),
Entry("Updates"),
Entry("Synchronisation status"),
Entry("Development tools"),
Entry("Safe mode"),
Entry("Open profile directory"),
Entry("Copy dev mode command"),
Entry("About"),
),
),
)
def choose_entry(position: int, key: str = "down", confirm: str = "enter"):
"""Select an entry from an arbitrary menu. The position has to be one based!"""
pyautogui.press(key, presses=position)
pyautogui.press(confirm)
def top(path: List[str], skip: Optional[Dict[str, int]] = None):
"""Select an entry from the top menu."""
# TODO: path gets modified, which can cause side effects
# skip non selectable entries
if skip is None:
skip = {}
logging.debug(f"Selecting {path} from top menu.")
pyautogui.press("alt") # focus the menu
# find toplevel entry and select it
toplevel_entry = path.pop(0)
match_index = [e.name for e in TOP_MENU_LAYOUT].index(toplevel_entry)
choose_entry(match_index, key="right")
pyautogui.press("down") # select the first entry, since match_index is zero based
last_entry = TOP_MENU_LAYOUT[match_index]
while path:
# find next entry and select it
next_entry = path.pop(0)
match_index = [e.name for e in last_entry.subentries].index(next_entry)
choose_entry(match_index - skip.get(next_entry, 0))
last_entry = last_entry.subentries[match_index]