summaryrefslogtreecommitdiff
path: root/awesome/lib/awful/mouse/drag_to_tag.lua
blob: 141456bc7f69c454b52d10e733de8df134f48e85 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
---------------------------------------------------------------------------
--- When the the mouse reach the end of the screen, then switch tag instead
--  of screens.
--
-- @author Julien Danjou <julien@danjou.info>
-- @copyright 2008 Julien Danjou
-- @submodule mouse
---------------------------------------------------------------------------

local capi = {screen = screen, mouse = mouse}
local util = require("awful.util")
local tag = require("awful.tag")
local resize = require("awful.mouse.resize")

local module = {}

function module.drag_to_tag(c)
    if (not c) or (not c.valid) then return end

    local coords = capi.mouse.coords()

    local dir = nil

    local wa = c.screen.workarea

    if coords.x >= wa.x + wa.width - 1 then
        capi.mouse.coords({ x = wa.x + 2 }, true)
        dir = "right"
    elseif coords.x <= wa.x + 1 then
        capi.mouse.coords({ x = wa.x + wa.width - 2 }, true)
        dir = "left"
    end

    local tags = c.screen.tags
    local t = c.screen.selected_tag
    local idx = t.index

    if dir then

        if dir == "right" then
            local newtag = tags[util.cycle(#tags, idx + 1)]
            c:move_to_tag(newtag)
            tag.viewnext()
        elseif dir == "left" then
            local newtag = tags[util.cycle(#tags, idx - 1)]
            c:move_to_tag(newtag)
            tag.viewprev()
        end
    end
end

resize.add_move_callback(function(c, _, _)
    if module.enabled then
        module.drag_to_tag(c)
    end
end, "mouse.move")

return setmetatable(module, {__call = function(_, ...) return module.drag_to_tag(...) end})