added statelessVirtualTermWriter
- changed how WindowManager handles new contexts
This commit is contained in:
159
src/kernel/ui/StatelessVirtualTermWriter.hx
Normal file
159
src/kernel/ui/StatelessVirtualTermWriter.hx
Normal file
@@ -0,0 +1,159 @@
|
||||
package kernel.ui;
|
||||
|
||||
import kernel.log.Log;
|
||||
import lib.Pos;
|
||||
import lib.Vec.Vec2;
|
||||
import lib.Color;
|
||||
|
||||
using tink.CoreApi;
|
||||
|
||||
/**
|
||||
Normal TermWriteable but without storing the the state in a buffer wehen disabled.
|
||||
When enabled or the screen size changes the render function is called.
|
||||
The render function is only called when needed.
|
||||
You can also request a re-render by calling `requestRender`.
|
||||
**/
|
||||
class StatelessVirtualTermWriter implements VirtualTermWriter {
|
||||
public var onResize(default, null):Signal<Vec2<Int>>;
|
||||
|
||||
private var onResizeTrigger: SignalTrigger<Vec2<Int>> = Signal.trigger();
|
||||
private var target:TermWriteable;
|
||||
private var enabled:Bool = false;
|
||||
private var renderFunc:Null<Void->Void> = null;
|
||||
private var renderRequested:Bool = false;
|
||||
private var onResizeLink: CallbackLink;
|
||||
|
||||
@:allow(kernel.ui)
|
||||
private function new(?target:TermWriteable) {
|
||||
onResize = onResizeTrigger.asSignal();
|
||||
setTarget(target);
|
||||
}
|
||||
|
||||
public function setRenderFunc(func: (Void->Void)) {
|
||||
this.renderFunc = func;
|
||||
}
|
||||
|
||||
public function requestRender() {
|
||||
if (renderFunc == null) {
|
||||
Log.warn("Render requested, but no render function set");
|
||||
return;
|
||||
}
|
||||
|
||||
if (enabled) {
|
||||
renderFunc();
|
||||
renderRequested = false;
|
||||
}else{
|
||||
renderRequested = true;
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// VirtualTermWriter implementation
|
||||
//
|
||||
|
||||
public function enable() {
|
||||
if (target == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
enabled = true;
|
||||
|
||||
if (renderFunc != null){
|
||||
renderFunc();
|
||||
}
|
||||
|
||||
renderRequested = false;
|
||||
}
|
||||
|
||||
public function disable() {
|
||||
enabled = false;
|
||||
}
|
||||
|
||||
public function setTarget(newTarget:TermWriteable) {
|
||||
if (newTarget == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (target != null) {
|
||||
onResizeLink.cancel();
|
||||
}
|
||||
|
||||
onResizeLink = newTarget.onResize.handle((size) -> {
|
||||
requestRender();
|
||||
onResizeTrigger.trigger(size);
|
||||
});
|
||||
|
||||
target = newTarget;
|
||||
}
|
||||
|
||||
public function isEnabled():Bool {
|
||||
return enabled;
|
||||
}
|
||||
|
||||
//
|
||||
// TermWriteable implementation
|
||||
//
|
||||
// Some functions return defualt values if the target is not set. This should not be an issue since
|
||||
// the render func is only called when the target is set.
|
||||
//
|
||||
|
||||
public inline function write(text:String) {
|
||||
if (enabled) target.write(text);
|
||||
}
|
||||
|
||||
public inline function scroll(y:Int) {
|
||||
if (enabled) target.scroll(y);
|
||||
}
|
||||
|
||||
public inline function getCursorPos():Pos {
|
||||
return enabled ? target.getCursorPos() : new Pos({x: 0, y: 0});
|
||||
}
|
||||
|
||||
public inline function setCursorPos(x:Int, y:Int) {
|
||||
if (enabled) target.setCursorPos(x, y);
|
||||
}
|
||||
|
||||
public inline function getCursorBlink():Bool {
|
||||
return enabled ? target.getCursorBlink() : false;
|
||||
}
|
||||
|
||||
public inline function setCursorBlink(blink:Bool) {
|
||||
if (enabled) target.setCursorBlink(blink);
|
||||
}
|
||||
|
||||
public inline function getSize():Vec2<Int> {
|
||||
return enabled ? target.getSize() : {x: 0, y: 0};
|
||||
}
|
||||
|
||||
public inline function clear() {
|
||||
if (enabled) target.clear();
|
||||
}
|
||||
|
||||
public inline function clearLine() {
|
||||
if (enabled) target.clearLine();
|
||||
}
|
||||
|
||||
public inline function getTextColor():Color {
|
||||
return enabled ? target.getTextColor() : Color.White;
|
||||
}
|
||||
|
||||
public inline function setTextColor(color:Color) {
|
||||
if (enabled) target.setTextColor(color);
|
||||
}
|
||||
|
||||
public inline function getBackgroundColor():Color {
|
||||
return enabled ? target.getBackgroundColor() : Color.Black;
|
||||
}
|
||||
|
||||
public inline function setBackgroundColor(color:Color) {
|
||||
if (enabled) target.setBackgroundColor(color);
|
||||
}
|
||||
|
||||
public inline function isColor():Bool {
|
||||
return enabled ? target.isColor() : false;
|
||||
}
|
||||
|
||||
public inline function reset() {
|
||||
if (enabled) target.reset();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user