Adding measure to new compile code. Cleaning up debug

master
Dustin Swan 3 weeks ago
parent 2cd5a609bb
commit cc9f5959d7
Signed by: dustinswan
GPG Key ID: 30D46587E2100467

@ -99,7 +99,7 @@ function sanitize(name: string): string {
if (ops[name]) return ops[name]; if (ops[name]) return ops[name];
const natives = ['measureText', 'storeSearch', 'debug', 'len', 'slice', 'str']; const natives = ['measure', 'measureText', 'storeSearch', 'debug', 'len', 'slice', 'str'];
if (natives.includes(name)) return `_rt.${name}`; if (natives.includes(name)) return `_rt.${name}`;
const reserved = [ const reserved = [
@ -214,23 +214,12 @@ export function compileAndRun(defs: Definition[]) {
} }
} }
/*
const compiledDefs = defs.map(def =>
`const ${sanitize(def.name)} = ${compile(def.body)};`
).join('\n');
*/
const lastName = defs[defs.length - 1].name; const lastName = defs[defs.length - 1].name;
const defNames = defs.map(d => sanitize(d.name)).join(', '); const defNames = defs.map(d => sanitize(d.name)).join(', ');
const code = `${compiledDefs.join('\n')} const code = `${compiledDefs.join('\n')}
return { ${defNames}, __result: ${sanitize(lastName)} };`; return { ${defNames}, __result: ${sanitize(lastName)} };`;
// console.log('--- Compiled Code ---');
// console.log(code);
// console.log('=====================');
const fn = new Function('_rt', code); const fn = new Function('_rt', code);
const allDefs = fn(_rt); const allDefs = fn(_rt);

@ -20,13 +20,13 @@ listRow = config \
child = Stack { child = Stack {
children = [ children = [
Rect { w = config.w, h = config.h, color = color }, Rect { w = config.w, h = config.h, color = color },
# centerV config.h ( centerV config.h (
Positioned { Positioned {
x = 10, x = 10,
y = 10, y = 10,
child = Text { content = config.child, color = "white" } child = Text { content = config.child, color = "white" }
} }
# ) )
] ]
} }
}; };
@ -34,7 +34,7 @@ listRow = config \
palette = state viewport \ palette = state viewport \
results = take 10 (storeSearch osState.palette.query); results = take 10 (storeSearch osState.palette.query);
_ = debug "palette results" results; _ = debug "viewport" viewport;
state = osState.palette; state = osState.palette;

@ -74,8 +74,6 @@ export function runAppCompiled(app: App, canvas: HTMLCanvasElement, rt: any) {
let instance = componentInstances.get(fullKey); let instance = componentInstances.get(fullKey);
if (!instance) { if (!instance) {
console.log('Creating stateful', fullKey);
console.log('ui.init:', ui.init);
instance = { instance = {
state: ui.init, state: ui.init,
update: ui.update, update: ui.update,
@ -87,7 +85,6 @@ export function runAppCompiled(app: App, canvas: HTMLCanvasElement, rt: any) {
instance.update = ui.update; instance.update = ui.update;
instance.view = ui.view; instance.view = ui.view;
} }
console.log('Instance state', instance.state);
const viewResult = instance.view(instance.state); const viewResult = instance.view(instance.state);
let viewUI = valueToUI(viewResult); let viewUI = valueToUI(viewResult);

@ -33,7 +33,6 @@ export const _rt = {
storeSearch: (query: string) => { storeSearch: (query: string) => {
const results: string[] = []; const results: string[] = [];
const searchTerm = query.toLowerCase(); const searchTerm = query.toLowerCase();
console.log("in storeSearch. query: ", searchTerm);
for (const name of Object.keys(store)) { for (const name of Object.keys(store)) {
if (searchTerm === '' || name.toLowerCase().includes(searchTerm)) { if (searchTerm === '' || name.toLowerCase().includes(searchTerm)) {
results.push(name); results.push(name);
@ -53,4 +52,62 @@ export const _rt = {
obj[path[path.length - 1]] = maybeValue; obj[path[path.length - 1]] = maybeValue;
} }
}, },
measure: (ui: any): { width: number, height: number } => {
switch (ui._tag) {
case 'Rect': return { width: ui.w, height: ui.h };
case 'Text': return { width: ui.content.length * 10, height: 20 }; // TODO
case 'Clip': return { width: ui.w, height: ui.h };
case 'Row': {
let totalWidth = 0;
let maxHeight = 0;
for (const child of ui.children) {
const size = _rt.measure(child);
totalWidth += size.width;
maxHeight = Math.max(maxHeight, size.height);
}
totalWidth += ui.gap * (ui.children.length - 1);
return { width: totalWidth, height: maxHeight };
}
case 'Column': {
let totalHeight = 0;
let maxWidth = 0;
for (const child of ui.children) {
const size = _rt.measure(child);
totalHeight += size.height;
maxWidth = Math.max(maxWidth, size.width);
}
totalHeight += ui.gap * (ui.children.length - 1);
return { width: maxWidth, height: totalHeight };
}
case 'Padding': {
const childSize = _rt.measure(ui.child);
return {
width: childSize.width + ui.amount * 2,
height: childSize.height + ui.amount * 2,
}
}
case 'Stack': {
let maxWidth = 0;
let maxHeight = 0;
for (const child of ui.children) {
const size = _rt.measure(child);
maxWidth = Math.max(maxWidth, size.width);
maxHeight = Math.max(maxHeight, size.height);
}
return { width: maxWidth, height: maxHeight };
}
case 'Clickable':
case 'Opacity':
case 'Positioned':
return _rt.measure(ui.child);
default:
return { width: 0, height: 0 };
}
}
} }

@ -47,7 +47,7 @@ export function valueToUI(value: any): any {
return { return {
kind: 'positioned', kind: 'positioned',
x: value.x || 0, x: value.x || 0,
y: value.p || 0, y: value.y || 0,
child: valueToUI(value.child), child: valueToUI(value.child),
}; };

Loading…
Cancel
Save