citadel-realms/js/ColorSchemeModel.js
2024-11-12 17:26:26 -05:00

148 lines
4.5 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 './model/Base16Themes.js';
class ModelBuilder {
constructor() {
this.CATEGORIES = [
['atelier', 'Atelier'],
['black-metal', 'Black Metal'],
['brushtrees', 'Brush Trees'],
['classic', 'Classic'],
['default', 'Default'],
['google', 'Google'],
['grayscale', 'Grayscale'],
['gruvbox', 'Gruvbox'],
['harmonic', 'Harmonic'],
['ia', 'iA'],
['material', 'Material'],
['papercolor', 'PaperColor'],
['solarized', 'Solarized'],
['summerfruit', 'Summerfruit'],
['tomorrow', 'Tomorrow'],
['unikitty', 'Unikitty'],
];
this.currentCategory = null;
this.storeModel = new Gio.ListStore(ColorSchemeNode);
}
matchesCategory(theme) {
return (this.CATEGORIES.length > 0 && theme.id.startsWith(this.CATEGORIES[0][0]));
}
appendCurrentCategory() {
if (this.currentCategory) {
this.storeModel.append(this.currentCategory);
this.currentCategory = null;
this.CATEGORIES.shift();
}
}
addToCategory(theme) {
if (!this.currentCategory) {
this.currentCategory = new ColorSchemeNode(this.CATEGORIES[0][1]);
}
this.currentCategory.addChild(new ColorSchemeNode(theme.name, theme));
}
addTheme(theme) {
if (this.matchesCategory(theme)) {
this.addToCategory(theme);
}
else {
if (this.currentCategory) {
this.appendCurrentCategory();
}
this.storeModel.append(new ColorSchemeNode(theme.name, theme));
}
}
static buildTreeModel() {
let builder = new ModelBuilder();
Base16Theme.THEME_LIST.forEach(theme => builder.addTheme(theme));
return Gtk.TreeListModel.new(builder.storeModel, false, false, item => item.child_model);
}
}
export class ColorSchemeNode extends GObject.Object {
constructor(text, theme = null) {
super();
this._text = text;
this.theme = theme;
this.child_model = null;
}
get text() {
return this._text;
}
addChild(child) {
if (this.child_model === null) {
this.child_model = new Gio.ListStore(_a);
}
this.child_model.append(child);
}
matchesId(id) {
return this.theme !== null && this.theme.id === id;
}
searchChildren(id) {
if (this.child_model) {
for (let i = 0; i < this.child_model.n_items; i++) {
let node = this.child_model.get_item(i);
if (node.matchesId(id)) {
return true;
}
}
}
return false;
}
}
_a = ColorSchemeNode;
(() => {
GObject.registerClass({
GTypeName: 'ColorSchemeNode',
Properties: {
'text': GObject.ParamSpec.string('text', '', '', GObject.ParamFlags.READWRITE, ''),
}
}, _a);
})();
export class ColorSchemeModel {
constructor() {
this.treeModel = ModelBuilder.buildTreeModel();
this.selection = Gtk.SingleSelection.new(this.treeModel);
}
selectedTheme() {
// @ts-ignore
let item = this.selection.selected_item.item;
return item === null || item === void 0 ? void 0 : item.theme;
}
changeSelected(offset) {
const n_items = this.selection.n_items;
if (n_items <= 1) {
return;
}
const pos = this.selection.selected;
if (pos == Gtk.INVALID_LIST_POSITION) {
return;
}
const newPos = pos + offset;
if (newPos < 0 || newPos >= n_items) {
return;
}
this.selection.selected = newPos;
return newPos;
}
selectPosition(pos) {
this.selection.selected = pos;
}
searchId(id, from = 0) {
for (let i = from; i < this.treeModel.n_items; i++) {
let row = this.treeModel.get_row(i);
let item = row === null || row === void 0 ? void 0 : row.item;
if (item && item.matchesId(id)) {
return row;
}
if (item.searchChildren(id)) {
row === null || row === void 0 ? void 0 : row.set_expanded(true);
if (from === 0) {
return this.searchId(id, i);
}
}
}
return null;
}
}