Cleaned up volume widget
This commit is contained in:
		| @ -8,9 +8,8 @@ return { | ||||
|  | ||||
| 	init = function () | ||||
| 		-- Make sure volume is unmuted | ||||
| 		volume.commands:unmute() | ||||
| 		-- Update volume widget at start | ||||
| 		volume.commands:get(widget.update) | ||||
| 		-- Implicitly updates widgets | ||||
| 		volume.commands.unmute() | ||||
| 	end, | ||||
|  | ||||
| 	for_each_screen = function (s) | ||||
|  | ||||
| @ -75,7 +75,7 @@ return function(s) | ||||
| 			bottom = beautiful.dpi(10), | ||||
| 			widget = wibox.container.margin | ||||
| 		}, | ||||
| 		bg ="#000000AA", | ||||
| 		bg ="#000000CC", | ||||
| 		widget = wibox.container.background, | ||||
| 		ontop = true, | ||||
| 		visible = true, | ||||
| @ -94,7 +94,7 @@ return function(s) | ||||
| 		screen = s, | ||||
|  | ||||
| 		placement = function(c) | ||||
| 			awful.placement.centered( | ||||
| 			awful.placement.bottom_right( | ||||
| 				c, | ||||
| 				{ | ||||
| 					honor_workarea = true, | ||||
| @ -107,10 +107,14 @@ return function(s) | ||||
|  | ||||
| 		-- Only set corner radius here. | ||||
| 		shape = function(cr, width, height) | ||||
| 			gears.shape.rounded_rect( | ||||
| 			gears.shape.partially_rounded_rect( | ||||
| 				cr, | ||||
| 				width, | ||||
| 				height, | ||||
| 				true, | ||||
| 				false, | ||||
| 				false, | ||||
| 				false, | ||||
| 				beautiful.dpi(20) | ||||
| 			) | ||||
| 		end | ||||
| @ -122,10 +126,18 @@ return function(s) | ||||
| 	} | ||||
|  | ||||
|  | ||||
| 	local function update_popup() | ||||
| 		volume.commands:get(function(status) | ||||
| 	awesome.connect_signal("module::volume:update", | ||||
| 		function(status) | ||||
|  | ||||
| 			awesome.emit_signal("widget::volume") | ||||
| 			-- Update slider | ||||
| 			widget. | ||||
| 				container. | ||||
| 				popup_layout. | ||||
| 				icon_bar_layout. | ||||
| 				bar_layout. | ||||
| 				volume_bar:set_value(status.value) | ||||
|  | ||||
| 			-- Update text | ||||
| 			widget. | ||||
| 				container. | ||||
| 				popup_layout. | ||||
| @ -133,11 +145,7 @@ return function(s) | ||||
| 				label_value_layout. | ||||
| 				value:set_text(status.value .. "%") | ||||
|  | ||||
| 			awesome.emit_signal( | ||||
| 				"widget::volume:update", | ||||
| 				status.value | ||||
| 			) | ||||
|  | ||||
| 			-- Update icon | ||||
| 			local icon | ||||
| 			if status.mute then | ||||
| 				icon = beautiful.icons.volume.mute | ||||
| @ -158,22 +166,8 @@ return function(s) | ||||
| 				icon_margin1. | ||||
| 				icon_margin2. | ||||
| 				icon:set_image(icon) | ||||
| 		end) | ||||
| 	end | ||||
|  | ||||
| 	local update_slider = function() | ||||
| 		volume.commands:get(function(status) | ||||
| 			widget. | ||||
| 				container. | ||||
| 				popup_layout. | ||||
| 				icon_bar_layout. | ||||
| 				bar_layout. | ||||
| 				volume_bar:set_value(status.value) | ||||
| 			update_popup() | ||||
| 		end) | ||||
| 	end | ||||
|  | ||||
| 	update_slider() | ||||
| 		end | ||||
| 	) | ||||
|  | ||||
| 	local hide_popup_timer = gears.timer { | ||||
| 		timeout = 1, | ||||
| @ -183,9 +177,9 @@ return function(s) | ||||
| 		end | ||||
| 	} | ||||
|  | ||||
|  | ||||
| 	awesome.connect_signal("module::volume_popup:show", | ||||
| 		function() | ||||
| 			update_slider() | ||||
| 			if s == mouse.screen then | ||||
| 				popup_container.visible = true | ||||
| 			end | ||||
| @ -198,11 +192,43 @@ return function(s) | ||||
| 		end | ||||
| 	) | ||||
|  | ||||
| 	popup_container:connect_signal("button::press", | ||||
| 	awesome.connect_signal("module::volume_popup:show_stay", | ||||
| 		function() | ||||
| 			popup_container.visible = false | ||||
| 			hide_popup_timer:stop() | ||||
| 			if s == mouse.screen then | ||||
| 				popup_container.visible = true | ||||
| 			end | ||||
|  | ||||
| 			if hide_popup_timer.started then | ||||
| 				hide_popup_timer:stop() | ||||
| 			end | ||||
| 		end | ||||
| 		) | ||||
|  | ||||
|  | ||||
| 	popup_container:connect_signal("button::press", | ||||
| 		function(_, _, _, button, mods) | ||||
| 			-- Right-click | ||||
| 			if (button == 1) then | ||||
| 				volume.toggle_mute() | ||||
|  | ||||
| 			-- Scroll up | ||||
| 			elseif (button == 4) then | ||||
| 				volume.volume_up() | ||||
|  | ||||
| 			-- Scroll down | ||||
| 			elseif (button == 5) then | ||||
| 				volume.volume_down() | ||||
| 			end | ||||
| 		end | ||||
| 	) | ||||
|  | ||||
| 	popup_container:connect_signal("mouse::enter", function(result) | ||||
| 		awesome.emit_signal("module::volume_popup:show_stay") | ||||
| 	end) | ||||
|  | ||||
| 	popup_container:connect_signal("mouse::leave", function(result) | ||||
| 		awesome.emit_signal("module::volume_popup:show") | ||||
| 	end) | ||||
|  | ||||
| end | ||||
|  | ||||
|  | ||||
| @ -1,5 +1,5 @@ | ||||
| local volume = {} | ||||
| volume.commands = {} | ||||
|  | ||||
|  | ||||
|  | ||||
| -- Create pamixer option string | ||||
| @ -8,82 +8,122 @@ if (config.volume.pamixer_sink ~= "") then | ||||
| 	volume.pamixer_options = volume.pamixer_options .. " --sink " .. config.volume.pamixer_sink | ||||
| end | ||||
|  | ||||
| function volume.commands:up() | ||||
| 	awful.spawn("pamixer --increase 5" .. volume.pamixer_options, false) | ||||
| end | ||||
|  | ||||
| function volume.commands:down() | ||||
| 	awful.spawn("pamixer --decrease 5" .. volume.pamixer_options, false) | ||||
| end | ||||
|  | ||||
| function volume.commands:set(value) | ||||
| 	awful.spawn("pamixer --set_volume" .. tostring(value) .. " " .. volume.pamixer_options, false) | ||||
| end | ||||
|  | ||||
| function volume.commands:mute() | ||||
| 	awful.spawn("pamixer --mute" .. volume.pamixer_options, false) | ||||
| end | ||||
|  | ||||
| function volume.commands:unmute() | ||||
| 	awful.spawn("pamixer --unmute" .. volume.pamixer_options, false) | ||||
| end | ||||
|  | ||||
| function volume.commands:get(callback) | ||||
| 	local muted | ||||
| 	local value | ||||
|  | ||||
| 	awful.spawn.easy_async("pamixer --get-mute --get-volume" .. volume.pamixer_options, | ||||
| 		function(stdout, stderr, exitreason, exitcode) | ||||
| 			muted = string.match(stdout, "(%w%w%w%w%w?) ")   -- "true" or "false" | ||||
| 			muted = (muted == "true") | ||||
|  | ||||
| 			value = string.match(stdout, "(%d?%d?%d)") -- (\d?\d?\d)\%) | ||||
|  | ||||
| 			if (value == nil) then | ||||
| 				value = false | ||||
| 			else | ||||
| 				value = tonumber(string.format("% 3d", value)) | ||||
| volume.commands = { | ||||
| 	up = function(callback) | ||||
| 		awful.spawn.easy_async( | ||||
| 			"pamixer --increase 5 " .. volume.pamixer_options, | ||||
| 			function(stdout, stderr, exitreason, exitcode) | ||||
| 				awesome.emit_signal("module::volume:update_read") | ||||
| 				if (callback ~= nil) then | ||||
| 					callback() | ||||
| 				end | ||||
| 			end | ||||
| 		) | ||||
| 	end, | ||||
|  | ||||
| 			callback({ | ||||
| 				mute = muted, | ||||
| 				value = value | ||||
| 			}) | ||||
| 		end | ||||
| 	) | ||||
| end | ||||
| 	down = function(callback) | ||||
| 		awful.spawn.easy_async( | ||||
| 			"pamixer --decrease 5 " .. volume.pamixer_options, | ||||
| 			function(stdout, stderr, exitreason, exitcode) | ||||
| 				awesome.emit_signal("module::volume:update_read") | ||||
| 				if (callback ~= nil) then | ||||
| 					callback() | ||||
| 				end | ||||
| 			end | ||||
| 		) | ||||
| 	end, | ||||
|  | ||||
| function volume.commands:watch(timeout, callback, widget) | ||||
| 	awful.widget.watch( | ||||
| 		"pamixer --get-mute --get-volume " .. volume.pamixer_options, | ||||
| 		timeout, callback, widget | ||||
| 	) | ||||
| end | ||||
| 	set = function(value, callback) | ||||
| 		awful.spawn.easy_async( | ||||
| 			"pamixer --set_volume " .. tostring(value) .. " " .. volume.pamixer_options, | ||||
| 			function(stdout, stderr, exitreason, exitcode) | ||||
| 				awesome.emit_signal("module::volume:update_read") | ||||
| 				if (callback ~= nil) then | ||||
| 					callback() | ||||
| 				end | ||||
| 			end | ||||
| 		) | ||||
| 	end, | ||||
|  | ||||
| 	mute = function(callback) | ||||
| 		awful.spawn.easy_async( | ||||
| 			"pamixer --mute " .. volume.pamixer_options, | ||||
| 			function(stdout, stderr, exitreason, exitcode) | ||||
| 				awesome.emit_signal("module::volume:update_read") | ||||
| 				if (callback ~= nil) then | ||||
| 					callback() | ||||
| 				end | ||||
| 			end | ||||
| 		) | ||||
| 	end, | ||||
|  | ||||
| 	unmute = function(callback) | ||||
| 		awful.spawn.easy_async( | ||||
| 			"pamixer --unmute " .. volume.pamixer_options, | ||||
| 			function(stdout, stderr, exitreason, exitcode) | ||||
| 				awesome.emit_signal("module::volume:update_read") | ||||
| 				if (callback ~= nil) then | ||||
| 					callback() | ||||
| 				end | ||||
| 			end | ||||
| 		) | ||||
| 	end, | ||||
|  | ||||
| volume.hooks = {} | ||||
| volume.add_hook = function(callback) | ||||
| 	volume.hooks[#volume.hooks + 1] = callback | ||||
| end | ||||
| 	watch = function(timeout, callback, widget) | ||||
| 		awful.widget.watch( | ||||
| 			"pamixer --get-mute --get-volume " .. volume.pamixer_options, | ||||
| 			timeout, callback, widget | ||||
| 		) | ||||
| 	end, | ||||
|  | ||||
| volume.exec_hooks = function() | ||||
| 	volume.commands:get(function(status) | ||||
| 		for i=1, #volume.hooks do | ||||
| 			volume.hooks[i](status) | ||||
| 		end | ||||
| 	end) | ||||
| end | ||||
| 	get = function(callback) | ||||
| 		local muted | ||||
| 		local value | ||||
|  | ||||
| 		awful.spawn.easy_async( | ||||
| 			"pamixer --get-mute --get-volume " .. volume.pamixer_options, | ||||
| 			function(stdout, stderr, exitreason, exitcode) | ||||
| 				muted = string.match(stdout, "(%w%w%w%w%w?) ") -- "true" or "false" | ||||
| 				muted = (muted == "true") | ||||
|  | ||||
| 				value = string.match(stdout, "(%d?%d?%d)") -- (\d?\d?\d)\%) | ||||
|  | ||||
| 				if (value == nil) then | ||||
| 					value = false | ||||
| 				else | ||||
| 					value = tonumber(string.format("% 3d", value)) | ||||
| 				end | ||||
|  | ||||
| 				callback({ | ||||
| 					mute = muted, | ||||
| 					value = value | ||||
| 				}) | ||||
| 			end | ||||
| 		) | ||||
| 	end | ||||
| } | ||||
|  | ||||
| awesome.connect_signal("module::volume:update_read", | ||||
| 	function() | ||||
| 		volume.commands.get( | ||||
| 			function(status) | ||||
| 				awesome.emit_signal( | ||||
| 					"module::volume:update", | ||||
| 					status | ||||
| 				) | ||||
| 			end | ||||
| 		) | ||||
| 	end | ||||
| ) | ||||
|  | ||||
| volume.volume_up = function() | ||||
| 	if volume.muted then | ||||
| 		volume.unmute() | ||||
| 	end | ||||
|  | ||||
| 	volume.commands:up() | ||||
| 	core.sound.play("volume_up") | ||||
|  | ||||
| 	volume.exec_hooks() | ||||
| 	volume.commands.up(function() | ||||
| 		core.sound.play("volume_up") | ||||
| 	end) | ||||
| end | ||||
|  | ||||
| volume.volume_down = function() | ||||
| @ -91,28 +131,23 @@ volume.volume_down = function() | ||||
| 		volume.unmute() | ||||
| 	end | ||||
|  | ||||
| 	volume.commands:down() | ||||
| 	core.sound.play("volume_down") | ||||
|  | ||||
| 	volume.exec_hooks() | ||||
| 	volume.commands.down(function() | ||||
| 		core.sound.play("volume_down") | ||||
| 	end) | ||||
| end | ||||
|  | ||||
| volume.mute = function() | ||||
| 	volume.commands:mute() | ||||
| 	volume.muted = true | ||||
|  | ||||
| 	volume.exec_hooks() | ||||
| 	volume.commands.mute() | ||||
| end | ||||
|  | ||||
| volume.unmute = function() | ||||
| 	volume.commands:unmute() | ||||
| 	volume.muted = false | ||||
|  | ||||
| 	volume.exec_hooks() | ||||
| 	volume.commands.unmute() | ||||
| end | ||||
|  | ||||
| volume.toggle_mute = function() | ||||
| 	volume.commands:get(function(status) | ||||
| 	volume.commands.get(function(status) | ||||
| 		if status.mute then | ||||
| 			volume.unmute() | ||||
| 		else | ||||
|  | ||||
| @ -23,6 +23,7 @@ widget.arc = wibox.widget { | ||||
| 	colors = {"#27D4CC", "#00446B"}, | ||||
| 	bg = "#FFFFFF30", | ||||
| 	paddings = beautiful.dpi(2), | ||||
| 	rounded_edge = true, | ||||
| 	widget = wibox.container.arcchart | ||||
| } | ||||
|  | ||||
| @ -69,10 +70,12 @@ widget.widget = wibox.widget { | ||||
|  | ||||
| widget.widget:connect_signal("mouse::enter", function(result) | ||||
| 	widget.widget.bg = beautiful.color.bar.hover_bg | ||||
| 	awesome.emit_signal("module::volume_popup:show_stay") | ||||
| end) | ||||
|  | ||||
| widget.widget:connect_signal("mouse::leave", function(result) | ||||
| 	widget.widget.bg = beautiful.color.transparent | ||||
| 	awesome.emit_signal("module::volume_popup:show") | ||||
| end) | ||||
|  | ||||
| widget.widget:connect_signal("button::press", | ||||
| @ -90,49 +93,39 @@ widget.widget:connect_signal("button::press", | ||||
| 		elseif (button == 5) then | ||||
| 			volume.volume_down() | ||||
| 		end | ||||
|  | ||||
| 		awesome.emit_signal("module::volume_popup:show") | ||||
| 		awesome.emit_signal("module::volume_popup:update") | ||||
| 	end | ||||
| ) | ||||
|  | ||||
| widget.update = function(status) | ||||
| awesome.connect_signal("module::volume:update", | ||||
| 	function(status) | ||||
| 		if (status.value == false) then | ||||
| 			widget.icon.image = beautiful.icons.volume.error | ||||
| 			widget.arc.value = 100; | ||||
| 			widget.bar.value = 100; | ||||
| 			return | ||||
| 		end | ||||
|  | ||||
| 	if (status.value == false) then | ||||
| 		widget.icon.image = beautiful.icons.volume.error | ||||
| 		widget.arc.value = 100; | ||||
| 		widget.bar.value = 100; | ||||
| 		return | ||||
| 	end | ||||
|  | ||||
| 	widget.arc.value = status.value / 100; | ||||
| 	widget.bar.value = status.value; | ||||
| 		widget.arc.value = status.value / 100; | ||||
| 		widget.bar.value = status.value; | ||||
|  | ||||
|  | ||||
| 	if (status.mute) then | ||||
| 		-- Set muted icon | ||||
| 		widget.icon.image = beautiful.icons.volume.mute | ||||
| 	else | ||||
| 		-- Set icon by value | ||||
| 		if status.value > 70 then | ||||
| 			widget.icon.image = beautiful.icons.volume.high | ||||
| 		elseif status.value > 40 then | ||||
| 			widget.icon.image = beautiful.icons.volume.medium | ||||
| 		elseif status.value > 0 then | ||||
| 			widget.icon.image = beautiful.icons.volume.low | ||||
| 		elseif status.value == 0 then | ||||
| 			widget.icon.image = beautiful.icons.volume.off | ||||
| 		if (status.mute) then | ||||
| 			-- Set muted icon | ||||
| 			widget.icon.image = beautiful.icons.volume.mute | ||||
| 		else | ||||
| 			-- Set icon by value | ||||
| 			if status.value > 70 then | ||||
| 				widget.icon.image = beautiful.icons.volume.high | ||||
| 			elseif status.value > 40 then | ||||
| 				widget.icon.image = beautiful.icons.volume.medium | ||||
| 			elseif status.value > 0 then | ||||
| 				widget.icon.image = beautiful.icons.volume.low | ||||
| 			elseif status.value == 0 then | ||||
| 				widget.icon.image = beautiful.icons.volume.off | ||||
| 			end | ||||
| 		end | ||||
| 	end | ||||
| end | ||||
|  | ||||
|  | ||||
| volume.add_hook(widget.update) | ||||
| volume.commands:watch( | ||||
| 	5, | ||||
| 	function() | ||||
| 		volume.commands:get(widget.update) | ||||
| 	end, | ||||
| 	widget.widget | ||||
| ) | ||||
|  | ||||
| return widget | ||||
|  | ||||
		Reference in New Issue
	
	Block a user