Deskbar: Refactor TRelicantTray::LocationForReplicant()

* if index == 0 return right away, no calculation required.
* rename rect to rowRect then copy it into replicantRect.
  - This makes it clearer to me what's going on.
* don't have to check if index > 0 in horizontal mode anymore since
  we are checking it upfront.
* introduce replicantWidth variable for understanding
* reduce indent

Was calculating the height in multi-row mode twice. Instead, use
the saved height, if that isn't set calculate height as before,
if no replicants, use minimum height (16px) and don't calculate.
This commit is contained in:
John Scipione
2017-11-05 21:04:13 -08:00
parent d8357181eb
commit a44504a168
+37 -29
View File
@@ -215,14 +215,17 @@ TReplicantTray::GetPreferredSize(float* preferredWidth, float* preferredHeight)
if (fMultiRowMode) { if (fMultiRowMode) {
width = static_cast<TBarApp*>(be_app)->Settings()->width width = static_cast<TBarApp*>(be_app)->Settings()->width
- kDragWidth - kGutter; - kDragWidth - kGutter;
if (ReplicantCount() > 0) if (fRightBottomReplicant.IsValid())
height = fRightBottomReplicant.bottom; height = fRightBottomReplicant.bottom;
// the height will be uniform for the number of rows necessary to show else if (ReplicantCount() > 0) {
// all the reps + any gutters necessary for spacing // The height will be uniform for the number of rows necessary
int32 rowCount = (int32)(height / kMaxReplicantHeight); // to show all the replicants and gutters.
height = kGutter + (rowCount * kMaxReplicantHeight) int32 rowCount = (int32)(height / kMaxReplicantHeight);
+ ((rowCount - 1) * kIconGap) + kGutter; height = kGutter + (rowCount * kMaxReplicantHeight)
height = std::max(kMinimumTrayHeight, height); + ((rowCount - 1) * kIconGap) + kGutter;
height = std::max(kMinimumTrayHeight, height);
} else
height = kMinimumTrayHeight;
} else { } else {
// if last replicant overruns clock then resize to accomodate // if last replicant overruns clock then resize to accomodate
if (ReplicantCount() > 0) { if (ReplicantCount() > 0) {
@@ -1155,11 +1158,14 @@ TReplicantTray::LocationForReplicant(int32 index, float replicantWidth)
} else } else
loc.x += 1; // keeps everything lined up nicely loc.x += 1; // keeps everything lined up nicely
if (index <= 0)
return loc;
if (fMultiRowMode) { if (fMultiRowMode) {
// try to find free space in every row // try to find free space in every row
for (int32 row = 0; ; loc.y += kMaxReplicantHeight + kIconGap, row++) { for (int32 row = 0; ; loc.y += kMaxReplicantHeight + kIconGap, row++) {
// determine free space in this row // determine free space in this row
BRect rect(loc.x, loc.y, BRect rowRect(loc.x, loc.y,
loc.x + static_cast<TBarApp*>(be_app)->Settings()->width loc.x + static_cast<TBarApp*>(be_app)->Settings()->width
- kDragRegionWidth * 2, - kDragRegionWidth * 2,
loc.y + kMaxReplicantHeight); loc.y + kMaxReplicantHeight);
@@ -1168,38 +1174,38 @@ TReplicantTray::LocationForReplicant(int32 index, float replicantWidth)
+ kTrayPadding; + kTrayPadding;
} }
BRect replicantRect = rowRect;
for (int32 i = 0; i < index; i++) { for (int32 i = 0; i < index; i++) {
BView* view = NULL; BView* view = NULL;
fShelf->ReplicantAt(i, &view); fShelf->ReplicantAt(i, &view);
if (view == NULL || view->Frame().top != rect.top) if (view == NULL || view->Frame().top != rowRect.top)
continue; continue;
rect.left = view->Frame().right + kIconGap + 1; // push this replicant placement past the last one
replicantRect.left = view->Frame().right + kIconGap + 1;
} }
if (rect.Width() >= width) { if (replicantRect.Width() >= replicantWidth) {
// the icon fits in this row // the icon fits in this row
loc = rect.LeftTop(); loc = replicantRect.LeftTop();
break; break;
} }
} }
} else { } else {
if (index > 0) { // get the last replicant added for placement reference
// get the last replicant added for placement reference BView* view = NULL;
BView* view = NULL; fShelf->ReplicantAt(index - 1, &view);
fShelf->ReplicantAt(index - 1, &view); if (view != NULL) {
if (view != NULL) { // push this replicant placement past the last one
// push this rep placement past the last one loc.x = view->Frame().right + kIconGap + 1;
loc.x = view->Frame().right + kIconGap + 1; loc.y = view->Frame().top;
loc.y = view->Frame().top;
}
} }
} }
if (loc.y > fRightBottomReplicant.top if (loc.y > fRightBottomReplicant.top
|| (loc.y == fRightBottomReplicant.top || (loc.y == fRightBottomReplicant.top
&& loc.x > fRightBottomReplicant.left)) { && loc.x > fRightBottomReplicant.left)) {
fRightBottomReplicant.Set(loc.x, loc.y, loc.x + width, fRightBottomReplicant.Set(loc.x, loc.y, loc.x + replicantWidth,
loc.y + kMaxReplicantHeight); loc.y + kMaxReplicantHeight);
fLastReplicant = index; fLastReplicant = index;
} }
@@ -1251,13 +1257,15 @@ TReplicantTray::RealignReplicants(int32 startIndex)
fRightBottomReplicant.Set(0, 0, 0, 0); fRightBottomReplicant.Set(0, 0, 0, 0);
BView* view = NULL; BView* view = NULL;
for (int32 i = startIndex; i < count; i++) { for (int32 index = startIndex; index < replicantCount; index++) {
fShelf->ReplicantAt(i, &view); fShelf->ReplicantAt(index, &view);
if (view != NULL) { if (view == NULL)
BPoint loc = LocationForReplicant(i, view->Frame().Width()); continue;
if (view->Frame().LeftTop() != loc)
view->MoveTo(loc); float replicantWidth = view->Frame().Width();
} BPoint loc = LocationForReplicant(index, replicantWidth);
if (view->Frame().LeftTop() != loc)
view->MoveTo(loc);
} }
} }