dasha-framework 0.0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- dasha/__init__.py +9 -0
- dasha/__main__.py +534 -0
- dasha/animation.py +90 -0
- dasha/application.py +276 -0
- dasha/border.py +34 -0
- dasha/canvas.py +331 -0
- dasha/client.py +642 -0
- dasha/clipboard.py +94 -0
- dasha/config.py +89 -0
- dasha/desktop.py +58 -0
- dasha/dialog.py +601 -0
- dasha/events.py +471 -0
- dasha/filedialog.py +323 -0
- dasha/help.py +900 -0
- dasha/helppages/applications.md +25 -0
- dasha/helppages/index.md +20 -0
- dasha/helppages/keyboard.md +45 -0
- dasha/helppages/menus.md +32 -0
- dasha/helppages/running.md +18 -0
- dasha/helppages/settings.md +25 -0
- dasha/helppages/windows.md +36 -0
- dasha/host.py +120 -0
- dasha/hosting.py +1081 -0
- dasha/inbox.py +122 -0
- dasha/install.py +369 -0
- dasha/keytest.py +86 -0
- dasha/logs.py +104 -0
- dasha/manager.py +2468 -0
- dasha/markdown.py +1173 -0
- dasha/menu.py +701 -0
- dasha/printing.py +42 -0
- dasha/processes.py +293 -0
- dasha/protocol.py +335 -0
- dasha/pseudoterminal/__init__.py +43 -0
- dasha/pseudoterminal/_posix.py +133 -0
- dasha/pseudoterminal/_windows.py +352 -0
- dasha/pseudoterminal/base.py +151 -0
- dasha/py.typed +0 -0
- dasha/recovery.py +239 -0
- dasha/registry.py +475 -0
- dasha/remote.py +284 -0
- dasha/rundialog.py +342 -0
- dasha/scrollbar.py +193 -0
- dasha/settings/__init__.py +326 -0
- dasha/settings/appearance.py +353 -0
- dasha/settings/applications.py +165 -0
- dasha/settings/colours.py +94 -0
- dasha/settings/keys.py +225 -0
- dasha/settings/lists.py +11 -0
- dasha/shell.py +118 -0
- dasha/shortcuts.py +536 -0
- dasha/statusbar.py +111 -0
- dasha/style.py +256 -0
- dasha/terminal/__init__.py +236 -0
- dasha/terminal/_posix.py +130 -0
- dasha/terminal/_winbuffer.py +264 -0
- dasha/terminal/_windows.py +349 -0
- dasha/terminal/ansi.py +46 -0
- dasha/theme.py +749 -0
- dasha/vt.py +1216 -0
- dasha/widgets/__init__.py +90 -0
- dasha/widgets/autocomplete.py +153 -0
- dasha/widgets/base.py +298 -0
- dasha/widgets/button.py +224 -0
- dasha/widgets/checkbox.py +52 -0
- dasha/widgets/cluster.py +357 -0
- dasha/widgets/collapsible.py +224 -0
- dasha/widgets/combobox.py +157 -0
- dasha/widgets/container.py +182 -0
- dasha/widgets/editing.py +195 -0
- dasha/widgets/groupbox.py +126 -0
- dasha/widgets/label.py +106 -0
- dasha/widgets/listbox.py +535 -0
- dasha/widgets/paragraph.py +265 -0
- dasha/widgets/password.py +60 -0
- dasha/widgets/progressbar.py +196 -0
- dasha/widgets/radiobutton.py +61 -0
- dasha/widgets/scrollbar.py +157 -0
- dasha/widgets/slider.py +196 -0
- dasha/widgets/spinbox.py +237 -0
- dasha/widgets/splitter.py +274 -0
- dasha/widgets/switch.py +190 -0
- dasha/widgets/table.py +413 -0
- dasha/widgets/tabs.py +291 -0
- dasha/widgets/terminalview.py +475 -0
- dasha/widgets/textarea.py +990 -0
- dasha/widgets/textbox.py +591 -0
- dasha/widgets/tree.py +443 -0
- dasha/window.py +992 -0
- dasha/windowmenu.py +165 -0
- dasha_framework-0.0.1.dist-info/METADATA +346 -0
- dasha_framework-0.0.1.dist-info/RECORD +96 -0
- dasha_framework-0.0.1.dist-info/WHEEL +5 -0
- dasha_framework-0.0.1.dist-info/entry_points.txt +3 -0
- dasha_framework-0.0.1.dist-info/licenses/LICENSE +201 -0
- dasha_framework-0.0.1.dist-info/top_level.txt +1 -0
dasha/client.py
ADDED
|
@@ -0,0 +1,642 @@
|
|
|
1
|
+
"""The application's side of an application hosted by the desktop. The
|
|
2
|
+
application's windows live here, and the desktop shows them.
|
|
3
|
+
|
|
4
|
+
``Application.run()`` comes here when the desktop started the process (and
|
|
5
|
+
set ``DASHA_ADDRESS``). ``ClientManager`` stands in for the
|
|
6
|
+
presentation manager, with the same methods, so that application and
|
|
7
|
+
widget code cannot tell the difference: windows and dialogs open,
|
|
8
|
+
``execute`` waits for an answer, and ``pick`` drops down a list. Whatever
|
|
9
|
+
the desktop must do (drawing the frames, moving windows, showing menus) is
|
|
10
|
+
sent to it as messages, and keys, mouse events and the rest come back the
|
|
11
|
+
same way.
|
|
12
|
+
|
|
13
|
+
Each window's interior is drawn here, onto a canvas of its own, and the rows
|
|
14
|
+
that have changed since the last time are sent. The desktop draws the frame
|
|
15
|
+
around them.
|
|
16
|
+
|
|
17
|
+
While it waits for a message, the application can be woken from another of
|
|
18
|
+
its threads (``call_from_thread``): through a pipe of its own, which it
|
|
19
|
+
waits on together with the desktop's connection.
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
import json
|
|
23
|
+
import os
|
|
24
|
+
import sys
|
|
25
|
+
import threading
|
|
26
|
+
import time
|
|
27
|
+
from collections.abc import Callable, Sequence
|
|
28
|
+
from dataclasses import dataclass, field
|
|
29
|
+
from multiprocessing.connection import Client, Connection, Pipe, wait
|
|
30
|
+
from pathlib import Path
|
|
31
|
+
from typing import Any, cast
|
|
32
|
+
|
|
33
|
+
from dasha import theme
|
|
34
|
+
from dasha.application import Application
|
|
35
|
+
from dasha.clipboard import CLIPBOARD, TEXT, Clipboard, ClipboardItem
|
|
36
|
+
from dasha.dialog import Dialog, Popup
|
|
37
|
+
from dasha.manager import PresentationManager, drawn_active
|
|
38
|
+
from dasha.menu import Menu
|
|
39
|
+
from dasha.printing import say
|
|
40
|
+
from dasha.processes import DESKTOP_PID_VARIABLE, watch_parent
|
|
41
|
+
from dasha.protocol import (
|
|
42
|
+
ADDRESS_VARIABLE,
|
|
43
|
+
MINIMUM_VERSION,
|
|
44
|
+
PROTOCOL_VERSION,
|
|
45
|
+
WINDOW_STYLES,
|
|
46
|
+
Message,
|
|
47
|
+
StyleTable,
|
|
48
|
+
decode_mouse,
|
|
49
|
+
decode_rect,
|
|
50
|
+
encode_rect,
|
|
51
|
+
encode_row,
|
|
52
|
+
encode_style,
|
|
53
|
+
receive,
|
|
54
|
+
send,
|
|
55
|
+
)
|
|
56
|
+
from dasha.shortcuts import Shortcuts
|
|
57
|
+
from dasha.statusbar import status_text
|
|
58
|
+
from dasha.widgets import Widget
|
|
59
|
+
from dasha.window import Window
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def encode_menus(menus: Sequence[Menu]) -> list[dict[str, object]]:
|
|
63
|
+
"""Menus encoded as data for the desktop to show: their titles, items and
|
|
64
|
+
submenus, and whether each goes after the Window menu."""
|
|
65
|
+
|
|
66
|
+
def items(menu: Menu) -> list[dict[str, object]]:
|
|
67
|
+
return [
|
|
68
|
+
{
|
|
69
|
+
"label": item.label,
|
|
70
|
+
"shortcut": item.shortcut,
|
|
71
|
+
"status": item.status,
|
|
72
|
+
"separator": item.is_separator,
|
|
73
|
+
"enabled": item.is_available,
|
|
74
|
+
"submenu": (
|
|
75
|
+
None if item.submenu is None else encode_menus([item.submenu])[0]
|
|
76
|
+
),
|
|
77
|
+
}
|
|
78
|
+
for item in menu.items
|
|
79
|
+
]
|
|
80
|
+
|
|
81
|
+
return [
|
|
82
|
+
{"title": menu.title, "after_window": menu.after_window, "items": items(menu)}
|
|
83
|
+
for menu in menus
|
|
84
|
+
]
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def look_of(window: Window) -> str:
|
|
88
|
+
"""How the desktop should frame a window: ``"window"``, ``"dialog"`` or
|
|
89
|
+
``"popup"`` (for a drop-down list or a list of suggestions)."""
|
|
90
|
+
if isinstance(window, Popup):
|
|
91
|
+
return "popup"
|
|
92
|
+
return "dialog" if isinstance(window, Dialog) else "window"
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def colours_of(window: Window) -> dict[str, list[object]]:
|
|
96
|
+
"""The window's own colours, for the desktop to draw its frame in: those
|
|
97
|
+
that differ from the theme's for its look. The rest follow the theme."""
|
|
98
|
+
theme_class = Dialog if isinstance(window, Dialog) else Window
|
|
99
|
+
return {
|
|
100
|
+
name: encode_style(getattr(window, name))
|
|
101
|
+
for name in WINDOW_STYLES
|
|
102
|
+
if getattr(window, name) != getattr(theme_class, name)
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
@dataclass
|
|
107
|
+
class _Sent:
|
|
108
|
+
"""What the desktop was last told about a window, so only changes go."""
|
|
109
|
+
|
|
110
|
+
state: dict[str, object] = field(default_factory=dict)
|
|
111
|
+
rows: list[list[list[object]]] = field(default_factory=list)
|
|
112
|
+
cursor: tuple[int, int] | None = None
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
class _Waker:
|
|
116
|
+
"""``call_from_thread``'s wake-ups for an application's loop: a byte
|
|
117
|
+
down a pipe, which the loop waits on together with the desktop's
|
|
118
|
+
connection. Only one is sent until the loop has taken it."""
|
|
119
|
+
|
|
120
|
+
def __init__(self) -> None:
|
|
121
|
+
self.reader, self._writer = Pipe(duplex=False)
|
|
122
|
+
"""What the loop waits on (with ``wait``)."""
|
|
123
|
+
self._lock = threading.Lock()
|
|
124
|
+
self._sent = False
|
|
125
|
+
|
|
126
|
+
def wake(self) -> None:
|
|
127
|
+
"""End the loop's wait, if a wake-up is not already on its way.
|
|
128
|
+
Safe to call from any thread."""
|
|
129
|
+
with self._lock:
|
|
130
|
+
if self._sent or self._writer.closed:
|
|
131
|
+
return
|
|
132
|
+
try:
|
|
133
|
+
self._writer.send_bytes(b"!")
|
|
134
|
+
except OSError:
|
|
135
|
+
return
|
|
136
|
+
self._sent = True
|
|
137
|
+
|
|
138
|
+
def take(self) -> None:
|
|
139
|
+
"""Take the wake-up out of the pipe, so that the next can be sent."""
|
|
140
|
+
with self._lock:
|
|
141
|
+
self._sent = False
|
|
142
|
+
while self.reader.poll():
|
|
143
|
+
self.reader.recv_bytes()
|
|
144
|
+
|
|
145
|
+
def close(self) -> None:
|
|
146
|
+
"""Close the pipe; waking does nothing after this."""
|
|
147
|
+
with self._lock:
|
|
148
|
+
self.reader.close()
|
|
149
|
+
self._writer.close()
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
class ClientManager(PresentationManager):
|
|
153
|
+
"""The presentation manager of an application run by a desktop in its
|
|
154
|
+
own process."""
|
|
155
|
+
|
|
156
|
+
def __init__(self, connection: Connection, welcome: Message) -> None:
|
|
157
|
+
super().__init__(shortcuts=Shortcuts(welcome.get("shortcuts") or {}))
|
|
158
|
+
self.connection = connection
|
|
159
|
+
self.version: int = welcome.get("version", PROTOCOL_VERSION)
|
|
160
|
+
"""The protocol version agreed with the desktop."""
|
|
161
|
+
columns, lines = welcome["size"]
|
|
162
|
+
self._size = (int(columns), int(lines))
|
|
163
|
+
self._running = True
|
|
164
|
+
self._ids: dict[int, Window] = {}
|
|
165
|
+
self._window_ids: dict[Window, int] = {}
|
|
166
|
+
self._next_id = 1
|
|
167
|
+
self._sent: dict[int, _Sent] = {}
|
|
168
|
+
self._active: int | None = None
|
|
169
|
+
self._modal: list[Dialog] = []
|
|
170
|
+
self._quiet_clipboard = False
|
|
171
|
+
self._sent_menus = ""
|
|
172
|
+
self._waker = _Waker()
|
|
173
|
+
CLIPBOARD.listeners.append(self._clipboard_changed)
|
|
174
|
+
name = welcome.get("theme_name")
|
|
175
|
+
self.theme_name = name if isinstance(name, str) else None
|
|
176
|
+
if isinstance(welcome.get("clipboard"), str):
|
|
177
|
+
self._clipboard_arrived({"type": "clipboard", "text": welcome["clipboard"]})
|
|
178
|
+
|
|
179
|
+
@property
|
|
180
|
+
def can_show_dialogs(self) -> bool:
|
|
181
|
+
return self._running
|
|
182
|
+
|
|
183
|
+
# Windows
|
|
184
|
+
|
|
185
|
+
def window_id(self, window: Window) -> int:
|
|
186
|
+
"""The number by which the desktop identifies a window."""
|
|
187
|
+
number = self._window_ids.get(window)
|
|
188
|
+
if number is None:
|
|
189
|
+
number = self._window_ids[window] = self._next_id
|
|
190
|
+
self._ids[number] = window
|
|
191
|
+
self._next_id += 1
|
|
192
|
+
return number
|
|
193
|
+
|
|
194
|
+
def add(self, window: Window) -> None:
|
|
195
|
+
super().add(window)
|
|
196
|
+
self.window_id(window) # told to the desktop with the next flush
|
|
197
|
+
|
|
198
|
+
def remove(self, window: Window) -> None:
|
|
199
|
+
number = self._window_ids.pop(window, None)
|
|
200
|
+
if number is not None:
|
|
201
|
+
del self._ids[number]
|
|
202
|
+
if self._sent.pop(number, None) is not None:
|
|
203
|
+
self._send("close", id=number)
|
|
204
|
+
super().remove(window)
|
|
205
|
+
|
|
206
|
+
def bring_to_front(self, window: Window) -> None:
|
|
207
|
+
super().bring_to_front(window)
|
|
208
|
+
self._request(window, "front")
|
|
209
|
+
|
|
210
|
+
def send_to_back(self, window: Window) -> None:
|
|
211
|
+
self._request(window, "send_to_back")
|
|
212
|
+
|
|
213
|
+
def activate_next(self) -> None:
|
|
214
|
+
self._send("request", id=None, op="next_window")
|
|
215
|
+
|
|
216
|
+
def activate_previous(self) -> None:
|
|
217
|
+
self._send("request", id=None, op="previous_window")
|
|
218
|
+
|
|
219
|
+
def maximise(self, window: Window) -> None:
|
|
220
|
+
self._request(window, "maximise")
|
|
221
|
+
|
|
222
|
+
def minimise(self, window: Window) -> None:
|
|
223
|
+
self._request(window, "minimise")
|
|
224
|
+
|
|
225
|
+
def restore(self, window: Window) -> None:
|
|
226
|
+
self._request(window, "restore")
|
|
227
|
+
|
|
228
|
+
def toggle_maximise(self, window: Window) -> None:
|
|
229
|
+
self._request(window, "toggle_maximise")
|
|
230
|
+
|
|
231
|
+
def start_moving(self, window: Window | None = None) -> None:
|
|
232
|
+
self._request(window, "move")
|
|
233
|
+
|
|
234
|
+
def start_resizing(self, window: Window | None = None) -> None:
|
|
235
|
+
self._request(window, "resize")
|
|
236
|
+
|
|
237
|
+
def request_exit(self) -> None:
|
|
238
|
+
self._send("request", id=None, op="exit")
|
|
239
|
+
|
|
240
|
+
def set_theme(self, name: str | Path, *, remember: bool | None = None) -> bool:
|
|
241
|
+
"""Ask the desktop to change to a theme, as its system menu would. The
|
|
242
|
+
colours of everything on the desktop change, including this
|
|
243
|
+
application's, once the desktop has applied the theme (and sent a
|
|
244
|
+
``theme`` message back). The desktop does this only while this is
|
|
245
|
+
the application the user is working in, and remembers the choice as
|
|
246
|
+
it would for its own menu. A theme file is looked for from this
|
|
247
|
+
process. Always returns ``True``, because the change was
|
|
248
|
+
requested."""
|
|
249
|
+
del remember # the desktop's to decide
|
|
250
|
+
text = str(name)
|
|
251
|
+
if isinstance(name, Path) or any(m in text for m in ("/", "\\", ".json")):
|
|
252
|
+
text = str(Path(text).expanduser().resolve())
|
|
253
|
+
self._send("theme", name=text)
|
|
254
|
+
return True
|
|
255
|
+
|
|
256
|
+
def _request(self, window: Window | None, op: str) -> None:
|
|
257
|
+
self.flush() # so the desktop knows the window first
|
|
258
|
+
number = self._window_ids.get(window) if window is not None else None
|
|
259
|
+
self._send("request", id=number, op=op)
|
|
260
|
+
|
|
261
|
+
# Dialogs
|
|
262
|
+
|
|
263
|
+
@property
|
|
264
|
+
def answering_dialog(self) -> Dialog | None:
|
|
265
|
+
return self._modal[-1] if self._modal else None
|
|
266
|
+
|
|
267
|
+
def execute(self, dialog: Dialog) -> str | None:
|
|
268
|
+
"""Show a dialog and wait for its answer. The dialog blocks only this
|
|
269
|
+
application's other windows; the rest of the desktop carries on."""
|
|
270
|
+
dialog.result = None
|
|
271
|
+
if dialog.centred:
|
|
272
|
+
dialog.centre(self.workspace)
|
|
273
|
+
self._modal.append(dialog)
|
|
274
|
+
try:
|
|
275
|
+
if dialog not in self._windows:
|
|
276
|
+
self.add(dialog)
|
|
277
|
+
self.bring_to_front(dialog)
|
|
278
|
+
while dialog.result is None and self._running:
|
|
279
|
+
self.pump()
|
|
280
|
+
finally:
|
|
281
|
+
self._modal.remove(dialog)
|
|
282
|
+
if dialog in self._windows:
|
|
283
|
+
self.remove(dialog)
|
|
284
|
+
return dialog.result if self._running else None
|
|
285
|
+
|
|
286
|
+
# The loop
|
|
287
|
+
|
|
288
|
+
def serve(self) -> None:
|
|
289
|
+
"""Handle messages until the application ends or the desktop goes
|
|
290
|
+
away. First calls whatever ``call_when_running`` held back."""
|
|
291
|
+
try:
|
|
292
|
+
self._now_running()
|
|
293
|
+
while self._running:
|
|
294
|
+
self.pump()
|
|
295
|
+
finally:
|
|
296
|
+
self.stop_listening()
|
|
297
|
+
if not self.connection.closed:
|
|
298
|
+
self._send("bye")
|
|
299
|
+
self.connection.close()
|
|
300
|
+
self._waker.close()
|
|
301
|
+
|
|
302
|
+
def pump(self) -> None:
|
|
303
|
+
"""Bring the desktop up to date, then handle one message (or run the
|
|
304
|
+
widgets' timers, if one is due first, or what ``call_from_thread``
|
|
305
|
+
has been given, if woken)."""
|
|
306
|
+
wakes = [wake for w in self._widgets() if (wake := w.wake_time()) is not None]
|
|
307
|
+
timeout = max(0.0, min(wakes) - time.monotonic()) if wakes else None
|
|
308
|
+
if self._from_threads:
|
|
309
|
+
timeout = 0.0
|
|
310
|
+
try:
|
|
311
|
+
if not self.connection.poll(0):
|
|
312
|
+
self.flush()
|
|
313
|
+
if self._running:
|
|
314
|
+
ready = wait([self.connection, self._waker.reader], timeout)
|
|
315
|
+
if self._waker.reader in ready:
|
|
316
|
+
self._waker.take()
|
|
317
|
+
if self.connection in ready:
|
|
318
|
+
self.handle(receive(self.connection))
|
|
319
|
+
except (EOFError, OSError):
|
|
320
|
+
self._running = False # the desktop has gone
|
|
321
|
+
return
|
|
322
|
+
self.run_timers(time.monotonic())
|
|
323
|
+
self._call_from_threads()
|
|
324
|
+
|
|
325
|
+
def wake(self) -> None:
|
|
326
|
+
"""Cut short the wait for a message from the desktop, so that the
|
|
327
|
+
loop comes round at once (see ``call_from_thread``). Safe to call
|
|
328
|
+
from any thread."""
|
|
329
|
+
self._waker.wake()
|
|
330
|
+
|
|
331
|
+
def _send(self, kind: str, **fields: object) -> None:
|
|
332
|
+
if self.connection.closed:
|
|
333
|
+
return
|
|
334
|
+
try:
|
|
335
|
+
send(self.connection, kind, **fields)
|
|
336
|
+
except OSError:
|
|
337
|
+
self._running = False
|
|
338
|
+
|
|
339
|
+
# Telling the desktop
|
|
340
|
+
|
|
341
|
+
def flush(self) -> None:
|
|
342
|
+
"""Send whatever has changed: the application's menus (including
|
|
343
|
+
whether each item is enabled), and its windows' frames and
|
|
344
|
+
contents."""
|
|
345
|
+
menus = json.dumps(
|
|
346
|
+
encode_menus([menu for app in self._applications for menu in app.menus])
|
|
347
|
+
)
|
|
348
|
+
if menus != self._sent_menus:
|
|
349
|
+
self._send("menus", menus=json.loads(menus))
|
|
350
|
+
self._sent_menus = menus
|
|
351
|
+
for window in list(self._windows):
|
|
352
|
+
self._flush_window(window)
|
|
353
|
+
|
|
354
|
+
def _state(self, window: Window) -> dict[str, object]:
|
|
355
|
+
focused = window.focused
|
|
356
|
+
width, height = window.content_size
|
|
357
|
+
return {
|
|
358
|
+
"title": window.title,
|
|
359
|
+
"footer": window.footer_parts(),
|
|
360
|
+
"rect": encode_rect(window.rect),
|
|
361
|
+
"maximised": window.maximised,
|
|
362
|
+
"flags": [
|
|
363
|
+
window.resizable,
|
|
364
|
+
window.maximisable,
|
|
365
|
+
window.minimisable,
|
|
366
|
+
window.closable,
|
|
367
|
+
],
|
|
368
|
+
"look": look_of(window),
|
|
369
|
+
"colours": colours_of(window),
|
|
370
|
+
"modal": window in self._modal,
|
|
371
|
+
"outside": window.close_on_outside_click,
|
|
372
|
+
"status": status_text(focused) or status_text(window),
|
|
373
|
+
"hints": [] if focused is None else focused.key_help(self.shortcuts),
|
|
374
|
+
"content": [width, height],
|
|
375
|
+
"scroll": [window.scroll_row, window.scroll_column],
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
def _flush_window(self, window: Window) -> None:
|
|
379
|
+
number = self.window_id(window)
|
|
380
|
+
sent = self._sent.setdefault(number, _Sent())
|
|
381
|
+
state = self._state(window)
|
|
382
|
+
if state != sent.state:
|
|
383
|
+
self._send("window", id=number, **state)
|
|
384
|
+
sent.state = state
|
|
385
|
+
canvas = window.render_interior(self.looks_active(window))
|
|
386
|
+
table = StyleTable()
|
|
387
|
+
rows = [
|
|
388
|
+
encode_row([canvas.cell(row, c) for c in range(canvas.width)], table)
|
|
389
|
+
for row in range(canvas.height)
|
|
390
|
+
]
|
|
391
|
+
# Compared by their colours, not their numbers in this message's
|
|
392
|
+
# table: a row whose colours alone change (a window's body colours,
|
|
393
|
+
# say) keeps the same numbers, but must be sent again.
|
|
394
|
+
shown = [
|
|
395
|
+
[[text, table.styles[cast(int, style)]] for text, style in runs]
|
|
396
|
+
for runs in rows
|
|
397
|
+
]
|
|
398
|
+
size_changed = len(shown) != len(sent.rows)
|
|
399
|
+
changed = [
|
|
400
|
+
[number_, runs]
|
|
401
|
+
for number_, runs in enumerate(rows)
|
|
402
|
+
if size_changed or shown[number_] != sent.rows[number_]
|
|
403
|
+
]
|
|
404
|
+
if changed or canvas.cursor != sent.cursor or size_changed:
|
|
405
|
+
self._send(
|
|
406
|
+
"paint",
|
|
407
|
+
id=number,
|
|
408
|
+
size=[canvas.width, canvas.height],
|
|
409
|
+
rows=changed,
|
|
410
|
+
styles=table.styles,
|
|
411
|
+
cursor=canvas.cursor,
|
|
412
|
+
)
|
|
413
|
+
sent.rows, sent.cursor = shown, canvas.cursor
|
|
414
|
+
|
|
415
|
+
def _told(self, window: Window, **changes: object) -> None:
|
|
416
|
+
"""Note what the desktop already knows, so it is not sent back."""
|
|
417
|
+
sent = self._sent.get(self.window_id(window))
|
|
418
|
+
if sent is not None:
|
|
419
|
+
sent.state.update(changes)
|
|
420
|
+
|
|
421
|
+
# Hearing from the desktop
|
|
422
|
+
|
|
423
|
+
def handle(self, message: Message) -> None:
|
|
424
|
+
"""Handle one message from the desktop."""
|
|
425
|
+
handlers: dict[str, Callable[[Message], None]] = {
|
|
426
|
+
"quit": self._quit_message,
|
|
427
|
+
"ping": lambda _: self._send("pong"),
|
|
428
|
+
"may_close": self._may_close,
|
|
429
|
+
"size": self._resized,
|
|
430
|
+
"clipboard": self._clipboard_arrived,
|
|
431
|
+
"theme": self._theme_arrived,
|
|
432
|
+
"shortcuts": lambda m: self.set_shortcuts(Shortcuts(m["shortcuts"])),
|
|
433
|
+
"active": lambda m: self._activated(m.get("id")),
|
|
434
|
+
"menu": self._menu_message,
|
|
435
|
+
"help": lambda _: self.press_help(),
|
|
436
|
+
}
|
|
437
|
+
handler = handlers.get(message["type"])
|
|
438
|
+
if handler is not None:
|
|
439
|
+
handler(message)
|
|
440
|
+
return
|
|
441
|
+
window = self._ids.get(message.get("id", -1))
|
|
442
|
+
if window is not None:
|
|
443
|
+
self._window_message(window, message["type"], message)
|
|
444
|
+
|
|
445
|
+
def _quit_message(self, message: Message) -> None:
|
|
446
|
+
del message
|
|
447
|
+
for application in list(self._applications):
|
|
448
|
+
application.quit()
|
|
449
|
+
self._running = False
|
|
450
|
+
|
|
451
|
+
def _theme_arrived(self, message: Message) -> None:
|
|
452
|
+
"""The desktop's theme changed: take it on, and draw afresh."""
|
|
453
|
+
colours = theme.build_theme(json.loads(str(message["theme"])))
|
|
454
|
+
name = message.get("name")
|
|
455
|
+
self.use_theme(colours, name if isinstance(name, str) else None)
|
|
456
|
+
|
|
457
|
+
def _may_close(self, message: Message) -> None:
|
|
458
|
+
del message
|
|
459
|
+
ok = all(application.can_close() for application in self.applications)
|
|
460
|
+
self._send("answer", question="may_close", ok=ok)
|
|
461
|
+
|
|
462
|
+
def _resized(self, message: Message) -> None:
|
|
463
|
+
self._size = (int(message["columns"]), int(message["lines"]))
|
|
464
|
+
|
|
465
|
+
def _menu_message(self, message: Message) -> None:
|
|
466
|
+
"""A menu item chosen; not while a dialog waits for its answer. Then
|
|
467
|
+
a shortcut (chosen before the desktop knew of the dialog) goes to
|
|
468
|
+
the dialog as the key it was, as it would in one process."""
|
|
469
|
+
if not self._modal:
|
|
470
|
+
menus = [menu for app in self._applications for menu in app.menus]
|
|
471
|
+
self._menu_chosen(menus, message["path"])
|
|
472
|
+
elif isinstance(key := message.get("key"), str):
|
|
473
|
+
self._input(self._modal[-1], "key", {"type": "key", "key": key})
|
|
474
|
+
|
|
475
|
+
def looks_active(self, window: Window) -> bool:
|
|
476
|
+
"""As for any presentation manager, except that the active window is
|
|
477
|
+
the one the desktop says is active (which may belong to another
|
|
478
|
+
application)."""
|
|
479
|
+
number = self._active
|
|
480
|
+
active = self._ids.get(number) if number is not None else None
|
|
481
|
+
return drawn_active(window, active, self._windows)
|
|
482
|
+
|
|
483
|
+
def _activated(self, number: object) -> None:
|
|
484
|
+
self._active = number if isinstance(number, int) else None
|
|
485
|
+
window = self._ids.get(self._active) if self._active is not None else None
|
|
486
|
+
if window is not None and window in self._windows:
|
|
487
|
+
super().bring_to_front(window) # keep this side's order the same
|
|
488
|
+
|
|
489
|
+
def _blocked(self, window: Window) -> bool:
|
|
490
|
+
"""Whether a dialog of this application's is waiting, elsewhere."""
|
|
491
|
+
return bool(self._modal) and window is not self._modal[-1]
|
|
492
|
+
|
|
493
|
+
def _window_message(self, window: Window, kind: str, message: Message) -> None:
|
|
494
|
+
if kind == "rect":
|
|
495
|
+
rect = decode_rect(message["rect"])
|
|
496
|
+
window.maximised = bool(message["maximised"])
|
|
497
|
+
window.rect = rect
|
|
498
|
+
self._told(window, rect=encode_rect(rect), maximised=window.maximised)
|
|
499
|
+
elif kind == "scroll":
|
|
500
|
+
window.scroll_to(int(message["row"]), int(message["column"]))
|
|
501
|
+
self._told(window, scroll=[window.scroll_row, window.scroll_column])
|
|
502
|
+
elif kind == "outside":
|
|
503
|
+
if isinstance(window, Dialog) and window.close_on_outside_click:
|
|
504
|
+
window.cancel()
|
|
505
|
+
elif self._blocked(window):
|
|
506
|
+
# A dialog of this application's is waiting for an answer. Typing
|
|
507
|
+
# sent before the desktop knew of it goes to it, as it would in
|
|
508
|
+
# one process; clicks and the rest are for the window: ignored.
|
|
509
|
+
if kind in ("key", "release", "paste"):
|
|
510
|
+
self._input(self._modal[-1], kind, message)
|
|
511
|
+
elif kind == "close_request":
|
|
512
|
+
self.close(window)
|
|
513
|
+
else:
|
|
514
|
+
self._input(window, kind, message)
|
|
515
|
+
|
|
516
|
+
def _input(self, window: Window, kind: str, message: Message) -> None:
|
|
517
|
+
if kind == "key":
|
|
518
|
+
key = str(message["key"])
|
|
519
|
+
if not window.handle_key(key, self.shortcuts):
|
|
520
|
+
if self.shortcuts.matches("help", key):
|
|
521
|
+
self.press_help()
|
|
522
|
+
elif isinstance(window, Dialog) and self.shortcuts.matches(
|
|
523
|
+
"dialog_cancel", key
|
|
524
|
+
):
|
|
525
|
+
window.cancel()
|
|
526
|
+
elif kind == "release":
|
|
527
|
+
window.handle_key_release(str(message["key"]))
|
|
528
|
+
elif kind == "paste":
|
|
529
|
+
window.handle_paste(str(message["text"]))
|
|
530
|
+
elif kind == "mouse":
|
|
531
|
+
event = decode_mouse(message["event"])
|
|
532
|
+
if window.handle_mouse(event) or event.action != "scroll":
|
|
533
|
+
return
|
|
534
|
+
notch = 3 # the wheel, where no widget took it: the window scrolls
|
|
535
|
+
amount = -notch if event.button in ("up", "left") else notch
|
|
536
|
+
if event.button in ("left", "right") or event.shift:
|
|
537
|
+
window.scroll_by(0, amount)
|
|
538
|
+
else:
|
|
539
|
+
window.scroll_by(amount, 0)
|
|
540
|
+
|
|
541
|
+
@staticmethod
|
|
542
|
+
def _menu_chosen(menus: Sequence[Menu], path: Sequence[Any]) -> None:
|
|
543
|
+
"""Run the menu item at ``path``: a menu's index, then an item's in
|
|
544
|
+
it, then (for submenus) an item's in that, if it can be chosen: it,
|
|
545
|
+
and every item whose submenu it is in."""
|
|
546
|
+
if not path:
|
|
547
|
+
return
|
|
548
|
+
try:
|
|
549
|
+
menu = menus[int(path[0])]
|
|
550
|
+
item = menu.items[int(path[1])]
|
|
551
|
+
for step in path[2:]:
|
|
552
|
+
assert item.submenu is not None and item.is_available
|
|
553
|
+
item = item.submenu.items[int(step)]
|
|
554
|
+
except (IndexError, AssertionError, ValueError):
|
|
555
|
+
return
|
|
556
|
+
if item.is_available and item.action is not None:
|
|
557
|
+
item.action()
|
|
558
|
+
|
|
559
|
+
# The clipboard, shared through the desktop
|
|
560
|
+
|
|
561
|
+
def stop_listening(self) -> None:
|
|
562
|
+
"""Stop sending clipboard changes to the desktop. The manager listens
|
|
563
|
+
for them from the start, and ``serve`` stops listening when it ends;
|
|
564
|
+
code that creates a ``ClientManager`` without serving it (such as a
|
|
565
|
+
test) calls this itself. Calling it again does nothing."""
|
|
566
|
+
if self._clipboard_changed in CLIPBOARD.listeners:
|
|
567
|
+
CLIPBOARD.listeners.remove(self._clipboard_changed)
|
|
568
|
+
|
|
569
|
+
def _clipboard_changed(self, clipboard: Clipboard) -> None:
|
|
570
|
+
item = clipboard.item
|
|
571
|
+
if self._quiet_clipboard or item is None or item.type != TEXT:
|
|
572
|
+
return
|
|
573
|
+
self._send("clipboard", text=item.data)
|
|
574
|
+
|
|
575
|
+
def _clipboard_arrived(self, message: Message) -> None:
|
|
576
|
+
self._quiet_clipboard = True
|
|
577
|
+
try:
|
|
578
|
+
CLIPBOARD.copy(ClipboardItem(TEXT, str(message["text"])))
|
|
579
|
+
finally:
|
|
580
|
+
self._quiet_clipboard = False
|
|
581
|
+
|
|
582
|
+
|
|
583
|
+
def connect() -> tuple[Connection, str] | None:
|
|
584
|
+
"""Connect to the desktop that started this process, using
|
|
585
|
+
``DASHA_ADDRESS`` and the key on standard input. Returns the
|
|
586
|
+
connection and the launch token with which to introduce this process, or
|
|
587
|
+
``None``, after explaining why on standard error."""
|
|
588
|
+
address = os.environ.pop(ADDRESS_VARIABLE, "")
|
|
589
|
+
secret, _, token = sys.stdin.readline().strip().partition(" ")
|
|
590
|
+
try:
|
|
591
|
+
client = Client(address, authkey=bytes.fromhex(secret))
|
|
592
|
+
# Needed on Windows only, where Client may give a PipeConnection.
|
|
593
|
+
connection = cast(Connection, client) # type: ignore[redundant-cast,unused-ignore]
|
|
594
|
+
except (OSError, ValueError) as error:
|
|
595
|
+
say(f"dasha: cannot reach the desktop: {error}", file=sys.stderr)
|
|
596
|
+
return None
|
|
597
|
+
return connection, token
|
|
598
|
+
|
|
599
|
+
|
|
600
|
+
def run_hosted(application: Application) -> int:
|
|
601
|
+
"""Run an application on the desktop that started this process."""
|
|
602
|
+
connected = connect()
|
|
603
|
+
if connected is None:
|
|
604
|
+
return 1
|
|
605
|
+
connection, token = connected
|
|
606
|
+
desktop = os.environ.pop(DESKTOP_PID_VARIABLE, "")
|
|
607
|
+
# If the desktop goes, so does this, hung or not.
|
|
608
|
+
watch_parent(int(desktop) if desktop.isdigit() else None)
|
|
609
|
+
try:
|
|
610
|
+
send(
|
|
611
|
+
connection,
|
|
612
|
+
"hello",
|
|
613
|
+
version=PROTOCOL_VERSION,
|
|
614
|
+
versions=[MINIMUM_VERSION, PROTOCOL_VERSION],
|
|
615
|
+
token=token,
|
|
616
|
+
name=application.name,
|
|
617
|
+
pid=os.getpid(),
|
|
618
|
+
)
|
|
619
|
+
welcome = receive(connection)
|
|
620
|
+
except (EOFError, OSError):
|
|
621
|
+
say("dasha: the desktop went away", file=sys.stderr)
|
|
622
|
+
return 1
|
|
623
|
+
if welcome["type"] != "welcome":
|
|
624
|
+
say(f"dasha: refused: {welcome.get('reason', '?')}", file=sys.stderr)
|
|
625
|
+
return 1
|
|
626
|
+
if welcome.get("version") not in range(MINIMUM_VERSION, PROTOCOL_VERSION + 1):
|
|
627
|
+
say(
|
|
628
|
+
"dasha: the desktop chose a protocol version this does not speak",
|
|
629
|
+
file=sys.stderr,
|
|
630
|
+
)
|
|
631
|
+
return 1
|
|
632
|
+
if welcome.get("theme"):
|
|
633
|
+
theme.apply_theme(theme.build_theme(json.loads(str(welcome["theme"]))))
|
|
634
|
+
Widget.key_releases = bool(welcome.get("key_releases"))
|
|
635
|
+
try:
|
|
636
|
+
manager = ClientManager(connection, welcome)
|
|
637
|
+
manager.quit_when_empty = True
|
|
638
|
+
manager.launch(application)
|
|
639
|
+
manager.serve()
|
|
640
|
+
finally:
|
|
641
|
+
Widget.key_releases = False # as the desktop's own run leaves it
|
|
642
|
+
return 0
|