make layout dependent on tags

This commit is contained in:
Xinhao Yuan 2019-07-12 18:04:39 -04:00
parent 2436da4c89
commit 40437871d3
5 changed files with 56 additions and 38 deletions

View File

@ -14,20 +14,17 @@ TL;DR --- I want the control of my layout.
Suppose this git is checked out at `~/.config/awesome/layout-machi` Suppose this git is checked out at `~/.config/awesome/layout-machi`
``` `machi = require("layout-machi")`
machi = require("layout-machi")
machi_editor = machi.editor.create()
machi_layout = machi.layout.create("default", machi_editor) The package provide a default layout `machi.default_layout` and editor `machi.default_editor`, which can be added into the layout list.
```
Then add the `machi_layout` in your tag layouts
## Use the layout ## Use the layout
Use `layout = machi.layout.create(name, editor)` to instantiate the layout with an editor object (see below on creating the editor). Use `layout = machi.layout.create(name, editor)` to instantiate the layout with an editor object.
`machi.default_editor` can be used, or see below on creating editors.
You can also create multiple layouts with different names and share the same editor. You can also create multiple layouts with different names and share the same editor.
The editor will restore the last setups of the layouts based on their names. The editor will restore the last setups of the layouts based on their names.
The layout will be independent on different tags.
## Editor ## Editor

View File

@ -88,7 +88,7 @@ end
-- @return whether any actions have been taken on the client -- @return whether any actions have been taken on the client
local function fit_region(c, cycle) local function fit_region(c, cycle)
local layout = api.layout.get(c.screen) local layout = api.layout.get(c.screen)
local regions = layout.machi_get_regions and layout.machi_get_regions(c.screen.workarea, c.screen) local regions = layout.machi_get_regions and layout.machi_get_regions(c.screen.workarea, c.screen.selected_tag.name)
if type(regions) ~= "table" or #regions < 1 then if type(regions) ~= "table" or #regions < 1 then
return false return false
end end
@ -467,6 +467,7 @@ local function create(data)
local function start_interactive(screen, layout) local function start_interactive(screen, layout)
screen = screen or api.screen.focused() screen = screen or api.screen.focused()
layout = layout or api.layout.get(screen) layout = layout or api.layout.get(screen)
local tag = screen.selected_tag
if layout.machi_set_cmd == nil then if layout.machi_set_cmd == nil then
api.naughty.notify({ api.naughty.notify({
@ -651,7 +652,7 @@ local function create(data)
end end
-- bring the current cmd to the front -- bring the current cmd to the front
data.cmds[#data.cmds + 1] = current_cmd data.cmds[#data.cmds + 1] = current_cmd
data.last_cmd[layout.machi_get_instance_name(screen)] = current_cmd data.last_cmd[layout.machi_get_instance_name(tag.name)] = current_cmd
if data.history_file then if data.history_file then
local file, err = io.open(data.history_file, "w") local file, err = io.open(data.history_file, "w")
@ -681,7 +682,7 @@ local function create(data)
if to_exit then if to_exit then
print("interactive layout editing ends") print("interactive layout editing ends")
if to_apply then if to_apply then
layout.machi_set_cmd(current_cmd, screen) layout.machi_set_cmd(current_cmd, tag.name)
api.layout.arrange(screen) api.layout.arrange(screen)
api.gears.timer{ api.gears.timer{
timeout = 1, timeout = 1,

View File

@ -1,5 +1,13 @@
local layout = require(... .. ".layout")
local editor = require(... .. ".editor")
local switcher = require(... .. ".switcher")
local default_editor = editor.create()
local default_layout = layout.create("default", default_editor)
return { return {
layout = require(... .. ".layout"), layout = layout,
editor = require(... .. ".editor"), editor = editor,
switcher = require(... .. ".switcher"), switcher = switcher,
default_editor = default_editor,
default_layout = default_layout,
} }

View File

@ -45,38 +45,50 @@ local function find_region(c, regions)
end end
local function create(name, editor) local function create(name, editor)
local priv = { local instances = {}
name = name,
editor = editor,
cmd = editor.get_last_cmd(name),
regions_cache = {}
}
local function get_instance_name(_screen) local function get_instance_name(suffix)
return name if suffix == nil then
end return name
else
local function get_regions(workarea, _screen) return name .. '+' .. suffix
if priv.cmd == nil then return {} end
local key = tostring(workarea.width) .. "x" .. tostring(workarea.height) .. "+" .. tostring(workarea.x) .. "+" .. tostring(workarea.y)
if priv.regions_cache[key] == nil then
priv.regions_cache[key] = priv.editor.run_cmd(workarea, priv.cmd)
end end
return priv.regions_cache[key]
end end
local function set_cmd(cmd, _screen) local function get_instance(suffix)
if priv.cmd ~= cmd then local instance_name = get_instance_name(suffix)
priv.cmd = cmd if instances[instance_name] == nil then
priv.regions_cache = {} instances[instance_name] = {
cmd = editor.get_last_cmd(instance_name),
regions_cache = {},
}
end
return instances[instance_name]
end
local function get_regions(workarea, suffix)
local instance = get_instance(suffix)
if instance.cmd == nil then return {} end
local key = tostring(workarea.width) .. "x" .. tostring(workarea.height) .. "+" .. tostring(workarea.x) .. "+" .. tostring(workarea.y)
if instance.regions_cache[key] == nil then
instance.regions_cache[key] = editor.run_cmd(workarea, instance.cmd)
end
return instance.regions_cache[key]
end
local function set_cmd(cmd, suffix)
local instance = get_instance(suffix)
if instance.cmd ~= cmd then
instance.cmd = cmd
instance.regions_cache = {}
end end
end end
local function arrange(p) local function arrange(p)
local wa = p.workarea local wa = p.workarea
local cls = p.clients local cls = p.clients
local regions = get_regions(wa, get_screen(p.screen)) local regions = get_regions(wa, get_screen(p.screen).selected_tag.name)
if #regions == 0 then return end if #regions == 0 then return end
@ -111,7 +123,7 @@ local function create(name, editor)
if context ~= "mouse.move" then return end if context ~= "mouse.move" then return end
local workarea = c.screen.workarea local workarea = c.screen.workarea
local regions = get_regions(workarea, c.screen) local regions = get_regions(workarea, c.screen.selected_tag.name)
if #regions == 0 then return end if #regions == 0 then return end

View File

@ -59,7 +59,7 @@ local function start(c)
local layout = api.layout.get(screen) local layout = api.layout.get(screen)
if c.floating or layout.machi_get_regions == nil then return end if c.floating or layout.machi_get_regions == nil then return end
local regions = layout.machi_get_regions(c.screen.workarea, c.screen) local regions = layout.machi_get_regions(c.screen.workarea, c.screen.selected_tag.name)
local infobox = api.wibox({ local infobox = api.wibox({
screen = screen, screen = screen,