-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathwindow.ts
More file actions
1055 lines (888 loc) · 28 KB
/
Copy pathwindow.ts
File metadata and controls
1055 lines (888 loc) · 28 KB
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { emptyFunction, keyNames, codeNames, extraCodes } from './constants.ts';
import EventEmitter from 'node:events';
import { glfw } from './core.ts';
import type {
TCbField,
TEvent,
TEventCb,
TImageData,
TKeyEvent,
TMonitor,
TMonitorMode,
TMouseButtonEvent,
TMouseScrollEvent,
TPos,
TRect,
TSize,
TNativeEmitter,
TWindowHandle,
TWindowMode,
} from './types.ts';
// oxlint-disable max-lines
const DEFAULT_WIDTH = 1280;
const DEFAULT_HEIGHT = 720;
export type TWindowOpts = Readonly<
Partial<{
/** Major OpenGL version to be used. Default is 2. */
major: number;
/** Minor OpenGL version to be used. Default is 1. */
minor: number;
/** Initial logical window width. Default is 1280. */
width: number;
/** Initial logical window height. Default is 720. */
height: number;
/** Display id to open the window on. Default is 0. */
display: number;
/** Whether vsync should be used. Default is false. */
vsync: boolean;
/** Whether fullscreen windows should iconify automatically on focus loss. Default is true. */
autoIconify: boolean;
/** Window display mode. Default is 'windowed'. */
mode: TWindowMode;
/** Whether the window has borders. Default is true. */
decorated: boolean;
/** Multisample antialiasing level. Default is 0. */
msaa: number;
/** Window icon. Default is null. */
icon: TImageData;
/** Window title. Default is current working directory. */
title: string;
/** Whether the window is resizable. Default is true. */
resizable: boolean;
/**
* Called right before window creation.
*
* `window` is the current Window object. `glfw` is the raw GLFW binding.
*/
onBeforeWindow: (window: Window, glfw: unknown) => void;
}>
>;
/**
* GLFW Window API wrapper.
*
* Window is a higher-level JS wrapper for the GLFW API. It helps managing
* window instances and extends EventEmitter to provide event handling.
*/
export class Window extends EventEmitter {
public constructor(opts: TWindowOpts = {}) {
super();
this._readOptions(opts);
this._applyWindowHints();
this._emitter = { emit: (t, e) => this.emit(t, e as TEvent) };
// This CREATES window, as mode switches from `undefined`
this.mode = opts.mode || 'windowed';
this._syncFramebufferRatio();
this._applyInitialAppearance(opts);
this._readContextAttributes();
this._bindStateEvents();
this.requestAnimationFrame = (cb) => setImmediate(() => glfw.drawWindow(this._window, cb));
this.cancelAnimationFrame = (id) => {
clearImmediate(id);
};
this._rafCb = () => {
/* nop */
};
this._drawWithCb = () => glfw.drawWindow(this._window, this._rafCb);
this.frame = (cb) => {
this._rafCb = cb;
return setImmediate(this._drawWithCb);
};
this.loop = (cb) => {
let next: NodeJS.Immediate | null = null;
const loopFunc = () => {
glfw.drawWindow(this._window, cb);
next = setImmediate(loopFunc);
};
next = setImmediate(loopFunc);
return () => {
if (next) {
clearImmediate(next);
}
};
};
this.event = null;
this.swapBuffers();
}
/** The ratio between physical and logical pixels, e.g. 2 for Retina. Default is 1. */
public get ratio(): number {
return this._ratio;
}
/** Alias for `ratio`. */
public get devicePixelRatio(): number {
return this._ratio;
}
/** GLFW window pointer, represented as a number. */
public get handle(): TWindowHandle {
return this._window;
}
/** Always 0. */
public get scrollX(): number {
return 0;
}
/** Always 0. */
public get scrollY(): number {
return 0;
}
/** Number of MSAA samples. */
public get msaa(): number {
return this._msaa;
}
/** OpenGL vendor/version info. */
public get version(): string {
return `GL ${this._major}.${this._minor}.${this._rev} Profile: ${this._prof}`;
}
/** Platform device, e.g. `wglGetCurrentDC()` or `glfwGetX11Display()`. */
public get platformDevice(): number {
return glfw.platformDevice();
}
/** Platform-specific window handle, e.g. Windows HWND. */
public get platformWindow(): number {
return glfw.platformWindow(this._window);
}
/** Platform OpenGL context id for this window. */
public get platformContext(): number {
return glfw.platformContext(this._window);
}
/** The size of the allocated framebuffer. */
public get framebufferSize(): TSize {
return glfw.getFramebufferSize(this._window);
}
/** Current OpenGL context pointer. */
public get currentContext(): TWindowHandle | null {
return glfw.getCurrentContext(this._window);
}
/**
* Window display mode.
*
* One of: 'windowed', 'borderless', 'fullscreen'. Here 'borderless' emulates
* fullscreen by a frameless, screen-sized window. When this property changes,
* a new window is created and the old one is hidden.
*/
public get mode(): TWindowMode {
return this._mode ?? 'windowed';
}
public set mode(v: TWindowMode) {
if (this._mode === v) {
return;
}
const currentMonitor = this.getCurrentMonitor();
const prevMode = this._mode;
this._mode = v;
if (this._window) {
// Fullscreen can't be hidden (uh-oh)
if (prevMode === 'fullscreen') {
this.destroy();
this._modeCache[prevMode] = null;
} else {
this.hide();
}
}
if (this._monitors.length === 0) {
throw new Error('No suitable display found for a new GLFW Window.');
}
this._display = currentMonitor ? this._monitors.indexOf(currentMonitor) : 0;
if (this._mode === 'windowed') {
this._x = this._prevX || this._x;
this._y = this._prevY || this._y;
this._width = this._prevWidth || this._width;
this._height = this._prevHeight || this._height;
this._decorated = this._prevDecorated || this._decorated;
this._prevX = null;
this._prevY = null;
this._prevWidth = null;
this._prevHeight = null;
this._prevDecorated = null;
} else if (
currentMonitor &&
(this._width !== currentMonitor.width || this._height !== currentMonitor.height)
) {
this._prevX = this._x;
this._prevY = this._y;
this._prevWidth = this._width;
this._prevHeight = this._height;
this._x = currentMonitor.pos_x;
this._y = currentMonitor.pos_y;
this._width = currentMonitor.width;
this._height = currentMonitor.height;
}
const prevWindow = this._modeCache[this._mode];
if (prevWindow) {
this._window = prevWindow;
this.show();
} else {
this._create();
this._modeCache[this._mode] = this._window;
}
if (this._mode === 'windowed') {
if (this._x && this._y) {
glfw.setWindowPos(this._window, this._x, this._y);
}
} else if (this._mode === 'borderless') {
const monitor = this._monitors[this._display];
glfw.setWindowPos(this._window, monitor.pos_x, monitor.pos_y);
glfw.setWindowSize(this._window, monitor.width, monitor.height);
}
this.makeCurrent();
glfw.swapInterval(this._vsync);
this._pxWidth = this._width * this._ratio;
this._pxHeight = this._height * this._ratio;
this.emit('mode', { type: 'mode', mode: this._mode });
this.emit('resize', { type: 'resize', width: this._pxWidth, height: this._pxHeight });
}
/** Width in physical pixels. */
public get width(): number {
return this._pxWidth;
}
public set width(v: number) {
if (this._pxWidth === v) {
return;
}
this._width = Math.floor(v / this._ratio);
this._pxWidth = v;
glfw.setWindowSize(this._window, this._width, this._height);
}
/** Height in physical pixels. */
public get height(): number {
return this._pxHeight;
}
public set height(v: number) {
if (this._pxHeight === v) {
return;
}
this._height = Math.floor(v / this._ratio);
this._pxHeight = v;
glfw.setWindowSize(this._window, this._width, this._height);
}
/** Alias for `width`. */
public get offsetWidth(): number {
return this._pxWidth;
}
public set offsetWidth(v: number) {
this.width = v;
}
/** Alias for `height`. */
public get offsetHeight(): number {
return this._pxHeight;
}
public set offsetHeight(v: number) {
this.height = v;
}
/** Alias for `width`. */
public get w(): number {
return this.width;
}
public set w(v: number) {
this.width = v;
}
/** Alias for `height`. */
public get h(): number {
return this.height;
}
public set h(v: number) {
this.height = v;
}
/** Physical width and height as a tuple. */
public get wh(): [number, number] {
return [this.width, this.height];
}
public set wh([width, height]: [number, number]) {
if (this._pxWidth === width && this._pxHeight === height) {
return;
}
this._width = Math.floor(width / this._ratio);
this._pxWidth = width;
this._height = Math.floor(height / this._ratio);
this._pxHeight = height;
glfw.setWindowSize(this._window, this._width, this._height);
}
/** The size of the allocated framebuffer. */
public get pxSize(): TSize {
const size = glfw.getFramebufferSize(this._window);
this._pxWidth = size.width;
this._pxHeight = size.height;
this._width = this._pxWidth / this._ratio;
this._height = this._pxHeight / this._ratio;
return size;
}
public set pxSize({ width, height }: TSize) {
this.wh = [width, height];
}
/** Logical window width. */
public get innerWidth(): number {
return this._width;
}
public set innerWidth(v: number) {
this.width = v;
}
/** Logical window height. */
public get innerHeight(): number {
return this._height;
}
public set innerHeight(v: number) {
this.height = v;
}
/** Alias for `innerWidth`. */
public get clientWidth(): number {
return this._width;
}
public set clientWidth(v: number) {
this.width = v;
}
/** Alias for `innerHeight`. */
public get clientHeight(): number {
return this._height;
}
public set clientHeight(v: number) {
this.height = v;
}
/** Alias for `.on('keydown', callback)`. Setter adds a callback. */
public get onkeydown(): TCbField<TKeyEvent> {
return this.listeners('keydown') as TEventCb<TKeyEvent>[];
}
public set onkeydown(cb: TEventCb<TKeyEvent>) {
this.on('keydown', cb);
}
/** Alias for `.on('keyup', callback)`. Setter adds a callback. */
public get onkeyup(): TCbField<TKeyEvent> {
return this.listeners('keyup') as TEventCb<TKeyEvent>[];
}
public set onkeyup(cb: TEventCb<TKeyEvent>) {
this.on('keyup', cb);
}
/** Alias for `.on('mousedown', callback)`. Setter adds a callback. */
public get onmousedown(): TCbField<TMouseButtonEvent> {
return this.listeners('mousedown') as TEventCb<TMouseButtonEvent>[];
}
public set onmousedown(cb: TEventCb<TMouseButtonEvent>) {
this.on('mousedown', cb);
}
/** Alias for `.on('mouseup', callback)`. Setter adds a callback. */
public get onmouseup(): TCbField<TMouseButtonEvent> {
return this.listeners('mouseup') as TEventCb<TMouseButtonEvent>[];
}
public set onmouseup(cb: TEventCb<TMouseButtonEvent>) {
this.on('mouseup', cb);
}
/** Alias for `.on('wheel', callback)`. Setter adds a callback. */
public get onwheel(): TCbField<TMouseScrollEvent> {
return this.listeners('wheel') as TEventCb<TMouseScrollEvent>[];
}
public set onwheel(cb: TEventCb<TMouseScrollEvent>) {
this.on('wheel', cb);
}
/** Alias for `.on('mousewheel', callback)`. Setter adds a callback. */
public get onmousewheel(): TCbField<TMouseScrollEvent> {
return this.listeners('mousewheel') as TEventCb<TMouseScrollEvent>[];
}
public set onmousewheel(cb: TEventCb<TMouseScrollEvent>) {
this.on('mousewheel', cb);
}
/** Alias for `.on('resize', callback)`. Setter adds a callback. */
public get onresize(): TCbField<TEvent & TSize> {
return this.listeners('resize') as TEventCb<TEvent & TSize>[];
}
public set onresize(cb: TEventCb<TEvent & TSize>) {
this.on('resize', cb);
}
/** Logical window size. */
public get size(): TSize {
const size = glfw.getWindowSize(this._window);
this._width = size.width;
this._height = size.height;
this._pxWidth = size.width * this._ratio;
this._pxHeight = size.height * this._ratio;
return size;
}
public set size({ width, height }: TSize) {
if (this._width === width && this._height === height) {
return;
}
this._width = width;
this._height = height;
this._pxWidth = width * this._ratio;
this._pxHeight = height * this._ratio;
glfw.setWindowSize(this._window, width, height);
}
/** Whether the window is resizable. */
public get resizable(): boolean {
const resizable = glfw.getWindowAttrib(this._window, glfw.RESIZABLE);
this._resizable = !!resizable;
return this._resizable;
}
public set resizable(v: boolean) {
this._resizable = v;
glfw.setWindowAttrib(
this._window,
glfw.RESIZABLE,
this._resizable ? glfw.TRUE : glfw.FALSE,
);
}
/** Window title. */
public get title(): string {
return this._title;
}
public set title(v: string) {
this._title = v;
glfw.setWindowTitle(this._window, this._title);
}
/**
* Window icon in RGBA format.
*
* Consider using the @node-3d/image Image implementation. The given image is
* vertically flipped unless `noflip` is true.
*
* @see https://github.com/node-3d/image
* @see https://github.com/node-3d/glfw/examples/icon.ts
*/
public get icon(): TImageData | null {
return this._icon;
}
public set icon(v: TImageData | null | undefined) {
if (!(v && typeof v === 'object')) {
this._icon = null;
return;
}
this._icon = v;
glfw.setWindowIcon(this._window, this._icon);
}
/** Whether the window is going to be closed. */
public get shouldClose(): boolean {
return !!glfw.windowShouldClose(this._window);
}
public set shouldClose(v: boolean) {
glfw.setWindowShouldClose(this._window, v ? glfw.TRUE : glfw.FALSE);
}
/** Window position X coordinate on the screen. */
public get x(): number {
return this._x;
}
public set x(v: number) {
if (this._x === v) {
return;
}
this._x = v;
glfw.setWindowPos(this._window, this._x, this._y);
}
/** Window position Y coordinate on the screen. */
public get y(): number {
return this._y;
}
public set y(v: number) {
if (this._y === v) {
return;
}
this._y = v;
glfw.setWindowPos(this._window, this._x, this._y);
}
/** Window position coordinates. */
public get pos(): TPos {
const pos = glfw.getWindowPos(this._window);
this._x = pos.x;
this._y = pos.y;
return pos;
}
public set pos({ x, y }: TPos) {
if (this._x === x && this._y === y) {
return;
}
this._x = x;
this._y = y;
glfw.setWindowPos(this._window, x, y);
}
public get vsync(): number {
return this._vsync;
}
public set vsync(isVsyncEnabled: number) {
if (this._vsync === isVsyncEnabled) {
return;
}
this._vsync = isVsyncEnabled;
this.makeCurrent();
glfw.swapInterval(this._vsync);
}
/** Cursor position coordinates. */
public get cursorPos(): TPos {
return glfw.getCursorPos(this._window);
}
public set cursorPos({ x, y }: TPos) {
glfw.setCursorPos(this._window, x, y);
}
/** Bound `requestAnimationFrame` method, returns a timer id. */
public requestAnimationFrame: (callback: (dateNow: number) => void) => NodeJS.Immediate;
/** Bound `cancelAnimationFrame` method. Cancels by id. */
public cancelAnimationFrame: (id: NodeJS.Immediate) => void;
/**
* Bound optimized single-frame method, returns a timer id.
*
* This method should only have one call per frame. Like `requestAnimationFrame`
* it issues a `setImmediate`, but does not need to create a new arrow function
* for the frame runner.
*/
public frame: (callback: (dateNow: number) => void) => NodeJS.Immediate;
/**
* Bound optimized loop method that continuously generates frames with `callback`.
*
* The returned function breaks the loop.
*/
public loop: (callback: (dateNow: number) => void) => () => void;
/** Currently dispatched event, or null outside event dispatch. */
public event: TEvent | null;
/** Get a monitor having the most overlap with this window. */
public getCurrentMonitor(): TMonitor | null {
if (!this._window) {
return this._primaryDisplay;
}
let bestOverlap = 0;
let bestMonitor = null;
const { x: wx, y: wy } = this.pos;
const { width: ww, height: wh } = this.size;
this._monitors = glfw.getMonitors();
for (const monitor of this._monitors) {
const { width: mw, height: mh } = monitor;
const { pos_x: mx, pos_y: my } = monitor;
const overlap =
Math.max(0, Math.min(wx + ww, mx + mw) - Math.max(wx, mx)) *
Math.max(0, Math.min(wy + wh, my + mh) - Math.max(wy, my));
if (bestOverlap < overlap) {
bestOverlap = overlap;
bestMonitor = monitor;
}
}
return bestMonitor;
}
/**
* Get a browser-like rect for this window.
*
* @see https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect
*/
public getBoundingClientRect(): TRect {
return {
x: 0,
y: 0,
width: this._pxWidth,
height: this._pxHeight,
left: 0,
top: 0,
right: this._pxWidth,
bottom: this._pxHeight,
};
}
/**
* Get key state.
*
* @see https://www.glfw.org/docs/latest/group__keys.html
*/
public getKey(key: number): number {
return glfw.getKey(this._window, key);
}
/**
* Get mouse button state.
*
* @see https://www.glfw.org/docs/latest/group__buttons.html
*/
public getMouseButton(button: number): number {
return glfw.getMouseButton(this._window, button);
}
/**
* Get window attribute.
*
* @see https://www.glfw.org/docs/latest/window_guide.html#window_attribs
*/
public getWindowAttrib(attrib: number): number {
return glfw.getWindowAttrib(this._window, attrib);
}
/** Set input mode option. */
public setInputMode(mode: number, value: number): void {
glfw.setInputMode(this._window, mode, value);
}
/** Swap the front and back buffers of the window. */
public swapBuffers(): void {
glfw.swapBuffers(this._window);
}
/** Make this window's GL context current. */
public makeCurrent(): void {
glfw.makeContextCurrent(this._window);
}
/** Destroy the GLFW window. */
public destroy(): void {
glfw.destroyWindow(this._window);
}
/** Minimize the window. */
public iconify(): void {
glfw.iconifyWindow(this._window);
}
/** Restore the window if it was previously iconified or maximized. */
public restore(): void {
glfw.restoreWindow(this._window);
}
/** Hide the window. */
public hide(): void {
glfw.hideWindow(this._window);
}
/** Show the window if it is hidden. */
public show(): void {
glfw.showWindow(this._window);
}
public emit(type: string, event: TEvent): boolean {
if (type === 'keydown' || type === 'keyup') {
const keyEvent = event as TKeyEvent;
const glfwCode = keyEvent.which;
keyEvent.which = extraCodes[glfwCode] || glfwCode;
event.keyCode = keyEvent.which;
keyEvent.key =
(keyEvent.charCode && String.fromCodePoint(keyEvent.charCode)) ||
keyEvent.code ||
keyNames[glfwCode] ||
'?';
keyEvent.code =
codeNames[glfwCode] ||
(keyEvent.code && `Key${keyEvent.code.toUpperCase()}`) ||
'UNKNOWN';
}
event.target = this;
event.type = type;
event.preventDefault = emptyFunction;
event.stopPropagation = emptyFunction;
this.makeCurrent();
this.event = event;
const isHandled = super.emit(type, event);
this.event = null;
return isHandled;
}
/** Draw one frame by polling events, calling `callback`, and swapping buffers. */
public drawWindow(cb: (dateNow: number) => void): void {
glfw.drawWindow(this._window, cb);
}
/** Alias for `emit`; event type is expected inside the event object. */
public dispatchEvent(event: TEvent): void {
this.emit(event.type, event);
}
/** Alias for `on`. */
public addEventListener(name: string, callback: TEventCb<TEvent>): this {
this.on(name, callback);
return this;
}
/** Alias for `removeListener`. */
public removeEventListener(name: string, callback: TEventCb<TEvent>): void {
this.removeListener(name, callback);
}
public static exit(): never {
// oxlint-disable-next-line unicorn/no-process-exit
process.exit(0);
}
// Previous windowed-mode X coordinate restored after leaving fullscreen-like modes.
private _prevX: number | null = null;
// Previous windowed-mode Y coordinate restored after leaving fullscreen-like modes.
private _prevY: number | null = null;
// Previous windowed-mode width restored after leaving fullscreen-like modes.
private _prevWidth: number | null = null;
// Previous windowed-mode height restored after leaving fullscreen-like modes.
private _prevHeight: number | null = null;
// Previous decoration state restored after leaving borderless mode.
private _prevDecorated: boolean | null = null;
// Requested or detected OpenGL major version.
private _major: number = 0;
// Requested or detected OpenGL minor version.
private _minor: number = 0;
// Detected OpenGL context revision.
private _rev: number = 0;
// Detected OpenGL profile value.
private _prof: number = 0;
// Cached window title mirrored to GLFW.
private _title: string = '';
// Cached window icon mirrored to GLFW.
private _icon: TImageData | null = null;
// Hidden GLFW window handles kept for mode switches.
private _modeCache: Partial<Record<TWindowMode, TWindowHandle | null>> = {};
// Logical window width.
private _width: number = DEFAULT_WIDTH;
// Physical framebuffer width.
private _pxWidth: number = DEFAULT_WIDTH;
// Logical window height.
private _height: number = DEFAULT_HEIGHT;
// Physical framebuffer height.
private _pxHeight: number = DEFAULT_HEIGHT;
// Ratio between physical framebuffer pixels and logical window pixels.
private _ratio: number = 1;
// User hook called immediately before a GLFW window is created.
private _onBeforeWindow: TWindowOpts['onBeforeWindow'];
// Current monitor index.
private _display: number = 0;
// Last known monitor list.
private _monitors: readonly TMonitor[] = [];
// Primary monitor fallback used before a native window exists.
private _primaryDisplay: TMonitor | null = null;
// Current swap interval value.
private _vsync: number = 0;
// Whether fullscreen windows iconify on focus loss.
private _autoIconify: boolean = true;
// Current logical window mode.
private _mode: TWindowMode | undefined;
// Whether GLFW creates decorated windows for this instance.
private _decorated: boolean = true;
// Multisample antialiasing level passed to GLFW.
private _msaa: number = 0;
// Current resizable flag mirrored to GLFW.
private _resizable: boolean = true;
// Event bridge passed into the native GLFW addon.
private _emitter: TNativeEmitter;
// Current native GLFW window handle.
private _window!: TWindowHandle; // assigned in constructor during `this.mode = x`
// Current window X coordinate.
private _x: number = 0;
// Current window Y coordinate.
private _y: number = 0;
// Cached frame callback used by the optimized frame path.
private _rafCb: (dateNow: number) => void;
// Cached draw runner used by the optimized frame path.
private _drawWithCb: () => void;
// Initialize cached fields from constructor options before creating a GLFW window.
private _readOptions(opts: TWindowOpts): void {
this._major = opts.major === undefined ? 2 : opts.major;
this._minor = opts.minor === undefined ? 1 : opts.minor;
this._width = opts.width || 1280;
this._pxWidth = this._width;
this._height = opts.height || 720;
this._pxHeight = this._height;
this._onBeforeWindow = opts.onBeforeWindow;
this._display = opts.display ?? 0;
this._monitors = glfw.getMonitors();
this._primaryDisplay = this._monitors.find((d) => d.is_primary) || null;
this._vsync = opts.vsync ? 1 : 0; // 0 for vsync off
this._autoIconify = opts.autoIconify !== false;
if (opts.decorated !== undefined) {
this._decorated = opts.decorated;
}
this._msaa = opts.msaa || 0;
this._resizable = opts.resizable !== false;
}
// Apply GLFW window hints that must be set before native window creation.
private _applyWindowHints(): void {
glfw.windowHint(glfw.CONTEXT_VERSION_MAJOR, this._major);
glfw.windowHint(glfw.CONTEXT_VERSION_MINOR, this._minor);
glfw.windowHint(glfw.RESIZABLE, this._resizable ? glfw.TRUE : glfw.FALSE);
glfw.windowHint(glfw.VISIBLE, glfw.TRUE);
glfw.windowHint(glfw.RED_BITS, 8);
glfw.windowHint(glfw.GREEN_BITS, 8);
glfw.windowHint(glfw.BLUE_BITS, 8);
glfw.windowHint(glfw.DEPTH_BITS, 24);
glfw.windowHint(glfw.REFRESH_RATE, 0);
glfw.windowHint(glfw.DOUBLEBUFFER, glfw.TRUE);
glfw.windowHint(glfw.AUTO_ICONIFY, this._autoIconify ? glfw.TRUE : glfw.FALSE);
glfw.windowHint(glfw.DECORATED, this._decorated ? glfw.TRUE : glfw.FALSE);
glfw.windowHint(glfw.SAMPLES, this._msaa);
}
// Compute device pixel ratio after the native window exists.
private _syncFramebufferRatio(): void {
const sizeWin = this.size;
const sizeFB = this.framebufferSize;
this._ratio = sizeFB.width / sizeWin.width;
}
// Apply initial title and icon values after the native window exists.
private _applyInitialAppearance(opts: TWindowOpts): void {
this.icon = opts.icon;
if (opts.title) {
this.title = opts.title;
} else {
const dirname = process.cwd();
this.title = dirname;
}
}
// Read OpenGL context attributes that are only available after window creation.
private _readContextAttributes(): void {
this._major = glfw.getWindowAttrib(this._window, glfw.CONTEXT_VERSION_MAJOR);
this._minor = glfw.getWindowAttrib(this._window, glfw.CONTEXT_VERSION_MINOR);
this._rev = glfw.getWindowAttrib(this._window, glfw.CONTEXT_REVISION);
this._prof = glfw.getWindowAttrib(this._window, glfw.OPENGL_PROFILE);
}
// Keep cached position and size fields aligned with native events.
private _bindStateEvents(): void {
this.on('window_pos', ({ x, y }) => {
this._x = x;
this._y = y;
});
this.on('wresize', ({ width, height }) => {
this._width = width;
this._height = height;
});
this.on('resize', ({ width, height }) => {
this._pxWidth = width;
this._pxHeight = height;
});
}
// Create a new native window according to the current display mode.
private _create(): void {
if (this._mode === 'windowed') {
glfw.windowHint(glfw.DECORATED, this._decorated ? glfw.TRUE : glfw.FALSE);
if (this._onBeforeWindow) {
this._onBeforeWindow(this, glfw);
}
this._window = glfw.createWindow(
this._width,
this._height,
this._emitter,
this._title ?? undefined,
);
} else if (this._mode === 'borderless') {
this._prevDecorated = this._decorated;
this._decorated = false;
glfw.windowHint(glfw.DECORATED, glfw.FALSE);
if (this._onBeforeWindow) {
this._onBeforeWindow(this, glfw);
}
this._window = glfw.createWindow(
this._width,
this._height,
this._emitter,
this._title ?? undefined,
);
} else if (this._mode === 'fullscreen') {
this._adjustFullscreen();
if (this._onBeforeWindow) {
this._onBeforeWindow(this, glfw);