aboutsummaryrefslogtreecommitdiff
path: root/.config/awesome/charitable/init.lua
blob: 8c0cc9b0a25e1a2d7096b7606a79c1e0fff36ed8 (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
-- @module sharetags
-- functions to share tags on multiple screens
local sharetags = {}

--{{{ Grab environment we need
local capi = { screen = screen, client = client }

local pairs = pairs
local ipairs = ipairs
local tag = require("awful.tag")
local awful = require("awful")

--}}}


--{{{ Functions

--{{{ create_tags: create a table of tags and bind them to screens
-- @param names : list to label the tags
-- @param layouts : list of layouts for the tags
-- @return table of tag objects
function sharetags.create_tags(names, layouts)
    local tags = {}
    local count = #names
    if capi.screen.count() >= #names then
        count = capi.screen.count() + 1
    end

    for tagnumber = 1, count do
        tags[tagnumber] = tag.add(names[tagnumber], {})
        tags[tagnumber].number = tagnumber

        awful.layout.set(layouts[tagnumber], tags[tagnumber])
    end
    return tags
end
--}}}

--{{{ sharetags.tag_move: move a tag to a screen
-- @param t : the tag object to move
-- @param screen_target : the screen object to move to
function sharetags.tag_move(t, target_screen)
    local ts = t or tag.selected()

    if not target_screen then return end

    local current_screen = ts.screen

    if current_screen and target_screen ~= current_screen then
        -- switch for tag
        local mynumber = ts.number

        -- sort tags
        local index = #target_screen.tags + 1
        for i, screen_tag in pairs(target_screen.tags) do
            local number = screen_tag.number
            if number ~= nil and mynumber < number then
                index = i
                break
            end
        end

        -- save curren_screen tags
        local selected_tags = current_screen.selected_tags

        ts.screen = target_screen
        ts.index = index

        -- restore curren_screen tag
        tag.viewmore(selected_tags, current_screen)


        -- switch for all clients on tag
        if #ts:clients() > 0 then
            for _, c in ipairs(ts:clients()) do
                if not c.sticky then
                    c.screen = target_screen
                    c:tags({ ts })

                    -- Fix maximized client if display sizes not equal
                    local is_maximized = c.maximized
                    if is_maximized then
                        c.maximized = false
                        c.maximized = true
                    end
                else
                    awful.client.toggletag(ts, c)
                end
            end
        end
    end
end
--}}}


-- Open tag on screen
function sharetags.select_tag(t, target_screen)
    local prev_focus = capi.client.focus;
    local tag_screen = t.screen
    local is_tag_select = t.selected;
    local is_tag_moved = target_screen ~= tag_screen


    if t.selected and target_screen ~= tag_screen and #tag_screen.selected_tags == 1 then
        sharetags.swap_screen(tag_screen, target_screen)
    else
        sharetags.tag_move(t, target_screen)
        t:view_only()
    end


    -- If there was a moving tag then the focus on the window is lost.  Checking
    -- if this is the same tag and thus restore focus on the window
    if is_tag_moved and is_tag_select and #t:clients() > 0 and prev_focus then
        capi.client.focus = prev_focus
    end
end

-- Toggle tag on screen
function sharetags.toggle_tag(t, screen)
    if t.screen ~= screen then
        sharetags.tag_move(t, screen)
        if not t.selected then
            tag.viewtoggle(t)
        end
    else
        tag.viewtoggle(t)
    end
end

-- Swap all selected tags between two screens
function sharetags.swap_screen(s1, s2)
    if #s1.selected_tags ~= 1 or #s2.selected_tags ~= 1 then
        print("can't swap multiple selected tags yet")
    end

    local t1 = s1.selected_tag
    local t2 = s2.selected_tag

    -- hide both tags in scratch space
    t1:swap(t2)

    -- Set selected in both screens to the correct count
    if #s1.selected_tags ~= 1 then
        for _, t in ipairs(s1.selected_tags) do
            t.selected = false
        end
    end

    if #s2.selected_tags ~= 1 then
        for _, t in ipairs(s2.selected_tags) do
            t.selected = false
        end
    end

    t1.selected = true
    t2.selected = true
end

return sharetags