awesomerc/ui/desktop_decoration/bar/init.lua

248 lines
8.0 KiB
Lua

local awibar = require "awful.wibar"
local awful = require "awful"
local battery_widget = require "battery-widget"
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 mycommands = require "rc.configuration.prompt_commands"
local mymainmenu = require "rc.ui.menu.mymainmenu"
local mytaglist = require "MyTagListWidget"
local bar_widgets = require "rc.ui.desktop_decoration.bar.widgets"
local mybattery = bar_widgets.battery
local myprompt = bar_widgets.prompt
local capi = {
screen = _G.screen,
}
local abs = math.abs
local dpi = beautiful.xresources.apply_dpi
local function get_screen_id(screen)
local s = capi.screen[screen or 1]
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_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,
},
bg = args.bg,
fg = args.fg,
shape = shape_callback,
widget = container_background,
}
return box_widget
end
local bar = { _private = { instances = {} }, mt = {} }
bar.widgets = bar_widgets
--- 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)
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 {
screen = screen,
bg = "#2196F3",
widget = awful.widget.launcher {
image = beautiful.icon_apps,
menu = mymainmenu(),
},
}
my_bar.textclock = build_widget {
screen = screen,
bg = "#FF5722",
fg = "#ECEFF1",
widget = textclock "%l:%M %p",
}
my_bar.promptbox = myprompt {
commands = mycommands,
}
-- This widget needs to be reworded and integrated inside the project.
my_bar.taglist = mytaglist.new {
screen = screen,
}
my_bar.battery = build_widget {
screen = screen,
bg = "#673AB7",
widget = mybattery {
screen = screen,
device_path = battery_widget.get_BAT0_device_path(),
color = "#ECEFF1",
},
}
my_bar.systray = build_widget {
screen = screen,
bg = "#455A64",
widget = systray(),
}
my_bar.wibar = awibar {
screen = screen,
position = "bottom",
height = dpi(beautiful.bar_height, screen),
width = beautiful.bar_width, -- Width is a percentage of the screen size
}
my_bar.wibar:setup {
-- Bar margins
{
-- Physical bar
{
-- Bar paddings
{
-- Bar content
{
-- Left side of the bar, align on the left
{
-- box margins
{
-- Left widget boxes
my_bar.launcher,
my_bar.promptbox,
spacing = dpi(
beautiful.bar_box_spacing,
screen
),
layout = layout_fixed.horizontal,
},
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),
widget = container_margin,
},
halign = "left",
widget = container_place,
},
expand = "outside",
layout = layout_align.horizontal,
-- Middle widget is the custom taglist
-- it doesn't need box/margins/...
{
my_bar.taglist,
widget = container_place,
},
{
-- Right side of the bar, align on the right
{
-- box margins
{
-- Right widget boxes
my_bar.systray,
my_bar.battery,
my_bar.textclock,
spacing = dpi(
beautiful.bar_box_spacing,
screen
),
layout = layout_fixed.horizontal,
},
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),
widget = container_margin,
},
halign = "right",
widget = container_place,
},
},
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),
widget = container_margin,
},
bg = "#263238D9",
shape = function(cr, width, height)
gshape.rounded_rect(cr, width, height, 10)
end,
widget = container_background,
},
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),
widget = container_margin,
}
return my_bar
end
function bar.mt:__call(...)
return self:instance(...)
end
return setmetatable(bar, bar.mt)