driverkit 0.0.0-stage → 0.1.0

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.
Files changed (159) hide show
  1. package/CONTROLS.md +578 -0
  2. package/README.md +104 -2
  3. package/dist/agent/backend.d.ts +69 -0
  4. package/dist/agent/backend.d.ts.map +1 -0
  5. package/dist/agent/backend.js +130 -0
  6. package/dist/agent/backend.js.map +1 -0
  7. package/dist/agent/control.d.ts +148 -0
  8. package/dist/agent/control.d.ts.map +1 -0
  9. package/dist/agent/control.js +342 -0
  10. package/dist/agent/control.js.map +1 -0
  11. package/dist/agent/index.d.ts +9 -0
  12. package/dist/agent/index.d.ts.map +1 -0
  13. package/dist/agent/index.js +6 -0
  14. package/dist/agent/index.js.map +1 -0
  15. package/dist/agent/loop.d.ts +136 -0
  16. package/dist/agent/loop.d.ts.map +1 -0
  17. package/dist/agent/loop.js +824 -0
  18. package/dist/agent/loop.js.map +1 -0
  19. package/dist/agent/render.d.ts +173 -0
  20. package/dist/agent/render.d.ts.map +1 -0
  21. package/dist/agent/render.js +425 -0
  22. package/dist/agent/render.js.map +1 -0
  23. package/dist/agent/tools.d.ts +138 -0
  24. package/dist/agent/tools.d.ts.map +1 -0
  25. package/dist/agent/tools.js +622 -0
  26. package/dist/agent/tools.js.map +1 -0
  27. package/dist/agent/types.d.ts +566 -0
  28. package/dist/agent/types.d.ts.map +1 -0
  29. package/dist/agent/types.js +32 -0
  30. package/dist/agent/types.js.map +1 -0
  31. package/dist/browser/budget.d.ts +92 -0
  32. package/dist/browser/budget.d.ts.map +1 -0
  33. package/dist/browser/budget.js +151 -0
  34. package/dist/browser/budget.js.map +1 -0
  35. package/dist/browser/connect.d.ts +368 -0
  36. package/dist/browser/connect.d.ts.map +1 -0
  37. package/dist/browser/connect.js +1038 -0
  38. package/dist/browser/connect.js.map +1 -0
  39. package/dist/browser/elsewhere.d.ts +89 -0
  40. package/dist/browser/elsewhere.d.ts.map +1 -0
  41. package/dist/browser/elsewhere.js +91 -0
  42. package/dist/browser/elsewhere.js.map +1 -0
  43. package/dist/browser/frames.d.ts +85 -0
  44. package/dist/browser/frames.d.ts.map +1 -0
  45. package/dist/browser/frames.js +11 -0
  46. package/dist/browser/frames.js.map +1 -0
  47. package/dist/browser/index.d.ts +8 -0
  48. package/dist/browser/index.d.ts.map +1 -0
  49. package/dist/browser/index.js +9 -0
  50. package/dist/browser/index.js.map +1 -0
  51. package/dist/check/check.d.ts +171 -0
  52. package/dist/check/check.d.ts.map +1 -0
  53. package/dist/check/check.js +666 -0
  54. package/dist/check/check.js.map +1 -0
  55. package/dist/check/index.d.ts +3 -0
  56. package/dist/check/index.d.ts.map +1 -0
  57. package/dist/check/index.js +2 -0
  58. package/dist/check/index.js.map +1 -0
  59. package/dist/check/types.d.ts +149 -0
  60. package/dist/check/types.d.ts.map +1 -0
  61. package/dist/check/types.js +29 -0
  62. package/dist/check/types.js.map +1 -0
  63. package/dist/controls/controls.d.ts +198 -0
  64. package/dist/controls/controls.d.ts.map +1 -0
  65. package/dist/controls/controls.js +742 -0
  66. package/dist/controls/controls.js.map +1 -0
  67. package/dist/controls/dom.d.ts +108 -0
  68. package/dist/controls/dom.d.ts.map +1 -0
  69. package/dist/controls/dom.js +56 -0
  70. package/dist/controls/dom.js.map +1 -0
  71. package/dist/controls/index.d.ts +26 -0
  72. package/dist/controls/index.d.ts.map +1 -0
  73. package/dist/controls/index.js +23 -0
  74. package/dist/controls/index.js.map +1 -0
  75. package/dist/controls/rows.d.ts +66 -0
  76. package/dist/controls/rows.d.ts.map +1 -0
  77. package/dist/controls/rows.js +127 -0
  78. package/dist/controls/rows.js.map +1 -0
  79. package/dist/core/index.d.ts +3 -0
  80. package/dist/core/index.d.ts.map +1 -0
  81. package/dist/core/index.js +15 -0
  82. package/dist/core/index.js.map +1 -0
  83. package/dist/core/registry.d.ts +192 -0
  84. package/dist/core/registry.d.ts.map +1 -0
  85. package/dist/core/registry.js +2799 -0
  86. package/dist/core/registry.js.map +1 -0
  87. package/dist/core/types.d.ts +2468 -0
  88. package/dist/core/types.d.ts.map +1 -0
  89. package/dist/core/types.js +19 -0
  90. package/dist/core/types.js.map +1 -0
  91. package/dist/otel/index.d.ts +122 -0
  92. package/dist/otel/index.d.ts.map +1 -0
  93. package/dist/otel/index.js +157 -0
  94. package/dist/otel/index.js.map +1 -0
  95. package/dist/react/index.d.ts +191 -0
  96. package/dist/react/index.d.ts.map +1 -0
  97. package/dist/react/index.js +547 -0
  98. package/dist/react/index.js.map +1 -0
  99. package/dist/react/runtime.d.ts +95 -0
  100. package/dist/react/runtime.d.ts.map +1 -0
  101. package/dist/react/runtime.js +54 -0
  102. package/dist/react/runtime.js.map +1 -0
  103. package/dist/react/store.d.ts +333 -0
  104. package/dist/react/store.d.ts.map +1 -0
  105. package/dist/react/store.js +695 -0
  106. package/dist/react/store.js.map +1 -0
  107. package/dist/relay/auth.d.ts +244 -0
  108. package/dist/relay/auth.d.ts.map +1 -0
  109. package/dist/relay/auth.js +391 -0
  110. package/dist/relay/auth.js.map +1 -0
  111. package/dist/relay/frames.d.ts +141 -0
  112. package/dist/relay/frames.d.ts.map +1 -0
  113. package/dist/relay/frames.js +20 -0
  114. package/dist/relay/frames.js.map +1 -0
  115. package/dist/relay/index.d.ts +8 -0
  116. package/dist/relay/index.d.ts.map +1 -0
  117. package/dist/relay/index.js +4 -0
  118. package/dist/relay/index.js.map +1 -0
  119. package/dist/relay/relay.d.ts +277 -0
  120. package/dist/relay/relay.d.ts.map +1 -0
  121. package/dist/relay/relay.js +1026 -0
  122. package/dist/relay/relay.js.map +1 -0
  123. package/dist/relay/serve.d.ts +33 -0
  124. package/dist/relay/serve.d.ts.map +1 -0
  125. package/dist/relay/serve.js +86 -0
  126. package/dist/relay/serve.js.map +1 -0
  127. package/dist/widget/chat.d.ts +290 -0
  128. package/dist/widget/chat.d.ts.map +1 -0
  129. package/dist/widget/chat.js +486 -0
  130. package/dist/widget/chat.js.map +1 -0
  131. package/dist/widget/css.d.ts +49 -0
  132. package/dist/widget/css.d.ts.map +1 -0
  133. package/dist/widget/css.js +532 -0
  134. package/dist/widget/css.js.map +1 -0
  135. package/dist/widget/dom.d.ts +143 -0
  136. package/dist/widget/dom.d.ts.map +1 -0
  137. package/dist/widget/dom.js +27 -0
  138. package/dist/widget/dom.js.map +1 -0
  139. package/dist/widget/highlight.d.ts +64 -0
  140. package/dist/widget/highlight.d.ts.map +1 -0
  141. package/dist/widget/highlight.js +136 -0
  142. package/dist/widget/highlight.js.map +1 -0
  143. package/dist/widget/index.d.ts +9 -0
  144. package/dist/widget/index.d.ts.map +1 -0
  145. package/dist/widget/index.js +20 -0
  146. package/dist/widget/index.js.map +1 -0
  147. package/dist/widget/position.d.ts +166 -0
  148. package/dist/widget/position.d.ts.map +1 -0
  149. package/dist/widget/position.js +497 -0
  150. package/dist/widget/position.js.map +1 -0
  151. package/dist/widget/settings.d.ts +116 -0
  152. package/dist/widget/settings.d.ts.map +1 -0
  153. package/dist/widget/settings.js +117 -0
  154. package/dist/widget/settings.js.map +1 -0
  155. package/dist/widget/widget.d.ts +442 -0
  156. package/dist/widget/widget.d.ts.map +1 -0
  157. package/dist/widget/widget.js +2005 -0
  158. package/dist/widget/widget.js.map +1 -0
  159. package/package.json +153 -4
@@ -0,0 +1,166 @@
1
+ /**
2
+ * Where the panel sits, and how it gets out of the way.
3
+ *
4
+ * **This is not decoration.** The widget drives the app underneath itself. A
5
+ * person asks it to select rows and the panel is now sitting on top of the
6
+ * rows they asked about, so moving it is how they see the thing the agent just
7
+ * did. That is the whole design brief for this file: one gesture, one
8
+ * keystroke, and the panel is somewhere else — and it is still there tomorrow.
9
+ *
10
+ * ## The unit is a fraction of the free space, not pixels
11
+ *
12
+ * A position is two numbers in `[0, 1]`: `fx = 0` means flush with the left
13
+ * edge, `fx = 1` flush with the right, `0.5` centred. The pixel position is
14
+ * derived in CSS, not here:
15
+ *
16
+ * ```css
17
+ * left: calc(var(--dk-gap) + var(--dk-x) * (100vw - var(--dk-gap) * 2));
18
+ * translate: calc(var(--dk-x) * -100%) calc(var(--dk-y) * -100%);
19
+ * ```
20
+ *
21
+ * At `fx = 1` the element is pushed to `100vw - gap` and then pulled back by
22
+ * its own full width, so its right edge lands on the gap. Work the algebra
23
+ * through for any `fx` and the left edge is `gap + fx * (vw - 2*gap - w)`,
24
+ * which is inside the viewport for every `fx` in `[0, 1]` and every viewport
25
+ * at least as wide as the panel. **A stored fraction cannot describe an
26
+ * off-screen panel.** Pixels can, and a panel 1,100px from the left of a
27
+ * window that is now 600px wide is unreachable — no header to grab, no corner
28
+ * to snap from — and the only cure is clearing site data, which nobody knows
29
+ * to do. So pixels are accepted as input (`{ x, y }`, which is what a host
30
+ * app has on hand) and converted once, against the viewport they were measured
31
+ * in; after that there are only fractions.
32
+ *
33
+ * The one case the algebra does not cover is a viewport narrower than the
34
+ * panel. There `vw - 2*gap - w` is negative and `fx = 1` would drag the panel
35
+ * off the LEFT edge, so `clamp` collapses that axis to 0 and the panel hugs
36
+ * the near edge where the header is still grabbable. The person's chosen
37
+ * fraction is kept, not overwritten, so widening the window puts it back.
38
+ *
39
+ * ## Nothing is written unless it has to be
40
+ *
41
+ * The default position is a declaration in `css.ts` and nothing else, so a
42
+ * host app can place the panel from its own stylesheet with no JavaScript, the
43
+ * same way it can retheme it. Inline custom properties appear only once
44
+ * somebody has actually chosen a position — an explicit `position` option, a
45
+ * remembered one, a drag, a keypress — because at that point the person
46
+ * outranks the author's stylesheet.
47
+ */
48
+ import type { ElementLike, EventLike, StorageLike, WindowLike } from "./dom.ts";
49
+ export type Corner = "top-left" | "top-right" | "bottom-left" | "bottom-right";
50
+ /** A corner, or a pixel offset from the viewport's top-left corner. Pixels are
51
+ * converted to a fraction the moment they are applied; see the file header. */
52
+ export type Position = Corner | {
53
+ x: number;
54
+ y: number;
55
+ };
56
+ /** Fractions of the free space, both in `[0, 1]`. */
57
+ export interface Fraction {
58
+ fx: number;
59
+ fy: number;
60
+ }
61
+ export declare function isCorner(p: Position): p is Corner;
62
+ /**
63
+ * How far a release can be from a corner and still land in it, in pixels.
64
+ *
65
+ * Corners are where a panel belongs nearly all the time — out of the way of
66
+ * the middle of the screen, which is where the app's content is — so a magnet
67
+ * is cheaper than asking for precision. Far from a corner the drop is honoured
68
+ * exactly, because somebody who dragged the panel to the middle of the screen
69
+ * meant it.
70
+ */
71
+ export declare const SNAP = 72;
72
+ /**
73
+ * How far a pointer must travel before it is a drag and not a click, in
74
+ * pixels.
75
+ *
76
+ * Distance and not time, because time is wrong in both directions: a slow,
77
+ * deliberate click on the launcher is not a drag, and a fast flick of the
78
+ * panel across the screen is not a click. Fingers and pens also wobble by a
79
+ * pixel or two on a tap that was never meant to move anything, which is the
80
+ * other reason this is not zero.
81
+ */
82
+ export declare const SLOP = 4;
83
+ export interface PositionerOptions {
84
+ /** The element custom properties are written to — the widget's host element,
85
+ * which is what `:host` positions. */
86
+ host: ElementLike;
87
+ /** Namespaces the storage key, so two driverkit apps on one origin do not
88
+ * fight over one remembered position. */
89
+ app: string;
90
+ position?: Position;
91
+ window?: WindowLike;
92
+ storage?: StorageLike;
93
+ /** Default true. */
94
+ persist?: boolean;
95
+ /** Default true. False leaves the panel wherever the option and the
96
+ * stylesheet put it. */
97
+ draggable?: boolean;
98
+ /** Pixels. 0 switches snapping off. */
99
+ snapThreshold?: number;
100
+ /** Must match `--dk-gap`, which is what the arithmetic in `css.ts` insets
101
+ * by. Only worth passing if the host app overrode that property. */
102
+ gap?: number;
103
+ /** Spoken to a screen reader. A panel that moves silently has moved only
104
+ * for the people who can see it. */
105
+ announce?: (text: string) => void;
106
+ }
107
+ export interface Positioner {
108
+ /**
109
+ * Make `el` draggable, and return the predicate its own click handler has to
110
+ * consult first: true means the click that just arrived is the tail of a
111
+ * drag of THIS element and belongs to the drag.
112
+ *
113
+ * Per element rather than one flag for the whole widget, because a drag of
114
+ * the header produces a click on the header — which nothing listens for —
115
+ * and a single shared flag would then still be set when the person's next
116
+ * real click landed on the launcher.
117
+ *
118
+ * `controls: "ignore"` refuses to start a drag from a button or input
119
+ * inside `el`, which is what keeps the header's Close button clickable.
120
+ */
121
+ handle: (el: ElementLike, controls?: "ignore" | "allow") => () => boolean;
122
+ /** Arrow keys. `plain` allows bare arrows, which only the move button does;
123
+ * everywhere else it takes Alt. Returns true if the key was used. */
124
+ key: (event: EventLike, plain?: boolean) => boolean;
125
+ moveTo: (position: Position) => void;
126
+ /** The next corner round, or the nearest one if the panel is between them. */
127
+ cycle: () => void;
128
+ /** The chosen position, or null while the stylesheet is still in charge. */
129
+ chosen: () => Fraction | null;
130
+ dispose: () => void;
131
+ }
132
+ export declare function storageKey(app: string): string;
133
+ /** The real `localStorage`, if reaching for it does not throw — which it does
134
+ * when site data is blocked, before any read has even been attempted. */
135
+ export declare function defaultStorage(): StorageLike | undefined;
136
+ /**
137
+ * Read one JSON value out of storage, or null for every way that can fail: no
138
+ * storage, a throwing read, an empty slot, malformed JSON, or a shape `parse`
139
+ * does not recognise.
140
+ *
141
+ * All of them mean the same thing to the caller — use the default — so none is
142
+ * worth distinguishing, and none is worth breaking a chat panel over. A
143
+ * private window and a person who blocked site data both throw on the way IN,
144
+ * before any value has been looked at, which is why the `getItem` call and the
145
+ * parsing are inside one try and not two.
146
+ *
147
+ * This is the only place in the package that reads persisted state. The
148
+ * settings view keeps its own key and its own shape and goes through here, so
149
+ * there is one mechanism to get the try/catch right in rather than two.
150
+ */
151
+ export declare function loadJson<T>(storage: StorageLike | undefined, key: string, parse: (value: unknown) => T | null): T | null;
152
+ /** Write one JSON value, and swallow every reason it can fail. */
153
+ export declare function saveJson(storage: StorageLike | undefined, key: string, value: unknown): void;
154
+ /** A remembered position, or null if there is not one to be had. */
155
+ export declare function read(storage: StorageLike | undefined, app: string): Fraction | null;
156
+ export declare function write(storage: StorageLike | undefined, app: string, at: Fraction): void;
157
+ /** The corner nearest `at`, and how far away it is in pixels. */
158
+ export declare function nearestCorner(at: Fraction, freeW: number, freeH: number): {
159
+ corner: Corner;
160
+ away: number;
161
+ };
162
+ /** What a screen reader is told. Corners get their name; anywhere else gets
163
+ * percentages, because "0.37" is not a place. */
164
+ export declare function describe(at: Fraction): string;
165
+ export declare function positioner(options: PositionerOptions): Positioner;
166
+ //# sourceMappingURL=position.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"position.d.ts","sourceRoot":"","sources":["../../src/widget/position.ts"],"names":[],"mappings":"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"}
@@ -0,0 +1,497 @@
1
+ /**
2
+ * Where the panel sits, and how it gets out of the way.
3
+ *
4
+ * **This is not decoration.** The widget drives the app underneath itself. A
5
+ * person asks it to select rows and the panel is now sitting on top of the
6
+ * rows they asked about, so moving it is how they see the thing the agent just
7
+ * did. That is the whole design brief for this file: one gesture, one
8
+ * keystroke, and the panel is somewhere else — and it is still there tomorrow.
9
+ *
10
+ * ## The unit is a fraction of the free space, not pixels
11
+ *
12
+ * A position is two numbers in `[0, 1]`: `fx = 0` means flush with the left
13
+ * edge, `fx = 1` flush with the right, `0.5` centred. The pixel position is
14
+ * derived in CSS, not here:
15
+ *
16
+ * ```css
17
+ * left: calc(var(--dk-gap) + var(--dk-x) * (100vw - var(--dk-gap) * 2));
18
+ * translate: calc(var(--dk-x) * -100%) calc(var(--dk-y) * -100%);
19
+ * ```
20
+ *
21
+ * At `fx = 1` the element is pushed to `100vw - gap` and then pulled back by
22
+ * its own full width, so its right edge lands on the gap. Work the algebra
23
+ * through for any `fx` and the left edge is `gap + fx * (vw - 2*gap - w)`,
24
+ * which is inside the viewport for every `fx` in `[0, 1]` and every viewport
25
+ * at least as wide as the panel. **A stored fraction cannot describe an
26
+ * off-screen panel.** Pixels can, and a panel 1,100px from the left of a
27
+ * window that is now 600px wide is unreachable — no header to grab, no corner
28
+ * to snap from — and the only cure is clearing site data, which nobody knows
29
+ * to do. So pixels are accepted as input (`{ x, y }`, which is what a host
30
+ * app has on hand) and converted once, against the viewport they were measured
31
+ * in; after that there are only fractions.
32
+ *
33
+ * The one case the algebra does not cover is a viewport narrower than the
34
+ * panel. There `vw - 2*gap - w` is negative and `fx = 1` would drag the panel
35
+ * off the LEFT edge, so `clamp` collapses that axis to 0 and the panel hugs
36
+ * the near edge where the header is still grabbable. The person's chosen
37
+ * fraction is kept, not overwritten, so widening the window puts it back.
38
+ *
39
+ * ## Nothing is written unless it has to be
40
+ *
41
+ * The default position is a declaration in `css.ts` and nothing else, so a
42
+ * host app can place the panel from its own stylesheet with no JavaScript, the
43
+ * same way it can retheme it. Inline custom properties appear only once
44
+ * somebody has actually chosen a position — an explicit `position` option, a
45
+ * remembered one, a drag, a keypress — because at that point the person
46
+ * outranks the author's stylesheet.
47
+ */
48
+ const CORNERS = {
49
+ "top-left": { fx: 0, fy: 0 },
50
+ "top-right": { fx: 1, fy: 0 },
51
+ "bottom-left": { fx: 0, fy: 1 },
52
+ "bottom-right": { fx: 1, fy: 1 },
53
+ };
54
+ /** The order the keyboard cycles, starting where the panel starts. */
55
+ const CYCLE = ["bottom-right", "bottom-left", "top-left", "top-right"];
56
+ export function isCorner(p) {
57
+ return typeof p === "string" && p in CORNERS;
58
+ }
59
+ /**
60
+ * How far a release can be from a corner and still land in it, in pixels.
61
+ *
62
+ * Corners are where a panel belongs nearly all the time — out of the way of
63
+ * the middle of the screen, which is where the app's content is — so a magnet
64
+ * is cheaper than asking for precision. Far from a corner the drop is honoured
65
+ * exactly, because somebody who dragged the panel to the middle of the screen
66
+ * meant it.
67
+ */
68
+ export const SNAP = 72;
69
+ /**
70
+ * How far a pointer must travel before it is a drag and not a click, in
71
+ * pixels.
72
+ *
73
+ * Distance and not time, because time is wrong in both directions: a slow,
74
+ * deliberate click on the launcher is not a drag, and a fast flick of the
75
+ * panel across the screen is not a click. Fingers and pens also wobble by a
76
+ * pixel or two on a tap that was never meant to move anything, which is the
77
+ * other reason this is not zero.
78
+ */
79
+ export const SLOP = 4;
80
+ /** The keyboard nudge, as a fraction of the free space. Deliberately coarse:
81
+ * the goal is "off the thing I am reading", and a 1px-at-a-time nudge would
82
+ * take sixty keystrokes to achieve it. */
83
+ const STEP = 0.1;
84
+ export function storageKey(app) {
85
+ return `driverkit:widget:position:${app}`;
86
+ }
87
+ /** The real `localStorage`, if reaching for it does not throw — which it does
88
+ * when site data is blocked, before any read has even been attempted. */
89
+ export function defaultStorage() {
90
+ try {
91
+ return globalThis.localStorage;
92
+ }
93
+ catch {
94
+ return undefined;
95
+ }
96
+ }
97
+ /**
98
+ * Read one JSON value out of storage, or null for every way that can fail: no
99
+ * storage, a throwing read, an empty slot, malformed JSON, or a shape `parse`
100
+ * does not recognise.
101
+ *
102
+ * All of them mean the same thing to the caller — use the default — so none is
103
+ * worth distinguishing, and none is worth breaking a chat panel over. A
104
+ * private window and a person who blocked site data both throw on the way IN,
105
+ * before any value has been looked at, which is why the `getItem` call and the
106
+ * parsing are inside one try and not two.
107
+ *
108
+ * This is the only place in the package that reads persisted state. The
109
+ * settings view keeps its own key and its own shape and goes through here, so
110
+ * there is one mechanism to get the try/catch right in rather than two.
111
+ */
112
+ export function loadJson(storage, key, parse) {
113
+ try {
114
+ const raw = storage?.getItem(key);
115
+ if (!raw)
116
+ return null;
117
+ return parse(JSON.parse(raw));
118
+ }
119
+ catch {
120
+ return null;
121
+ }
122
+ }
123
+ /** Write one JSON value, and swallow every reason it can fail. */
124
+ export function saveJson(storage, key, value) {
125
+ try {
126
+ storage?.setItem(key, JSON.stringify(value));
127
+ }
128
+ catch {
129
+ // A full quota, a private window, a user who blocked site data. The
130
+ // position is still correct for this session; it just will not be
131
+ // remembered, and that is not worth a thrown error inside a pointerup
132
+ // handler that would leave the drag state wedged.
133
+ }
134
+ }
135
+ /** A remembered position, or null if there is not one to be had. */
136
+ export function read(storage, app) {
137
+ return loadJson(storage, storageKey(app), (value) => {
138
+ const at = value;
139
+ const fx = finite(at?.fx);
140
+ const fy = finite(at?.fy);
141
+ if (fx === null || fy === null)
142
+ return null;
143
+ return { fx: unit(fx), fy: unit(fy) };
144
+ });
145
+ }
146
+ export function write(storage, app, at) {
147
+ saveJson(storage, storageKey(app), at);
148
+ }
149
+ /** The corner nearest `at`, and how far away it is in pixels. */
150
+ export function nearestCorner(at, freeW, freeH) {
151
+ let best = null;
152
+ for (const corner of CYCLE) {
153
+ const c = CORNERS[corner];
154
+ const away = Math.hypot((at.fx - c.fx) * Math.max(freeW, 0), (at.fy - c.fy) * Math.max(freeH, 0));
155
+ if (!best || away < best.away)
156
+ best = { corner, away };
157
+ }
158
+ return best;
159
+ }
160
+ /** What a screen reader is told. Corners get their name; anywhere else gets
161
+ * percentages, because "0.37" is not a place. */
162
+ export function describe(at) {
163
+ for (const corner of CYCLE) {
164
+ const c = CORNERS[corner];
165
+ if (c.fx === at.fx && c.fy === at.fy)
166
+ return corner.replace("-", " ");
167
+ }
168
+ return `${Math.round(at.fx * 100)}% across, ${Math.round(at.fy * 100)}% down`;
169
+ }
170
+ export function positioner(options) {
171
+ const host = options.host;
172
+ const win = options.window ?? globalThis;
173
+ const gap = options.gap ?? 16;
174
+ const threshold = options.snapThreshold ?? SNAP;
175
+ const persist = options.persist !== false;
176
+ const storage = persist ? (options.storage ?? defaultStorage()) : undefined;
177
+ /** The position somebody chose, unclamped, or null while nobody has — in
178
+ * which case `css.ts` is still in charge. */
179
+ let chosen = null;
180
+ /** Where the stylesheet put the panel, read back off the layout once at
181
+ * mount. Needed because the clamp has to be able to rescue the DEFAULT
182
+ * position too: `:host` says "flush right", a window narrower than the
183
+ * panel makes that mean "off the left edge", and no amount of CSS can
184
+ * notice. */
185
+ let fromCss = null;
186
+ /** Sticky. Once a custom property has been written inline there is no way to
187
+ * un-write it with `StyleLike`, so after the first write this keeps writing
188
+ * — otherwise a clamp applied in a narrow window would survive the window
189
+ * being widened again, which is the bug the clamp exists to prevent. */
190
+ let wrote = false;
191
+ let dragging = false;
192
+ let snapped = false;
193
+ let disposed = false;
194
+ {
195
+ const m = measure();
196
+ if (m)
197
+ fromCss = fromLayout(m);
198
+ }
199
+ // Remembered beats the option: the option is the author's idea of where the
200
+ // panel goes and the remembered one is where this particular person put it,
201
+ // and the person was looking at their own screen when they decided.
202
+ const remembered = read(storage, options.app);
203
+ if (remembered)
204
+ set(remembered, { save: false });
205
+ // Not saved: the option is the author's default and saving it would turn
206
+ // this person's storage into a copy of it, so the day the author changed the
207
+ // option nobody would ever see the new one.
208
+ else if (options.position !== undefined)
209
+ applyPosition(options.position, false);
210
+ // Mounting into a window that is already too narrow for the panel is the
211
+ // same problem as resizing into one.
212
+ else
213
+ paint();
214
+ const onResize = () => {
215
+ // Re-applied rather than recomputed: the chosen position is unchanged, but
216
+ // the clamp it goes through is not, so a panel that was flush right in a
217
+ // wide window and does not fit in a narrow one comes back to the near edge
218
+ // — and returns to the right edge when the window is widened again.
219
+ paint();
220
+ };
221
+ win.addEventListener?.("resize", onResize);
222
+ return {
223
+ handle,
224
+ key,
225
+ moveTo,
226
+ cycle,
227
+ chosen: () => (chosen ? { ...chosen } : null),
228
+ dispose: () => {
229
+ disposed = true;
230
+ win.removeEventListener?.("resize", onResize);
231
+ },
232
+ };
233
+ // -- applying ------------------------------------------------------------
234
+ function set(at, opts) {
235
+ chosen = { fx: unit(at.fx), fy: unit(at.fy) };
236
+ snapped = opts.animate === true && !reducedMotion();
237
+ paint();
238
+ if (opts.save)
239
+ write(storage, options.app, chosen);
240
+ }
241
+ function paint() {
242
+ host.className = `dk-host${dragging ? " dk-dragging" : ""}${snapped ? " dk-snap" : ""}`;
243
+ const at = chosen ?? fromCss;
244
+ if (!at || !host.style)
245
+ return;
246
+ const m = measure();
247
+ // The clamp, and the only place the viewport is allowed to override the
248
+ // person: a panel wider than the window has to give up its fraction on
249
+ // that axis or it ends up off the OPPOSITE edge, header first, with
250
+ // nothing left to grab. The chosen value is not overwritten, so widening
251
+ // the window puts the panel back where the person left it.
252
+ const fx = m && m.freeW <= 0 ? 0 : at.fx;
253
+ const fy = m && m.freeH <= 0 ? 0 : at.fy;
254
+ // Nothing is written while the stylesheet is still in charge and the clamp
255
+ // has nothing to say — that silence is what lets a host app position the
256
+ // panel from its own CSS.
257
+ if (chosen === null && !wrote && fx === at.fx && fy === at.fy)
258
+ return;
259
+ wrote = true;
260
+ host.style.setProperty("--dk-x", String(round(fx)));
261
+ host.style.setProperty("--dk-y", String(round(fy)));
262
+ }
263
+ function moveTo(position) {
264
+ applyPosition(position, true);
265
+ }
266
+ function applyPosition(position, save) {
267
+ if (isCorner(position)) {
268
+ set(CORNERS[position], { save, animate: true });
269
+ if (save)
270
+ options.announce?.(`Panel moved to ${describe(CORNERS[position])}`);
271
+ return;
272
+ }
273
+ const m = measure();
274
+ // Without a measurable box there is no way to turn a pixel offset into a
275
+ // fraction, and guessing would put the panel somewhere nobody asked for.
276
+ // Leaving the stylesheet in charge is the honest failure.
277
+ if (!m)
278
+ return;
279
+ set({
280
+ fx: m.freeW > 0 ? (position.x - gap) / m.freeW : 0,
281
+ fy: m.freeH > 0 ? (position.y - gap) / m.freeH : 0,
282
+ }, { save, animate: true });
283
+ if (save)
284
+ options.announce?.(`Panel moved to ${describe(chosen)}`);
285
+ }
286
+ function cycle() {
287
+ const m = measure();
288
+ const at = current(m);
289
+ if (!at || !m)
290
+ return;
291
+ const near = nearestCorner(at, m.freeW, m.freeH);
292
+ // From between corners, the first press lands on the nearest one rather
293
+ // than jumping across the screen; from a corner it advances.
294
+ const next = near.away === 0 ? CYCLE[(CYCLE.indexOf(near.corner) + 1) % CYCLE.length] : near.corner;
295
+ set(CORNERS[next], { save: true, animate: true });
296
+ options.announce?.(`Panel moved to ${describe(CORNERS[next])}`);
297
+ }
298
+ function measure() {
299
+ const rect = host.getBoundingClientRect?.();
300
+ const vw = win.innerWidth;
301
+ const vh = win.innerHeight;
302
+ // Null, rather than a zero-sized viewport, when the window cannot say how
303
+ // big it is — which is what every non-browser host looks like. A clamp
304
+ // against a viewport of 0x0 concludes that nothing fits and drags the
305
+ // panel to the corner, which would be a confident answer derived from no
306
+ // information at all.
307
+ if (!rect || vw === undefined || vh === undefined || vw <= 0 || vh <= 0)
308
+ return null;
309
+ return { rect, freeW: vw - gap * 2 - rect.width, freeH: vh - gap * 2 - rect.height };
310
+ }
311
+ /** Where the panel is now, as a fraction. Derived from the measured box when
312
+ * nobody has chosen a position, because in that case the answer lives in
313
+ * the stylesheet and the layout is the only thing that knows it. */
314
+ function current(m) {
315
+ if (chosen)
316
+ return chosen;
317
+ if (!m)
318
+ return null;
319
+ return fromLayout(m);
320
+ }
321
+ /** The fraction that produced this layout — the inverse of the arithmetic in
322
+ * `css.ts`. On an axis with no free space the inverse does not exist, so it
323
+ * falls back to the stylesheet's own default of 1 rather than inventing a
324
+ * position; that is the case where the clamp is about to take over anyway. */
325
+ function fromLayout(m) {
326
+ return {
327
+ fx: m.freeW > 0 ? unit((m.rect.left - gap) / m.freeW) : 1,
328
+ fy: m.freeH > 0 ? unit((m.rect.top - gap) / m.freeH) : 1,
329
+ };
330
+ }
331
+ function reducedMotion() {
332
+ try {
333
+ return win.matchMedia?.("(prefers-reduced-motion: reduce)").matches === true;
334
+ }
335
+ catch {
336
+ return false;
337
+ }
338
+ }
339
+ // -- dragging ------------------------------------------------------------
340
+ function handle(el, controls = "allow") {
341
+ let swallow = false;
342
+ const swallowsClick = () => {
343
+ const was = swallow;
344
+ swallow = false;
345
+ return was;
346
+ };
347
+ if (options.draggable === false)
348
+ return swallowsClick;
349
+ let drag = null;
350
+ el.addEventListener("pointerdown", (e) => {
351
+ // Cleared on every press, not only on the presses that start a drag, so
352
+ // a drag that ended without a click — released outside the handle, which
353
+ // pointer capture makes normal — cannot leave a flag that swallows the
354
+ // person's next real press of this same button.
355
+ swallow = false;
356
+ if (disposed)
357
+ return;
358
+ if (e.button !== undefined && e.button !== 0)
359
+ return;
360
+ if (controls === "ignore" && isControl(e.target))
361
+ return;
362
+ const m = measure();
363
+ if (!m)
364
+ return;
365
+ drag = {
366
+ ...(e.pointerId === undefined ? {} : { id: e.pointerId }),
367
+ x: e.clientX ?? 0,
368
+ y: e.clientY ?? 0,
369
+ left: m.rect.left,
370
+ top: m.rect.top,
371
+ moved: false,
372
+ };
373
+ // Stops the text selection and the native image-drag that otherwise
374
+ // happen while a person is dragging a panel by its title.
375
+ e.preventDefault?.();
376
+ if (e.pointerId !== undefined)
377
+ el.setPointerCapture?.(e.pointerId);
378
+ });
379
+ el.addEventListener("pointermove", (e) => {
380
+ if (!drag || !mine(e, drag.id))
381
+ return;
382
+ const dx = (e.clientX ?? 0) - drag.x;
383
+ const dy = (e.clientY ?? 0) - drag.y;
384
+ // Below the slop NOTHING moves — not by a pixel. A launcher that shifts
385
+ // under a click that was meant to open it reads as a bug.
386
+ if (!drag.moved && Math.hypot(dx, dy) < SLOP)
387
+ return;
388
+ drag.moved = true;
389
+ dragging = true;
390
+ const m = measure();
391
+ if (!m)
392
+ return;
393
+ // Clamped here and not only on release, so the panel never spends even
394
+ // one frame with its header under the edge of the window.
395
+ set({
396
+ fx: m.freeW > 0 ? (drag.left + dx - gap) / m.freeW : 0,
397
+ fy: m.freeH > 0 ? (drag.top + dy - gap) / m.freeH : 0,
398
+ }, { save: false });
399
+ });
400
+ const end = (e) => {
401
+ if (!drag || !mine(e, drag.id))
402
+ return;
403
+ const moved = drag.moved;
404
+ const id = drag.id;
405
+ drag = null;
406
+ dragging = false;
407
+ if (id !== undefined)
408
+ el.releasePointerCapture?.(id);
409
+ if (!moved) {
410
+ paint();
411
+ return;
412
+ }
413
+ // The click that follows a drag belongs to the drag. Without this,
414
+ // nudging the collapsed launcher aside opens the panel on top of the
415
+ // thing the person was trying to uncover.
416
+ swallow = true;
417
+ const m = measure();
418
+ const at = current(m);
419
+ if (!at || !m)
420
+ return;
421
+ const near = nearestCorner(at, m.freeW, m.freeH);
422
+ const snapping = threshold > 0 && near.away <= threshold;
423
+ set(snapping ? CORNERS[near.corner] : at, { save: true, animate: snapping });
424
+ options.announce?.(`Panel moved to ${describe(chosen)}`);
425
+ };
426
+ el.addEventListener("pointerup", end);
427
+ // A cancelled pointer is a pointer that is not coming back — the browser
428
+ // took it for a scroll, the pen left the tablet. Treating it as a release
429
+ // is what keeps the panel from being stuck to a pointer that no longer
430
+ // exists.
431
+ el.addEventListener("pointercancel", end);
432
+ el.addEventListener("lostpointercapture", end);
433
+ return swallowsClick;
434
+ }
435
+ function mine(e, id) {
436
+ return id === undefined || e.pointerId === undefined || e.pointerId === id;
437
+ }
438
+ // -- keyboard ------------------------------------------------------------
439
+ /**
440
+ * Alt plus an arrow throws the panel to that edge; a bare arrow, which only
441
+ * the move button sees, nudges it.
442
+ *
443
+ * Alt is the modifier because the bare arrows inside this panel already
444
+ * belong to the text input and the transcript, and a positioning feature
445
+ * that eats the Left arrow in a text field has broken typing to gain a
446
+ * convenience. Alt+Arrow also works from the collapsed launcher, which is
447
+ * the case that matters: the quickest way to see what the agent just did is
448
+ * one keystroke that does not require finding anything first.
449
+ *
450
+ * The edge rather than a nudge, for the modified form, for the same reason
451
+ * the snap exists — "get out of my way" is a corner, not an offset.
452
+ */
453
+ function key(event, plain = false) {
454
+ const dir = ARROWS[event.key ?? ""];
455
+ if (!dir)
456
+ return false;
457
+ const alt = event.altKey === true;
458
+ if (!alt && !plain)
459
+ return false;
460
+ const m = measure();
461
+ const at = current(m);
462
+ if (!at)
463
+ return false;
464
+ const next = alt
465
+ ? { fx: dir.x === 0 ? at.fx : dir.x < 0 ? 0 : 1, fy: dir.y === 0 ? at.fy : dir.y < 0 ? 0 : 1 }
466
+ : { fx: at.fx + dir.x * STEP, fy: at.fy + dir.y * STEP };
467
+ event.preventDefault?.();
468
+ set(next, { save: true, animate: alt });
469
+ options.announce?.(`Panel moved to ${describe(chosen)}`);
470
+ return true;
471
+ }
472
+ }
473
+ const ARROWS = {
474
+ ArrowLeft: { x: -1, y: 0 },
475
+ ArrowRight: { x: 1, y: 0 },
476
+ ArrowUp: { x: 0, y: -1 },
477
+ ArrowDown: { x: 0, y: 1 },
478
+ };
479
+ /** Something a click belongs to. A header is a drag handle and also the place
480
+ * the Close button lives, and a person who presses Close and twitches two
481
+ * pixels still meant Close. */
482
+ function isControl(target) {
483
+ const tag = target?.tagName;
484
+ return tag === "BUTTON" || tag === "INPUT" || tag === "TEXTAREA" || tag === "SELECT" || tag === "A";
485
+ }
486
+ function unit(n) {
487
+ return n < 0 ? 0 : n > 1 ? 1 : n;
488
+ }
489
+ function finite(n) {
490
+ return typeof n === "number" && Number.isFinite(n) ? n : null;
491
+ }
492
+ /** Four places is a tenth of a pixel on a 4K screen, and it keeps the value
493
+ * written into CSS and into storage short enough to read in devtools. */
494
+ function round(n) {
495
+ return Math.round(n * 1e4) / 1e4;
496
+ }
497
+ //# sourceMappingURL=position.js.map