refactor: config module

This commit is contained in:
Aire-One 2021-12-29 14:36:16 +01:00
parent e1fd60f264
commit 805d15d1c8
4 changed files with 160 additions and 185 deletions

96
config.lua Normal file
View File

@ -0,0 +1,96 @@
local abutton = require "awful.button"
local gdebug = require "gears.debug"
local gtable = require "gears.table"
local config = { mt = {}, _private = {} }
-- Titlebar
config._private.titlebar_height = 38
config._private.titlebar_radius = 9
config._private.titlebar_color = "#1E1E24"
config._private.titlebar_margin_left = 0
config._private.titlebar_margin_right = 0
config._private.titlebar_font = "Sans 11"
config._private.titlebar_items = {
left = { "close", "minimize", "maximize" },
middle = "title",
right = { "sticky", "ontop", "floating" },
}
config._private.context_menu_theme = {
bg_focus = "#aed9e0",
bg_normal = "#5e6472",
border_color = "#00000000",
border_width = 0,
fg_focus = "#242424",
fg_normal = "#fefefa",
font = "Sans 11",
height = 27.5,
width = 250,
}
config._private.win_shade_enabled = true
config._private.no_titlebar_maximized = false
config._private.mb_move = abutton.names.LEFT
config._private.mb_contextmenu = abutton.names.MIDDLE
config._private.mb_resize = abutton.names.RIGHT
config._private.mb_win_shade_rollup = abutton.names.SCROLL_UP
config._private.mb_win_shade_rolldown = abutton.names.SCROLL_DOWN
-- Titlebar Items
config._private.button_size = 16
config._private.button_margin_horizontal = 5
-- _private.button_margin_vertical
config._private.button_margin_top = 2
-- _private.button_margin_bottom = 0
-- _private.button_margin_left = 0
-- _private.button_margin_right = 0
config._private.tooltips_enabled = true
config._private.tooltip_messages = {
close = "close",
minimize = "minimize",
maximize_active = "unmaximize",
maximize_inactive = "maximize",
floating_active = "enable tiling mode",
floating_inactive = "enable floating mode",
ontop_active = "don't keep above other windows",
ontop_inactive = "keep above other windows",
sticky_active = "disable sticky mode",
sticky_inactive = "enable sticky mode",
}
config._private.close_color = "#ee4266"
config._private.minimize_color = "#ffb400"
config._private.maximize_color = "#4CBB17"
config._private.floating_color = "#f6a2ed"
config._private.ontop_color = "#f6a2ed"
config._private.sticky_color = "#f6a2ed"
function config.init(args)
-- properties that are table
local table_args = {
titlebar_items = true,
context_menu_theme = true,
tooltip_messages = true,
}
-- Apply changes to our _private properties
if args then
for prop, value in pairs(args) do
if table_args[prop] == true then
gtable.crush(config._private[prop], value)
elseif prop == "titlebar_radius" then
config._private[prop] = math.max(3, value)
else
config._private[prop] = value
end
end
end
end
function config.mt:__index(k)
return config._private[k]
end
function config.mt:__newindex(k, v)
config._private[k] = v
end
return setmetatable(config, config.mt)

145
init.lua
View File

@ -46,6 +46,8 @@ local gdk = lgi.require("Gdk", "3.0")
-- => nice
-- ============================================================
-- Config
local config = require "awesome-wm-nice.config"
-- Colors
local colors = require "awesome-wm-nice.colors"
local color_darken = colors.darken
@ -84,66 +86,7 @@ local nice = {}
-- => Defaults
-- ============================================================
local _private = {}
-- Titlebar
_private.titlebar_height = 38
_private.titlebar_radius = 9
_private.titlebar_color = "#1E1E24"
_private.titlebar_margin_left = 0
_private.titlebar_margin_right = 0
_private.titlebar_font = "Sans 11"
_private.titlebar_items = {
left = { "close", "minimize", "maximize" },
middle = "title",
right = { "sticky", "ontop", "floating" },
}
_private.context_menu_theme = {
bg_focus = "#aed9e0",
bg_normal = "#5e6472",
border_color = "#00000000",
border_width = 0,
fg_focus = "#242424",
fg_normal = "#fefefa",
font = "Sans 11",
height = 27.5,
width = 250,
}
_private.win_shade_enabled = true
_private.no_titlebar_maximized = false
_private.mb_move = abutton.names.LEFT
_private.mb_contextmenu = abutton.names.MIDDLE
_private.mb_resize = abutton.names.RIGHT
_private.mb_win_shade_rollup = abutton.names.SCROLL_UP
_private.mb_win_shade_rolldown = abutton.names.SCROLL_DOWN
-- Titlebar Items
_private.button_size = 16
_private.button_margin_horizontal = 5
-- _private.button_margin_vertical
_private.button_margin_top = 2
-- _private.button_margin_bottom = 0
-- _private.button_margin_left = 0
-- _private.button_margin_right = 0
_private.tooltips_enabled = true
_private.tooltip_messages = {
close = "close",
minimize = "minimize",
maximize_active = "unmaximize",
maximize_inactive = "maximize",
floating_active = "enable tiling mode",
floating_inactive = "enable floating mode",
ontop_active = "don't keep above other windows",
ontop_inactive = "keep above other windows",
sticky_active = "disable sticky mode",
sticky_inactive = "enable sticky mode",
}
_private.close_color = "#ee4266"
_private.minimize_color = "#ffb400"
_private.maximize_color = "#4CBB17"
_private.floating_color = "#f6a2ed"
_private.ontop_color = "#f6a2ed"
_private.sticky_color = "#f6a2ed"
-- ------------------------------------------------------------
-- => Saving and loading of color rules
@ -155,32 +98,32 @@ local gfilesys = require "gears.filesystem"
local config_dir = gfilesys.get_configuration_dir()
local color_rules_filename = "color_rules"
local color_rules_filepath = config_dir .. "/nice/" .. color_rules_filename
_private.color_rules = t.load(color_rules_filepath) or {}
config.color_rules = t.load(color_rules_filepath) or {}
-- Saves the contents of _private.color_rules table to file
-- Saves the contents of config.color_rules table to file
local function save_color_rules()
t.save(_private.color_rules, color_rules_filepath)
t.save(config.color_rules, color_rules_filepath)
end
-- Adds a color rule entry to the color_rules table for the given client and saves to file
local function set_color_rule(c, color)
_private.color_rules[c.instance] = color
config.color_rules[c.instance] = color
save_color_rules()
end
-- Fetches the color rule for the given client instance
local function get_color_rule(c)
return _private.color_rules[c.instance]
return config.color_rules[c.instance]
end
-- ------------------------------------------------------------
function nice.get_titlebar_mouse_bindings(c)
local shade_enabled = _private.win_shade_enabled
local shade_enabled = config.win_shade_enabled
-- Add functionality for double click to (un)maximize, and single click and hold to move
local clicks = 0
local tolerance = double_click_jitter_tolerance
local buttons = {
abutton({}, _private.mb_move, function()
abutton({}, config.mb_move, function()
local cx, cy = _G.mouse.coords().x, _G.mouse.coords().y
local delta = double_click_time_window_ms / 1000
clicks = clicks + 1
@ -208,7 +151,7 @@ function nice.get_titlebar_mouse_bindings(c)
clicks = 0
end)
end),
abutton({}, _private.mb_contextmenu, function()
abutton({}, config.mb_contextmenu, function()
local menu_items = {}
local function add_item(text, callback)
menu_items[#menu_items + 1] = { text, callback }
@ -236,26 +179,26 @@ function nice.get_titlebar_mouse_bindings(c)
end
c._nice_right_click_menu = awful.menu {
items = menu_items,
theme = _private.context_menu_theme,
theme = config.context_menu_theme,
}
c._nice_right_click_menu:show()
end),
abutton({}, _private.mb_resize, function()
abutton({}, config.mb_resize, function()
c:activate { context = "mouse_click", action = "mouse_resize" }
end),
}
if _private.win_shade_enabled then
if config.win_shade_enabled then
buttons[#buttons + 1] = abutton(
{},
_private.mb_win_shade_rollup,
config.mb_win_shade_rollup,
function()
shade.shade_roll_up(c)
end
)
buttons[#buttons + 1] = abutton(
{},
_private.mb_win_shade_rolldown,
config.mb_win_shade_rolldown,
function()
shade.shade_roll_down(c)
end
@ -296,7 +239,7 @@ function nice.add_window_decoration(c)
)
local stroke_color_outer_sides = darken(darken_amount)
local stroke_color_outer_bottom = darken(darken_amount)
local titlebar_height = _private.titlebar_height
local titlebar_height = config.titlebar_height
local background_fill_top = gradient(
lighten(titlebar_gradient_c1_lighten),
client_color,
@ -309,7 +252,7 @@ function nice.add_window_decoration(c)
background_source = background_fill_top,
color = client_color,
height = titlebar_height,
radius = _private.titlebar_radius,
radius = config.titlebar_radius,
stroke_offset_inner = 1.5,
stroke_width_inner = 1,
stroke_offset_outer = 0.5,
@ -352,28 +295,22 @@ function nice.add_window_decoration(c)
{
{
{
utils.create_titlebar_items(
c,
_private.titlebar_items.left
),
utils.create_titlebar_items(c, config.titlebar_items.left),
widget = wcontainer_margin,
left = _private.titlebar_margin_left,
left = config.titlebar_margin_left,
},
{
utils.create_titlebar_items(
c,
_private.titlebar_items.middle
config.titlebar_items.middle
),
buttons = nice.get_titlebar_mouse_bindings(c),
layout = wlayout_flex_horizontal,
},
{
utils.create_titlebar_items(
c,
_private.titlebar_items.right
),
utils.create_titlebar_items(c, config.titlebar_items.right),
widget = wcontainer_margin,
right = _private.titlebar_margin_right,
right = config.titlebar_margin_right,
},
layout = wlayout_align_horizontal,
},
@ -483,11 +420,11 @@ function nice.add_window_decoration(c)
layout = wlayout_align_horizontal,
buttons = resize_button,
}
if _private.win_shade_enabled then
if config.win_shade_enabled then
shade.add_window_shade(c, titlebar.widget, bottom.widget)
end
if _private.no_titlebar_maximized then
if config.no_titlebar_maximized then
c:connect_signal("property::maximized", function()
if c.maximized then
local curr_screen_workarea = client.focus.screen.workarea
@ -503,8 +440,8 @@ function nice.add_window_decoration(c)
awful.titlebar.show(c)
-- Shape the client
c.shape = shapes.rounded_rect {
tl = _private.titlebar_radius,
tr = _private.titlebar_radius,
tl = config.titlebar_radius,
tr = config.titlebar_radius,
bl = 4,
br = 4,
}
@ -518,34 +455,17 @@ end
function nice.apply_client_shape(c)
c.shape = shapes.rounded_rect {
tl = _private.titlebar_radius,
tr = _private.titlebar_radius,
tl = config.titlebar_radius,
tr = config.titlebar_radius,
bl = 4,
br = 4,
}
end
function nice.initialize(args)
local crush = require("gears.table").crush
local table_args = {
titlebar_items = true,
context_menu_theme = true,
tooltip_messages = true,
}
if args then
for prop, value in pairs(args) do
if table_args[prop] == true then
crush(_private[prop], value)
elseif prop == "titlebar_radius" then
value = max(3, value)
_private[prop] = value
else
_private[prop] = value
end
end
end
config.init(args)
utils.validate_mb_bindings(_private)
utils.validate_mb_bindings(config)
_G.client.connect_signal("request::titlebars", function(c)
-- Callback
@ -554,7 +474,7 @@ function nice.initialize(args)
c._nice_base_color = utils.get_dominant_color(c)
set_color_rule(c, c._nice_base_color)
nice.add_window_decoration(c)
-- table.save(_private, config_dir .. "/nice/private")
-- table.save(config, config_dir .. "/nice/private")
c:disconnect_signal(
"request::activate",
c._cb_add_window_decorations
@ -570,7 +490,7 @@ function nice.initialize(args)
nice.add_window_decoration(c)
else
-- Otherwise use the default titlebar temporarily
c._nice_base_color = _private.titlebar_color
c._nice_base_color = config.titlebar_color
nice.add_window_decoration(c)
-- Connect a signal to determine the client color and then re-decorate it
c:connect_signal("request::activate", c._cb_add_window_decorations)
@ -588,6 +508,7 @@ end
return gtable.join(nice, {
colors = colors,
config = config,
shade = shade,
shapes = shapes,
table = t,

View File

@ -1,3 +1,4 @@
local config = require "awesome-wm-nice.config"
local shapes = require "awesome-wm-nice.shapes"
local wibox = require "wibox"
local wlayout_manual = require "wibox.layout.manual"
@ -6,9 +7,6 @@ local shade = {}
-- Legacy global variables
local bottom_edge_height = 3
local _private = {}
_private.titlebar_radius = 9
_private.titlebar_height = 38
-- Adds a window shade to the given client
function shade.add_window_shade(c, src_top, src_bottom)
@ -18,19 +16,19 @@ function shade.add_window_shade(c, src_top, src_bottom)
w.background = "transparent"
w.x = geo.x
w.y = geo.y
w.height = _private.titlebar_height + bottom_edge_height
w.height = config.titlebar_height + bottom_edge_height
w.ontop = true
w.visible = false
w.shape = shapes.rounded_rect {
tl = _private.titlebar_radius,
tr = _private.titlebar_radius,
tl = config.titlebar_radius,
tr = config.titlebar_radius,
bl = 4,
br = 4,
}
-- Need to use a manual layout because layout fixed seems to introduce a thin gap
src_top.point = { x = 0, y = 0 }
src_top.forced_width = geo.width
src_bottom.point = { x = 0, y = _private.titlebar_height }
src_bottom.point = { x = 0, y = config.titlebar_height }
w.widget = { src_top, src_bottom, layout = wlayout_manual }
-- Clean up resources when a client is killed
c:connect_signal("request::unmanage", function()

View File

@ -1,6 +1,7 @@
local abutton = require "awful.button"
local atooltip = require "awful.tooltip"
local colors = require "awesome-wm-nice.colors"
local config = require "awesome-wm-nice.config"
local get_font_height = require("beautiful").get_font_height
local imagebox = require "wibox.widget.imagebox"
local shapes = require "awesome-wm-nice.shapes"
@ -14,47 +15,6 @@ local widgets = {}
local cache = {}
-- Legacy global variables
local _private = {}
_private.titlebar_height = 38
_private.titlebar_font = "Sans 11"
_private.button_size = 16
_private.button_margin_horizontal = 5
-- _private.button_margin_vertical
_private.button_margin_top = 2
-- _private.button_margin_bottom = 0
-- _private.button_margin_left = 0
-- _private.button_margin_right = 0
_private.tooltips_enabled = true
_private.tooltip_messages = {
close = "close",
minimize = "minimize",
maximize_active = "unmaximize",
maximize_inactive = "maximize",
floating_active = "enable tiling mode",
floating_inactive = "enable floating mode",
ontop_active = "don't keep above other windows",
ontop_inactive = "keep above other windows",
sticky_active = "disable sticky mode",
sticky_inactive = "enable sticky mode",
}
_private.tooltip_messages = {
close = "close",
minimize = "minimize",
maximize_active = "unmaximize",
maximize_inactive = "maximize",
floating_active = "enable tiling mode",
floating_inactive = "enable floating mode",
ontop_active = "don't keep above other windows",
ontop_inactive = "keep above other windows",
sticky_active = "disable sticky mode",
sticky_inactive = "enable sticky mode",
}
cache.close_color = "#ee4266"
cache.minimize_color = "#ffb400"
cache.maximize_color = "#4CBB17"
cache.floating_color = "#f6a2ed"
cache.ontop_color = "#f6a2ed"
cache.sticky_color = "#f6a2ed"
local title_color_dark = "#242424"
local title_color_light = "#fefefa"
local title_unfocused_opacity = 0.7
@ -91,7 +51,7 @@ function widgets.create_button_image(name, is_focused, event, is_on)
if not cache[key_color] then
local key_base_color = name .. "_color"
-- Maybe the user has at least provided a base color? If not we just pick a pesudo-random color
local base_color = cache[key_base_color] or get_next_color()
local base_color = config[key_base_color] or get_next_color()
cache[key_base_color] = base_color
local button_color = base_color
local H = colors.hex2hsv(base_color)
@ -110,14 +70,14 @@ function widgets.create_button_image(name, is_focused, event, is_on)
-- Save the generate color because why not lol
cache[key_color] = button_color
end
local button_size = _private.button_size
local button_size = config.button_size
-- If it is a toggle button, we create an outline instead of a filled shape if it is in off state
-- _private[key_img] = (is_on ~= nil and is_on == false) and
-- config[key_img] = (is_on ~= nil and is_on == false) and
-- shapes.circle_outline(
-- _private[key_color], button_size,
-- _private.button_border_width) or
-- config[key_color], button_size,
-- config.button_border_width) or
-- shapes.circle_filled(
-- _private[key_color], button_size)
-- config[key_color], button_size)
cache[key_img] = shapes.circle_filled(cache[key_color], button_size)
return cache[key_img]
end
@ -125,7 +85,7 @@ end
-- Creates a titlebar button widget
function widgets.create_titlebar_button(c, name, button_callback, property)
local button_img = imagebox(nil, false)
if _private.tooltips_enabled then
if config.tooltips_enabled then
local tooltip = atooltip {
timer_function = function()
local prop = name
@ -134,7 +94,7 @@ function widgets.create_titlebar_button(c, name, button_callback, property)
and (c[property] and "_active" or "_inactive")
or ""
)
return _private.tooltip_messages[prop]
return config.tooltip_messages[prop]
end,
delay_show = 0.5,
margins_leftright = 12,
@ -200,23 +160,23 @@ function widgets.create_titlebar_button(c, name, button_callback, property)
widget = wcontainer_place,
{
widget = wcontainer_margin,
top = _private.button_margin_top
or _private.button_margin_vertical
or _private.button_margin,
bottom = _private.button_margin_bottom
or _private.button_margin_vertical
or _private.button_margin,
left = _private.button_margin_left
or _private.button_margin_horizontal
or _private.button_margin,
right = _private.button_margin_right
or _private.button_margin_horizontal
or _private.button_margin,
top = config.button_margin_top
or config.button_margin_vertical
or config.button_margin,
bottom = config.button_margin_bottom
or config.button_margin_vertical
or config.button_margin,
left = config.button_margin_left
or config.button_margin_horizontal
or config.button_margin,
right = config.button_margin_right
or config.button_margin_horizontal
or config.button_margin,
{
button_img,
widget = wcontainer_constraint,
height = _private.button_size,
width = _private.button_size,
height = config.button_size,
width = config.button_size,
strategy = "exact",
},
},
@ -243,7 +203,7 @@ function widgets.create_titlebar_title(c)
title_widget.markup =
("<span foreground='%s' font='%s'>%s</span>"):format(
text_color,
_private.titlebar_font,
config.titlebar_font,
c.name
)
end
@ -255,8 +215,8 @@ function widgets.create_titlebar_title(c)
title_widget.opacity = 1
end)
update()
local titlebar_font_height = get_font_height(_private.titlebar_font)
local leftover_space = _private.titlebar_height - titlebar_font_height
local titlebar_font_height = get_font_height(config.titlebar_font)
local leftover_space = config.titlebar_height - titlebar_font_height
local margin_vertical = leftover_space > 1 and leftover_space / 2 or 0
return {
title_widget,