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 <[email protected]>
Tested-by: Commit checker robot <[email protected]>
This commit is contained in:
Pascal Abresch
2023-01-15 08:45:06 +00:00
committed by Adrien Destugues
parent fb0b77979a
commit 322be3bee0
4 changed files with 49 additions and 1 deletions
+5
View File
@@ -0,0 +1,5 @@
namespace BPrivate {
void AdoptScrollBarFontSize(BView*);
} // namespace BPrivate
+2 -1
View File
@@ -9,6 +9,7 @@
#include "StatusView.h" #include "StatusView.h"
#include <StatusView.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@@ -63,7 +64,7 @@ void
StatusView::AttachedToWindow() StatusView::AttachedToWindow()
{ {
SetFont(be_plain_font); SetFont(be_plain_font);
SetFontSize(ceilf(be_plain_font->Size() * 0.83f)); BPrivate::AdoptScrollBarFontSize(this);
BMessage message(UPDATE_STATUS); BMessage message(UPDATE_STATUS);
message.AddInt32("line", 1); message.AddInt32("line", 1);
+1
View File
@@ -59,6 +59,7 @@ for architectureObject in [ MultiArchSubDirSetup ] {
SettingsHandler.cpp SettingsHandler.cpp
SettingsMessage.cpp SettingsMessage.cpp
ShakeTrackingFilter.cpp ShakeTrackingFilter.cpp
StatusView.cpp
StringForRate.cpp StringForRate.cpp
StringForSize.cpp StringForSize.cpp
StripeView.cpp StripeView.cpp
+41
View File
@@ -0,0 +1,41 @@
/*
* Copyright 2021, Pascal R. G. Abresch, [email protected].
* Distributed under the terms of the MIT License.
*/
#include <ControlLook.h>
#include <View.h>
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