2008-09-29 16:49:18 +02:00
|
|
|
---------------------------------------------------------------------------
|
|
|
|
-- @author Julien Danjou <julien@danjou.info>
|
|
|
|
-- @copyright 2008 Julien Danjou
|
|
|
|
-- @release @AWESOME_VERSION@
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
-- Grab environment we need
|
|
|
|
local math = math
|
2008-12-03 15:23:10 +01:00
|
|
|
local image = image
|
2008-10-23 11:22:58 +02:00
|
|
|
local pairs = pairs
|
2009-05-18 16:42:03 +02:00
|
|
|
local type = type
|
2009-04-11 14:07:45 +02:00
|
|
|
local setmetatable = setmetatable
|
2009-05-07 14:13:36 +02:00
|
|
|
local type = type
|
2008-09-29 16:49:18 +02:00
|
|
|
local capi =
|
|
|
|
{
|
|
|
|
wibox = wibox,
|
|
|
|
widget = widget,
|
|
|
|
client = client,
|
|
|
|
}
|
2009-08-04 21:04:32 +02:00
|
|
|
local abutton = require("awful.button")
|
2008-11-13 11:53:41 +01:00
|
|
|
local beautiful = require("beautiful")
|
2008-09-29 16:49:18 +02:00
|
|
|
local hooks = require("awful.hooks")
|
|
|
|
local util = require("awful.util")
|
2008-10-09 16:27:37 +02:00
|
|
|
local widget = require("awful.widget")
|
2008-11-17 17:12:54 +01:00
|
|
|
local mouse = require("awful.mouse")
|
2009-04-12 15:00:11 +02:00
|
|
|
local client = require("awful.client")
|
2009-07-02 01:41:26 +02:00
|
|
|
local layout = require("awful.widget.layout")
|
2008-09-29 16:49:18 +02:00
|
|
|
|
|
|
|
--- Titlebar module for awful
|
|
|
|
module("awful.titlebar")
|
|
|
|
|
|
|
|
-- Privata data
|
2009-04-11 14:07:45 +02:00
|
|
|
local data = setmetatable({}, { __mode = 'k' })
|
2008-09-29 16:49:18 +02:00
|
|
|
|
2009-04-12 13:54:29 +02:00
|
|
|
-- Predeclaration for buttons
|
|
|
|
local button_groups
|
|
|
|
|
2009-04-29 15:05:23 +02:00
|
|
|
local function button_callback_focus_raise_move(w, t)
|
|
|
|
capi.client.focus = t.client
|
|
|
|
t.client:raise()
|
|
|
|
mouse.client.move(t.client)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function button_callback_move(w, t)
|
|
|
|
return mouse.client.move(t.client)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function button_callback_resize(w, t)
|
|
|
|
return mouse.client.resize(t.client)
|
|
|
|
end
|
|
|
|
|
2008-09-29 16:49:18 +02:00
|
|
|
--- Create a standard titlebar.
|
|
|
|
-- @param c The client.
|
|
|
|
-- @param args Arguments.
|
2009-02-02 09:51:28 +01:00
|
|
|
-- modkey: the modkey used for the bindings.
|
2008-09-29 16:49:18 +02:00
|
|
|
-- fg: the foreground color.
|
|
|
|
-- bg: the background color.
|
|
|
|
-- fg_focus: the foreground color for focused window.
|
|
|
|
-- fg_focus: the background color for focused window.
|
|
|
|
-- width: the titlebar width
|
|
|
|
function add(c, args)
|
2009-02-02 09:28:45 +01:00
|
|
|
if not c or (c.type ~= "normal" and c.type ~= "dialog") then return end
|
2008-09-29 16:49:18 +02:00
|
|
|
if not args then args = {} end
|
2008-10-09 16:27:37 +02:00
|
|
|
local theme = beautiful.get()
|
2008-09-29 16:49:18 +02:00
|
|
|
-- Store colors
|
|
|
|
data[c] = {}
|
|
|
|
data[c].fg = args.fg or theme.titlebar_fg_normal or theme.fg_normal
|
|
|
|
data[c].bg = args.bg or theme.titlebar_bg_normal or theme.bg_normal
|
|
|
|
data[c].fg_focus = args.fg_focus or theme.titlebar_fg_focus or theme.fg_focus
|
|
|
|
data[c].bg_focus = args.bg_focus or theme.titlebar_bg_focus or theme.bg_focus
|
|
|
|
data[c].width = args.width
|
2009-05-01 04:16:17 +02:00
|
|
|
data[c].font = args.font or theme.titlebar_font or theme.font
|
2008-09-29 16:49:18 +02:00
|
|
|
|
2008-10-12 17:14:44 +02:00
|
|
|
local tb = capi.wibox(args)
|
2008-09-29 16:49:18 +02:00
|
|
|
|
2009-07-02 01:41:26 +02:00
|
|
|
local title = capi.widget({ type = "textbox" })
|
2009-01-14 20:29:21 +01:00
|
|
|
if c.name then
|
2009-05-01 04:16:17 +02:00
|
|
|
title.text = "<span font_desc='" .. data[c].font .. "'> " ..
|
|
|
|
util.escape(c.name) .. " </span>"
|
2009-01-14 20:29:21 +01:00
|
|
|
end
|
2008-11-05 16:22:13 +01:00
|
|
|
|
|
|
|
-- Redirect relevant events to the client the titlebar belongs to
|
2009-08-14 16:46:35 +02:00
|
|
|
local bts = util.table.join(
|
2009-08-04 21:04:32 +02:00
|
|
|
abutton({ }, 1, button_callback_focus_raise_move),
|
|
|
|
abutton({ args.modkey }, 1, button_callback_move),
|
|
|
|
abutton({ args.modkey }, 3, button_callback_resize))
|
2009-08-14 16:46:35 +02:00
|
|
|
title:buttons(bts)
|
2008-09-29 16:49:18 +02:00
|
|
|
|
2009-07-02 01:41:26 +02:00
|
|
|
local appicon = capi.widget({ type = "imagebox" })
|
2008-09-29 16:49:18 +02:00
|
|
|
appicon.image = c.icon
|
|
|
|
|
2009-04-12 13:54:29 +02:00
|
|
|
-- for each button group, call create for the client.
|
|
|
|
-- if a button set is created add the set to the
|
|
|
|
-- data[c].button_sets for late updates and add the
|
|
|
|
-- individual buttons to the array part of the widget
|
|
|
|
-- list
|
2009-07-02 01:41:26 +02:00
|
|
|
local widget_list = {
|
|
|
|
layout = layout.horizontal.rightleft
|
|
|
|
}
|
|
|
|
local iw = 1
|
2009-04-12 13:54:29 +02:00
|
|
|
local is = 1
|
|
|
|
data[c].button_sets = {}
|
|
|
|
for i = 1, #button_groups do
|
2009-08-04 20:58:34 +02:00
|
|
|
local set = button_groups[i].create(c, args.modkey, theme)
|
2009-04-12 13:54:29 +02:00
|
|
|
if (set) then
|
|
|
|
data[c].button_sets[is] = set
|
|
|
|
is = is + 1
|
|
|
|
for n,b in pairs(set) do
|
|
|
|
widget_list[iw] = b
|
|
|
|
iw = iw + 1
|
2008-12-08 16:43:24 +01:00
|
|
|
end
|
2008-11-12 11:55:35 +01:00
|
|
|
end
|
2008-09-29 16:49:18 +02:00
|
|
|
end
|
|
|
|
|
2009-07-02 01:41:26 +02:00
|
|
|
tb.widgets = {
|
|
|
|
widget_list,
|
|
|
|
{
|
|
|
|
appicon = appicon,
|
|
|
|
title = title,
|
|
|
|
layout = layout.horizontal.flex
|
|
|
|
},
|
|
|
|
layout = layout.horizontal.rightleft
|
|
|
|
}
|
|
|
|
|
2008-09-29 16:49:18 +02:00
|
|
|
c.titlebar = tb
|
|
|
|
|
|
|
|
update(c)
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Update a titlebar. This should be called in some hooks.
|
|
|
|
-- @param c The client to update.
|
|
|
|
-- @param prop The property name which has changed.
|
|
|
|
function update(c, prop)
|
2009-05-18 16:42:03 +02:00
|
|
|
if type(c) == "client" and c.titlebar and data[c] then
|
2008-10-19 19:14:55 +02:00
|
|
|
local widgets = c.titlebar.widgets
|
2008-09-29 16:49:18 +02:00
|
|
|
if prop == "name" then
|
2009-07-02 01:41:26 +02:00
|
|
|
if widgets[2].title then
|
|
|
|
widgets[2].title.text = "<span font_desc='" .. data[c].font ..
|
2009-05-01 04:16:17 +02:00
|
|
|
"'> ".. util.escape(c.name) .. " </span>"
|
2008-09-29 16:49:18 +02:00
|
|
|
end
|
|
|
|
elseif prop == "icon" then
|
2009-07-02 01:41:26 +02:00
|
|
|
if widgets[2].appicon then
|
|
|
|
widgets[2].appicon.image = c.icon
|
2008-09-29 16:49:18 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
if capi.client.focus == c then
|
|
|
|
c.titlebar.fg = data[c].fg_focus
|
|
|
|
c.titlebar.bg = data[c].bg_focus
|
|
|
|
else
|
|
|
|
c.titlebar.fg = data[c].fg
|
|
|
|
c.titlebar.bg = data[c].bg
|
2009-04-12 13:54:29 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
-- iterated of all registered button_sets and update
|
|
|
|
local sets = data[c].button_sets
|
|
|
|
for i = 1, #sets do
|
|
|
|
sets[i].update(c,prop)
|
2008-09-29 16:49:18 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Remove a titlebar from a client.
|
|
|
|
-- @param c The client.
|
|
|
|
function remove(c)
|
|
|
|
c.titlebar = nil
|
|
|
|
data[c] = nil
|
|
|
|
end
|
|
|
|
|
2009-04-12 13:54:29 +02:00
|
|
|
-- Create a new button for the toolbar
|
|
|
|
-- @param c The client of the titlebar
|
|
|
|
-- @param name The base name of the button (i.e. close)
|
|
|
|
-- @param modkey ... you know that one, don't you?
|
|
|
|
-- @param theme The theme from beautifull. Used to get the image paths
|
|
|
|
-- @param state The state the button is associated to. Containse path the action and info about the image
|
|
|
|
local function button_new(c, name, modkey, theme, state)
|
2009-08-14 16:46:35 +02:00
|
|
|
local bts = abutton({ }, 1, nil, state.action)
|
|
|
|
|
2009-04-12 13:54:29 +02:00
|
|
|
-- get the image path from the theme. Only return a button if we find an image
|
|
|
|
local img
|
|
|
|
img = "titlebar_" .. name .. "_button_" .. state.img
|
|
|
|
img = theme[img]
|
|
|
|
if not img then return end
|
|
|
|
img = image(img)
|
|
|
|
if not img then return end
|
|
|
|
|
|
|
|
-- now create the button
|
|
|
|
local bname = name .. "_" .. state.idx
|
|
|
|
local button = widget.button({ image = img })
|
|
|
|
if not button then return end
|
2009-08-14 16:46:35 +02:00
|
|
|
local rbts = button:buttons()
|
2009-04-12 13:54:29 +02:00
|
|
|
|
2009-08-14 16:46:35 +02:00
|
|
|
for k, v in pairs(rbts) do
|
|
|
|
bts[#bts + 1] = v
|
|
|
|
end
|
2009-04-12 13:54:29 +02:00
|
|
|
|
2009-08-14 16:46:35 +02:00
|
|
|
button:buttons(bts)
|
2009-04-12 13:54:29 +02:00
|
|
|
button.visible = false
|
|
|
|
return button
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Update the buttons in a button group
|
|
|
|
-- @param s The button group to update
|
|
|
|
-- @param c The client of the titlebar
|
|
|
|
-- @param p The property that has changed
|
|
|
|
local function button_group_update(s,c,p)
|
|
|
|
-- hide the currently active button, get the new state and show the new button
|
|
|
|
local n = s.select_state(c,p)
|
|
|
|
if n == nil then return end
|
|
|
|
if (s.active ~= nil) then s.active.visible = false end
|
|
|
|
s.active = s.buttons[n]
|
|
|
|
s.active.visible = true
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Create all buttons in a group
|
|
|
|
-- @param c The client of the titlebar
|
|
|
|
-- @param group The button group to create the buttons for
|
|
|
|
-- @param modkey ...
|
|
|
|
-- @param theme Theme for the image paths
|
|
|
|
local function button_group_create(c, group, modkey, theme )
|
|
|
|
local s = {}
|
|
|
|
s.name = group.name
|
|
|
|
s.select_state = group.select_state
|
2009-07-02 01:41:26 +02:00
|
|
|
s.buttons = {
|
|
|
|
layout = layout.horizontal.rightleft
|
|
|
|
}
|
2009-04-12 13:54:29 +02:00
|
|
|
for n,state in pairs(group.states) do
|
|
|
|
s.buttons[n] = button_new(c, s.name, modkey, theme, state)
|
|
|
|
if (s.buttons[n] == nil) then return end
|
|
|
|
for a,v in pairs(group.attributes) do
|
|
|
|
s.buttons[n][a] = v
|
|
|
|
end
|
|
|
|
end
|
|
|
|
function s.update(c,p) button_group_update(s,c,p) end
|
|
|
|
return s
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Builds a new button group
|
|
|
|
-- @param name The base name for the buttons in the group (i.e. "close")
|
|
|
|
-- @param attrs Common attributes for the buttons (i.e. {align = "right")
|
|
|
|
-- @param sfn State select function.
|
|
|
|
-- @param args The states of the button
|
|
|
|
local function button_group(name, attrs, sfn, ...)
|
|
|
|
local s = {}
|
|
|
|
s.name = name
|
|
|
|
s.select_state = sfn
|
|
|
|
s.attributes = attrs
|
|
|
|
s.states = {}
|
|
|
|
|
|
|
|
for i = 1, #arg do
|
|
|
|
local state = arg[i]
|
|
|
|
s.states[state.idx] = state
|
|
|
|
end
|
|
|
|
|
|
|
|
function s.create(c,modkey, theme) return button_group_create(c,s,modkey, theme) end
|
|
|
|
return s
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Select a state for a client based on an attribute of the client and whether it has focus
|
|
|
|
-- @param c The client of the titlebar
|
|
|
|
-- @param p The property that has changed
|
|
|
|
-- @param a The property to check
|
|
|
|
local function select_state(c,p,a)
|
|
|
|
if (c == nil) then return "n/i" end
|
|
|
|
if capi.client.focus == c then
|
|
|
|
if c[a] then
|
|
|
|
return "f/a"
|
|
|
|
else
|
|
|
|
return "f/i"
|
|
|
|
end
|
|
|
|
else
|
|
|
|
if c[a] then
|
|
|
|
return "n/a"
|
|
|
|
else
|
|
|
|
return "n/i"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-04-12 15:00:11 +02:00
|
|
|
-- Select a state for a client based on whether it's floating or not
|
|
|
|
-- @param c The client of the titlebar
|
|
|
|
-- @param p The property that has changed
|
|
|
|
local function select_state_floating(c,p)
|
|
|
|
if not c then return end
|
|
|
|
if capi.client.focus == c then
|
|
|
|
if client.floating.get(c) then
|
|
|
|
return "f/a"
|
|
|
|
end
|
|
|
|
return "f/i"
|
|
|
|
end
|
|
|
|
if client.floating.get(c) then
|
|
|
|
return "n/a"
|
|
|
|
end
|
|
|
|
return "n/i"
|
|
|
|
end
|
|
|
|
|
2009-04-12 13:54:29 +02:00
|
|
|
-- Select a state for a client based on whether it's maximized or not
|
|
|
|
-- @param c The client of the titlebar
|
|
|
|
-- @param p The property that has changed
|
|
|
|
local function select_state_maximized(c,p)
|
|
|
|
if (c == nil) then return "n/i" end
|
|
|
|
if capi.client.focus == c then
|
|
|
|
if c.maximized_horizontal or c.maximized_vertical then
|
|
|
|
return "f/a"
|
|
|
|
else
|
|
|
|
return "f/i"
|
|
|
|
end
|
|
|
|
else
|
|
|
|
if c.maximized_horizontal or c.maximized_vertical then
|
|
|
|
return "n/a"
|
|
|
|
else
|
|
|
|
return "n/i"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Select a state for a client based on whether it has focus or not
|
|
|
|
-- @param c The client of the titlebar
|
|
|
|
-- @param p The property that has changed
|
|
|
|
local function select_state_focus(c,p)
|
|
|
|
if c and capi.client.focus == c then
|
|
|
|
return "f"
|
|
|
|
end
|
|
|
|
return "n"
|
|
|
|
end
|
|
|
|
|
|
|
|
-- These are the predefined button groups
|
|
|
|
-- A short explanation using 'close_buttons' as an example:
|
|
|
|
-- "close" : name of the button, the images for this button are taken from the
|
|
|
|
-- theme variables titlebar_close_button_...
|
|
|
|
-- { align ... : attributes of all the buttons
|
|
|
|
-- select_state_focus : This function returns a short string used to describe
|
|
|
|
-- the state. In this case either "n" or "f" depending on
|
|
|
|
-- the focus state of the client. These strings can be
|
|
|
|
-- choosen freely but the< must match one of the idx fuekds
|
|
|
|
-- of the states below
|
|
|
|
-- { idx = "n" ... : This is the state of the button for the 'unfocussed'
|
|
|
|
-- (normal) state. The idx = "n" parameter connects this
|
|
|
|
-- button to the return value of the 'select_state_focus'
|
|
|
|
-- function. The img = "normal" parameter is used to
|
|
|
|
-- determine its image. In this case the iamge is taken from
|
|
|
|
-- the theme variable "titlebar_close_button_normal".
|
|
|
|
-- Finally the last parameter is the action for mouse
|
|
|
|
-- button 1
|
|
|
|
|
|
|
|
local ontop_buttons = button_group("ontop",
|
2009-04-12 15:05:33 +02:00
|
|
|
{ align = "right" },
|
2009-04-12 13:54:29 +02:00
|
|
|
function(c,p) return select_state(c, p, "ontop") end,
|
2009-04-12 15:05:33 +02:00
|
|
|
{ idx = "n/i", img = "normal_inactive",
|
2009-04-29 15:05:23 +02:00
|
|
|
action = function(w, t) t.client.ontop = true end },
|
2009-04-12 15:05:33 +02:00
|
|
|
{ idx = "f/i", img = "focus_inactive",
|
2009-04-29 15:05:23 +02:00
|
|
|
action = function(w, t) t.client.ontop = true end },
|
2009-04-12 15:05:33 +02:00
|
|
|
{ idx = "n/a", img = "normal_active",
|
2009-04-29 15:05:23 +02:00
|
|
|
action = function(w, t) t.client.ontop = false end },
|
2009-04-12 15:05:33 +02:00
|
|
|
{ idx = "f/a", img = "focus_active",
|
2009-04-29 15:05:23 +02:00
|
|
|
action = function(w, t) t.client.ontop = false end })
|
2009-04-12 13:54:29 +02:00
|
|
|
|
|
|
|
local sticky_buttons = button_group("sticky",
|
2009-04-12 15:05:33 +02:00
|
|
|
{ align = "right" },
|
2009-04-12 13:54:29 +02:00
|
|
|
function(c,p) return select_state(c,p,"sticky") end,
|
2009-04-12 15:05:33 +02:00
|
|
|
{ idx = "n/i", img = "normal_inactive",
|
2009-04-29 15:05:23 +02:00
|
|
|
action = function(w, t) t.client.sticky = true end },
|
2009-04-12 15:05:33 +02:00
|
|
|
{ idx = "f/i", img = "focus_inactive",
|
2009-04-29 15:05:23 +02:00
|
|
|
action = function(w, t) t.client.sticky = true end },
|
2009-04-12 15:05:33 +02:00
|
|
|
{ idx = "n/a", img = "normal_active",
|
2009-04-29 15:05:23 +02:00
|
|
|
action = function(w, t) t.client.sticky = false end },
|
2009-04-12 15:05:33 +02:00
|
|
|
{ idx = "f/a", img = "focus_active",
|
2009-04-29 15:05:23 +02:00
|
|
|
action = function(w, t) t.client.sticky = false end })
|
2009-04-12 13:54:29 +02:00
|
|
|
|
|
|
|
local maximized_buttons = button_group("maximized",
|
2009-04-12 15:05:33 +02:00
|
|
|
{ align = "right" },
|
2009-04-12 13:54:29 +02:00
|
|
|
select_state_maximized,
|
2009-04-12 15:05:33 +02:00
|
|
|
{ idx = "n/i", img = "normal_inactive",
|
2009-04-29 15:05:23 +02:00
|
|
|
action = function(w, t) t.client.maximized_horizontal = true
|
|
|
|
t.client.maximized_vertical = true end },
|
2009-04-12 15:05:33 +02:00
|
|
|
{ idx = "f/i", img = "focus_inactive",
|
2009-04-29 15:05:23 +02:00
|
|
|
action = function(w, t) t.client.maximized_horizontal = true
|
|
|
|
t.client.maximized_vertical = true end },
|
2009-04-12 15:05:33 +02:00
|
|
|
{ idx = "n/a", img = "normal_active",
|
2009-04-29 15:05:23 +02:00
|
|
|
action = function(w, t) t.client.maximized_horizontal = false
|
|
|
|
t.client.maximized_vertical = false end },
|
2009-04-12 15:05:33 +02:00
|
|
|
{ idx = "f/a", img = "focus_active",
|
2009-04-29 15:05:23 +02:00
|
|
|
action = function(w, t) t.client.maximized_horizontal = false
|
|
|
|
t.client.maximized_vertical = false end })
|
2009-07-02 01:41:26 +02:00
|
|
|
|
|
|
|
local close_buttons = button_group("close",
|
|
|
|
{ align = "left" },
|
|
|
|
select_state_focus,
|
|
|
|
{ idx = "n", img = "normal",
|
|
|
|
action = function (w, t) t.client:kill() end },
|
|
|
|
{ idx = "f", img = "focus",
|
|
|
|
action = function (w, t) t.client:kill() end })
|
|
|
|
|
2009-04-29 15:05:23 +02:00
|
|
|
local function floating_update(w, t)
|
2009-04-12 15:00:11 +02:00
|
|
|
client.floating.toggle(t.client)
|
|
|
|
end
|
2009-04-12 13:54:29 +02:00
|
|
|
|
2009-04-12 15:00:11 +02:00
|
|
|
local floating_buttons = button_group("floating",
|
|
|
|
{ align = "right"},
|
|
|
|
select_state_floating,
|
|
|
|
{ idx = "n/i", img = "normal_inactive", action = floating_update },
|
|
|
|
{ idx = "f/i", img = "focus_inactive", action = floating_update },
|
|
|
|
{ idx = "n/a", img = "normal_active", action = floating_update },
|
|
|
|
{ idx = "f/a", img = "focus_active", action = floating_update })
|
|
|
|
|
2009-07-02 01:41:26 +02:00
|
|
|
button_groups = { close_buttons,
|
|
|
|
ontop_buttons,
|
2009-04-12 15:00:11 +02:00
|
|
|
sticky_buttons,
|
|
|
|
maximized_buttons,
|
2009-07-02 01:41:26 +02:00
|
|
|
floating_buttons }
|
2009-04-12 13:54:29 +02:00
|
|
|
|
2008-09-29 16:49:18 +02:00
|
|
|
-- Register standards hooks
|
2009-07-10 12:19:08 +02:00
|
|
|
capi.client.add_signal("focus", update)
|
2008-09-29 16:49:18 +02:00
|
|
|
hooks.unfocus.register(update)
|
|
|
|
hooks.property.register(update)
|
|
|
|
|
|
|
|
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80
|