Created volume widget types
This commit is contained in:
		@ -1,7 +1,40 @@
 | 
				
			|||||||
local P = {}
 | 
					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 = {}
 | 
						widget = {}
 | 
				
			||||||
 | 
						setmetatable(widget, self)
 | 
				
			||||||
 | 
						self.__index = self
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	widget.icon = wibox.widget {
 | 
						widget.icon = wibox.widget {
 | 
				
			||||||
		resize = true,
 | 
							resize = true,
 | 
				
			||||||
@ -16,7 +49,7 @@ function P.new()
 | 
				
			|||||||
			bottom = beautiful.dpi(1),
 | 
								bottom = beautiful.dpi(1),
 | 
				
			||||||
			layout = wibox.container.margin,
 | 
								layout = wibox.container.margin,
 | 
				
			||||||
		},
 | 
							},
 | 
				
			||||||
		max_value = 1,
 | 
							max_value = 100,
 | 
				
			||||||
		thickness = beautiful.dpi(4),
 | 
							thickness = beautiful.dpi(4),
 | 
				
			||||||
		start_angle = 4.71238898, -- 2pi*3/4
 | 
							start_angle = 4.71238898, -- 2pi*3/4
 | 
				
			||||||
		--forced_height = beautiful.dpi(16),
 | 
							--forced_height = beautiful.dpi(16),
 | 
				
			||||||
@ -27,26 +60,6 @@ function P.new()
 | 
				
			|||||||
		widget = wibox.container.arcchart
 | 
							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 {
 | 
						widget.widget = wibox.widget {
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
			{   -- Right space
 | 
								{   -- Right space
 | 
				
			||||||
@ -70,6 +83,11 @@ function P.new()
 | 
				
			|||||||
		layout = wibox.container.background,
 | 
							layout = wibox.container.background,
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						widget.tooltip = awful.tooltip {
 | 
				
			||||||
 | 
							objects = { widget.widget },
 | 
				
			||||||
 | 
							text = ""
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	widget.widget:connect_signal("mouse::enter", function(result)
 | 
						widget.widget:connect_signal("mouse::enter", function(result)
 | 
				
			||||||
		widget.widget.bg = beautiful.color.bar.hover_bg
 | 
							widget.widget.bg = beautiful.color.bar.hover_bg
 | 
				
			||||||
	end)
 | 
						end)
 | 
				
			||||||
							
								
								
									
										86
									
								
								volume/bar_widget.lua
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										86
									
								
								volume/bar_widget.lua
									
									
									
									
									
										Normal 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
 | 
				
			||||||
@ -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 = {}
 | 
					local P = {}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
---
 | 
					---
 | 
				
			||||||
@ -29,10 +33,10 @@ function P:_get_status()
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
			if (value == nil) then
 | 
								if (value == nil) then
 | 
				
			||||||
				self.volume = nil
 | 
									self.volume = nil
 | 
				
			||||||
				self.ready = false
 | 
									self._ready = false
 | 
				
			||||||
			else
 | 
								else
 | 
				
			||||||
	 			self.volume = tonumber(string.format("% 3d", value))
 | 
						 			self.volume = tonumber(string.format("% 3d", value))
 | 
				
			||||||
				self.ready = true
 | 
									self._ready = true
 | 
				
			||||||
			end
 | 
								end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			self:_update_widget()
 | 
								self:_update_widget()
 | 
				
			||||||
@ -46,33 +50,22 @@ end
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function P:_update_widget()
 | 
					function P:_update_widget()
 | 
				
			||||||
	if (not self.ready) then
 | 
						if (not self._ready) then
 | 
				
			||||||
		self.widget.icon.image = beautiful.icons.volume.error
 | 
							self.widget:set_state("error")
 | 
				
			||||||
		self.widget.arc.value = 100;
 | 
							self.widget:set_value(self.max_value);
 | 
				
			||||||
		--self.widget.bar.value = 100;
 | 
							self.widget:set_tooltip("Volume error");
 | 
				
			||||||
		self.widget.tooltip.text = "Volume error"
 | 
					 | 
				
			||||||
		return
 | 
							return
 | 
				
			||||||
	end
 | 
						end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	self.widget.arc.value = self.volume / 100;
 | 
						self.widget:set_value(self.volume);
 | 
				
			||||||
	self.widget.tooltip.text = "Volume " .. self.volume .. "%"
 | 
						self.widget:set_state(self.muted)
 | 
				
			||||||
	--self.widget.bar.value = self.volume;
 | 
						self.widget:set_tooltip("Volume " .. self.volume .. "%");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if (self.muted) then
 | 
						if (self.muted) then
 | 
				
			||||||
		-- Set muted icon
 | 
							self.widget:set_state("muted")
 | 
				
			||||||
		self.widget.icon.image = beautiful.icons.volume.mute
 | 
					 | 
				
			||||||
	else
 | 
						else
 | 
				
			||||||
		-- Set icon by value
 | 
							self.widget:set_state("unmuted")
 | 
				
			||||||
		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
 | 
					 | 
				
			||||||
	end
 | 
						end
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -80,7 +73,7 @@ end
 | 
				
			|||||||
-- Simple actions
 | 
					-- Simple actions
 | 
				
			||||||
---
 | 
					---
 | 
				
			||||||
function P:volume_up()
 | 
					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
 | 
							return
 | 
				
			||||||
	end
 | 
						end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -91,14 +84,14 @@ function P:volume_up()
 | 
				
			|||||||
	awful.spawn("pamixer --increase 5" .. self:_full_args(), false)
 | 
						awful.spawn("pamixer --increase 5" .. self:_full_args(), false)
 | 
				
			||||||
	wrapper.sound.play("volume_up")
 | 
						wrapper.sound.play("volume_up")
 | 
				
			||||||
	self.volume = self.volume + 5
 | 
						self.volume = self.volume + 5
 | 
				
			||||||
	if self.volume > 100 then
 | 
						if self.volume > self.max_value then
 | 
				
			||||||
		self.volume = 100
 | 
							self.volume = self.max_value
 | 
				
			||||||
	end
 | 
						end
 | 
				
			||||||
	self:_update_widget()
 | 
						self:_update_widget()
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function P:volume_down()
 | 
					function P:volume_down()
 | 
				
			||||||
	if (not self.ready) or (self.volume <= 0) then
 | 
						if (not self._ready) or (self.volume <= 0) then
 | 
				
			||||||
		return
 | 
							return
 | 
				
			||||||
	end
 | 
						end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -116,7 +109,7 @@ function P:volume_down()
 | 
				
			|||||||
end
 | 
					end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function P:volume_set(value)
 | 
					function P:volume_set(value)
 | 
				
			||||||
	if (not self.ready) then
 | 
						if (not self._ready) then
 | 
				
			||||||
		return
 | 
							return
 | 
				
			||||||
	end
 | 
						end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -124,13 +117,21 @@ function P:volume_set(value)
 | 
				
			|||||||
		self:unmute()
 | 
							self:unmute()
 | 
				
			||||||
	end
 | 
						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)
 | 
						awful.spawn("pamixer --set_volume " .. tostring(value) .. self:_full_args(), false)
 | 
				
			||||||
	self.volume = value
 | 
						self.volume = value
 | 
				
			||||||
	self:_update_widget()
 | 
						self:_update_widget()
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function P:mute()
 | 
					function P:mute()
 | 
				
			||||||
	if (not self.ready) then
 | 
						if (not self._ready) then
 | 
				
			||||||
		return
 | 
							return
 | 
				
			||||||
	end
 | 
						end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -140,7 +141,7 @@ function P:mute()
 | 
				
			|||||||
end
 | 
					end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function P:unmute()
 | 
					function P:unmute()
 | 
				
			||||||
	if (not self.ready) then
 | 
						if (not self._ready) then
 | 
				
			||||||
		return
 | 
							return
 | 
				
			||||||
	end
 | 
						end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -150,7 +151,7 @@ function P:unmute()
 | 
				
			|||||||
end
 | 
					end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function P:toggle_mute()
 | 
					function P:toggle_mute()
 | 
				
			||||||
	if (not self.ready) then
 | 
						if (not self._ready) then
 | 
				
			||||||
		return
 | 
							return
 | 
				
			||||||
	end
 | 
						end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -170,14 +171,19 @@ function P:new(args)
 | 
				
			|||||||
	v = {
 | 
						v = {
 | 
				
			||||||
		pa_options = args.cli_options or "",
 | 
							pa_options = args.cli_options or "",
 | 
				
			||||||
		pa_sink = args.pa_sink or "",
 | 
							pa_sink = args.pa_sink or "",
 | 
				
			||||||
		update_interval = args.update_interal or 1,
 | 
							update_interval = args.update_interal or 5,
 | 
				
			||||||
		ready = false, -- is all the information in this class up-to-date?
 | 
							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
 | 
							-- if this is false, ui will show an error and most methods will
 | 
				
			||||||
		-- do nothing. Updated in _get_status()
 | 
							-- do nothing. Updated in _get_status()
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	-- Create widget for this volume interface
 | 
						-- Create widget for this volume interface
 | 
				
			||||||
	v.widget = volume_widget.new()
 | 
						v.widget = widget_types[v.widget_type]:new()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	-- Attach button press signals
 | 
						-- Attach button press signals
 | 
				
			||||||
	v.widget.widget:connect_signal("button::press",
 | 
						v.widget.widget:connect_signal("button::press",
 | 
				
			||||||
@ -192,11 +198,6 @@ function P:new(args)
 | 
				
			|||||||
		end
 | 
							end
 | 
				
			||||||
	)
 | 
						)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	v.widget.tooltip = awful.tooltip {
 | 
					 | 
				
			||||||
		objects = { v.widget.widget },
 | 
					 | 
				
			||||||
		text = "System volume"
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	setmetatable(v, self)
 | 
						setmetatable(v, self)
 | 
				
			||||||
	self.__index = self
 | 
						self.__index = self
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user