Browse Source

Merge pull request #2529 from actionless/hotkeys-popup-dont-show-empty-groups

fix(awful: hotkeys_popup): don't show group label if group itself is empty
mergify[bot] 1 month ago
parent
commit
344964a44a
No account linked to committer's email address
3 changed files with 142 additions and 91 deletions
  1. 1
    0
      .travis.yml
  2. 96
    90
      lib/awful/hotkeys_popup/widget.lua
  3. 45
    1
      tests/test-awesomerc.lua

+ 1
- 0
.travis.yml View File

@@ -42,6 +42,7 @@ addons:
42 42
       - xvfb
43 43
       - zsh
44 44
       - x11-apps
45
+      - vim-nox
45 46
       # Need xorg-macros
46 47
       - xutils-dev
47 48
       # lgi.

+ 96
- 90
lib/awful/hotkeys_popup/widget.lua View File

@@ -310,109 +310,115 @@ function widget.new(args)
310 310
         return margin
311 311
     end
312 312
 
313
+    function widget_instance:_create_group_columns(column_layouts, group, keys, s, wibox_height)
314
+        local line_height = beautiful.get_font_height(self.font)
315
+        local group_label_height = line_height + self.group_margin
316
+        -- -1 for possible pagination:
317
+        local max_height_px = wibox_height - group_label_height
318
+
319
+        local joined_descriptions = ""
320
+        for i, key in ipairs(keys) do
321
+            joined_descriptions = joined_descriptions .. key.description .. (i~=#keys and "\n" or "")
322
+        end
323
+        -- +1 for group label:
324
+        local items_height = gstring.linecount(joined_descriptions) * line_height + group_label_height
325
+        local current_column
326
+        local available_height_px = max_height_px
327
+        local add_new_column = true
328
+        for i, column in ipairs(column_layouts) do
329
+            if ((column.height_px + items_height) < max_height_px) or
330
+                (i == #column_layouts and column.height_px < max_height_px / 2)
331
+            then
332
+                current_column = column
333
+                add_new_column = false
334
+                available_height_px = max_height_px - current_column.height_px
335
+                break
336
+            end
337
+        end
338
+        local overlap_leftovers
339
+        if items_height > available_height_px then
340
+            local new_keys = {}
341
+            overlap_leftovers = {}
342
+            -- +1 for group title and +1 for possible hyphen (v):
343
+            local available_height_items = (available_height_px - group_label_height*2) / line_height
344
+            for i=1,#keys do
345
+                table.insert(((i<available_height_items) and new_keys or overlap_leftovers), keys[i])
346
+            end
347
+            keys = new_keys
348
+            table.insert(keys, {key=markup.fg(self.modifiers_fg, "▽"), description=""})
349
+        end
350
+        if not current_column then
351
+            current_column = {layout=wibox.layout.fixed.vertical()}
352
+        end
353
+        current_column.layout:add(self:_group_label(group))
354
+
355
+        local function insert_keys(_keys, _add_new_column)
356
+            local max_label_width = 0
357
+            local max_label_content = ""
358
+            local joined_labels = ""
359
+            for i, key in ipairs(_keys) do
360
+                local length = string.len(key.key or '') + string.len(key.description or '')
361
+                local modifiers = key.mod
362
+                if not modifiers or modifiers == "none" then
363
+                    modifiers = ""
364
+                else
365
+                    length = length + string.len(modifiers) + 1 -- +1 for "+" character
366
+                    modifiers = markup.fg(self.modifiers_fg, modifiers.."+")
367
+                end
368
+                local rendered_hotkey = markup.font(self.font,
369
+                    modifiers .. (key.key or "") .. " "
370
+                ) .. markup.font(self.description_font,
371
+                    key.description or ""
372
+                )
373
+                if length > max_label_width then
374
+                    max_label_width = length
375
+                    max_label_content = rendered_hotkey
376
+                end
377
+                joined_labels = joined_labels .. rendered_hotkey .. (i~=#_keys and "\n" or "")
378
+                end
379
+            current_column.layout:add(wibox.widget.textbox(joined_labels))
380
+            local max_width, _ = wibox.widget.textbox(max_label_content):get_preferred_size(s)
381
+            max_width = max_width + self.group_margin
382
+            if not current_column.max_width or max_width > current_column.max_width then
383
+                current_column.max_width = max_width
384
+            end
385
+            -- +1 for group label:
386
+            current_column.height_px = (current_column.height_px or 0) +
387
+                gstring.linecount(joined_labels)*line_height + group_label_height
388
+            if _add_new_column then
389
+                table.insert(column_layouts, current_column)
390
+            end
391
+        end
392
+
393
+        insert_keys(keys, add_new_column)
394
+        if overlap_leftovers then
395
+            current_column = {layout=wibox.layout.fixed.vertical()}
396
+            insert_keys(overlap_leftovers, true)
397
+        end
398
+    end
313 399
 
314 400
     function widget_instance:_create_wibox(s, available_groups, show_awesome_keys)
315 401
         s = get_screen(s)
316 402
         local wa = s.workarea
317
-        local height = (self.height < wa.height) and self.height or
403
+        local wibox_height = (self.height < wa.height) and self.height or
318 404
             (wa.height - self.border_width * 2)
319
-        local width = (self.width < wa.width) and self.width or
405
+        local wibox_width = (self.width < wa.width) and self.width or
320 406
             (wa.width - self.border_width * 2)
321 407
 
322 408
         -- arrange hotkey groups into columns
323
-        local line_height = beautiful.get_font_height(self.font)
324
-        local group_label_height = line_height + self.group_margin
325
-        -- -1 for possible pagination:
326
-        local max_height_px = height - group_label_height
327 409
         local column_layouts = {}
328 410
         for _, group in ipairs(available_groups) do
329 411
             local keys = gtable.join(
330 412
                 show_awesome_keys and self._cached_awful_keys[group] or nil,
331 413
                 self._additional_hotkeys[group]
332 414
             )
333
-            local joined_descriptions = ""
334
-            for i, key in ipairs(keys) do
335
-                joined_descriptions = joined_descriptions .. key.description .. (i~=#keys and "\n" or "")
336
-            end
337
-            -- +1 for group label:
338
-            local items_height = gstring.linecount(joined_descriptions) * line_height + group_label_height
339
-            local current_column
340
-            local available_height_px = max_height_px
341
-            local add_new_column = true
342
-            for i, column in ipairs(column_layouts) do
343
-                if ((column.height_px + items_height) < max_height_px) or
344
-                    (i == #column_layouts and column.height_px < max_height_px / 2)
345
-                then
346
-                    current_column = column
347
-                    add_new_column = false
348
-                    available_height_px = max_height_px - current_column.height_px
349
-                    break
350
-                end
351
-            end
352
-            local overlap_leftovers
353
-            if items_height > available_height_px then
354
-                local new_keys = {}
355
-                overlap_leftovers = {}
356
-                -- +1 for group title and +1 for possible hyphen (v):
357
-                local available_height_items = (available_height_px - group_label_height*2) / line_height
358
-                for i=1,#keys do
359
-                    table.insert(((i<available_height_items) and new_keys or overlap_leftovers), keys[i])
360
-                end
361
-                keys = new_keys
362
-                table.insert(keys, {key=markup.fg(self.modifiers_fg, "▽"), description=""})
363
-            end
364
-            if not current_column then
365
-                current_column = {layout=wibox.layout.fixed.vertical()}
366
-            end
367
-            current_column.layout:add(self:_group_label(group))
368
-
369
-            local function insert_keys(_keys, _add_new_column)
370
-                local max_label_width = 0
371
-                local max_label_content = ""
372
-                local joined_labels = ""
373
-                for i, key in ipairs(_keys) do
374
-                    local length = string.len(key.key or '') + string.len(key.description or '')
375
-                    local modifiers = key.mod
376
-                    if not modifiers or modifiers == "none" then
377
-                        modifiers = ""
378
-                    else
379
-                        length = length + string.len(modifiers) + 1 -- +1 for "+" character
380
-                        modifiers = markup.fg(self.modifiers_fg, modifiers.."+")
381
-                    end
382
-                    local rendered_hotkey = markup.font(self.font,
383
-                        modifiers .. (key.key or "") .. " "
384
-                    ) .. markup.font(self.description_font,
385
-                        key.description or ""
386
-                    )
387
-                    if length > max_label_width then
388
-                        max_label_width = length
389
-                        max_label_content = rendered_hotkey
390
-                    end
391
-                    joined_labels = joined_labels .. rendered_hotkey .. (i~=#_keys and "\n" or "")
392
-                    end
393
-                current_column.layout:add(wibox.widget.textbox(joined_labels))
394
-                local max_width, _ = wibox.widget.textbox(max_label_content):get_preferred_size(s)
395
-                max_width = max_width + self.group_margin
396
-                if not current_column.max_width or max_width > current_column.max_width then
397
-                    current_column.max_width = max_width
398
-                end
399
-                -- +1 for group label:
400
-                current_column.height_px = (current_column.height_px or 0) +
401
-                    gstring.linecount(joined_labels)*line_height + group_label_height
402
-                if _add_new_column then
403
-                    table.insert(column_layouts, current_column)
404
-                end
405
-            end
406
-
407
-            insert_keys(keys, add_new_column)
408
-            if overlap_leftovers then
409
-                current_column = {layout=wibox.layout.fixed.vertical()}
410
-                insert_keys(overlap_leftovers, true)
415
+            if #keys > 0 then
416
+                self:_create_group_columns(column_layouts, group, keys, s, wibox_height)
411 417
             end
412 418
         end
413 419
 
414 420
         -- arrange columns into pages
415
-        local available_width_px = width
421
+        local available_width_px = wibox_width
416 422
         local pages = {}
417 423
         local columns = wibox.layout.fixed.horizontal()
418 424
         local previous_page_last_layout
@@ -423,7 +429,7 @@ function widget.new(args)
423 429
                 )
424 430
                 table.insert(pages, columns)
425 431
                 columns = wibox.layout.fixed.horizontal()
426
-                available_width_px = width - item.max_width
432
+                available_width_px = wibox_width - item.max_width
427 433
                 item.layout:insert(
428 434
                     1, self:_group_label("PgUp - Prev Page", self.label_bg)
429 435
                 )
@@ -452,10 +458,10 @@ function widget.new(args)
452 458
             wibox = mywibox,
453 459
         }
454 460
         mywibox:geometry({
455
-            x = wa.x + math.floor((wa.width - width - self.border_width*2) / 2),
456
-            y = wa.y + math.floor((wa.height - height - self.border_width*2) / 2),
457
-            width = width,
458
-            height = height,
461
+            x = wa.x + math.floor((wa.width - wibox_width - self.border_width*2) / 2),
462
+            y = wa.y + math.floor((wa.height - wibox_height - self.border_width*2) / 2),
463
+            width = wibox_width,
464
+            height = wibox_height,
459 465
         })
460 466
         mywibox:set_widget(pages[1])
461 467
 

+ 45
- 1
tests/test-awesomerc.lua View File

@@ -24,6 +24,8 @@ local function num_pairs(container_table)
24 24
   return number_of_items
25 25
 end
26 26
 
27
+local test_context = {}
28
+
27 29
 local steps = {
28 30
     function(count)
29 31
         if count <= 5 then
@@ -239,11 +241,22 @@ local steps = {
239 241
         elseif count == 2 then
240 242
             assert(num_pairs(cached_wiboxes) > 0)
241 243
             assert(num_pairs(cached_wiboxes[s]) == 1)
244
+
245
+        elseif (
246
+            test_context.hotkeys01_count_vim and
247
+            (count - test_context.hotkeys01_count_vim) == 2
248
+        ) then
249
+            -- new wibox instance should be generated for including vim hotkeys:
250
+            assert(num_pairs(cached_wiboxes[s]) == 2)
242 251
         end
243 252
 
244 253
         local hotkeys_wibox
254
+        local visible_hotkeys_widget
245 255
         for _, widget in pairs(cached_wiboxes[s]) do
246 256
             hotkeys_wibox = widget.wibox
257
+            if hotkeys_wibox.visible then
258
+                visible_hotkeys_widget = widget
259
+            end
247 260
         end
248 261
 
249 262
         if count == 2 then
@@ -255,7 +268,38 @@ local steps = {
255 268
         elseif count == 3 then
256 269
             assert(not hotkeys_wibox.visible)
257 270
             root.fake_input("key_release", "Super_L")
258
-            return true
271
+            -- now let's run vim so hotkeys widget will show hotkeys for it:
272
+            test_context.hotkeys01_clients_before = #client.get()
273
+            awful.spawn("xterm -e vim")
274
+
275
+        elseif not test_context.hotkeys01_count_vim then
276
+            -- if xterm with vim got already opened:
277
+            if (
278
+                    test_context.hotkeys01_clients_before and
279
+                    test_context.hotkeys01_clients_before < #client.get()
280
+            ) then
281
+                -- open hotkeys popup with vim hotkeys:
282
+                awful.key.execute({modkey}, "s")
283
+                test_context.hotkeys01_count_vim = count
284
+            end
285
+
286
+        elseif test_context.hotkeys01_count_vim then
287
+            if (count - test_context.hotkeys01_count_vim) == 1 then
288
+                assert(visible_hotkeys_widget ~= nil)
289
+                assert(visible_hotkeys_widget.current_page == 1)
290
+                -- Should change the page on PgDn:
291
+                root.fake_input("key_press", "Next")
292
+            elseif (count - test_context.hotkeys01_count_vim) == 2 then
293
+                assert(visible_hotkeys_widget ~= nil)
294
+                assert(visible_hotkeys_widget.current_page == 2)
295
+                root.fake_input("key_release", "Next")
296
+                -- Should disappear on anykey
297
+                root.fake_input("key_press", "Super_L")
298
+            elseif (count - test_context.hotkeys01_count_vim) == 3 then
299
+                assert(not visible_hotkeys_widget)
300
+                root.fake_input("key_release", "Super_L")
301
+                return true
302
+            end
259 303
         end
260 304
     end,
261 305
 

Loading…
Cancel
Save