noctalia-shell/Widgets/NIconButton.qml
2025-08-12 10:40:32 -04:00

79 lines
2 KiB
QML

import QtQuick
import Quickshell
import Quickshell.Widgets
import qs.Services
Rectangle {
id: root
readonly property real scaling: Scaling.scale(screen)
// Multiplier to control how large the button container is relative to Style.baseWidgetSize
property real sizeMultiplier: 1.0
property real size: Style.baseWidgetSize * sizeMultiplier * scaling
property string icon
property string tooltipText
property bool showBorder: true
property bool enabled: true
property bool hovering: false
property var onEntered: function () {}
property var onExited: function () {}
property var onClicked: function () {}
property real fontPointSize: Style.fontSizeMedium
property string fontFamily: "Material Symbols Outlined"
implicitWidth: size
implicitHeight: size
color: root.hovering ? Colors.accentPrimary : "transparent"
radius: width * 0.5
border.color: showBorder ? Colors.accentPrimary : "transparent"
border.width: Math.max(1, Style.borderThin * scaling)
NText {
anchors.centerIn: parent
// Little hack to keep things centered at high scaling
anchors.horizontalCenterOffset: -1 * (scaling - 1.0)
anchors.verticalCenterOffset: 0
text: root.icon
font.family: fontFamily
font.pointSize: root.fontPointSize * scaling
color: root.hovering ? Colors.onAccent : showBorder ? Colors.accentPrimary : Colors.textPrimary
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
opacity: root.enabled ? Style.opacityFull : Style.opacityMedium
}
NTooltip {
id: tooltip
target: root
positionAbove: false
text: root.tooltipText
}
MouseArea {
anchors.fill: parent
cursorShape: Qt.PointingHandCursor
hoverEnabled: true
onEntered: {
hovering = true
if (tooltipText) {
tooltip.show()
}
root.onEntered()
}
onExited: {
hovering = false
if (tooltipText) {
tooltip.hide()
}
root.onExited()
}
onClicked: {
if (tooltipText) {
tooltip.hide()
}
root.onClicked()
}
}
}