Support screen objects in awful.tag

This commit makes the code in awful.tag work with screen objects.

Signed-off-by: Uli Schlachter <psychon@znc.in>
This commit is contained in:
Uli Schlachter 2016-02-26 20:07:26 +01:00
parent c17e331b92
commit fb5a98c765
1 changed files with 39 additions and 30 deletions

View File

@ -24,6 +24,10 @@ local capi =
root = root root = root
} }
local function get_screen(s)
return s and capi.screen[s]
end
-- we use require("awful.client") inside functions to prevent circular dependencies. -- we use require("awful.client") inside functions to prevent circular dependencies.
local client local client
@ -45,8 +49,8 @@ tag.history.limit = 20
-- selected tag is used. -- selected tag is used.
function tag.move(new_index, target_tag) function tag.move(new_index, target_tag)
target_tag = target_tag or tag.selected() target_tag = target_tag or tag.selected()
local scr = tag.getscreen(target_tag) local scr = get_screen(tag.getscreen(target_tag))
local tmp_tags = tag.gettags(scr) local tmp_tags = tag.gettags(scr and scr.index)
if (not new_index) or (new_index < 1) or (new_index > #tmp_tags) then if (not new_index) or (new_index < 1) or (new_index > #tmp_tags) then
return return
@ -94,7 +98,7 @@ function tag.add(name, props)
-- connected to property::activated to be called without a valid tag. -- connected to property::activated to be called without a valid tag.
-- set properties cannot be used as this has to be set before the first -- set properties cannot be used as this has to be set before the first
-- signal is sent -- signal is sent
properties.screen = properties.screen or ascreen.focused() properties.screen = get_screen(properties.screen or ascreen.focused())
-- Index is also required -- Index is also required
properties.index = (#tag.gettags(properties.screen))+1 properties.index = (#tag.gettags(properties.screen))+1
@ -119,7 +123,7 @@ end
-- @param layout The layout or layout table to set for this tags by default. -- @param layout The layout or layout table to set for this tags by default.
-- @return A table with all created tags. -- @return A table with all created tags.
function tag.new(names, screen, layout) function tag.new(names, screen, layout)
screen = screen or 1 screen = get_screen(screen or 1)
local tags = {} local tags = {}
for id, name in ipairs(names) do for id, name in ipairs(names) do
table.insert(tags, id, tag.add(name, {screen = screen, table.insert(tags, id, tag.add(name, {screen = screen,
@ -135,7 +139,7 @@ function tag.new(names, screen, layout)
end end
--- Find a suitable fallback tag. --- Find a suitable fallback tag.
-- @param screen The screen number to look for a tag on. [awful.screen.focused()] -- @param screen The screen to look for a tag on. [awful.screen.focused()]
-- @param invalids A table of tags we consider unacceptable. [selectedlist(scr)] -- @param invalids A table of tags we consider unacceptable. [selectedlist(scr)]
function tag.find_fallback(screen, invalids) function tag.find_fallback(screen, invalids)
local scr = screen or ascreen.focused() local scr = screen or ascreen.focused()
@ -159,7 +163,7 @@ function tag.delete(target_tag, fallback_tag)
target_tag = target_tag or tag.selected() target_tag = target_tag or tag.selected()
if target_tag == nil or target_tag.activated == false then return end if target_tag == nil or target_tag.activated == false then return end
local target_scr = tag.getscreen(target_tag) local target_scr = get_screen(tag.getscreen(target_tag))
local tags = tag.gettags(target_scr) local tags = tag.gettags(target_scr)
local idx = tag.getidx(target_tag) local idx = tag.getidx(target_tag)
local ntags = #tags local ntags = #tags
@ -214,7 +218,7 @@ end
--- Update the tag history. --- Update the tag history.
-- @param obj Screen object. -- @param obj Screen object.
function tag.history.update(obj) function tag.history.update(obj)
local s = obj.index local s = get_screen(obj.index)
local curtags = tag.selectedlist(s) local curtags = tag.selectedlist(s)
-- create history table -- create history table
if not data.history[s] then if not data.history[s] then
@ -252,12 +256,12 @@ function tag.history.update(obj)
end end
--- Revert tag history. --- Revert tag history.
-- @param screen The screen number. -- @param screen The screen.
-- @param idx Index in history. Defaults to "previous" which is a special index -- @param idx Index in history. Defaults to "previous" which is a special index
-- toggling between last two selected sets of tags. Number (eg 1) will go back -- toggling between last two selected sets of tags. Number (eg 1) will go back
-- to the given index in history. -- to the given index in history.
function tag.history.restore(screen, idx) function tag.history.restore(screen, idx)
local s = screen or ascreen.focused() local s = get_screen(screen or ascreen.focused())
local i = idx or "previous" local i = idx or "previous"
local sel = tag.selectedlist(s) local sel = tag.selectedlist(s)
-- do nothing if history empty -- do nothing if history empty
@ -281,16 +285,17 @@ function tag.history.restore(screen, idx)
-- remove the reverted history entry -- remove the reverted history entry
if i ~= "previous" then table.remove(data.history[s], i) end if i ~= "previous" then table.remove(data.history[s], i) end
capi.screen[s]:emit_signal("tag::history::update") s:emit_signal("tag::history::update")
end end
--- Get a list of all tags on a screen --- Get a list of all tags on a screen
-- @param s Screen number -- @param s Screen
-- @return A table with all available tags -- @return A table with all available tags
function tag.gettags(s) function tag.gettags(s)
s = get_screen(s)
local tags = {} local tags = {}
for _, t in ipairs(root.tags()) do for _, t in ipairs(root.tags()) do
if tag.getscreen(t) == s then if get_screen(tag.getscreen(t)) == s then
table.insert(tags, t) table.insert(tags, t)
end end
end end
@ -302,7 +307,7 @@ function tag.gettags(s)
end end
--- Set a tag's screen --- Set a tag's screen
-- @param s Screen number -- @param s Screen
-- @param t tag object -- @param t tag object
function tag.setscreen(s, t) function tag.setscreen(s, t)
@ -312,7 +317,7 @@ function tag.setscreen(s, t)
s, t = t, s s, t = t, s
end end
s = s or ascreen.focused() s = get_screen(s or ascreen.focused())
local sel = tag.selected local sel = tag.selected
local old_screen = tag.getproperty(t, "screen") local old_screen = tag.getproperty(t, "screen")
if s == old_screen then return end if s == old_screen then return end
@ -347,11 +352,12 @@ end
-- @return Screen number -- @return Screen number
function tag.getscreen(t) function tag.getscreen(t)
t = t or tag.selected() t = t or tag.selected()
return tag.getproperty(t, "screen") local prop = tag.getproperty(t, "screen")
return prop and prop.index
end end
--- Return a table with all visible tags --- Return a table with all visible tags
-- @param s Screen number. -- @param s Screen.
-- @return A table with all selected tags. -- @return A table with all selected tags.
function tag.selectedlist(s) function tag.selectedlist(s)
local screen = s or ascreen.focused() local screen = s or ascreen.focused()
@ -366,7 +372,7 @@ function tag.selectedlist(s)
end end
--- Return only the first visible tag. --- Return only the first visible tag.
-- @param s Screen number. -- @param s Screen.
function tag.selected(s) function tag.selected(s)
return tag.selectedlist(s)[1] return tag.selectedlist(s)[1]
end end
@ -517,7 +523,8 @@ end
function tag.incnmaster(add, t, sensible) function tag.incnmaster(add, t, sensible)
if sensible then if sensible then
client = client or require("awful.client") client = client or require("awful.client")
local ntiled = #client.tiled(tag.getscreen(t)) local screen = get_screen(tag.getscreen(t))
local ntiled = #client.tiled(screen and screen.index)
local nmaster = tag.getnmaster(t) local nmaster = tag.getnmaster(t)
if nmaster > ntiled then if nmaster > ntiled then
@ -575,7 +582,8 @@ end
function tag.incncol(add, t, sensible) function tag.incncol(add, t, sensible)
if sensible then if sensible then
client = client or require("awful.client") client = client or require("awful.client")
local ntiled = #client.tiled(tag.getscreen(t)) local screen = get_screen(tag.getscreen(t))
local ntiled = #client.tiled(screen and screen.index)
local nmaster = tag.getnmaster(t) local nmaster = tag.getnmaster(t)
local nsecondary = ntiled - nmaster local nsecondary = ntiled - nmaster
@ -595,7 +603,7 @@ function tag.incncol(add, t, sensible)
end end
--- View no tag. --- View no tag.
-- @tparam[opt] int screen The screen number. -- @tparam[opt] int|screen screen The screen.
function tag.viewnone(screen) function tag.viewnone(screen)
local tags = tag.gettags(screen or ascreen.focused()) local tags = tag.gettags(screen or ascreen.focused())
for _, t in pairs(tags) do for _, t in pairs(tags) do
@ -605,9 +613,9 @@ end
--- View a tag by its taglist index. --- View a tag by its taglist index.
-- @param i The relative index to see. -- @param i The relative index to see.
-- @param[opt] screen The screen number. -- @param[opt] screen The screen.
function tag.viewidx(i, screen) function tag.viewidx(i, screen)
screen = screen or ascreen.focused() screen = get_screen(screen or ascreen.focused())
local tags = tag.gettags(screen) local tags = tag.gettags(screen)
local showntags = {} local showntags = {}
for _, t in ipairs(tags) do for _, t in ipairs(tags) do
@ -622,7 +630,7 @@ function tag.viewidx(i, screen)
showntags[util.cycle(#showntags, k + i)].selected = true showntags[util.cycle(#showntags, k + i)].selected = true
end end
end end
capi.screen[screen]:emit_signal("tag::history::update") screen:emit_signal("tag::history::update")
end end
--- Get a tag's index in the gettags() table. --- Get a tag's index in the gettags() table.
@ -640,13 +648,13 @@ function tag.getidx(query_tag)
end end
--- View next tag. This is the same as tag.viewidx(1). --- View next tag. This is the same as tag.viewidx(1).
-- @param screen The screen number. -- @param screen The screen.
function tag.viewnext(screen) function tag.viewnext(screen)
return tag.viewidx(1, screen) return tag.viewidx(1, screen)
end end
--- View previous tag. This is the same a tag.viewidx(-1). --- View previous tag. This is the same a tag.viewidx(-1).
-- @param screen The screen number. -- @param screen The screen.
function tag.viewprev(screen) function tag.viewprev(screen)
return tag.viewidx(-1, screen) return tag.viewidx(-1, screen)
end end
@ -670,9 +678,9 @@ end
--- View only a set of tags. --- View only a set of tags.
-- @param tags A table with tags to view only. -- @param tags A table with tags to view only.
-- @param[opt] screen The screen number of the tags. -- @param[opt] screen The screen of the tags.
function tag.viewmore(tags, screen) function tag.viewmore(tags, screen)
screen = screen or ascreen.focused() screen = get_screen(screen or ascreen.focused())
local screen_tags = tag.gettags(screen) local screen_tags = tag.gettags(screen)
for _, _tag in ipairs(screen_tags) do for _, _tag in ipairs(screen_tags) do
if not util.table.hasitem(tags, _tag) then if not util.table.hasitem(tags, _tag) then
@ -682,7 +690,7 @@ function tag.viewmore(tags, screen)
for _, _tag in ipairs(tags) do for _, _tag in ipairs(tags) do
_tag.selected = true _tag.selected = true
end end
capi.screen[screen]:emit_signal("tag::history::update") screen:emit_signal("tag::history::update")
end end
--- Toggle selection of a tag --- Toggle selection of a tag
@ -736,7 +744,7 @@ end
function tag.withcurrent(c) function tag.withcurrent(c)
local tags = {} local tags = {}
for _, t in ipairs(c:tags()) do for _, t in ipairs(c:tags()) do
if tag.getscreen(t) == c.screen then if get_screen(tag.getscreen(t)) == get_screen(c.screen) then
table.insert(tags, t) table.insert(tags, t)
end end
end end
@ -752,8 +760,9 @@ function tag.withcurrent(c)
end end
local function attached_connect_signal_screen(screen, sig, func) local function attached_connect_signal_screen(screen, sig, func)
screen = get_screen(screen)
capi.tag.connect_signal(sig, function(_tag) capi.tag.connect_signal(sig, function(_tag)
if tag.getscreen(_tag) == screen then if get_screen(tag.getscreen(_tag)) == screen then
func(_tag) func(_tag)
end end
end) end)