Incorporating reddit comments from u/sigprof. Thanks!

1. Instead of setting beautiful.layout_machi. Provide `machi.get_icon()` to generate the icon.
2. Make `layout.create(name, editor)` take `name` as a function to customize tag-dependent naming.
3. Editor/switch now grabs theme settings before starting instead of loading.
This commit is contained in:
Xinhao Yuan 2019-07-13 18:39:38 -04:00
parent 7e2c0359af
commit 6ea5f035ed
5 changed files with 76 additions and 52 deletions

View File

@ -21,13 +21,23 @@ Suppose this git is checked out at `~/.config/awesome/layout-machi`
The package provide a default layout `machi.default_layout` and editor `machi.default_editor`, which can be added into the layout list. The package provide a default layout `machi.default_layout` and editor `machi.default_editor`, which can be added into the layout list.
The package comes with the icon for `layoutbox`, which can be set with the following statement (after a theme has been loaded):
`require("beautiful").layout_machi = machi.get_icon()`
## Use the layout ## Use the layout
Use `layout = machi.layout.create(name, editor)` to instantiate the layout with an editor object. Use `layout = machi.layout.create(name, editor)` to instantiate the layout with an editor object.
`name` can be a string or a function taking a tag object and returning a string.
This is used for having different actual layout dependent on tags.
`editor` are used for editing and persisting the layouts.
`machi.default_editor` can be used, or see below on creating editors. `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 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 dependent on different tags. The default layout, `machi.default_layout`, uses `"default+" .. tag.name` as name, thus allows the actual layout to be tag-name-dependent.
To differentiate tags with the same name, you may need a more advanced naming function.
## Editor ## Editor

View File

@ -26,17 +26,6 @@ local function max(a, b)
if a < b then return b else return a end if a < b then return b else return a end
end end
local label_font_family = api.beautiful.get_font(
api.beautiful.mono_font or api.beautiful.font):get_family()
local label_size = api.dpi(30)
local info_size = api.dpi(60)
-- colors are in rgba
local border_color = with_alpha(api.gears.color(api.beautiful.border_focus), 0.75)
local active_color = with_alpha(api.gears.color(api.beautiful.bg_focus), 0.5)
local open_color = with_alpha(api.gears.color(api.beautiful.bg_normal), 0.5)
local closed_color = open_color
local init_max_depth = 2
local function is_tiling(c) local function is_tiling(c)
return return
not (c.tomb_floating or c.floating or c.maximized_horizontal or c.maximized_vertical or c.maximized or c.fullscreen) not (c.tomb_floating or c.floating or c.maximized_horizontal or c.maximized_vertical or c.maximized or c.fullscreen)
@ -88,7 +77,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.selected_tag.name) local regions = layout.machi_get_regions and layout.machi_get_regions(c.screen.workarea, c.screen.selected_tag)
if type(regions) ~= "table" or #regions < 1 then if type(regions) ~= "table" or #regions < 1 then
return false return false
end end
@ -190,6 +179,7 @@ local function create(data)
end end
local gap = data.gap or 0 local gap = data.gap or 0
local init_max_depth = 2
local closed_areas local closed_areas
local open_areas local open_areas
@ -465,6 +455,16 @@ local function create(data)
end end
local function start_interactive(screen, layout) local function start_interactive(screen, layout)
local label_font_family = api.beautiful.get_font(
api.beautiful.mono_font or api.beautiful.font):get_family()
local label_size = api.dpi(30)
local info_size = api.dpi(60)
-- colors are in rgba
local border_color = with_alpha(api.gears.color(api.beautiful.border_focus), 0.75)
local active_color = with_alpha(api.gears.color(api.beautiful.bg_focus), 0.5)
local open_color = with_alpha(api.gears.color(api.beautiful.bg_normal), 0.5)
local closed_color = open_color
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 local tag = screen.selected_tag
@ -652,7 +652,9 @@ 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(tag.name)] = current_cmd if not layout.is_dynamic then
data.last_cmd[layout.machi_get_instance_name(tag)] = current_cmd
end
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")
@ -682,7 +684,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, tag.name) layout.machi_set_cmd(current_cmd, tag)
api.layout.arrange(screen) api.layout.arrange(screen)
api.gears.timer{ api.gears.timer{
timeout = 1, timeout = 1,

View File

@ -2,15 +2,26 @@ local layout = require(... .. ".layout")
local editor = require(... .. ".editor") local editor = require(... .. ".editor")
local switcher = require(... .. ".switcher") local switcher = require(... .. ".switcher")
local default_editor = editor.create() local default_editor = editor.create()
local default_layout = layout.create("default", default_editor) local default_layout = layout.create(
function (tag)
return "default+" .. tag.name
end,
default_editor)
local gcolor = require("gears.color") local gcolor = require("gears.color")
local beautiful = require("beautiful") local beautiful = require("beautiful")
local icon_raw
local source = debug.getinfo(1, "S").source local source = debug.getinfo(1, "S").source
if source:sub(1, 1) == "@" then if source:sub(1, 1) == "@" then
local base = source:match("^@(.-)[^/]+$") icon_raw = source:match("^@(.-)[^/]+$") .. "icon.png"
beautiful.layout_machi = gcolor.recolor_image( end
base .. "icon.png", beautiful.fg_normal)
local function get_icon()
if icon_raw ~= nil then
return gcolor.recolor_image(icon_raw, beautiful.fg_normal)
else
return nil
end
end end
return { return {
@ -19,4 +30,6 @@ return {
switcher = switcher, switcher = switcher,
default_editor = default_editor, default_editor = default_editor,
default_layout = default_layout, default_layout = default_layout,
icon_raw = icon_raw,
get_icon = get_icon,
} }

View File

@ -47,27 +47,26 @@ end
local function create(name, editor) local function create(name, editor)
local instances = {} local instances = {}
local function get_instance_name(suffix) local get_instance_name
if suffix == nil then if type(name) == "function" then
return name get_instance_name = name
else else
return name .. '+' .. suffix get_instance_name = function (tag) return name end
end
end end
local function get_instance(suffix) local function get_instance(tag)
local instance_name = get_instance_name(suffix) local name = get_instance_name(tag)
if instances[instance_name] == nil then if instances[name] == nil then
instances[instance_name] = { instances[name] = {
cmd = editor.get_last_cmd(instance_name), cmd = editor.get_last_cmd(name),
regions_cache = {}, regions_cache = {},
} }
end end
return instances[instance_name] return instances[name]
end end
local function get_regions(workarea, suffix) local function get_regions(workarea, tag)
local instance = get_instance(suffix) local instance = get_instance(tag)
if instance.cmd == nil then return {} end if instance.cmd == nil then return {} end
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)
@ -77,8 +76,8 @@ local function create(name, editor)
return instance.regions_cache[key] return instance.regions_cache[key]
end end
local function set_cmd(cmd, suffix) local function set_cmd(cmd, tag)
local instance = get_instance(suffix) local instance = get_instance(tag)
if instance.cmd ~= cmd then if instance.cmd ~= cmd then
instance.cmd = cmd instance.cmd = cmd
instance.regions_cache = {} instance.regions_cache = {}
@ -88,7 +87,7 @@ local function create(name, editor)
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).selected_tag.name) local regions = get_regions(wa, get_screen(p.screen).selected_tag)
if #regions == 0 then return end if #regions == 0 then return end
@ -123,7 +122,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.selected_tag.name) local regions = get_regions(workarea, c.screen.selected_tag)
if #regions == 0 then return end if #regions == 0 then return end

View File

@ -40,18 +40,18 @@ local function with_alpha(col, alpha)
end end
local tablist_font_desc = api.beautiful.get_merged_font(
api.beautiful.mono_font or api.beautiful.font, api.dpi(10))
local font_color = with_alpha(api.gears.color(api.beautiful.fg_normal), 1)
local label_size = api.dpi(30)
local border_color = with_alpha(api.gears.color(api.beautiful.border_focus), 0.75)
local fill_color = with_alpha(api.gears.color(api.beautiful.bg_normal), 0.5)
local fill_color_hl = with_alpha(api.gears.color(api.beautiful.bg_focus), 1)
-- for comparing floats
local threshold = 0.1
local traverse_radius = api.dpi(5)
local function start(c) local function start(c)
local tablist_font_desc = api.beautiful.get_merged_font(
api.beautiful.mono_font or api.beautiful.font, api.dpi(10))
local font_color = with_alpha(api.gears.color(api.beautiful.fg_normal), 1)
local label_size = api.dpi(30)
local border_color = with_alpha(api.gears.color(api.beautiful.border_focus), 0.75)
local fill_color = with_alpha(api.gears.color(api.beautiful.bg_normal), 0.5)
local fill_color_hl = with_alpha(api.gears.color(api.beautiful.bg_focus), 1)
-- for comparing floats
local threshold = 0.1
local traverse_radius = api.dpi(5)
local screen = c.screen local screen = c.screen
local screen_x = screen.geometry.x local screen_x = screen.geometry.x
local screen_y = screen.geometry.y local screen_y = screen.geometry.y
@ -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.selected_tag.name) local regions = layout.machi_get_regions(c.screen.workarea, c.screen.selected_tag)
local infobox = api.wibox({ local infobox = api.wibox({
screen = screen, screen = screen,