titlebar: widgets are now returned/set as array
Signed-off-by: Julien Danjou <julien@danjou.info>
This commit is contained in:
parent
874e62d849
commit
56b42a7878
|
@ -55,7 +55,7 @@ apptags =
|
||||||
}
|
}
|
||||||
|
|
||||||
-- Define if we want to use titlebar on all applications.
|
-- Define if we want to use titlebar on all applications.
|
||||||
use_titlebar = false
|
use_titlebar = true
|
||||||
-- }}}
|
-- }}}
|
||||||
|
|
||||||
-- {{{ Initialization
|
-- {{{ Initialization
|
||||||
|
|
|
@ -1167,16 +1167,19 @@ function titlebar.add(c, args)
|
||||||
if args.bg then targs.bg = args.bg end
|
if args.bg then targs.bg = args.bg end
|
||||||
local tb = capi.titlebar(targs)
|
local tb = capi.titlebar(targs)
|
||||||
|
|
||||||
tb:widget_add(capi.widget({ type = "appicon", name = "appicon", align = "left" }))
|
|
||||||
|
|
||||||
local title = capi.widget({ type = "textbox", name = "title", align = "flex" })
|
local title = capi.widget({ type = "textbox", name = "title", align = "flex" })
|
||||||
title:mouse_add(capi.mouse({ }, 1, function (t) t.client:mouse_move() end))
|
title:mouse_add(capi.mouse({ }, 1, function (t) t.client:mouse_move() end))
|
||||||
title:mouse_add(capi.mouse({ args.modkey }, 3, function (t) t.client:mouse_resize() end))
|
title:mouse_add(capi.mouse({ args.modkey }, 3, function (t) t.client:mouse_resize() end))
|
||||||
tb:widget_add(title)
|
|
||||||
|
|
||||||
local close_button= capi.widget({ type = "textbox", name = "close", align = "right" })
|
local close_button= capi.widget({ type = "textbox", name = "close", align = "right" })
|
||||||
close_button:mouse_add(capi.mouse({ }, 1, function (t) t.client:kill() end))
|
close_button:mouse_add(capi.mouse({ }, 1, function (t) t.client:kill() end))
|
||||||
tb:widget_add(close_button)
|
|
||||||
|
tb.widgets =
|
||||||
|
{
|
||||||
|
capi.widget({ type = "appicon", name = "appicon", align = "left" }),
|
||||||
|
title,
|
||||||
|
close_button
|
||||||
|
}
|
||||||
|
|
||||||
titlebar.update(c)
|
titlebar.update(c)
|
||||||
|
|
||||||
|
@ -1187,21 +1190,27 @@ end
|
||||||
-- @param c The client to update.
|
-- @param c The client to update.
|
||||||
function titlebar.update(c)
|
function titlebar.update(c)
|
||||||
if c.titlebar and titlebar.data[c] then
|
if c.titlebar and titlebar.data[c] then
|
||||||
local widgets = c.titlebar:widget_get()
|
local widgets = c.titlebar.widgets
|
||||||
if widgets.title then
|
local title, close
|
||||||
widgets.title.text = " " .. escape(c.name)
|
for k, v in ipairs(widgets) do
|
||||||
|
if v.name == "title" then title = v end
|
||||||
|
if v.name == "close" then close = v end
|
||||||
|
if title and close then break end
|
||||||
|
end
|
||||||
|
if title then
|
||||||
|
title.text = " " .. escape(c.name)
|
||||||
end
|
end
|
||||||
if capi.client.focus_get() == c then
|
if capi.client.focus_get() == c then
|
||||||
c.titlebar.fg = titlebar.data[c].fg_focus
|
c.titlebar.fg = titlebar.data[c].fg_focus
|
||||||
c.titlebar.bg = titlebar.data[c].bg_focus
|
c.titlebar.bg = titlebar.data[c].bg_focus
|
||||||
if widgets.close then
|
if close then
|
||||||
widgets.close.text = "<bg image=\"@AWESOME_ICON_PATH@/titlebar/closer.png\" resize=\"true\"/>"
|
close.text = "<bg image=\"@AWESOME_ICON_PATH@/titlebar/closer.png\" resize=\"true\"/>"
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
c.titlebar.fg = titlebar.data[c].fg
|
c.titlebar.fg = titlebar.data[c].fg
|
||||||
c.titlebar.bg = titlebar.data[c].bg
|
c.titlebar.bg = titlebar.data[c].bg
|
||||||
if widgets.close then
|
if close then
|
||||||
widgets.close.text = "<bg image=\"@AWESOME_ICON_PATH@/titlebar/close.png\" resize=\"true\"/>"
|
close.text = "<bg image=\"@AWESOME_ICON_PATH@/titlebar/close.png\" resize=\"true\"/>"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -674,7 +674,6 @@ luaA_statusbar_newindex(lua_State *L)
|
||||||
widget_ref(widget);
|
widget_ref(widget);
|
||||||
lua_pop(L, 1);
|
lua_pop(L, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
return 0;
|
return 0;
|
||||||
|
|
135
titlebar.c
135
titlebar.c
|
@ -349,100 +349,6 @@ luaA_titlebar_new(lua_State *L)
|
||||||
return luaA_titlebar_userdata_new(globalconf.L, tb);
|
return luaA_titlebar_userdata_new(globalconf.L, tb);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Add a widget to a titlebar.
|
|
||||||
* \param L The Lua VM state.
|
|
||||||
* \return The number of value pushed.
|
|
||||||
*
|
|
||||||
* \luastack
|
|
||||||
* \lvalue A titlebar.
|
|
||||||
* \lparam A widget.
|
|
||||||
*/
|
|
||||||
static int
|
|
||||||
luaA_titlebar_widget_add(lua_State *L)
|
|
||||||
{
|
|
||||||
titlebar_t **tb = luaA_checkudata(L, 1, "titlebar");
|
|
||||||
widget_t **widget = luaA_checkudata(L, 2, "widget");
|
|
||||||
widget_node_t *witer, *w;
|
|
||||||
|
|
||||||
if((*widget)->type == systray_new)
|
|
||||||
luaL_error(L, "cannot add systray widget to titlebar");
|
|
||||||
|
|
||||||
/* check that there is not already a widget with that name in the titlebar */
|
|
||||||
for(witer = (*tb)->widgets; witer; witer = witer->next)
|
|
||||||
if(witer->widget != *widget
|
|
||||||
&& !a_strcmp(witer->widget->name, (*widget)->name))
|
|
||||||
luaL_error(L, "a widget with name `%s' already on titlebar", witer->widget->name);
|
|
||||||
|
|
||||||
w = p_new(widget_node_t, 1);
|
|
||||||
|
|
||||||
w->widget = *widget;
|
|
||||||
widget_node_list_append(&(*tb)->widgets, w);
|
|
||||||
widget_ref(widget);
|
|
||||||
|
|
||||||
(*tb)->need_update = true;
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Remove a widget from a titlebar.
|
|
||||||
* \param L The Lua VM State.
|
|
||||||
*
|
|
||||||
* \luastack
|
|
||||||
* \lvalue A statusbar.
|
|
||||||
* \lparam A widget.
|
|
||||||
*/
|
|
||||||
static int
|
|
||||||
luaA_titlebar_widget_remove(lua_State *L)
|
|
||||||
{
|
|
||||||
titlebar_t **tb = luaA_checkudata(L, 1, "titlebar");
|
|
||||||
widget_t **widget = luaA_checkudata(L, 2, "widget");
|
|
||||||
widget_node_t *w, *wnext;
|
|
||||||
|
|
||||||
for(w = (*tb)->widgets; w; w = wnext)
|
|
||||||
{
|
|
||||||
wnext = w->next;
|
|
||||||
if(w->widget == *widget)
|
|
||||||
{
|
|
||||||
if((*widget)->detach)
|
|
||||||
(*widget)->detach(*widget, *tb);
|
|
||||||
widget_unref(widget);
|
|
||||||
widget_node_list_detach(&(*tb)->widgets, w);
|
|
||||||
p_delete(&w);
|
|
||||||
(*tb)->need_update = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/** Get all widgets from a titlebar.
|
|
||||||
* \param L The Lua VM state.
|
|
||||||
* \return The number of value pushed.
|
|
||||||
*
|
|
||||||
* \luastack
|
|
||||||
* \lvalue A titlebar
|
|
||||||
* \lreturn A table with all widgets from the titlebar.
|
|
||||||
*/
|
|
||||||
static int
|
|
||||||
luaA_titlebar_widget_get(lua_State *L)
|
|
||||||
{
|
|
||||||
titlebar_t **tb = luaA_checkudata(L, 1, "titlebar");
|
|
||||||
widget_node_t *witer;
|
|
||||||
|
|
||||||
lua_newtable(L);
|
|
||||||
|
|
||||||
for(witer = (*tb)->widgets; witer; witer = witer->next)
|
|
||||||
{
|
|
||||||
luaA_widget_userdata_new(L, witer->widget);
|
|
||||||
/* ref again for the list */
|
|
||||||
widget_ref(&witer->widget);
|
|
||||||
lua_setfield(L, -2, witer->widget->name);
|
|
||||||
}
|
|
||||||
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Titlebar newindex.
|
/** Titlebar newindex.
|
||||||
* \param L The Lua VM state.
|
* \param L The Lua VM state.
|
||||||
* \return The number of elements pushed on stack.
|
* \return The number of elements pushed on stack.
|
||||||
|
@ -455,6 +361,7 @@ luaA_titlebar_newindex(lua_State *L)
|
||||||
const char *buf, *attr = luaL_checklstring(L, 2, &len);
|
const char *buf, *attr = luaL_checklstring(L, 2, &len);
|
||||||
client_t *c = NULL, **newc;
|
client_t *c = NULL, **newc;
|
||||||
int i;
|
int i;
|
||||||
|
widget_node_t *witer;
|
||||||
|
|
||||||
switch(a_tokenize(attr, len))
|
switch(a_tokenize(attr, len))
|
||||||
{
|
{
|
||||||
|
@ -521,6 +428,33 @@ luaA_titlebar_newindex(lua_State *L)
|
||||||
globalconf.default_screen, buf, len))
|
globalconf.default_screen, buf, len))
|
||||||
(*titlebar)->need_update = true;
|
(*titlebar)->need_update = true;
|
||||||
return 0;
|
return 0;
|
||||||
|
case A_TK_WIDGETS:
|
||||||
|
luaA_checktable(L, 3);
|
||||||
|
|
||||||
|
/* remove all widgets */
|
||||||
|
for(witer = (*titlebar)->widgets; witer; witer = (*titlebar)->widgets)
|
||||||
|
{
|
||||||
|
if(witer->widget->detach)
|
||||||
|
witer->widget->detach(witer->widget, *titlebar);
|
||||||
|
widget_unref(&witer->widget);
|
||||||
|
widget_node_list_detach(&(*titlebar)->widgets, witer);
|
||||||
|
p_delete(&witer);
|
||||||
|
}
|
||||||
|
|
||||||
|
(*titlebar)->need_update = true;
|
||||||
|
|
||||||
|
/* now read all widgets and add them */
|
||||||
|
lua_pushnil(L);
|
||||||
|
while(lua_next(L, 3))
|
||||||
|
{
|
||||||
|
widget_t **widget = luaA_checkudata(L, -1, "widget");
|
||||||
|
widget_node_t *w = p_new(widget_node_t, 1);
|
||||||
|
w->widget = *widget;
|
||||||
|
widget_node_list_append(&(*titlebar)->widgets, w);
|
||||||
|
widget_ref(widget);
|
||||||
|
lua_pop(L, 1);
|
||||||
|
}
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -550,6 +484,8 @@ luaA_titlebar_index(lua_State *L)
|
||||||
titlebar_t **titlebar = luaA_checkudata(L, 1, "titlebar");
|
titlebar_t **titlebar = luaA_checkudata(L, 1, "titlebar");
|
||||||
const char *attr = luaL_checklstring(L, 2, &len);
|
const char *attr = luaL_checklstring(L, 2, &len);
|
||||||
client_t *c;
|
client_t *c;
|
||||||
|
widget_node_t *witer;
|
||||||
|
int i = 0;
|
||||||
|
|
||||||
if(luaA_usemetatable(L, 1, 2))
|
if(luaA_usemetatable(L, 1, 2))
|
||||||
return 1;
|
return 1;
|
||||||
|
@ -576,6 +512,14 @@ luaA_titlebar_index(lua_State *L)
|
||||||
case A_TK_BG:
|
case A_TK_BG:
|
||||||
luaA_pushcolor(L, &(*titlebar)->colors.bg);
|
luaA_pushcolor(L, &(*titlebar)->colors.bg);
|
||||||
break;
|
break;
|
||||||
|
case A_TK_WIDGETS:
|
||||||
|
lua_newtable(L);
|
||||||
|
for(witer = (*titlebar)->widgets; witer; witer = witer->next)
|
||||||
|
{
|
||||||
|
luaA_widget_userdata_new(L, witer->widget);
|
||||||
|
lua_rawseti(L, -2, ++i);
|
||||||
|
}
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -606,9 +550,6 @@ const struct luaL_reg awesome_titlebar_methods[] =
|
||||||
};
|
};
|
||||||
const struct luaL_reg awesome_titlebar_meta[] =
|
const struct luaL_reg awesome_titlebar_meta[] =
|
||||||
{
|
{
|
||||||
{ "widget_add", luaA_titlebar_widget_add },
|
|
||||||
{ "widget_remove", luaA_titlebar_widget_remove },
|
|
||||||
{ "widget_get", luaA_titlebar_widget_get },
|
|
||||||
{ "__index", luaA_titlebar_index },
|
{ "__index", luaA_titlebar_index },
|
||||||
{ "__newindex", luaA_titlebar_newindex },
|
{ "__newindex", luaA_titlebar_newindex },
|
||||||
{ "__eq", luaA_titlebar_eq },
|
{ "__eq", luaA_titlebar_eq },
|
||||||
|
|
Loading…
Reference in New Issue