PowerStatus: Use BNumberFormat for percentage value
This provides automatic percentage formatting according to the locale setting. Change-Id: I022331866acc2e20d819625c6d070807f476c745 Reviewed-on: https://review.haiku-os.org/c/haiku/+/6209 Tested-by: Automation <[email protected]> Reviewed-by: Máximo Castañeda <[email protected]> Reviewed-by: Jérôme Duval <[email protected]>
This commit is contained in:
@@ -32,6 +32,7 @@
|
|||||||
#include <MenuItem.h>
|
#include <MenuItem.h>
|
||||||
#include <MessageRunner.h>
|
#include <MessageRunner.h>
|
||||||
#include <Notification.h>
|
#include <Notification.h>
|
||||||
|
#include <NumberFormat.h>
|
||||||
#include <Path.h>
|
#include <Path.h>
|
||||||
#include <PopUpMenu.h>
|
#include <PopUpMenu.h>
|
||||||
#include <Resources.h>
|
#include <Resources.h>
|
||||||
@@ -58,8 +59,8 @@ const uint32 kMsgToggleTime = 'tgtm';
|
|||||||
const uint32 kMsgToggleStatusIcon = 'tgsi';
|
const uint32 kMsgToggleStatusIcon = 'tgsi';
|
||||||
const uint32 kMsgToggleExtInfo = 'texi';
|
const uint32 kMsgToggleExtInfo = 'texi';
|
||||||
|
|
||||||
const int32 kLowBatteryPercentage = 15;
|
const double kLowBatteryPercentage = 0.15;
|
||||||
const int32 kNoteBatteryPercentage = 30;
|
const double kNoteBatteryPercentage = 0.3;
|
||||||
|
|
||||||
|
|
||||||
PowerStatusView::PowerStatusView(PowerStatusDriverInterface* interface,
|
PowerStatusView::PowerStatusView(PowerStatusDriverInterface* interface,
|
||||||
@@ -112,7 +113,7 @@ PowerStatusView::_Init()
|
|||||||
fShowTime = false;
|
fShowTime = false;
|
||||||
fShowStatusIcon = true;
|
fShowStatusIcon = true;
|
||||||
|
|
||||||
fPercent = 100;
|
fPercent = 1.0;
|
||||||
fOnline = true;
|
fOnline = true;
|
||||||
fTimeLeft = 0;
|
fTimeLeft = 0;
|
||||||
}
|
}
|
||||||
@@ -197,11 +198,11 @@ PowerStatusView::_DrawBattery(BView* view, BRect rect)
|
|||||||
view->FillRect(BRect(left, floorf(rect.top + rect.Height() / 4) + 1,
|
view->FillRect(BRect(left, floorf(rect.top + rect.Height() / 4) + 1,
|
||||||
rect.left - 1, floorf(rect.bottom - rect.Height() / 4)));
|
rect.left - 1, floorf(rect.bottom - rect.Height() / 4)));
|
||||||
|
|
||||||
int32 percent = fPercent;
|
double percent = fPercent;
|
||||||
if (percent > 100)
|
if (percent > 1.0)
|
||||||
percent = 100;
|
percent = 1.0;
|
||||||
else if (percent < 0 || !fHasBattery)
|
else if (percent < 0.0 || !fHasBattery)
|
||||||
percent = 0;
|
percent = 0.0;
|
||||||
|
|
||||||
rect.InsetBy(gap, gap);
|
rect.InsetBy(gap, gap);
|
||||||
|
|
||||||
@@ -215,13 +216,13 @@ PowerStatusView::_DrawBattery(BView* view, BRect rect)
|
|||||||
}
|
}
|
||||||
|
|
||||||
BRect unfilled = rect;
|
BRect unfilled = rect;
|
||||||
if (percent > 0)
|
if (percent > 0.0)
|
||||||
unfilled.left += unfilled.Width() * percent / 100.0;
|
unfilled.left += unfilled.Width() * percent;
|
||||||
|
|
||||||
view->SetHighColor(unfilledColor);
|
view->SetHighColor(unfilledColor);
|
||||||
view->FillRect(unfilled);
|
view->FillRect(unfilled);
|
||||||
|
|
||||||
if (percent > 0) {
|
if (percent > 0.0) {
|
||||||
// draw filled area
|
// draw filled area
|
||||||
rgb_color fillColor;
|
rgb_color fillColor;
|
||||||
if (percent <= kLowBatteryPercentage)
|
if (percent <= kLowBatteryPercentage)
|
||||||
@@ -232,7 +233,7 @@ PowerStatusView::_DrawBattery(BView* view, BRect rect)
|
|||||||
fillColor.set_to(20, 180, 0);
|
fillColor.set_to(20, 180, 0);
|
||||||
|
|
||||||
BRect fill = rect;
|
BRect fill = rect;
|
||||||
fill.right = fill.left + fill.Width() * percent / 100.0;
|
fill.right = fill.left + fill.Width() * percent;
|
||||||
|
|
||||||
// draw bevel
|
// draw bevel
|
||||||
rgb_color bevelLightColor = tint_color(fillColor, 0.2);
|
rgb_color bevelLightColor = tint_color(fillColor, 0.2);
|
||||||
@@ -371,8 +372,14 @@ PowerStatusView::_SetLabel(char* buffer, size_t bufferLength)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!fShowTime && fPercent >= 0) {
|
if (!fShowTime && fPercent >= 0) {
|
||||||
snprintf(buffer, bufferLength, "%s%" B_PRId32 "%%%s", open, fPercent,
|
BNumberFormat numberFormat;
|
||||||
close);
|
BString data;
|
||||||
|
|
||||||
|
if (numberFormat.FormatPercent(data, fPercent) != B_OK) {
|
||||||
|
data.SetToFormat("%" B_PRId32 "%%", int32(fPercent * 100));
|
||||||
|
}
|
||||||
|
|
||||||
|
snprintf(buffer, bufferLength, "%s%s%s", open, data.String(), close);
|
||||||
} else if (fShowTime && fTimeLeft >= 0) {
|
} else if (fShowTime && fTimeLeft >= 0) {
|
||||||
snprintf(buffer, bufferLength, "%s%" B_PRIdTIME ":%02" B_PRIdTIME "%s",
|
snprintf(buffer, bufferLength, "%s%" B_PRIdTIME ":%02" B_PRIdTIME "%s",
|
||||||
open, fTimeLeft / 3600, (fTimeLeft / 60) % 60, close);
|
open, fTimeLeft / 3600, (fTimeLeft / 60) % 60, close);
|
||||||
@@ -380,11 +387,10 @@ PowerStatusView::_SetLabel(char* buffer, size_t bufferLength)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void
|
void
|
||||||
PowerStatusView::Update(bool force, bool notify)
|
PowerStatusView::Update(bool force, bool notify)
|
||||||
{
|
{
|
||||||
int32 previousPercent = fPercent;
|
double previousPercent = fPercent;
|
||||||
time_t previousTimeLeft = fTimeLeft;
|
time_t previousTimeLeft = fTimeLeft;
|
||||||
bool wasOnline = fOnline;
|
bool wasOnline = fOnline;
|
||||||
bool hadBattery = fHasBattery;
|
bool hadBattery = fHasBattery;
|
||||||
@@ -392,16 +398,16 @@ PowerStatusView::Update(bool force, bool notify)
|
|||||||
fHasBattery = fBatteryInfo.full_capacity > 0;
|
fHasBattery = fBatteryInfo.full_capacity > 0;
|
||||||
|
|
||||||
if (fBatteryInfo.full_capacity > 0 && fHasBattery) {
|
if (fBatteryInfo.full_capacity > 0 && fHasBattery) {
|
||||||
fPercent = (100 * fBatteryInfo.capacity) / fBatteryInfo.full_capacity;
|
fPercent = (double)fBatteryInfo.capacity / fBatteryInfo.full_capacity;
|
||||||
fOnline = (fBatteryInfo.state & BATTERY_DISCHARGING) == 0;
|
fOnline = (fBatteryInfo.state & BATTERY_DISCHARGING) == 0;
|
||||||
fTimeLeft = fBatteryInfo.time_left;
|
fTimeLeft = fBatteryInfo.time_left;
|
||||||
} else {
|
} else {
|
||||||
fPercent = 0;
|
fPercent = 0.0;
|
||||||
fOnline = false;
|
fOnline = false;
|
||||||
fTimeLeft = -1;
|
fTimeLeft = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fHasBattery && (fPercent <= 0 || fPercent > 100)) {
|
if (fHasBattery && (fPercent <= 0 || fPercent > 1.0)) {
|
||||||
// Just ignore this probe -- it obviously returned invalid values
|
// Just ignore this probe -- it obviously returned invalid values
|
||||||
fPercent = previousPercent;
|
fPercent = previousPercent;
|
||||||
fTimeLeft = previousTimeLeft;
|
fTimeLeft = previousTimeLeft;
|
||||||
@@ -429,12 +435,19 @@ PowerStatusView::Update(bool force, bool notify)
|
|||||||
close = ")";
|
close = ")";
|
||||||
}
|
}
|
||||||
if (fHasBattery) {
|
if (fHasBattery) {
|
||||||
size_t length = snprintf(text, sizeof(text), "%s%" B_PRId32
|
BNumberFormat numberFormat;
|
||||||
"%%%s", open, fPercent, close);
|
BString data;
|
||||||
|
size_t length;
|
||||||
|
|
||||||
|
if (numberFormat.FormatPercent(data, fPercent) != B_OK) {
|
||||||
|
data.SetToFormat("%" B_PRId32 "%%", int32(fPercent * 100));
|
||||||
|
}
|
||||||
|
|
||||||
|
length = snprintf(text, sizeof(text), "%s%s%s", open, data.String(), close);
|
||||||
|
|
||||||
if (fTimeLeft >= 0) {
|
if (fTimeLeft >= 0) {
|
||||||
length += snprintf(text + length, sizeof(text) - length,
|
length += snprintf(text + length, sizeof(text) - length, "\n%" B_PRIdTIME
|
||||||
"\n%" B_PRIdTIME ":%02" B_PRIdTIME, fTimeLeft / 3600,
|
":%02" B_PRIdTIME, fTimeLeft / 3600, (fTimeLeft / 60) % 60);
|
||||||
(fTimeLeft / 60) % 60);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const char* state = NULL;
|
const char* state = NULL;
|
||||||
|
|||||||
@@ -64,7 +64,7 @@ protected:
|
|||||||
|
|
||||||
battery_info fBatteryInfo;
|
battery_info fBatteryInfo;
|
||||||
|
|
||||||
int32 fPercent;
|
double fPercent;
|
||||||
time_t fTimeLeft;
|
time_t fTimeLeft;
|
||||||
bool fOnline;
|
bool fOnline;
|
||||||
bool fHasBattery;
|
bool fHasBattery;
|
||||||
|
|||||||
Reference in New Issue
Block a user