Creation callbacks for templates

This commit is contained in:
Yessiest 2022-10-24 15:19:57 +04:00
parent 7d60e5625f
commit 379b838751
1 changed files with 80 additions and 45 deletions

View File

@ -1,11 +1,11 @@
-- This file is part of Reno desktop. -- this file is part of reno desktop.
-- --
-- Reno desktop is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. -- reno desktop is free software: you can redistribute it and/or modify it under the terms of the gnu general public license as published by the free software foundation, either version 3 of the license, or (at your option) any later version.
-- --
-- Reno desktop is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. -- reno desktop is distributed in the hope that it will be useful, but without any warranty; without even the implied warranty of merchantability or fitness for a particular purpose. see the gnu general public license for more details.
-- --
-- You should have received a copy of the GNU General Public License along with Reno desktop. If not, see <https://www.gnu.org/licenses/>. -- you should have received a copy of the gnu general public license along with reno desktop. if not, see <https://www.gnu.org/licenses/>.
-- RenoTK (formerly AWMTK2) - Template/Granular styling library for Reno -- renotk (formerly awmtk2) - template/granular styling library for reno
local wibox = require("wibox") local wibox = require("wibox")
local awful = require("awful") local awful = require("awful")
local gears = require("gears") local gears = require("gears")
@ -15,23 +15,23 @@ beautiful.templates = beautiful.templates or {}
local awmtk = {} local awmtk = {}
-- {{{ Utils -- {{{ utils
awmtk.create_delta = function(name,instance_delta,class_delta,parent_delta) awmtk.create_delta = function(name,instance_delta,class_delta,parent_delta)
-- To save memory, we create proxies for lower layers called "deltas" -- to save memory, we create proxies for lower layers called "deltas"
-- This function creates that proxy layer using metatables -- this function creates that proxy layer using metatables
-- Fun story - i used instance_delta instead of {} at first. -- fun story - i used instance_delta instead of {} at first.
-- The results were horrifying and confusing. -- the results were horrifying and confusing.
return setmetatable({},{ return setmetatable({},{
__index = function(self,k) __index = function(self,k)
-- Per-instance overrides are top priority -- per-instance overrides are top priority
if rawget(instance_delta,k) then if rawget(instance_delta,k) then
return rawget(instance_delta,k) return rawget(instance_delta,k)
-- Class-wide overrides are second in priority -- class-wide overrides are second in priority
elseif type(class_delta[name]) == "table" elseif type(class_delta[name]) == "table"
and rawget(class_delta[name],k) then and rawget(class_delta[name],k) then
return rawget(class_delta[name],k) return rawget(class_delta[name],k)
-- Parent is fallback -- parent is fallback
elseif parent_delta[k] then elseif parent_delta[k] then
return parent_delta[k] return parent_delta[k]
end end
@ -40,7 +40,7 @@ awmtk.create_delta = function(name,instance_delta,class_delta,parent_delta)
end end
awmtk.create_style = function(name,parent,overrides) awmtk.create_style = function(name,parent,overrides)
-- A style is essentially a layer of deltas upon the previous (parent) style -- a style is essentially a layer of deltas upon the previous (parent) style
local new_style = {} local new_style = {}
local odelta = (overrides and overrides[name]) or {} local odelta = (overrides and overrides[name]) or {}
local cdelta = beautiful.widgets[name] or {} local cdelta = beautiful.widgets[name] or {}
@ -73,35 +73,70 @@ awmtk.build_templates = function(templates,style)
return new_templates return new_templates
end end
awmtk.mask_object_call = function(obj,call)
local new = {}
for k,v in pairs(obj) do
new[k] = v
end
return setmetatable(new,{
__index = obj,
__call = call
})
end
awmtk.wrap_hooks = function(w,callbacks)
-- attach hooks to function
local mcall = getmetatable(w).__call
return awmtk.mask_object_call(w,function(...)
if callbacks and callbacks.on_create_pre then
callbacks.on_create_pre(...)
end
local widget = mcall(...)
if callbacks and callbacks.on_create then
callbacks.on_create(widget,...)
end
if callbacks and callbacks.on_ready then
local func = function()
callbacks.on_ready(widget)
end
widget:connect_signal("widget::layout_changed",func)
widget:connect_signal("widget::layout_changed",function()
widget:disconnect_signal("widget::layout_changed",func)
end)
end
return widget
end)
end
awmtk.merge = gears.table.join awmtk.merge = gears.table.join
-- }}} -- }}}
-- {{{ Default style -- {{{ default style
-- Prototype style -- prototype style
-- Notice that it's not awmtk.default style - it's used as a base for default. -- notice that it's not awmtk.default style - it's used as a base for default.
awmtk.proto_style = { awmtk.proto_style = {
base = setmetatable({ base = setmetatable({
-- { Backgrounds -- { backgrounds
-- custom background color for highlighting elements -- custom background color for highlighting elements
bg_highlight = beautiful.bg_highlight or beautiful.bg_focus, bg_highlight = beautiful.bg_highlight or beautiful.bg_focus,
-- allow more complex themes to define background images -- allow more complex themes to define background images
bgimage_focus = beautiful.bgimage_focus, bgimage_focus = beautiful.bgimage_focus,
bgimage_normal = beautiful.bgimage_normal, bgimage_normal = beautiful.bgimage_normal,
-- } -- }
-- { Borders -- { borders
-- Borders for popups -- borders for popups
shape_border_width = beautiful.shape_border_width or 0, shape_border_width = beautiful.shape_border_width or 0,
shape_border_color = beautiful.shape_border_color or beautiful.bg_normal, shape_border_color = beautiful.shape_border_color or beautiful.bg_normal,
-- } -- }
-- { Callbacks -- { callbacks
-- a tiny bit more complex thing to account for more extensibility -- a tiny bit more complex thing to account for more extensibility
-- the stub functions do nothing - you should implement functionality inside theme -- the stub functions do nothing - you should implement functionality inside theme
onpress = function() end, onpress = function() end,
onrelease = function() end, onrelease = function() end,
-- } -- }
-- { Shapes -- { shapes
margins = 5, margins = 5,
spacing = 5, spacing = 5,
shape = function(cr, width, height) shape = function(cr, width, height)
@ -111,7 +146,7 @@ awmtk.proto_style = {
},{__index = beautiful}) },{__index = beautiful})
} }
-- Subclasses -- subclasses
awmtk.proto_style.container = awmtk.create_delta("container",{ awmtk.proto_style.container = awmtk.create_delta("container",{
},awmtk.proto_style,awmtk.proto_style.base) },awmtk.proto_style,awmtk.proto_style.base)
@ -167,13 +202,13 @@ awmtk.proto_style.checkbox = awmtk.create_delta("checkbox", {
}, awmtk.proto_style,awmtk.proto_style.base) }, awmtk.proto_style,awmtk.proto_style.base)
-- }}} -- }}}
-- {{{ Generic templates -- {{{ generic templates
awmtk.proto_templates = { awmtk.proto_templates = {
-- Templates are built first using the given style, then applied to contents -- templates are built first using the given style, then applied to contents
-- through returned function -- through returned function
container = function(style) container = function(style)
-- Container is practically any "box" that contains buttons, textboxes, -- container is practically any "box" that contains buttons, textboxes,
-- and anything you want to put into the container. Do not confuse with -- and anything you want to put into the container. do not confuse with
-- popup - containers are designed to separate contents within a popup. -- popup - containers are designed to separate contents within a popup.
return function(layout,options) return function(layout,options)
return awmtk.merge({ return awmtk.merge({
@ -188,14 +223,14 @@ awmtk.proto_templates = {
shape = style.container.shape, shape = style.container.shape,
shape_border_color = style.container.shape_border_color, shape_border_color = style.container.shape_border_color,
shape_border_width = style.container.shape_border_width, shape_border_width = style.container.shape_border_width,
widget = wibox.container.background widget = awmtk.wrap_hooks(wibox.container.background,options)
},options or {}) },options or {})
end end
end, end,
button = function(style) button = function(style)
-- Self explanatory. Notice that this does not bear any function - -- self explanatory. notice that this does not bear any function -
-- only the visual part of the button. By design, onpress and onrelease -- only the visual part of the button. by design, onpress and onrelease
-- callbacks should be connected to button events for animations -- callbacks should be connected to button events for animations
return function(layout,options) return function(layout,options)
return awmtk.merge({ return awmtk.merge({
@ -216,7 +251,7 @@ awmtk.proto_templates = {
end, end,
textbox = function(style) textbox = function(style)
-- Nothing fancy here, but you can set per-widget fonts using beautiful. -- nothing fancy here, but you can set per-widget fonts using beautiful.
return function(options) return function(options)
return awmtk.merge({ return awmtk.merge({
font = style.textbox.font, font = style.textbox.font,
@ -226,7 +261,7 @@ awmtk.proto_templates = {
end, end,
hseparator = function(style) hseparator = function(style)
-- Wow, i guess? -- wow, i guess?
return function(options) return function(options)
return awmtk.merge({ return awmtk.merge({
widget = wibox.widget.separator, widget = wibox.widget.separator,
@ -239,7 +274,7 @@ awmtk.proto_templates = {
end, end,
vseparator = function(style) vseparator = function(style)
-- I'm running out of comments -- i'm running out of comments
return function(options) return function(options)
return awmtk.merge({ return awmtk.merge({
widget = wibox.widget.separator, widget = wibox.widget.separator,
@ -252,8 +287,8 @@ awmtk.proto_templates = {
end, end,
article = function(style) article = function(style)
-- Article is a template that combines 3 common pieces of a full item: -- article is a template that combines 3 common pieces of a full item:
-- Icon, name and description. Designed to be placed within a container -- icon, name and description. designed to be placed within a container
-- or a button. -- or a button.
return function(options) return function(options)
return awmtk.merge({ return awmtk.merge({
@ -323,7 +358,7 @@ awmtk.proto_templates = {
end, end,
popup = function(style) popup = function(style)
-- Popup is a distinct template designed to accomodate the "root" of -- popup is a distinct template designed to accomodate the "root" of
-- a popup, allowing one to add titlebars to popups, for example. -- a popup, allowing one to add titlebars to popups, for example.
return function(widget,options) return function(widget,options)
return awmtk.merge({ return awmtk.merge({
@ -341,13 +376,13 @@ awmtk.proto_templates = {
end, end,
titlebar = function(style) titlebar = function(style)
-- Titlebar is a separate class specifically for window and popup -- titlebar is a separate class specifically for window and popup
-- titlebars. The decision to make it a separate class was due to -- titlebars. the decision to make it a separate class was due to
-- the fact that much customization is done through default theme table -- the fact that much customization is done through default theme table
return function(layout,options) return function(layout,options)
-- If there's one thing that fascinates me, it's how much weird -- if there's one thing that fascinates me, it's how much weird
-- bugs i manage to uncover by some sort of miraculous accident. -- bugs i manage to uncover by some sort of miraculous accident.
-- This one fixes a race condition in margins+(left/right/bottom/top) configuration scenario -- this one fixes a race condition in margins+(left/right/bottom/top) configuration scenario
local margins = style.titlebar.margins local margins = style.titlebar.margins
if (style.titlebar.left or if (style.titlebar.left or
style.titlebar.right or style.titlebar.right or
@ -368,7 +403,7 @@ awmtk.proto_templates = {
end, end,
wibar = function(style) wibar = function(style)
-- Just you regular old wibar, but as a style template. -- just you regular old wibar, but as a style template.
return function(layout,options) return function(layout,options)
local margins = style.wibar.margins local margins = style.wibar.margins
if (style.wibar.left or if (style.wibar.left or
@ -390,7 +425,7 @@ awmtk.proto_templates = {
end, end,
slider = function(style) slider = function(style)
-- Slider widget but wired to work with the AWMTK2 namespace system -- slider widget but wired to work with the awmtk2 namespace system
return function(args) return function(args)
return awmtk.merge({ return awmtk.merge({
handle_shape = style.slider.handle_shape or style.slider.shape, handle_shape = style.slider.handle_shape or style.slider.shape,
@ -426,12 +461,12 @@ awmtk.proto_templates = {
end end
} }
-- Last but not least - we export a default template lib and default style. -- last but not least - we export a default template lib and default style.
-- This is done in order to allow overriding default style behaviour from theme -- this is done in order to allow overriding default style behaviour from theme
awmtk.default = awmtk.create_style("default",awmtk.proto_style,{}) awmtk.default = awmtk.create_style("default",awmtk.proto_style,{})
awmtk.templates = awmtk.create_template_lib("templates",awmtk.proto_templates,{}) awmtk.templates = awmtk.create_template_lib("templates",awmtk.proto_templates,{})
-- Generic styles for widgets that need them -- generic styles for widgets that need them
awmtk.generic = {} awmtk.generic = {}
awmtk.generic.menu = awmtk.create_style("generic_menu",awmtk.default,{}) awmtk.generic.menu = awmtk.create_style("generic_menu",awmtk.default,{})
awmtk.generic.button_list = awmtk.create_style("generic_button_list",awmtk.default,{}) awmtk.generic.button_list = awmtk.create_style("generic_button_list",awmtk.default,{})