-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathstyle.js
83 lines (70 loc) · 1.82 KB
/
style.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
'use strict';
import Gio from 'gi://Gio';
import GLib from 'gi://GLib';
import St from 'gi://St';
import { tempPath } from './utils.js';
export const Style = class {
constructor() {
this.styles = {};
this.style_contents = {};
}
unloadAll() {
let ctx = St.ThemeContext.get_for_stage(global.stage);
let theme = ctx.get_theme();
Object.keys(this.styles).forEach((k) => {
let fn = this.styles[k];
theme.unload_stylesheet(fn);
try {
fn.delete(null);
} catch (err) {
console.log(err);
}
});
}
build(name, style_array) {
let fn = this.styles[name];
let ctx = St.ThemeContext.get_for_stage(global.stage);
let theme = ctx.get_theme();
let content = '';
style_array.forEach((k) => {
content = `${content}\n${k}`;
});
if (this.style_contents[name] === content) {
// log('skip regeneration');
return;
}
if (fn) {
theme.unload_stylesheet(fn);
} else {
fn = Gio.File.new_for_path(tempPath(`${name}.css`));
this.styles[name] = fn;
}
this.style_contents[name] = content;
const [, etag] = fn.replace_contents(
content,
null,
false,
Gio.FileCreateFlags.REPLACE_DESTINATION,
null,
);
theme.load_stylesheet(fn);
// log(content);
}
rgba(color) {
let clr = color || [1, 1, 1, 1];
let res = clr.map((r) => Math.floor(255 * r));
res[3] = clr[3].toFixed(1);
return res.join(',');
}
hex(color) {
let r = Math.floor(color[0] * 255).toString(16);
let g = Math.floor(color[1] * 255).toString(16);
let b = Math.floor(color[2] * 255).toString(16);
if (r.length == 1) r += r;
if (g.length == 1) g += g;
if (b.length == 1) b += b;
let res = `#${r}${g}${b}`;
console.log(`${color} ${res}`);
return res;
}
};