at master 3.8 kB view raw
1local beautiful = require("beautiful") 2local gears = require("gears") 3local awful = require("awful") 4local wibox = require("wibox") 5local naughty = require("naughty") 6 7local helpers = {} 8 9helpers.rrect = function(radius) 10 radius = radius or dpi(4) 11 return function(cr, width, height) 12 gears.shape.rounded_rect(cr, width, height, radius) 13 end 14end 15 16helpers.addhover = function(element, bg, hbg) 17 element:connect_signal("mouse::enter", function(self) 18 self.bg = hbg 19 end) 20 element:connect_signal("mouse::leave", function(self) 21 self.bg = bg 22 end) 23end 24 25helpers.colortext = function(txt, fg) 26 if fg == "" then 27 fg = beautiful.fg 28 end 29 30 return "<span foreground='" .. fg .. "'>" .. txt .. "</span>" 31end 32 33local filepath = "/tmp/awm_tag_storage" 34 35helpers.tag = { 36 store_current = function(tag) 37 if not tag then 38 return 39 end 40 local file = io.open(filepath, "w") 41 if file then 42 file:write(tostring(tag.index)) 43 file:close() 44 end 45 end, 46 read_last = function() 47 local file = io.open(filepath, "r") 48 if not file then 49 return nil 50 end 51 local content = file:read("*all") 52 file:close() 53 return content 54 end, 55} 56 57helpers.buffer = {} 58helpers.buffer.__index = helpers.buffer 59 60function helpers.buffer.new(max) 61 return setmetatable({ 62 data = {}, 63 max = max, 64 size = 0, 65 head = 1, 66 tail = 0, 67 }, helpers.buffer) 68end 69 70function helpers.buffer:push(v) 71 if self.size < self.max then 72 self.tail = (self.tail % self.max) + 1 73 self.data[self.tail] = v 74 self.size = self.size + 1 75 else 76 self.tail = (self.tail % self.max) + 1 77 self.data[self.tail] = v 78 self.head = (self.head % self.max) + 1 79 end 80end 81 82function helpers.buffer:items() 83 local pos = self.head 84 local remaining = self.size 85 local max = self.max 86 local data = self.data 87 local logical_i = 0 88 89 return function() 90 if remaining <= 0 then 91 return nil 92 end 93 94 logical_i = logical_i + 1 95 local v = data[pos] 96 97 pos = (pos % max) + 1 98 remaining = remaining - 1 99 100 return logical_i, v 101 end 102end 103 104helpers.table_contains = function(tbl, value_to_find) 105 for _, value in pairs(tbl) do 106 if value == value_to_find then 107 return true 108 end 109 end 110 return false 111end 112 113local menu = {} 114helpers.menu = menu 115local current_menu = nil 116 117menu.create = function(theme) 118 local w = awful.popup({ 119 widget = { 120 { 121 homogeneous = false, 122 column_count = 1, 123 spacing = 3, 124 minimum_column_width = 10, 125 minimum_row_height = 10, 126 layout = wibox.layout.grid, 127 id = "grid_id", 128 }, 129 widget = wibox.container.margin, 130 margins = theme.margins or 0, 131 }, 132 ontop = true, 133 visible = false, 134 border_width = theme.border_width or 0, 135 border_color = theme.border_color or beautiful.bg, 136 bg = theme.bg or beautiful.bg, 137 fg = theme.fg or beautiful.fg, 138 shape = helpers.rrect(theme.radius or 0), 139 }) 140 141 w.add_widget = function(self, widget) 142 self.widget:get_children_by_id("grid_id")[1]:add(widget) 143 end 144 145 function w:toggle(x, y) 146 -- Close any other open menu first 147 if current_menu and current_menu ~= self and current_menu.visible then 148 current_menu.visible = false 149 end 150 151 self.screen = mouse.screen 152 self.visible = not self.visible 153 154 if self.visible then 155 current_menu = self 156 if x and y then 157 -- Better positioning logic 158 local screen_geo = self.screen.geometry 159 160 -- Adjust Y if menu would go off screen 161 if y + self.height > screen_geo.height then 162 self.y = math.max(screen_geo.y, y - self.height) 163 else 164 self.y = y 165 end 166 167 -- Adjust X if menu would go off screen 168 if x + self.width > screen_geo.width then 169 self.x = math.max(screen_geo.x, x - self.width) 170 else 171 self.x = x 172 end 173 end 174 else 175 if current_menu == self then 176 current_menu = nil 177 end 178 end 179 end 180 181 function w:hide() 182 self.visible = false 183 if current_menu == self then 184 current_menu = nil 185 end 186 end 187 188 return w 189end 190 191return helpers