From 15723950c6a95ffd978ca45a5730ac032c6b62b7 Mon Sep 17 00:00:00 2001 From: Michael Damsbo Lyngs Date: Mon, 7 Jul 2025 10:26:03 +0200 Subject: [PATCH] feat: refactor bluelight to use factory method This allows the user of the widget to pass options without having to edit the source code to change the parameters --- bluelight-widget/README.md | 16 +++--- bluelight-widget/init.lua | 109 +++++++++++++++++++++++-------------- 2 files changed, 76 insertions(+), 49 deletions(-) diff --git a/bluelight-widget/README.md b/bluelight-widget/README.md index eb96b939..6e34bb0b 100644 --- a/bluelight-widget/README.md +++ b/bluelight-widget/README.md @@ -29,8 +29,9 @@ s.mytasklist, -- Middle widget { -- Right widgets layout = wibox.layout.fixed.horizontal, ... - bluelight_widget, -- Add the widget here - margin(bluelight, true), -- Add the widget with my wrapper + bluelight_widget(), -- Add the widget here + margin(bluelight_widget(), true), -- Add the widget with my wrapper + bluelight_widget({night_args = {"-O", "3500", "-P", "-g", "0.75"}}), -- Pass arguments in string or table of strings ... } ``` @@ -43,14 +44,15 @@ s.mytasklist, -- Middle widget ## Customization -You can customize the widget by modifying the following parameters in the widget's source file: +You can pass arguments to the bluelight method. The following arguments are avaliable: | Name | Default | Description | |------------|----------------------------------------|------------------------------------------------------------| -| `ICON_DIR` | ```awesome-wm-widgets/bluelight-widget/``` | Directory where the widget icons (sun.svg and moon.svg) are stored. | -| `CMD` | ```redshift``` | Command to run Redshift. | -| `NIGHT_CMD`| ```-O 2500 -g 0.75``` | Command options for activating Night Mode. | -| `DAY_CMD` | ```-x``` | Command options for activating Day Mode. | +| `cmd` | ```redshift``` | Command to run Redshift. | +| `night_args`| ```-O 2500 -g 0.75 -P``` | Command options for activating Night Mode. | +| `day_args` | ```-x``` | Command options for activating Day Mode. | +| `night_icon` | ```awesome-wm-widgets/bluelight-widget/moon.svg``` | Image to show when Night Mode is activated. | +| `day_icon` | ```awesome-wm-widgets/bluelight-widget/sun.svg``` | Image to show when Day Mode is activated | ## Dependencies diff --git a/bluelight-widget/init.lua b/bluelight-widget/init.lua index a960b795..d4694471 100644 --- a/bluelight-widget/init.lua +++ b/bluelight-widget/init.lua @@ -15,58 +15,83 @@ local ICON_DIR = gfs.get_configuration_dir() .. "awesome-wm-widgets/bluelight-wi local DAY_ICON = ICON_DIR .. "sun.svg" local NIGHT_ICON = ICON_DIR .. "moon.svg" -local CMD = "redshift" -local NIGHT_CMD = "-O 2500 -g 0.75" -local DAY_CMD = "-x" -local day = true +---@class Bluelight.Widget.Opts +---@field cmd string? +---@field night_args string[] | string? +---@field day_args string[]? | string? +---@field day_icon string? +---@field night_icon string? -local widget = wibox.widget({ - { +---@type Bluelight.Widget.Opts +local default_opts = { + cmd = "redshift", + night_args = { "-O", "2500", "-g", "0.75", "-P" }, + day_args = { "-x" }, + day_icon = DAY_ICON, + night_icon = NIGHT_ICON, +} +---@param opts Bluelight.Widget.Opts +local factory = function(opts) + opts = opts or {} + local cmd = opts.cmd or default_opts.cmd + local night_args = opts.night_args or default_opts.night_args + local day_args = opts.day_args or default_opts.day_args + local day_icon = opts.day_icon or default_opts.day_icon + local night_icon = opts.night_icon or default_opts.night_icon + + local day = true + + local widget = wibox.widget({ { - id = "icon", - image = DAY_ICON, - resize = true, - widget = wibox.widget.imagebox, + { + id = "icon", + image = DAY_ICON, + resize = true, + widget = wibox.widget.imagebox, + }, + layout = wibox.layout.fixed.horizontal, + widget = wibox.container.margin, }, + border_width = 5, + widget = wibox.container.background, layout = wibox.layout.fixed.horizontal, - widget = wibox.container.margin, - }, - border_width = 5, - widget = wibox.container.background, - layout = wibox.layout.fixed.horizontal, -}) + }) -function widget:update() - local icon = self:get_children_by_id("icon")[1] - if day then - icon:set_image(DAY_ICON) - else - icon:set_image(NIGHT_ICON) + function widget:update() + local icon = self:get_children_by_id("icon")[1] + if day then + icon:set_image(day_icon) + else + icon:set_image(night_icon) + end end -end -local function on_day() - awful.spawn(CMD .. " " .. DAY_CMD) - widget:update() -end + local function on_day() + local args = type(day_args) == "table" and table.concat(day_args, " ") or day_args + awful.spawn(cmd .. " " .. args) + widget:update() + end -local function on_night() - awful.spawn(CMD .. " " .. NIGHT_CMD) - widget:update() -end + local function on_night() + local args = type(night_args) == "table" and table.concat(night_args, " ") or night_args + awful.spawn(cmd .. " " .. args) + widget:update() + end -local function toggle() - day = not day - if day then - on_day() - else - on_night() + local function toggle() + day = not day + if day then + on_day() + else + on_night() + end end -end -widget:buttons(awful.util.table.join(awful.button({}, 1, function() - toggle() -end))) + widget:buttons(awful.util.table.join(awful.button({}, 1, function() + toggle() + end))) + return widget +end -return widget +return factory