Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 21 additions & 12 deletions tiling-assistant@leleat-on-github/extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,15 @@ export default class TilingAssistantExtension extends Extension {

this._twm = twmModule.TilingWindowManager;
this._twm.initialize();
await Util.initialize(cancellable);

try {
await Util.initialize(cancellable);
} catch (e) {
if (e.matches(Gio.IOErrorEnum, Gio.IOErrorEnum.CANCELLED))
return;
Comment thread
3v1n0 marked this conversation as resolved.
Resolved

throw e;
}

this._moveHandler = new MoveHandler();
this._resizeHandler = new ResizeHandler();
Expand Down Expand Up @@ -237,28 +245,28 @@ export default class TilingAssistantExtension extends Extension {
this._cancellable?.cancel();
this._cancellable = null;

this._settingsOverrider.destroy();
this._settingsOverrider?.destroy();
this._settingsOverrider = null;
this._moveHandler.destroy();
this._moveHandler?.destroy();
this._moveHandler = null;
this._resizeHandler.destroy();
this._resizeHandler?.destroy();
this._resizeHandler = null;
this._keybindingHandler.destroy();
this._keybindingHandler?.destroy();
this._keybindingHandler = null;
this._layoutsManager.destroy();
this._layoutsManager?.destroy();
this._layoutsManager = null;
this._focusHintManager.destroy();
this._focusHintManager?.destroy();
this._focusHintManager = null;

this._altTabOverride.destroy();
this._altTabOverride?.destroy();
this._altTabOverride = null;

this._twm.destroy();
this._twm?.destroy();
this._twm = null;

Util.destroy();

this.settings.destroy();
this.settings?.destroy();
this.settings = null;

// Restore old functions.
Expand Down Expand Up @@ -330,13 +338,14 @@ export default class TilingAssistantExtension extends Extension {
const userPath = GLib.get_user_config_dir();
const path = GLib.build_filenamev([userPath, '/tiling-assistant/tiledSessionRestore2.json']);
const file = Gio.File.new_for_path(path);
if (!file.query_exists(null))
return;

let contents;
try {
[contents] = await file.load_contents_async(cancellable);
} catch (e) {
if (e.matches(Gio.IOErrorEnum, Gio.IOErrorEnum.NOT_FOUND))
return;

if (!e.matches(Gio.IOErrorEnum, Gio.IOErrorEnum.CANCELLED))
logError(e);
return;
Expand Down
73 changes: 53 additions & 20 deletions tiling-assistant@leleat-on-github/src/extension/utility.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Config, Main } from '../dependencies/shell.js';
import { Direction, Orientation, Settings } from '../common.js';

Gio._promisify(Gio.File.prototype, 'load_contents_async');
Gio._promisify(Gio.File.prototype, 'make_directory_async');
Gio._promisify(Gio.File.prototype, 'query_info_async');

const ShellVerison = Config.PACKAGE_VERSION.split('.').map(Number.parseInt);
Expand Down Expand Up @@ -128,6 +129,56 @@ export class Util {
return global.get_pointer()[2] & modMask;
}

/**
* Creates `dir` and all its missing parent directories, asynchronously.
*
* There is no asynchronous make_directory_with_parents(), so walk up to
* the first existing ancestor and create the missing directories top-down.
*
* @param {Gio.File} dir the directory to create.
* @param {?Gio.Cancellable} [cancellable] a cancellable to cancel the
* operation with.
* @param {number} [priority] the IO priority of the operation.
* @returns {Promise<void>}
*/
static async maybeCreateDirectory(dir, cancellable = null, priority = GLib.PRIORITY_DEFAULT) {
try {
await dir.make_directory_async(priority, cancellable);
return;
} catch (e) {
if (e.matches(Gio.IOErrorEnum, Gio.IOErrorEnum.EXISTS))
return;
if (!e.matches(Gio.IOErrorEnum, Gio.IOErrorEnum.NOT_FOUND))
throw e;
}

const missingDirs = [dir];
for (let parent = dir.get_parent(); parent; parent = parent.get_parent()) {
try {
// eslint-disable-next-line no-await-in-loop
await parent.make_directory_async(priority, cancellable);
} catch (e) {
if (e.matches(Gio.IOErrorEnum, Gio.IOErrorEnum.EXISTS))
break;
else if (e.matches(Gio.IOErrorEnum, Gio.IOErrorEnum.NOT_FOUND))
missingDirs.unshift(parent);
else
throw e;
}
}

// Sadly we must be sequential here, so we can't use Promise.all
for (const missingDir of missingDirs) {
try {
// eslint-disable-next-line no-await-in-loop
await missingDir.make_directory_async(priority, cancellable);
} catch (e) {
if (!e.matches(Gio.IOErrorEnum, Gio.IOErrorEnum.EXISTS))
throw e;
}
}
}

/**
* Loads the layouts from disk and keeps them in memory, refreshing them
* whenever the layouts file changes. This avoids reading the file
Expand All @@ -138,25 +189,7 @@ export class Util {
const dir = Gio.File.new_for_path(
GLib.build_filenamev([userDir, 'tiling-assistant']));

try {
await dir.query_info_async(
Gio.FILE_ATTRIBUTE_STANDARD_TYPE,
Gio.FileQueryInfoFlags.NONE,
GLib.PRIORITY_DEFAULT,
cancellable
);
} catch (e) {
if (e.matches(Gio.IOErrorEnum, Gio.IOErrorEnum.NOT_FOUND)) {
try {
dir.make_directory_with_parents(cancellable);
} catch (createError) {
if (createError.code !== Gio.IOErrorEnum.EXISTS)
logError(createError);
}
} else {
logError(e);
}
}
await this.maybeCreateDirectory(dir, cancellable);

this._layoutsFile = dir.get_child('layouts.json');
this._layoutsMonitor = dir.monitor_directory(
Expand Down Expand Up @@ -282,7 +315,7 @@ export class Util {
}

static is_wayland_compositor() {
return Meta.is_wayland_compositor === undefined || Meta.is_wayland_compositor();
return Meta.is_wayland_compositor?.() ?? true;
}

/**
Expand Down
Loading