diff --git a/README.md b/README.md index 5e37f39..fcfb3fa 100644 --- a/README.md +++ b/README.md @@ -14,20 +14,17 @@ TL;DR --- I want the control of my layout. Suppose this git is checked out at `~/.config/awesome/layout-machi` -``` -machi = require("layout-machi") -machi_editor = machi.editor.create() +`machi = require("layout-machi")` -machi_layout = machi.layout.create("default", machi_editor) -``` - -Then add the `machi_layout` in your tag layouts +The package provide a default layout `machi.default_layout` and editor `machi.default_editor`, which can be added into the layout list. ## 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. The editor will restore the last setups of the layouts based on their names. +The layout will be independent on different tags. ## Editor diff --git a/editor.lua b/editor.lua index ac1931d..c9136c6 100644 --- a/editor.lua +++ b/editor.lua @@ -88,7 +88,7 @@ end -- @return whether any actions have been taken on the client local function fit_region(c, cycle) 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 return false end @@ -467,6 +467,7 @@ local function create(data) local function start_interactive(screen, layout) screen = screen or api.screen.focused() layout = layout or api.layout.get(screen) + local tag = screen.selected_tag if layout.machi_set_cmd == nil then api.naughty.notify({ @@ -651,7 +652,7 @@ local function create(data) end -- bring the current cmd to the front 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 local file, err = io.open(data.history_file, "w") @@ -681,7 +682,7 @@ local function create(data) if to_exit then print("interactive layout editing ends") if to_apply then - layout.machi_set_cmd(current_cmd, screen) + layout.machi_set_cmd(current_cmd, tag.name) api.layout.arrange(screen) api.gears.timer{ timeout = 1, diff --git a/init.lua b/init.lua index fdd4808..4c01aa4 100644 --- a/init.lua +++ b/init.lua @@ -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 { - layout = require(... .. ".layout"), - editor = require(... .. ".editor"), - switcher = require(... .. ".switcher"), + layout = layout, + editor = editor, + switcher = switcher, + default_editor = default_editor, + default_layout = default_layout, } diff --git a/layout.lua b/layout.lua index d84be9a..94a5b99 100644 --- a/layout.lua +++ b/layout.lua @@ -45,38 +45,50 @@ local function find_region(c, regions) end local function create(name, editor) - local priv = { - name = name, - editor = editor, - cmd = editor.get_last_cmd(name), - regions_cache = {} - } + local instances = {} - local function get_instance_name(_screen) - return name - end - - local function get_regions(workarea, _screen) - 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) + local function get_instance_name(suffix) + if suffix == nil then + return name + else + return name .. '+' .. suffix end - return priv.regions_cache[key] end - local function set_cmd(cmd, _screen) - if priv.cmd ~= cmd then - priv.cmd = cmd - priv.regions_cache = {} + local function get_instance(suffix) + local instance_name = get_instance_name(suffix) + if instances[instance_name] == nil then + 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 local function arrange(p) local wa = p.workarea 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 @@ -111,7 +123,7 @@ local function create(name, editor) if context ~= "mouse.move" then return end 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 diff --git a/switcher.lua b/switcher.lua index 170b299..816cabb 100644 --- a/switcher.lua +++ b/switcher.lua @@ -59,7 +59,7 @@ local function start(c) local layout = api.layout.get(screen) 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({ screen = screen,