subclass window

This commit is contained in:
Aylur
2023-08-15 23:26:51 +02:00
parent 2bf9e59c87
commit 791e3c6909
9 changed files with 226 additions and 57 deletions
+1 -3
View File
@@ -63,9 +63,7 @@ rules:
indent:
- error
- 4
- ignoredNodes:
- 'CallExpression[callee.object.name=GObject][callee.property.name=registerClass] > ClassExpression:first-child'
MemberExpression: 'off'
- SwitchCase: 1
keyword-spacing:
- error
- before: true
+1
View File
@@ -23,6 +23,7 @@
<file>widgets/progressbar.js</file>
<file>widgets/menu.js</file>
<file>widgets/entry.js</file>
<file>widgets/window.js</file>
<file>service/service.js</file>
<file>service/apps.js</file>
+30 -30
View File
@@ -31,24 +31,24 @@ COMMANDS:
export function main(args: string[]) {
switch (args[1]) {
case 'version':
case '-v':
case '--version':
print(pkg.version);
return;
case 'version':
case '-v':
case '--version':
print(pkg.version);
return;
case 'help':
case '-h':
case '--help':
print(help(args[0]));
return;
case 'help':
case '-h':
case '--help':
print(help(args[0]));
return;
case 'clear-cache':
Utils.exec(`rm -r ${Utils.CACHE_DIR}`);
return;
case 'clear-cache':
Utils.exec(`rm -r ${Utils.CACHE_DIR}`);
return;
default:
break;
default:
break;
}
// @ts-ignore
@@ -69,24 +69,24 @@ export function main(args: string[]) {
);
switch (args[1]) {
case 'toggle-window':
actions.activate_action('toggle-window', new GLib.Variant('s', args[2]));
return;
case 'toggle-window':
actions.activate_action('toggle-window', new GLib.Variant('s', args[2]));
return;
case 'run-js':
actions.activate_action('run-js', new GLib.Variant('s', args[2]));
return;
case 'run-js':
actions.activate_action('run-js', new GLib.Variant('s', args[2]));
return;
case 'inspector':
actions.activate_action('inspector', null);
return;
case 'inspector':
actions.activate_action('inspector', null);
return;
case 'quit':
actions.activate_action('quit', null);
return;
case 'quit':
actions.activate_action('quit', null);
return;
default:
print(help(args[0]));
return;
default:
print(help(args[0]));
return;
}
}
+2
View File
@@ -16,6 +16,7 @@ import Revealer from './widgets/revealer.js';
import ProgressBar from './widgets/progressbar.js';
import Entry from './widgets/entry.js';
import { Menu, MenuItem } from './widgets/menu.js';
import Window from './widgets/window.js';
interface ServiceAPI {
instance: {
@@ -220,4 +221,5 @@ Widget.ProgressBar = (params: object) => constructor(ProgressBar, params);
Widget.Menu = (params: object) => constructor(Menu, params);
Widget.MenuItem = (params: object) => constructor(MenuItem, params);
Widget.Entry = (params: object) => constructor(Entry, params);
Widget.Window = (params: object) => constructor(Window, params);
Widget.Widget = ({ type, ...params }: { type: { new(...args: any[]): Gtk.Widget } }) => constructor(type, params);
-1
View File
@@ -45,7 +45,6 @@ export default class CenterBox extends Box {
_start!: Gtk.Widget | null;
get start_widget() { return this._start || null; }
set start_widget(child: Gtk.Widget | null) {
print('test', this._start);
if (this._start)
this.remove(this._start);
+1 -1
View File
@@ -59,7 +59,7 @@ export default class EventBox extends Gtk.EventBox {
}
vfunc_enter_notify_event(event: Gdk.EventCrossing): boolean {
this.unset_state_flags(Gtk.StateFlags.PRELIGHT);
this.set_state_flags(Gtk.StateFlags.PRELIGHT, false);
return runCmd(this.onHover, this, event);
}
+12 -8
View File
@@ -20,12 +20,6 @@ export default class Stack extends Gtk.Stack {
GObject.ParamFlags.READWRITE | GObject.ParamFlags.CONSTRUCT,
'none',
),
// @ts-ignore
'items': GObject.ParamSpec.jsobject(
'items', 'Items', 'Items',
GObject.ParamFlags.READWRITE | GObject.ParamFlags.CONSTRUCT,
[],
),
'shown': GObject.ParamSpec.string(
'shown', 'Shown', 'Shown',
GObject.ParamFlags.READWRITE | GObject.ParamFlags.CONSTRUCT,
@@ -35,6 +29,11 @@ export default class Stack extends Gtk.Stack {
}, this);
}
constructor({ items = [], ...params }: { [key: string]: any } = {}) {
super(params);
this.items = items;
}
add_named(child: Gtk.Widget, name: string): void {
this._items.push([name, child]);
super.add_named(child, name);
@@ -46,8 +45,7 @@ export default class Stack extends Gtk.Stack {
this.get_children().forEach(ch => this.remove(ch));
this._items = [];
items.forEach(([name, widget]) => {
if (widget)
this.add_named(widget, name);
widget && this.add_named(widget, name);
});
this.show_all();
}
@@ -68,9 +66,15 @@ export default class Stack extends Gtk.Stack {
get shown() { return this.visible_child_name; }
set shown(name: string) {
if (name === null || !this.get_child_by_name(name)) {
this.visible = false;
return;
}
if (!name)
return;
this.visible = true;
this.set_visible_child_name(name);
}
}
+165
View File
@@ -0,0 +1,165 @@
import GObject from 'gi://GObject';
import Gtk from 'gi://Gtk?version=3.0';
import Gdk from 'gi://Gdk?version=3.0';
import App from '../app.js';
const { GtkLayerShell } = imports.gi;
export interface Params {
anchor?: string[]
exclusive?: boolean
focusable?: boolean
layer?: string
margins?: number[] | number
monitor?: number | null
popup?: boolean
visible?: boolean | null
}
export default class Window extends Gtk.Window {
static {
GObject.registerClass({ GTypeName: 'AgsWindow' }, this);
}
constructor({
anchor = [],
exclusive = false,
focusable = false,
layer = 'top',
margins = [],
monitor = null,
popup = false,
visible = null,
...params
}: Params) {
super(params);
GtkLayerShell.init_for_window(this);
GtkLayerShell.set_namespace(this, this.name);
this.show_all();
this.anchor = anchor;
this.exclusive = exclusive;
this.focusable = focusable;
this.layer = layer;
this.margins = margins;
this.monitor = monitor;
this.popup = popup;
this.visible = visible === true || visible === null && !popup;
}
set monitor(monitor: number | null) {
if (monitor === null)
return;
if (typeof monitor === 'number') {
const display = Gdk.Display.get_default();
display
? GtkLayerShell.set_monitor(this, display.get_monitor(monitor))
: console.error(`Could not find monitor with id: ${monitor}`);
}
}
_exclusive = false;
get exclusive() { return this._exclusive; }
set exclusive(exclusive: boolean) {
this._exclusive = exclusive;
GtkLayerShell.auto_exclusive_zone_enable(this);
}
_layer = 'top';
get layet() { return this._layer; }
set layer(layer: string) {
this._layer;
GtkLayerShell.set_layer(this, GtkLayerShell.Layer[layer?.toUpperCase()]);
}
_anchor: string[] = [];
get anchor() { return this._anchor; }
set anchor(anchor: string[] | string) {
this._anchor = [];
['TOP', 'LEFT', 'RIGHT', 'BOTTOM'].forEach(side =>
GtkLayerShell.set_anchor(
this, GtkLayerShell.Edge[side], false,
),
);
if (typeof anchor === 'string')
anchor = anchor.split(' ');
if (Array.isArray(anchor)) {
anchor.forEach(side => {
GtkLayerShell.set_anchor(
this,
GtkLayerShell.Edge[side.toUpperCase()],
true,
);
this._anchor.push(side);
});
}
}
_margins: number[] | number = 0;
set margins(margin: number[] | number) {
let margins: [side: string, index: number][] = [];
if (typeof margin === 'number')
margin = [margin];
switch (margin.length) {
case 1:
margins = [['TOP', 0], ['RIGHT', 0], ['BOTTOM', 0], ['LEFT', 0]];
break;
case 2:
margins = [['TOP', 0], ['RIGHT', 1], ['BOTTOM', 0], ['LEFT', 1]];
break;
case 3:
margins = [['TOP', 0], ['RIGHT', 1], ['BOTTOM', 2], ['LEFT', 1]];
break;
case 4:
margins = [['TOP', 0], ['RIGHT', 1], ['BOTTOM', 2], ['LEFT', 3]];
break;
default:
break;
}
margins.forEach(([side, i]) =>
GtkLayerShell.set_margin(this, GtkLayerShell.Edge[side], (margin as number[])[i]),
);
this._margins = margin;
}
_popup!: number;
get popup() { return !!this._popup; }
set popup(popup: boolean) {
if (this._popup)
this.disconnect(this._popup);
if (popup) {
this.connect('key-press-event', (_, event) => {
if (event.get_keyval()[1] === Gdk.KEY_Escape)
App.getWindow(this.name) ? App.closeWindow(this.name) : this.hide();
});
}
}
get focusable() {
return GtkLayerShell.get_keyboard_mode(this) === GtkLayerShell.KeyboardMode.ON_DEMAND;
}
set focusable(focusable: boolean) {
GtkLayerShell.set_keyboard_mode(
this, GtkLayerShell.KeyboardMode[focusable ? 'ON_DEMAND' : 'NONE'],
);
}
// @ts-ignore
get child() { return this.get_child(); }
set child(child: Gtk.Widget) {
const widget = this.get_child();
if (widget)
this.remove(widget);
if (child)
this.add(child);
}
}
+14 -14
View File
@@ -77,20 +77,20 @@ export default function Window({
margin = [margin];
switch (margin.length) {
case 1:
margins = [['TOP', 0], ['RIGHT', 0], ['BOTTOM', 0], ['LEFT', 0]];
break;
case 2:
margins = [['TOP', 0], ['RIGHT', 1], ['BOTTOM', 0], ['LEFT', 1]];
break;
case 3:
margins = [['TOP', 0], ['RIGHT', 1], ['BOTTOM', 2], ['LEFT', 1]];
break;
case 4:
margins = [['TOP', 0], ['RIGHT', 1], ['BOTTOM', 2], ['LEFT', 3]];
break;
default:
break;
case 1:
margins = [['TOP', 0], ['RIGHT', 0], ['BOTTOM', 0], ['LEFT', 0]];
break;
case 2:
margins = [['TOP', 0], ['RIGHT', 1], ['BOTTOM', 0], ['LEFT', 1]];
break;
case 3:
margins = [['TOP', 0], ['RIGHT', 1], ['BOTTOM', 2], ['LEFT', 1]];
break;
case 4:
margins = [['TOP', 0], ['RIGHT', 1], ['BOTTOM', 2], ['LEFT', 3]];
break;
default:
break;
}
margins.forEach(([side, i]) =>