From 8934d40c0bab4066aec8d3a38911656776f37284 Mon Sep 17 00:00:00 2001 From: Djeeberjr Date: Mon, 30 Jan 2023 16:44:52 +0100 Subject: [PATCH] use new event delegate in HomeContext --- src/lib/HomeContext.hx | 21 ++++++++++----------- src/lib/ui/rendere/List.hx | 22 ++++++++++++++++++++-- 2 files changed, 30 insertions(+), 13 deletions(-) diff --git a/src/lib/HomeContext.hx b/src/lib/HomeContext.hx index ee81d26..cd6e1b0 100644 --- a/src/lib/HomeContext.hx +++ b/src/lib/HomeContext.hx @@ -17,6 +17,8 @@ class HomeContext { private final workspaces: Map = []; private var currentWorkspace: Int = -1; + private var listRenderer: List; + public function new() { } @@ -27,14 +29,9 @@ class HomeContext { 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); + listRenderer = new List((i)->{ + if (workspaces[i] != null) { + focusContext(i); }else { var newContext = WindowManager.instance.createNewContext(); @@ -42,11 +39,14 @@ class HomeContext { var term = new Terminal(); term.invoke(newContext); - addContextToWorkspace(newContext,clickedContext); - focusContext(clickedContext); + addContextToWorkspace(newContext,i); + focusContext(i); } }); + ctx.delegateEvents(listRenderer); + render(); + // Register global key bindings to react to main terminal KernelEvents.instance.onKey.handle(e -> { // Is HOME pressed? @@ -95,7 +95,6 @@ class HomeContext { 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 listRenderer = new List(); listRenderer.render(list).renderToContext(ctx); } diff --git a/src/lib/ui/rendere/List.hx b/src/lib/ui/rendere/List.hx index f0d15be..0b77af0 100644 --- a/src/lib/ui/rendere/List.hx +++ b/src/lib/ui/rendere/List.hx @@ -1,7 +1,11 @@ package lib.ui.rendere; -class List { - public function new() {} +class List implements UIEventDelegate{ + private final onElementClick: NullVoid>; + + public function new(?onElementClick: Int->Void) { + this.onElementClick = onElementClick; + } public function render(list:Array): Canvas { var canvas = new Canvas(); @@ -17,4 +21,18 @@ class List { } return canvas; } + + public function getEventHandlers(): UIEvents{ + return { + onClick: handleClick + }; + } + + private function handleClick(e: {button:kernel.ButtonType, pos:Pos}): Void{ + if (this.onElementClick == null){ + return; + } + + this.onElementClick(e.pos.y); + } }