Skip to content

Commit

Permalink
ability to change the origin style (parent of <html>)
Browse files Browse the repository at this point in the history
I want to invisibly set the zoom to the devicePixelRatio in the
demo site, but still allow people to see the effect of zoom on
<html> as they would in a normal browser.

I don't think this will be useful for other purposes, and it
effectively clears the style cache, so not documenting it.

---

I've been thinking about how to optimize the use of the zoom
property for devicePixelRatio for more than a month now. The way
the API is now, you have to regenerate both a DOM and a layout
tree if you want to change it. This could be bad for CellEngine
since it can retain tens of thousands of layouts.

I seriously considered having an element's style list be mutable.
I mostly figured out how Firefox tracks style damage, restyle
roots, lazy frame construction, etc. Avoiding recreating the DOM
and box tree when you want to change styles would lower GC
pressure and reduce CPU time. However, the CPU doesn't get off
that easily since layout is far more expensive than DOM and box
generation combined. It would also be a big API change because
I would have to move generate and style calc into layout.

Ultimatley I decided this isn't a problem to fix here because:
1. Server-side doesn't benefit from mutable DOM, and that's the
   primary focus for dropflow
2. CellEngine could use zoom: 1 for measurement and, when DPR
   changes, only regenerate in-viewport layouts (mark the rest
   dirty). Also worth noting is that CellEngine doesn't even need
   zoom since it only renders text. Who knows if it ever will.
3. CellEngine should probably not be retaining that many layouts.
   It would be better to page them in and out, only creating all
   of them when needed (measuring).
  • Loading branch information
chearon committed Nov 20, 2024
1 parent 5c9c18e commit c37883f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/api.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {HTMLElement, TextNode} from './dom.js';
import {DeclaredStyle, initialStyle, computeElementStyle} from './style.js';
import {DeclaredStyle, getOriginStyle, computeElementStyle} from './style.js';
import {registerFont, unregisterFont, getFontUrls, RegisterFontOptions} from './text-font.js';
import {generateBlockContainer, layoutBlockBox, BlockFormattingContext, BlockContainer} from './layout-flow.js';
import HtmlPaintBackend from './paint-html.js';
Expand All @@ -18,7 +18,7 @@ export {createDeclaredStyle as style} from './style.js';
export {registerFont, unregisterFont};

export function generate(rootElement: HTMLElement): BlockContainer {
if (rootElement.style === initialStyle) {
if (rootElement.style === getOriginStyle()) {
throw new Error(
'To use the hyperscript API, pass the element tree to dom() and use ' +
'the return value as the argument to generate().'
Expand Down
6 changes: 3 additions & 3 deletions src/dom.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {Box} from './layout-box.js';
import {loggableText} from './util.js';
import {Style, DeclaredStyle, initialStyle, EMPTY_STYLE} from './style.js';
import {Style, DeclaredStyle, getOriginStyle, EMPTY_STYLE} from './style.js';
import {query, queryAll, Adapter} from './style-query.js';

export class TextNode {
Expand All @@ -11,7 +11,7 @@ export class TextNode {

constructor(id: string, text: string, parent: HTMLElement | null = null) {
this.id = id;
this.style = initialStyle;
this.style = getOriginStyle();
this.text = text;
this.parent = parent;
}
Expand Down Expand Up @@ -40,7 +40,7 @@ export class HTMLElement {
) {
this.id = id;
this.tagName = tagName;
this.style = initialStyle;
this.style = getOriginStyle();
this.declaredStyle = declaredStyle;
this.parent = parent;
this.attrs = attrs;
Expand Down
22 changes: 20 additions & 2 deletions src/style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,25 @@ const initialPlainStyle: ComputedStyle = Object.freeze({
overflow: 'visible'
});

export const initialStyle = new Style(initialPlainStyle);
let originStyle = new Style(initialPlainStyle);

export function getOriginStyle() {
return originStyle;
}

/**
* Set the style that the <html> style inherits from
*
* Be careful calling this. It makes the inheritance style cache useless for any
* styles created after calling it. Using it incorrectly can hurt performance.
*
* Currently the only legitimately known usage is to set the zoom to a desired
* CSS-to-device pixel density (devicePixelRatio). As such, it should only be
* called when devicePixelRatio actually changes.
*/
export function setOriginStyle(style: Partial<ComputedStyle>) {
originStyle = new Style({...initialPlainStyle, ...style});
}

type InheritedStyleDefinitions = {[K in keyof DeclaredStyleProperties]: boolean};

Expand Down Expand Up @@ -964,7 +982,7 @@ export function computeElementStyle(el: HTMLElement | TextNode) {
el.style = createStyle(el.parent!.style, EMPTY_STYLE);
} else {
const styles = el.getDeclaredStyles();
const parentStyle = el.parent ? el.parent.style : initialStyle;
const parentStyle = el.parent ? el.parent.style : originStyle;
const uaDeclaredStyle = uaDeclaredStyles[el.tagName];
if (uaDeclaredStyle) styles.push(uaDeclaredStyle);
if (!el.parent) styles.push(rootDeclaredStyle);
Expand Down

0 comments on commit c37883f

Please sign in to comment.