Fix spacing in listview and box.

This commit is contained in:
Dave Davenport 2017-01-03 14:25:24 +01:00
parent fe0219a025
commit e982a439fb
4 changed files with 53 additions and 55 deletions

View file

@ -131,17 +131,18 @@ Each property is closed by a semi-colon `;`;
The following properties are currently supports:
* all widgets:
* spacing: distance
* padding: distance
* padding: distance distance
* padding: distance distance distance
* padding: distance distance distance distance
* window:
* font: string
* background: color
* foreground: color
* border-width: integer
* spacing: integer
* padding: integer
* padding-left: integer
* padding-right: integer
* padding-top: integer
* padding-bottom: integer
* transparency: string
- real
- background
@ -156,22 +157,15 @@ The following properties are currently supports:
* foreground: color
* box
* spacing: integer
* padding: integer
* padding-left: integer
* padding-right: integer
* padding-top: integer
* padding-bottom: integer
* textbox:
* background: color
* foreground: color
* listview:
* spacing: integer
* lines: integer
* columns: integer
* fixed-height: boolean
* dynamic: boolean
* scrollbar: boolean
* scrollbar-width: integer
* cycle: boolean

View file

@ -421,6 +421,14 @@ void rofi_theme_convert_old_theme ( void )
p->name = g_strdup("spacing");
p->value.i = config.padding;
g_hash_table_replace ( listview_widget->properties, p->name, p );
p = rofi_theme_property_create ( P_INTEGER );
p->name = g_strdup("columns");
p->value.i = config.padding;
g_hash_table_replace ( listview_widget->properties, p->name, p );
p = rofi_theme_property_create ( P_INTEGER );
p->name = g_strdup("fixed-height");
p->value.i = !(config.fixed_num_lines);
g_hash_table_replace ( listview_widget->properties, p->name, p );
}
{
// Border width.

View file

@ -39,21 +39,13 @@ const char *BOX_CLASS_NAME = "@box";
/** Default spacing used in the box*/
#define DEFAULT_SPACING 2
/**
* @param box Handle to the box widget.
* @param spacing The spacing to apply.
*
* Set the spacing to apply between the children in pixels.
*/
void box_set_spacing ( box * box, unsigned int spacing );
struct _box
{
widget widget;
boxType type;
int max_size;
// Padding between elements
int spacing;
Distance spacing;
GList *children;
};
@ -63,6 +55,7 @@ static void box_update ( widget *wid );
static int box_get_desired_height ( widget *wid )
{
box *b = (box *)wid;
int spacing = distance_get_pixel ( b->spacing );
int active_widgets = 0;
int height = 0;
if ( b->type == BOX_VERTICAL ){
@ -79,7 +72,7 @@ static int box_get_desired_height ( widget *wid )
height += widget_get_desired_height ( child );
}
if ( active_widgets > 0 ){
height += (active_widgets - 1)*b->spacing;
height += (active_widgets - 1)*spacing;
}
} else {
for ( GList *iter = g_list_first ( b->children ); iter != NULL; iter = g_list_next ( iter ) ) {
@ -97,6 +90,7 @@ static int box_get_desired_height ( widget *wid )
static void vert_calculate_size ( box *b )
{
int spacing = distance_get_pixel ( b->spacing );
int expanding_widgets = 0;
int active_widgets = 0;
int rem_width = widget_padding_get_remaining_width ( WIDGET (b) );
@ -123,7 +117,7 @@ static void vert_calculate_size ( box *b )
}
}
if ( active_widgets > 0 ){
b->max_size += ( active_widgets - 1 ) * b->spacing;
b->max_size += ( active_widgets - 1 ) * spacing;
}
if ( b->max_size > rem_height ) {
b->max_size = rem_height;
@ -147,13 +141,13 @@ static void vert_calculate_size ( box *b )
bottom -= expanding_widgets_size;
widget_move ( child, widget_padding_get_left ( WIDGET ( b ) ), bottom );
widget_resize ( child, rem_width, expanding_widgets_size );
bottom -= b->spacing;
bottom -= spacing;
}
else {
widget_move ( child, widget_padding_get_left ( WIDGET ( b ) ), top );
top += expanding_widgets_size;
widget_resize ( child, rem_width, expanding_widgets_size );
top += b->spacing;
top += spacing;
}
rem -= expanding_widgets_size;
index++;
@ -162,12 +156,12 @@ static void vert_calculate_size ( box *b )
else if ( child->end ) {
bottom -= widget_get_height ( child );
widget_move ( child, widget_padding_get_left ( WIDGET ( b ) ), bottom );
bottom -= b->spacing;
bottom -= spacing;
}
else {
widget_move ( child, widget_padding_get_left ( WIDGET ( b ) ), top );
top += widget_get_height ( child );
top += b->spacing;
top += spacing;
}
}
}
@ -175,6 +169,7 @@ static void vert_calculate_size ( box *b )
}
static void hori_calculate_size ( box *b )
{
int spacing = distance_get_pixel ( b->spacing );
int expanding_widgets = 0;
int active_widgets = 0;
int rem_width = widget_padding_get_remaining_width ( WIDGET (b) );
@ -201,7 +196,7 @@ static void hori_calculate_size ( box *b )
b->max_size += child->w;
}
}
b->max_size += MAX ( 0, ( ( active_widgets - 1 ) * b->spacing ) );
b->max_size += MAX ( 0, ( ( active_widgets - 1 ) * spacing ) );
if ( b->max_size > (rem_width)) {
b->max_size = rem_width;
g_log ( LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Widgets to large (width) for box: %d %d", b->max_size, b->widget.w );
@ -224,13 +219,13 @@ static void hori_calculate_size ( box *b )
right -= expanding_widgets_size;
widget_move ( child, right, widget_padding_get_top ( WIDGET ( b ) ));
widget_resize ( child, expanding_widgets_size, rem_height );
right -= b->spacing;
right -= spacing;
}
else {
widget_move ( child, left, widget_padding_get_top ( WIDGET ( b ) ) );
left += expanding_widgets_size;
widget_resize ( child, expanding_widgets_size, rem_height );
left += b->spacing;
left += spacing;
}
rem -= expanding_widgets_size;
index++;
@ -239,12 +234,12 @@ static void hori_calculate_size ( box *b )
else if ( child->end ) {
right -= widget_get_width ( child );
widget_move ( child, right, widget_padding_get_top ( WIDGET ( b ) ) );
right -= b->spacing;
right -= spacing;
}
else {
widget_move ( child, left, widget_padding_get_top ( WIDGET ( b ) ) );
left += widget_get_width ( child );
left += b->spacing;
left += spacing;
}
}
}
@ -354,7 +349,7 @@ box * box_create ( const char *name, boxType type )
b->widget.get_desired_height = box_get_desired_height;
b->widget.enabled = TRUE;
box_set_spacing ( b, distance_get_pixel (rofi_theme_get_distance ( b->widget.class_name, b->widget.name, NULL, "spacing",DEFAULT_SPACING )));
b->spacing = rofi_theme_get_distance ( b->widget.class_name, b->widget.name, NULL, "spacing",DEFAULT_SPACING );
return b;
}
@ -374,16 +369,7 @@ static void box_update ( widget *wid )
int box_get_fixed_pixels ( box *box )
{
if ( box != NULL ) {
printf("max size: %d\n", box->max_size);
return box->max_size;
}
return 0;
}
void box_set_spacing ( box * box, unsigned int spacing )
{
if ( box != NULL ) {
box->spacing = spacing;
widget_queue_redraw ( WIDGET ( box ) );
}
}

View file

@ -56,11 +56,12 @@ struct _listview
unsigned int req_elements;
unsigned int cur_elements;
unsigned int spacing;
Distance spacing;
unsigned int menu_lines;
unsigned int max_displayed_lines;
unsigned int menu_columns;
unsigned int fixed_num_lines;
unsigned int dynamic;
unsigned int eh;
gboolean cycle;
gboolean multi_select;
@ -163,20 +164,21 @@ static void listview_draw ( widget *wid, cairo_t *draw )
scrollbar_set_handle_length ( lv->scrollbar, lv->cur_columns * lv->max_rows );
scrollbar_set_handle ( lv->scrollbar, lv->selected );
lv->last_offset = offset;
int spacing = distance_get_pixel ( lv->spacing );
if ( lv->cur_elements > 0 && lv->max_rows > 0 ) {
// Set new x/y possition.
unsigned int max = MIN ( lv->cur_elements, lv->req_elements - offset );
if ( lv->rchanged ) {
unsigned int width = lv->widget.w - lv->spacing * ( lv->cur_columns - 1 );
unsigned int width = lv->widget.w - spacing * ( lv->cur_columns - 1 );
width -= widget_padding_get_padding_width ( wid );
if ( widget_enabled ( WIDGET ( lv->scrollbar ) ) ) {
width -= lv->spacing;
width -= spacing;
width -= widget_get_width ( WIDGET ( lv->scrollbar ) );
}
unsigned int element_width = ( width ) / lv->cur_columns;
for ( unsigned int i = 0; i < max; i++ ) {
unsigned int ex = widget_padding_get_left ( wid ) + ( ( i ) / lv->max_rows ) * ( element_width + lv->spacing );
unsigned int ey = widget_padding_get_top ( wid ) + ( ( i ) % lv->max_rows ) * ( lv->element_height + lv->spacing );
unsigned int ex = widget_padding_get_left ( wid ) + ( ( i ) / lv->max_rows ) * ( element_width + spacing );
unsigned int ey = widget_padding_get_top ( wid ) + ( ( i ) % lv->max_rows ) * ( lv->element_height + spacing );
textbox_moveresize ( lv->boxes[i], ex, ey, element_width, lv->element_height );
update_element ( lv, i, i + offset, TRUE );
@ -254,7 +256,8 @@ static void listview_resize ( widget *wid, short w, short h )
lv->widget.w = MAX ( 0, w );
lv->widget.h = MAX ( 0, h );
int height = lv->widget.h - widget_padding_get_padding_height ( WIDGET (lv) );
lv->max_rows = ( lv->spacing + height ) / ( lv->element_height + lv->spacing );
int spacing = distance_get_pixel ( lv->spacing );
lv->max_rows = ( spacing + height ) / ( lv->element_height + spacing );
lv->max_elements = lv->max_rows * lv->menu_columns;
widget_move ( WIDGET ( lv->scrollbar ),
@ -352,9 +355,11 @@ listview *listview_create ( const char *name, listview_update_callback cb, void
lv->udata = udata;
// Some settings.
lv->spacing = distance_get_pixel (rofi_theme_get_distance (lv->widget.class_name, lv->widget.name, NULL, "spacing", DEFAULT_SPACING ));
lv->menu_columns = rofi_theme_get_integer (lv->widget.class_name, lv->widget.name, NULL, "columns", config.menu_columns);
lv->fixed_num_lines = rofi_theme_get_boolean (lv->widget.class_name, lv->widget.name, NULL, "fixed-height", config.fixed_num_lines );
lv->spacing = rofi_theme_get_distance (lv->widget.class_name, lv->widget.name, NULL, "spacing", DEFAULT_SPACING );
lv->menu_columns = rofi_theme_get_integer (lv->widget.class_name, lv->widget.name, NULL, "columns", config.menu_columns );
lv->fixed_num_lines = rofi_theme_get_boolean (lv->widget.class_name, lv->widget.name, NULL, "fixed-height", config.fixed_num_lines );
lv->dynamic = rofi_theme_get_boolean (lv->widget.class_name, lv->widget.name, NULL, "dynamic", TRUE );
listview_set_show_scrollbar ( lv, rofi_theme_get_boolean ( lv->widget.class_name, lv->widget.name, NULL, "scrollbar", !config.hide_scrollbar ));
listview_set_scrollbar_width ( lv, rofi_theme_get_integer ( lv->widget.class_name, lv->widget.name, NULL, "scrollbar-width", config.scrollbar_width ));
lv->cycle = rofi_theme_get_boolean ( lv->widget.class_name, lv->widget.name, NULL, "cycle", config.cycle );
@ -459,18 +464,23 @@ void listview_nav_page_next ( listview *lv )
static int listview_get_desired_height ( widget *wid )
{
listview *lv = (listview *)wid;
int spacing = distance_get_pixel ( lv->spacing );
if ( lv == NULL || lv->widget.enabled == FALSE ) {
return 0;
}
int h = lv->menu_lines;
if ( !( lv->fixed_num_lines ) ) {
h = MIN ( lv->menu_lines, lv->req_elements );
if ( lv->dynamic ) {
h = MIN ( lv->menu_lines, lv->req_elements );
} else {
h = MIN ( lv->menu_lines, lv->max_displayed_lines );
}
}
if ( h == 0 ) {
return widget_padding_get_padding_height ( WIDGET (lv) );
}
int height = widget_padding_get_padding_height ( WIDGET (lv) );
height += h*(lv->element_height+lv->spacing) - lv->spacing;
height += h*(lv->element_height+spacing) - spacing;
return height;
}