use new event delegate in HomeContext

This commit is contained in:
Djeeberjr 2023-01-30 16:44:52 +01:00
parent 8f7739c26a
commit 8934d40c0b
2 changed files with 30 additions and 13 deletions

View File

@ -17,6 +17,8 @@ class HomeContext {
private final workspaces: Map<Int,WindowContext> = []; private final workspaces: Map<Int,WindowContext> = [];
private var currentWorkspace: Int = -1; private var currentWorkspace: Int = -1;
private var listRenderer: List;
public function new() { public function new() {
} }
@ -27,14 +29,9 @@ class HomeContext {
WindowManager.instance.focusContextToOutput(ctx,"main"); WindowManager.instance.focusContextToOutput(ctx,"main");
// Setup renderer and event handlers for main terminal // Setup renderer and event handlers for main terminal
render(); listRenderer = new List((i)->{
if (workspaces[i] != null) {
ctx.onClick.handle(e -> { focusContext(i);
if (e.button != Left) return;
var clickedContext = e.pos.y;
if (workspaces[clickedContext] != null) {
focusContext(clickedContext);
}else { }else {
var newContext = WindowManager.instance.createNewContext(); var newContext = WindowManager.instance.createNewContext();
@ -42,11 +39,14 @@ class HomeContext {
var term = new Terminal(); var term = new Terminal();
term.invoke(newContext); term.invoke(newContext);
addContextToWorkspace(newContext,clickedContext); addContextToWorkspace(newContext,i);
focusContext(clickedContext); focusContext(i);
} }
}); });
ctx.delegateEvents(listRenderer);
render();
// Register global key bindings to react to main terminal // Register global key bindings to react to main terminal
KernelEvents.instance.onKey.handle(e -> { KernelEvents.instance.onKey.handle(e -> {
// Is HOME pressed? // Is HOME pressed?
@ -95,7 +95,6 @@ class HomeContext {
ctx.setCursorPos(0,0); 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}'];
var listRenderer = new List();
listRenderer.render(list).renderToContext(ctx); listRenderer.render(list).renderToContext(ctx);
} }

View File

@ -1,7 +1,11 @@
package lib.ui.rendere; package lib.ui.rendere;
class List { class List implements UIEventDelegate{
public function new() {} private final onElementClick: Null<Int->Void>;
public function new(?onElementClick: Int->Void) {
this.onElementClick = onElementClick;
}
public function render(list:Array<String>): Canvas { public function render(list:Array<String>): Canvas {
var canvas = new Canvas(); var canvas = new Canvas();
@ -17,4 +21,18 @@ class List {
} }
return canvas; 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);
}
} }