Skip to content

Commit 65809a6

Browse files
merrymanlinusha
authored andcommitted
🎨: refactor layout-policy resizing and prevent feedbackloop via meta flag
Replaces old and not very well working control flows with a more sophisticated approach to determining sizing behavior of a styled morph within the context of various layouts. According to the identified sizing behavior, the extent is applied in varying fashions. Also previously accidental re-execution of layouts would happen due to policies applying themselves, which lead to inefficient layout applications overall. This is now prevented with a meta flag.
1 parent 545b452 commit 65809a6

File tree

2 files changed

+29
-24
lines changed

2 files changed

+29
-24
lines changed

‎lively.morphic/components/policy.js

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1295,7 +1295,7 @@ export class StylePolicy {
12951295
* @returns { boolean | object } Wether or not size is controlled via layout and if so, the concrete policy.
12961296
*/
12971297
isResizedByLayout (aSubmorph) {
1298-
const layout = aSubmorph.owner && aSubmorph.owner.layout;
1298+
let layout = aSubmorph.owner && aSubmorph.owner.layout;
12991299
let heightPolicy = 'fixed'; let widthPolicy = 'fixed';
13001300
if (aSubmorph.isText) {
13011301
if (!aSubmorph.fixedHeight) heightPolicy = 'hug';
@@ -1306,6 +1306,19 @@ export class StylePolicy {
13061306
if (widthPolicy !== 'hug') widthPolicy = layout.getResizeWidthPolicyFor(aSubmorph);
13071307
if (heightPolicy === 'fill' || widthPolicy === 'fill') return { widthPolicy, heightPolicy };
13081308
}
1309+
1310+
layout = aSubmorph.layout;
1311+
1312+
if (layout?.hugContentsVertically ||
1313+
layout?.hugContentsHorizontally ||
1314+
widthPolicy === 'hug' ||
1315+
heightPolicy === 'hug') {
1316+
return {
1317+
widthPolicy: layout?.hugContentsHorizontally ? 'hug' : widthPolicy,
1318+
heightPolicy: layout?.hugContentsVertically ? 'hug' : heightPolicy
1319+
};
1320+
}
1321+
13091322
return false;
13101323
}
13111324

@@ -1498,8 +1511,17 @@ export class PolicyApplicator extends StylePolicy {
14981511
if (propName === 'position' && this.isPositionedByLayout(morphToBeStyled)) continue;
14991512
let resizePolicy;
15001513
if (propName === 'extent' && (resizePolicy = this.isResizedByLayout(morphToBeStyled))) {
1501-
if (resizePolicy.widthPolicy === 'fixed' && morphToBeStyled.width !== propValue.x) morphToBeStyled.width = propValue.x;
1502-
if (resizePolicy.heightPolicy === 'fixed' && morphToBeStyled.height !== propValue.y) morphToBeStyled.height = propValue.y;
1514+
morphToBeStyled.withMetaDo({ deferLayoutApplication: true }, () => {
1515+
if (resizePolicy.widthPolicy === 'fixed' && morphToBeStyled.width !== propValue.x) {
1516+
morphToBeStyled.width = propValue.x;
1517+
}
1518+
if (resizePolicy.heightPolicy === 'fixed' && morphToBeStyled.height !== propValue.y) {
1519+
morphToBeStyled.height = propValue.y;
1520+
}
1521+
if (morphToBeStyled.isText && (resizePolicy.widthPolicy === 'hug' || resizePolicy.heightPolicy === 'hug')) {
1522+
morphToBeStyled.withMetaDo({ doNotFit: false }, () => morphToBeStyled.fit());
1523+
}
1524+
});
15031525
continue;
15041526
}
15051527

@@ -1514,32 +1536,11 @@ export class PolicyApplicator extends StylePolicy {
15141536
if (propName === 'position') continue;
15151537
}
15161538

1517-
// FIXME: other special cases??
1518-
if (morphToBeStyled.isText && propName === 'extent') {
1519-
if (!morphToBeStyled.fixedWidth && !morphToBeStyled.fixedHeight) continue;
1520-
if (!morphToBeStyled.fixedWidth) propValue = propValue.withX(morphToBeStyled.width);
1521-
if (!morphToBeStyled.fixedHeight) propValue = propValue.withY(morphToBeStyled.height);
1522-
}
1523-
1524-
if (morphToBeStyled.isText && propName === 'width' && morphToBeStyled.lineWrapping !== 'no-wrap') {
1525-
if (!morphToBeStyled.fixedWidth) continue;
1526-
morphToBeStyled.width = propValue;
1527-
morphToBeStyled.withMetaDo({ doNotFit: false }, () => morphToBeStyled.fit());
1528-
}
1529-
15301539
if (['border', 'borderTop', 'borderBottom', 'borderRight', 'borderLeft'].includes(propName)) continue; // handled by sub props;
15311540

15321541
if (!obj.equals(morphToBeStyled[propName], propValue)) {
15331542
morphToBeStyled[propName] = propValue;
15341543
}
1535-
1536-
// we may be late for the game when setting these props
1537-
// se we need to make sure, we restore the morphs "intended extent"
1538-
// for this purpose we enforce the masterSubmorph extent
1539-
if (['fixedHeight', 'fixedWidth'].includes(propName) &&
1540-
morphToBeStyled._parametrizedProps?.extent) {
1541-
morphToBeStyled.extent = morphToBeStyled._parametrizedProps.extent;
1542-
}
15431544
}
15441545
}
15451546

‎lively.morphic/layout.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,10 @@ export class TilingLayout extends Layout {
419419
}
420420

421421
scheduleApply (submorph, animation, change = {}) {
422+
if (change.meta?.deferLayoutApplication) {
423+
return;
424+
}
425+
422426
if (!change.meta?.isLayoutAction || !this.container?._yogaNode?.getParent()) {
423427
this._alreadyComputed = false;
424428
}

0 commit comments

Comments
 (0)