Move formatBytes into a util file

This commit is contained in:
Arnaud 2026-02-23 11:38:00 +04:00
parent 78cbfd2be6
commit 592abb3f84
No known key found for this signature in database
GPG Key ID: 20E40A5D3110766F
6 changed files with 20 additions and 37 deletions

View File

@ -161,6 +161,7 @@ qt_add_qml_module(appqml
NodeHeader.qml
Widgets.qml
DebugPanel.qml
Utils.js
)
# Set up QML module directory for runtime

View File

@ -2,6 +2,7 @@ import QtQuick
import QtQuick.Layouts
import Logos.Theme
import Logos.Controls
import "Utils.js" as Utils
ArcWidget {
id: root
@ -12,26 +13,6 @@ ArcWidget {
fraction: root.total > 0 ? Math.min(root.used / root.total, 1.0) : 0
function formatBytes(bytes) {
if (bytes <= 0) {
return "0 B"
}
if (bytes < 1024) {
return bytes + " B"
}
if (bytes < 1024 * 1024) {
return (bytes / 1024).toFixed(1) + " KB"
}
if (bytes < 1024 * 1024 * 1024) {
return (bytes / (1024 * 1024)).toFixed(1) + " MB"
}
return (bytes / (1024 * 1024 * 1024)).toFixed(2) + " GB"
}
function refreshSpace() {
let space = root.backend.space()
root.total = space.total
@ -52,15 +33,14 @@ ArcWidget {
spacing: 2
LogosText {
text: root.total > 0 ? root.formatBytes(root.used) : "—"
text: root.total > 0 ? Utils.formatBytes(root.used) : "—"
font.pixelSize: 15
font.bold: true
Layout.alignment: Qt.AlignHCenter
}
LogosText {
text: root.total > 0 ? "/ " + root.formatBytes(
root.total) : "STORAGE"
text: root.total > 0 ? "/ " + Utils.formatBytes(root.total) : "STORAGE"
font.pixelSize: 9
color: Theme.palette.textTertiary
font.letterSpacing: 1.3

View File

@ -5,6 +5,7 @@ import QtQuick.Layouts
import QtCore
import Logos.Theme
import Logos.Controls
import "Utils.js" as Utils
// qmllint disable unqualified
ColumnLayout {
@ -31,17 +32,6 @@ ColumnLayout {
onRejected: pendingManifest = null
}
function formatBytes(bytes) {
if (bytes <= 0)
return "0 B"
if (bytes < 1024)
return bytes + " B"
if (bytes < 1024 * 1024)
return (bytes / 1024).toFixed(1) + " KB"
if (bytes < 1024 * 1024 * 1024)
return (bytes / (1024 * 1024)).toFixed(1) + " MB"
return (bytes / (1024 * 1024 * 1024)).toFixed(2) + " GB"
}
Connections {
target: root.backend
@ -189,7 +179,7 @@ ColumnLayout {
}
Text {
width: 80
text: root.formatBytes(parseInt(modelData.datasetSize))
text: Utils.formatBytes(parseInt(modelData.datasetSize))
color: Theme.palette.textSecondary
font.pixelSize: 11
elide: Text.ElideRight

View File

@ -18,12 +18,10 @@ ArcWidget {
fraction: root.uploadProgress / 100.0
fillColor: root.isDone ? Theme.palette.success : Theme.palette.text
// Center content
ColumnLayout {
anchors.centerIn: parent
spacing: 2
// Idle or done: upload icon
UploadIcon {
dotColor: Theme.palette.textSecondary
dotSize: 4

13
src/qml/Utils.js Normal file
View File

@ -0,0 +1,13 @@
.pragma library
function formatBytes(bytes) {
if (bytes <= 0)
return "0 B"
if (bytes < 1024)
return bytes + " B"
if (bytes < 1024 * 1024)
return (bytes / 1024).toFixed(1) + " KB"
if (bytes < 1024 * 1024 * 1024)
return (bytes / (1024 * 1024)).toFixed(1) + " MB"
return (bytes / (1024 * 1024 * 1024)).toFixed(2) + " GB"
}

View File

@ -35,6 +35,7 @@
<file alias="DownloadIcon.qml">qml/icons/DownloadIcon.qml</file>
<file alias="DeleteIcon.qml">qml/icons/DeleteIcon.qml</file>
<file alias="ArcWidget.qml">qml/icons/ArcWidget.qml</file>
<file alias="Utils.js">qml/Utils.js</file>
<file>icons/storage.png</file>
</qresource>
</RCC>