-
Notifications
You must be signed in to change notification settings - Fork 5
/
nw.lua
2230 lines (1864 loc) · 55.2 KB
/
nw.lua
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
--Cross-platform windows for Lua.
--Written by Cosmin Apreutesei. Public domain.
local ffi = require'ffi'
local glue = require'glue'
local box2d = require'box2d'
local events = require'events'
local time = require'time'
local assert = glue.assert --assert with string.format
local indexof = glue.indexof
local nw = {}
local backends = {
Windows = 'nw_winapi',
OSX = 'nw_cocoa',
Linux = 'nw_xlib',
}
local bkname = assert(backends[ffi.os], 'unsupported OS %s', ffi.os)
nw.backend = require(bkname)
nw.backend.frontend = nw
--helpers --------------------------------------------------------------------
local function optarg(opt, true_arg, false_arg, nil_arg)
opt = glue.index(opt)
return function(arg)
if arg == true then
return true_arg
elseif arg == false then
return false_arg
elseif arg == nil then
return nil_arg
elseif opt[arg] then
return arg
else
error('invalid argument', 2)
end
end
end
--oo -------------------------------------------------------------------------
local object = {}
function object:dead()
return self._dead or false
end
function object:_check()
assert(not self._dead, 'dead object')
end
--create a get / set method `m() -> v` / `m(v)` implemented via calls to
--separate getter and setter methods in the backend.
function object:_property(name)
local getter = 'get_'..name
local setter = 'set_'..name
self[name] = function(self, val)
self:_check()
if val == nil then
return self.backend[getter](self.backend)
else
self.backend[setter](self.backend, val)
end
end
end
--events ---------------------------------------------------------------------
glue.update(object, events)
local fire = object.fire
function object:fire(...)
if self._dead then return end
if self._events_disabled then return end
return fire(self, ...)
end
--enable or disable events. returns the old state.
function object:events(enabled)
if enabled == nil then
return not self._events_disabled
end
local old = not self._events_disabled
self._events_disabled = not enabled
return old
end
--app object -----------------------------------------------------------------
local app = glue.update({}, object)
--return the singleton app object.
--load a default backend on the first call if no backend was set by the user.
function nw:app()
if not self._app then
self._app = app:_init(self, self.backend.app)
end
return self._app
end
function app:_init(nw, backend_class)
self.nw = nw
self._running = false
self._windows = {} --{window1, ...}
self._notifyicons = {} --{icon = true}
self._autoquit = true --quit after the last visible window closes
self._ignore_numlock = false --ignore the state of the numlock key on keyboard events
self.backend = backend_class:init(self)
self._state = self:_get_state()
return self
end
--version checks -------------------------------------------------------------
--check if v2 >= v1, where v1 and v2 have the form 'maj.min.etc...'.
local function check_version(v1, v2)
local v1 = v1:lower()
local v2 = v2:lower()
local ret
while v1 ~= '' do --while there's another part of ver1 to check...
if v2 == '' then --there's no part of ver2 to check against.
return false
end
local n1, n2
n1, v1 = v1:match'^(%d*)%.?(.*)' --eg. '3.0' -> '3', '0'
n2, v2 = v2:match'^(%d*)%.?(.*)'
assert(n1 ~= '', 'invalid syntax') --ver1 part is a dot.
assert(n2 ~= '', 'invalid syntax') --ver2 part is a dot.
if ret == nil then --haven't decided yet
if n1 ~= '' then --above checks imply n2 ~= '' also.
local n1 = tonumber(n1)
local n2 = tonumber(n2)
if n1 ~= n2 then --version parts are different, decide now.
ret = n2 > n1
end
end
end
end
if ret ~= nil then --a comparison has been made.
return ret
end
return true --no more parts of v1 to check.
end
local qcache = {} --{query = true|false}
function app:ver(q)
if qcache[q] == nil then
local what, qver = q:match'^([^%s]+)%s*(.*)$'
assert(what, 'invalid query')
local ver = self.backend:ver(what:lower())
qcache[q] = ver and (qver == '' and ver or check_version(qver, ver)) or false
end
return qcache[q]
end
--message loop and timers ----------------------------------------------------
local password = {} --distinguish yielding via app:sleep() from other yielding
--sleep function that can be used inside the function passed to app:run().
--unlike time.sleep(), it allows processing of events while waiting.
function app:sleep(seconds) --no arg, true or false means sleep forever.
coroutine.yield(password, seconds or true)
end
--start the main loop and/or run a function asynchronously.
function app:run(func)
--schedule to run a function asynchronously.
if func then
local proc = coroutine.wrap(function()
local ok, err = xpcall(func, debug.traceback)
if not ok then
error(err, 2)
end
coroutine.yield(password) --proc finished
end)
local was_running = self._running
local function step()
local pwd, sleep_time = proc()
assert(pwd == password, 'yield in async proc')
if not sleep_time then --proc finished
--if app was not running when we started, stop it back
if not was_running then
self:stop()
end
return
end
if sleep_time == true then return end --sleep forever
self:runafter(sleep_time, step)
end
self:runafter(0, step)
end
if self._running then return end --ignore while running
self._running = true --run() barrier
self.backend:run()
self._running = false
self._stopping = false --stop() barrier
end
function app:poll(timeout)
return self.backend:poll(timeout)
end
function app:running()
return self._running
end
function app:stop()
--if not self._running then return end --ignore while not running
if self._stopping then return end --ignore repeated attempts
self._stopping = true
self.backend:stop()
end
function app:runevery(seconds, func)
seconds = math.max(0, seconds)
self.backend:runevery(seconds, func)
end
function app:runafter(seconds, func)
self:runevery(seconds, function()
func()
return false
end)
end
app._maxfps = 60
function app:maxfps(fps)
if fps == nil then
return self._maxfps or false
else
self._maxfps = fps
end
end
--quitting -------------------------------------------------------------------
function app:autoquit(autoquit)
if autoquit == nil then
return self._autoquit
else
self._autoquit = autoquit
end
end
--ask the app and all windows if they can quit. need unanimous agreement to quit.
function app:_canquit()
self._quitting = true --quit() barrier
local allow = self:fire'quitting' ~= false
for _,win in ipairs(self:windows()) do
if not win:dead() and not win:parent() then
allow = win:_canclose('quit', nil) and allow
end
end
self._quitting = nil
return allow
end
function app:_forcequit()
self._quitting = true --quit() barrier
local t = self:windows()
for i = #t, 1, -1 do
local win = t[i]
if not win:dead() and not win:parent() then
win:free'force'
end
end
--free notify icons otherwise they hang around (both in XP and in OSX).
self:_free_notifyicons()
self:_free_dockicon()
self:stop()
self._quitting = nil
end
function app:quit()
if self._quitting then return end --ignore if already quitting
if not self._running then return end --ignore if not running
if self:_canquit() then
self:_forcequit()
end
end
function app:_backend_quitting()
self:quit()
end
--window list ----------------------------------------------------------------
--get existing windows in creation order
function app:windows(arg, filter)
if arg == '#' then
if filter then
local n = 0
for _,win in ipairs(self._windows) do
n = n + (filter(win) ~= false and 1 or 0)
end
return n
else
return #self._windows
end
elseif type(arg) == 'function' then
local t = {}
for _,win in ipairs(self._windows) do
if filter(win) ~= false then
t[#t+1] = win
end
end
return t
else
return glue.extend({}, self._windows) --take a snapshot
end
end
function app:_window_created(win)
table.insert(self._windows, win)
self:fire('window_created', win)
end
function app:_window_closed(win)
self:fire('window_closed', win)
table.remove(self._windows, indexof(win, self._windows))
end
--windows --------------------------------------------------------------------
local window = glue.update({}, object)
local defaults = {
--state
visible = true,
minimized = false,
maximized = false,
enabled = true,
--positioning
min_cw = 1,
min_ch = 1,
--frame
title = '',
transparent = false,
corner_radius = 0,
background_color = false,
--behavior
topmost = false,
minimizable = true,
maximizable = true,
closeable = true,
resizeable = true,
fullscreenable = true,
activable = true,
autoquit = false, --quit the app on closing
hideonclose = true, --only hide on close without freeing the window
edgesnapping = 'screen',
sticky = false, --only for child windows
}
--default overrides for parented windows
local defaults_child = {
minimizable = false,
maximizable = false,
fullscreenable = false,
edgesnapping = 'parent siblings screen',
sticky = true,
}
local opengl_defaults = {
version = '1.0',
vsync = true,
fsaa = false,
}
local function opengl_options(t)
if not t then return end
local glopt = glue.update({}, opengl_defaults)
if t ~= true then
glue.update(glopt, t)
end
return glopt
end
local frame_types = glue.index{'normal', 'none', 'toolbox'}
local function checkframe(frame)
frame =
frame == true and 'normal' or
frame == false and 'none' or
frame or 'normal'
assert(frame_types[frame], 'invalid frame type')
return frame
end
function app:window(...)
local t
if type((...)) ~= 'table' then
local cw, ch, title, visible = ...
t = {cw = cw, ch = ch, title = title, visible = visible}
else
t = ...
end
return window:_new(self, self.backend.window, t)
end
function window:_new(app, backend_class, useropt)
--check/normalize args.
local opt = glue.update({},
defaults,
useropt.parent and defaults_child or nil,
useropt)
opt.frame = checkframe(opt.frame)
opt.opengl = opengl_options(useropt.opengl)
--non-activable windows must be frameless (Windows limitation)
if not opt.activable then
assert(opt.frame == 'none', 'windows with a title bar cannot be non-activable')
end
if opt.parent then
--prevent creating child windows in parent's closed() event or after.
assert(not opt.parent._closed, 'parent is closed')
--child windows can't be minimizable because they don't show in taskbar.
assert(not opt.minimizable, 'child windows cannot be minimizable')
assert(not opt.minimized, 'child windows cannot be minimized')
--child windows can't be maximizable or fullscreenable (X11 limitation).
assert(not opt.maximizable, 'child windows cannot be maximizable')
assert(not opt.fullscreenable, 'child windows cannot be fullscreenable')
end
if opt.sticky then
assert(opt.parent, 'sticky windows must have a parent')
end
--unparented toolboxes don't make sense because they don't show in taskbar
--so they can't be activated when they are completely behind other windows.
--they can't be (minimiz|maximiz|fullscreen)able either (Windows/X11 limitation).
if opt.frame == 'toolbox' then
assert(opt.parent, 'toolbox windows must have a parent')
end
--transparent windows must be frameless (Windows limitation)
if opt.transparent then
assert(opt.frame == 'none', 'transparent windows must be frameless')
end
if not opt.resizeable then
if useropt.maximizable == nil then opt.maximizable = false end
if useropt.fullscreenable == nil then opt.fullscreenable = false end
assert(not opt.maximizable, 'a maximizable window cannot be non-resizeable')
assert(not opt.fullscreenable, 'a fullscreenable cannot be non-resizeable')
end
--maxsize constraints result in undefined behavior in maximized and fullscreen state.
--they work except in Unity which doesn't respect them when maximizing.
--also Windows doesn't center the window on screen in fullscreen mode.
if opt.max_cw or opt.max_ch then
assert(not opt.maximizable, 'a maximizable window cannot have a maximum size')
assert(not opt.fullscreenable, 'a fullscreenable window cannot have a maximum size')
end
--if missing some frame coords but given some client coords, convert client
--coords to frame coords, and replace missing frame coords with the result.
if not (opt.x and opt.y and opt.w and opt.h) and (opt.cx or opt.cy or opt.cw or opt.ch) then
local x1, y1, w1, h1 = app:client_to_frame(
opt.frame,
opt.menu and true or false,
opt.resizeable and true or false,
opt.cx or 0,
opt.cy or 0,
opt.cw or 0,
opt.ch or 0)
opt.x = opt.x or (opt.cx and x1)
opt.y = opt.y or (opt.cy and y1)
opt.w = opt.w or (opt.cw and w1)
opt.h = opt.h or (opt.ch and h1)
end
--width and height must be given, either of the client area or of the frame.
assert(opt.w, 'w or cw expected')
assert(opt.h, 'h or ch expected')
--either cascading or fixating the position, there's no mix.
assert((not opt.x) == (not opt.y),
'both x (or cx) and y (or cy) or none expected')
if opt.x == 'center-main' or opt.x == 'center-active' then
local disp = opt.x == 'center-active'
and app:active_display() or app:main_display()
opt.x = disp.cx + (disp.cw - opt.w) / 2
end
if opt.y == 'center-main' or opt.y == 'center-active' then
local disp = opt.y == 'center-active'
and app:active_display() or app:main_display()
opt.y = disp.cy + (disp.ch - opt.h) / 2
end
--avoid zero client sizes (X limitation)
opt.min_cw = math.max(opt.min_cw, 1)
opt.min_ch = math.max(opt.min_ch, 1)
--avoid negative corner radius
opt.corner_radius = math.max(opt.corner_radius, 0)
self = glue.update({app = app}, self)
--stored properties
self._parent = opt.parent
self._frame = opt.frame
self._transparent = opt.transparent
self._corner_radius = opt.corner_radius
self._background_color = opt.background_color
self._minimizable = opt.minimizable
self._maximizable = opt.maximizable
self._closeable = opt.closeable
self._resizeable = opt.resizeable
self._fullscreenable = opt.fullscreenable
self._activable = opt.activable
self._autoquit = opt.autoquit
self._hideonclose = opt.hideonclose
self._sticky = opt.sticky
self._opengl = opt.opengl
self:edgesnapping(opt.edgesnapping)
--internal state
self._mouse = {inside = false}
self._down = {}
self._views = {}
self._cursor_visible = true
self._cursor = 'arrow'
self.backend = backend_class:new(app.backend, self, opt)
--cached window state
self._state = self:_get_state()
self._client_rect = {self:client_rect()}
self._frame_rect = {self:frame_rect()}
self:_init_manual_resize()
app:_window_created(self)
self:invalidate()
--windows are created hidden to allow proper setup before events start.
if opt.visible then
self:show()
end
if opt.tooltip then
self:tooltip(tooltip)
end
return self
end
--closing --------------------------------------------------------------------
function window:_canclose(reason, closing_window)
if reason == true then return true end --force
if self._closing then return false end --reject while closing
self._closing = true --_canclose() barrier
local allow = self:fire('closing', reason, closing_window) ~= false
--children must agree too
for i,win in ipairs(self:children()) do
allow = win:_canclose(reason, closing_window) and allow
end
self._closing = nil
return allow
end
function window:close(reason)
if self:hideonclose() and not self:visible() then
return
end
if self:_backend_closing(reason) then
if not self._quitapp and self:hideonclose() then
self:hide()
else
self.backend:forceclose()
end
end
end
function window:free(dontask)
if self:_backend_closing(dontask == true or 'free', true) then
self.backend:forceclose()
end
end
local function is_alive_root_and_visible(win)
return not win:dead() and not win:parent() and win:visible()
end
function window:_backend_closing(reason, donthide)
if self._closed then return false end --reject if closed
if not self:_canclose(reason or 'close', self) then
return false
end
if self:autoquit() or (
app:autoquit()
and not self:parent() --closing a root window
and app:windows('#', is_alive_root_and_visible) == 1 --the only one
) then
self._quitapp = app:_canquit() or nil
end
if not donthide and not self._quitapp and self:hideonclose() then
self:hide()
return false
end
return true
end
function window:_backend_closed()
if self._closed then return end --ignore if closed
self._closed = true --_backend_closing() and _backend_closed() barrier
self:fire'closed'
app:_window_closed(self)
self:_free_views()
self._dead = true
if self._quitapp then
app:_forcequit()
self._quitapp = nil
end
end
--activation -----------------------------------------------------------------
local modes = glue.index{'alert', 'force', 'info'}
function app:activate(mode)
mode = mode or 'alert'
assert(modes[mode], 'invalid mode')
self.backend:activate(mode)
end
function app:active_window()
return self.backend:active_window()
end
function app:active()
return self.backend:active()
end
function window:activate()
self:_check()
if not self:visible() then return end
self.backend:activate()
end
function window:active()
self:_check()
if not self:visible() then return false end --false if hidden
return self.backend:active()
end
--single app instance --------------------------------------------------------
function app:already_running()
return self.backend:already_running()
end
function app:wakeup_other_instances()
self.backend:wakeup_other_instances()
end
function app:_backend_wakeup()
self:fire'wakeup'
end
function app:check_single_instance()
if self:already_running() then
self:wakeup_other_instances()
os.exit(0)
end
self:on('wakeup', function(self)
self:activate()
end)
end
--state/app visibility (OSX only) --------------------------------------------
function app:visible(visible)
if visible == nil then
return self.backend:visible()
elseif visible then
self:unhide()
else
self:hide()
end
end
function app:unhide()
self.backend:unhide()
end
function app:hide()
self.backend:hide()
end
--state/visibility -----------------------------------------------------------
function window:visible(visible)
self:_check()
if visible == nil then
return self.backend:visible()
elseif visible then
self:show()
else
self:hide()
end
end
function window:show()
self:_check()
self.backend:show()
end
function window:hide()
self:_check()
if self:fullscreen() then return end
self.backend:hide()
end
function window:showmodal()
assert(self:activable(), 'window cannot be shown modal: non-activable')
assert(self:parent(), 'without cannot be shown modal: no parent')
self:once('hidden', function(self)
self:parent():enabled(true)
end)
self:parent():enabled(false)
self:show()
end
--state/minimizing -----------------------------------------------------------
function window:isminimized()
self:_check()
if self:parent() then
return false -- child windows cannot be minimized
end
return self.backend:minimized()
end
function window:minimize()
self:_check()
self.backend:minimize()
end
--state/maximizing -----------------------------------------------------------
function window:ismaximized()
self:_check()
return self.backend:maximized()
end
function window:maximize()
self:_check()
self.backend:maximize()
end
--state/restoring ------------------------------------------------------------
function window:restore()
self:_check()
if self:visible() and self:fullscreen() then
self:fullscreen(false)
else
self.backend:restore()
end
end
function window:shownormal()
self:_check()
self.backend:shownormal()
end
--state/fullscreen -----------------------------------------------------------
function window:fullscreen(fullscreen)
self:_check()
if fullscreen == nil then
return self.backend:fullscreen()
elseif fullscreen then
self.backend:enter_fullscreen()
else
self.backend:exit_fullscreen()
end
end
--state/state string ---------------------------------------------------------
function window:_get_state()
local t = {}
table.insert(t, self:visible() and 'visible' or nil)
table.insert(t, self:isminimized() and 'minimized' or nil)
table.insert(t, self:ismaximized() and 'maximized' or nil)
table.insert(t, self:fullscreen() and 'fullscreen' or nil)
table.insert(t, self:active() and 'active' or nil)
return table.concat(t, ' ')
end
function app:_get_state()
local t = {}
table.insert(t, self:visible() and 'visible' or nil)
table.insert(t, self:active() and 'active' or nil)
return table.concat(t, ' ')
end
--state/change event ---------------------------------------------------------
local function diff(s, old, new)
local olds = old:find(s, 1, true) and 1 or 0
local news = new:find(s, 1, true) and 1 or 0
return news - olds
end
local function trigger(self, diff, event_up, event_down)
if diff > 0 then
self:fire(event_up)
elseif diff < 0 then
self:fire(event_down)
end
end
function window:_rect_changed(old_rect, new_rect, changed_event, moved_event, resized_event)
if self:dead() then return end
local x0, y0, w0, h0 = unpack(old_rect)
local x1, y1, w1, h1 = unpack(new_rect)
local moved = x1 ~= x0 or y1 ~= y0
local resized = w1 ~= w0 or h1 ~= h0
if moved or resized then
self:fire(changed_event, x1, y1, w1, h1, x0, y0, w0, h0)
end
if moved then
self:fire(moved_event, x1, y1, x0, y0)
end
if resized then
self:fire(resized_event, w1, h1, w0, h0)
end
return new_rect
end
function window:_backend_changed()
if self._events_disabled then return end
--check if the state has really changed and generate synthetic events
--for each state flag that has actually changed.
local old = self._state
local new = self:_get_state()
self._state = new
if new ~= old then
self:fire('changed', old, new)
trigger(self, diff('visible', old, new), 'shown', 'hidden')
trigger(self, diff('minimized', old, new), 'minimized', 'unminimized')
trigger(self, diff('maximized', old, new), 'maximized', 'unmaximized')
trigger(self, diff('fullscreen', old, new), 'entered_fullscreen', 'exited_fullscreen')
trigger(self, diff('active', old, new), 'activated', 'deactivated')
end
self._client_rect = self:_rect_changed(self._client_rect, {self:client_rect()},
'client_rect_changed', 'client_moved', 'client_resized')
self._frame_rect = self:_rect_changed(self._frame_rect, {self:frame_rect()},
'frame_rect_changed', 'frame_moved', 'frame_resized')
end
function app:_backend_changed()
local old = self._state
local new = self:_get_state()
self._state = new
if new ~= old then
self:fire('changed', old, new)
trigger(self, diff('hidden', old, new), 'hidden', 'unhidden')
trigger(self, diff('active', old, new), 'activated', 'deactivated')
end
end
--state/enabled --------------------------------------------------------------
window:_property'enabled'
--positioning/helpers --------------------------------------------------------
local function override_point(x, y, x1, y1)
return x1 or x, y1 or y
end
local function override_rect(x, y, w, h, x1, y1, w1, h1)
return x1 or x, y1 or y, w1 or w, h1 or h
end
local function frame_rect(x, y, w, h, w1, h1, w2, h2)
return x - w1, y - h1, w + w1 + w2, h + h1 + h2
end
local function unframe_rect(x, y, w, h, w1, h1, w2, h2)
local x, y, w, h = frame_rect(x, y, w, h, -w1, -h1, -w2, -h2)
w = math.max(1, w) --avoid zero client sizes
h = math.max(1, h)
return x, y, w, h
end
--positioning/frame extents --------------------------------------------------
function app:frame_extents(frame, has_menu, resizeable)
frame = checkframe(frame)
if frame == 'none' then
return 0, 0, 0, 0
end
return self.backend:frame_extents(frame, has_menu, resizeable)
end
function app:client_to_frame(frame, has_menu, resizeable, x, y, w, h)
return frame_rect(x, y, w, h, self:frame_extents(frame, has_menu, resizeable))
end
function app:frame_to_client(frame, has_menu, resizeable, x, y, w, h)
return unframe_rect(x, y, w, h, self:frame_extents(frame, has_menu, resizeable))
end
--positioning/client rect ----------------------------------------------------
function window:_can_get_rect()
return not self:isminimized()
end
function window:_can_set_rect()
return not (self:isminimized() or self:ismaximized() or self:fullscreen())
end
function window:_get_client_size()
if not self:_can_get_rect() then return end
return self.backend:get_client_size()
end
function window:_get_client_pos()
if not self:_can_get_rect() then return end
return self.backend:get_client_pos()
end
--convert point in client space to screen space.
function window:to_screen(x, y)
local cx, cy = self:_get_client_pos()
if not cx then return end
return cx+x, cy+y
end
--convert point in screen space to client space.
function window:to_client(x, y)
local cx, cy = self:_get_client_pos()
if not cx then return end
return x-cx, y-cy
end
function window:client_size(cw, ch) --sets or returns cw, ch
if cw or ch then
if not cw or not ch then
local cw0, ch0 = self:client_size()
cw = cw or cw0
ch = ch or ch0
end
self:client_rect(nil, nil, cw, ch)
else
return self:_get_client_size()
end
end
function window:client_rect(x1, y1, w1, h1)
if x1 or y1 or w1 or h1 then
if not self:_can_set_rect() then return end
local cx, cy, cw, ch = self:client_rect()
local ccw, cch = cw, ch
local cx, cy, cw, ch = override_rect(cx, cy, cw, ch, x1, y1, w1, h1)
local x, y, w, h = self:frame_rect()
local dx, dy = self:to_client(x, y)
local dw, dh = w - ccw, h - cch
self.backend:set_frame_rect(cx + dx, cy + dy, cw + dw, ch + dh)
else
local x, y = self:_get_client_pos()
if not x then return end
return x, y, self:_get_client_size()
end
end
--positioning/frame rect -----------------------------------------------------
function window:frame_rect(x, y, w, h) --returns x, y, w, h