From 2cbcc86dce2632f10dcf70d4578463c9b9a7664e Mon Sep 17 00:00:00 2001 From: Djeeberjr Date: Sat, 28 Jan 2023 03:56:07 +0100 Subject: [PATCH] added main term (may rename later) --- src/lib/MainTerm.hx | 101 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 src/lib/MainTerm.hx diff --git a/src/lib/MainTerm.hx b/src/lib/MainTerm.hx new file mode 100644 index 0000000..7076d4a --- /dev/null +++ b/src/lib/MainTerm.hx @@ -0,0 +1,101 @@ +package lib; + +import kernel.KernelEvents; +import bin.Terminal; +import kernel.ui.WindowManager; +import kernel.ui.WindowContext; + +class MainTerm { + private static final MAX_CONTEXT:Int = 10; + + private var ctx: WindowContext = null; + private final workspaces: Map = []; + private var currentWorkspace: Int = -1; + + public function new() { + + } + + public function run() { + // Create main terminal context + ctx = WindowManager.instance.createNewContext(); + WindowManager.instance.focusContextToOutput(ctx,"main"); + + // Setup renderer and event handlers for main terminal + render(); + + ctx.onClick.handle(e -> { + if (e.button != Left) return; + var clickedContext = e.pos.y; + + if (workspaces[clickedContext] != null) { + focusContext(clickedContext); + }else { + var newContext = WindowManager.instance.createNewContext(); + + // Create new terminal + var term = new Terminal(); + term.invoke(newContext); + + addContextToWorkspace(newContext,clickedContext); + focusContext(clickedContext); + } + }); + + // Register global key bindings to react to main terminal + KernelEvents.instance.onKey.handle(e -> { + // Is HOME pressed? + // TODO: remove magic number + if (e.keyCode == 268){ + focusMainTerm(); + } + }); + } + + /** + Adds a context to the list of contexts to manage. Returns the context ID. Returns -1 if the context could not be added. + **/ + private function addContextNextWorkspace(context: WindowContext): Int { + for (i in 0...MAX_CONTEXT) { + if (!workspaces.exists(i)) { + workspaces[i] = context; + return i; + } + } + + return -1; + } + + private function addContextToWorkspace(context: WindowContext, id: Int) { + if (!workspaces.exists(id)) { + workspaces[id] = context; + } + } + + private function focusContext(id: Int) { + if (workspaces.exists(id)) { + WindowManager.instance.focusContextToOutput(workspaces[id],"main"); + currentWorkspace = id; + } + } + + private function focusMainTerm() { + render(); + WindowManager.instance.focusContextToOutput(ctx,"main"); + currentWorkspace = -1; + } + + private function render() { + ctx.clear(); + ctx.setCursorPos(0,0); + + for(i in 0...MAX_CONTEXT){ + ctx.setCursorPos(0,i); + if (workspaces.exists(i)) { + ctx.write("Switch to context " + i); + }else{ + ctx.write("Create new Terminal on context " + i); + } + } + } +}