コード例 #1
0
void
PowerStatusView::Update(bool force)
{
	int32 previousPercent = fPercent;
	time_t previousTimeLeft = fTimeLeft;
	bool wasOnline = fOnline;
	bool hadBattery = fHasBattery;

	_GetBatteryInfo(fBatteryID, &fBatteryInfo);
	fHasBattery = fBatteryInfo.full_capacity > 0;

	if (fBatteryInfo.full_capacity > 0 && fHasBattery) {
		fPercent = (100 * fBatteryInfo.capacity) / fBatteryInfo.full_capacity;
		fOnline = (fBatteryInfo.state & BATTERY_DISCHARGING) == 0;
		fTimeLeft = fBatteryInfo.time_left;
	} else {
		fPercent = 0;
		fOnline = false;
		fTimeLeft = -1;
	}

	if (fHasBattery && (fPercent <= 0 || fPercent > 100)) {
		// Just ignore this probe -- it obviously returned invalid values
		fPercent = previousPercent;
		fTimeLeft = previousTimeLeft;
		fOnline = wasOnline;
		fHasBattery = hadBattery;
		return;
	}

	if (fInDeskbar) {
		// make sure the tray icon is large enough
		float width = fShowStatusIcon ? kMinIconWidth + 2 : 0;

		if (fShowLabel) {
			char text[64];
			_SetLabel(text, sizeof(text));

			if (text[0])
				width += ceilf(StringWidth(text)) + 4;
		} else {
			char text[256];
			const char* open = "";
			const char* close = "";
			if (fOnline) {
				open = "(";
				close = ")";
			}
			if (fHasBattery) {
				size_t length = snprintf(text, sizeof(text), "%s%" B_PRId32
					"%%%s", open, fPercent, close);
				if (fTimeLeft >= 0) {
					length += snprintf(text + length, sizeof(text) - length,
						"\n%" B_PRId32 ":%02" B_PRId32, fTimeLeft / 3600,
						(fTimeLeft / 60) % 60);
				}

				const char* state = NULL;
				if ((fBatteryInfo.state & BATTERY_CHARGING) != 0)
					state = B_TRANSLATE("charging");
				else if ((fBatteryInfo.state & BATTERY_DISCHARGING) != 0)
					state = B_TRANSLATE("discharging");

				if (state != NULL) {
					snprintf(text + length, sizeof(text) - length, "\n%s",
						state);
				}
			} else
				strcpy(text, B_TRANSLATE("no battery"));
			SetToolTip(text);
		}
		if (width == 0) {
			// make sure we're not going away completely
			width = 8;
		}

		if (width != Bounds().Width())
			ResizeTo(width, Bounds().Height());
	}

	if (force || wasOnline != fOnline
		|| (fShowTime && fTimeLeft != previousTimeLeft)
		|| (!fShowTime && fPercent != previousPercent)) {
		Invalidate();
	}

	if (!fOnline && fHasBattery && previousPercent > kLowBatteryPercentage
			&& fPercent <= kLowBatteryPercentage) {
		_NotifyLowBattery();
	}
}
コード例 #2
0
ファイル: PowerStatusView.cpp プロジェクト: RTOSkit/haiku
void
PowerStatusView::Update(bool force)
{
	int32 previousPercent = fPercent;
	bool previousTimeLeft = fTimeLeft;
	bool wasOnline = fOnline;

	_GetBatteryInfo(&fBatteryInfo, fBatteryID);

	fHasBattery = (fBatteryInfo.state & BATTERY_CRITICAL_STATE) == 0;

	if (fBatteryInfo.full_capacity > 0 && fHasBattery) {
		fPercent = (100 * fBatteryInfo.capacity) / fBatteryInfo.full_capacity;
		fOnline = (fBatteryInfo.state & BATTERY_CHARGING) != 0;
		fTimeLeft = fBatteryInfo.time_left;
	} else {
		fPercent = 0;
		fOnline = false;
		fTimeLeft = false;
	}


	if (fInDeskbar) {
		// make sure the tray icon is large enough
		float width = fShowStatusIcon ? kMinIconWidth + 2 : 0;

		if (fShowLabel) {
			char text[64];
			_SetLabel(text, sizeof(text));

			if (text[0])
				width += ceilf(StringWidth(text)) + 4;
		} else {
			char text[256];
			const char* open = "";
			const char* close = "";
			if (fOnline) {
				open = "(";
				close = ")";
			}
			if (fHasBattery) {
				size_t length = snprintf(text, sizeof(text), "%s%ld%%%s",
					open, fPercent, close);
				if (fTimeLeft) {
					length += snprintf(text + length, sizeof(text) - length,
						"\n%ld:%02ld", fTimeLeft / 3600, (fTimeLeft / 60) % 60);
				}

				const char* state = NULL;
				if ((fBatteryInfo.state & BATTERY_CHARGING) != 0)
					state = B_TRANSLATE("charging");
				else if ((fBatteryInfo.state & BATTERY_DISCHARGING) != 0)
					state = B_TRANSLATE("discharging");

				if (state != NULL) {
					snprintf(text + length, sizeof(text) - length, "\n%s",
						state);
				}
			} else
				strcpy(text, B_TRANSLATE("no battery"));
			SetToolTip(text);
		}
		if (width == 0) {
			// make sure we're not going away completely
			width = 8;
		}

		if (width != Bounds().Width())
			ResizeTo(width, Bounds().Height());
	}

	if (force || wasOnline != fOnline
		|| (fShowTime && fTimeLeft != previousTimeLeft)
		|| (!fShowTime && fPercent != previousPercent))
		Invalidate();
}