Skip to content

Commit

Permalink
Migrate preferences code to ESM (mostly)
Browse files Browse the repository at this point in the history
  • Loading branch information
retrixe authored and andyholmes committed Apr 9, 2024
1 parent 7ba0830 commit addda66
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 99 deletions.
3 changes: 3 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,9 @@
"files": [
"webextension/**/*.js",
"src/shell/**/*.js",
"src/utils/**/*.js",
"src/preferences/**/*.js",
"src/gsconnect-preferences",
"src/extension.js",
"src/prefs.js"
],
Expand Down
2 changes: 1 addition & 1 deletion src/extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import * as Keybindings from './shell/keybindings.js';
import * as Notification from './shell/notification.js';
import * as Input from './shell/input.js';
import * as Utils from './shell/utils.js';
import Remote from './utils/remote.mjs';
import * as Remote from './utils/remote.js';
import setup from './utils/setup.mjs';

const QuickSettingsMenu = Main.panel.statusArea.quickSettings;
Expand Down
59 changes: 20 additions & 39 deletions src/gsconnect-preferences
Original file line number Diff line number Diff line change
@@ -1,50 +1,31 @@
#!/usr/bin/env gjs
#!/usr/bin/env -S gjs -m

// SPDX-FileCopyrightText: GSConnect Developers https://github.com/GSConnect
//
// SPDX-License-Identifier: GPL-2.0-or-later

// -*- mode: js; -*-

'use strict';
import Gdk from 'gi://Gdk?version=3.0';
import 'gi://GdkPixbuf?version=2.0';
import Gio from 'gi://Gio?version=2.0';
import GLib from 'gi://GLib?version=2.0';
import GObject from 'gi://GObject?version=2.0';
import Gtk from 'gi://Gtk?version=3.0';

imports.gi.versions.Gdk = '3.0';
imports.gi.versions.GdkPixbuf = '2.0';
imports.gi.versions.Gio = '2.0';
imports.gi.versions.GioUnix = '2.0';
imports.gi.versions.GLib = '2.0';
imports.gi.versions.GObject = '2.0';
imports.gi.versions.Gtk = '3.0';
import system from 'system';

const Gdk = imports.gi.Gdk;
const Gio = imports.gi.Gio;
const GLib = imports.gi.GLib;
const GObject = imports.gi.GObject;
const Gtk = imports.gi.Gtk;
import {Window} from './preferences/service.js';
import setup, {setupGettext} from './utils/setup.mjs';
import Config from './config.mjs';

import('gi://GioUnix?version=2.0').catch(() => {}); // Set version for optional dependency

// Bootstrap
function get_datadir() {
let [, path] = /@([^:]+):\d+/.exec(new Error().stack.split('\n')[1]);
const info = Gio.File.new_for_path(path)
.query_info('standard::*', Gio.FileQueryInfoFlags.NOFOLLOW_SYMLINKS, null);
path = info.get_is_symlink() ? info.get_symlink_target() : path;

return Gio.File.new_for_path(path).get_parent().get_path();
}

imports.searchPath.unshift(get_datadir());
imports.config.PACKAGE_DATADIR = imports.searchPath[0];

// Bootstrap
const {setup, setupGettext} = imports.utils.setup;
setup(imports.config.PACKAGE_DATADIR);
setup(GLib.path_get_dirname(GLib.filename_from_uri(import.meta.url)[0]));
setupGettext();

// Local Imports
const Config = imports.config;
const Settings = imports.preferences.service;


/**
* Class representing the GSConnect service daemon.
Expand All @@ -66,7 +47,7 @@ const Preferences = GObject.registerClass({

vfunc_activate() {
if (this._window === undefined) {
this._window = new Settings.Window({
this._window = new Window({
application: this,
});
}
Expand All @@ -78,21 +59,21 @@ const Preferences = GObject.registerClass({
super.vfunc_startup();

// Init some resources
let provider = new Gtk.CssProvider();
const provider = new Gtk.CssProvider();
provider.load_from_resource(`${Config.APP_PATH}/application.css`);
Gtk.StyleContext.add_provider_for_screen(
Gdk.Screen.get_default(),
provider,
Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION
);

let actions = [
const actions = [
['refresh', null],
['connect', GLib.VariantType.new('s')],
];

for (let [name, type] of actions) {
let action = new Gio.SimpleAction({
for (const [name, type] of actions) {
const action = new Gio.SimpleAction({
name: name,
parameter_type: type,
});
Expand All @@ -102,7 +83,7 @@ const Preferences = GObject.registerClass({

vfunc_activate_action(action_name, parameter) {
try {
let paramArray = [];
const paramArray = [];

if (parameter instanceof GLib.Variant)
paramArray[0] = parameter;
Expand All @@ -125,4 +106,4 @@ const Preferences = GObject.registerClass({
}
});

(new Preferences()).run([imports.system.programInvocationName].concat(ARGV));
(new Preferences()).run([system.programInvocationName].concat(ARGV));
23 changes: 11 additions & 12 deletions src/preferences/device.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,21 @@
//
// SPDX-License-Identifier: GPL-2.0-or-later

'use strict';
import Gio from 'gi://Gio';
import GLib from 'gi://GLib';
import GObject from 'gi://GObject';
import Gtk from 'gi://Gtk';
import Pango from 'gi://Pango';

const Gio = imports.gi.Gio;
const GLib = imports.gi.GLib;
const GObject = imports.gi.GObject;
const Gtk = imports.gi.Gtk;
const Pango = imports.gi.Pango;

const Config = imports.config;
const Keybindings = imports.preferences.keybindings;
import Config from '../config.mjs';
import * as Keybindings from './keybindings.js';


// Build a list of plugins and shortcuts for devices
const DEVICE_PLUGINS = [];
const DEVICE_SHORTCUTS = {};

// FIXME ESM
for (const name in imports.service.plugins) {
const module = imports.service.plugins[name];

Expand All @@ -41,7 +40,7 @@ for (const name in imports.service.plugins) {
* @param {Gtk.ListBoxRow} row - The current row
* @param {Gtk.ListBoxRow} before - The previous row
*/
function rowSeparators(row, before) {
export function rowSeparators(row, before) {
const header = row.get_header();

if (before === null) {
Expand All @@ -63,7 +62,7 @@ function rowSeparators(row, before) {
* @param {Gtk.ListBoxRow} row2 - The second row
* @return {number} -1, 0 or 1
*/
function titleSortFunc(row1, row2) {
export function titleSortFunc(row1, row2) {
if (!row1.title || !row2.title)
return 0;

Expand Down Expand Up @@ -249,7 +248,7 @@ const CommandEditor = GObject.registerClass({
/**
* A widget for configuring a remote device.
*/
var Panel = GObject.registerClass({
export const Panel = GObject.registerClass({
GTypeName: 'GSConnectPreferencesDevicePanel',
Properties: {
'device': GObject.ParamSpec.object(
Expand Down
20 changes: 9 additions & 11 deletions src/preferences/keybindings.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@
//
// SPDX-License-Identifier: GPL-2.0-or-later

'use strict';

const Gdk = imports.gi.Gdk;
const Gio = imports.gi.Gio;
const GLib = imports.gi.GLib;
const GObject = imports.gi.GObject;
const Gtk = imports.gi.Gtk;
import Gdk from 'gi://Gdk';
import Gio from 'gi://Gio';
import GLib from 'gi://GLib';
import GObject from 'gi://GObject';
import Gtk from 'gi://Gtk';


/*
Expand All @@ -32,7 +30,7 @@ const _MODIFIERS = [
/**
* Response enum for ShortcutChooserDialog
*/
var ResponseType = {
export const ResponseType = {
CANCEL: Gtk.ResponseType.CANCEL,
SET: Gtk.ResponseType.APPLY,
UNSET: 2,
Expand All @@ -42,7 +40,7 @@ var ResponseType = {
/**
* A simplified version of the shortcut editor from GNOME Control Center
*/
var ShortcutChooserDialog = GObject.registerClass({
export const ShortcutChooserDialog = GObject.registerClass({
GTypeName: 'GSConnectPreferencesShortcutEditor',
Template: 'resource:///org/gnome/Shell/Extensions/GSConnect/ui/preferences-shortcut-editor.ui',
Children: [
Expand Down Expand Up @@ -209,7 +207,7 @@ var ShortcutChooserDialog = GObject.registerClass({
* @param {number} [grabFlags] - Grab Flags
* @param {boolean} %true if available, %false on error or unavailable
*/
async function checkAccelerator(accelerator, modeFlags = 0, grabFlags = 0) {
export async function checkAccelerator(accelerator, modeFlags = 0, grabFlags = 0) {
try {
let result = false;

Expand Down Expand Up @@ -276,7 +274,7 @@ async function checkAccelerator(accelerator, modeFlags = 0, grabFlags = 0) {
* @param {string} accelerator - An accelerator as taken by Gtk.ShortcutLabel
* @return {string} An accelerator or %null if it should be unset.
*/
async function getAccelerator(summary, accelerator = null) {
export async function getAccelerator(summary, accelerator = null) {
try {
const dialog = new ShortcutChooserDialog({
summary: summary,
Expand Down
32 changes: 16 additions & 16 deletions src/preferences/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,26 @@
//
// SPDX-License-Identifier: GPL-2.0-or-later

'use strict';
import Gdk from 'gi://Gdk';
import GdkPixbuf from 'gi://GdkPixbuf';
import Gio from 'gi://Gio';
import GLib from 'gi://GLib';
import GObject from 'gi://GObject';
import Gtk from 'gi://Gtk';

const Gdk = imports.gi.Gdk;
const GdkPixbuf = imports.gi.GdkPixbuf;
const Gio = imports.gi.Gio;
const GLib = imports.gi.GLib;
const GObject = imports.gi.GObject;
const Gtk = imports.gi.Gtk;
import system from 'system';

const Config = imports.config;
const Device = imports.preferences.device;
const Remote = imports.utils.remote;
import Config from '../config.mjs';
import {Panel, rowSeparators} from './device.js';
import {Service} from '../utils/remote.js';


/*
* Header for support logs
*/
const LOG_HEADER = new GLib.Bytes(`
GSConnect: ${Config.PACKAGE_VERSION} (${Config.IS_USER ? 'user' : 'system'})
GJS: ${imports.system.version}
GJS: ${system.version}
Session: ${GLib.getenv('XDG_SESSION_TYPE')}
OS: ${GLib.get_os_info('PRETTY_NAME')}
--------------------------------------------------------------------------------
Expand Down Expand Up @@ -91,7 +91,7 @@ async function generateSupportLog(time) {
/**
* "Connect to..." Dialog
*/
var ConnectDialog = GObject.registerClass({
const ConnectDialog = GObject.registerClass({
GTypeName: 'GSConnectConnectDialog',
Template: 'resource:///org/gnome/Shell/Extensions/GSConnect/ui/connect-dialog.ui',
Children: [
Expand Down Expand Up @@ -132,7 +132,7 @@ var ConnectDialog = GObject.registerClass({
});


var Window = GObject.registerClass({
export const Window = GObject.registerClass({
GTypeName: 'GSConnectPreferencesWindow',
Properties: {
'display-mode': GObject.ParamSpec.string(
Expand Down Expand Up @@ -173,7 +173,7 @@ var Window = GObject.registerClass({
});

// Service Proxy
this.service = new Remote.Service();
this.service = new Service();

this._deviceAddedId = this.service.connect(
'device-added',
Expand All @@ -198,7 +198,7 @@ var Window = GObject.registerClass({
this.service_box.set_focus_vadjustment(this.service_window.vadjustment);

// Device List
this.device_list.set_header_func(Device.rowSeparators);
this.device_list.set_header_func(rowSeparators);

// Discoverable InfoBar
this.settings.bind(
Expand Down Expand Up @@ -580,7 +580,7 @@ var Window = GObject.registerClass({
try {
if (!this.stack.get_child_by_name(device.id)) {
// Add the device preferences
const prefs = new Device.Panel(device);
const prefs = new Panel(device);
this.stack.add_titled(prefs, device.id, device.name);

// Add a row to the device list
Expand Down
14 changes: 6 additions & 8 deletions src/utils/remote.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@
//
// SPDX-License-Identifier: GPL-2.0-or-later

'use strict';

const Gio = imports.gi.Gio;
const GLib = imports.gi.GLib;
const GObject = imports.gi.GObject;
import Gio from 'gi://Gio';
import GLib from 'gi://GLib';
import GObject from 'gi://GObject';

const SERVICE_NAME = 'org.gnome.Shell.Extensions.GSConnect';
const SERVICE_PATH = '/org/gnome/Shell/Extensions/GSConnect';
const DEVICE_NAME = 'org.gnome.Shell.Extensions.GSConnect.Device';
const DEVICE_PATH = '/org/gnome/Shell/Extensions/GSConnect/Device';
// const DEVICE_PATH = '/org/gnome/Shell/Extensions/GSConnect/Device';


const _PROPERTIES = {
Expand Down Expand Up @@ -51,7 +49,7 @@ function _proxyInit(proxy, cancellable = null) {
/**
* A simple proxy wrapper for devices exported over DBus.
*/
var Device = GObject.registerClass({
export const Device = GObject.registerClass({
GTypeName: 'GSConnectRemoteDevice',
Implements: [Gio.DBusInterface],
Properties: {
Expand Down Expand Up @@ -222,7 +220,7 @@ var Device = GObject.registerClass({
/**
* A simple proxy wrapper for the GSConnect service.
*/
var Service = GObject.registerClass({
export const Service = GObject.registerClass({
GTypeName: 'GSConnectRemoteService',
Implements: [Gio.DBusInterface],
Properties: {
Expand Down
12 changes: 0 additions & 12 deletions src/utils/remote.mjs

This file was deleted.

0 comments on commit addda66

Please sign in to comment.