HomeContext new ui system

This commit is contained in:
Djeeberjr 2023-02-05 02:56:18 +01:00
parent a4e4e103bd
commit b0be0ba8b4

View File

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