You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

170 lines
4.7 KiB

'use strict';
// TODO: Add manual server entries
// TODO: Keyboard shortcuts
const app_id = 'org.snapcast.control.gtk';
var run_local = false;
try {
pkg.initGettext();
} catch(e) {
imports.package.init({
name: app_id
, version: '0.0.1'
, prefix: '/usr/local'
, libdir: '/usr/local/lib/x86_64-linux-gnu'
});
const pkg = imports.package;
run_local = true;
}
pkg.id = app_id;
pkg.initGettext();
pkg.initFormat();
pkg.require({
'Gdk': '3.0'
, 'Gio': '2.0'
, 'GLib': '2.0'
, 'GObject': '2.0'
, 'Gtk': '3.0'
});
const {Gdk, Gio, GLib, Gtk, GObject} = imports.gi;
const Util = imports.util;
const AppWindow = imports.appwindow;
function initEnvironment() {
window.getApp = function() {
return Gio.Application.get_default();
};
}
const Application = GObject.registerClass(class SnapControl extends Gtk.Application {
_init() {
super._init({
application_id: pkg.name
, flags: (Gio.ApplicationFlags.CAN_OVERRIDE_APP_ID | Gio.ApplicationFlags.FLAGS_NONE)
});
GLib.set_application_name('Snap Control');
Gtk.Window.set_default_icon_name(pkg.id);
this.connect('startup', this._on_startup.bind(this));
this.connect('activate', this._on_activate.bind(this));
this.connect('shutdown', this._on_shutdown.bind(this));
}
_on_startup() {
this.load_css();
Util.initActions(this, [{
name: 'servers'
, activate: this.showServersWindow.bind(this)
}, {
name: 'about'
, activate: this.showAboutWindow.bind(this)
}, {
name: 'quit'
, activate: (a, b) => {this.win.close()}
}]);
this._initAppMenu();
}
_on_activate() {
if (this.win === undefined) {
this.win = new AppWindow.SnapControlWindow({ application: this });
this.win.show_all();
}
this.win.present();
}
_on_shutdown() {
}
load_css() {
let css_provider = new Gtk.CssProvider();
if (run_local) {
let t = imports.searchPath[0].split('/');
let p = t.splice(0, t.length - 1).concat(['data', 'application.css']);
css_provider.load_from_path(GLib.DIR_SEPARATOR_S + GLib.build_filenamev(p));
} else {
let res_path = Gio.File.new_for_uri('resource:///org/snapcast/control/gtk/application.css');
css_provider.load_from_file(res_path);
}
let screen = Gdk.Screen.get_default();
let context = new Gtk.StyleContext();
Gtk.StyleContext.add_provider_for_screen(Gdk.Screen.get_default(), css_provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION);
}
_initAppMenu() {
let builder = new Gtk.Builder();
if (run_local) {
let t = imports.searchPath[0].split('/');
let p = t.splice(0, t.length - 1).concat(['data', 'app-menu.ui']);
builder.add_from_file(GLib.DIR_SEPARATOR_S + GLib.build_filenamev(p));
} else {
builder.add_from_resource('/org/snapcast/control/gtk/app-menu.ui');
}
let menu = builder.get_object('app-menu');
this.set_app_menu(menu);
}
_getBuilderFilePath(filename) {
let ret = filename;
if (run_local) {
let t = imports.searchPath[0].split('/');
let p = t.splice(0, t.length - 1).concat(['data', filename]);
ret = GLib.DIR_SEPARATOR_S + GLib.build_filenamev(p);
} else {
ret = 'resource:///org/snapcast/control/gtk/' + filename;
}
return ret
}
showAboutWindow() {
let aboutdialog = new Gtk.AboutDialog({modal: true, transient_for: this.win});
aboutdialog.set_program_name("%s %s".format(aboutdialog.get_program_name(), pkg.version));
let about_comment = "";
//about_comment += _("A controller for the Snapcast multi-room audio system.");
about_comment += _("A controller for the ") + '<a href="https://github.com/badaix/snapcast">Snapcast</a>' + (" multi-room audio system.");
//about_comment += '\n' + _("GTK+") + ' %d.%d.%d'.format(Gtk.get_major_version(), Gtk.get_minor_version(), Gtk.get_micro_version());
let commentLabel = aboutdialog.get_template_child(Gtk.AboutDialog, 'comments_label');
commentLabel.set_markup(about_comment);
commentLabel.show();
aboutdialog.set_copyright("Copyright 2020 The Programmer.");
aboutdialog.set_logo_icon_name(pkg.name);
aboutdialog.set_website("https://git.lalonde.me/matth/Snapcontrol");
aboutdialog.set_website_label(_("Homepage"));
aboutdialog.set_license_type(Gtk.License.GPL_3_0);
aboutdialog.set_authors([
"Matthieu Lalonde <matth@noreply.git.lalonde.me>"
]);
aboutdialog.add_credit_section('', []);
aboutdialog.add_credit_section(_("Thanks to"), [
'Snapcast https://github.com/badaix/snapcast'
, ' Johannes Pohl https://github.com/badaix'
]);
aboutdialog.connect('response', () => {aboutdialog.destroy()});
aboutdialog.show();
}
showServersWindow() {
if (this.win) {
this.win.showServersWindow();
}
}
});
function main(argv) {
initEnvironment();
//window.getApp = () => Gio.Application.get_default();
return (new Application()).run(argv);
}
if (run_local) {
main([]);
}