Skip to content

Commit

Permalink
remove global ags object (#114)
Browse files Browse the repository at this point in the history
resolves #107
  • Loading branch information
Aylur authored Oct 14, 2023
1 parent 7b8e30b commit 7512796
Show file tree
Hide file tree
Showing 25 changed files with 237 additions and 440 deletions.
52 changes: 52 additions & 0 deletions src/ags.src.gresource.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?xml version="1.0" encoding="UTF-8"?>
<gresources>
<gresource prefix="/com/github/Aylur/ags">
<file>app.js</file>
<file>client.js</file>
<file>main.js</file>
<file>service.js</file>
<file>utils.js</file>
<file>variable.js</file>
<file>widget.js</file>

<file>widgets/constructor.js</file>
<file>widgets/overrides.js</file>
<file>widgets/box.js</file>
<file>widgets/button.js</file>
<file>widgets/centerbox.js</file>
<file>widgets/circularprogress.js</file>
<file>widgets/entry.js</file>
<file>widgets/eventbox.js</file>
<file>widgets/icon.js</file>
<file>widgets/label.js</file>
<file>widgets/menu.js</file>
<file>widgets/overlay.js</file>
<file>widgets/progressbar.js</file>
<file>widgets/revealer.js</file>
<file>widgets/scrollable.js</file>
<file>widgets/slider.js</file>
<file>widgets/stack.js</file>
<file>widgets/window.js</file>

<file>service/applications.js</file>
<file>service/audio.js</file>
<file>service/battery.js</file>
<file>service/bluetooth.js</file>
<file>service/hyprland.js</file>
<file>service/mpris.js</file>
<file>service/network.js</file>
<file>service/notifications.js</file>
<file>service/systemtray.js</file>

<file>dbus/types.js</file>
<file>dbus/com.github.Aylur.ags.xml</file>
<file>dbus/com.github.Aylur.ags.client.xml</file>
<file>dbus/org.freedesktop.DBus.xml</file>
<file>dbus/org.freedesktop.Notifications.xml</file>
<file>dbus/org.freedesktop.UPower.Device.xml</file>
<file>dbus/org.mpris.MediaPlayer2.Player.xml</file>
<file>dbus/org.mpris.MediaPlayer2.xml</file>
<file>dbus/org.kde.StatusNotifierWatcher.xml</file>
<file>dbus/org.kde.StatusNotifierItem.xml</file>
</gresource>
</gresources>
75 changes: 31 additions & 44 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ interface Config {
maxStreamVolume: number
}

export default class App extends Gtk.Application {
class App extends Gtk.Application {
static {
GObject.registerClass({
Signals: {
Expand All @@ -31,41 +31,35 @@ export default class App extends Gtk.Application {
}

private _dbus!: Gio.DBusExportedObject;
private _windows: Map<string, Gtk.Window>;
private _closeDelay!: { [key: string]: number };
private _cssProviders: Gtk.CssProvider[] = [];
private _busName: string;
private _objectPath: string;

static configPath: string;
static configDir: string;
static config: Config;
static instance: App;

static removeWindow(w: Gtk.Window | string) { App.instance.removeWindow(w); }
static addWindow(w: Gtk.Window) { App.instance.addWindow(w); }
static get windows() { return App.instance._windows; }
static getWindow(name: string) { return App.instance.getWindow(name); }
static closeWindow(name: string) { App.instance.closeWindow(name); }
static openWindow(name: string) { App.instance.openWindow(name); }
static toggleWindow(name: string) { App.instance.toggleWindow(name); }
static quit() { App.instance.quit(); }

static resetCss() {
private _objectPath!: string;

private _windows: Map<string, Gtk.Window> = new Map();
private _configPath!: string;
private _configDir!: string;
private _config!: Config;

get windows() { return this._windows; }
get configPath() { return this._configPath; }
get configDir() { return this._configDir; }
get config() { return this._config; }

resetCss() {
const screen = Gdk.Screen.get_default();
if (!screen) {
console.error("couldn't get screen");
return;
}

App.instance._cssProviders.forEach(provider => {
this._cssProviders.forEach(provider => {
Gtk.StyleContext.remove_provider_for_screen(screen, provider);
});

App.instance._cssProviders = [];
this._cssProviders = [];
}

static applyCss(path: string) {
applyCss(path: string) {
const screen = Gdk.Screen.get_default();
if (!screen) {
console.error("couldn't get screen");
Expand All @@ -81,30 +75,21 @@ export default class App extends Gtk.Application {
Gtk.STYLE_PROVIDER_PRIORITY_USER,
);

App.instance._cssProviders.push(cssProvider);
this._cssProviders.push(cssProvider);
}

constructor(bus: string, path: string, configPath: string) {
super({
application_id: bus,
flags: Gio.ApplicationFlags.DEFAULT_FLAGS,
});

this._busName = bus;
setup(bus: string, path: string, configPath: string) {
this.application_id = bus;
this.flags = Gio.ApplicationFlags.DEFAULT_FLAGS;
this._objectPath = path;
this._windows = new Map();

const dir = configPath.split('/');
dir.pop();
App.configDir = dir.join('/');
App.configPath = configPath;
App.instance = this;
this._configDir = configPath.split('/').slice(0, -1).join('/');
this._configPath = configPath;
}

connectWidget(
widget: Gtk.Widget,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
callback: (widget: Gtk.Widget, ...args: any[]) => void,
callback: (widget: Gtk.Widget, ...args: unknown[]) => void,
event = 'window-toggled',
) {
connect(this, widget, callback, event);
Expand Down Expand Up @@ -185,13 +170,13 @@ export default class App extends Gtk.Application {

private async _load() {
try {
const mod = await import(`file://${App.configPath}`);
const mod = await import(`file://${this._configPath}`);
const config = mod.default as Config;
config.closeWindowDelay ||= {};
config.notificationPopupTimeout ||= 3000;
config.maxStreamVolume ||= 1.5;
config.cacheNotificationActions ||= false;
App.config = config;
this._config = config;

if (!config) {
console.error('Missing default export');
Expand All @@ -202,7 +187,7 @@ export default class App extends Gtk.Application {
this._closeDelay = config.closeWindowDelay;

if (config.style)
App.applyCss(config.style);
this.applyCss(config.style);

if (config.windows && !Array.isArray(config.windows)) {
console.error('windows attribute has to be an array, ' +
Expand All @@ -222,11 +207,11 @@ export default class App extends Gtk.Application {
private _register() {
Gio.bus_own_name(
Gio.BusType.SESSION,
this._busName,
this.application_id,
Gio.BusNameOwnerFlags.NONE,
(connection: Gio.DBusConnection) => {
this._dbus = Gio.DBusExportedObject
.wrapJSObject(AgsIFace(this._busName) as string, this);
.wrapJSObject(AgsIFace(this.application_id) as string, this);

this._dbus.export(connection, this._objectPath);
},
Expand Down Expand Up @@ -265,3 +250,5 @@ export default class App extends Gtk.Application {

Quit() { this.quit(); }
}

export default new App();
26 changes: 3 additions & 23 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import Gio from 'gi://Gio';
import GLib from 'gi://GLib';
import * as Utils from './utils.js';
import App from './app.js';
import app from './app.js';
import client from './client.js';
import Service from './service.js';
import Variable from './variable.js';
import Widget from './widget.js';

const BIN_NAME = pkg.name.split('.').pop() as string;
const APP_BUS = (name: string) => `${pkg.name}.${name}`;
Expand All @@ -25,15 +22,7 @@ OPTIONS:
-t, --toggle-window Show or hide a window
-r, --run-js Evaluate given string as a function and execute it
-p, --run-promise Evaluate and execute function as Promise
--clear-cache Remove ${Utils.CACHE_DIR}
EXAMPLES
ags --config $HOME/.config/ags/main.js --bus-name second-instance
ags --run-js "ags.Service.Mpris.getPlayer()?.name"
ags --run-promise "ags.Utils.timeout(1000, () => {
resolve('a second later');
})"
ags --toggle-window window-name`;
--clear-cache Remove ${Utils.CACHE_DIR}`;

function isRunning(dbusName: string) {
return Gio.DBus.session.call_sync(
Expand Down Expand Up @@ -126,23 +115,14 @@ export function main(args: string[]) {
}
}

// @ts-expect-error
globalThis.ags = {
App,
Utils,
Widget,
Service,
Variable,
};

const bus = APP_BUS(flags.busName);
const path = APP_PATH(flags.busName);

if (!isRunning(bus)) {
if (flags.quit)
return;

const app = new App(bus, path, flags.config);
app.setup(bus, path, flags.config);
app.connect('config-parsed', () => {
if (flags.toggleWindow)
app.ToggleWindow(flags.toggleWindow);
Expand Down
Loading

0 comments on commit 7512796

Please sign in to comment.