-
Notifications
You must be signed in to change notification settings - Fork 28
/
main.js
2195 lines (1968 loc) · 66.6 KB
/
main.js
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
'use strict';
const electron = require('electron');
const app = electron.app;
const fs = require('fs');
const path_mod = require('path');
const formats = require('./formats.js');
const traread = require('./traread.js');
var package_json = {}; /* parsed form of our package.json file */
var main_extension = {}; /* extra code for bound games */
var isbound = false; /* true if we're a single-game app */
var bound_game_path = null;
var winmenus = {}; /* maps window ID to a Menu (Win/Linux only) */
var gamewins = {}; /* maps window ID to a game structure */
var trawins = {}; /* maps window ID to a trashow structure */
var aboutwin = null; /* the splash/about window, if active */
var cardwin = null; /* the postcard window, if active */
var prefswin = null; /* the preferences window, if active */
var transcriptwin = null; /* the transcript browser window, if active */
var gamedialog = false; /* track whether the game-open dialog is visible */
var prefs = {
gamewin_width: 600,
gamewin_height: 800,
gamewin_marginlevel: 1,
gamewin_colortheme: 'lightdark',
gamewin_font: 'lora',
gamewin_customfont: null,
gamewin_zoomlevel: 0,
trashowwin_width: 600,
trashowwin_height: 530,
glulx_terp: 'quixe' // engine.id from formats.js
// could also have zcode_terp, hugo_terp, ink-json_terp here. (I know, ink-json_terp is ugly, I should rename that.)
};
var prefspath = path_mod.join(app.getPath('userData'), 'lectrote-prefs.json');
var prefstimer = null;
var prefswriting = false;
var app_ready = false; /* true once the ready event occurs */
var app_quitting = false; /* true once the will-quit event occurs */
var launch_paths = []; /* game files passed in before app_ready */
var aboutwin_initial = false; /* true if the aboutwin was auto-opened */
var selected_transcript = null; /* in the transcript window */
var window_icon = null; /* icon to apply to all windows (only used on Linux) */
var tray_icon = null; /* icon to use for system tray (only on Windows) */
var search_string = ''; /* recent text search in a game window */
/* Return a list of all open game objects. */
function game_list()
{
var ls = [];
for (var id in gamewins) {
var game = gamewins[id];
ls.push(game);
}
return ls;
}
/* Return the game object for a given window. */
function game_for_window(win)
{
if (!win)
return undefined;
return gamewins[win.id];
}
/* Return the game object for a given webcontents. */
function game_for_webcontents(webcontents)
{
if (!webcontents)
return undefined;
for (var id in gamewins) {
var game = gamewins[id];
if (game.win && game.win.webContents === webcontents)
return game;
}
return undefined;
}
/* Return the trashow object for a given window. */
function trashow_for_window(win)
{
if (!win)
return undefined;
return trawins[win.id];
}
/* Get the transcript selected in the given window. This may be a
trashow window, or the highlighted entry in the transcript browser
window. Returns the transcript filename. */
function get_active_transcript(win)
{
if (win == transcriptwin)
return selected_transcript;
var tra = trashow_for_window(win);
if (tra)
return tra.filename;
return null;
}
/* Return the trashow object for a given webcontents. */
function trashow_for_webcontents(webcontents)
{
if (!webcontents)
return undefined;
for (var id in trawins) {
var tra = trawins[id];
if (tra.win && tra.win.webContents === webcontents)
return tra;
}
return undefined;
}
/* Return the trashow object for a given transcript filename. */
function trashow_for_filename(filename)
{
if (!filename)
return undefined;
for (var id in trawins) {
var tra = trawins[id];
if (tra.filename == filename)
return tra;
}
return undefined;
}
/* A game *or* trashow object. */
function game_trashow_for_window(win)
{
if (!win)
return undefined;
var game = gamewins[win.id];
if (game)
return game;
var tra = trawins[win.id];
if (tra)
return tra;
return undefined;
}
function game_trashow_for_webcontents(webcontents)
{
if (!webcontents)
return undefined;
for (var id in gamewins) {
var game = gamewins[id];
if (game.win && game.win.webContents === webcontents)
return game;
}
for (var id in trawins) {
var tra = trawins[id];
if (tra.win && tra.win.webContents === webcontents)
return tra;
}
return undefined;
}
/* Windows for all games and trashow objects. */
function get_all_game_trashow_windows()
{
var res = [];
for (var id in gamewins) {
var game = gamewins[id];
if (game.win)
res.push(game.win);
}
for (var id in trawins) {
var tra = trawins[id];
if (tra.win)
res.push(tra.win);
}
return res;
}
function construct_recent_game_menu()
{
var res = [];
if (isbound)
return res;
/* This requires a utility function because of Javascript's lousy
closures. */
var add = function(path) {
var opts = {
label: path_mod.basename(path),
click: function() {
launch_game(path);
}
};
res.push(opts);
};
var recents = prefs.recent_games;
if (recents && recents.length) {
for (var ix=0; ix<recents.length; ix++) {
add(recents[ix]);
}
}
return res;
}
/* Add a game to the recently-opened list. Note that this does *not* affect
the "File / Open Recent" submenu! This is an Electron limitation:
https://github.com/atom/electron/issues/527
The submenu will be filled in next time the app launches.
*/
function add_recent_game(path)
{
if (isbound) {
/* We're in bound-game mode and shouldn't be talking about
open lists at all. */
return;
}
/* The system recent list is easy -- it handles its own ordering
and uniqueness. This list shows up on the Dock icon on MacOS. */
app.addRecentDocument(path);
var recents = prefs.recent_games;
if (recents === undefined) {
recents = [];
prefs.recent_games = recents;
}
/* Remove any duplicate so we can move this game to the top. */
for (var ix=0; ix<recents.length; ix++) {
if (recents[ix] == path) {
recents.splice(ix, 1);
break;
}
}
/* Push onto beginning. */
recents.unshift(path);
/* Keep no more than 8. */
if (recents.length > 8)
recents.length = 8;
note_prefs_dirty();
}
/* If you create two windows in a row, the second should be offset. But
if you create a window, close it, and create a new window, the second
should not be offset. Sorry, it's messy to describe and messy to
implement.
The effect is to pick the lowest offset value not used by any window.
The map argument should be a window collection (gamewins or trawins).
*/
function pick_window_offset(map)
{
/* Create a list of all offsets currently in use. */
var offsets = [];
for (var id in map) {
var offset = map[id].offset;
if (offset !== undefined)
offsets[offset] = true;
}
for (var ix=0; true; ix++) {
if (!offsets[ix])
return ix;
}
}
/* If a game window is moved, we no longer care about offsets, because we're
now offsetting from a new position. Clear our existing offset info.
*/
function clear_window_offsets(map)
{
for (var id in map) {
delete map[id].offset;
}
}
/* Called only at app startup. */
function load_prefs()
{
try {
var prefsstr = fs.readFileSync(prefspath, { encoding:'utf8' });
var obj = JSON.parse(prefsstr);
for (var key in obj) {
prefs[key] = obj[key];
}
}
catch (ex) {
/* console.error('load_prefs: unable to load preferences: %s: %s', prefspath, ex); */
}
/* Check to make sure the recent files still exist. */
var recents = prefs.recent_games;
if (recents && recents.length) {
var ls = [];
for (var ix=0; ix<recents.length; ix++) {
try {
fs.accessSync(recents[ix], fs.R_OK);
ls.push(recents[ix]);
}
catch (ex) {}
}
if (ls.length < recents.length) {
prefs.recent_games = ls;
note_prefs_dirty();
}
}
}
/* We can't rely on the web-frame algorithm for this, because we have
to include it when creating the browser window. So we have our
own bit of code: basically 10% larger/smaller per unit.
*/
function zoom_factor_for_level(val)
{
if (!val)
return 1;
return Math.exp(val * 0.09531017980432493);
}
/* Send the current zoom factor to all game windows.
*/
function set_zoom_factor_all(val)
{
for (var win of get_all_game_trashow_windows()) {
invoke_app_hook(win, 'set_zoom_factor', val);
}
if (main_extension.set_zoom_factor)
main_extension.set_zoom_factor(val);
}
/* Set up a window-options object (for creating a BrowserWindow) to have
a previously-set window position, if there is one.
*/
function window_position_prefs(winopts, key)
{
var val;
val = prefs[key+'_x'];
if (val !== undefined)
winopts.x = val;
val = prefs[key+'_y'];
if (val !== undefined)
winopts.y = val;
}
/* Set up a window-options object (for creating a BrowserWindow) to have
a previously-set window size, if there is one. If not, use the
given defaults.
*/
function window_size_prefs(winopts, key, defwidth, defheight)
{
var val;
val = prefs[key+'_width'];
if (val === undefined)
val = defwidth;
winopts.width = val;
val = prefs[key+'_height'];
if (val === undefined)
val = defheight;
winopts.height = val;
}
/* Create a function that sets the preferences for a window position.
This can be set as the window's 'move' callback.
*/
function window_position_prefs_handler(key, win)
{
return function() {
prefs[key+'_x'] = win.getPosition()[0];
prefs[key+'_y'] = win.getPosition()[1];
note_prefs_dirty();
}
}
/* Create a function that sets the preferences for a window size.
This can be set as the window's 'resize' callback.
*/
function window_size_prefs_handler(key, win)
{
return function() {
prefs[key+'_width'] = win.getSize()[0];
prefs[key+'_height'] = win.getSize()[1];
note_prefs_dirty();
}
}
/* Called whenever we update the prefs object. This waits five seconds
(to consolidate writes) and then launches an async file-write.
*/
function note_prefs_dirty()
{
/* If a timer is in flight, we're covered. */
if (prefstimer !== null)
return;
prefstimer = setTimeout(handle_write_prefs, 5000);
}
/* Callback for prefs-dirty timer. */
function handle_write_prefs()
{
prefstimer = null;
/* If prefswriting is true, a writeFile call is in flight. Yes, this
is an annoying corner case. We have new data to write but we have
to wait for the writeFile to finish. We do this by punting for
another five seconds! */
if (prefswriting) {
note_prefs_dirty();
return;
}
prefswriting = true;
var prefsstr = JSON.stringify(prefs);
fs.writeFile(prefspath, prefsstr, { encoding:'utf8' }, function(err) {
prefswriting = false;
});
}
/* Called when the app is shutting down. Write out the prefs if they're dirty.
*/
function write_prefs_now()
{
if (prefstimer !== null) {
clearTimeout(prefstimer);
prefstimer = null;
var prefsstr = JSON.stringify(prefs);
fs.writeFileSync(prefspath, prefsstr, { encoding:'utf8' });
}
}
/* Call one of the functions in apphooks.js (in the game renderer process).
The argument is passed as a JSON string.
(This is fire-and-forget! Beware races. If you want to invoke a bunch
of hooks, use "sequence".)
*/
function invoke_app_hook(win, func, arg)
{
win.webContents.send(func, arg);
}
/* Given a pathname, figure out what kind of game it is. This relies on
the format list in formats.js. It returns a format object from that
file.
Returns null if the game type is not recognized. Throws an exception
if the file is unreadable.
*/
function game_file_discriminate(path)
{
var fd = fs.openSync(path, 'r');
var buf = Buffer.alloc(16);
var len = fs.readSync(fd, buf, 0, 16, 0);
fs.closeSync(fd);
/* Try Blorbs first. */
if (buf[0] == 0x46 && buf[1] == 0x4F && buf[2] == 0x52 && buf[3] == 0x4D
&& buf[8] == 0x49 && buf[9] == 0x46 && buf[10] == 0x52 && buf[11] == 0x53) {
/* Blorb file */
var gametype = parse_blorb(path);
if (gametype == 'GLUL')
return formats.formatmap.glulx;
else if (gametype == 'ZCOD')
return formats.formatmap.zcode;
}
/* Try the format identify functions. */
for (let i = 0; i < formats.formatlist.length; i++) {
var format = formats.formatlist[i];
if (format.identify && format.identify(buf)) {
return format;
}
}
/* Fall back to checking file extensions. */
var pathsuffix = path_mod.extname(path).toLowerCase();
if (pathsuffix.startsWith('.'))
pathsuffix = pathsuffix.slice(1);
for (let i = 0; i < formats.formatlist.length; i++) {
var format = formats.formatlist[i];
if ((!format.extensions) || (!format.engines))
continue;
for (var jx=0; jx<format.extensions.length; jx++) {
if (format.extensions[jx] == pathsuffix)
return format;
}
}
return null;
}
/* Given the pathname of a Blorb file, look through it for a Z-code or
Glulx chunk. Return 'ZCOD' or 'GLUL', or null if neither is found.
*/
function parse_blorb(path)
{
var res = null;
var fd = fs.openSync(path, 'r');
var buf = Buffer.alloc(16);
var len = fs.readSync(fd, buf, 0, 16, 0);
if (!(buf[0] == 0x46 && buf[1] == 0x4F && buf[2] == 0x52 && buf[3] == 0x4D
&& buf[8] == 0x49 && buf[9] == 0x46 && buf[10] == 0x52 && buf[11] == 0x53)) {
/* not Blorb at all. */
fs.closeSync(fd);
return null;
}
/* Search through the chunks for a Zcode/Glulx chunk. */
len = fs.readSync(fd, buf, 0, 12, 0);
var filelen = buf.readUInt32BE(4) + 8;
var pos = 12;
while (pos < filelen) {
len = fs.readSync(fd, buf, 0, 8, pos);
pos += 8;
var chunktype = buf.toString('utf8', 0, 4);
var chunklen = buf.readUInt32BE(4);
if (chunktype == 'ZCOD' || chunktype == 'GLUL') {
res = chunktype;
break;
}
pos += chunklen;
if (pos & 1)
pos++;
}
fs.closeSync(fd);
return res;
}
/* Bring up the select-a-game dialog.
*/
function select_load_game()
{
if (isbound) {
/* We're in bound-game mode and shouldn't be opening any other
games. */
return;
}
if (gamedialog) {
/* The dialog is already up. I'd focus it if I had a way to do that,
but I don't. */
return;
}
var filters = [];
for (var ix=0; ix<formats.formatlist.length; ix++) {
var format = formats.formatlist[ix];
filters.push({ name:format.longname, extensions:format.extensions });
}
if (true) {
/* The file dialog can only show one filter-row at a
time. So we construct one that has a union of the types, and
push it onto the beginning of the filters list. */
var arr = [];
for (var ix=0; ix<filters.length; ix++)
arr = arr.concat(filters[ix].extensions);
filters.unshift({ name: 'All IF Files', extensions: arr });
}
var opts = {
title: 'Select an IF game file',
properties: ['openFile'],
filters: filters,
};
gamedialog = true;
electron.dialog.showOpenDialog(null, opts).then(function(res) {
gamedialog = false;
if (!res || res.canceled)
return;
var ls = res.filePaths;
if (!ls || !ls.length)
return;
launch_game(ls[0]);
});
}
/* Open a game window for a given game file.
*/
function launch_game(path)
{
var kind = null;
/* Figure out what kind of game file this is, so we know which play.html
file to load. */
try {
kind = game_file_discriminate(path);
}
catch (ex) {
electron.dialog.showErrorBox('The game file could not be read.', ''+ex);
return;
}
if (!kind) {
electron.dialog.showErrorBox('Could not recognize game file.', path);
return;
}
if (aboutwin && aboutwin_initial) {
/* Dispose of the (temporary) splash window. This needs a time delay
for some annoying internal reason. */
setTimeout( function() { if (aboutwin) aboutwin.close(); }, 50);
}
add_recent_game(path);
var engine = kind.engines[0];
var enginepref = prefs[kind.id+'_terp'];
if (enginepref) {
var neweng = formats.enginemap[enginepref];
if (neweng && neweng.format == kind.id)
engine = neweng;
}
var win = null;
var game = {
type: 'game',
path: path,
basehtml: engine.html,
engineid: engine.id,
title: null,
signature: null
};
var winopts = {
title: require('electron').app.getName(),
width: prefs.gamewin_width, height: prefs.gamewin_height,
minWidth: 400, minHeight: 400,
backgroundColor: (electron.nativeTheme.shouldUseDarkColors ? '#000' : '#FFF'),
webPreferences: {
spellcheck: false,
nodeIntegration: true,
contextIsolation: false,
enableRemoteModule: false,
zoomFactor: zoom_factor_for_level(prefs.gamewin_zoomlevel)
}
};
/* Note that Electron recommends contextIsolation:true, but the
way we're including interpreter code isn't compatible with
contextIsolation. Shouldn't be a problem since we don't support
arbitrary remote/game content. */
/* backgroundColor should maybe be based on darkmode+theme, not
just the darkmode flag. */
/* The webPreferences.zoomFactor doesn't seem to take effect in
Electron 1.6.11, so we'll send it over via IPC later. */
if (window_icon)
winopts.icon = window_icon;
if (process.platform == 'win32' && !isbound) {
/* On Windows, set the window icon to an appropriate document icon.
(But not in the bound version -- we leave that as the
game's app icon.) */
winopts.icon = path_mod.join(__dirname, kind.docicon);
}
/* BUG: The offsetting only applies if you have a window location
preference. For a brand-new user this will not be true. */
var offset = pick_window_offset(gamewins);
if (prefs.gamewin_x !== undefined)
winopts.x = prefs.gamewin_x + 20 * offset;
if (prefs.gamewin_y !== undefined)
winopts.y = prefs.gamewin_y + 20 * offset;
win = new electron.BrowserWindow(winopts);
if (!win)
return;
game.win = win;
game.id = win.id;
game.offset = offset;
gamewins[game.id] = game;
if (process.platform != 'darwin') {
var template = construct_menu_template('game');
var menu = electron.Menu.buildFromTemplate(template);
win.setMenu(menu);
winmenus[win.id] = menu;
}
if (process.platform == 'darwin' && !isbound) {
/* On Mac, set the window document link to the game file URL.
(But not in the bound version.) */
win.setRepresentedFilename(game.path);
}
/* Game window callbacks */
win.on('closed', function() {
delete gamewins[game.id];
delete winmenus[game.id];
game.win = null;
game = null;
win = null;
/* In the bound version, closing the game window means closing
the app. */
if (isbound)
app.quit();
});
win.on('focus', function() {
window_focus_update(win, game);
});
win.webContents.on('dom-ready', function() {
if (!win) {
return;
}
var game = game_for_webcontents(win.webContents);
if (!game) {
return;
}
var funcs = [];
funcs.push({
key: 'set_zoom_factor',
arg: winopts.webPreferences.zoomFactor });
funcs.push({
key: 'set_margin_level',
arg: prefs.gamewin_marginlevel });
funcs.push({
key: 'set_color_theme',
arg: { theme:prefs.gamewin_colortheme, darklight:electron.nativeTheme.shouldUseDarkColors } });
funcs.push({
key: 'set_font',
arg: { font:prefs.gamewin_font, customfont:prefs.gamewin_customfont } });
if (game.suppress_autorestore) {
funcs.push({ key: 'set_clear_autosave', arg: true });
game.suppress_autorestore = false;
}
funcs.push({
key: 'load_named_game',
arg: {
path: game.path, format: kind.id, engine: game.engineid,
transcriptdir: path_mod.join(app.getPath('userData'), 'transcripts')
} });
invoke_app_hook(win, 'sequence', funcs);
});
win.webContents.on('found-in-page', function(ev, res) {
var game = game_for_window(win);
if (!game)
return;
if (game.foundinpage && game.foundinpage.requestId == res.requestId) {
/* merge fields into game.foundinpage */
Object.assign(game.foundinpage, res);
}
else {
game.foundinpage = res;
}
if (game.foundinpage.finalUpdate && game.foundinpage.activeMatchOrdinal == game.foundinpage.matches && game.foundinpage.matches > 1) {
/* The last match, by definition, is in the one in the search
widget. If we've landed on it, search *again* to jump around
to the beginning (or back, as the case may be). */
var webcontents = game.win.webContents;
var forward = game.searchforward;
webcontents.findInPage(game.last_search, { findNext:true, forward:forward });
}
});
win.on('resize', window_size_prefs_handler('gamewin', win));
win.on('move', function() {
prefs.gamewin_x = win.getPosition()[0];
prefs.gamewin_y = win.getPosition()[1];
note_prefs_dirty();
/* We're starting with a new position, so erase the history of
what windows go here. */
clear_window_offsets(gamewins);
var game = game_for_window(win);
if (game)
game.offset = 0;
});
/* Load the game UI and go. */
win.loadURL('file://' + __dirname + '/' + game.basehtml);
}
/* Reset the game by reloading its HTML document.
*/
function reset_game(game)
{
if (!game.win)
return;
var winopts = {
type: 'question',
message: 'Are you sure you want to reset the game to the beginning? This will discard all your progress since your last SAVE command.',
buttons: ['Reset', 'Cancel'],
cancelId: 1
};
if (window_icon)
winopts.icon = window_icon;
/* We use a synchronous showMessageBox call, which blocks (is modal for)
the entire app. The async call would only block the game window, but
that causes weird results (e.g., cmd-Q fails to shut down the blocked
game window). */
var res = electron.dialog.showMessageBoxSync(game.win, winopts);
if (res == 0) {
var win = game.win;
/* Set a flag to inhibit autorestore (but not autosave). This
will be cleared when the page finishes loading. */
game.suppress_autorestore = true;
/* Load the game UI and go. */
win.loadURL('file://' + __dirname + '/' + game.basehtml);
}
}
/* Open a transcript browser window. (We must not already have one for this
filename.)
*/
function open_transcript_display_window(filename)
{
check_transcript_andthen(
filename,
open_transcript_display_window_next,
(ex) => {
electron.dialog.showErrorBox('The transcript could not be read.', ''+ex);
});
}
/* Callback, invoked when we have all the transcript metadata.
*/
function open_transcript_display_window_next(dat)
{
var filename = dat.filename;
var path = dat.path;
var win = null;
var tra = {
type: 'trashow',
filename: filename,
path: path,
title: dat.title,
timestamps: false,
}
var winopts = {
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
enableRemoteModule: false,
zoomFactor: zoom_factor_for_level(prefs.gamewin_zoomlevel)
},
width: prefs.trashowwin_width, height: prefs.trashowwin_height,
minWidth: 400, minHeight: 400,
backgroundColor: (electron.nativeTheme.shouldUseDarkColors ? '#000' : '#FFF'),
};
if (window_icon)
winopts.icon = window_icon;
var offset = pick_window_offset(trawins);
if (prefs.trashowwin_x !== undefined)
winopts.x = prefs.trashowwin_x + 20 * offset;
if (prefs.trashowwin_y !== undefined)
winopts.y = prefs.trashowwin_y + 20 * offset;
win = new electron.BrowserWindow(winopts);
if (!win)
return;
tra.win = win;
tra.id = win.id;
tra.offset = offset;
trawins[tra.id] = tra;
if (process.platform != 'darwin') {
var template = construct_menu_template('trashow');
var menu = electron.Menu.buildFromTemplate(template);
win.setMenu(menu);
winmenus[win.id] = menu;
}
win.on('closed', function() {
delete trawins[tra.id];
delete winmenus[tra.id];
tra.win = null;
tra = null;
win = null;
});
win.on('focus', function() {
window_focus_update(win, tra);
});
win.webContents.on('found-in-page', function(ev, res) {
var tra = trashow_for_window(win);
if (!tra)
return;
if (tra.foundinpage && tra.foundinpage.requestId == res.requestId) {
/* merge fields into tra.foundinpage */
Object.assign(tra.foundinpage, res);
}
else {
tra.foundinpage = res;
}
if (tra.foundinpage.finalUpdate && tra.foundinpage.activeMatchOrdinal == tra.foundinpage.matches && tra.foundinpage.matches > 1) {
/* The last match, by definition, is in the one in the search
widget. If we've landed on it, search *again* to jump around
to the beginning (or back, as the case may be). */
var webcontents = tra.win.webContents;
var forward = tra.searchforward;
webcontents.findInPage(tra.last_search, { findNext:true, forward:forward });
}
});
win.on('resize', window_size_prefs_handler('trashowwin', win));
win.on('move', function() {
prefs.trashowwin_x = win.getPosition()[0];
prefs.trashowwin_y = win.getPosition()[1];
note_prefs_dirty();
/* We're starting with a new position, so erase the history of
what windows go here. */
clear_window_offsets(trawins);
var tra = trashow_for_window(win);
if (tra)
tra.offset = 0;
});
win.webContents.on('dom-ready', function() {
if (!win) {
return;
}
var tra = trashow_for_webcontents(win.webContents);
if (!tra) {
return;
}
var funcs = [];
funcs.push({
key: 'set_zoom_factor',
arg: winopts.webPreferences.zoomFactor });
funcs.push({
key: 'set_margin_level',
arg: prefs.gamewin_marginlevel });
funcs.push({
key: 'set_color_theme',
arg: { theme:prefs.gamewin_colortheme, darklight:electron.nativeTheme.shouldUseDarkColors } });
funcs.push({
key: 'set_font',
arg: { font:prefs.gamewin_font, customfont:prefs.gamewin_customfont } });
funcs.push({
key: 'load_transcript',
arg: {
path: tra.path, filename: tra.filename,
title: tra.title
} });
invoke_app_hook(win, 'sequence', funcs);
});
win.loadURL('file://' + __dirname + '/trashow.html');
}
/* Open the "About Lectrote" window. (It must not already exist.)
*/
function open_about_window()
{
var winopts = {
webPreferences: { nodeIntegration: true, contextIsolation: false, enableRemoteModule: false },
width: 650, height: 450,
backgroundColor: (electron.nativeTheme.shouldUseDarkColors ? '#000' : '#FFF'),
useContentSize: true,
resizable: false
};
if (main_extension.about_window_size) {
if (main_extension.about_window_size.width)
winopts.width = main_extension.about_window_size.width;
if (main_extension.about_window_size.height)
winopts.height = main_extension.about_window_size.height;
}
window_position_prefs(winopts, 'aboutwin');
if (window_icon)
winopts.icon = window_icon;
aboutwin = new electron.BrowserWindow(winopts);
if (process.platform != 'darwin') {
var template = construct_menu_template('about');
var menu = electron.Menu.buildFromTemplate(template);
aboutwin.setMenu(menu);
winmenus[aboutwin.id] = menu;
}
aboutwin.on('closed', function() {
delete winmenus[aboutwin.id];