From 8a7ddc006b3a78d3b3bc203012aeefcacd15b2d9 Mon Sep 17 00:00:00 2001 From: Drauthius Date: Wed, 5 Apr 2017 19:44:15 +0200 Subject: [PATCH] Tags on a removed screen will be moved to the primary screen. When disconnecting a screen, it would previously leave the tags on the old screen, throwing errors when trying to move them to an existing screen. This change makes sure that the tags are moved over to an existing screen when the screen is removed. It doesn't make sense to allow tags without screens, since all clients (programs) that were on the old tags will be moved to another tag on an existing screen anyway. awful.screen.connect_for_each_screen(func) can be used to restore tags to a screen when it is connected again. Closes #2. --- init.lua | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/init.lua b/init.lua index 40868ef..e2c5d11 100644 --- a/init.lua +++ b/init.lua @@ -6,6 +6,9 @@ -- Grab environment we need local awful = require("awful") +local capi = { + screen = screen +} local sharedtags = { _VERSION = "sharedtags v1.0.0 for v4.0", @@ -36,6 +39,35 @@ local sharedtags = { ]] } +-- Add a signal for each new screen, which just listens for the remove +-- event, and moves over all tags when it happens. +awful.screen.connect_for_each_screen(function(s) + -- When the screen is removed, all tags need to be moved over to an existing + -- screen. If they are not, accessing the tags will result in an error. It + -- doesn't make sense to fix the error, since clients on the now-hidden tags + -- will automatically be moved to a tag on a visible screen. + s:connect_signal("removed",function() + -- The screen to move the orphaned tags to. + local newscreen = capi.screen.primary + -- The currently selected tags on that screen. + local seltags = newscreen.selected_tags + + -- Move over all tags to an existing screen. + for _,tag in ipairs(s.tags) do + sharedtags.movetag(tag, newscreen) + end + + -- Restore the viewed tags on the new screen. + for i,tag in ipairs(seltags) do + if i == 1 then + tag:view_only() + else + awful.tag.viewtoggle(tag) + end + end + end) +end) + --- Create new tag objects. -- The first tag defined for each screen will be automatically selected. -- @tparam table def A list of tables with the optional keys `name`, `layout` @@ -60,7 +92,7 @@ function sharedtags.new(def) for i,t in ipairs(def) do tags[i] = awful.tag.add(t.name or i, { - screen = t.screen or awful.screen.primary, + screen = (t.screen and t.screen <= capi.screen.count()) and t.screen or capi.screen.primary, layout = t.layout, sharedtagindex = i })