Files
illogical-impulse/dots/.config/quickshell/ii/modules/common/widgets/VisuallyCenteredStyledText.qml
T
2026-03-21 11:59:41 +01:00

46 lines
1.4 KiB
QML

pragma ComponentBehavior: Bound
import QtQuick
import QtQuick.Layouts
Item {
id: root
property alias textWidget: textWidget
property alias text: textWidget.text
property alias horizontalAlignment: textWidget.horizontalAlignment
property alias verticalAlignment: textWidget.verticalAlignment
property alias font: textWidget.font
property alias color: textWidget.color
property alias elide: textWidget.elide
property alias wrapMode: textWidget.wrapMode
property alias animateChange: textWidget.animateChange
// In many cases the baseline is a bit high to accomodate the dangling parts of "g" and "y",
// making most text (especiall number-only text) not well-balanced.
// This adjusts the rounding to make sure the text gets lowered if not internally pixel-aligned
property bool lowerBias: true
implicitWidth: textMetrics.width
implicitHeight: textMetrics.height
TextMetrics {
id: textMetrics
font: root.font
text: root.text
}
StyledText {
id: textWidget
anchors {
left: parent.left
right: parent.right
}
y: {
const value = (parent.height - textMetrics.height) / 2;
return root.lowerBias ? Math.ceil(value) : Math.round(value);
}
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
}
}