From e002d02e500a3bdd9d563cdb47e78d72c3c90bcd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ole=20J=C3=B8rgen=20Br=C3=B8nner?= Date: Thu, 21 Sep 2017 16:01:21 +0200 Subject: [PATCH] Move utils to separate file --- extension.js | 1 + tiling.js | 78 +++------------------------------------------------- utils.js | 70 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+), 74 deletions(-) create mode 100644 utils.js diff --git a/extension.js b/extension.js index 4d5d078..a0480d8 100644 --- a/extension.js +++ b/extension.js @@ -1,5 +1,6 @@ const Extension = imports.misc.extensionUtils.getCurrentExtension(); const Tiling = Extension.imports.tiling; +const utils = Extension.utils; const Gio = imports.gi.Gio; const Meta = imports.gi.Meta; const Main = imports.ui.main; diff --git a/tiling.js b/tiling.js index 95b548d..33b54a7 100644 --- a/tiling.js +++ b/tiling.js @@ -1,5 +1,6 @@ // Globals +const Extension = imports.misc.extensionUtils.getCurrentExtension(); const GLib = imports.gi.GLib; const Tweener = imports.ui.tweener; const Lang = imports.lang; @@ -7,6 +8,7 @@ const Meta = imports.gi.Meta; const Main = imports.ui.main; const Shell = imports.gi.Shell; const Gio = imports.gi.Gio; +const utils = Extension.imports.utils; // Gap between windows window_gap = 10 @@ -51,35 +53,6 @@ spaces.spaceOf = (meta_window) => { } window.spaces = spaces; -debug_all = true; // Consider the default value in `debug_filter` to be true -debug_filter = { "#preview": false }; -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, " | ")); -} - -print_stacktrace = () => { - let trace = (new Error()).stack.split("\n") - // Remove _this_ frame - trace.splice(0, 1); - // Remove some uninteresting frames - let filtered = trace.filter((frame) => { - return frame !== "wrapper@resource:///org/gnome/gjs/modules/lang.js:178" - }); - let args = Array.prototype.splice.call(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); -} - - focus = () => { let meta_window = global.display.focus_window; if (!meta_window) @@ -128,10 +101,6 @@ move = (meta_window, x, y, onComplete, onStart, delay, transition) => { } -timestamp = () => { - return GLib.get_monotonic_time()/1000 -} - ensuring = false; ensure_viewport = (meta_window, force) => { if (ensuring == meta_window && !force) { @@ -218,10 +187,6 @@ ensure_viewport = (meta_window, force) => { move_to(meta_window, x, y, delay, transition); } -framestr = (rect) => { - return "[ x:"+rect.x + ", y:" + rect.y + " w:" + rect.width + " h:"+rect.height + " ]"; -} - focus_handler = (meta_window, user_data) => { debug("focus:", meta_window.title, framestr(meta_window.get_frame_rect())); @@ -459,30 +424,6 @@ add_all_from_workspace = (workspace) => { }) } -/** - * 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) - */ -dynamic_function_ref = (handler_name, owner_obj) => { - owner_obj = owner_obj || window; - return function() { - owner_obj[handler_name].apply(owner_obj, arguments); - } -} - -/** - * Adapts a function operating on a meta_window to a key handler - */ -as_key_handler = function(fn) { - if(typeof(fn) === "string") { - fn = dynamic_function_ref(fn); - } - return function(screen, monitor, meta_window, binding) { - return fn(meta_window); - } -} - first_frame = (meta_window_actor) => { meta_window_actor.disconnect('first_frame'); let meta_window = meta_window_actor.meta_window; @@ -533,23 +474,12 @@ previous = () => { spaces[meta_window.get_workspace().workspace_index][focus()-1].activate(timestamp) } -util = { - swap: function(array, i, j) { - let temp = array[i]; - array[i] = array[j]; - array[j] = temp; - }, - in_bounds: function(array, i) { - return i >= 0 && i < array.length; - } -}; - move_helper = (meta_window, delta) => { // NB: delta should be 1 or -1 let ws = spaces[meta_window.get_workspace().workspace_index] let i = ws.indexOf(meta_window) - if(util.in_bounds(ws, i+delta)) { - util.swap(ws, i, i+delta); + if(utils.in_bounds(ws, i+delta)) { + utils.swap(ws, i, i+delta); ensure_viewport(meta_window, true); } } diff --git a/utils.js b/utils.js new file mode 100644 index 0000000..d86f977 --- /dev/null +++ b/utils.js @@ -0,0 +1,70 @@ + +debug_all = true; // Consider the default value in `debug_filter` to be true +debug_filter = { "#preview": false }; +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, " | ")); +} + +print_stacktrace = () => { + let trace = (new Error()).stack.split("\n") + // Remove _this_ frame + trace.splice(0, 1); + // Remove some uninteresting frames + let filtered = trace.filter((frame) => { + return frame !== "wrapper@resource:///org/gnome/gjs/modules/lang.js:178" + }); + let args = Array.prototype.splice.call(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); +} + +framestr = (rect) => { + return "[ x:"+rect.x + ", y:" + rect.y + " w:" + rect.width + " h:"+rect.height + " ]"; +} + +timestamp = () => { + return GLib.get_monotonic_time()/1000 +} + +/** + * 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) + */ +dynamic_function_ref = (handler_name, owner_obj) => { + owner_obj = owner_obj || window; + return function() { + owner_obj[handler_name].apply(owner_obj, arguments); + } +} + +/** + * Adapts a function operating on a meta_window to a key handler + */ +as_key_handler = function(fn) { + if(typeof(fn) === "string") { + fn = dynamic_function_ref(fn); + } + 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; +} +