Skip to content

Commit a029afe

Browse files
window manager pattern
1 parent 91daaa0 commit a029afe

14 files changed

Lines changed: 362 additions & 261 deletions

File tree

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ollieos",
3-
"version": "1.0.0",
3+
"version": "1.0.1",
44
"description": "",
55
"private": true,
66
"main": "server.js",

src/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { LocalStorageFS } from "./fs_impl/localstorage";
1515
import { initial_fs_setup } from "./initial_fs_setup";
1616

1717
import Swal from "sweetalert2";
18+
import {DOMWindowManager} from "./window_impl/dom";
1819

1920

2021
const boot_screen = document.getElementById("boot_screen");
@@ -138,12 +139,14 @@ async function main() {
138139
// create initial files
139140
await initial_fs_setup(fs);
140141

142+
// create a dom window manager
143+
const wm = new DOMWindowManager();
141144

142145
// create a terminal using the registry and filesystem
143146
const term = new WrappedTerminal(fs, prog_reg, sfx_reg,{
144147
screenReaderMode: false,
145148
cursorBlink: true,
146-
});
149+
}, true, wm);
147150

148151
term.set_variable("VERSION", document.body.dataset.version);
149152
term.set_variable("ENV", "web");

src/programs/fsedit.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import type { Program } from "../types";
22
import { ANSI } from "../term_ctl";
3-
import {VirtualWindow} from "../windowing";
43

54
export default {
65
name: "fsedit",
@@ -43,16 +42,21 @@ export default {
4342
// url encode the directory
4443
const encoded_dir = encodeURIComponent(dir);
4544

46-
// open fsedit in a popup window
47-
//window.open(`./fsedit?type=${fs_name}&dir=${encoded_dir}`, "_blank", "popup=true")
48-
4945
const iframe = document.createElement("iframe");
5046
iframe.src = `./fsedit?type=${fs_name}&dir=${encoded_dir}`;
5147
iframe.style.border = "none";
5248
iframe.style.width = "100%";
5349
iframe.style.height = "100%";
5450

55-
const wind = new VirtualWindow();
51+
const wm = term.get_window_manager();
52+
if (!wm) {
53+
// fallback to opening in a popup window
54+
window.open(`./fsedit?type=${fs_name}&dir=${encoded_dir}`, "_blank", "popup=true");
55+
term.writeln("Opened fsedit in a new popup window.");
56+
return 0;
57+
}
58+
59+
const wind = new wm.Window();
5660
wind.title = "fsedit";
5761

5862
wind.width = "75vw";

src/programs/window/close.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import {ANSI} from "../../term_ctl";
22
import { ProgramMainData } from "../../types"
3-
import {get_window_by_id} from "../../windowing";
43

54
// extract from ANSI to make code less verbose
65
const { STYLE, FG, PREFABS } = ANSI;
@@ -25,7 +24,8 @@ export const close_subcommand = async (data: ProgramMainData) => {
2524
return 1;
2625
}
2726

28-
const wind = get_window_by_id(window_id);
27+
const wm = term.get_window_manager();
28+
const wind = wm!.get_window_by_id(window_id);
2929

3030
if (!wind) {
3131
term.writeln(`${PREFABS.error}No window found with ID '${window_id}'.`)

src/programs/window/hide.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import {ANSI} from "../../term_ctl";
22
import { ProgramMainData } from "../../types"
3-
import {get_window_by_id} from "../../windowing";
43

54
// extract from ANSI to make code less verbose
65
const { STYLE, FG, PREFABS } = ANSI;
@@ -25,7 +24,8 @@ export const hide_subcommand = async (data: ProgramMainData) => {
2524
return 1;
2625
}
2726

28-
const wind = get_window_by_id(window_id);
27+
const wm = term.get_window_manager();
28+
const wind = wm!.get_window_by_id(window_id);
2929

3030
if (!wind) {
3131
term.writeln(`${PREFABS.error}No window found with ID '${window_id}'.`)

src/programs/window/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ export default {
4646
return 0;
4747
}
4848

49+
if (!term.get_window_manager()) {
50+
term.writeln(`${PREFABS.error}No window manager found.${STYLE.reset_all}`);
51+
return 1;
52+
}
53+
4954
switch (args[0]) {
5055
case "list":
5156
return await list_subcommand(data);

src/programs/window/list.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import {ANSI, NEWLINE} from "../../term_ctl";
22
import { ProgramMainData } from "../../types"
3-
import {get_all_windows} from "../../windowing";
43

54
// extract from ANSI to make code less verbose
65
const { STYLE, FG } = ANSI;
@@ -24,7 +23,8 @@ export const list_subcommand = async (data: ProgramMainData) => {
2423

2524
term.write(NEWLINE);
2625

27-
const all_windows = get_all_windows();
26+
const wm = term.get_window_manager();
27+
const all_windows = wm!.get_all_windows();
2828
for (const win of all_windows) {
2929
if (only_visible && !win.visible) {
3030
continue;

src/programs/window/show.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import {ANSI} from "../../term_ctl";
22
import { ProgramMainData } from "../../types"
3-
import {get_window_by_id} from "../../windowing";
43

54
// extract from ANSI to make code less verbose
65
const { STYLE, FG, PREFABS } = ANSI;
@@ -25,7 +24,8 @@ export const show_subcommand = async (data: ProgramMainData) => {
2524
return 1;
2625
}
2726

28-
const wind = get_window_by_id(window_id);
27+
const wm = term.get_window_manager();
28+
const wind = wm!.get_window_by_id(window_id);
2929

3030
if (!wind) {
3131
term.writeln(`${PREFABS.error}No window found with ID '${window_id}'.`)

src/term_ctl.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ import { IDisposable, ITerminalOptions, Terminal } from "@xterm/xterm";
33
import { ProgramRegistry, recurse_mount_and_register_with_output } from "./prog_registry";
44
import type { AbstractFileSystem } from "./filesystem";
55

6-
import type { KeyEvent, KeyEventHandler, RegisteredKeyEventIdentifier, Program } from "./types";
6+
import type { KeyEvent, KeyEventHandler, RegisteredKeyEventIdentifier } from "./types";
77
import { register_builtin_key_handlers, change_prompt as change_prompt, register_builtin_fs_handlers } from "./event_handlers";
88
import { SoundRegistry } from "./sfx_registry";
9+
import {AbstractWindowManager} from "./windowing";
910

1011
export const NEWLINE = "\r\n";
1112
/* eslint-disable-next-line no-control-regex, no-misleading-character-class */
@@ -106,6 +107,7 @@ export class WrappedTerminal extends Terminal {
106107
_prog_registry: ProgramRegistry;
107108
_sfx_registry: SoundRegistry;
108109
_fs: AbstractFileSystem;
110+
_wm: AbstractWindowManager | null = null;
109111

110112
_key_handlers: Map<RegisteredKeyEventIdentifier, { handler: KeyEventHandler, block: boolean }[]> = new Map();
111113
_on_printable_handlers: KeyEventHandler[] = [];
@@ -127,6 +129,9 @@ export class WrappedTerminal extends Terminal {
127129
return this._fs;
128130
}
129131

132+
get_window_manager(): AbstractWindowManager | null {
133+
return this._wm;
134+
}
130135

131136
get_variable(name: string): string | undefined {
132137
return this._vars.get(name);
@@ -674,12 +679,13 @@ export class WrappedTerminal extends Terminal {
674679
}
675680

676681
// be sure to call initialise after this
677-
constructor(fs: AbstractFileSystem, prog_registry?: ProgramRegistry, sound_registry?: SoundRegistry, xterm_opts?: ITerminalOptions, register_builtin_handlers = true) {
682+
constructor(fs: AbstractFileSystem, prog_registry?: ProgramRegistry, sound_registry?: SoundRegistry, xterm_opts?: ITerminalOptions, register_builtin_handlers = true, wm?: AbstractWindowManager) {
678683
super(xterm_opts);
679684

680685
this._fs = fs;
681686
this._prog_registry = prog_registry || new ProgramRegistry();
682687
this._sfx_registry = sound_registry || new SoundRegistry();
688+
this._wm = wm || null;
683689

684690
if (register_builtin_handlers) {
685691
register_builtin_key_handlers(this);

0 commit comments

Comments
 (0)