From 49d3b2a14211ac897e4f613d2ef4a255fcff76e4 Mon Sep 17 00:00:00 2001 From: John Scipione Date: Tue, 4 Feb 2025 02:16:24 -0500 Subject: [PATCH] Tracker: Fixup hardcoded values for ModelMenuItem padding This is to make BMenuBar and BMenu margins the same. The hardcoded menu item margin numbers come from this math on what the BMenu and BMenuBar item margins respectively used to be a long time ago: 14 - 8 = 6 // for left margin (14 + 20) - (8 + 8) = 18 // for left + right margin We have since updated these margins so they need to be recalculated. Use BPrivate::MenuPrivate to get the new margin deltas. IconMenuItem and ModelMenuItem classes are used by Tracker add-ons and those can be in the menu bar as well. Change-Id: Ie4147c31e6b19764cb36b9c414f7e91c47b7d9bd Reviewed-on: https://review.haiku-os.org/c/haiku/+/8905 Tested-by: Commit checker robot Reviewed-by: Adrien Destugues --- headers/private/tracker/IconMenuItem.h | 5 ++ src/kits/tracker/IconMenuItem.cpp | 65 +++++++++++++++++++++++--- 2 files changed, 63 insertions(+), 7 deletions(-) diff --git a/headers/private/tracker/IconMenuItem.h b/headers/private/tracker/IconMenuItem.h index 9fe4069771..520e441739 100644 --- a/headers/private/tracker/IconMenuItem.h +++ b/headers/private/tracker/IconMenuItem.h @@ -104,6 +104,11 @@ class ModelMenuItem : public BMenuItem { private: void DrawIcon(); + private: + float _ExtraLeftPadding(); + float _ExtraPadding(); + void _GetHorizontalItemMarginDelta(float* _leftDelta, float* _rightDelta); + Model fModel; float fHeightDelta; bool fDrawText; diff --git a/src/kits/tracker/IconMenuItem.cpp b/src/kits/tracker/IconMenuItem.cpp index 2125cfb7a5..6d45235af9 100644 --- a/src/kits/tracker/IconMenuItem.cpp +++ b/src/kits/tracker/IconMenuItem.cpp @@ -41,6 +41,7 @@ All rights reserved. #include #include #include +#include #include #include "IconCache.h" @@ -126,10 +127,10 @@ ModelMenuItem::DrawContent() { if (fDrawText) { BPoint drawPoint(ContentLocation()); - drawPoint.x += ListIconSize() + (ListIconSize() / 4) - + (fExtraPad ? 6 : 0); + drawPoint.x += ListIconSize() + ListIconSize() / 4 + _ExtraLeftPadding(); if (fHeightDelta > 0) - drawPoint.y += ceil(fHeightDelta / 2); + drawPoint.y += ceilf(fHeightDelta / 2); + Menu()->MovePenTo(drawPoint); _inherited::DrawContent(); } @@ -154,10 +155,9 @@ ModelMenuItem::DrawIcon() // center icon with text. float deltaHeight = fHeightDelta < 0 ? -fHeightDelta : 0; - where.y += ceil(deltaHeight / 2); + where.y += ceilf(deltaHeight / 2); - if (fExtraPad) - where.x += 6; + where.x += _ExtraLeftPadding(); Menu()->SetDrawingMode(B_OP_OVER); Menu()->SetLowColor(B_TRANSPARENT_32_BIT); @@ -177,6 +177,56 @@ ModelMenuItem::DrawIcon() } +float +ModelMenuItem::_ExtraLeftPadding() +{ + if (!fExtraPad) + return 0; + + // BMenu and BMenuBar have different margins, + // we want to make them the same. See fExtraPad. + float leftDelta; + _GetHorizontalItemMarginDelta(&leftDelta, NULL); + + return leftDelta; +} + + +float +ModelMenuItem::_ExtraPadding() +{ + if (!fExtraPad) + return 0; + + // BMenu and BMenuBar have different margins, + // we want to make them the same. See fExtraPad. + float leftDelta, rightDelta; + _GetHorizontalItemMarginDelta(&leftDelta, &rightDelta); + + return leftDelta + rightDelta; +} + + +void +ModelMenuItem::_GetHorizontalItemMarginDelta(float* _leftDelta, float* _rightDelta) +{ + float menuLeft, menuRight, menuBarLeft, menuBarRight; + + BMenu tempMenu("temp"); + BPrivate::MenuPrivate menuPrivate(&tempMenu); + menuPrivate.GetItemMargins(&menuLeft, NULL, &menuRight, NULL); + + BPrivate::MenuPrivate menuBarPrivate(Menu()); + menuBarPrivate.GetItemMargins(&menuBarLeft, NULL, &menuBarRight, NULL); + + if (_leftDelta != NULL) + *_leftDelta = menuLeft - menuBarLeft; + + if (_rightDelta != NULL) + *_rightDelta = menuRight - menuBarRight; +} + + void ModelMenuItem::GetContentSize(float* width, float* height) { @@ -186,7 +236,8 @@ ModelMenuItem::GetContentSize(float* width, float* height) fHeightDelta = iconSize - *height; if (*height < iconSize) *height = iconSize; - *width += iconSize / 4 + iconSize + (fExtraPad ? 18 : 0); + + *width += iconSize + iconSize / 4 + _ExtraPadding(); }