UI: Separator spacer
authorDalai Felinto <dfelinto@gmail.com>
Mon, 11 Jun 2018 12:33:53 +0000 (14:33 +0200)
committerDalai Felinto <dfelinto@gmail.com>
Mon, 11 Jun 2018 12:46:35 +0000 (14:46 +0200)
This support layout.separator_spacer() to be used by headers as a way to
dynamically separate the ui buttons.

Right now no UI file is changed, though we can use this right away in the
timeline, and shortly after in the viewport header (moving settings from
the topbar to it).

Original design by William Reynish.

Review: Campbell Barton
D3468

release/scripts/startup/bl_ui/space_time.py
source/blender/editors/include/UI_interface.h
source/blender/editors/interface/interface.c
source/blender/editors/interface/interface_align.c
source/blender/editors/interface/interface_handlers.c
source/blender/editors/interface/interface_layout.c
source/blender/editors/interface/interface_widgets.c
source/blender/editors/screen/area.c
source/blender/makesrna/intern/rna_ui_api.c

index 7c584b3..16ad53c 100644 (file)
@@ -36,13 +36,7 @@ class TIME_HT_editor_buttons(Header):
         toolsettings = context.tool_settings
         screen = context.screen
 
-        layout.separator()   # XXX: This should be dynamic (e.g. layout.separator(stretch=1.0))
-        layout.separator()
-        layout.separator()
-        layout.separator()
-        layout.separator()
-        layout.separator()
-        layout.separator()
+        layout.separator_spacer()
 
         row = layout.row(align=True)
         row.prop(toolsettings, "use_keyframe_insert_auto", text="", toggle=True)
@@ -67,13 +61,7 @@ class TIME_HT_editor_buttons(Header):
         row.operator("screen.keyframe_jump", text="", icon='NEXT_KEYFRAME').next = True
         row.operator("screen.frame_jump", text="", icon='FF').end = True
 
-        layout.separator()  # XXX: This should be dynamic (e.g. layout.separator(stretch=1.0))
-        layout.separator()
-        layout.separator()
-        layout.separator()
-        layout.separator()
-        layout.separator()
-        layout.separator()
+        layout.separator_spacer()
 
         row = layout.row()
         row.scale_x = 0.95
index 42b78fa..c82a320 100644 (file)
@@ -305,7 +305,8 @@ typedef enum {
        UI_BTYPE_NODE_SOCKET            = (53 << 9),
        UI_BTYPE_SEPR                   = (54 << 9),
        UI_BTYPE_SEPR_LINE              = (55 << 9),
-       UI_BTYPE_GRIP                   = (56 << 9),  /* resize handle (resize uilist) */
+       UI_BTYPE_SEPR_SPACER            = (56 << 9),  /* Dynamically fill available space. */
+       UI_BTYPE_GRIP                   = (57 << 9),  /* resize handle (resize uilist) */
 } eButType;
 
 #define BUTTYPE     (63 << 9)
@@ -904,6 +905,7 @@ void UI_exit(void);
 #define UI_ITEM_O_DEPRESS       (1 << 9)
 #define UI_ITEM_R_COMPACT       (1 << 10)
 
+#define UI_HEADER_OFFSET_START ((void)0, 0.4f * UI_UNIT_X)
 
 /* uiLayoutOperatorButs flags */
 enum {
@@ -1126,6 +1128,7 @@ void uiItemLDrag(uiLayout *layout, struct PointerRNA *ptr, const char *name, int
 void uiItemM(uiLayout *layout, struct bContext *C, const char *menuname, const char *name, int icon); /* menu */
 void uiItemV(uiLayout *layout, const char *name, int icon, int argval); /* value */
 void uiItemS(uiLayout *layout); /* separator */
+void uiItemSpacer(uiLayout *layout); /* Special separator. */
 
 void uiItemPopoverPanel_ptr(
         uiLayout *layout, struct bContext *C,
index 28f4c40..811bd44 100644 (file)
@@ -86,6 +86,9 @@
 
 #include "interface_intern.h"
 
+/* prototypes. */
+static void ui_but_to_pixelrect(struct rcti *rect, const struct ARegion *ar, struct uiBlock *block, struct uiBut *but);
+
 /* avoid unneeded calls to ui_but_value_get */
 #define UI_BUT_VALUE_UNSET DBL_MAX
 #define UI_GET_BUT_VALUE_INIT(_but, _value) if (_value == DBL_MAX) {  (_value) = ui_but_value_get(_but); } (void)0
@@ -225,6 +228,39 @@ void ui_region_to_window(const ARegion *ar, int *x, int *y)
        *y += ar->winrct.ymin;
 }
 
+static void ui_update_flexible_spacing(const ARegion *region, uiBlock *block)
+{
+       int sepr_flex_len = 0;
+       for (uiBut *but = block->buttons.first; but; but = but->next) {
+               if (but->type == UI_BTYPE_SEPR_SPACER) {
+                       sepr_flex_len++;
+               }
+       }
+
+       if (sepr_flex_len == 0) {
+               return;
+       }
+
+       rcti rect;
+       ui_but_to_pixelrect(&rect, region, block, block->buttons.last);
+       const float buttons_width = (float)rect.xmax + UI_HEADER_OFFSET_START;
+       const float region_width = (float)region->sizex * U.dpi_fac;
+
+       if (region_width <= buttons_width) {
+               return;
+       }
+
+       const float spacing = ((region_width - buttons_width) / (float)sepr_flex_len);
+       float offset = 0;
+       for (uiBut *but = block->buttons.first; but; but = but->next) {
+               BLI_rctf_translate(&but->rect, offset, 0);
+               if (but->type == UI_BTYPE_SEPR_SPACER) {
+                       offset += spacing;
+               }
+       }
+       ui_block_bounds_calc(block);
+}
+
 static void ui_update_window_matrix(const wmWindow *window, const ARegion *region, uiBlock *block)
 {
        /* window matrix and aspect */
@@ -283,7 +319,7 @@ static void ui_block_bounds_calc_text(uiBlock *block, float offset)
        UI_fontstyle_set(&style->widget);
 
        for (init_col_bt = bt = block->buttons.first; bt; bt = bt->next) {
-               if (!ELEM(bt->type, UI_BTYPE_SEPR, UI_BTYPE_SEPR_LINE)) {
+               if (!ELEM(bt->type, UI_BTYPE_SEPR, UI_BTYPE_SEPR_LINE, UI_BTYPE_SEPR_SPACER)) {
                        j = BLF_width(style->widget.uifont_id, bt->drawstr, sizeof(bt->drawstr));
 
                        if (j > i)
@@ -1182,6 +1218,7 @@ void UI_block_end_ex(const bContext *C, uiBlock *block, const int xy[2], int r_x
 {
        wmWindow *window = CTX_wm_window(C);
        Scene *scene = CTX_data_scene(C);
+       ARegion *region = CTX_wm_region(C);
        uiBut *but;
 
        BLI_assert(block->active);
@@ -1258,6 +1295,8 @@ void UI_block_end_ex(const bContext *C, uiBlock *block, const int xy[2], int r_x
                UI_block_align_end(block);
        }
 
+       ui_update_flexible_spacing(region, block);
+
        block->endblock = 1;
 }
 
@@ -3225,7 +3264,8 @@ static uiBut *ui_def_but(
                 UI_BTYPE_BLOCK, UI_BTYPE_BUT, UI_BTYPE_LABEL,
                 UI_BTYPE_PULLDOWN, UI_BTYPE_ROUNDBOX, UI_BTYPE_LISTBOX,
                 UI_BTYPE_BUT_MENU, UI_BTYPE_SCROLL, UI_BTYPE_GRIP,
-                UI_BTYPE_SEPR, UI_BTYPE_SEPR_LINE) ||
+                UI_BTYPE_SEPR, UI_BTYPE_SEPR_LINE,
+                UI_BTYPE_SEPR_SPACER) ||
            (but->type >= UI_BTYPE_SEARCH_MENU))
        {
                /* pass */
index 619dce1..c945746 100644 (file)
@@ -111,7 +111,7 @@ enum {
 bool ui_but_can_align(const uiBut *but)
 {
        const bool btype_can_align = !ELEM(but->type, UI_BTYPE_LABEL, UI_BTYPE_CHECKBOX, UI_BTYPE_CHECKBOX_N,
-                                          UI_BTYPE_TAB, UI_BTYPE_SEPR, UI_BTYPE_SEPR_LINE);
+                                          UI_BTYPE_TAB, UI_BTYPE_SEPR, UI_BTYPE_SEPR_LINE, UI_BTYPE_SEPR_SPACER);
        return (btype_can_align && (BLI_rctf_size_x(&but->rect) > 0.0f) && (BLI_rctf_size_y(&but->rect) > 0.0f));
 }
 
@@ -499,7 +499,8 @@ void ui_block_align_calc(uiBlock *block, const ARegion *region)
 
 bool ui_but_can_align(uiBut *but)
 {
-       return !ELEM(but->type, UI_BTYPE_LABEL, UI_BTYPE_CHECKBOX, UI_BTYPE_CHECKBOX_N, UI_BTYPE_SEPR, UI_BTYPE_SEPR_LINE);
+       return !ELEM(but->type, UI_BTYPE_LABEL, UI_BTYPE_CHECKBOX, UI_BTYPE_CHECKBOX_N,
+                    UI_BTYPE_SEPR, UI_BTYPE_SEPR_LINE, UI_BTYPE_SEPR_SPACER);
 }
 
 static bool buts_are_horiz(uiBut *but1, uiBut *but2)
index 43eadc1..02844f5 100644 (file)
@@ -7177,6 +7177,7 @@ static int ui_do_button(bContext *C, uiBlock *block, uiBut *but, const wmEvent *
                        /* quiet warnings for unhandled types */
                case UI_BTYPE_SEPR:
                case UI_BTYPE_SEPR_LINE:
+               case UI_BTYPE_SEPR_SPACER:
                case UI_BTYPE_EXTRA:
                        break;
        }
index 64d1641..f031017 100644 (file)
@@ -2216,6 +2216,26 @@ void uiItemS(uiLayout *layout)
        uiDefBut(block, (is_menu) ? UI_BTYPE_SEPR_LINE : UI_BTYPE_SEPR, 0, "", 0, 0, space, space, NULL, 0.0, 0.0, 0, 0, "");
 }
 
+/* Flexible spacing. */
+void uiItemSpacer(uiLayout *layout)
+{
+       uiBlock *block = layout->root->block;
+       bool is_menu = ui_block_is_menu(block);
+
+       if (is_menu) {
+               printf("Error: separator_spacer() not supported in menus.\n");
+               return;
+       }
+
+       if (block->direction != UI_DIR_RIGHT) {
+               printf("Error: separator_spacer() only supported in horizontal blocks\n.");
+               return;
+       }
+
+       UI_block_layout_set_current(block, layout);
+       uiDefBut(block, UI_BTYPE_SEPR_SPACER, 0, "", 0, 0, 0.3f * UI_UNIT_X, UI_UNIT_Y, NULL, 0.0, 0.0, 0, 0, "");
+}
+
 /* level items */
 void uiItemMenuF(uiLayout *layout, const char *name, int icon, uiMenuCreateFunc func, void *arg)
 {
@@ -2599,7 +2619,7 @@ static bool ui_item_is_radial_displayable(uiItem *item)
 static bool ui_item_is_radial_drawable(uiButtonItem *bitem)
 {
 
-       if (ELEM(bitem->but->type, UI_BTYPE_SEPR, UI_BTYPE_SEPR_LINE))
+       if (ELEM(bitem->but->type, UI_BTYPE_SEPR, UI_BTYPE_SEPR_LINE, UI_BTYPE_SEPR_SPACER))
                return false;
 
        return true;
index 3188bc8..ac8d900 100644 (file)
@@ -4423,6 +4423,7 @@ void ui_draw_but(const bContext *C, ARegion *ar, uiStyle *style, uiBut *but, rct
 
                        case UI_BTYPE_SEPR:
                        case UI_BTYPE_SEPR_LINE:
+                       case UI_BTYPE_SEPR_SPACER:
                                break;
 
                        case UI_BTYPE_BUT:
index b4d932d..f73ce24 100644 (file)
@@ -2102,7 +2102,7 @@ void ED_region_header_layout(const bContext *C, ARegion *ar)
        Header header = {NULL};
        int maxco, xco, yco;
        int headery = ED_area_headersize();
-       const int start_ofs = 0.4f * UI_UNIT_X;
+       const int start_ofs = UI_HEADER_OFFSET_START;
        bool region_layout_based = ar->flag & RGN_FLAG_DYNAMIC_SIZE;
 
        /* set view2d view matrix for scrolling (without scrollers) */
index 618a754..3940f13 100644 (file)
@@ -742,6 +742,9 @@ void RNA_api_ui_layout(StructRNA *srna)
        func = RNA_def_function(srna, "separator", "uiItemS");
        RNA_def_function_ui_description(func, "Item. Inserts empty space into the layout between items");
 
+       func = RNA_def_function(srna, "separator_spacer", "uiItemSpacer");
+       RNA_def_function_ui_description(func, "Item. Inserts horizontal spacing empty space into the layout between items");
+
        /* context */
        func = RNA_def_function(srna, "context_pointer_set", "uiLayoutSetContextPointer");
        parm = RNA_def_string(func, "name", NULL, 0, "Name", "Name of entry in the context");