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 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);
}

View File

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