From a44504a168548e129e58eeace336a6a2315257ad Mon Sep 17 00:00:00 2001 From: John Scipione Date: Mon, 23 Oct 2017 21:07:18 -0700 Subject: [PATCH] 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. --- src/apps/deskbar/StatusView.cpp | 66 ++++++++++++++++++--------------- 1 file changed, 37 insertions(+), 29 deletions(-) diff --git a/src/apps/deskbar/StatusView.cpp b/src/apps/deskbar/StatusView.cpp index 7943f5a3cb..633935f6d7 100644 --- a/src/apps/deskbar/StatusView.cpp +++ b/src/apps/deskbar/StatusView.cpp @@ -215,14 +215,17 @@ TReplicantTray::GetPreferredSize(float* preferredWidth, float* preferredHeight) if (fMultiRowMode) { width = static_cast(be_app)->Settings()->width - kDragWidth - kGutter; - if (ReplicantCount() > 0) + if (fRightBottomReplicant.IsValid()) height = fRightBottomReplicant.bottom; - // the height will be uniform for the number of rows necessary to show - // all the reps + any gutters necessary for spacing - int32 rowCount = (int32)(height / kMaxReplicantHeight); - height = kGutter + (rowCount * kMaxReplicantHeight) - + ((rowCount - 1) * kIconGap) + kGutter; - height = std::max(kMinimumTrayHeight, height); + else if (ReplicantCount() > 0) { + // The height will be uniform for the number of rows necessary + // to show all the replicants and gutters. + int32 rowCount = (int32)(height / kMaxReplicantHeight); + height = kGutter + (rowCount * kMaxReplicantHeight) + + ((rowCount - 1) * kIconGap) + kGutter; + height = std::max(kMinimumTrayHeight, height); + } else + height = kMinimumTrayHeight; } else { // if last replicant overruns clock then resize to accomodate if (ReplicantCount() > 0) { @@ -1155,11 +1158,14 @@ TReplicantTray::LocationForReplicant(int32 index, float replicantWidth) } else loc.x += 1; // keeps everything lined up nicely + if (index <= 0) + return loc; + if (fMultiRowMode) { // try to find free space in every row for (int32 row = 0; ; loc.y += kMaxReplicantHeight + kIconGap, row++) { // determine free space in this row - BRect rect(loc.x, loc.y, + BRect rowRect(loc.x, loc.y, loc.x + static_cast(be_app)->Settings()->width - kDragRegionWidth * 2, loc.y + kMaxReplicantHeight); @@ -1168,38 +1174,38 @@ TReplicantTray::LocationForReplicant(int32 index, float replicantWidth) + kTrayPadding; } + BRect replicantRect = rowRect; for (int32 i = 0; i < index; i++) { BView* view = NULL; fShelf->ReplicantAt(i, &view); - if (view == NULL || view->Frame().top != rect.top) + if (view == NULL || view->Frame().top != rowRect.top) 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 - loc = rect.LeftTop(); + loc = replicantRect.LeftTop(); break; } } } else { - if (index > 0) { - // get the last replicant added for placement reference - BView* view = NULL; - fShelf->ReplicantAt(index - 1, &view); - if (view != NULL) { - // push this rep placement past the last one - loc.x = view->Frame().right + kIconGap + 1; - loc.y = view->Frame().top; - } + // get the last replicant added for placement reference + BView* view = NULL; + fShelf->ReplicantAt(index - 1, &view); + if (view != NULL) { + // push this replicant placement past the last one + loc.x = view->Frame().right + kIconGap + 1; + loc.y = view->Frame().top; } } if (loc.y > fRightBottomReplicant.top || (loc.y == fRightBottomReplicant.top && 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); fLastReplicant = index; } @@ -1251,13 +1257,15 @@ TReplicantTray::RealignReplicants(int32 startIndex) fRightBottomReplicant.Set(0, 0, 0, 0); BView* view = NULL; - for (int32 i = startIndex; i < count; i++) { - fShelf->ReplicantAt(i, &view); - if (view != NULL) { - BPoint loc = LocationForReplicant(i, view->Frame().Width()); - if (view->Frame().LeftTop() != loc) - view->MoveTo(loc); - } + for (int32 index = startIndex; index < replicantCount; index++) { + fShelf->ReplicantAt(index, &view); + if (view == NULL) + continue; + + float replicantWidth = view->Frame().Width(); + BPoint loc = LocationForReplicant(index, replicantWidth); + if (view->Frame().LeftTop() != loc) + view->MoveTo(loc); } }