-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
294 lines (260 loc) · 10.4 KB
/
index.ts
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
import type { ServerLuaFunction } from 'yues-client';
import type { ClipboardData, DragOptions, RectF, Vector2dF, Color, SizeF } from '@/@types';
import type { Font } from '@/components/font';
import type { Window } from '@/components/window';
import { serverPropName, idPropName, luaVarRef, eventListPropName, initMethodName } from '@/components/@symbols';
import { Responder } from '@/components/responder';
import { tuple } from '@/utils';
import { doDrag, objectGetterOnRef, refGetterOnRef, refSetterOnRef, setColor } from '@/lua-functions';
const proxiedMethods = tuple(
'offsetfromview',
'offsetfromwindow',
'getbounds',
'layout',
'schedulepaint',
'schedulepaintrect',
'setvisible',
'isvisible',
'setenabled',
'isenabled',
'focus',
'hasfocus',
'setfocusable',
'isfocusable',
'setmousedowncanmovewindow',
'ismousedowncanmovewindow',
'dodrag',
'dodragwithoptions',
'canceldrag',
'isdragging',
'registerdraggedtypes',
'setcursor',
'setcolor',
'setbackgroundcolor',
'setstyle',
'getcomputedlayout',
'getminimumsize',
'getparent',
'getwindow',
);
const events = tuple(
'dragleave',
'sizechanged',
);
export class View<
Events extends string = never
> extends Responder<
Events | typeof events[number]
> {
protected get [eventListPropName](): string[] { return (events as string[]).concat(super[eventListPropName]) };
protected async [initMethodName]() {
await super[initMethodName]();
const server = this[serverPropName];
const luavar = this[luaVarRef];
const proxied = await Promise.all([
server.createFunction(
...objectGetterOnRef(luavar, 'offsetfromview'),
async (func, view: View) => {
if (!await view.initialized) throw new Error('offsetfromview: view is not initialized');
return func(view[idPropName]);
},
),
server.createFunction(`return ${luavar}:offsetfromwindow()`, []),
server.createFunction(`return ${luavar}:getbounds()`, []),
server.createFunction(`${luavar}:layout()`, []),
server.createFunction(`${luavar}:schedulepaint()`, []),
server.createFunction(`${luavar}:schedulepaintrect(rect)`, ['rect']),
server.createFunction(`${luavar}:setvisible(visible)`, ['visible']),
server.createFunction(`return ${luavar}:isvisible()`, []),
server.createFunction(`${luavar}:setenabled(enable)`, ['enable']),
server.createFunction(`return ${luavar}:isenabled()`, []),
server.createFunction(`${luavar}:focus()`, []),
server.createFunction(`return ${luavar}:hasfocus()`, []),
server.createFunction(`${luavar}:setfocusable(focusable)`, ['focusable']),
server.createFunction(`return ${luavar}:isfocusable()`, []),
server.createFunction(`${luavar}:setmousedowncanmovewindow(can)`, ['can']),
server.createFunction(`return ${luavar}:ismousedowncanmovewindow()`, []),
server.createFunction(
...doDrag(luavar, false),
async (func, data: ClipboardData, operations: number) => {
const fixedData = {
type: data.type,
value: data.type === 'image' ? data.value[idPropName] : data.value,
};
return func(fixedData, operations);
},
),
server.createFunction(
...doDrag(luavar, true),
async (func, data: ClipboardData, operations: number, options: DragOptions) => {
const fixedData = {
type: data.type,
value: data.type === 'image' ? data.value[idPropName] : data.value,
};
return func(fixedData, operations, options);
},
),
server.createFunction(`${luavar}:canceldrag()`, []),
server.createFunction(`return ${luavar}:isdragging()`, []),
server.createFunction(`${luavar}:registerdraggedtypes(types)`, ['types']),
server.createFunction(...refSetterOnRef(luavar, 'setcursor')),
server.createFunction(...setColor(luavar, 'setcolor')),
server.createFunction(...setColor(luavar, 'setbackgroundcolor')),
server.createFunction(`${luavar}:setstyle(style)`, ['style']),
server.createFunction(`return ${luavar}:getcomputedlayout()`, []),
server.createFunction(`return ${luavar}:getminimumsize()`, []),
server.createFunction(...refGetterOnRef(luavar, 'getparent')),
server.createFunction(...refGetterOnRef(luavar, 'getwindow')),
]);
proxiedMethods.forEach((name, i) => this[name] = proxied[i]);
}
/**
* Return offset from `view`
*/
offsetfromview: ServerLuaFunction<[view: View<any>], Vector2dF>;
/**
* Return offset from the window that owns the view
*/
offsetfromwindow: ServerLuaFunction<[], Vector2dF>;
/**
* Return the position and size of the view, relative to its parent
*/
getbounds: ServerLuaFunction<[], RectF>;
/**
* Make the view re-recalculate its layout
*/
layout: ServerLuaFunction<[], void>;
/**
* Schedule to repaint the whole view
*/
schedulepaint: ServerLuaFunction<[], void>;
/**
* Schedule to repaint the `rect` area in view
*/
schedulepaintrect: ServerLuaFunction<[rect: RectF], void>;
/**
* Show/Hide the view
*/
setvisible: ServerLuaFunction<[visible: boolean], void>;
/**
* Return whether the view is visible
*/
isvisible: ServerLuaFunction<[], boolean>;
/**
* Set whether the view is enabled.
*
* The enabled state of each view is not affected by its parent, disabling a container-like view does not have any effect
*/
setenabled: ServerLuaFunction<[enable: boolean], void>;
/**
* Return whether the view is enabled
*/
isenabled: ServerLuaFunction<[], boolean>;
/**
* Move the keyboard focus to the view
*/
focus: ServerLuaFunction<[], void>;
/**
* Return whether the view has keyboard focus
*/
hasfocus: ServerLuaFunction<[], boolean>;
/**
* Set whether the view can be focused on
*/
setfocusable: ServerLuaFunction<[focusable: boolean], void>;
/**
* Return whether the view can be focused on
*/
isfocusable: ServerLuaFunction<[], boolean>;
/**
* Set whether dragging mouse would move the window.
*
* For most platforms this method only works for frameless windows, having this feature may also prevent mouse events to happen.
*
* On macOS the `Container` view has this feature turned on by default. To turn this feature on for the view, the view's parent view must also has this feature turned on.
*
* On Windows the view with this feature will be treated as titlebar, e.g. double-clicking would maximize the window, right-clicking may show the system menu
*/
setmousedowncanmovewindow: ServerLuaFunction<[can: boolean], void>;
/**
* Return whether dragging the view would move the window
*/
ismousedowncanmovewindow: ServerLuaFunction<[], boolean>;
/**
* Start a drag session but do not set drag image.
*
* The return value is an integer indicating the result of dragging.
*
* This method should only be called in the `mousedown` event, when user starts to drag the cursor.
*
* Note that on macOS certain views may have `ismousedowncanmovewindow` defaulting to `true`, which will prevent drag session to start.
* Make sure to call `setmousedowncanmovewindow(false)` for drag sources
*/
dodrag: ServerLuaFunction<[data: ClipboardData, operations: number], number>;
/**
* Start a drag session.
*
* The return value is an integer indicating the result of dragging.
*
* This method should only be called in the `mousedown` event, when user starts to drag the cursor.
*
* Note that on macOS certain views may have `ismousedowncanmovewindow` defaulting to `true`, which will prevent drag session to start.
* Make sure to call `setmousedowncanmovewindow(false)` for drag sources
*/
dodragwithoptions: ServerLuaFunction<[data: ClipboardData, operations: number, options: DragOptions], number>;
/**
* Cancel current drag session if the view is being used as drag source
*/
canceldrag: ServerLuaFunction<[], void>;
/**
* Return whether the view is being used as drag source
*/
isdragging: ServerLuaFunction<[], boolean>;
/**
* Make the view a drag destination that accepts `types`
*/
registerdraggedtypes: ServerLuaFunction<[types: ClipboardData['type'][]], void>;
/**
* Set the cursor to show when hovering the view.
*
* On Linux, setting cursor would force the view to own its own GDK window. For certain views like `Label`, this may have remove the view's background color
*/
setcursor: ServerLuaFunction<[cursor: Font], void>;
/**
* Change the color used for drawing text in the view.
*
* This methods only works for `View`s that display text, like `Label` or `Entry`
*/
setcolor: ServerLuaFunction<[color: Color], void>;
/**
* Change the background color of the view
*/
setbackgroundcolor: ServerLuaFunction<[color: Color], void>;
/**
* Change the styles of the view.
*
* Available style properties can be found at [Layout System](https://libyue.com/docs/v0.11.0/lua/guides/layout_system.html)
*/
setstyle: ServerLuaFunction<[styles: Record<string, number | string>], void>;
/**
* Return string representation of the view's layout
*/
getcomputedlayout: ServerLuaFunction<[], string>;
/**
* Return the minimum size needed to show the view
*/
getminimumsize: ServerLuaFunction<[], SizeF>;
/**
* Return parent view
*/
getparent: ServerLuaFunction<[], View>;
/**
* Return the window that the view belongs to
*/
getwindow: ServerLuaFunction<[], Window>;
async destroy() {
if (!await this.initialized) return;
await Promise.all(proxiedMethods.map((name) => this[name].destroy()));
await super.destroy();
}
}