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

92 lines
2.9 KiB
JavaScript

var _a;
import GObject from 'gi://GObject';
import Gdk from 'gi://Gdk?version=4.0';
import Gtk from 'gi://Gtk?version=4.0';
import { Realm } from "./model/Realm.js";
export class RealmsView extends Gtk.Widget {
constructor() {
super();
this._selectedRealm = null;
this._setupHiddenRealmsFilter();
this._setupRealmSorter();
const keyController = new Gtk.EventControllerKey();
keyController.connect('key-pressed', (_, keyval) => {
switch (keyval) {
case Gdk.KEY_j:
case Gdk.KEY_Down:
this.nextRealm();
break;
case Gdk.KEY_k:
case Gdk.KEY_Up:
this.prevRealm();
break;
case Gdk.KEY_Escape:
this.activate_action('app.quit', null);
print("escaped");
break;
}
});
this.add_controller(keyController);
}
_changeSelected(offset) {
const n_items = this._realmsSelection.n_items;
if (n_items <= 1) {
return;
}
const pos = this._realmsSelection.selected;
if (pos == Gtk.INVALID_LIST_POSITION) {
return;
}
const newPos = pos + offset;
if (newPos < 0 || newPos >= n_items) {
return;
}
this._realmsSelection.selected = newPos;
}
nextRealm() {
this._changeSelected(1);
}
prevRealm() {
this._changeSelected(-1);
}
get selectedRealm() {
return this._selectedRealm;
}
set selectedRealm(value) {
if (this.selectedRealm === value) {
return;
}
this._selectedRealm = value;
}
_setupHiddenRealmsFilter() {
this._hiddenRealmsFilterModel.filter = Gtk.CustomFilter.new(item => !item.is_system_realm);
}
_setupRealmSorter() {
// 1) Sort 'Current' the lowest (top of list).
// 2) Then sort 'Running' lower than not 'Running'
// 3) Realms in the same run state sorted by lowest timestamp
this._realmSorter.set_sort_func((a, b) => {
if (a.is_current || (a.is_running && !b.is_running)) {
return Gtk.Ordering.SMALLER;
}
else if (b.is_current || (b.is_running && !a.is_running)) {
return Gtk.Ordering.LARGER;
}
else {
return b.timestamp - a.timestamp;
}
});
}
}
_a = RealmsView;
(() => {
GObject.registerClass({
GTypeName: 'RealmsView',
Template: 'resource:///com/subgraph/citadel/Realms/ui/RealmsView.ui',
Properties: {
'selected-realm': GObject.ParamSpec.object('selected-realm', '', '', GObject.ParamFlags.READWRITE, Realm),
},
InternalChildren: ['realmsSelection', 'hiddenRealmsFilterModel', 'realmSorter'],
}, _a);
})();