Skip to content

Commit

Permalink
improve drawing
Browse files Browse the repository at this point in the history
  • Loading branch information
Im-Beast committed Nov 27, 2021
1 parent 8c3ed4c commit 6a56518
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 30 deletions.
38 changes: 8 additions & 30 deletions src/tui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ export interface TuiInstance {
readonly once: TuiInstance["emitter"]["once"];
readonly draw: () => void;
readonly stopDrawing: () => void;
readonly restartDrawing: () => void;
rectangle: () => TuiRectangle;
children: AnyComponent[];
components: AnyComponent[];
Expand Down Expand Up @@ -79,6 +78,7 @@ export function createTui(
): TuiInstance {
const emitter = createEventEmitter() as TuiInstance["emitter"];

let timeoutHandle: number | undefined;
const tui: TuiInstance = {
id: instanceId++,
emitter,
Expand All @@ -93,10 +93,6 @@ export function createTui(
styler,
});

tui.components = tui.components.sort((b, a) =>
b.drawPriority - a.drawPriority
);

for (const component of tui.components) {
component.draw();
}
Expand All @@ -116,12 +112,14 @@ export function createTui(
render(tui.canvas);

tui.selected.active = false;

timeoutHandle = setTimeout(
tui.draw,
getStaticValue(refreshRate) - tui.canvas.deltaTime,
);
},
stopDrawing() {
clearInterval(intervalId);
},
restartDrawing() {
restartInterval();
clearTimeout(timeoutHandle);
},
rectangle() {
const { columns: width, rows: height } = getStaticValue(tui.size);
Expand All @@ -144,26 +142,6 @@ export function createTui(
writer,
};

let intervalId: number;
const restartInterval = () => {
if (intervalId) clearInterval(intervalId);
const dynamicRR = typeof refreshRate === "function";
let lastRR: number;
setInterval(
dynamicRR
? () => {
tui.draw();
const rr = refreshRate();
if (lastRR && lastRR !== rr) {
restartInterval();
}
}
: tui.draw,
getStaticValue(refreshRate),
);
};

restartInterval();

tui.draw();
return tui;
}
4 changes: 4 additions & 0 deletions src/tui_component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,10 @@ export function createComponent<
instance.components.push(component);
object.children.push(component);

instance.components = instance.components.sort((b, a) =>
b.drawPriority - a.drawPriority
);

return component as Extension extends void
? TuiComponent<Name, Events, DataTypes>
: ExtendedTuiComponent<Name, Extension, Events, DataTypes>;
Expand Down

0 comments on commit 6a56518

Please sign in to comment.