From 836100d5058d2034874afeaf0a47799cf735ad02 Mon Sep 17 00:00:00 2001 From: Adrien Destugues Date: Fri, 7 Aug 2020 16:01:30 +0200 Subject: [PATCH] Pulse: adjust chip size to fit CPU name Fixes #6904 --- src/apps/pulse/NormalPulseView.cpp | 75 +++++++++++------------------- src/apps/pulse/NormalPulseView.h | 2 +- src/apps/pulse/PulseWindow.cpp | 11 +++-- 3 files changed, 35 insertions(+), 53 deletions(-) diff --git a/src/apps/pulse/NormalPulseView.cpp b/src/apps/pulse/NormalPulseView.cpp index 6485b86c5e..45fc56218d 100644 --- a/src/apps/pulse/NormalPulseView.cpp +++ b/src/apps/pulse/NormalPulseView.cpp @@ -19,6 +19,7 @@ #include #include +#include #include #include #include @@ -29,28 +30,11 @@ #define B_TRANSLATION_CONTEXT "NormalPulseView" -float -max_font_size(BFont font, const char* text, float maxSize, float maxWidth) -{ - const float steps = 0.5f; - - for (float size = maxSize; size > 4; size -= steps) { - font.SetSize(size); - if (font.StringWidth(text) <= maxWidth) - return size; - } - - return 4; -} - - -// #pragma mark - - - NormalPulseView::NormalPulseView(BRect rect) : PulseView(rect, "NormalPulseView"), fBrandLogo(NULL) { + SetResizingMode(B_NOT_RESIZABLE); SetViewUIColor(B_PANEL_BACKGROUND_COLOR); SetLowUIColor(ViewUIColor()); @@ -60,6 +44,15 @@ NormalPulseView::NormalPulseView(BRect rect) mode2->SetMessage(new BMessage(PV_DESKBAR_MODE)); DetermineVendorAndProcessor(); + BFont font(be_plain_font); + font.SetSize(8); + SetFont(&font); + + float width = std::max(StringWidth(fProcessor), 48.0f); + fChipRect = BRect(10, (rect.Height() - width - 15) / 2, 25 + width, + (rect.Height() + width + 15) / 2); + float progressLeft = fChipRect.right + 29; + float cpuLeft = fChipRect.right + 5; // Allocate progress bars and button pointers system_info systemInfo; @@ -70,15 +63,15 @@ NormalPulseView::NormalPulseView(BRect rect) // Set up the CPU activity bars and buttons for (int x = 0; x < fCpuCount; x++) { - BRect r(PROGRESS_MLEFT, PROGRESS_MTOP + ITEM_OFFSET * x, - PROGRESS_MLEFT + ProgressBar::PROGRESS_WIDTH, + BRect r(progressLeft, PROGRESS_MTOP + ITEM_OFFSET * x, + progressLeft + ProgressBar::PROGRESS_WIDTH, PROGRESS_MTOP + ITEM_OFFSET * x + ProgressBar::PROGRESS_HEIGHT); char* str2 = (char *)B_TRANSLATE("CPU progress bar"); fProgressBars[x] = new ProgressBar(r, str2); AddChild(fProgressBars[x]); - r.Set(CPUBUTTON_MLEFT, CPUBUTTON_MTOP + ITEM_OFFSET * x, - CPUBUTTON_MLEFT + CPUBUTTON_WIDTH + 7, + r.Set(cpuLeft, CPUBUTTON_MTOP + ITEM_OFFSET * x, + cpuLeft + CPUBUTTON_WIDTH + 7, CPUBUTTON_MTOP + ITEM_OFFSET * x + CPUBUTTON_HEIGHT + 7); char temp[4]; snprintf(temp, sizeof(temp), "%hhd", int8(x + 1)); @@ -90,6 +83,8 @@ NormalPulseView::NormalPulseView(BRect rect) fProgressBars[0]->MoveBy(-3, 12); fCpuButtons[0]->Hide(); } + + ResizeTo(progressLeft + ProgressBar::PROGRESS_WIDTH + 10, rect.Height()); } @@ -101,19 +96,6 @@ NormalPulseView::~NormalPulseView() } -void -NormalPulseView::CalculateFontSizes() -{ - BFont font; - GetFont(&font); - - fProcessorFontSize = max_font_size(font, fProcessor, 11.0f, 46.0f); - - if (fBrandLogo == NULL) - fVendorFontSize = max_font_size(font, fVendor, 13.0f, 46.0f); -} - - void NormalPulseView::DetermineVendorAndProcessor() { @@ -247,6 +229,8 @@ NormalPulseView::DrawChip(BRect r) BPoint(innerRect.right - 8, innerRect.top), B_MIXED_COLORS); StrokeLine(BPoint(innerRect.left + 8, innerRect.bottom), BPoint(innerRect.right - 8, innerRect.bottom), B_MIXED_COLORS); + + SetDrawingMode(B_OP_OVER); } @@ -257,25 +241,24 @@ NormalPulseView::Draw(BRect rect) SetDrawingMode(B_OP_OVER); // Processor picture - DrawChip(BRect(10, 10, 74, 74)); + DrawChip(fChipRect); if (fBrandLogo != NULL) { - DrawBitmap(fBrandLogo, BPoint(18, 17)); + DrawBitmap(fBrandLogo, BPoint( + 9 + (fChipRect.Width() - fBrandLogo->Bounds().Width()) / 2, + fChipRect.top + 8)); } else { SetHighColor(240, 240, 240); - SetFontSize(fVendorFontSize); - float width = StringWidth(fVendor); - MovePenTo(10 + (32 - width / 2), 30); + MovePenTo(10 + (fChipRect.Width() - width) / 2, fChipRect.top + 20); DrawString(fVendor); } // Draw processor type and speed SetHighColor(240, 240, 240); - SetFontSize(fProcessorFontSize); float width = StringWidth(fProcessor); - MovePenTo(10 + (32 - width / 2), 55); + MovePenTo(10 + (fChipRect.Width() - width) / 2, fChipRect.top + 48); DrawString(fProcessor); char buffer[64]; @@ -287,11 +270,8 @@ NormalPulseView::Draw(BRect rect) // We can't assume anymore that a CPU clock speed is always static. // Let's compute the best font size for the CPU speed string each time... - BFont font; - GetFont(&font); - SetFontSize(max_font_size(font, buffer, fProcessorFontSize, 46.0f)); width = StringWidth(buffer); - MovePenTo(10 + (32 - width / 2), 64); + MovePenTo(10 + (fChipRect.Width() - width) / 2, fChipRect.top + 58); DrawString(buffer); PopState(); @@ -320,9 +300,6 @@ NormalPulseView::Pulse() void NormalPulseView::AttachedToWindow() { - SetFont(be_bold_font); - CalculateFontSizes(); - fPreviousTime = system_time(); BMessenger messenger(Window()); diff --git a/src/apps/pulse/NormalPulseView.h b/src/apps/pulse/NormalPulseView.h index aa76496488..bb61ee9136 100644 --- a/src/apps/pulse/NormalPulseView.h +++ b/src/apps/pulse/NormalPulseView.h @@ -39,7 +39,7 @@ class NormalPulseView : public PulseView { int32 fCpuCount; BBitmap* fBrandLogo; - float fVendorFontSize, fProcessorFontSize; + BRect fChipRect; }; #endif diff --git a/src/apps/pulse/PulseWindow.cpp b/src/apps/pulse/PulseWindow.cpp index 6ae131a373..54e270033e 100644 --- a/src/apps/pulse/PulseWindow.cpp +++ b/src/apps/pulse/PulseWindow.cpp @@ -51,8 +51,11 @@ PulseWindow::PulseWindow(BRect rect) fNormalPulseView->Hide(); SetSizeLimits(GetMinimumViewWidth() - 1, 4096, 2, 4096); ResizeTo(rect.Width(), rect.Height()); - } else + } else { fMiniPulseView->Hide(); + BRect r = fNormalPulseView->Bounds(); + ResizeTo(r.Width(), r.Height()); + } } @@ -117,6 +120,7 @@ PulseWindow::SetMode(int newmode) switch (newmode) { case PV_NORMAL_MODE: + { if (fMode == MINI_WINDOW_MODE) { pulseapp->fPrefs->mini_window_rect = Frame(); pulseapp->fPrefs->window_mode = NORMAL_WINDOW_MODE; @@ -127,12 +131,13 @@ PulseWindow::SetMode(int newmode) fMode = NORMAL_WINDOW_MODE; SetType(B_TITLED_WINDOW); SetFlags(B_NOT_RESIZABLE | B_NOT_ZOOMABLE); - ResizeTo(pulseapp->fPrefs->normal_window_rect.IntegerWidth(), - pulseapp->fPrefs->normal_window_rect.IntegerHeight()); + BRect r = fNormalPulseView->Bounds(); + ResizeTo(r.Width(), r.Height()); MoveTo(pulseapp->fPrefs->normal_window_rect.left, pulseapp->fPrefs->normal_window_rect.top); MoveOnScreen(B_MOVE_IF_PARTIALLY_OFFSCREEN); break; + } case PV_MINI_MODE: if (fMode == NORMAL_WINDOW_MODE) {