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/>.
-- RenoTK (formerly AWMTK2) - Template/Granular styling library for Reno
-- 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
local wibox = require("wibox")
local awful = require("awful")
local gears = require("gears")
@ -15,23 +15,23 @@ beautiful.templates = beautiful.templates or {}
local awmtk = {}
-- {{{ Utils
-- {{{ utils
awmtk.create_delta = function(name,instance_delta,class_delta,parent_delta)
-- To save memory, we create proxies for lower layers called "deltas"
-- This function creates that proxy layer using metatables
-- to save memory, we create proxies for lower layers called "deltas"
-- this function creates that proxy layer using metatables
-- Fun story - i used instance_delta instead of {} at first.
-- The results were horrifying and confusing.
-- fun story - i used instance_delta instead of {} at first.
-- the results were horrifying and confusing.
return setmetatable({},{
__index = function(self,k)
-- Per-instance overrides are top priority
-- per-instance overrides are top priority
if rawget(instance_delta,k) then
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"
and rawget(class_delta[name],k) then
return rawget(class_delta[name],k)
-- Parent is fallback
-- parent is fallback
elseif parent_delta[k] then
return parent_delta[k]
end
@ -40,7 +40,7 @@ awmtk.create_delta = function(name,instance_delta,class_delta,parent_delta)
end
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 odelta = (overrides and overrides[name]) or {}
local cdelta = beautiful.widgets[name] or {}
@ -73,35 +73,70 @@ awmtk.build_templates = function(templates,style)
return new_templates
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
-- }}}
-- {{{ Default style
-- {{{ default style
-- Prototype style
-- Notice that it's not awmtk.default style - it's used as a base for default.
-- prototype style
-- notice that it's not awmtk.default style - it's used as a base for default.
awmtk.proto_style = {
base = setmetatable({
-- { Backgrounds
-- { backgrounds
-- custom background color for highlighting elements
bg_highlight = beautiful.bg_highlight or beautiful.bg_focus,
-- allow more complex themes to define background images
bgimage_focus = beautiful.bgimage_focus,
bgimage_normal = beautiful.bgimage_normal,
-- }
-- { Borders
-- Borders for popups
-- { borders
-- borders for popups
shape_border_width = beautiful.shape_border_width or 0,
shape_border_color = beautiful.shape_border_color or beautiful.bg_normal,
-- }
-- { Callbacks
-- { callbacks
-- a tiny bit more complex thing to account for more extensibility
-- the stub functions do nothing - you should implement functionality inside theme
onpress = function() end,
onrelease = function() end,
-- }
-- { Shapes
-- { shapes
margins = 5,
spacing = 5,
shape = function(cr, width, height)
@ -111,7 +146,7 @@ awmtk.proto_style = {
},{__index = beautiful})
}
-- Subclasses
-- subclasses
awmtk.proto_style.container = awmtk.create_delta("container",{
},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)
-- }}}
-- {{{ Generic templates
-- {{{ generic 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
container = function(style)
-- Container is practically any "box" that contains buttons, textboxes,
-- and anything you want to put into the container. Do not confuse with
-- container is practically any "box" that contains buttons, textboxes,
-- and anything you want to put into the container. do not confuse with
-- popup - containers are designed to separate contents within a popup.
return function(layout,options)
return awmtk.merge({
@ -188,14 +223,14 @@ awmtk.proto_templates = {
shape = style.container.shape,
shape_border_color = style.container.shape_border_color,
shape_border_width = style.container.shape_border_width,
widget = wibox.container.background
widget = awmtk.wrap_hooks(wibox.container.background,options)
},options or {})
end
end,
button = function(style)
-- Self explanatory. Notice that this does not bear any function -
-- only the visual part of the button. By design, onpress and onrelease
-- self explanatory. notice that this does not bear any function -
-- only the visual part of the button. by design, onpress and onrelease
-- callbacks should be connected to button events for animations
return function(layout,options)
return awmtk.merge({
@ -216,7 +251,7 @@ awmtk.proto_templates = {
end,
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 awmtk.merge({
font = style.textbox.font,
@ -226,7 +261,7 @@ awmtk.proto_templates = {
end,
hseparator = function(style)
-- Wow, i guess?
-- wow, i guess?
return function(options)
return awmtk.merge({
widget = wibox.widget.separator,
@ -239,7 +274,7 @@ awmtk.proto_templates = {
end,
vseparator = function(style)
-- I'm running out of comments
-- i'm running out of comments
return function(options)
return awmtk.merge({
widget = wibox.widget.separator,
@ -252,8 +287,8 @@ awmtk.proto_templates = {
end,
article = function(style)
-- Article is a template that combines 3 common pieces of a full item:
-- Icon, name and description. Designed to be placed within a container
-- article is a template that combines 3 common pieces of a full item:
-- icon, name and description. designed to be placed within a container
-- or a button.
return function(options)
return awmtk.merge({
@ -323,7 +358,7 @@ awmtk.proto_templates = {
end,
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.
return function(widget,options)
return awmtk.merge({
@ -341,13 +376,13 @@ awmtk.proto_templates = {
end,
titlebar = function(style)
-- Titlebar is a separate class specifically for window and popup
-- titlebars. The decision to make it a separate class was due to
-- titlebar is a separate class specifically for window and popup
-- titlebars. the decision to make it a separate class was due to
-- the fact that much customization is done through default theme table
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.
-- 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
if (style.titlebar.left or
style.titlebar.right or
@ -368,7 +403,7 @@ awmtk.proto_templates = {
end,
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)
local margins = style.wibar.margins
if (style.wibar.left or
@ -390,7 +425,7 @@ awmtk.proto_templates = {
end,
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 awmtk.merge({
handle_shape = style.slider.handle_shape or style.slider.shape,
@ -426,12 +461,12 @@ awmtk.proto_templates = {
end
}
-- 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
-- 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
awmtk.default = awmtk.create_style("default",awmtk.proto_style,{})
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.menu = awmtk.create_style("generic_menu",awmtk.default,{})
awmtk.generic.button_list = awmtk.create_style("generic_button_list",awmtk.default,{})