lain icon indicating copy to clipboard operation
lain copied to clipboard

Show a pop-up when hovering the mouse over the battery widget

Open last5bits opened this issue 7 years ago • 3 comments

I'd like to share a snippet that shows a pop-up for the current battery status when the mouse cursor hovers over the widget. It was inspired by net_widgets/wireless.lua and looks like this:

image

local show_battery_popup = nil 
local hide_battery_popup = nil 
mybattery = lain.widget.bat({
  timeout = 5,
  settings = function()
    widget:set_markup(" ↯ " .. bat_now.perc)

    local function hide()
      if widget.notification ~= nil then
        naughty.destroy(widget.notification)
        widget.notification = nil 
      end 
    end 

    local function show()
      hide()
      mybattery:update()

      local msg = ""
      for i = 1, #bat_now.n_status do
        local format = "<span font_desc=\"monospace\">" ..
          "┌[Battery %d]\n" ..
          "├Status:\t%s\n" ..
          "└Percentage:\t%s\n\n" ..
          "</span>"
        msg = msg .. string.format(format, i - 1, bat_now.n_status[i], bat_now.n_perc[i])
      end 
      msg = msg .. "<span font_desc=\"monospace\">" ..
        "Time left:\t" .. bat_now.time .. "</span>"

      widget.notification = naughty.notify({
        text = msg,
        timeout = 0,
        screen = mouse.screen,
        position = "bottom_right"
      })  
    end 

    show_battery_popup = show
    hide_battery_popup = hide

  end 
})
widget:connect_signal('mouse::enter', function () show_battery_popup() end)
widget:connect_signal('mouse::leave', function () hide_battery_popup() end)

I'm fairly new to Lua so please let me know what you think. Thanks for the widgets and keep up the good work!

last5bits avatar Jun 22 '19 20:06 last5bits

Closing the issue since it is just a PSA.

last5bits avatar Jun 22 '19 20:06 last5bits

I'm fairly new to Lua so please let me know what you think.

Each 5 seconds you are re-creating hide() and show().

I would suggest to use awful.tooltip. Following is an untested snippet.

local batstat = {}

local mybattery = lain.widget.bat {
    timeout = 5,
    settings = function()
        widget:set_markup(" ↯ " .. bat_now.perc)
        batstat = bat_now
    end
}

local mybattery_t = awful.tooltip {
    objects = { mybattery.widget },
    timer_function = function()
        local msg = ""
        for i = 1, #batstat.n_status do
            msg = msg .. lain.util.markup.font("monospace 10",
            string.format("┌[Battery %d]\n├Status:\t%s\n└Percentage:\t%s\n\n",
            i-1, batstat.n_status[i], batstat.n_perc[i]))
        end
        return msg .. lain.util.markup.font("monospace 10", "Time left:\t" .. batstat.time)
    end
}

lcpz avatar Jun 23 '19 11:06 lcpz

Each 5 seconds you are re-creating hide() and show().

I would suggest to use awful.tooltip. Following is an untested snippet.

That works like a charm, thanks for the feedback!

last5bits avatar Jun 23 '19 14:06 last5bits