From d584d251947b6abe5636ce27cc17bf816427330f Mon Sep 17 00:00:00 2001 From: gnat Date: Sun, 17 Nov 2024 16:27:41 -0800 Subject: [PATCH] add new widgets; rewrite workspace indicator --- lua/widgets/bar/battery.lua | 33 +++++++++++++++++++++++++++++++++ lua/widgets/bar/clock.lua | 18 ++++++++++++++++++ lua/widgets/bar/init.lua | 31 ++++++++++++++++++++++++++++++- lua/widgets/bar/volume.lua | 26 ++++++++++++++++++++++++++ lua/widgets/bar/workspaces.lua | 27 ++++++++++++++++++--------- 5 files changed, 125 insertions(+), 10 deletions(-) create mode 100644 lua/widgets/bar/battery.lua create mode 100644 lua/widgets/bar/clock.lua create mode 100644 lua/widgets/bar/volume.lua diff --git a/lua/widgets/bar/battery.lua b/lua/widgets/bar/battery.lua new file mode 100644 index 0000000..588b62e --- /dev/null +++ b/lua/widgets/bar/battery.lua @@ -0,0 +1,33 @@ +local Battery = lgi.require("AstalBattery") + +local battery = Battery.get_default() + +local icons = { + {"󰂎", "󰁺", "󰁻", "󰁼", "󰁽", "󰁾", "󰁿", "󰂀", "󰂁", "󰂂", "󰁹"}, + {"󰢟", "󰢜", "󰂆", "󰂇", "󰂈", "󰢝", "󰂉", "󰢞", "󰂊", "󰂋", "󰂅"}, +}; + +return Widget.Box({ + class_name = 'battery-container', + children = { + Widget.CircularProgress({ + class_name = 'battery-dial', + rounded = false, + inverted = false, + start_at = -.25, + end_at = .75, + value = bind(battery, 'percentage'), + child = Widget.Label({ + halign = "CENTER", + hexpand = true, + justify = 2, + setup = function(self) + self:hook(battery, 'notify::percentage', function(self, percentage) + self.label = icons[battery:get_charging() and 1 or 2][math.floor(percentage*10)] + end) + self.label = icons[battery:get_charging() and 2 or 1][math.floor(battery:get_percentage()*10)] + end + }) + }) + } +}) diff --git a/lua/widgets/bar/clock.lua b/lua/widgets/bar/clock.lua new file mode 100644 index 0000000..a7d2ca4 --- /dev/null +++ b/lua/widgets/bar/clock.lua @@ -0,0 +1,18 @@ +local datetime = Variable(0):poll(1000, "date +'%d %b %H:%M:%S'", function(out, _) return out end) +local unix_seconds = Variable(0):poll(1000, "date +'%s'", function(out, _) return out end) + +return Widget.Box({ + class_name = 'clock', + vertical = true, + valign = 'CENTER', + children = { + Widget.Label({ + halign = 'START', + label = bind(datetime, 'value') + }), + Widget.Label({ + halign = 'START', + label = bind(unix_seconds, 'value') + }) + } +}) diff --git a/lua/widgets/bar/init.lua b/lua/widgets/bar/init.lua index 20422fc..121f25d 100644 --- a/lua/widgets/bar/init.lua +++ b/lua/widgets/bar/init.lua @@ -1,9 +1,38 @@ local workspaces = require(... .. '.workspaces') +local clock = require(... .. '.clock') +local battery = require(... .. '.battery') +local volume = require(... .. '.volume') +local brightness = require(... .. '.brightness') return Astal.Window({ namespace = "bar", name = "bar", anchor = Astal.WindowAnchor.TOP + Astal.WindowAnchor.LEFT + Astal.WindowAnchor.RIGHT, exclusivity = "EXCLUSIVE", - child = workspaces, + child = Widget.CenterBox({ + start_widget = Widget.Box({ + class_name = 'left', + children = { + workspaces, + } + }), + center_widget = Widget.Box({}), + end_widget = Widget.Box({ + class_name = 'right', + halign = 'END', + children = { + Widget.Box({ + class_name = 'sliders', + vertical = true, + children = { + volume, + brightness + } + }), + battery, + Gtk.Separator({}), + clock + } + }), + }) }) diff --git a/lua/widgets/bar/volume.lua b/lua/widgets/bar/volume.lua new file mode 100644 index 0000000..59eb74d --- /dev/null +++ b/lua/widgets/bar/volume.lua @@ -0,0 +1,26 @@ +local wp = lgi.require("AstalWp").get_default() + +local endpoint = wp:get_audio():get_default_speaker() + +return Widget.Box({ + class_name = 'volume-slider', + children = { + Widget.Button({ + child = Widget.Icon({ + icon = bind(endpoint, "volume-icon"), + }), + on_clicked = function() + endpoint:set_mute(not endpoint:get_mute()) + end + }), + Widget.Slider({ + class_name = 'volume-slider', + hexpand = true, + draw_value = false, + value = bind(endpoint, 'volume'), + on_value_changed = function(self) + endpoint:set_volume(self.value) + end + }) + } +}) diff --git a/lua/widgets/bar/workspaces.lua b/lua/widgets/bar/workspaces.lua index c181db7..8586c39 100644 --- a/lua/widgets/bar/workspaces.lua +++ b/lua/widgets/bar/workspaces.lua @@ -1,4 +1,4 @@ -local Hyprland = astal.require("AstalHyprland") +local Hyprland = lgi.require("AstalHyprland") local map, sequence = require('lua.lib').map, require('lua.lib').sequence local hypr = Hyprland.get_default() @@ -15,18 +15,26 @@ local function workspace_row(start, stop) self:hook(hypr, 'notify::focused-workspace', function(self, workspace) self:toggle_class_name('focused', workspace:get_id() == i) end) - self:hook(hypr, 'notify::workspaces', function(self, workspaces) - map(workspaces, function(workspace) + + local function update() + local workspaces = hypr:get_workspaces() + for _,workspace in ipairs(workspaces) do if workspace:get_id() == i then - -- ick - local count = 0 - for _ in ipairs(workspace:get_clients()) do + local count = 0; + + for _ in pairs(workspace:get_clients()) do count = count + 1 end - self:toggle_class_name('occupied', workspace:get_id() == i and count > 0) + + self:toggle_class_name('occupied', count > 0) end - end) - end) + end + end + + self:hook(hypr, 'notify::clients', update) + self:hook(hypr, 'notify::workspaces', update) + + update() end, on_click_release = function() hypr:message_async(string.format("dispatch workspace %d", i)) @@ -41,6 +49,7 @@ return Widget.Box({ vertical = true, hexpand = false, halign = 'START', + valign = 'CENTER', children = { workspace_row(1, 5), workspace_row(6, 10),