Allow users to change checkbox style.

Added:
theme.menu_checkbox_padding
theme.menu_checkbox_color
theme.menu_checkbox_checked_color
This commit is contained in:
minde 2016-01-03 20:33:34 +02:00
parent 56fb5d3ba8
commit 0fb39f8d01
1 changed files with 75 additions and 51 deletions

View File

@ -1,88 +1,111 @@
local setmetatable = setmetatable local setmetatable = setmetatable
local print = print local color = require("gears.color")
local color = require("gears.color") local cairo = require("lgi").cairo
local cairo = require( "lgi" ).cairo local beautiful = require("beautiful")
local beautiful = require( "beautiful" )
local module = {} local module = {}
local checkedI local checkedI
local notcheckedI local notcheckedI
local isinit = false local isinit = false
local function init() -- Default checkbox style
local size = beautiful.menu_height or 16 local function default()
notcheckedI = cairo.ImageSurface(cairo.Format.ARGB32, size,size) local size = beautiful.menu_height or 16
checkedI = cairo.ImageSurface(cairo.Format.ARGB32, size,size) local x_padding = 3
local cr2 = cairo.Context(notcheckedI) local sp = size * (beautiful.menu_checkbox_padding or 0.08)
local cr = cairo.Context(checkedI) local rs = size - (2 * sp)
notcheckedI = cairo.ImageSurface(cairo.Format.ARGB32, size, size)
checkedI = cairo.ImageSurface(cairo.Format.ARGB32, size, size)
-- Checked
local cr = cairo.Context(checkedI)
cr:set_operator(cairo.Operator.CLEAR) cr:set_operator(cairo.Operator.CLEAR)
cr2:set_operator(cairo.Operator.CLEAR)
cr:paint() cr:paint()
cr2:paint()
cr:set_operator(cairo.Operator.SOURCE) cr:set_operator(cairo.Operator.SOURCE)
cr2:set_operator(cairo.Operator.SOURCE)
local sp = size*0.15 cr:set_line_width(1)
local rs = size - (2*sp) cr:set_source(color(beautiful.menu_checkbox_color or beautiful.fg_normal))
cr:set_source(color(beautiful.fg_normal)) cr:move_to(sp, sp);cr:line_to(rs, sp)
cr2:set_source(color(beautiful.fg_normal)) cr:move_to(sp, sp);cr:line_to(sp, rs)
cr:set_line_width(2) cr:move_to(sp, rs);cr:line_to(rs, rs)
cr2:set_line_width(2) cr:move_to(rs, sp);cr:line_to(rs, rs)
cr:move_to( sp , sp );cr:line_to( rs , sp )
cr:move_to( sp , sp );cr:line_to( sp , rs )
cr:move_to( sp , rs );cr:line_to( rs , rs )
cr:move_to( rs , sp );cr:line_to( rs , rs )
cr:move_to( sp , sp );cr:line_to( rs , rs )
cr:move_to( sp , rs );cr:line_to( rs , sp )
cr:stroke() cr:stroke()
cr2:move_to( sp , sp );cr2:line_to (rs , sp , beautiful.fg_normal ) cr:set_line_width(2)
cr2:move_to( sp , sp );cr2:line_to (sp , rs , beautiful.fg_normal ) cr:set_source(color(beautiful.menu_checkbox_checked_color or beautiful.fg_normal))
cr2:move_to( sp , rs );cr2:line_to (rs , rs , beautiful.fg_normal ) cr:move_to(sp + x_padding, sp + x_padding);cr:line_to(rs - x_padding, rs - x_padding)
cr2:move_to( rs , sp );cr2:line_to (rs , rs , beautiful.fg_normal ) cr:move_to(sp + x_padding, rs - x_padding);cr:line_to(rs - x_padding, sp + x_padding)
cr:stroke()
-- Unchecked
local cr2 = cairo.Context(notcheckedI)
cr2:set_operator(cairo.Operator.CLEAR)
cr2:paint()
cr2:set_operator(cairo.Operator.SOURCE)
cr2:set_line_width(1)
cr2:set_source(color(beautiful.menu_checkbox_color or beautiful.fg_normal))
cr2:move_to(sp, sp);cr2:line_to(rs, sp)
cr2:move_to(sp, sp);cr2:line_to(sp, rs)
cr2:move_to(sp, rs);cr2:line_to(rs, rs)
cr2:move_to(rs, sp);cr2:line_to(rs, rs)
cr2:stroke() cr2:stroke()
isinit = true isinit = true
end end
-- Holo checkbox style
local function holo() local function holo()
local size = beautiful.menu_height or 16 local size = (beautiful.menu_height or 16)
notcheckedI = cairo.ImageSurface(cairo.Format.ARGB32, size,size) local x_padding = 3
checkedI = cairo.ImageSurface(cairo.Format.ARGB32, size,size) local padding = size * (beautiful.menu_checkbox_padding or 0.08)
local cr2 = cairo.Context(notcheckedI)
local cr = cairo.Context(checkedI) notcheckedI = cairo.ImageSurface(cairo.Format.ARGB32, size, size)
cr:set_operator(cairo.Operator.CLEAR) checkedI = cairo.ImageSurface(cairo.Format.ARGB32, size, size)
size = (size-(2*padding))/2
-- Unchecked
local cr2 = cairo.Context(notcheckedI)
cr2:set_operator(cairo.Operator.CLEAR) cr2:set_operator(cairo.Operator.CLEAR)
cr:paint()
cr2:paint() cr2:paint()
cr:set_operator(cairo.Operator.SOURCE)
cr2:set_operator(cairo.Operator.SOURCE) cr2:set_operator(cairo.Operator.SOURCE)
local col = color(beautiful.menu_outline_color or beautiful.menu_border_color or beautiful.border_color)
cr:set_source(col) cr2:set_source(color(beautiful.menu_checkbox_color or beautiful.fg_normal))
cr2:set_source(col)
cr:set_line_width(1)
cr2:set_line_width(1) cr2:set_line_width(1)
size = size -2 cr2:arc(size+1, size+1, size, 0, math.pi*2)
cr:arc(size/2+1,size/2+1,size/2,0,math.pi*2)
cr:stroke()
cr2:arc(size/2+1,size/2+1,size/2,0,math.pi*2)
cr2:stroke() cr2:stroke()
size = size - 8
cr:set_source(color(beautiful.fg_normal)) -- Checked
cr:arc(size/2+5,size/2+5,size/2,0,math.pi*2) local cr = cairo.Context(checkedI)
cr:set_operator(cairo.Operator.CLEAR)
cr:paint()
cr:set_operator(cairo.Operator.SOURCE)
cr:set_line_width(1)
cr:set_source(color(beautiful.menu_checkbox_color or beautiful.fg_normal))
cr:arc(size+1, size+1, size, 0, math.pi*2)
cr:stroke()
cr:set_source(color(beautiful.menu_checkbox_checked_color or beautiful.fg_normal))
cr:arc(size+1, size+1, size-x_padding, 0, math.pi*2)
cr:fill() cr:fill()
isinit = true
end end
local style = { local style = {
holo = holo, holo = holo,
default = init, default = default,
} }
function module.checked() function module.checked()
if not isinit then if not isinit then
style[beautiful.menu_checkbox_style or "default"]() style[beautiful.menu_checkbox_style or "default"]()
end end
return checkedI return checkedI
end end
@ -90,6 +113,7 @@ function module.unchecked()
if not isinit then if not isinit then
style[beautiful.menu_checkbox_style or "default"]() style[beautiful.menu_checkbox_style or "default"]()
end end
return notcheckedI return notcheckedI
end end