@gtkx/css 0.6.1 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/css.js +52 -1
- package/dist/style-sheet.js +14 -6
- package/package.json +4 -4
package/dist/css.js
CHANGED
|
@@ -1,5 +1,55 @@
|
|
|
1
1
|
import { serializeStyles } from "@emotion/serialize";
|
|
2
2
|
import { getGtkCache } from "./cache.js";
|
|
3
|
+
function expandNestedRules(styles, className) {
|
|
4
|
+
const selector = `.${className}`;
|
|
5
|
+
const expandedStyles = styles.replace(/&/g, selector);
|
|
6
|
+
const rules = [];
|
|
7
|
+
let currentRule = "";
|
|
8
|
+
let braceDepth = 0;
|
|
9
|
+
let inSelector = false;
|
|
10
|
+
let mainProperties = "";
|
|
11
|
+
for (let i = 0; i < expandedStyles.length; i++) {
|
|
12
|
+
const char = expandedStyles[i];
|
|
13
|
+
if (char === "{") {
|
|
14
|
+
braceDepth++;
|
|
15
|
+
if (braceDepth === 1 && currentRule.includes(selector)) {
|
|
16
|
+
inSelector = true;
|
|
17
|
+
currentRule += char;
|
|
18
|
+
}
|
|
19
|
+
else {
|
|
20
|
+
currentRule += char;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
else if (char === "}") {
|
|
24
|
+
braceDepth--;
|
|
25
|
+
currentRule += char;
|
|
26
|
+
if (braceDepth === 0 && inSelector) {
|
|
27
|
+
rules.push(currentRule.trim());
|
|
28
|
+
currentRule = "";
|
|
29
|
+
inSelector = false;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
else if (braceDepth === 0 && char === "." && expandedStyles.slice(i).startsWith(selector)) {
|
|
33
|
+
if (mainProperties.length > 0 || currentRule.length > 0) {
|
|
34
|
+
mainProperties += currentRule;
|
|
35
|
+
currentRule = "";
|
|
36
|
+
}
|
|
37
|
+
currentRule += char;
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
currentRule += char;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
if (currentRule.trim()) {
|
|
44
|
+
mainProperties += currentRule;
|
|
45
|
+
}
|
|
46
|
+
const allRules = [];
|
|
47
|
+
if (mainProperties.trim()) {
|
|
48
|
+
allRules.push(`${selector}{${mainProperties.trim()}}`);
|
|
49
|
+
}
|
|
50
|
+
allRules.push(...rules);
|
|
51
|
+
return allRules.join("\n");
|
|
52
|
+
}
|
|
3
53
|
/**
|
|
4
54
|
* Creates a CSS class from styles and injects them into GTK.
|
|
5
55
|
* Works like Emotion's css function but outputs to GTK's CssProvider.
|
|
@@ -20,9 +70,10 @@ export const css = (...args) => {
|
|
|
20
70
|
const serialized = serializeStyles(args, cache.registered);
|
|
21
71
|
const className = `${cache.key}-${serialized.name}`;
|
|
22
72
|
if (cache.inserted[serialized.name] === undefined) {
|
|
23
|
-
const cssRule =
|
|
73
|
+
const cssRule = expandNestedRules(serialized.styles, className);
|
|
24
74
|
cache.sheet.insert(cssRule);
|
|
25
75
|
cache.inserted[serialized.name] = serialized.styles;
|
|
76
|
+
cache.registered[className] = serialized.styles;
|
|
26
77
|
}
|
|
27
78
|
return className;
|
|
28
79
|
};
|
package/dist/style-sheet.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { events } from "@gtkx/ffi";
|
|
2
|
-
import
|
|
3
|
-
import
|
|
2
|
+
import * as Gdk from "@gtkx/ffi/gdk";
|
|
3
|
+
import * as Gtk from "@gtkx/ffi/gtk";
|
|
4
4
|
const STYLE_PROVIDER_PRIORITY_APPLICATION = 600;
|
|
5
5
|
let isGtkReady = false;
|
|
6
6
|
const pendingSheets = [];
|
|
@@ -11,7 +11,15 @@ const flushPendingStyles = () => {
|
|
|
11
11
|
}
|
|
12
12
|
pendingSheets.length = 0;
|
|
13
13
|
};
|
|
14
|
+
const resetGtkState = () => {
|
|
15
|
+
isGtkReady = false;
|
|
16
|
+
// Re-register the start listener for the next start() call
|
|
17
|
+
events.once("start", flushPendingStyles);
|
|
18
|
+
};
|
|
19
|
+
// Initial registration
|
|
14
20
|
events.once("start", flushPendingStyles);
|
|
21
|
+
// Handle stop to reset state and prepare for potential restart
|
|
22
|
+
events.on("stop", resetGtkState);
|
|
15
23
|
/**
|
|
16
24
|
* Custom StyleSheet implementation for Emotion that outputs to GTK's CssProvider.
|
|
17
25
|
* Implements Emotion's StyleSheet interface to enable CSS-in-JS with GTK widgets.
|
|
@@ -30,10 +38,10 @@ export class StyleSheet {
|
|
|
30
38
|
}
|
|
31
39
|
ensureProvider() {
|
|
32
40
|
if (!this.provider) {
|
|
33
|
-
this.provider = new CssProvider();
|
|
34
|
-
this.display = DisplayManager.get().getDefaultDisplay();
|
|
41
|
+
this.provider = new Gtk.CssProvider();
|
|
42
|
+
this.display = Gdk.DisplayManager.get().getDefaultDisplay();
|
|
35
43
|
if (this.display) {
|
|
36
|
-
StyleContext.addProviderForDisplay(this.display, this.provider, STYLE_PROVIDER_PRIORITY_APPLICATION);
|
|
44
|
+
Gtk.StyleContext.addProviderForDisplay(this.display, this.provider, STYLE_PROVIDER_PRIORITY_APPLICATION);
|
|
37
45
|
this.isRegistered = true;
|
|
38
46
|
}
|
|
39
47
|
}
|
|
@@ -64,7 +72,7 @@ export class StyleSheet {
|
|
|
64
72
|
}
|
|
65
73
|
flush() {
|
|
66
74
|
if (this.provider && this.display && this.isRegistered) {
|
|
67
|
-
StyleContext.removeProviderForDisplay(this.display, this.provider);
|
|
75
|
+
Gtk.StyleContext.removeProviderForDisplay(this.display, this.provider);
|
|
68
76
|
this.isRegistered = false;
|
|
69
77
|
}
|
|
70
78
|
this.rules = [];
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gtkx/css",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.0",
|
|
4
4
|
"description": "Emotion-style CSS-in-JS for GTKX applications",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"gtk",
|
|
@@ -33,9 +33,9 @@
|
|
|
33
33
|
"dist"
|
|
34
34
|
],
|
|
35
35
|
"dependencies": {
|
|
36
|
-
"@emotion/cache": "11.14.0",
|
|
37
|
-
"@emotion/serialize": "1.3.3",
|
|
38
|
-
"@gtkx/ffi": "0.
|
|
36
|
+
"@emotion/cache": "^11.14.0",
|
|
37
|
+
"@emotion/serialize": "^1.3.3",
|
|
38
|
+
"@gtkx/ffi": "0.8.0"
|
|
39
39
|
},
|
|
40
40
|
"scripts": {
|
|
41
41
|
"build": "tsc -b && cp ../../README.md .",
|