From 322be3bee0a03f5f8a037f1f744e6663ccda42d6 Mon Sep 17 00:00:00 2001 From: Pascal Abresch Date: Sun, 25 Jul 2021 15:03:20 +0200 Subject: [PATCH] StatusView: compute font size based on scrollbar size Compute a font size that just fits the available space, instead of using an arbitrary scaling. This makes the code adjust to any font and any UI size. Select the appropriate font using a binary search, which will need only a few attempts (I think 6 font sizes will be tried at the default config) Change-Id: Ie3b8678678c0d940981f1785418aa8ab354d01c5 Reviewed-on: https://review.haiku-os.org/c/haiku/+/3893 Reviewed-by: waddlesplash Tested-by: Commit checker robot --- headers/private/shared/StatusView.h | 5 ++++ src/apps/stylededit/StatusView.cpp | 3 ++- src/kits/shared/Jamfile | 1 + src/kits/shared/StatusView.cpp | 41 +++++++++++++++++++++++++++++ 4 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 headers/private/shared/StatusView.h create mode 100644 src/kits/shared/StatusView.cpp diff --git a/headers/private/shared/StatusView.h b/headers/private/shared/StatusView.h new file mode 100644 index 0000000000..9a96dc5494 --- /dev/null +++ b/headers/private/shared/StatusView.h @@ -0,0 +1,5 @@ +namespace BPrivate { + +void AdoptScrollBarFontSize(BView*); + +} // namespace BPrivate diff --git a/src/apps/stylededit/StatusView.cpp b/src/apps/stylededit/StatusView.cpp index 243d333b3e..c13d39a931 100644 --- a/src/apps/stylededit/StatusView.cpp +++ b/src/apps/stylededit/StatusView.cpp @@ -9,6 +9,7 @@ #include "StatusView.h" +#include #include #include @@ -63,7 +64,7 @@ void StatusView::AttachedToWindow() { SetFont(be_plain_font); - SetFontSize(ceilf(be_plain_font->Size() * 0.83f)); + BPrivate::AdoptScrollBarFontSize(this); BMessage message(UPDATE_STATUS); message.AddInt32("line", 1); diff --git a/src/kits/shared/Jamfile b/src/kits/shared/Jamfile index aea432cbe7..bf0e23b2c4 100644 --- a/src/kits/shared/Jamfile +++ b/src/kits/shared/Jamfile @@ -59,6 +59,7 @@ for architectureObject in [ MultiArchSubDirSetup ] { SettingsHandler.cpp SettingsMessage.cpp ShakeTrackingFilter.cpp + StatusView.cpp StringForRate.cpp StringForSize.cpp StripeView.cpp diff --git a/src/kits/shared/StatusView.cpp b/src/kits/shared/StatusView.cpp new file mode 100644 index 0000000000..542799278c --- /dev/null +++ b/src/kits/shared/StatusView.cpp @@ -0,0 +1,41 @@ +/* + * Copyright 2021, Pascal R. G. Abresch, nep@packageloss.eu. + * Distributed under the terms of the MIT License. + */ + +#include +#include + + +namespace BPrivate { + + +void +AdoptScrollBarFontSize(BView* view) +{ + float maxSize = be_control_look->GetScrollBarWidth(); + BFont testFont = be_plain_font; + float currentSize; + font_height fontHeight; + + float minFontSize = 0.0f; + float maxFontSize = 48.0f; + + while (maxFontSize - minFontSize > 1.0f) { + float midFontSize = (maxFontSize + minFontSize) / 2.0f; + + testFont.SetSize(midFontSize); + testFont.GetHeight(&fontHeight); + currentSize = fontHeight.ascent + fontHeight.descent; + + if (currentSize > maxSize) + maxFontSize = midFontSize; + else + minFontSize = midFontSize; + } + + view->SetFontSize(minFontSize); +} + + +} // namespace BPrivate