From 145e19b2e6e7747827dd35940a7255a03fd8a833 Mon Sep 17 00:00:00 2001 From: Julien Danjou Date: Tue, 19 Aug 2008 18:20:39 +0200 Subject: [PATCH] statusbar: widget is now a function Signed-off-by: Julien Danjou --- awesomerc.lua.in | 5 ++-- lib/awful.lua.in | 5 ++-- statusbar.c | 88 ++++++++++++++++++++++++++++++++++---------------------- 3 files changed, 57 insertions(+), 41 deletions(-) diff --git a/awesomerc.lua.in b/awesomerc.lua.in index b66ba5ca..79742080 100644 --- a/awesomerc.lua.in +++ b/awesomerc.lua.in @@ -138,15 +138,14 @@ for s = 1, screen.count() do mystatusbar[s] = statusbar({ position = "top", name = "mystatusbar" .. s, fg = beautiful.fg_normal, bg = beautiful.bg_normal }) -- Add widgets to the statusbar - order matters - mystatusbar[s].widgets = - { + mystatusbar[s]:widgets({ mytaglist, mytasklist, mypromptbox, mytextbox, mylayoutbox[s], s == screen.count() and mysystray or nil - } + }) mystatusbar[s].screen = s end -- }}} diff --git a/lib/awful.lua.in b/lib/awful.lua.in index 571f1033..748f26b7 100644 --- a/lib/awful.lua.in +++ b/lib/awful.lua.in @@ -1404,12 +1404,11 @@ function titlebar.add(c, args) local close_button= capi.widget({ type = "textbox", name = "close", align = "right" }) close_button:mouse_add(capi.mouse({ }, 1, function (t) t.client:kill() end)) - tb.widgets = - { + tb:widgets({ capi.widget({ type = "appicon", name = "appicon", align = "left" }), title, close_button - } + }) titlebar.update(c) diff --git a/statusbar.c b/statusbar.c index aeadfeee..bb753575 100644 --- a/statusbar.c +++ b/statusbar.c @@ -493,14 +493,11 @@ luaA_statusbar_new(lua_State *L) * \lfield fg Foreground color. * \lfield bg Background color. * \lfield position The position. - * \lfield widget The statusbar widgets. */ static int luaA_statusbar_index(lua_State *L) { size_t len; - int i = 0; - widget_node_t *witer; statusbar_t **statusbar = luaA_checkudata(L, 1, "statusbar"); const char *attr = luaL_checklstring(L, 2, &len); @@ -526,16 +523,64 @@ luaA_statusbar_index(lua_State *L) case A_TK_POSITION: lua_pushstring(L, position_tostr((*statusbar)->position)); break; - case A_TK_WIDGETS: + default: + return 0; + } + + return 1; +} + +/** Get or set the statusbar widgets. + * \param L The Lua VM state. + * \return The number of elements pushed on stack. + * \luastack + * \param None, or a table of widgets to set. + * \return The current statusbar widgets. +*/ +static int +luaA_statusbar_widgets(lua_State *L) +{ + statusbar_t **statusbar = luaA_checkudata(L, 1, "statusbar"); + widget_node_t *witer; + + if(lua_gettop(L) == 2) + { + luaA_checktable(L, 2); + + /* remove all widgets */ + for(witer = (*statusbar)->widgets; witer; witer = (*statusbar)->widgets) + { + if(witer->widget->detach) + witer->widget->detach(witer->widget, *statusbar); + widget_unref(&witer->widget); + widget_node_list_detach(&(*statusbar)->widgets, witer); + p_delete(&witer); + } + + (*statusbar)->need_update = true; + + /* now read all widgets and add them */ + lua_pushnil(L); + while(lua_next(L, 2)) + { + 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(&(*statusbar)->widgets, w); + widget_ref(widget); + lua_pop(L, 1); + } + lua_pop(L, 1); + } + else + { + int i = 0; lua_newtable(L); for(witer = (*statusbar)->widgets; witer; witer = witer->next) { luaA_widget_userdata_new(L, witer->widget); lua_rawseti(L, -2, ++i); } - break; - default: - return 0; } return 1; @@ -579,7 +624,6 @@ luaA_statusbar_newindex(lua_State *L) const char *buf, *attr = luaL_checklstring(L, 2, &len); position_t p; int screen; - widget_node_t *witer; switch(a_tokenize(attr, len)) { @@ -662,33 +706,6 @@ luaA_statusbar_newindex(lua_State *L) } } break; - case A_TK_WIDGETS: - luaA_checktable(L, 3); - - /* remove all widgets */ - for(witer = (*statusbar)->widgets; witer; witer = (*statusbar)->widgets) - { - if(witer->widget->detach) - witer->widget->detach(witer->widget, *statusbar); - widget_unref(&witer->widget); - widget_node_list_detach(&(*statusbar)->widgets, witer); - p_delete(&witer); - } - - (*statusbar)->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(&(*statusbar)->widgets, w); - widget_ref(widget); - lua_pop(L, 1); - } - break; default: return 0; } @@ -703,6 +720,7 @@ const struct luaL_reg awesome_statusbar_methods[] = }; const struct luaL_reg awesome_statusbar_meta[] = { + { "widgets", luaA_statusbar_widgets }, { "__index", luaA_statusbar_index }, { "__newindex", luaA_statusbar_newindex }, { "__gc", luaA_statusbar_gc }, -- 2.11.4.GIT