Created volume widget types

master
Mark 2022-04-24 08:27:47 -07:00
parent f945f98cb6
commit b7e3263d85
Signed by: Mark
GPG Key ID: AD62BB059C2AAEE4
3 changed files with 166 additions and 61 deletions

View File

@ -1,7 +1,40 @@
local P = {}
function P.new()
function P:set_value(value)
-- Set widget value. (0 - 100)
self.arc.value = value
if value > 70 then
self.icon.image = beautiful.icons.volume.high
elseif value > 40 then
self.icon.image = beautiful.icons.volume.medium
elseif value > 0 then
self.icon.image = beautiful.icons.volume.low
elseif value == 0 then
self.icon.image = beautiful.icons.volume.off
end
end
function P:set_state(state)
-- Set widget value. (0 - 100)
if (state == "error") then
self.icon.image = beautiful.icons.volume.error
elseif (state == "muted") then
self.icon.image = beautiful.icons.volume.mute
elseif (state == "unmuted") then
return
end
end
function P:set_tooltip(text)
-- Set widget value. (0 - 100)
self.tooltip.text = text
end
function P:new()
widget = {}
setmetatable(widget, self)
self.__index = self
widget.icon = wibox.widget {
resize = true,
@ -16,7 +49,7 @@ function P.new()
bottom = beautiful.dpi(1),
layout = wibox.container.margin,
},
max_value = 1,
max_value = 100,
thickness = beautiful.dpi(4),
start_angle = 4.71238898, -- 2pi*3/4
--forced_height = beautiful.dpi(16),
@ -27,26 +60,6 @@ function P.new()
widget = wibox.container.arcchart
}
--[[
widget.bar = wibox.widget {
max_value = 100,
value = 0,
--forced_height = 20,
forced_width = beautiful.dpi(50),
paddings = 0,
border_width = 0,
color = {
type = "linear",
from = {0, 0}, to = {beautiful.dpi(50), 0},
stops = { { 0, "#27D4CC" }, { 1, "#00446B" }}
},
background_color = "#FFFFFF30",
widget = wibox.widget.progressbar,
shape = gears.shape.rounded_bar,
}
}
--]]
widget.widget = wibox.widget {
{
{ -- Right space
@ -70,6 +83,11 @@ function P.new()
layout = wibox.container.background,
}
widget.tooltip = awful.tooltip {
objects = { widget.widget },
text = ""
}
widget.widget:connect_signal("mouse::enter", function(result)
widget.widget.bg = beautiful.color.bar.hover_bg
end)

86
volume/bar_widget.lua Normal file
View File

@ -0,0 +1,86 @@
local P = {}
function P:set_value(value)
-- Set widget value. (0 - 100)
self.bar.value = value
end
function P:set_state(state)
-- Set widget value. (0 - 100)
if (state == "error") then
return
elseif (state == "muted") then
return
elseif (state == "unmuted") then
return
end
end
function P:set_tooltip(text)
-- Set widget value. (0 - 100)
self.tooltip.text = text
end
function P:new()
widget = {}
setmetatable(widget, self)
self.__index = self
widget.bar = wibox.widget {
max_value = 100,
value = 0,
--forced_height = 20,
forced_width = beautiful.dpi(50),
paddings = 0,
border_width = 0,
color = {
type = "linear",
from = {0, 0}, to = {beautiful.dpi(50), 0},
stops = { { 0, "#27D4CC" }, { 1, "#00446B" }}
},
background_color = "#FFFFFF30",
widget = wibox.widget.progressbar,
shape = gears.shape.rounded_bar,
}
widget.widget = wibox.widget {
{
{ -- Right space
widget = wibox.widget.separator,
color = beautiful.color.transparent,
forced_width = beautiful.dpi(3)
},
{
widget.bar,
top = beautiful.dpi(2),
bottom = beautiful.dpi(2),
layout = wibox.container.margin,
},
{ -- Left space
widget = wibox.widget.separator,
color = beautiful.color.transparent,
forced_width = beautiful.dpi(3)
},
layout = wibox.layout.align.horizontal,
},
layout = wibox.container.background,
}
widget.tooltip = awful.tooltip {
objects = { widget.widget },
text = ""
}
widget.widget:connect_signal("mouse::enter", function(result)
widget.widget.bg = beautiful.color.bar.hover_bg
end)
widget.widget:connect_signal("mouse::leave", function(result)
widget.widget.bg = beautiful.color.transparent
end)
return widget
end
return P

View File

@ -1,4 +1,8 @@
local volume_widget = require("volume/widget")
local widget_types = {
bar = require("volume/bar_widget"),
arc = require("volume/arc_widget")
}
local P = {}
---
@ -29,10 +33,10 @@ function P:_get_status()
if (value == nil) then
self.volume = nil
self.ready = false
self._ready = false
else
self.volume = tonumber(string.format("% 3d", value))
self.ready = true
self._ready = true
end
self:_update_widget()
@ -46,33 +50,22 @@ end
function P:_update_widget()
if (not self.ready) then
self.widget.icon.image = beautiful.icons.volume.error
self.widget.arc.value = 100;
--self.widget.bar.value = 100;
self.widget.tooltip.text = "Volume error"
if (not self._ready) then
self.widget:set_state("error")
self.widget:set_value(self.max_value);
self.widget:set_tooltip("Volume error");
return
end
self.widget.arc.value = self.volume / 100;
self.widget.tooltip.text = "Volume " .. self.volume .. "%"
--self.widget.bar.value = self.volume;
self.widget:set_value(self.volume);
self.widget:set_state(self.muted)
self.widget:set_tooltip("Volume " .. self.volume .. "%");
if (self.muted) then
-- Set muted icon
self.widget.icon.image = beautiful.icons.volume.mute
self.widget:set_state("muted")
else
-- Set icon by value
if self.volume > 70 then
self.widget.icon.image = beautiful.icons.volume.high
elseif self.volume > 40 then
self.widget.icon.image = beautiful.icons.volume.medium
elseif self.volume > 0 then
self.widget.icon.image = beautiful.icons.volume.low
elseif self.volume == 0 then
self.widget.icon.image = beautiful.icons.volume.off
end
self.widget:set_state("unmuted")
end
end
@ -80,7 +73,7 @@ end
-- Simple actions
---
function P:volume_up()
if (not self.ready) or (self.volume >= 100) then
if (not self._ready) or (self.volume >= self.max_value) then
return
end
@ -91,14 +84,14 @@ function P:volume_up()
awful.spawn("pamixer --increase 5" .. self:_full_args(), false)
wrapper.sound.play("volume_up")
self.volume = self.volume + 5
if self.volume > 100 then
self.volume = 100
if self.volume > self.max_value then
self.volume = self.max_value
end
self:_update_widget()
end
function P:volume_down()
if (not self.ready) or (self.volume <= 0) then
if (not self._ready) or (self.volume <= 0) then
return
end
@ -116,7 +109,7 @@ function P:volume_down()
end
function P:volume_set(value)
if (not self.ready) then
if (not self._ready) then
return
end
@ -124,13 +117,21 @@ function P:volume_set(value)
self:unmute()
end
if (value < 0) then
value = 0
end
if (value > self.max_value) then
value = self.max_value
end
awful.spawn("pamixer --set_volume " .. tostring(value) .. self:_full_args(), false)
self.volume = value
self:_update_widget()
end
function P:mute()
if (not self.ready) then
if (not self._ready) then
return
end
@ -140,7 +141,7 @@ function P:mute()
end
function P:unmute()
if (not self.ready) then
if (not self._ready) then
return
end
@ -150,7 +151,7 @@ function P:unmute()
end
function P:toggle_mute()
if (not self.ready) then
if (not self._ready) then
return
end
@ -170,14 +171,19 @@ function P:new(args)
v = {
pa_options = args.cli_options or "",
pa_sink = args.pa_sink or "",
update_interval = args.update_interal or 1,
ready = false, -- is all the information in this class up-to-date?
update_interval = args.update_interal or 5,
enable_tooltip = args.enable_tooltip or true,
max_value = args.max_value or 100,
widget_type = args.widget_type or "arc",
_ready = false, -- is all the information in this class up-to-date?
-- if this is false, ui will show an error and most methods will
-- do nothing. Updated in _get_status()
}
-- Create widget for this volume interface
v.widget = volume_widget.new()
v.widget = widget_types[v.widget_type]:new()
-- Attach button press signals
v.widget.widget:connect_signal("button::press",
@ -192,11 +198,6 @@ function P:new(args)
end
)
v.widget.tooltip = awful.tooltip {
objects = { v.widget.widget },
text = "System volume"
}
setmetatable(v, self)
self.__index = self