Display download/upload speed in dock icon

Implementation is based on Transmission sources.

Closes #2761
Closes #3671
Closes #7098
Closes #11350
Closes #18527

PR #19595
This commit is contained in:
Nick Korotysh 2023-10-16 08:45:24 +02:00 committed by GitHub
parent d60f9c6142
commit e6ec3d0c2b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 317 additions and 12 deletions

View file

@ -94,7 +94,7 @@ namespace
Utils::Misc::SizeUnit unit;
};
std::optional<SplitToFriendlyUnitResult> splitToFriendlyUnit(const qint64 bytes)
std::optional<SplitToFriendlyUnitResult> splitToFriendlyUnit(const qint64 bytes, const int unitThreshold = 1024)
{
if (bytes < 0)
return std::nullopt;
@ -102,7 +102,7 @@ namespace
int i = 0;
auto value = static_cast<qreal>(bytes);
while ((value >= 1024) && (i < static_cast<int>(Utils::Misc::SizeUnit::ExbiByte)))
while ((value >= unitThreshold) && (i < static_cast<int>(Utils::Misc::SizeUnit::ExbiByte)))
{
value /= 1024;
++i;
@ -270,6 +270,24 @@ QString Utils::Misc::friendlyUnit(const qint64 bytes, const bool isSpeed, const
+ QChar::Nbsp + unitString(result->unit, isSpeed);
}
QString Utils::Misc::friendlyUnitCompact(const qint64 bytes)
{
// avoid 1000-1023 values, use next larger unit instead
const std::optional<SplitToFriendlyUnitResult> result = splitToFriendlyUnit(bytes, 1000);
if (!result)
return QCoreApplication::translate("misc", "Unknown", "Unknown (size)");
int precision = 0; // >= 100
if (result->value < 10)
precision = 2; // 0 - 9.99
if (result->value < 100)
precision = 1; // 10 - 99.9
return Utils::String::fromDouble(result->value, precision)
// use only one character for unit representation
+ QChar::Nbsp + unitString(result->unit, false)[0];
}
int Utils::Misc::friendlyUnitPrecision(const SizeUnit unit)
{
// friendlyUnit's number of digits after the decimal point