widget: store supported align and use to render flex widgets
Signed-off-by: Julien Danjou <julien@danjou.info>
This commit is contained in:
parent
93be5b3cae
commit
2e3744de9d
8
draw.h
8
draw.h
|
@ -45,10 +45,10 @@ typedef struct
|
||||||
|
|
||||||
typedef enum
|
typedef enum
|
||||||
{
|
{
|
||||||
AlignLeft = 0,
|
AlignLeft = (0),
|
||||||
AlignRight,
|
AlignRight = (1),
|
||||||
AlignCenter,
|
AlignCenter = (1 << 1),
|
||||||
AlignFlex,
|
AlignFlex = (1 << 2),
|
||||||
} alignment_t;
|
} alignment_t;
|
||||||
|
|
||||||
typedef struct vector_t vector_t;
|
typedef struct vector_t vector_t;
|
||||||
|
|
|
@ -143,6 +143,8 @@ struct widget_t
|
||||||
luaA_ref mouse_enter, mouse_leave;
|
luaA_ref mouse_enter, mouse_leave;
|
||||||
/** Alignement */
|
/** Alignement */
|
||||||
alignment_t align;
|
alignment_t align;
|
||||||
|
/** Supported alignment */
|
||||||
|
alignment_t align_supported;
|
||||||
/** Misc private data */
|
/** Misc private data */
|
||||||
void *data;
|
void *data;
|
||||||
/** Button bindings */
|
/** Button bindings */
|
||||||
|
|
48
widget.c
48
widget.c
|
@ -201,25 +201,47 @@ widget_render(widget_node_array_t *widgets, draw_context_t *ctx, xcb_gcontext_t
|
||||||
widgets->tab[i].geometry.x = ctx->width - right;
|
widgets->tab[i].geometry.x = ctx->width - right;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* save left value */
|
||||||
|
int fake_left = left;
|
||||||
|
|
||||||
|
/* compute width of flex aligned widgets that does not support it */
|
||||||
int flex = 0;
|
int flex = 0;
|
||||||
for(int i = 0; i < widgets->len; i++)
|
for(int i = 0; i < widgets->len; i++)
|
||||||
if(widgets->tab[i].widget->align == AlignFlex && widgets->tab[i].widget->isvisible)
|
if(widgets->tab[i].widget->align == AlignFlex
|
||||||
flex++;
|
&& widgets->tab[i].widget->isvisible)
|
||||||
|
{
|
||||||
|
if(widgets->tab[i].widget->align_supported & AlignFlex)
|
||||||
|
flex++;
|
||||||
|
else
|
||||||
|
fake_left += widgets->tab[i].widget->geometry(widgets->tab[i].widget,
|
||||||
|
screen, ctx->height,
|
||||||
|
ctx->width - (fake_left + right)).width;
|
||||||
|
}
|
||||||
|
|
||||||
if(flex)
|
/* now compute everybody together! */
|
||||||
{
|
int flex_rendered = 0;
|
||||||
int length = (ctx->width - (left + right)) / flex;
|
for(int i = 0; i < widgets->len; i++)
|
||||||
|
if(widgets->tab[i].widget->align == AlignFlex
|
||||||
for(int i = 0; i < widgets->len; i++)
|
&& widgets->tab[i].widget->isvisible)
|
||||||
if(widgets->tab[i].widget->align == AlignFlex && widgets->tab[i].widget->isvisible)
|
{
|
||||||
|
if(widgets->tab[i].widget->align_supported & AlignFlex)
|
||||||
{
|
{
|
||||||
|
int width = (ctx->width - (right + fake_left)) / flex;
|
||||||
|
/* give last pixels to last flex to be rendered */
|
||||||
|
if(flex_rendered == flex - 1)
|
||||||
|
width += (ctx->width - (right + fake_left)) % flex;
|
||||||
widgets->tab[i].geometry = widgets->tab[i].widget->geometry(widgets->tab[i].widget,
|
widgets->tab[i].geometry = widgets->tab[i].widget->geometry(widgets->tab[i].widget,
|
||||||
screen, ctx->height,
|
screen, ctx->height,
|
||||||
length);
|
width);
|
||||||
widgets->tab[i].geometry.x = left;
|
flex_rendered++;
|
||||||
left += widgets->tab[i].geometry.width;
|
|
||||||
}
|
}
|
||||||
}
|
else
|
||||||
|
widgets->tab[i].geometry = widgets->tab[i].widget->geometry(widgets->tab[i].widget,
|
||||||
|
screen, ctx->height,
|
||||||
|
ctx->width - (left + right));
|
||||||
|
widgets->tab[i].geometry.x = left;
|
||||||
|
left += widgets->tab[i].geometry.width;
|
||||||
|
}
|
||||||
|
|
||||||
/* draw everything! */
|
/* draw everything! */
|
||||||
draw_rectangle(ctx, rectangle, 1.0, true, &ctx->bg);
|
draw_rectangle(ctx, rectangle, 1.0, true, &ctx->bg);
|
||||||
|
@ -259,8 +281,8 @@ widget_render(widget_node_array_t *widgets, draw_context_t *ctx, xcb_gcontext_t
|
||||||
void
|
void
|
||||||
widget_common_new(widget_t *widget)
|
widget_common_new(widget_t *widget)
|
||||||
{
|
{
|
||||||
widget->align = AlignLeft;
|
|
||||||
widget->button = widget_common_button;
|
widget->button = widget_common_button;
|
||||||
|
widget->align_supported = AlignLeft | AlignRight;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Invalidate widgets which should be refresh upon
|
/** Invalidate widgets which should be refresh upon
|
||||||
|
|
|
@ -178,6 +178,7 @@ textbox_new(alignment_t align)
|
||||||
w = p_new(widget_t, 1);
|
w = p_new(widget_t, 1);
|
||||||
widget_common_new(w);
|
widget_common_new(w);
|
||||||
w->align = align;
|
w->align = align;
|
||||||
|
w->align_supported |= AlignFlex;
|
||||||
w->draw = textbox_draw;
|
w->draw = textbox_draw;
|
||||||
w->index = luaA_textbox_index;
|
w->index = luaA_textbox_index;
|
||||||
w->newindex = luaA_textbox_newindex;
|
w->newindex = luaA_textbox_newindex;
|
||||||
|
|
Loading…
Reference in New Issue