Skip to content

Commit

Permalink
Ported workspace.js.
Browse files Browse the repository at this point in the history
  • Loading branch information
jtaala committed Sep 16, 2023
1 parent bc38265 commit ebbb5b8
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 48 deletions.
57 changes: 27 additions & 30 deletions virtTiling.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
const ExtensionUtils = imports.misc.extensionUtils;
const Extension = ExtensionUtils.getCurrentExtension();
const Utils = Extension.imports.utils;
const Tiling = Extension.imports.tiling;
import St from 'gi://St';

const St = imports.gi.St;
import { Utils, Tiling } from './imports.js';

let fitProportionally = Tiling.fitProportionally;
let prefs = {
Expand All @@ -13,7 +10,7 @@ let prefs = {

let virtStage = null;

function repl() {
export function repl() {
if (virtStage) {
virtStage.destroy();
}
Expand All @@ -31,7 +28,7 @@ function repl() {
width: monitorWidth * 3,
});

let monitorStyle = `background-color: blue;`
let monitorStyle = `background-color: blue;`;
let monitor = new St.Widget({
name: "monitor0",
style: monitorStyle,
Expand All @@ -53,10 +50,10 @@ function repl() {
y: panel.height,
width: monitor.width,
height: monitor.height - panel.height,
}
};

let tilingStyle = `background-color: rgba(190, 190, 0, 0.3);`
let tilingContainer = new St.Widget({name: "tiling", style: tilingStyle});
let tilingStyle = `background-color: rgba(190, 190, 0, 0.3);`;
let tilingContainer = new St.Widget({ name: "tiling", style: tilingStyle });

global.stage.add_actor(virtStage);
virtStage.x = 3000;
Expand All @@ -79,9 +76,9 @@ function repl() {
tilingContainer.x = space_.targetX * scale;
}

sync()
sync();

Utils.printActorTree(virtStage, Utils.mkFmt({nameOnly: true}));
Utils.printActorTree(virtStage, Utils.mkFmt({ nameOnly: true }));

movecolumntoviewportposition(tilingContainer, monitor, columns[1][0], 30);

Expand All @@ -95,27 +92,27 @@ function repl() {
w_m: window position (relative to monitor)
w_t: window position (relative to tiling)
*/
function t_s(m_s, w_m, w_t) {
export function t_s(m_s, w_m, w_t) {
return w_m - w_t + m_s;
}

/**
Calculates the tiling position such that column `k` is positioned at `x`
relative to the viewport (or workArea?)
*/
function movecolumntoviewportposition(tilingActor, viewport, window, x) {
export function movecolumntoviewportposition(tilingActor, viewport, window, x) {
tilingActor.x = t_s(viewport.x, x, window.x);
}

function renderAndView(container, columns) {
export function renderAndView(container, columns) {
for (let child of container.get_children()) {
child.destroy();
}

render(columns, container);
}

function fromSpace(space, scale = 1) {
export function fromSpace(space, scale = 1) {
return space.map(
col => col.map(
metaWindow => {
Expand All @@ -126,11 +123,11 @@ function fromSpace(space, scale = 1) {
};
}
)
)
);
}

/** Render a dummy view of the windows */
function render(columns, tiling) {
export function render(columns, tiling) {
const windowStyle = `border: black solid 1px; background-color: red`;

function createWindowActor(window) {
Expand All @@ -151,17 +148,17 @@ function render(columns, tiling) {
}
}

function allocateDefault(column, availableHeight, preAllocatedWindow) {
export function allocateDefault(column, availableHeight, preAllocatedWindow) {
if (column.length === 1) {
return [availableHeight];
} else {
// Distribute available height amongst non-selected windows in proportion to their existing height
const gap = prefs.window_gap;
const minHeight = 15;

function heightOf(window) {
const heightOf = window => {
return window.height;
}
};

const k = preAllocatedWindow && column.indexOf(preAllocatedWindow);
const selectedHeight = preAllocatedWindow && heightOf(preAllocatedWindow);
Expand Down Expand Up @@ -195,12 +192,12 @@ function allocateDefault(column, availableHeight, preAllocatedWindow) {
}
}

function allocateEqualHeight(column, available) {
export function allocateEqualHeight(column, available) {
available -= (column.length - 1) * prefs.window_gap;
return column.map(_ => Math.floor(available / column.length));
}

function layoutGrabColumn(column, x, y0, targetWidth, availableHeight, grabWindow) {
export function layoutGrabColumn(column, x, y0, targetWidth, availableHeight, grabWindow) {
function mosh(windows, height, y0) {
let targetHeights = fitProportionally(
windows.map(mw => mw.rect.height),
Expand All @@ -212,7 +209,7 @@ function layoutGrabColumn(column, x, y0, targetWidth, availableHeight, grabWindo

const k = column.indexOf(grabWindow);
if (k < 0) {
throw new Error("Anchor doesn't exist in column " + grabWindow.title);
throw new Error(`Anchor doesn't exist in column ${grabWindow.title}`);
}

const gap = prefs.window_gap;
Expand All @@ -224,13 +221,13 @@ function layoutGrabColumn(column, x, y0, targetWidth, availableHeight, grabWindo
const H2 = availableHeight - (yGrabRel + f.height - y0) - gap - (column.length - k - 2) * gap;
k > 0 && mosh(column.slice(0, k), H1, y0);
let y = mosh(column.slice(k, k + 1), f.height, yGrabRel);
k+1 < column.length && mosh(column.slice(k + 1), H2, y);
k + 1 < column.length && mosh(column.slice(k + 1), H2, y);

return targetWidth;
}


function layoutColumnSimple(windows, x, y0, targetWidth, targetHeights, time) {
export function layoutColumnSimple(windows, x, y0, targetWidth, targetHeights, time) {
let y = y0;

for (let i = 0; i < windows.length; i++) {
Expand All @@ -251,19 +248,19 @@ function layoutColumnSimple(windows, x, y0, targetWidth, targetHeights, time) {
/**
Mutates columns
*/
function layout(columns, workArea, prefs, options = {}) {
export function layout(columns, workArea, prefs, options = {}) {
let gap = prefs.window_gap;
let availableHeight = workArea.height;

let {inGrab, selectedWindow} = options;
let { inGrab, selectedWindow } = options;
let selectedIndex = -1;

if (selectedWindow) {
selectedIndex = columns.findIndex(col => col.includes(selectedWindow));
}

let y0 = workArea.y
let x = 0
let y0 = workArea.y;
let x = 0;

for (let i = 0; i < columns.length; i++) {
let column = columns[i];
Expand Down
36 changes: 18 additions & 18 deletions workspace.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const ExtensionUtils = imports.misc.extensionUtils;
const Extension = ExtensionUtils.getCurrentExtension();
const Lib = Extension.imports.lib;
const { Gio, GLib } = imports.gi;
import Gio from 'gi://Gio';
import GLib from 'gi://GLib';

import Lib from './imports.js';

/**
* Workspace related utility functions used by other modules.
Expand All @@ -11,11 +11,11 @@ const WORKSPACE_KEY = 'org.gnome.shell.extensions.paperwm.workspace';
let workspaceSettingsCache;

let schemaSource, workspaceList;
function enable() {
export function enable(extension) {
workspaceSettingsCache = {};

schemaSource = Gio.SettingsSchemaSource.new_from_directory(
GLib.build_filenamev([Extension.path, "schemas"]),
GLib.build_filenamev([extension.path, "schemas"]),
Gio.SettingsSchemaSource.get_default(),
false
);
Expand All @@ -25,36 +25,36 @@ function enable() {
});
}

function disable() {
export function disable() {
workspaceSettingsCache = null;
schemaSource = null;
workspaceList = null;
}

function getSchemaSource() {
export function getSchemaSource() {
return schemaSource;
}

function getWorkspaceName(settings, index) {
export function getWorkspaceName(settings, index) {
let name = settings.get_string('name') ?? `Workspace ${index + 1}`;
if (!name || name === '') {
name = `Workspace ${index + 1}`;
}
return name;
}

function getWorkspaceList() {
export function getWorkspaceList() {
return workspaceList;
}

/**
* Returns list of ordered workspace UUIDs.
*/
function getListUUID() {
export function getListUUID() {
return getWorkspaceList().get_strv('list');
}

function getWorkspaceSettings(index) {
export function getWorkspaceSettings(index) {
let list = getListUUID();
for (let uuid of list) {
let settings = getWorkspaceSettingsByUUID(uuid);
Expand All @@ -65,7 +65,7 @@ function getWorkspaceSettings(index) {
return getNewWorkspaceSettings(index);
}

function getNewWorkspaceSettings(index) {
export function getNewWorkspaceSettings(index) {
let uuid = GLib.uuid_string_random();
let settings = getWorkspaceSettingsByUUID(uuid);
let list = getListUUID();
Expand All @@ -75,7 +75,7 @@ function getNewWorkspaceSettings(index) {
return [uuid, settings];
}

function getWorkspaceSettingsByUUID(uuid) {
export function getWorkspaceSettingsByUUID(uuid) {
if (!workspaceSettingsCache[uuid]) {
let settings = new Gio.Settings({
settings_schema: getSchemaSource().lookup(WORKSPACE_KEY, true),
Expand All @@ -87,15 +87,15 @@ function getWorkspaceSettingsByUUID(uuid) {
}

/** Returns [[uuid, settings, name], ...] (Only used for debugging/development atm.) */
function findWorkspaceSettingsByName(regex) {
export function findWorkspaceSettingsByName(regex) {
let list = getListUUID();
let settings = list.map(getWorkspaceSettingsByUUID);
return Lib.zip(list, settings, settings.map(s => s.get_string('name')))
.filter(([uuid, s, name]) => name.match(regex));
}

/** Only used for debugging/development atm. */
function deleteWorkspaceSettingsByName(regex, dryrun = true) {
export function deleteWorkspaceSettingsByName(regex, dryrun = true) {
let out = "";
function rprint(...args) { console.debug(...args); out += `${args.join(" ")}\n`; }
let n = global.workspace_manager.get_n_workspaces();
Expand All @@ -114,7 +114,7 @@ function deleteWorkspaceSettingsByName(regex, dryrun = true) {
}

/** Only used for debugging/development atm. */
function deleteWorkspaceSettings(uuid) {
export function deleteWorkspaceSettings(uuid) {
// NB! Does not check if the settings is currently in use. Does not reindex subsequent settings.
let list = getListUUID();
let i = list.indexOf(uuid);
Expand All @@ -129,7 +129,7 @@ function deleteWorkspaceSettings(uuid) {
}

// Useful for debugging
function printWorkspaceSettings() {
export function printWorkspaceSettings() {
let list = getListUUID();
let settings = list.map(getWorkspaceSettingsByUUID);
let zipped = Lib.zip(list, settings);
Expand Down

0 comments on commit ebbb5b8

Please sign in to comment.