PaperWM/utils.js
2018-04-30 20:20:13 +02:00

150 lines
4.2 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const Extension = imports.misc.extensionUtils.extensions['paperwm@hedning:matrix.org']
var debug_all = false; // Turn off by default
var debug_filter = {};
function debug() {
let keyword = arguments[0];
let filter = debug_filter[keyword];
if (filter === false)
return;
if (debug_all || filter === true)
print(Array.prototype.join.call(arguments, " | "));
}
function print_stacktrace(error) {
let trace;
if (!error) {
trace = (new Error()).stack.split("\n")
// Remove _this_ frame
trace.splice(0, 1);
} else {
trace = error.stack.split("\n");
}
// Remove some uninteresting frames
let filtered = trace.filter((frame) => {
return frame !== "wrapper@resource:///org/gnome/gjs/modules/lang.js:178"
});
let args = [...arguments];
args.splice(0, 1, "stacktrace:"+(args[0] ? args[0] : ""))
// Use non-breaking space to encode new lines (otherwise every frame is
// prefixed by timestamp)
let nl = " ";
args.push(nl+filtered.join(nl))
debug.apply(null, args);
}
function framestr(rect) {
return "[ x:"+rect.x + ", y:" + rect.y + " w:" + rect.width + " h:"+rect.height + " ]";
}
/**
* Look up the function by name at call time. This makes it convenient to
* redefine the function without re-registering all signal handler, keybindings,
* etc. (this is like a function symbol in lisp)
*/
function dynamic_function_ref(handler_name, owner_obj) {
owner_obj = owner_obj || window;
return function() {
owner_obj[handler_name].apply(this, arguments);
}
}
/**
* Adapts a function operating on a meta_window to a key handler
*/
function as_key_handler(fn, owner_obj = window) {
if(typeof(fn) === "string") {
fn = dynamic_function_ref(fn, owner_obj);
}
return function(screen, monitor, meta_window, binding) {
return fn(meta_window);
}
}
function swap(array, i, j) {
let temp = array[i];
array[i] = array[j];
array[j] = temp;
}
function in_bounds(array, i) {
return i >= 0 && i < array.length;
}
//// Debug and development utils
const Tiling = Extension.imports.tiling;
function setDevGlobals() {
// Accept the risk of this interfering with existing code for now
metaWindow = global.display.focus_window;
meta_window = global.display.focus_window;
workspace = global.screen.get_active_workspace();
actor = metaWindow.get_compositor_private();
space = Tiling.spaces.spaceOfWindow(metaWindow);
}
/**
* Visualize the frame and buffer bounding boxes of a meta window
*/
function toggleWindowBoxes(metaWindow) {
metaWindow = metaWindow || global.display.focus_window;
if(metaWindow._paperDebugBoxes) {
metaWindow._paperDebugBoxes.forEach(box => {
box.destroy();
});
delete metaWindow._paperDebugBoxes;
return [];
}
let frame = metaWindow.get_frame_rect()
let inputFrame = metaWindow.get_buffer_rect()
let actor = metaWindow.get_compositor_private();
makeFrameBox = function({x, y, width, height}, color) {
let frameBox = new St.Widget();
frameBox.set_position(x, y)
frameBox.set_size(width, height)
frameBox.set_style("border: 2px" + color + " solid");
return frameBox;
}
let boxes = [];
boxes.push(makeFrameBox(frame, "red"));
boxes.push(makeFrameBox(inputFrame, "blue"));
if(inputFrame.x !== actor.x || inputFrame.y !== actor.y
|| inputFrame.width !== actor.width || inputFrame.height !== actor.height) {
boxes.push(makeFrameBox(actor, "yellow"));
}
boxes.forEach(box => global.stage.add_actor(box));
metaWindow._paperDebugBoxes = boxes;
return boxes;
}
function sum(array) {
return array.reduce((a, b) => a + b, 0);
}
function setWorkspaceName(name, workspace) {
let i;
if (workspace === undefined) {
i = global.screen.get_active_workspace_index();
} else {
i = workspace.index();
}
let settings = new Gio.Settings({ schema_id:
'org.gnome.desktop.wm.preferences'});
let names = settings.get_strv('workspace-names');
let oldName = names[i];
names[i] = name;
settings.set_strv('workspace-names', names);
return oldName;
}