Skip to content

Commit

Permalink
refactor: ensure components shared between extension.js and prefs.js …
Browse files Browse the repository at this point in the history
…imports only the right libraries
  • Loading branch information
domferr committed Nov 3, 2024
1 parent e6b2ad7 commit 2308549
Show file tree
Hide file tree
Showing 47 changed files with 86 additions and 63 deletions.
28 changes: 20 additions & 8 deletions esbuild.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -124,17 +124,29 @@ build({
target: 'firefox78',
platform: 'node',
format: 'esm',
external: ['gi://*', 'resource://*', 'system', 'gettext', 'cairo', '@girs*'],
plugins: [sassPlugin()]/*,
banner: {
js: banner,
},
footer: {
js: footer
}*/
external: ['gi://*', 'resource://*'],
plugins: [sassPlugin()],
}).then(() => {
fs.renameSync(path.resolve(distDir, "extension.css"), path.resolve(distDir, "stylesheet.css"));
fs.cpSync(resourcesDir, distDir, { recursive: true });
// warn if you imported GTK libraries in GNOME Shell (Gdk, Gtk or Adw)
const extensionJSContent = fs.readFileSync(`${distDir}/extension.js`).toString().split('\n');
['Gdk', 'Gtk', 'Adw'].forEach(moduleName => {
for (let lineNumber = 0; lineNumber < extensionJSContent.length; lineNumber++) {
if (extensionJSContent[lineNumber].indexOf(`import ${moduleName}`) >= 0) {
console.error(`⚠️ Error: "${moduleName}" was imported in extension.js at line ${lineNumber}`);
}
}
});
// warn if you imported GNOME Shell libraries in Preferences (Clutter, Meta, St or Shell)
const prefsJSContent = fs.readFileSync(`${distDir}/prefs.js`).toString().split('\n');
['Clutter', 'Meta', 'Mtk', 'St', 'Shell'].forEach(moduleName => {
for (let lineNumber = 0; lineNumber < prefsJSContent.length; lineNumber++) {
if (prefsJSContent[lineNumber].indexOf(`import ${moduleName}`) >= 0) {
console.error(`⚠️ Error: "${moduleName}" was imported in prefs.js at line ${lineNumber}`);
}
}
});
}).then(async () => {
console.log(" 💡", "Generating legacy version...");
// duplicate the build into distLegacyDir
Expand Down
2 changes: 1 addition & 1 deletion src/components/editor/editableTilePreview.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import TilePreview from '../tilepreview/tilePreview';
import { GObject, St, Clutter, Mtk } from '@gi';
import { GObject, St, Clutter, Mtk } from '@gi.ext';
import Tile from '../layout/Tile';
import Slider from './slider';
import TileUtils from '../layout/TileUtils';
Expand Down
2 changes: 1 addition & 1 deletion src/components/editor/editorDialog.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Settings from '@settings/settings';
import { registerGObjectClass } from '@/utils/gjs';
import { St, Clutter, Gio } from '@gi';
import { St, Clutter, Gio } from '@gi.ext';
import LayoutButton from '../../indicator/layoutButton';
import GlobalState from '@utils/globalState';
import Layout from '@/components/layout/Layout';
Expand Down
2 changes: 1 addition & 1 deletion src/components/editor/hoverLine.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { registerGObjectClass } from '@/utils/gjs';
import { GLib, St, Clutter, Shell } from '@gi';
import { GLib, St, Clutter, Shell } from '@gi.ext';
import EditableTilePreview from './editableTilePreview';
import { getScalingFactorOf } from '@utils/ui';

Expand Down
2 changes: 1 addition & 1 deletion src/components/editor/layoutEditor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { registerGObjectClass } from '@/utils/gjs';
import { GObject, St, Clutter, Mtk, Meta, Shell } from '@gi';
import { GObject, St, Clutter, Mtk, Meta, Shell } from '@gi.ext';
import Settings from '@settings/settings';
import * as Main from 'resource:///org/gnome/shell/ui/main.js';
import {
Expand Down
2 changes: 1 addition & 1 deletion src/components/editor/slider.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { registerGObjectClass } from '@/utils/gjs';
import { GObject, St, Clutter, Mtk, Meta } from '@gi';
import { GObject, St, Clutter, Mtk, Meta } from '@gi.ext';
import EditableTilePreview from './editableTilePreview';
import { getEventCoords, getScalingFactorOf } from '@utils/ui';

Expand Down
2 changes: 1 addition & 1 deletion src/components/layout/LayoutWidget.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { St, Clutter, Mtk } from '@gi';
import { St, Clutter, Mtk } from '@gi.ext';
import TilePreview from '../tilepreview/tilePreview';
import {
buildRectangle,
Expand Down
2 changes: 1 addition & 1 deletion src/components/layout/Tile.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { GObject } from '@prefs.gi'; // prefs.gi because for transitivity this is also declared in prefs.js
import { GObject } from '@gi.shared'; // gi.shared because it is imported by Layout which is also imported in prefs.ts

export default class Tile {
// @ts-expect-error "GObject has TYPE_JSOBJECT"
Expand Down
2 changes: 1 addition & 1 deletion src/components/layout/TileUtils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Mtk } from '@gi';
import { Mtk } from '@gi.ext';
import Tile from './Tile';
import { buildRectangle } from '@utils/ui';

Expand Down
2 changes: 1 addition & 1 deletion src/components/snapassist/snapAssist.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { registerGObjectClass } from '@/utils/gjs';
import { GObject, St, Clutter, Mtk, Meta, Gio } from '@gi';
import { GObject, St, Clutter, Mtk, Meta, Gio } from '@gi.ext';
import SnapAssistTile from './snapAssistTile';
import SnapAssistLayout from './snapAssistLayout';
import Layout from '../layout/Layout';
Expand Down
2 changes: 1 addition & 1 deletion src/components/snapassist/snapAssistLayout.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { registerGObjectClass } from '@/utils/gjs';
import { buildRectangle, getScalingFactorOf } from '@/utils/ui';
import { Clutter, Mtk } from '@gi';
import { Clutter, Mtk } from '@gi.ext';
import LayoutWidget from '../layout/LayoutWidget';
import Layout from '../layout/Layout';
import Tile from '../layout/Tile';
Expand Down
2 changes: 1 addition & 1 deletion src/components/snapassist/snapAssistTile.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { registerGObjectClass } from '@/utils/gjs';
import TilePreview from '../tilepreview/tilePreview';
import Tile from '../layout/Tile';
import { St, Clutter, Mtk } from '@gi';
import { St, Clutter, Mtk } from '@gi.ext';
import { getScalingFactorOf } from '@utils/ui';

@registerGObjectClass
Expand Down
2 changes: 1 addition & 1 deletion src/components/snapassist/snapAssistTileButton.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Tile from '@components/layout/Tile';
import { registerGObjectClass } from '@utils/gjs';
import { St, Clutter, Mtk } from '@gi';
import { St, Clutter, Mtk } from '@gi.ext';
import SnapAssistTile from '@components/snapassist/snapAssistTile';

@registerGObjectClass
Expand Down
2 changes: 1 addition & 1 deletion src/components/tilepreview/blurTilePreview.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/ban-ts-comment */
import { registerGObjectClass } from '@/utils/gjs';
import { Shell } from '@gi';
import { Shell } from '@gi.ext';
import TilePreview from './tilePreview';

@registerGObjectClass
Expand Down
2 changes: 1 addition & 1 deletion src/components/tilepreview/selectionTilePreview.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { registerGObjectClass } from '@/utils/gjs';
import { GObject, St, Clutter, Gio } from '@gi';
import { GObject, St, Clutter, Gio } from '@gi.ext';
import TilePreview from './tilePreview';
import Settings from '@settings/settings';
import { buildBlurEffect } from '@utils/ui';
Expand Down
2 changes: 1 addition & 1 deletion src/components/tilepreview/tilePreview.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { St, Clutter, Mtk, Meta } from '@gi';
import { St, Clutter, Mtk, Meta } from '@gi.ext';
import { registerGObjectClass } from '@/utils/gjs';
import { buildRectangle, getScalingFactorOf } from '@utils/ui';
import GlobalState from '@utils/globalState';
Expand Down
2 changes: 1 addition & 1 deletion src/components/tilingsystem/edgeTilingManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
isPointInsideRect,
clampPointInsideRect,
} from '@utils/ui';
import { GObject, Mtk } from '@gi';
import { GObject, Mtk } from '@gi.ext';
import Settings from '@settings/settings';
import { registerGObjectClass } from '@utils/gjs';

Expand Down
2 changes: 1 addition & 1 deletion src/components/tilingsystem/extendedWindow.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Tile from '@components/layout/Tile';
import { Mtk, Meta } from '@gi';
import { Mtk, Meta } from '@gi.ext';

interface ExtendedWindow extends Meta.Window {
originalSize: Mtk.Rectangle | undefined;
Expand Down
2 changes: 1 addition & 1 deletion src/components/tilingsystem/resizeManager.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { St, Mtk, Meta } from '@gi';
import { St, Mtk, Meta } from '@gi.ext';
import SignalHandling from '@utils/signalHandling';
import Settings from '@settings/settings';
import ExtendedWindow from './extendedWindow';
Expand Down
2 changes: 1 addition & 1 deletion src/components/tilingsystem/tilingLayout.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { registerGObjectClass } from '@/utils/gjs';
import { Clutter, Mtk, Meta } from '@gi';
import { Clutter, Mtk, Meta } from '@gi.ext';
import TilePreview, {
TilePreviewConstructorProperties,
} from '../tilepreview/tilePreview';
Expand Down
2 changes: 1 addition & 1 deletion src/components/tilingsystem/tilingManager.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Clutter, Mtk, Meta, GLib } from '@gi';
import { Clutter, Mtk, Meta, GLib } from '@gi.ext';
import * as Main from 'resource:///org/gnome/shell/ui/main.js';
import { logger } from '@utils/logger';
import {
Expand Down
2 changes: 1 addition & 1 deletion src/components/tilingsystem/touchPointer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { buildRectangle } from '@utils/ui';
import { Mtk, Meta } from '@gi';
import { Mtk, Meta } from '@gi.ext';

export default class TouchPointer {
private static _instance: TouchPointer | null = null;
Expand Down
6 changes: 4 additions & 2 deletions src/components/windowBorderManager.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { GObject, Meta, St, Clutter } from '@gi';
import { GObject, Meta, St, Clutter } from '@gi.ext';
import SignalHandling from '@utils/signalHandling';
import { logger } from '@utils/logger';
import { registerGObjectClass } from '@utils/gjs';
Expand Down Expand Up @@ -27,6 +27,8 @@ class WindowBorder extends St.Bin {
this.trackWindow(win, true);

this.connect('destroy', () => {
this._bindings.forEach((b) => b.unbind());
this._bindings = [];
this._signals.disconnect();
});
}
Expand All @@ -36,7 +38,7 @@ class WindowBorder extends St.Bin {

this._bindings.forEach((b) => b.unbind());
this._bindings = [];
this._signals.disconnect(this._window);
this._signals.disconnect();
this._window = win;
this.close();
const winActor =
Expand Down
2 changes: 1 addition & 1 deletion src/components/windowManager/tilingShellWindowManager.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { registerGObjectClass } from '@utils/gjs';
import { logger } from '@utils/logger';
import SignalHandling from '@utils/signalHandling';
import { GObject, Meta } from '@gi';
import { GObject, Meta } from '@gi.ext';
import * as Main from 'resource:///org/gnome/shell/ui/main.js';

const debug = logger('TilingShellWindowManager');
Expand Down
2 changes: 1 addition & 1 deletion src/components/window_menu/layoutIcon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Tile from '@components/layout/Tile';
import SnapAssistTile from '@components/snapassist/snapAssistTile';
import { registerGObjectClass } from '@utils/gjs';
import { buildRectangle, getScalingFactorOf } from '@utils/ui';
import { Clutter, Mtk } from '@gi';
import { Clutter, Mtk } from '@gi.ext';

@registerGObjectClass
export default class LayoutIcon extends LayoutWidget<SnapAssistTile> {
Expand Down
2 changes: 1 addition & 1 deletion src/components/window_menu/layoutTileButtons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Layout from '@components/layout/Layout';
import LayoutWidget from '@components/layout/LayoutWidget';
import { registerGObjectClass } from '@utils/gjs';
import { buildMarginOf, buildRectangle, getScalingFactorOf } from '@utils/ui';
import { Clutter, Mtk } from '@gi';
import { Clutter, Mtk } from '@gi.ext';
import SnapAssistTileButton from '../snapassist/snapAssistTileButton';
import Tile from '@components/layout/Tile';

Expand Down
2 changes: 1 addition & 1 deletion src/components/window_menu/overriddenWindowMenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import * as windowMenu from 'resource:///org/gnome/shell/ui/windowMenu.js';
import * as PopupMenu from 'resource:///org/gnome/shell/ui/popupMenu.js';
import * as Main from 'resource:///org/gnome/shell/ui/main.js';
import { GObject, St, Clutter, Meta } from '@gi';
import { GObject, St, Clutter, Meta } from '@gi.ext';
import GlobalState from '@utils/globalState';
import Settings from '@settings/settings';
import { registerGObjectClass } from '@utils/gjs';
Expand Down
2 changes: 1 addition & 1 deletion src/dbus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const node = `<node>
</interface>
</node>`;

import { Gio } from '@gi';
import { Gio } from '@gi.ext';

export default class DBus {
private _dbus: Gio.DBusExportedObject | null;
Expand Down
2 changes: 1 addition & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import './styles/stylesheet.scss';

import { Gio, GLib, Meta } from '@gi.ext';
import { logger } from '@utils/logger';
import { getMonitors, squaredEuclideanDistance } from '@/utils/ui';
import * as Main from 'resource:///org/gnome/shell/ui/main.js';
import { TilingManager } from '@/components/tilingsystem/tilingManager';
import { Gio, GLib, Meta } from '@gi';
import Settings from '@settings/settings';
import SignalHandling from './utils/signalHandling';
import GlobalState from './utils/globalState';
Expand Down
4 changes: 1 addition & 3 deletions src/gi.ts → src/gi.ext.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { Gio, GLib, GObject } from '@gi.shared';
import Clutter from 'gi://Clutter';
import Gio from 'gi://Gio';
import GLib from 'gi://GLib';
import GObject from 'gi://GObject';
import Meta from 'gi://Meta';
import Mtk from 'gi://Mtk';
import Shell from 'gi://Shell';
Expand Down
6 changes: 6 additions & 0 deletions src/gi.prefs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Gio, GLib, GObject } from '@gi.shared';
import Gdk from 'gi://Gdk';
import Gtk from 'gi://Gtk'; // Starting from GNOME 40, the preferences dialog uses GTK4
import Adw from 'gi://Adw';

export { Adw, Gio, GLib, GObject, Gdk, Gtk };
9 changes: 9 additions & 0 deletions src/gi.shared.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// import this file from places that are imported by both prefs.js and extension.js
// ensuring you do not import GNOME Shell libraries in Preferences (Clutter, Meta, St or Shell)
// and you do not import GTK libraries in GNOME Shell (Gdk, Gtk or Adw)

import Gio from 'gi://Gio';
import GLib from 'gi://GLib';
import GObject from 'gi://GObject';

export { Gio, GLib, GObject };
2 changes: 1 addition & 1 deletion src/indicator/defaultMenu.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { GObject, St, Clutter, Gio } from '@gi';
import { GObject, St, Clutter, Gio } from '@gi.ext';
import SignalHandling from '@utils/signalHandling';
import Indicator from './indicator';
import * as Main from 'resource:///org/gnome/shell/ui/main.js';
Expand Down
2 changes: 1 addition & 1 deletion src/indicator/editingMenu.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { St } from '@gi';
import { St } from '@gi.ext';
import Indicator from './indicator';
import * as IndicatorUtils from './utils';
import * as PopupMenu from 'resource:///org/gnome/shell/ui/popupMenu.js';
Expand Down
2 changes: 1 addition & 1 deletion src/indicator/indicator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { St, Clutter, Shell, Gio } from '@gi';
import { St, Clutter, Shell, Gio } from '@gi.ext';
import * as Main from 'resource:///org/gnome/shell/ui/main.js';
import * as PanelMenu from 'resource:///org/gnome/shell/ui/panelMenu.js';
import Settings from '@settings/settings';
Expand Down
2 changes: 1 addition & 1 deletion src/indicator/layoutButton.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { St, Clutter, Mtk } from '@gi';
import { St, Clutter, Mtk } from '@gi.ext';
import LayoutWidget from '@/components/layout/LayoutWidget';
import SnapAssistTile from '@/components/snapassist/snapAssistTile';
import Layout from '@/components/layout/Layout';
Expand Down
2 changes: 1 addition & 1 deletion src/indicator/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { St, Clutter, Gio } from '@gi';
import { St, Clutter, Gio } from '@gi.ext';

export const createButton = (
iconName: string,
Expand Down
2 changes: 1 addition & 1 deletion src/keybindings.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as Main from 'resource:///org/gnome/shell/ui/main.js';
import { GObject, Meta, Gio, Shell, GLib } from '@gi';
import { GObject, Meta, Gio, Shell, GLib } from '@gi.ext';
import Settings from '@settings/settings';
import SettingsOverride from '@settings/settingsOverride';
import SignalHandling from '@utils/signalHandling';
Expand Down
6 changes: 0 additions & 6 deletions src/prefs.gi.ts

This file was deleted.

4 changes: 1 addition & 3 deletions src/prefs.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import Gtk from 'gi://Gtk'; // Starting from GNOME 40, the preferences dialog uses GTK4
import Adw from 'gi://Adw';
import { Gio, GLib, Gdk, GObject } from '@prefs.gi';
import { Gtk, Adw, Gio, GLib, Gdk, GObject } from '@gi.prefs';
import Settings, { ActivationKey } from './settings/settings';
import { logger } from './utils/logger';
import { ExtensionPreferences } from 'resource:///org/gnome/Shell/Extensions/js/extensions/prefs.js';
Expand Down
2 changes: 1 addition & 1 deletion src/settings/settings.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Gio, GObject, GLib } from '@prefs.gi';
import { Gio, GObject, GLib } from '@gi.shared';
import Layout from '../components/layout/Layout';
import Tile from '../components/layout/Tile';

Expand Down
2 changes: 1 addition & 1 deletion src/settings/settingsExport.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Gio, GLib } from '@prefs.gi';
import { Gio, GLib } from '@gi.prefs';
import Settings from '@settings/settings';
import SettingsOverride from '@settings/settingsOverride';

Expand Down
2 changes: 1 addition & 1 deletion src/settings/settingsOverride.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Settings from '@settings/settings';
import { Gio, GLib } from '@prefs.gi';
import { Gio, GLib } from '@gi.shared';

export default class SettingsOverride {
// map schema_id with map of keys and old values
Expand Down
2 changes: 1 addition & 1 deletion src/utils/gjs.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/ban-ts-comment */
/* eslint-disable @typescript-eslint/no-explicit-any */
import { GObject } from '@gi';
import { GObject } from '@gi.ext';

// Taken from https://github.com/material-shell/material-shell/blob/main/src/utils/gjs.ts
// Decorator function to call `GObject.registerClass` with the given class.
Expand Down
2 changes: 1 addition & 1 deletion src/utils/globalState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { registerGObjectClass } from '@utils/gjs';
import Layout from '../components/layout/Layout';
import Settings from '../settings/settings';
import SignalHandling from './signalHandling';
import { GObject, Meta, Gio } from '@gi';
import { GObject, Meta, Gio } from '@gi.ext';
import * as Main from 'resource:///org/gnome/shell/ui/main.js';
import { logger } from './logger';

Expand Down
Loading

0 comments on commit 2308549

Please sign in to comment.