From 67a81a1409371ddc5484f67128b4aec67e9d37ce Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Tue, 14 Feb 2017 19:47:47 +0100 Subject: [PATCH 1/9] support for awesome v4 and better overall It uses wiboxes instead of notifications now. --- init.lua | 685 +++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 614 insertions(+), 71 deletions(-) diff --git a/init.lua b/init.lua index 7ae1a8e..ce122cb 100644 --- a/init.lua +++ b/init.lua @@ -18,6 +18,10 @@ local capi = { screen = screen, awesome = awesome, } +local wibox = require("wibox") + +local xresources = require("beautiful").xresources +local dpi = xresources and xresources.apply_dpi or function() end --- Escape pango markup, taken from naughty. local escape_markup = function(s) @@ -30,14 +34,18 @@ end -- Configuration. This can be overridden: global or via args to cyclefocus.cycle. local cyclefocus cyclefocus = { - -- Should clients be raised during cycling? - raise_clients = true, + -- Should clients be shown during cycling? This should be a function, + -- which receives a client object, and can make use of + -- cyclefocus.show_client (the default implementation). + -- Use false to disable showing clients. + show_clients = true, -- Should clients be focused during cycling? + -- This is required for the tasklist to highlight the selected entry. focus_clients = true, -- How many entries should get displayed before and after the current one? - display_next_count = 2, - display_prev_count = 2, -- only 0 for prev, works better with naughty notifications. + display_next_count = 3, + display_prev_count = 3, -- only 0 for prev, works better with naughty notifications. -- Preset to be used for the notification. naughty_preset = { @@ -50,21 +58,13 @@ cyclefocus = { -- - client: the current client object. -- - idx: index number of current entry in clients list. -- - displayed_list: the list of entries in the list, might be filtered. - naughty_preset_for_offset = { + preset_for_offset = { -- Default callback, which will be applied for all offsets (first). default = function (preset, args) -- Default font and icon size (gets overwritten for current/0 index). preset.font = 'sans 8' preset.icon_size = 36 - preset.text = escape_markup(cyclefocus.get_object_name(args.client)) - - -- Display the notification on the current screen (mouse). - preset.screen = capi.mouse.screen - - -- Set notification width, based on screen/workarea width. - local s = preset.screen - local wa = capi.screen[s].workarea - preset.width = floor(wa.width * 0.618) + preset.text = escape_markup(cyclefocus.get_client_title(args.client, false)) preset.icon = cyclefocus.icon_loader(args.client.icon) end, @@ -73,24 +73,24 @@ cyclefocus = { ["0"] = function (preset, args) preset.font = 'sans 12' preset.icon_size = 48 - -- Use get_object_name to handle .name=nil. - preset.text = escape_markup(cyclefocus.get_object_name(args.client)) + preset.text = escape_markup(cyclefocus.get_client_title(args.client, true)) -- Add screen number if there are multiple. if screen.count() > 1 then - preset.text = preset.text .. " [screen " .. args.client.screen .. "]" + preset.text = preset.text .. " [screen " .. tostring(args.client.screen.index) .. "]" end preset.text = preset.text .. " [#" .. args.idx .. "] " preset.text = '' .. preset.text .. '' end, -- You can refer to entries by their offset. - ["-1"] = function (preset, args) - -- preset.icon_size = 32 - end, - ["1"] = function (preset, args) - -- preset.icon_size = 32 - end + -- ["-1"] = function (preset, args) + -- -- preset.icon_size = 32 + -- end, + -- ["1"] = function (preset, args) + -- -- preset.icon_size = 32 + -- end }, + -- naughty_preset_for_offset = cyclefocus.preset, -- DEPRECATED -- Default builtin filters. -- These are meant to get applied always, but you could override them. @@ -98,6 +98,21 @@ cyclefocus = { function(c, source_c) return not c.minimized end, }, + -- EXPERIMENTAL: Only add clients to the history that have been focused by + -- cyclefocus. + -- This allows to switch clients using other methods, but those are then + -- not added to cyclefocus' internal history. + -- The get_next_client function will then first consider the most recent + -- entry in the history stack, if it's not focused currently. + -- + -- You can use cyclefocus.history.add to manually add an entry, or + -- cyclefocus.history.append if you want to add it to the end of the stack. + -- This might be useful in a request::activate signal handler. + -- XXX: needs to be also handled in request::activate then probably. + -- TODO: make this configurable during runtime of the binding, e.g. by + -- flagging entries in the stack or using different stacks. + -- only_add_internal_focus_changes_to_history = true, + -- The filter to ignore clients altogether (get not added to the history stack). -- This is different from the cycle_filters. -- The function should return true / the client if it's ok, nil otherwise. @@ -155,15 +170,24 @@ cyclefocus.filters = { end end return false - end + end, + + -- EXPERIMENTAL: + -- Skip clients that were added through "focus" signal. + -- Replaces only_add_internal_focus_changes_to_history. + not_through_focus_signal = function (c, source_c) + local attribs = cyclefocus.history.attribs(c) + return not attribs.source or attribs.source ~= "focus" + end, } local ignore_focus_signal = false -- Flag to ignore the focus signal internally. +local showing_client -- Debug function. Set focusstyle.debug to activate it. {{{ cyclefocus.debug = function(msg, level) - local level = level or 1 + level = level or 1 if not cyclefocus.debug_level or cyclefocus.debug_level < level then return end @@ -189,6 +213,16 @@ local get_object_name = function (o) end end cyclefocus.get_object_name = get_object_name + + +cyclefocus.get_client_title = function (c, current) + -- Use get_object_name to handle .name=nil. + local title = cyclefocus.get_object_name(c) + if #title > 80 then + title = title:sub(1, 80) .. '…' + end + return title +end -- }}} @@ -198,21 +232,45 @@ local history = { stack = {} } +--- Remove a client from the history stack. +-- @tparam table Client. function history.delete(c) + local k = history._get_key(c) + if k then + table.remove(history.stack, k) + end +end + +function history._get_key(c) for k, v in ipairs(history.stack) do - if v == c then - table.remove(history.stack, k) - break + if v[1] == c then + return k end end end -function history.add(c) +function history.attribs(c) + local k = history._get_key(c) + if k then + return history.stack[k][2] + end +end + +function history.clear() + history.stack = {} +end + +-- @param filter: a function / boolean to filter clients: true means to add it. +function history.add(c, filter, append, attribs) + local filter = filter or cyclefocus.filter_focus_history + local append = append or false + local attribs = attribs or {} + -- Less verbose debugging during startup/restart. cyclefocus.debug("history.add: " .. get_object_name(c), capi.awesome.startup and 4 or 2) - if cyclefocus.filter_focus_history then - if not cyclefocus.filter_focus_history(c) then + if filter and type(filter) == "function" then + if not filter(c) then cyclefocus.debug("Filtered! " .. get_object_name(c), 2) return true end @@ -220,9 +278,88 @@ function history.add(c) -- Remove any existing entries from the stack. history.delete(c) - -- Record the client has latest focused - table.insert(history.stack, 1, c) + + if append then + table.insert(history.stack, {c, attribs}) + else + table.insert(history.stack, 1, {c, attribs}) + end + + -- Manually add it to awesome's internal history (where we've removed the + -- signal from). + awful.client.focus.history.add(c) +end + +function history.movetotop(c) + local attribs = history.attribs(c) + history.add(c, true, false, attribs) +end + +function history.append(c, filter, attribs) + return history.add(c, filter, true, attribs) +end + +--- Save the history into a X property. +function history.persist() + local ids = {} + for _, v in ipairs(history.stack) do + table.insert(ids, v[1].window) + end + local xprop = table.concat(ids, " ") + capi.awesome.set_xproperty('awesome.cyclefocus.history', xprop) +end + +--- Load history from the X property. +function history.load() + local xprop = capi.awesome.get_xproperty('awesome.cyclefocus.history') + if not xprop or xprop == "" then + return + end + + local cls = capi.client.get() + local ids = {} + for id in string.gmatch(xprop, "%S+") do + table.insert(ids, 1, id) + end + for _,window in ipairs(ids) do + local found = false + for _,c in pairs(cls) do + if tonumber(window) == c.window then + history.add(c, true, false, {source="load"}) + found = true + break + end + end + end +end + +-- Persist history when restarting awesome. +capi.awesome.register_xproperty('awesome.cyclefocus.history', 'string') +capi.awesome.connect_signal("exit", function(restarting) + ignore_focus_signal = true + if restarting then + history.persist() + end +end) + +-- On startup / restart: load the history and jump to the last focused client. +cyclefocus.load_on_startup = function() + capi.awesome.disconnect_signal("refresh", cyclefocus.load_on_startup) + + ignore_focus_signal = true + history.load() + if history.stack[1] then + -- bdebug({c=history.stack[1]}, 'load_on_startup::jump') + showing_client = history.stack[1][1] + awful.client.jumpto(showing_client) + showing_client = nil + end + ignore_focus_signal = false end +capi.awesome.connect_signal("refresh", cyclefocus.load_on_startup) + +-- Export it. At least history.add should be. +cyclefocus.history = history -- }}} -- Connect to signals. {{{ @@ -233,15 +370,30 @@ capi.client.connect_signal("focus", function (c) cyclefocus.debug("Ignoring focus signal: " .. get_object_name(c), 4) return end - history.add(c) + history.add(c, nil, nil, {source="focus"}) end) +-- Disable awesome's internal history handler to handle `ignore_focus_signal`. +-- https://github.com/awesomeWM/awesome/pull/906. +if awful.client.focus.history.disable_tracking then + awful.client.focus.history.disable_tracking() +else + capi.client.disconnect_signal("focus", awful.client.focus.history.add) +end + capi.client.connect_signal("manage", function (c) if ignore_focus_signal then cyclefocus.debug("Ignoring focus signal (manage): " .. get_object_name(c), 2) return end - history.add(c) + + -- During startup: append any clients, to make them known, + -- but not override history.load etc. + if capi.awesome.startup then + history.append(c) + else + history.add(c, nil, false, {source="manage"}) + end end) capi.client.connect_signal("unmanage", function (c) @@ -264,6 +416,217 @@ local raise_client = function(c) c:raise() end + +-- Keep track of the client where "ontop" needs to be restored, and forget +-- about it in "unmanage", to avoid an "invalid object" error. +-- Ref: https://github.com/awesomeWM/awesome/issues/110 +local restore_ontop_c +local restore_callback_show_client +local show_client_restore_client_props = {} +client.connect_signal("unmanage", function (c) + if restore_ontop_c and c == restore_ontop_c[1] then + restore_ontop_c = nil + end + if c == restore_callback_show_client then + restore_callback_show_client = nil + end + if c == showing_client then + showing_client = nil + end + + if show_client_restore_client_props[c] then + show_client_restore_client_props[c] = nil + end +end) + + +local beautiful = require("beautiful") + +--- Callback to get properties for clients that are shown during cycling. +-- @client c +-- @return table +cyclefocus.decorate_show_client = function(c) + return { + -- border_color = beautiful.fg_focus, + border_color = beautiful.border_focus, + border_width = c.border_width or 1, + -- XXX: changes layout / triggers resizes. + -- border_width = 10, + } +end +--- Callback to get properties for other clients that are visible during cycling. +-- @client c +-- @return table +cyclefocus.decorate_show_client_others = function(c) + return { + -- XXX: too distracting. + -- opacity = 0.7 + } +end + +local show_client_apply_props = {} + +local show_client_apply_props_others = {} +local show_client_restore_client_props_others = {} + +local callback_show_client_lock +local decorate_if_showing_client = function (c) + if c == showing_client then + cyclefocus.callback_show_client(c) + end +end +-- A table with property callbacks. Could be merged with decorate_if_showing_client. +local update_show_client_restore_client_props = {} +--- Callback when a client gets shown during cycling. +-- This can be overridden itself, but it's meant to be configured through +-- decorate_show_client instead. +-- @client c +-- @param boolean Restore the previous state? +cyclefocus.callback_show_client = function (c, restore) + if callback_show_client_lock then return end + callback_show_client_lock = true + + if restore then + -- Restore all saved properties. + if show_client_restore_client_props[c] then + -- Disconnect signals. + for k,_ in pairs(show_client_restore_client_props[c]) do + client.disconnect_signal("property::" .. k, decorate_if_showing_client) + client.disconnect_signal("property::" .. k, update_show_client_restore_client_props[c][k]) + end + + for k,v in pairs(show_client_restore_client_props[c]) do + c[k] = v + end + + -- Restore properties for other clients. + for _c,props in pairs(show_client_restore_client_props_others[c]) do + for k,v in pairs(props) do + -- XXX: might have an "invalid object" here! + _c[k] = v + end + end + + show_client_apply_props[c] = nil + show_client_restore_client_props[c] = nil + show_client_restore_client_props_others[c] = nil + end + else + -- Save orig settings on first call. + local first_call = not show_client_restore_client_props[c] + if first_call then + show_client_restore_client_props[c] = {} + show_client_apply_props[c] = {} + + -- Get props to apply and store original values. + show_client_apply_props[c] = cyclefocus.decorate_show_client(c) + update_show_client_restore_client_props[c] = {} + for k,_ in pairs(show_client_apply_props[c]) do + show_client_restore_client_props[c][k] = c[k] + end + + -- Get props for other clients and store original values. + -- TODO: handle all screens?! + show_client_apply_props_others[c] = cyclefocus.decorate_show_client_others(c) + show_client_restore_client_props_others[c] = {} + for s in capi.screen do + for _,_c in pairs(awful.client.visible(s)) do + if _c ~= c then + show_client_restore_client_props_others[c][_c] = {} + for k,_ in pairs(show_client_apply_props_others[c]) do + show_client_restore_client_props_others[c][_c][k] = _c[k] + end + end + end + end + end + -- Apply props from callback. + for k,v in pairs(show_client_apply_props[c]) do + c[k] = v + end + -- Apply props for other clients. + for _c,_ in pairs(show_client_restore_client_props_others[c]) do + for k,v in pairs(show_client_apply_props_others[c]) do + _c[k] = v -- see: XXX_1 + end + end + + if first_call then + for k,_ in pairs(show_client_apply_props[c]) do + client.connect_signal("property::" .. k, decorate_if_showing_client) + + -- Update client props to be restored during showing a client, + -- e.g. border_color from focus signals. + update_show_client_restore_client_props[c][k] = function() + show_client_restore_client_props[c][k] = c[k] + end + client.connect_signal("property::" .. k, update_show_client_restore_client_props[c][k]) + end + -- TODO: merge with above; also disconnect on restore. + -- for k,v in pairs(show_client_apply_props_others[c]) do + -- client.connect_signal("property::" .. k, decorate_if_showing_client) + -- end + end + end + + callback_show_client_lock = false +end + +-- Helper function to restore state of the temporarily selected client. +cyclefocus.show_client = function (c) + showing_client = c + + if c then + if restore_callback_show_client then + cyclefocus.callback_show_client(restore_callback_show_client, true) + end + restore_callback_show_client = c + + -- (Re)store ontop property. + if restore_ontop_c then + restore_ontop_c[1].ontop = restore_ontop_c[2] + end + restore_ontop_c = {c, c.ontop} + c.ontop = true + + -- Make the clients tag visible, if it currently is not. + local sel_tags = awful.tag.selectedlist(c.screen) + local c_tag = c.first_tag or c:tags()[1] + if not awful.util.table.hasitem(sel_tags, c_tag) then + -- Select only the client's first tag, after de-selecting + -- all others. + + -- Make the client sticky temporarily, so it will be + -- considered visbile internally. + -- NOTE: this is done for client_maybevisible (used by autofocus). + local restore_sticky = c.sticky + c.sticky = true + + for i, t in pairs(awful.tag.gettags(c.screen)) do + if t ~= c_tag then + t.selected = false + end + end + c_tag.selected = true + + -- Restore. + c.sticky = restore_sticky + end + cyclefocus.callback_show_client(c, false) + + else -- No client provided, restore only. + if restore_ontop_c then + restore_ontop_c[1].ontop = restore_ontop_c[2] + end + cyclefocus.callback_show_client(restore_callback_show_client, true) + showing_client = nil + end +end + +--- Cached main wibox. +local wbox +local wbox_screen + -- Main function. cyclefocus.cycle = function(startdirection, _args) local args = awful.util.table.join(awful.util.table.clone(cyclefocus), _args) @@ -277,6 +640,11 @@ cyclefocus.cycle = function(startdirection, _args) local filter_result_cache = {} -- Holds cached filter results. + local show_clients = args.show_clients + if show_clients and type(show_clients) ~= 'function' then + show_clients = cyclefocus.show_client + end + -- Support single filter. if args.cycle_filter then cycle_filters = awful.util.table.clone(cycle_filters) @@ -290,33 +658,62 @@ cyclefocus.cycle = function(startdirection, _args) local orig_client = capi.client.focus -- Will be jumped to via Escape (abort). local idx = 1 -- Currently focused client in the stack. + -- Save list of selected tags for all screens. + local restore_tag_selected = {} + for s = 1, capi.screen.count() do + restore_tag_selected[s] = {} + for _,t in pairs(awful.tag.gettags(s)) do + restore_tag_selected[s][t] = t.selected + end + end + local notifications = {} + local notifications_by_client = {} --- Helper function to get the next client. -- @param direction 1 (forward) or -1 (backward). + -- @param idx Current index in the stack. + -- @param stack Current stack (default: history.stack). + -- @param consider_cur_idx Also look at the current idx, and consider it + -- when it's not focused. -- @return client or nil and current index in stack. - local get_next_client = function(direction, idx, stack) + local get_next_client = function(direction, idx, stack, consider_cur_idx) local startidx = idx local stack = stack or history.stack + local consider_cur_idx = consider_cur_idx or args.focus_clients local nextc - cyclefocus.debug('get_next_client: #' .. idx .. ", dir=" .. direction .. ", start=" .. startidx, 1) - for _ = 1, #stack do - cyclefocus.debug('find loop: #' .. idx .. ", dir=" .. direction, 3) - - idx = idx + direction - if idx < 1 then - idx = #stack - elseif idx > #stack then - idx = 1 + cyclefocus.debug('get_next_client: #' .. idx .. ", dir=" .. direction + .. ", start=" .. startidx .. ", consider_cur=" .. tostring(consider_cur_idx), 2) + + local n = #stack + if consider_cur_idx then + local c_top = stack[idx][1] + if c_top ~= capi.client.focus then + n = n+1 + cyclefocus.debug("Considering nextc from top of stack: " .. tostring(c_top), 2) + else + consider_cur_idx = false + end + end + for loop_stack_i = 1, n do + if not consider_cur_idx or loop_stack_i ~= 1 then + idx = idx + direction + if idx < 1 then + idx = #stack + elseif idx > #stack then + idx = 1 + end end - nextc = stack[idx] + cyclefocus.debug('find loop: #' .. idx .. ", dir=" .. direction, 3) + nextc = stack[idx][1] if nextc then -- Filtering. if cycle_filters then -- Get and init filter cache data structure. {{{ + -- TODO: move function(s) up? local get_cached_filter_result = function(f, a, b) local b = b or false -- handle nil if filter_result_cache[f] == nil then @@ -369,25 +766,53 @@ cyclefocus.cycle = function(startdirection, _args) local first_run = true local nextc + + -- Get the screen before moving the mouse. + local s = awful.screen.focused and awful.screen.focused() or mouse.screen + + -- Move mouse pointer away to avoid sloppy focus kicking in. + -- TODO: go to current screen's 0/0 (not total). Have an option/method for this! + local mouse_coords + if show_clients then + restore_mouse_coords = capi.mouse.coords() + capi.mouse.coords({ x = 0, y = 0 }, true) + end + capi.keygrabber.run(function(mod, key, event) + local start_keygrabber = os.clock() -- Helper function to exit out of the keygrabber. -- If a client is given, it will be jumped to. - local exit_grabber = function (c) + local exit_grabber = function(c) cyclefocus.debug("exit_grabber: " .. get_object_name(c), 2) - if notifications then - for _, v in pairs(notifications) do - naughty.destroy(v) - end + if wbox then + wbox.visible = false end capi.keygrabber.stop() + + -- Restore. + if show_clients then + show_clients() + end if c then + showing_client = c -- NOTE: awful.client.jumpto(c) resets mouse. capi.client.focus = c raise_client(c) - history.add(c) + if c ~= orig_client then + history.movetotop(c) + end + end + + -- Restore mouse if it has not been moved during cycling. + if restore_mouse_coords then + local coords = capi.mouse.coords() + if coords.x == 0 and coords.y == 0 then + capi.mouse.coords(restore_mouse_coords, true) + end end ignore_focus_signal = false + return true end @@ -398,6 +823,15 @@ cyclefocus.cycle = function(startdirection, _args) -- Abort on Escape. if key == 'Escape' then + -- Restore previously selected tags for screen. + if restore_tag_selected then + for s = 1, capi.screen.count() do + for _,t in pairs(awful.tag.gettags(s)) do + t.selected = restore_tag_selected[s][t] + end + end + end + return exit_grabber(orig_client) end @@ -411,6 +845,9 @@ cyclefocus.cycle = function(startdirection, _args) if first_run then nextc, idx = get_next_client(direction, idx) end + if show_clients then + show_clients(nextc) + end return exit_grabber(nextc) end @@ -431,28 +868,68 @@ cyclefocus.cycle = function(startdirection, _args) return exit_grabber() end + -- Show the client, which triggers setup of restore_callback_show_client etc. + if show_clients then + show_clients(nextc) + end -- Focus client. if args.focus_clients then capi.client.focus = nextc end - -- Raise client. - if args.raise_clients then - raise_client(nextc) - end - if not args.display_notifications then return true end - -- Create notification with index, name and screen. + local container_margin_top_bottom = dpi(5) + local container_margin_left_right = dpi(5) + local icon_margin = 5 + if not wbox then + wbox = wibox({ontop = true }) + wbox._for_screen = mouse.screen + wbox:set_fg(beautiful.fg_normal) + wbox:set_bg("#ffffff00") + + local container_inner = wibox.layout.align.vertical() + container_layout = wibox.layout.margin( + container_inner, + container_margin_left_right, container_margin_left_right, + container_margin_top_bottom, container_margin_top_bottom) + container_layout = wibox.widget.background(container_layout) + container_layout:set_bg(beautiful.bg_normal..'cc') + + -- constraint:set_widget(layout) + -- constraint = wibox.layout.constraint(layout, "max", w, h/2) + -- wbox:set_widget(constraint) + wbox:set_widget(container_layout) + layout = wibox.layout.flex.vertical() + container_inner:set_middle(layout) + else + layout:reset() + end + + -- Set geometry always, the screen might have changed. + if not wbox_screen or wbox_screen ~= s then + wbox_screen = s + local wa = screen[wbox_screen].workarea + local w = math.ceil(wa.width * 0.618) + wbox:geometry({ + -- right-align. + x = math.ceil(wa.x + wa.width - w), + width = w, + }) + end + local wbox_height = 0 + local max_icon_size = 48 + + -- Create entry with index, name and screen. local do_notification_for_idx_offset = function(offset, c, idx, displayed_list) -- {{{ -- TODO: make this configurable using placeholders. local naughty_args = {} -- .. ", [tags " .. table.concat(tags, ", ") .. "]" -- Get naughty preset from naughty_preset, and callbacks. - naughty_args.preset = awful.util.table.clone(args.naughty_preset) + local preset = awful.util.table.clone(args.naughty_preset) -- Callback. local args_for_cb = { @@ -460,23 +937,82 @@ cyclefocus.cycle = function(startdirection, _args) offset=offset, idx=idx, displayed_list=displayed_list } - local preset_for_offset = args.naughty_preset_for_offset + local preset_for_offset = args.preset_for_offset local preset_cb = preset_for_offset[tostring(offset)] -- Callback for all. if preset_for_offset.default then - preset_for_offset.default(naughty_args.preset, args_for_cb) + preset_for_offset.default(preset, args_for_cb) end -- Callback for offset. if preset_cb then - preset_cb(naughty_args.preset, args_for_cb) + preset_cb(preset, args_for_cb) + end + + local entrybox = wibox({}) + -- local entry_layout = wibox.layout.flex.horizontal() + local entry_layout = wibox.layout.fixed.horizontal() + + -- From naughty. + local icon = preset.icon + local icon_margin = icon_margin + local iconmarginbox + if icon then + -- surface.load_uncached(icon) + local surface = require("gears.surface") + local cairo = require("lgi").cairo + iconbox = wibox.widget.imagebox() + local icon_size = preset.icon_size + if icon_size then + local scaled = cairo.ImageSurface(cairo.Format.ARGB32, icon_size, icon_size) + local cr = cairo.Context(scaled) + cr:scale(icon_size / icon:get_height(), icon_size / icon:get_width()) + cr:set_source_surface(icon, 0, 0) + cr:paint() + icon = scaled + icon_margin = icon_margin + math.max(0, (max_icon_size - icon_size)/2) + end + + -- Margin. + iconmarginbox = wibox.layout.margin(iconbox) + iconmarginbox:set_margins(icon_margin) + + iconbox:set_resize(false) + iconbox:set_image(icon) + icon_h = icon:get_height() + + entry_layout:add(iconmarginbox) end - -- Replace previous notification, if any. - if notifications[tostring(offset)] then - naughty_args.replaces_id = notifications[tostring(offset)].id + local textbox = wibox.widget.textbox() + textbox:set_markup(preset.text) + textbox:set_font(preset.font) + textbox:set_wrap("word_char") + textbox:set_ellipsize("middle") + textbox_margin = wibox.layout.margin(textbox) + textbox_margin:set_margins(dpi(5)) + + entry_layout:add(textbox_margin) + entry_layout = wibox.layout.margin(entry_layout, dpi(5), dpi(5), + dpi(2), dpi(2)) + local entry_with_bg = wibox.widget.background(entry_layout) + if offset == 0 then + entry_with_bg:set_fg(beautiful.fg_focus) + entry_with_bg:set_bg(beautiful.bg_focus) + else + entry_with_bg:set_fg(beautiful.fg_normal) + -- entry_with_bg:set_bg(beautiful.bg_normal.."dd") end + layout:add(entry_with_bg) + + -- TODO: get context automatically?! + -- local context = wbox:get_widget_context() + -- local context = {dpi=176} + local context = {} - notifications[tostring(offset)] = naughty.notify(naughty_args) + -- Add height to outer wibox. + -- Hack for newer API (https://github.com/awesomeWM/awesome/pull/398#issuecomment-134955805). + local _, entry_height = entry_with_bg:fit(context, wbox.width, 2^20) + wbox_height = wbox_height + entry_height end -- }}} -- Get clients before and after currently selected one. @@ -486,7 +1022,7 @@ cyclefocus.cycle = function(startdirection, _args) local dlist = {} -- A table with offset => stack index. dlist[0] = _idx - prevnextlist[_idx] = false + prevnextlist[_idx][1] = false -- Build dlist for both directions, depending on how many entries should get displayed. for _,dir in ipairs({1, -1}) do @@ -494,11 +1030,11 @@ cyclefocus.cycle = function(startdirection, _args) local n = dir == 1 and args.display_next_count or args.display_prev_count for i = 1, n do local _i = i * dir - _, _idx = get_next_client(dir, _idx, prevnextlist) + _, _idx = get_next_client(dir, _idx, prevnextlist, false) if _ then dlist[_i] = _idx end - prevnextlist[_idx] = false + prevnextlist[_idx][1] = false end end @@ -508,9 +1044,10 @@ cyclefocus.cycle = function(startdirection, _args) table.sort(offsets) -- Issue the notifications. + local start = os.clock() for _,i in ipairs(offsets) do _idx = dlist[i] - do_notification_for_idx_offset(i, history.stack[_idx], _idx, dlist) + do_notification_for_idx_offset(i, history.stack[_idx][1], _idx, dlist) -- Unset client from prevnext list. local k = awful.util.table.hasitem(prevnextlist, _c) if k then @@ -518,7 +1055,13 @@ cyclefocus.cycle = function(startdirection, _args) prevnextlist[k] = false end end - + local wa = screen[s].workarea + local h = wbox_height + container_margin_top_bottom*2 + wbox:geometry({ + height = h, + y = wa.y + floor(wa.height/2 - h/2), + }) + wbox.visible = true return true end) end From ae88f4c61e270bf4581f0cc921e2691dcdb20fb2 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Tue, 14 Feb 2017 22:13:33 +0100 Subject: [PATCH 2/9] Cleanup thanks to luacheck --- README.md | 29 ++++++++------- init.lua | 104 +++++++++++++++++++++++------------------------------- 2 files changed, 59 insertions(+), 74 deletions(-) diff --git a/README.md b/README.md index 4b3f4ff..9f1c18d 100644 --- a/README.md +++ b/README.md @@ -82,22 +82,20 @@ Setup `modkey+Tab` to cycle through all windows (assuming modkey is ```lua -- modkey+Tab: cycle through all clients. -awful.key({ modkey, }, "Tab", function(c) - cyclefocus.cycle(1, {modifier="Super_L"}) +awful.key({ modkey }, "Tab", function(c) + cyclefocus.cycle({modifier="Super_L"}) end), -- modkey+Shift+Tab: backwards awful.key({ modkey, "Shift" }, "Tab", function(c) - cyclefocus.cycle(-1, {modifier="Super_L"}) + cyclefocus.cycle({modifier="Super_L"}) end), ``` -The first argument to `cyclefocus.cycle` is the starting direction: 1 means -backwards in history (incrementing index for the history stack), -1 means to go -in the opposite direction. `1` is the normal behavior, while `-1` refers to the -shifted version. - -The second argument is a table of optional arguments. We need to pass the -modifier being used (as seen by awesome's `keygrabber`) here. +You can pass a table of optional arguments. +We need to pass the modifier (as seen by awesome's `keygrabber`) here. +Internally the direction gets set according to if the `Shift` modifier key +is present, so that the second definition is only necessary to trigger it in +the opposite direction from the beginning. See the `init.lua` file for a full reference, or refer to the [settings section below](#settings). @@ -110,7 +108,7 @@ There is a helper function `cyclefocus.key`, which can be used instead of ```lua -- Alt-Tab: cycle through clients on the same screen. -- This must be a clientkeys mapping to have source_c available in the callback. -cyclefocus.key({ "Mod1", }, "Tab", 1, { +cyclefocus.key({ "Mod1", }, "Tab", { -- cycle_filters as a function callback: -- cycle_filters = { function (c, source_c) return c.screen == source_c.screen end }, @@ -120,10 +118,11 @@ cyclefocus.key({ "Mod1", }, "Tab", 1, { }), ``` -The first two arguments are the same as with `awful.key`: a list of modifiers and -the key. Then follows the direction and the list of optional arguments again. -(here the `modifier` argument is not required, because it is given in the first -argument). +The first two arguments are the same as with `awful.key`: a list of modifiers +and the key. Then the table with optional arguments to `cyclefocus.cycle()` +follows. +(here the `modifier` argument is not required, because it gets used from +the first argument). #### `cycle_filters` diff --git a/init.lua b/init.lua index ce122cb..8e0b2af 100644 --- a/init.lua +++ b/init.lua @@ -95,7 +95,7 @@ cyclefocus = { -- Default builtin filters. -- These are meant to get applied always, but you could override them. cycle_filters = { - function(c, source_c) return not c.minimized end, + function(c, source_c) return not c.minimized end, --luacheck: no unused args }, -- EXPERIMENTAL: Only add clients to the history that have been focused by @@ -149,7 +149,7 @@ cyclefocus.filters = { end, -- Only marked clients (via awful.client.mark and .unmark). - marked = function (c, source_c) + marked = function (c, source_c) --luacheck: no unused args return awful.client.ismarked(c) end, @@ -175,7 +175,7 @@ cyclefocus.filters = { -- EXPERIMENTAL: -- Skip clients that were added through "focus" signal. -- Replaces only_add_internal_focus_changes_to_history. - not_through_focus_signal = function (c, source_c) + not_through_focus_signal = function (c, source_c) --luacheck: no unused args local attribs = cyclefocus.history.attribs(c) return not attribs.source or attribs.source ~= "focus" end, @@ -215,7 +215,7 @@ end cyclefocus.get_object_name = get_object_name -cyclefocus.get_client_title = function (c, current) +cyclefocus.get_client_title = function (c, current) --luacheck: no unused args -- Use get_object_name to handle .name=nil. local title = cyclefocus.get_object_name(c) if #title > 80 then @@ -262,9 +262,9 @@ end -- @param filter: a function / boolean to filter clients: true means to add it. function history.add(c, filter, append, attribs) - local filter = filter or cyclefocus.filter_focus_history - local append = append or false - local attribs = attribs or {} + filter = filter or cyclefocus.filter_focus_history + append = append or false + attribs = attribs or {} -- Less verbose debugging during startup/restart. cyclefocus.debug("history.add: " .. get_object_name(c), capi.awesome.startup and 4 or 2) @@ -322,11 +322,9 @@ function history.load() table.insert(ids, 1, id) end for _,window in ipairs(ids) do - local found = false for _,c in pairs(cls) do if tonumber(window) == c.window then history.add(c, true, false, {source="load"}) - found = true break end end @@ -457,7 +455,7 @@ end --- Callback to get properties for other clients that are visible during cycling. -- @client c -- @return table -cyclefocus.decorate_show_client_others = function(c) +cyclefocus.decorate_show_client_others = function(c) --luacheck: no unused args return { -- XXX: too distracting. -- opacity = 0.7 @@ -602,7 +600,7 @@ cyclefocus.show_client = function (c) local restore_sticky = c.sticky c.sticky = true - for i, t in pairs(awful.tag.gettags(c.screen)) do + for _, t in pairs(awful.tag.gettags(c.screen)) do if t ~= c_tag then t.selected = false end @@ -626,10 +624,16 @@ end --- Cached main wibox. local wbox local wbox_screen +local layout -- Main function. -cyclefocus.cycle = function(startdirection, _args) - local args = awful.util.table.join(awful.util.table.clone(cyclefocus), _args) +cyclefocus.cycle = function(startdirection_or_args, args) + if type(startdirection_or_args) == 'number' then + awful.util.deprecate('startdirection is not used anymore: pass in args only', {raw=true}) + else + args = startdirection_or_args + end + args = awful.util.table.join(awful.util.table.clone(cyclefocus), args) -- The key name of the (last) modifier: this gets used for the "release" event. local modifier = args.modifier or 'Alt_L' local keys = args.keys or {'Tab', 'ISO_Left_Tab'} @@ -656,7 +660,6 @@ cyclefocus.cycle = function(startdirection, _args) -- Internal state. local orig_client = capi.client.focus -- Will be jumped to via Escape (abort). - local idx = 1 -- Currently focused client in the stack. -- Save list of selected tags for all screens. local restore_tag_selected = {} @@ -667,9 +670,6 @@ cyclefocus.cycle = function(startdirection, _args) end end - local notifications = {} - local notifications_by_client = {} - --- Helper function to get the next client. -- @param direction 1 (forward) or -1 (backward). -- @param idx Current index in the stack. @@ -679,8 +679,8 @@ cyclefocus.cycle = function(startdirection, _args) -- @return client or nil and current index in stack. local get_next_client = function(direction, idx, stack, consider_cur_idx) local startidx = idx - local stack = stack or history.stack - local consider_cur_idx = consider_cur_idx or args.focus_clients + stack = stack or history.stack + consider_cur_idx = consider_cur_idx or args.focus_clients local nextc @@ -715,7 +715,7 @@ cyclefocus.cycle = function(startdirection, _args) -- Get and init filter cache data structure. {{{ -- TODO: move function(s) up? local get_cached_filter_result = function(f, a, b) - local b = b or false -- handle nil + b = b or false -- handle nil if filter_result_cache[f] == nil then filter_result_cache[f] = { [a] = { [b] = { } } } return nil @@ -728,7 +728,7 @@ cyclefocus.cycle = function(startdirection, _args) return filter_result_cache[f][a][b] end local set_cached_filter_result = function(f, a, b, value) - local b = b or false -- handle nil + b = b or false -- handle nil get_cached_filter_result(f, a, b) -- init filter_result_cache[f][a][b] = value end -- }}} @@ -766,21 +766,20 @@ cyclefocus.cycle = function(startdirection, _args) local first_run = true local nextc + local idx = 1 -- Currently focused client in the stack. -- Get the screen before moving the mouse. - local s = awful.screen.focused and awful.screen.focused() or mouse.screen + local initial_screen = awful.screen.focused and awful.screen.focused() or mouse.screen -- Move mouse pointer away to avoid sloppy focus kicking in. -- TODO: go to current screen's 0/0 (not total). Have an option/method for this! - local mouse_coords + local restore_mouse_coords if show_clients then restore_mouse_coords = capi.mouse.coords() capi.mouse.coords({ x = 0, y = 0 }, true) end capi.keygrabber.run(function(mod, key, event) - local start_keygrabber = os.clock() - -- Helper function to exit out of the keygrabber. -- If a client is given, it will be jumped to. local exit_grabber = function(c) @@ -883,7 +882,6 @@ cyclefocus.cycle = function(startdirection, _args) local container_margin_top_bottom = dpi(5) local container_margin_left_right = dpi(5) - local icon_margin = 5 if not wbox then wbox = wibox({ontop = true }) wbox._for_screen = mouse.screen @@ -891,7 +889,7 @@ cyclefocus.cycle = function(startdirection, _args) wbox:set_bg("#ffffff00") local container_inner = wibox.layout.align.vertical() - container_layout = wibox.layout.margin( + local container_layout = wibox.layout.margin( container_inner, container_margin_left_right, container_margin_left_right, container_margin_top_bottom, container_margin_top_bottom) @@ -909,8 +907,8 @@ cyclefocus.cycle = function(startdirection, _args) end -- Set geometry always, the screen might have changed. - if not wbox_screen or wbox_screen ~= s then - wbox_screen = s + if not wbox_screen or wbox_screen ~= initial_screen then + wbox_screen = initial_screen local wa = screen[wbox_screen].workarea local w = math.ceil(wa.width * 0.618) wbox:geometry({ @@ -923,19 +921,14 @@ cyclefocus.cycle = function(startdirection, _args) local max_icon_size = 48 -- Create entry with index, name and screen. - local do_notification_for_idx_offset = function(offset, c, idx, displayed_list) -- {{{ - -- TODO: make this configurable using placeholders. - local naughty_args = {} - -- .. ", [tags " .. table.concat(tags, ", ") .. "]" - - -- Get naughty preset from naughty_preset, and callbacks. + local display_entry_for_idx_offset = function(offset, c, _idx, displayed_list) -- {{{ local preset = awful.util.table.clone(args.naughty_preset) -- Callback. local args_for_cb = { client=c, offset=offset, - idx=idx, + idx=_idx, displayed_list=displayed_list } local preset_for_offset = args.preset_for_offset local preset_cb = preset_for_offset[tostring(offset)] @@ -948,19 +941,16 @@ cyclefocus.cycle = function(startdirection, _args) preset_cb(preset, args_for_cb) end - local entrybox = wibox({}) -- local entry_layout = wibox.layout.flex.horizontal() local entry_layout = wibox.layout.fixed.horizontal() -- From naughty. local icon = preset.icon - local icon_margin = icon_margin + local icon_margin = 5 local iconmarginbox if icon then - -- surface.load_uncached(icon) - local surface = require("gears.surface") local cairo = require("lgi").cairo - iconbox = wibox.widget.imagebox() + local iconbox = wibox.widget.imagebox() local icon_size = preset.icon_size if icon_size then local scaled = cairo.ImageSurface(cairo.Format.ARGB32, icon_size, icon_size) @@ -978,7 +968,6 @@ cyclefocus.cycle = function(startdirection, _args) iconbox:set_resize(false) iconbox:set_image(icon) - icon_h = icon:get_height() entry_layout:add(iconmarginbox) end @@ -988,7 +977,7 @@ cyclefocus.cycle = function(startdirection, _args) textbox:set_font(preset.font) textbox:set_wrap("word_char") textbox:set_ellipsize("middle") - textbox_margin = wibox.layout.margin(textbox) + local textbox_margin = wibox.layout.margin(textbox) textbox_margin:set_margins(dpi(5)) entry_layout:add(textbox_margin) @@ -1043,19 +1032,12 @@ cyclefocus.cycle = function(startdirection, _args) for n in pairs(dlist) do table.insert(offsets, n) end table.sort(offsets) - -- Issue the notifications. - local start = os.clock() + -- Display the wibox. for _,i in ipairs(offsets) do _idx = dlist[i] - do_notification_for_idx_offset(i, history.stack[_idx][1], _idx, dlist) - -- Unset client from prevnext list. - local k = awful.util.table.hasitem(prevnextlist, _c) - if k then - -- cyclefocus.debug("SHOULD NOT HAPPEN: should be nil", 0) - prevnextlist[k] = false - end + display_entry_for_idx_offset(i, history.stack[_idx][1], _idx, dlist) end - local wa = screen[s].workarea + local wa = screen[initial_screen].workarea local h = wbox_height + container_margin_top_bottom*2 wbox:geometry({ height = h, @@ -1068,11 +1050,15 @@ end -- A helper method to wrap awful.key. -function cyclefocus.key(mods, key, startdirection, _args) - local mods = mods or {modkey} or {"Mod4"} - local key = key or "Tab" - local startdirection = startdirection or 1 - local args = awful.util.table.clone(_args) or {} +function cyclefocus.key(mods, key, startdirection_or_args, args) + mods = mods or {modkey} or {"Mod4"} + key = key or "Tab" + if type(startdirection_or_args) == 'number' then + awful.util.deprecate('startdirection is not used anymore: pass in mods, key, args', {raw=true}) + else + args = startdirection_or_args + end + args = awful.util.table.clone(args) or {} if not args.keys then if key == "Tab" then args.keys = {"Tab", "ISO_Left_Tab"} @@ -1085,7 +1071,7 @@ function cyclefocus.key(mods, key, startdirection, _args) return awful.key(mods, key, function(c) args.initiating_client = c -- only for clientkeys, might be nil! - cyclefocus.cycle(startdirection, args) + cyclefocus.cycle(args) end) end From a9d769f6550883549639dab67547e95512b809c3 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Tue, 14 Feb 2017 22:29:51 +0100 Subject: [PATCH 3/9] Travis: run luacheck --- .luacheckrc | 44 ++++++++++++++++++++++++++++++++++++++++++++ .travis.yml | 20 ++++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 .luacheckrc create mode 100644 .travis.yml diff --git a/.luacheckrc b/.luacheckrc new file mode 100644 index 0000000..8ee3bda --- /dev/null +++ b/.luacheckrc @@ -0,0 +1,44 @@ +-- Only allow symbols available in all Lua versions +std = "min" + +-- Get rid of "unused argument self"-warnings +self = false + +-- The default config may set global variables +-- files["init.lua"].allow_defined_top = true + +-- This file itself +files[".luacheckrc"].ignore = {"111", "112", "131"} + +-- Global objects defined by the C code +read_globals = { + "awesome", + "button", + "client", + "dbus", + "drawable", + "drawin", + "key", + "keygrabber", + "mousegrabber", + "root", + "selection", + "tag", + "window", + -- Global settings. + "modkey", +} + +-- screen may not be read-only, because newer luacheck versions complain about +-- screen[1].tags[1].selected = true. +-- The same happens with the following code: +-- local tags = mouse.screen.tags +-- tags[7].index = 4 +-- client may not be read-only due to client.focus. +globals = { + "screen", + "mouse", + "client" +} + +-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80 diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..f9e53f7 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,20 @@ +# Based on https://github.com/mpeterv/hererocks + +language: python +sudo: false + +env: + - LUA="lua 5.3" + +install: + - pip install hererocks + - hererocks env --$LUA -rlatest + - source env/bin/activate + - luarocks install luacheck + +script: + - luacheck *.lua + +branches: + only: + - master From c69e32d978c8b3c963caf4af362edd2b11d17c2e Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Tue, 14 Feb 2017 23:04:39 +0100 Subject: [PATCH 4/9] Fix entry_with_bg:fit call --- init.lua | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/init.lua b/init.lua index 8e0b2af..14a6cfa 100644 --- a/init.lua +++ b/init.lua @@ -993,15 +993,10 @@ cyclefocus.cycle = function(startdirection_or_args, args) end layout:add(entry_with_bg) - -- TODO: get context automatically?! - -- local context = wbox:get_widget_context() - -- local context = {dpi=176} - local context = {} - -- Add height to outer wibox. - -- Hack for newer API (https://github.com/awesomeWM/awesome/pull/398#issuecomment-134955805). - local _, entry_height = entry_with_bg:fit(context, wbox.width, 2^20) - wbox_height = wbox_height + entry_height + local context = {dpi=beautiful.xresources.get_dpi(initial_screen)} + local _, h = entry_with_bg:fit(context, wbox.width, 2^20) + wbox_height = wbox_height + h end -- }}} -- Get clients before and after currently selected one. From 8b37907ec8016e2370840051b8110a89c1797099 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Tue, 14 Feb 2017 23:17:51 +0100 Subject: [PATCH 5/9] Fix deprecation warnings --- init.lua | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/init.lua b/init.lua index 14a6cfa..95a410e 100644 --- a/init.lua +++ b/init.lua @@ -347,9 +347,8 @@ cyclefocus.load_on_startup = function() ignore_focus_signal = true history.load() if history.stack[1] then - -- bdebug({c=history.stack[1]}, 'load_on_startup::jump') showing_client = history.stack[1][1] - awful.client.jumpto(showing_client) + showing_client:jump_to() showing_client = nil end ignore_focus_signal = false @@ -403,13 +402,13 @@ end) -- NOTE: awful.client.jumpto also focuses the screen / resets the mouse. -- See https://github.com/blueyed/awesome-cyclefocus/issues/6 -- Based on awful.client.jumpto, without the code for mouse. --- Calls awful.tag.viewonly always to update the tag history, also when +-- Calls tag:viewonly always to update the tag history, also when -- the client is visible. local raise_client = function(c) -- Try to make client visible, this also covers e.g. sticky local t = c:tags()[1] if t then - awful.tag.viewonly(t) + t:view_only() end c:raise() end @@ -588,7 +587,7 @@ cyclefocus.show_client = function (c) c.ontop = true -- Make the clients tag visible, if it currently is not. - local sel_tags = awful.tag.selectedlist(c.screen) + local sel_tags = c.screen.selected_tags local c_tag = c.first_tag or c:tags()[1] if not awful.util.table.hasitem(sel_tags, c_tag) then -- Select only the client's first tag, after de-selecting @@ -600,7 +599,7 @@ cyclefocus.show_client = function (c) local restore_sticky = c.sticky c.sticky = true - for _, t in pairs(awful.tag.gettags(c.screen)) do + for _, t in pairs(c.screen.tags) do if t ~= c_tag then t.selected = false end @@ -663,9 +662,9 @@ cyclefocus.cycle = function(startdirection_or_args, args) -- Save list of selected tags for all screens. local restore_tag_selected = {} - for s = 1, capi.screen.count() do + for s in capi.screen do restore_tag_selected[s] = {} - for _,t in pairs(awful.tag.gettags(s)) do + for _,t in pairs(s.tags) do restore_tag_selected[s][t] = t.selected end end @@ -824,8 +823,8 @@ cyclefocus.cycle = function(startdirection_or_args, args) if key == 'Escape' then -- Restore previously selected tags for screen. if restore_tag_selected then - for s = 1, capi.screen.count() do - for _,t in pairs(awful.tag.gettags(s)) do + for s in capi.screen do + for _,t in pairs(s.tags) do t.selected = restore_tag_selected[s][t] end end @@ -889,11 +888,11 @@ cyclefocus.cycle = function(startdirection_or_args, args) wbox:set_bg("#ffffff00") local container_inner = wibox.layout.align.vertical() - local container_layout = wibox.layout.margin( + local container_layout = wibox.container.margin( container_inner, container_margin_left_right, container_margin_left_right, container_margin_top_bottom, container_margin_top_bottom) - container_layout = wibox.widget.background(container_layout) + container_layout = wibox.container.background(container_layout) container_layout:set_bg(beautiful.bg_normal..'cc') -- constraint:set_widget(layout) @@ -963,7 +962,7 @@ cyclefocus.cycle = function(startdirection_or_args, args) end -- Margin. - iconmarginbox = wibox.layout.margin(iconbox) + iconmarginbox = wibox.container.margin(iconbox) iconmarginbox:set_margins(icon_margin) iconbox:set_resize(false) @@ -977,13 +976,13 @@ cyclefocus.cycle = function(startdirection_or_args, args) textbox:set_font(preset.font) textbox:set_wrap("word_char") textbox:set_ellipsize("middle") - local textbox_margin = wibox.layout.margin(textbox) + local textbox_margin = wibox.container.margin(textbox) textbox_margin:set_margins(dpi(5)) entry_layout:add(textbox_margin) - entry_layout = wibox.layout.margin(entry_layout, dpi(5), dpi(5), + entry_layout = wibox.container.margin(entry_layout, dpi(5), dpi(5), dpi(2), dpi(2)) - local entry_with_bg = wibox.widget.background(entry_layout) + local entry_with_bg = wibox.container.background(entry_layout) if offset == 0 then entry_with_bg:set_fg(beautiful.fg_focus) entry_with_bg:set_bg(beautiful.bg_focus) From 1c481e062259bb73a10c2611cb8b75e29828a11d Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Sun, 2 Apr 2017 16:53:54 +0200 Subject: [PATCH 6/9] Improve moving/restoring mouse --- init.lua | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/init.lua b/init.lua index 95a410e..01fe587 100644 --- a/init.lua +++ b/init.lua @@ -410,7 +410,7 @@ local raise_client = function(c) if t then t:view_only() end - c:raise() + c:jump_to() end @@ -771,11 +771,15 @@ cyclefocus.cycle = function(startdirection_or_args, args) local initial_screen = awful.screen.focused and awful.screen.focused() or mouse.screen -- Move mouse pointer away to avoid sloppy focus kicking in. - -- TODO: go to current screen's 0/0 (not total). Have an option/method for this! local restore_mouse_coords if show_clients then - restore_mouse_coords = capi.mouse.coords() - capi.mouse.coords({ x = 0, y = 0 }, true) + local s = capi.screen[capi.mouse.screen] + local coords = capi.mouse.coords() + restore_mouse_coords = {s = s, x = coords.x, y = coords.y} + local pos = {x = s.geometry.x, y = s.geometry.y} + -- move cursor without triggering signals mouse::enter and mouse::leave + capi.mouse.coords(pos, true) + restore_mouse_coords.moved = pos end capi.keygrabber.run(function(mod, key, event) @@ -792,23 +796,25 @@ cyclefocus.cycle = function(startdirection_or_args, args) if show_clients then show_clients() end + + -- Restore mouse if it has not been moved during cycling. + if restore_mouse_coords then + if restore_mouse_coords.s == capi.screen[capi.mouse.screen] then + local coords = capi.mouse.coords() + local moved_coords = restore_mouse_coords.moved + if moved_coords.x == coords.x and moved_coords.y == coords.y then + capi.mouse.coords({x = restore_mouse_coords.x, y = restore_mouse_coords.y}, true) + end + end + end + if c then showing_client = c - -- NOTE: awful.client.jumpto(c) resets mouse. - capi.client.focus = c raise_client(c) if c ~= orig_client then history.movetotop(c) end end - - -- Restore mouse if it has not been moved during cycling. - if restore_mouse_coords then - local coords = capi.mouse.coords() - if coords.x == 0 and coords.y == 0 then - capi.mouse.coords(restore_mouse_coords, true) - end - end ignore_focus_signal = false return true From 8816ba94b2001ca729b592ed32af2d9e178fbd08 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Wed, 26 Apr 2017 23:15:55 +0200 Subject: [PATCH 7/9] README: Donate header --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 9f1c18d..e922aee 100644 --- a/README.md +++ b/README.md @@ -337,6 +337,8 @@ You can report bugs and wishes at the [Github issue tracker][]. Pull requests would be awesome! :) +## Donate + [![Flattr this git repo](http://api.flattr.com/button/flattr-badge-large.png)](https://flattr.com/submit/auto?user_id=blueyed&url=https://github.com/blueyed/awesome-cyclefocus&title=awesome-cyclefocus&language=en&tags=github&category=software) Bitcoin: 16EVhEpXxfNiT93qT2uxo4DsZSHzNdysSp From 423990b3b528e6f8df88494197774a08e32cd27e Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Wed, 26 Apr 2017 23:16:51 +0200 Subject: [PATCH 8/9] Improve restoring of previously selected tags --- init.lua | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/init.lua b/init.lua index 01fe587..882db3c 100644 --- a/init.lua +++ b/init.lua @@ -797,6 +797,19 @@ cyclefocus.cycle = function(startdirection_or_args, args) show_clients() end + -- Restore previously selected tags for screen(s). + -- With a given client, handle other screens first, otherwise + -- the focus might be on the wrong screen. + if restore_tag_selected then + for s in capi.screen do + if not c or s ~= c.screen then + for _,t in pairs(s.tags) do + t.selected = restore_tag_selected[s][t] + end + end + end + end + -- Restore mouse if it has not been moved during cycling. if restore_mouse_coords then if restore_mouse_coords.s == capi.screen[capi.mouse.screen] then @@ -827,15 +840,6 @@ cyclefocus.cycle = function(startdirection_or_args, args) -- Abort on Escape. if key == 'Escape' then - -- Restore previously selected tags for screen. - if restore_tag_selected then - for s in capi.screen do - for _,t in pairs(s.tags) do - t.selected = restore_tag_selected[s][t] - end - end - end - return exit_grabber(orig_client) end From a349fb1d1fbe42d52baddf6d677a86ae325624c5 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Thu, 27 Apr 2017 00:14:13 +0200 Subject: [PATCH 9/9] doc update --- README.md | 80 +++++++++++++++++++++++++++++++------------------------ init.lua | 37 ++++++++++++------------- 2 files changed, 62 insertions(+), 55 deletions(-) diff --git a/README.md b/README.md index e922aee..833063f 100644 --- a/README.md +++ b/README.md @@ -214,36 +214,35 @@ The default settings are: ```lua cyclefocus = { - -- Should clients be raised during cycling? - raise_clients = true, - -- Should clients be focused during cycling? + -- Should clients get shown during cycling? + -- This should be a function (or `false` to disable showing clients), which + -- receives a client object, and can make use of cyclefocus.show_client + -- (the default implementation). + show_clients = true, + -- Should clients get focused during cycling? + -- This is required for the tasklist to highlight the selected entry. focus_clients = true, -- How many entries should get displayed before and after the current one? - display_next_count = 2, - display_prev_count = 2, -- only 0 for prev, works better with naughty notifications. - - -- Preset to be used for the notification. - naughty_preset = { - position = 'top_left', - timeout = 0, - }, - - naughty_preset_for_offset = { - -- Default callback, which will be applied for all offsets (first). + display_next_count = 3, + display_prev_count = 3, + + -- Default preset to for entries. + -- `preset_for_offset` (below) gets added to it. + default_preset = {}, + + --- Templates for entries in the list. + -- The following arguments get passed to a callback: + -- - client: the current client object. + -- - idx: index number of current entry in clients list. + -- - displayed_list: the list of entries in the list, possibly filtered. + preset_for_offset = { + -- Default callback, which will gets applied for all offsets (first). default = function (preset, args) -- Default font and icon size (gets overwritten for current/0 index). preset.font = 'sans 8' preset.icon_size = 36 - preset.text = escape_markup(cyclefocus.get_object_name(args.client)) - - -- Display the notification on the current screen (mouse). - preset.screen = capi.mouse.screen - - -- Set notification width, based on screen/workarea width. - local s = preset.screen - local wa = capi.screen[s].workarea - preset.width = floor(wa.width * 0.618) + preset.text = escape_markup(cyclefocus.get_client_title(args.client, false)) preset.icon = cyclefocus.icon_loader(args.client.icon) end, @@ -252,31 +251,42 @@ cyclefocus = { ["0"] = function (preset, args) preset.font = 'sans 12' preset.icon_size = 48 - -- Use get_object_name to handle .name=nil. - preset.text = escape_markup(cyclefocus.get_object_name(args.client)) - -- Add screen number if there are multiple. + preset.text = escape_markup(cyclefocus.get_client_title(args.client, true)) + -- Add screen number if there is more than one. if screen.count() > 1 then - preset.text = preset.text .. " [screen " .. args.client.screen .. "]" + preset.text = preset.text .. " [screen " .. tostring(args.client.screen.index) .. "]" end preset.text = preset.text .. " [#" .. args.idx .. "] " preset.text = '' .. preset.text .. '' end, -- You can refer to entries by their offset. - ["-1"] = function (preset, args) - -- preset.icon_size = 32 - end, - ["1"] = function (preset, args) - -- preset.icon_size = 32 - end + -- ["-1"] = function (preset, args) + -- -- preset.icon_size = 32 + -- end, + -- ["1"] = function (preset, args) + -- -- preset.icon_size = 32 + -- end }, -- Default builtin filters. - -- These are meant to get applied always, but you could override them. + -- (meant to get applied always, but you could override them) cycle_filters = { - function(c, source_c) return not c.minimized end, + function(c, source_c) return not c.minimized end, --luacheck: no unused args }, + -- EXPERIMENTAL: only add clients to the history that have been focused by + -- cyclefocus. + -- This allows to switch clients using other methods, but those are then + -- not added to cyclefocus' internal history. + -- The get_next_client function will then first consider the most recent + -- entry in the history stack, if it's not focused currently. + -- + -- You can use cyclefocus.history.add to manually add an entry, or + -- cyclefocus.history.append if you want to add it to the end of the stack. + -- This might be useful in a request::activate signal handler. + -- only_add_internal_focus_changes_to_history = true, + -- The filter to ignore clients altogether (get not added to the history stack). -- This is different from the cycle_filters. -- The function should return true / the client if it's ok, nil otherwise. diff --git a/init.lua b/init.lua index 882db3c..edb1691 100644 --- a/init.lua +++ b/init.lua @@ -34,32 +34,30 @@ end -- Configuration. This can be overridden: global or via args to cyclefocus.cycle. local cyclefocus cyclefocus = { - -- Should clients be shown during cycling? This should be a function, - -- which receives a client object, and can make use of - -- cyclefocus.show_client (the default implementation). - -- Use false to disable showing clients. + -- Should clients get shown during cycling? + -- This should be a function (or `false` to disable showing clients), which + -- receives a client object, and can make use of cyclefocus.show_client + -- (the default implementation). show_clients = true, - -- Should clients be focused during cycling? + -- Should clients get focused during cycling? -- This is required for the tasklist to highlight the selected entry. focus_clients = true, -- How many entries should get displayed before and after the current one? display_next_count = 3, - display_prev_count = 3, -- only 0 for prev, works better with naughty notifications. + display_prev_count = 3, - -- Preset to be used for the notification. - naughty_preset = { - position = 'top_left', - timeout = 0, - }, + -- Default preset to for entries. + -- `preset_for_offset` (below) gets added to it. + default_preset = {}, - --- Templates for naughty notifications. - -- The following arguments are passed to a callback: + --- Templates for entries in the list. + -- The following arguments get passed to a callback: -- - client: the current client object. -- - idx: index number of current entry in clients list. - -- - displayed_list: the list of entries in the list, might be filtered. + -- - displayed_list: the list of entries in the list, possibly filtered. preset_for_offset = { - -- Default callback, which will be applied for all offsets (first). + -- Default callback, which will gets applied for all offsets (first). default = function (preset, args) -- Default font and icon size (gets overwritten for current/0 index). preset.font = 'sans 8' @@ -74,7 +72,7 @@ cyclefocus = { preset.font = 'sans 12' preset.icon_size = 48 preset.text = escape_markup(cyclefocus.get_client_title(args.client, true)) - -- Add screen number if there are multiple. + -- Add screen number if there is more than one. if screen.count() > 1 then preset.text = preset.text .. " [screen " .. tostring(args.client.screen.index) .. "]" end @@ -90,15 +88,14 @@ cyclefocus = { -- -- preset.icon_size = 32 -- end }, - -- naughty_preset_for_offset = cyclefocus.preset, -- DEPRECATED -- Default builtin filters. - -- These are meant to get applied always, but you could override them. + -- (meant to get applied always, but you could override them) cycle_filters = { function(c, source_c) return not c.minimized end, --luacheck: no unused args }, - -- EXPERIMENTAL: Only add clients to the history that have been focused by + -- EXPERIMENTAL: only add clients to the history that have been focused by -- cyclefocus. -- This allows to switch clients using other methods, but those are then -- not added to cyclefocus' internal history. @@ -931,7 +928,7 @@ cyclefocus.cycle = function(startdirection_or_args, args) -- Create entry with index, name and screen. local display_entry_for_idx_offset = function(offset, c, _idx, displayed_list) -- {{{ - local preset = awful.util.table.clone(args.naughty_preset) + local preset = awful.util.table.clone(args.default_preset) -- Callback. local args_for_cb = {