awesomerc/ui/desktop_decoration/bar.lua

226 lines
7.2 KiB
Lua
Raw Normal View History

2021-05-25 17:37:29 +02:00
local awibar = require 'awful.wibar'
local awful = require 'awful'
local battery_widget = require 'battery-widget'
2021-05-25 17:37:29 +02:00
local beautiful = require 'beautiful'
local container_background = require 'wibox.container.background'
local container_margin = require 'wibox.container.margin'
local container_place = require 'wibox.container.place'
local gshape = require 'gears.shape'
local layout_align = require 'wibox.layout.align'
local layout_fixed = require 'wibox.layout.fixed'
local systray = require 'wibox.widget.systray'
local textclock = require 'wibox.widget.textclock'
local widget = require 'wibox.widget'
local mybattery = require 'rc.ui.desktop_decoration.widgets.battery'
local mycommands = require 'rc.configuration.prompt_commands'
2021-05-25 17:37:29 +02:00
local mymainmenu = require 'rc.ui.menu.mymainmenu'
local myprompt = require 'rc.ui.desktop_decoration.widgets.prompt'
local mytaglist = require 'MyTagListWidget'
2021-05-25 17:37:29 +02:00
local capi = {
screen = _G.screen
}
local abs = math.abs
local dpi = beautiful.xresources.apply_dpi
2021-05-25 17:37:29 +02:00
local function get_screen_id (screen)
local s = capi.screen[screen or 1]
2021-05-25 17:37:29 +02:00
return s.index
end
--- Build a widget box in the bar.
-- A widget box is a combinaision of "shaped" `wibox.container/background` and
-- `wibox.container.margin` to create this nice round-colored-buttons I like.
-- @tparam args table
-- @tparam args.screen The screen where the widget will be drawn.
-- @tparam args.widget The widget to draw.
-- @tparam[opt] args.bg The box's background.
-- @tpram[opt] args.fg The box's foreground (used mainly for textbox text color).
-- @treturn wibox.container.background The builded widget.
local function build_widget (args)
local screen_id = get_screen_id(args.screen)
-- Callback for the shape function.
-- If the widget is almost a square: draw a circle. Otherwise, draw a
-- rounded_bar.
local shape_callback = function (cr, width, height)
local shape = gshape.circle
-- 10 is an arbitrary value I found after some tests :shrug:
if abs(width - height) > 10 then
shape = gshape.rounded_bar
end
return shape(cr, width, height)
end
local box_widget = widget {
widget = container_background,
shape = shape_callback,
bg = args.bg,
fg = args.fg,
{
widget = container_margin,
draw_empty = false,
top = dpi(beautiful.bar_box_padding_y, screen_id),
bottom = dpi(beautiful.bar_box_padding_y, screen_id),
right = dpi(beautiful.bar_box_padding_x, screen_id),
left = dpi(beautiful.bar_box_padding_x, screen_id),
args.widget
}
}
return box_widget
end
local bar = { _private = { instances = {} }, mt = {} }
--- Get the bar instance for a given screen.
-- If no instance was found, we build a new one.
-- @tparam screen screen|integer The bar's screen.
-- @treturn wibox.wibar
function bar:instance (screen)
2021-05-25 17:37:29 +02:00
local screen_id = get_screen_id(screen)
local instance = self._private.instances[screen_id]
if not instance then
instance = bar.new(screen)
self._private.instances[screen_id] = instance
end
return instance
end
function bar.new (screen)
local my_bar = {}
my_bar.launcher = build_widget {
2021-05-25 17:37:29 +02:00
screen = screen,
bg = '#2196F3',
widget = awful.widget.launcher {
image = beautiful.icon_apps,
menu = mymainmenu()
}
2021-05-25 17:37:29 +02:00
}
my_bar.textclock = build_widget {
bg = '#FF5722',
fg = '#ECEFF1',
widget = textclock('%l:%M %p')
2021-05-25 17:37:29 +02:00
}
my_bar.promptbox = myprompt {
commands = mycommands
}
2021-05-25 17:37:29 +02:00
-- This widget needs to be reworded and integrated inside the project.
my_bar.taglist = mytaglist.new {
screen = screen
}
2021-05-25 17:37:29 +02:00
my_bar.battery = build_widget {
2021-05-25 17:37:29 +02:00
screen = screen,
bg = '#673AB7',
widget = mybattery {
screen = screen,
device_path = battery_widget.get_BAT0_device_path(),
color = '#ECEFF1'
2021-05-25 17:37:29 +02:00
}
}
my_bar.systray = build_widget {
2021-05-25 17:37:29 +02:00
screen = screen,
bg = '#455A64',
widget = systray()
2021-05-25 17:37:29 +02:00
}
my_bar.wibar = awibar {
2021-05-25 17:37:29 +02:00
screen = screen,
position = 'bottom',
height = dpi(beautiful.bar_height, screen),
width = beautiful.bar_width -- Width is a percentage of the screen size
2021-05-25 17:37:29 +02:00
}
my_bar.wibar:setup {
-- Bar margins
widget = container_margin,
top = dpi(beautiful.bar_margin_y, screen),
bottom = dpi(beautiful.bar_margin_y, screen),
right = dpi(beautiful.bar_margin_x, screen),
left = dpi(beautiful.bar_margin_x, screen),
{
-- Physical bar
widget = container_background,
bg = '#263238D9',
shape = function (cr, width, height)
gshape.rounded_rect(cr, width, height, 10)
end,
{
-- Bar paddings
widget = container_margin,
top = dpi(beautiful.bar_padding_y, screen),
bottom = dpi(beautiful.bar_padding_y, screen),
right = dpi(beautiful.bar_padding_x, screen),
left = dpi(beautiful.bar_padding_x, screen),
{
-- Bar content
layout = layout_align.horizontal,
{
-- Left side of the bar, box margins
widget = container_margin,
top = dpi(beautiful.bar_box_margin_y, screen),
bottom = dpi(beautiful.bar_box_margin_y, screen),
right = dpi(beautiful.bar_box_margin_x, screen),
left = dpi(beautiful.bar_box_margin_x, screen),
{
-- Left widget boxes
layout = layout_fixed.horizontal,
spacing = dpi(beautiful.bar_box_spacing, screen),
my_bar.launcher,
my_bar.promptbox
}
},
-- Middle widget is the custom taglist
-- it doesn't need box/margins/...
{
widget = container_place,
my_bar.taglist
},
{
-- Right side of the bar, box margins
widget = container_margin,
top = dpi(beautiful.bar_box_margin_y, screen),
bottom = dpi(beautiful.bar_box_margin_y, screen),
right = dpi(beautiful.bar_box_margin_x, screen),
left = dpi(beautiful.bar_box_margin_x, screen),
{
-- Right widget boxes
layout = layout_fixed.horizontal,
spacing = dpi(beautiful.bar_box_spacing, screen),
my_bar.systray,
my_bar.battery,
my_bar.textclock
}
}
}
}
2021-05-25 17:37:29 +02:00
}
}
return my_bar
end
function bar.mt:__call (...)
2021-05-25 17:37:29 +02:00
return self:instance(...)
end
return setmetatable(bar, bar.mt)