UI: Improve toolbar width snapping

Allow narrower toolbar before snapping to two column layout.
This commit is contained in:
Harley Acheson 2020-01-03 15:37:35 +11:00 committed by Campbell Barton
parent d8dc310103
commit ac7eb71089
1 changed files with 15 additions and 8 deletions

View File

@ -33,6 +33,7 @@
#include "ED_screen.h"
#include "UI_interface.h"
#include "UI_interface_icons.h"
/* -------------------------------------------------------------------- */
/** \name Generic Tool System Region Callbacks
@ -63,17 +64,23 @@ void ED_region_generic_tools_region_message_subscribe(const struct bContext *UNU
int ED_region_generic_tools_region_snap_size(const ARegion *ar, int size, int axis)
{
if (axis == 0) {
/* Note, this depends on the icon size: see #ICON_DEFAULT_HEIGHT_TOOLBAR. */
const float snap_units[] = {2 + 0.8f, 4 + 0.8f};
const float aspect = BLI_rctf_size_x(&ar->v2d.cur) / (BLI_rcti_size_x(&ar->v2d.mask) + 1);
const int icon_size = ICON_DEFAULT_HEIGHT_TOOLBAR / aspect;
const float column = 1.25f * icon_size;
const float margin = 0.5f * icon_size;
const float snap_units[] = {
column + margin, (2.0f * column) + margin, (2.7f * column) + margin};
int best_diff = INT_MAX;
int best_size = size;
for (uint i = 0; i < ARRAY_SIZE(snap_units); i += 1) {
const int test_size = (snap_units[i] * U.widget_unit) / (UI_DPI_FAC * aspect);
const int test_diff = ABS(test_size - size);
if (test_diff < best_diff) {
best_size = test_size;
best_diff = test_diff;
/* Only snap if less than last snap unit. */
if (size <= snap_units[ARRAY_SIZE(snap_units) - 1]) {
for (uint i = 0; i < ARRAY_SIZE(snap_units); i += 1) {
const int test_size = snap_units[i];
const int test_diff = ABS(test_size - size);
if (test_diff < best_diff) {
best_size = test_size;
best_diff = test_diff;
}
}
}
return best_size;