From 5ebc74fb9cf62118345e646d7b04e7a173890a38 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sun, 16 Jun 2019 15:36:03 +0200 Subject: Convert to a Spoon Convert the script to a Hammerspoon Spoon. Allows a user to set a custom binding to activate the mode. --- init.lua | 39 +++++++++++++++++++++++++++++++++++++++ window.lua | 54 +++++++++++++++++++++++++++++------------------------- 2 files changed, 68 insertions(+), 25 deletions(-) create mode 100644 init.lua diff --git a/init.lua b/init.lua new file mode 100644 index 0000000..b6654de --- /dev/null +++ b/init.lua @@ -0,0 +1,39 @@ +-- Copyright (c) 2019 Teddy Wing +-- +-- This program is free software: you can redistribute it and/or modify +-- it under the terms of the GNU General Public License as published by +-- the Free Software Foundation, either version 3 of the License, or +-- (at your option) any later version. +-- +-- This program is distributed in the hope that it will be useful, +-- but WITHOUT ANY WARRANTY; without even the implied warranty of +-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +-- GNU General Public License for more details. +-- +-- You should have received a copy of the GNU General Public License +-- along with this program. If not, see . + + +local obj = {} + +-- Metadata +obj.name = 'WindowMode' +obj.version = '0.0.1' +obj.author = 'Teddy Wing' +obj.license = 'GNU GPLv3+ (https://www.gnu.org/licenses/gpl-3.0.txt)' + +-- Internal function used to find our location, so we know where to load files from +local function script_path() + local str = debug.getinfo(2, "S").source:sub(2) + return str:match("(.*/)") +end +obj.spoonPath = script_path() + +local window = dofile(obj.spoonPath .. '/window.lua') + +function obj:bindHotkeys(keys) + local mode = keys['mode'] + window:bindModal(mode[1], mode[2]) +end + +return obj diff --git a/window.lua b/window.lua index cdb775e..d780877 100644 --- a/window.lua +++ b/window.lua @@ -14,7 +14,7 @@ -- along with this program. If not, see . -units = { +local units = { up_small = { x = 0, y = -5 }, right_small = { x = 5, y = 0 }, down_small = { x = 0, y = 5 }, @@ -40,7 +40,7 @@ units = { maximum = { x = 0.00, y = 0.00, w = 1.00, h = 1.00 } } -window = {} +local window = {} function window.up_small() hs.window.focusedWindow():move(units.up_small, nil, false, 0) end @@ -139,32 +139,36 @@ function window.move_to_top() end -window_mode = hs.hotkey.modal.new({'ctrl', 'option'}, 'w', 'Window') -window_mode:bind({'ctrl', 'option'}, 'w', 'Window Off', function() - window_mode:exit() -end) +function window:bindModal(modifiers, keycode) + window_mode = hs.hotkey.modal.new(modifiers, keycode, 'Window') + window_mode:bind(modifiers, keycode, 'Window Off', function() + window_mode:exit() + end) -window_mode:bind({'shift'}, 'k', window.up_small, nil, window.up_small) -window_mode:bind({'shift'}, 'l', window.right_small, nil, window.right_small) -window_mode:bind({'shift'}, 'j', window.down_small, nil, window.down_small) -window_mode:bind({'shift'}, 'h', window.left_small, nil, window.left_small) + window_mode:bind({'shift'}, 'k', window.up_small, nil, window.up_small) + window_mode:bind({'shift'}, 'l', window.right_small, nil, window.right_small) + window_mode:bind({'shift'}, 'j', window.down_small, nil, window.down_small) + window_mode:bind({'shift'}, 'h', window.left_small, nil, window.left_small) -window_mode:bind({}, 'k', window.up_med, nil, window.up_med) -window_mode:bind({}, 'l', window.right_med, nil, window.right_med) -window_mode:bind({}, 'j', window.down_med, nil, window.down_med) -window_mode:bind({}, 'h', window.left_med, nil, window.left_med) + window_mode:bind({}, 'k', window.up_med, nil, window.up_med) + window_mode:bind({}, 'l', window.right_med, nil, window.right_med) + window_mode:bind({}, 'j', window.down_med, nil, window.down_med) + window_mode:bind({}, 'h', window.left_med, nil, window.left_med) -window_mode:bind({}, 'i', window.move_to_top, nil, window.move_to_top) + window_mode:bind({}, 'i', window.move_to_top, nil, window.move_to_top) -window_mode:bind({}, ']', function() hs.window.focusedWindow():moveOneScreenEast(true, false, 0) end) -window_mode:bind({}, '[', function() hs.window.focusedWindow():moveOneScreenWest(true, false, 0) end) + window_mode:bind({}, ']', function() hs.window.focusedWindow():moveOneScreenEast(true, false, 0) end) + window_mode:bind({}, '[', function() hs.window.focusedWindow():moveOneScreenWest(true, false, 0) end) -window_mode:bind({'shift'}, 'e', window.decrease_up, nil, window.decrease_up) -window_mode:bind({'shift'}, 'd', window.increase_down, nil, window.increase_down) -window_mode:bind({'shift'}, 'f', window.increase_right, nil, window.increase_right) -window_mode:bind({'shift'}, 's', window.decrease_left, nil, window.decrease_left) + window_mode:bind({'shift'}, 'e', window.decrease_up, nil, window.decrease_up) + window_mode:bind({'shift'}, 'd', window.increase_down, nil, window.increase_down) + window_mode:bind({'shift'}, 'f', window.increase_right, nil, window.increase_right) + window_mode:bind({'shift'}, 's', window.decrease_left, nil, window.decrease_left) -window_mode:bind({}, 'e', window.decrease_up_med, nil, window.decrease_up_med) -window_mode:bind({}, 'd', window.increase_down_med, nil, window.increase_down_med) -window_mode:bind({}, 'f', window.increase_right_med, nil, window.increase_right_med) -window_mode:bind({}, 's', window.decrease_left_med, nil, window.decrease_left_med) + window_mode:bind({}, 'e', window.decrease_up_med, nil, window.decrease_up_med) + window_mode:bind({}, 'd', window.increase_down_med, nil, window.increase_down_med) + window_mode:bind({}, 'f', window.increase_right_med, nil, window.increase_right_med) + window_mode:bind({}, 's', window.decrease_left_med, nil, window.decrease_left_med) +end + +return window -- cgit v1.2.3