change layout.create prototype

This commit is contained in:
Xinhao Yuan 2020-03-21 16:49:20 -04:00
parent 964244d693
commit a1378403c7
4 changed files with 44 additions and 38 deletions

View File

@ -40,16 +40,14 @@ You can change it after loading the module.
## Use the layout ## Use the layout
Use `local layout = machi.layout.create(name, editor[, default_cmd])` to instantiate the layout with an editor object. Use `local layout = machi.layout.create(args)` to instantiate the layout with an editor object. `args` is a table of arguments, where the followings can be used:
`name` can be a string or a function returning a string (see `init.lua` and "Advanced" below). - `name`: the constant name of the layout
This is used for having different actual layout dependent on tags. - `name_func`: a `function(t)` closure that returns a string for tag `t`. `name_func` overrides `name`
- `persistent`: whether to keep a history of the command for the layout. Default is `true`
- `default_cmd`: the command to use if there is no persistent history for this layout.
`editor` are used for editing and persisting the layouts. The function is compatible with the previous `machi.layout.create(name, editor, default_cmd)` calls.
You can use the exsiting `machi.default_editor`, or see below on creating editors.
You can create multiple layouts with different names and share the same editor.
`default_cmd` is the initial command if no history command exists for the layout.
## The layout editor and commands ## The layout editor and commands
@ -252,15 +250,6 @@ Calling `machi.switcher.start()` will create a switcher supporting the following
So far, the key binding is not configurable. One has to modify the source code to change it. So far, the key binding is not configurable. One has to modify the source code to change it.
## Advanced
### `name` as a function in `machi.layout.create`
When passed in as a function, `name` takes the tag `t` and returns (1) a string for the tag-dependent name of the layout, and (2) a boolean indicating the persistence of the layout.
The default layout, `machi.default_layout`, uses the screen geometry and the tag name for name, thus allows the actual layout to be tag- and screen-dependent.
To differentiate tags with the same name, you may need a more advanced naming function.
## Caveats ## Caveats
1. layout-machi handles `beautiful.useless_gap` slightly differently. 1. layout-machi handles `beautiful.useless_gap` slightly differently.

View File

@ -839,7 +839,7 @@ function module.create(data)
end end
if #open_areas == 0 then if #open_areas == 0 then
current_info = current_info .. " (enter to save)" current_info = current_info .. " (enter to apply)"
end end
else else
if key == "Return" then if key == "Return" then
@ -858,7 +858,7 @@ function module.create(data)
-- 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
local instance_name, persistent = layout.machi_get_instance_name(tag) local instance_name, persistent = layout.machi_get_instance_info(tag)
if persistent then if persistent then
data.last_cmd[instance_name] = current_cmd data.last_cmd[instance_name] = current_cmd
if data.history_file then if data.history_file then
@ -877,9 +877,11 @@ function module.create(data)
end end
file:close() file:close()
end end
current_info = "Saved!"
else
current_info = "Applied!"
end end
current_info = "Saved!"
to_exit = true to_exit = true
to_apply = true to_apply = true
end end

View File

@ -7,10 +7,10 @@ local function default_name(tag)
tostring(tag.screen.geometry.width) .. "x" .. tostring(tag.screen.geometry.height) .. "+" .. tostring(tag.screen.geometry.width) .. "x" .. tostring(tag.screen.geometry.height) .. "+" ..
tostring(tag.screen.geometry.x) .. "+" .. tostring(tag.screen.geometry.y) .. '+' .. tag.name tostring(tag.screen.geometry.x) .. "+" .. tostring(tag.screen.geometry.y) .. '+' .. tag.name
end end
return tag.machi_name_cache, true return tag.machi_name_cache
end end
local default_editor = editor.create() local default_editor = editor.create()
local default_layout = layout.create(default_name, default_editor) local default_layout = layout.create{ name_func = default_name, editor = default_editor }
local gcolor = require("gears.color") local gcolor = require("gears.color")
local beautiful = require("beautiful") local beautiful = require("beautiful")

View File

@ -113,23 +113,38 @@ function module.set_geometry(c, region_lu, region_rd, useless_gap, border_width)
end end
end end
function module.create(name, editor, default_cmd) function module.create(args_or_name, editor, default_cmd)
local instances = {} local args
if type(args_or_name) == "string" then
args = {
name = args_or_name
}
elseif type(args_or_name) == "function" then
args = {
name_func = args_or_name
}
elseif type(args_or_name) == "table" then
args = args_or_name
else
return nil
end
args.editor = args.editor or editor
args.default_cmd = args.default_cmd or default_cmd or global_default_cmd
args.persistent = args.persistent == nil or args.persistent
local get_instance_name local instances = {}
if type(name) == "function" then
get_instance_name = name local function get_instance_info(tag)
else return (args.name_func and args.name_func(tag) or args.name), args.persistent
get_instance_name = function () return name, true end end
end
local function get_instance_(tag) local function get_instance_(tag)
local name, persistent = get_instance_name(tag) local name, persistent = get_instance_info(tag)
if instances[name] == nil then if instances[name] == nil then
instances[name] = { instances[name] = {
cmd = persistent and editor.get_last_cmd(name) or nil, cmd = persistent and args.editor.get_last_cmd(name) or nil,
regions_cache = {}, regions_cache = {},
} }
if instances[name].cmd == nil then if instances[name].cmd == nil then
instances[name].cmd = default_cmd instances[name].cmd = default_cmd
end end
@ -144,7 +159,7 @@ function module.create(name, editor, default_cmd)
local key = tostring(workarea.width) .. "x" .. tostring(workarea.height) .. "+" .. tostring(workarea.x) .. "+" .. tostring(workarea.y) local key = tostring(workarea.width) .. "x" .. tostring(workarea.height) .. "+" .. tostring(workarea.x) .. "+" .. tostring(workarea.y)
if instance.regions_cache[key] == nil then if instance.regions_cache[key] == nil then
instance.regions_cache[key] = editor.run_cmd(workarea, cmd) instance.regions_cache[key] = args.editor.run_cmd(workarea, cmd)
end end
return instance.regions_cache[key], cmd:sub(1,1) == "d" return instance.regions_cache[key], cmd:sub(1,1) == "d"
end end
@ -301,7 +316,7 @@ function module.create(name, editor, default_cmd)
name = "machi", name = "machi",
arrange = arrange, arrange = arrange,
resize_handler = resize_handler, resize_handler = resize_handler,
machi_get_instance_name = get_instance_name, machi_get_instance_info = get_instance_info,
machi_set_cmd = set_cmd, machi_set_cmd = set_cmd,
machi_get_regions = get_regions, machi_get_regions = get_regions,
} }