Letrec. index in stdlib. fixing NaNs when measuring things without gaps, w or h. more window management stuff in OS
This commit is contained in:
parent
db4abfa450
commit
5f592f40bf
5 changed files with 89 additions and 15 deletions
22
src/ui.ts
22
src/ui.ts
|
|
@ -88,8 +88,10 @@ function renderUI(ui: UIValue, ctx: CanvasRenderingContext2D, x: number, y: numb
|
|||
case 'row': {
|
||||
let offsetX = 0;
|
||||
for (const child of ui.children) {
|
||||
const size = measure(child);
|
||||
console.log("row child: ", child.kind, "width: ", size.width)
|
||||
renderUI(child, ctx, x + offsetX, y);
|
||||
offsetX += measure(child).width + ui.gap;
|
||||
offsetX += measure(child).width + (ui.gap || 0);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
|
@ -98,7 +100,7 @@ function renderUI(ui: UIValue, ctx: CanvasRenderingContext2D, x: number, y: numb
|
|||
let offsetY = 0;
|
||||
for (const child of ui.children) {
|
||||
renderUI(child, ctx, x, y + offsetY);
|
||||
offsetY += measure(child).height + ui.gap;
|
||||
offsetY += measure(child).height + (ui.gap || 0);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
|
@ -146,8 +148,16 @@ function renderUI(ui: UIValue, ctx: CanvasRenderingContext2D, x: number, y: numb
|
|||
}
|
||||
|
||||
export function measure(ui: UIValue): { width: number, height: number } {
|
||||
const result = _measure(ui);
|
||||
if (isNaN(result.width) || isNaN(result.height)) {
|
||||
console.warn('NaN from:', ui.kind, ui);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
export function _measure(ui: UIValue): { width: number, height: number } {
|
||||
switch (ui.kind) {
|
||||
case 'rect': return { width: ui.w, height: ui.h };
|
||||
case 'rect': return { width: ui.w || 0, height: ui.h || 0 };
|
||||
|
||||
case 'text': return { width: ui.content.length * 10, height: 20 }; // TODO
|
||||
|
||||
|
|
@ -159,7 +169,7 @@ export function measure(ui: UIValue): { width: number, height: number } {
|
|||
totalWidth += size.width;
|
||||
maxHeight = Math.max(maxHeight, size.height);
|
||||
}
|
||||
totalWidth += ui.gap * (ui.children.length - 1);
|
||||
totalWidth += (ui.gap || 0) * (ui.children.length - 1);
|
||||
return { width: totalWidth, height: maxHeight };
|
||||
}
|
||||
|
||||
|
|
@ -171,7 +181,7 @@ export function measure(ui: UIValue): { width: number, height: number } {
|
|||
totalHeight += size.height;
|
||||
maxWidth = Math.max(maxWidth, size.width);
|
||||
}
|
||||
totalHeight += ui.gap * (ui.children.length - 1);
|
||||
totalHeight += (ui.gap || 0) * (ui.children.length - 1);
|
||||
return { width: maxWidth, height: totalHeight };
|
||||
}
|
||||
|
||||
|
|
@ -209,6 +219,8 @@ export function measure(ui: UIValue): { width: number, height: number } {
|
|||
|
||||
case 'stateful':
|
||||
throw new Error('Stateful components cannot be measured');
|
||||
|
||||
default: return { width: 0, height: 0 };
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue