Browse Source

naughty: Add position "middle" to center notifications in screen (#2775)

Signed-off-by: Michael Beaumont <mjboamail@gmail.com>
Michael Beaumont 10 months ago
parent
commit
9e2a544ba3

+ 2
- 0
lib/naughty/core.lua View File

@@ -151,6 +151,7 @@ screen.connect_for_each_screen(function(s)
151 151
         bottom_left = {},
152 152
         bottom_middle = {},
153 153
         bottom_right = {},
154
+        middle = {},
154 155
     }
155 156
 end)
156 157
 
@@ -537,7 +538,7 @@ end
537 538
 -- @tparam[opt=focused] integer|screen args.screen Target screen for the notification.
538 539
 -- @string[opt="top_right"] args.position Corner of the workarea displaying the popups.
539 540
 --   Values: `"top_right"`, `"top_left"`, `"bottom_left"`,
541
+--   `"bottom_right"`, `"top_middle"`, `"bottom_middle"`, `"middle"`.
540 542
 -- @bool[opt=true] args.ontop Boolean forcing popups to display on top.
541 543
 -- @int[opt=`beautiful.notification_height` or auto] args.height Popup height.
542 544
 -- @int[opt=`beautiful.notification_width` or auto] args.width Popup width.

+ 29
- 10
lib/naughty/layout/legacy.lua View File

@@ -54,14 +54,37 @@ screen.connect_for_each_screen(function(s)
54 54
         bottom_left = {},
55 55
         bottom_middle = {},
56 56
         bottom_right = {},
57
+        middle = {},
57 58
     }
58 59
 end)
59 60
 
61
+--- Sum heights of notifications at position
62
+--
63
+-- @param s Screen to use
64
+-- @param position top_right | top_left | bottom_right | bottom_left
65
+--   | top_middle | bottom_middle | middle
66
+-- @param[opt] idx Index of last notification
67
+-- @return Height of notification stack with spacing
68
+local function get_total_heights(s, position, idx)
69
+    local sum = 0
70
+    local notifications = current_notifications[s][position]
71
+    idx = idx or #notifications
72
+    for i = 1, idx, 1 do
73
+        local n = notifications[i]
74
+
75
+        -- `n` will not nil when there is too many notifications to fit in `s`
76
+        if n then
77
+            sum = sum + n.height + naughty.config.spacing
78
+        end
79
+    end
80
+    return sum
81
+end
82
+
60 83
 --- Evaluate desired position of the notification by index - internal
61 84
 --
62 85
 -- @param s Screen to use
63 86
 -- @param position top_right | top_left | bottom_right | bottom_left
87
+--   | top_middle | bottom_middle | middle
64 88
 -- @param idx Index of the notification
65 89
 -- @param[opt] width Popup width.
66 90
 -- @param height Popup height
@@ -83,21 +106,16 @@ local function get_offset(s, position, idx, width, height)
83 106
     end
84 107
 
85 108
     -- calculate existing popups' height
86
-    local existing = 0
87
-    for i = 1, idx-1, 1 do
88
-        local n = current_notifications[s][position][i]
89
-
90
-        -- `n` will not nil when there is too many notifications to fit in `s`
91
-        if n then
92
-            existing = existing + n.height + naughty.config.spacing
93
-        end
94
-    end
109
+    local existing = get_total_heights(s, position, idx-1)
95 110
 
96 111
     -- calculate y
97 112
     if position:match("top") then
98 113
         v.y = ws.y + naughty.config.padding + existing
99
-    else
114
+    elseif position:match("bottom") then
100 115
         v.y = ws.y + ws.height - (naughty.config.padding + height + existing)
116
+    else
117
+        local total = get_total_heights(s, position)
118
+        v.y = ws.y + (ws.height - total) / 2 + naughty.config.padding + existing
101 119
     end
102 120
 
103 121
     -- Find old notification to replace in case there is not enough room.

+ 2
- 0
lib/naughty/notification.lua View File

@@ -105,6 +105,7 @@ local notification = {}
105 105
 -- * *bottom_right*
106 106
 -- * *top_middle*
107 107
 -- * *bottom_middle*
108
+-- * *middle*
108 109
 --
109 110
 --@DOC_awful_notification_corner_EXAMPLE@
110 111
 --
@@ -414,7 +415,7 @@ end
414 415
 -- @tparam[opt=focused] integer|screen args.screen Target screen for the notification.
415 416
 -- @string[opt="top_right"] args.position Corner of the workarea displaying the popups.
416 417
 --   Values: `"top_right"`, `"top_left"`, `"bottom_left"`,
418
+--   `"bottom_right"`, `"top_middle"`, `"bottom_middle"`, `"middle"`.
417 419
 -- @bool[opt=true] args.ontop Boolean forcing popups to display on top.
418 420
 -- @int[opt=`beautiful.notification_height` or auto] args.height Popup height.
419 421
 -- @int[opt=`beautiful.notification_width` or auto] args.width Popup width.

+ 1
- 0
tests/examples/awful/notification/corner.lua View File

@@ -8,6 +8,7 @@ for _, pos in ipairs {
8 8
     "bottom_left",
9 9
     "bottom_middle",
10 10
     "bottom_right",
11
+    "middle",
11 12
 } do
12 13
     naughty.notify {
13 14
         title    = pos,

+ 1
- 0
tests/test-naughty-legacy.lua View File

@@ -342,6 +342,7 @@ end
342 342
 local positions = {
343 343
     "top_left"      , "top_middle"    , "top_right"     ,
344 344
     "bottom_left"   , "bottom_middle" , "bottom_right"  ,
345
+    "middle"        ,
345 346
 }
346 347
 
347 348
 -- Test each corners.

Loading…
Cancel
Save