diff --git a/src/lib/HomeContext.hx b/src/lib/HomeContext.hx index cd6e1b0..fcdcbc5 100644 --- a/src/lib/HomeContext.hx +++ b/src/lib/HomeContext.hx @@ -1,6 +1,8 @@ package lib; -import lib.ui.rendere.List; +import lib.ui.elements.UIElement; +import lib.ui.elements.TextElement; +import lib.ui.elements.RootElement; import kernel.KernelEvents; import bin.Terminal; import kernel.ui.WindowManager; @@ -11,47 +13,31 @@ import kernel.ui.WindowContext; Listens to global hotkey "POS1"/"HOME" to switch back to this context. **/ class HomeContext { - private static final MAX_CONTEXT:Int = 10; + private static inline final MAX_CONTEXT:Int = 10; - private var ctx: WindowContext = null; - private final workspaces: Map = []; - private var currentWorkspace: Int = -1; + private var ctx:WindowContext = null; + private final workspaces:Map = []; + private var currentWorkspace:Int = -1; - private var listRenderer: List; + private var renderer:RootElement; - public function new() { - - } + public function new() {} public function run() { // Create main terminal context ctx = WindowManager.instance.createNewContext(); - WindowManager.instance.focusContextToOutput(ctx,"main"); + WindowManager.instance.focusContextToOutput(ctx, "main"); - // Setup renderer and event handlers for main terminal - listRenderer = new List((i)->{ - if (workspaces[i] != null) { - focusContext(i); - }else { - var newContext = WindowManager.instance.createNewContext(); + renderer = new RootElement(); - // Create new terminal - var term = new Terminal(); - term.invoke(newContext); - - addContextToWorkspace(newContext,i); - focusContext(i); - } - }); - - ctx.delegateEvents(listRenderer); + ctx.delegateEvents(renderer); render(); // 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){ + if (e.keyCode == 268) { focusMainTerm(); } }); @@ -60,7 +46,7 @@ class HomeContext { /** 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 { + private function addContextNextWorkspace(context:WindowContext):Int { for (i in 0...MAX_CONTEXT) { if (!workspaces.exists(i)) { workspaces[i] = context; @@ -71,31 +57,53 @@ class HomeContext { return -1; } - private function addContextToWorkspace(context: WindowContext, id: Int) { + private function addContextToWorkspace(context:WindowContext, id:Int) { if (!workspaces.exists(id)) { workspaces[id] = context; } } - private function focusContext(id: Int) { + private function focusContext(id:Int) { if (workspaces.exists(id)) { - WindowManager.instance.focusContextToOutput(workspaces[id],"main"); + WindowManager.instance.focusContextToOutput(workspaces[id], "main"); currentWorkspace = id; } } private function focusMainTerm() { render(); - WindowManager.instance.focusContextToOutput(ctx,"main"); + WindowManager.instance.focusContextToOutput(ctx, "main"); currentWorkspace = -1; } + private function handleSelectContext(contextId:Int) { + if (workspaces[contextId] != null) { + focusContext(contextId); + } else { + var newContext = WindowManager.instance.createNewContext(); + + // Create new terminal + var term = new Terminal(); + term.invoke(newContext); + + addContextToWorkspace(newContext, contextId); + focusContext(contextId); + } + } + private function render() { ctx.clear(); - ctx.setCursorPos(0,0); - var list = [for (i in 0...MAX_CONTEXT) workspaces.exists(i) ? 'Switch to context ${i}' : 'Create new Terminal on context ${i}']; + var list = [ + for (i in 0...MAX_CONTEXT) workspaces.exists(i) ? 'Switch to context ${i}' : 'Create new Terminal on context ${i}' + ]; - listRenderer.render(list).renderToContext(ctx); + var children:Array = [ + for (i => line in list) new TextElement(line, {onClick: this.handleSelectContext.bind(i)}) + ]; + + renderer.setChildren(children); + + renderer.render().renderToContext(ctx); } }