112 lines
3.1 KiB
JavaScript
112 lines
3.1 KiB
JavaScript
|
var _a;
|
||
|
import GObject from 'gi://GObject';
|
||
|
import Gio from 'gi://Gio';
|
||
|
import Gtk from 'gi://Gtk?version=4.0';
|
||
|
import { Base16Theme } from './Base16Theme.js';
|
||
|
export class ColorSchemeModel {
|
||
|
constructor() {
|
||
|
let builder = new TreeModelBuilder();
|
||
|
this.treeModel = builder.buildTreeModel();
|
||
|
this.selection = Gtk.SingleSelection.new(this.treeModel);
|
||
|
}
|
||
|
}
|
||
|
class ColorModelNode extends GObject.Object {
|
||
|
constructor(data) {
|
||
|
super();
|
||
|
this.data = data;
|
||
|
}
|
||
|
get text() {
|
||
|
return this.data.name;
|
||
|
}
|
||
|
child_model() {
|
||
|
if (this.data instanceof ColorThemeCategory) {
|
||
|
return this.data.children;
|
||
|
}
|
||
|
else {
|
||
|
return null;
|
||
|
}
|
||
|
}
|
||
|
isThemeWithId(id) {
|
||
|
return (this.data instanceof Base16Theme) &&
|
||
|
(this.data.id === id);
|
||
|
}
|
||
|
hasChildThemeWithId(id) {
|
||
|
const child_model = this.child_model();
|
||
|
if (child_model) {
|
||
|
for (let i = 0; i < child_model.n_items; i++) {
|
||
|
let node = child_model.get_item(i);
|
||
|
if (node.isThemeWithId(id)) {
|
||
|
return true;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return false;
|
||
|
}
|
||
|
static newScheme(color) {
|
||
|
return new _a(color);
|
||
|
}
|
||
|
static newCategory(category) {
|
||
|
return new _a(category);
|
||
|
}
|
||
|
}
|
||
|
_a = ColorModelNode;
|
||
|
(() => {
|
||
|
GObject.registerClass({
|
||
|
GTypeName: 'ColorModelNode',
|
||
|
Properties: {
|
||
|
'text': GObject.ParamSpec.string('text', '', '', GObject.ParamFlags.READWRITE, ''),
|
||
|
}
|
||
|
}, _a);
|
||
|
})();
|
||
|
class ColorThemeCategory {
|
||
|
constructor(name) {
|
||
|
this.name = name;
|
||
|
this.children = new Gio.ListStore(ColorModelNode);
|
||
|
}
|
||
|
append(color) {
|
||
|
this.children.append(ColorModelNode.newScheme(color));
|
||
|
}
|
||
|
}
|
||
|
class TreeModelBuilder {
|
||
|
constructor() {
|
||
|
this.categories = Base16Theme.CATEGORIES;
|
||
|
this.currentCategory = null;
|
||
|
this.storeModel = new Gio.ListStore(ColorModelNode);
|
||
|
}
|
||
|
buildTreeModel() {
|
||
|
Base16Theme.THEMES.forEach(theme => this.addTheme(theme));
|
||
|
return Gtk.TreeListModel.new(this.storeModel, false, false, item => item.child_model());
|
||
|
}
|
||
|
appendCurrentCategory() {
|
||
|
if (this.currentCategory) {
|
||
|
this.storeModel.append(ColorModelNode.newCategory(this.currentCategory));
|
||
|
this.currentCategory = null;
|
||
|
this.categories.shift();
|
||
|
}
|
||
|
}
|
||
|
matchesNextCategory(theme) {
|
||
|
if (this.categories.length === 0) {
|
||
|
return false;
|
||
|
}
|
||
|
let [id, _name] = this.categories[0];
|
||
|
return theme.id === id;
|
||
|
}
|
||
|
addTheme(theme) {
|
||
|
if (this.matchesNextCategory(theme)) {
|
||
|
this.addToCategory(theme);
|
||
|
return;
|
||
|
}
|
||
|
if (this.currentCategory) {
|
||
|
this.appendCurrentCategory();
|
||
|
}
|
||
|
this.storeModel.append(ColorModelNode.newScheme(theme));
|
||
|
}
|
||
|
addToCategory(theme) {
|
||
|
if (!this.currentCategory) {
|
||
|
let [_id, name] = this.categories[0];
|
||
|
this.currentCategory = new ColorThemeCategory(name);
|
||
|
}
|
||
|
this.currentCategory.append(theme);
|
||
|
}
|
||
|
}
|