Skip to content

Commit

Permalink
basic style cache
Browse files Browse the repository at this point in the history
  • Loading branch information
ianharrigan committed Feb 17, 2025
1 parent 74ba533 commit 20bbae8
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 4 deletions.
24 changes: 24 additions & 0 deletions haxe/ui/core/Component.hx
Original file line number Diff line number Diff line change
Expand Up @@ -1243,6 +1243,30 @@ class Component extends ComponentImpl
// Style related
//***********************************************************************************************************

private var _styleCacheKey:String = null;
private var styleCacheKey(get, never):String;
private function get_styleCacheKey():String {
if (_styleCacheKey == null) {
var sb = new StringBuf();
var ref = this;
do {
sb.add(ref.className);
sb.add("_id:");
sb.add(ref.id);
sb.add("_classes:");
for (c in ref.classes) {
sb.add(c);
sb.add("|");
}
ref = ref.parentComponent;
} while (ref != null);

//_styleCacheKey = sb.toString();
return sb.toString();
}
return _styleCacheKey;
}

@:noCompletion private var _customStyle:Style = null;

/**
Expand Down
36 changes: 32 additions & 4 deletions haxe/ui/styles/StyleSheet.hx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package haxe.ui.styles;
import haxe.crypto.Sha1;
import haxe.ui.core.Component;
import haxe.ui.styles.elements.AnimationKeyFrames;
import haxe.ui.styles.elements.Directive;
import haxe.ui.styles.elements.ImportElement;
import haxe.ui.styles.elements.MediaQuery;
import haxe.ui.styles.elements.RuleElement;
Expand Down Expand Up @@ -142,18 +143,45 @@ class StyleSheet {
}
}

private var _componentStyleCache = new Map<String, Array<Map<String, Directive>>>();

public function buildStyleFor(c:Component, style:Style = null):Style {
if (style == null) {
style = {};
}
for (r in rules) {
if (!r.match(c)) {
continue;

var styleCacheKey = @:privateAccess c.styleCacheKey;

var cachedDirectives = _componentStyleCache.get(styleCacheKey);
if (cachedDirectives != null) {
for (directives in cachedDirectives) {
style.mergeDirectives(directives);
}
/*
var n = 0;
for (_ in _componentStyleCache.keys()) {
n++;
}
trace(n);
*/
} else {
//trace(styleCacheKey);
cachedDirectives = [];
for (r in rules) {
if (!r.match(c)) {
continue;
}

cachedDirectives.push(r.directives);
}

style.mergeDirectives(r.directives);
for (directives in cachedDirectives) {
style.mergeDirectives(directives);
}
_componentStyleCache.set(styleCacheKey, cachedDirectives);
}


return style;
}
}

0 comments on commit 20bbae8

Please sign in to comment.