Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exit fullscreen before every action #27

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
58 changes: 36 additions & 22 deletions MiroWindowsManager.spoon/init.lua
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
-- Copyright (c) 2018 Miro Mannino
-- Permission is hereby granted, free of charge, to any person obtaining a copy of this
-- software and associated documentation files (the "Software"), to deal in the Software
-- Permission is hereby granted, free of charge, to any person obtaining a copy of this
-- software and associated documentation files (the "Software"), to deal in the Software
-- without restriction, including without limitation the rights to use, copy, modify, merge,
-- publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
-- to whom the Software is furnished to do so, subject to the following conditions:
--
-- The above copyright notice and this permission notice shall be included in all copies
-- or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
-- INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
-- INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
-- PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
-- FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
-- OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
-- FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
-- OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
-- DEALINGS IN THE SOFTWARE.

--- === MiroWindowsManager ===
---
--- With this script you will be able to move the window in halves and in corners using your keyboard and mainly using arrows. You would also be able to resize them by thirds, quarters, or halves.
---
---
--- Official homepage for more info and documentation: [https://github.com/miromannino/miro-windows-manager](https://github.com/miromannino/miro-windows-manager)
---
--- Download: [https://github.com/miromannino/miro-windows-manager/raw/master/MiroWindowsManager.spoon.zip](https://github.com/miromannino/miro-windows-manager/raw/master/MiroWindowsManager.spoon.zip)
Expand All @@ -36,14 +36,14 @@ obj.license = "MIT - https://opensource.org/licenses/MIT"

--- MiroWindowsManager.sizes
--- Variable
--- The sizes that the window can have.
--- The sizes that the window can have.
--- The sizes are expressed as dividend of the entire screen's size.
--- For example `{2, 3, 3/2}` means that it can be 1/2, 1/3 and 2/3 of the total screen's size
obj.sizes = {2, 3, 3/2}

--- MiroWindowsManager.fullScreenSizes
--- Variable
--- The sizes that the window can have in full-screen.
--- The sizes that the window can have in full-screen.
--- The sizes are expressed as dividend of the entire screen's size.
--- For example `{1, 4/3, 2}` means that it can be 1/1 (hence full screen), 3/4 and 1/2 of the total screen's size
obj.fullScreenSizes = {1, 4/3, 2}
Expand All @@ -61,12 +61,20 @@ obj._pressed = {
right = false
}

function obj:_exitFullScreen(win)
local win = win or hs.window.focusedWindow()
if win:isFullScreen() then
win:setFullScreen(false)
end
end

function obj:_nextStep(dim, offs, cb)
if hs.window.focusedWindow() then
local win = hs.window.frontmostWindow()
self:_exitFullScreen(win)
local axis = dim == 'w' and 'x' or 'y'
local oppDim = dim == 'w' and 'h' or 'w'
local oppAxis = dim == 'w' and 'y' or 'x'
local win = hs.window.frontmostWindow()
local id = win:id()
local screen = win:screen()

Expand Down Expand Up @@ -95,14 +103,15 @@ end
function obj:_nextFullScreenStep()
if hs.window.focusedWindow() then
local win = hs.window.frontmostWindow()
self:_exitFullScreen(win)
local id = win:id()
local screen = win:screen()

cell = hs.grid.get(win, screen)

local nextSize = self.fullScreenSizes[1]
for i=1,#self.fullScreenSizes do
if cell.w == self.GRID.w / self.fullScreenSizes[i] and
if cell.w == self.GRID.w / self.fullScreenSizes[i] and
cell.h == self.GRID.h / self.fullScreenSizes[i] and
cell.x == (self.GRID.w - self.GRID.w / self.fullScreenSizes[i]) / 2 and
cell.y == (self.GRID.h - self.GRID.h / self.fullScreenSizes[i]) / 2 then
Expand All @@ -125,21 +134,26 @@ function obj:_moveNextScreenStep()
local win = hs.window.frontmostWindow()
local id = win:id()
local screen = win:screen()

win:move(win:frame():toUnitRect(screen:frame()), screen:next(), true, 0)
if win:isFullScreen() then
win:setFullScreen(false)
win:move(win:frame():toUnitRect(screen:frame()), screen:next(), true, 1)
else
win:move(win:frame():toUnitRect(screen:frame()), screen:next(), true, 0)
end
end
end

function obj:_fullDimension(dim)
if hs.window.focusedWindow() then
local win = hs.window.frontmostWindow()
self:_exitFullScreen(win)
local id = win:id()
local screen = win:screen()
cell = hs.grid.get(win, screen)

if (dim == 'x') then
cell = '0,0 ' .. self.GRID.w .. 'x' .. self.GRID.h
else
else
cell[dim] = self.GRID[dim]
cell[dim == 'w' and 'x' or 'y'] = 0
end
Expand Down Expand Up @@ -178,57 +192,57 @@ function obj:bindHotkeys(mapping)

hs.hotkey.bind(mapping.down[1], mapping.down[2], function ()
self._pressed.down = true
if self._pressed.up then
if self._pressed.up then
self:_fullDimension('h')
else
self:_nextStep('h', true, function (cell, nextSize)
cell.y = self.GRID.h - self.GRID.h / nextSize
cell.h = self.GRID.h / nextSize
end)
end
end, function ()
end, function ()
self._pressed.down = false
end)

hs.hotkey.bind(mapping.right[1], mapping.right[2], function ()
self._pressed.right = true
if self._pressed.left then
if self._pressed.left then
self:_fullDimension('w')
else
self:_nextStep('w', true, function (cell, nextSize)
cell.x = self.GRID.w - self.GRID.w / nextSize
cell.w = self.GRID.w / nextSize
end)
end
end, function ()
end, function ()
self._pressed.right = false
end)

hs.hotkey.bind(mapping.left[1], mapping.left[2], function ()
self._pressed.left = true
if self._pressed.right then
if self._pressed.right then
self:_fullDimension('w')
else
self:_nextStep('w', false, function (cell, nextSize)
cell.x = 0
cell.w = self.GRID.w / nextSize
end)
end
end, function ()
end, function ()
self._pressed.left = false
end)

hs.hotkey.bind(mapping.up[1], mapping.up[2], function ()
self._pressed.up = true
if self._pressed.down then
if self._pressed.down then
self:_fullDimension('h')
else
self:_nextStep('h', false, function (cell, nextSize)
cell.y = 0
cell.h = self.GRID.h / nextSize
end)
end
end, function ()
end, function ()
self._pressed.up = false
end)

Expand Down