Fluid 1.3.0
Material Design for QtQuick and Qml
Loading...
Searching...
No Matches
CheckBox.qml
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Pier Luigi Fiorini <pierluigi.fiorini@gmail.com>
2// SPDX-License-Identifier: MPL-2.0
3
4import QtQuick
5import QtQuick.Templates as T
6import Fluid as MD
7
18T.CheckBox {
19 id: control
20
22 property MD.typescale typescale: MD.Tokens.typescale.labelLarge
23
25 property bool error: false
26
27 implicitWidth: Math.max(implicitBackgroundWidth + leftInset + rightInset, implicitContentWidth + leftPadding + rightPadding)
28 implicitHeight: Math.max(implicitBackgroundHeight + topInset + bottomInset, implicitContentHeight + topPadding + bottomPadding, implicitIndicatorHeight + topPadding + bottomPadding)
29
30 spacing: MD.Tokens.checkBox.contentSpacing
31 padding: MD.Tokens.checkBox.contentPadding
32 verticalPadding: padding + 7
33
34 hoverEnabled: true
35 focusPolicy: Qt.StrongFocus
36
37 QtObject {
38 id: state
39
40 property color iconColor: control.error ? control.MD.Style.onErrorColor : control.MD.Style.onPrimaryColor
41 property color containerColor: control.checkState === Qt.Unchecked ? "transparent" : control.error ? control.MD.Style.errorColor : control.MD.Style.primaryColor
42 property color outlineColor: control.error ? control.MD.Style.errorColor : MD.Style.onSurfaceVariantColor
43 property color labelColor: control.MD.Style.onSurfaceColor
44 property color stateLayerColor: "transparent"
45
46 property real outlineWidth: control.checkState == Qt.Checked ? MD.Tokens.checkBox.selectedOutlineWidth : MD.Tokens.checkBox.unselectedOutlineWidth
47
48 property real contentOpacity: 1.0
49 property real containerOpacity: 1.0
50 property real stateLayerOpacity: 1.0
51 }
52
53 states: [
54 State {
55 name: "disabled"
56 when: !control.enabled
57
58 PropertyChanges {
59 target: state
60 iconColor: control.MD.Style.surfaceColor
61 containerColor: control.checkState === Qt.Unchecked ? "transparent" : control.error ? control.MD.Style.errorColor : control.MD.Style.onSurfaceColor
62 outlineColor: control.error ? control.MD.Style.errorColor : control.checkState === Qt.Checked ? MD.Style.onSurfaceVariantColor : control.MD.Style.onSurfaceColor
63 outlineWidth: control.checkState == Qt.Checked ? MD.Tokens.checkBox.selectedOutlineWidth : MD.Tokens.checkBox.unselectedOutlineWidth
64 contentOpacity: MD.Tokens.checkBox.selectedDisabledContainerOpacity
65 containerOpacity: MD.Tokens.checkBox.selectedDisabledContainerOpacity
66 }
67 },
68 State {
69 name: "hovered"
70 when: control.hovered && control.enabled
71
72 PropertyChanges {
73 target: state
74 outlineColor: control.error ? control.MD.Style.errorColor : control.checkState === Qt.Checked ? MD.Style.onSurfaceVariantColor : control.MD.Style.onSurfaceColor
75 stateLayerColor: control.error ? control.MD.Style.errorColor : control.checkState === Qt.Checked ? control.MD.Style.primaryColor : control.MD.Style.onSurfaceColor
76 stateLayerOpacity: MD.Tokens.checkBox.hoverStateLayerOpacity
77 }
78 },
79 State {
80 name: "focused"
81 when: control.visualFocus && control.enabled
82
83 PropertyChanges {
84 target: state
85 outlineColor: control.error ? control.MD.Style.errorColor : control.checkState === Qt.Checked ? MD.Style.onSurfaceVariantColor : control.MD.Style.onSurfaceColor
86 stateLayerColor: control.error ? control.MD.Style.errorColor : control.checkState === Qt.Checked ? control.MD.Style.primaryColor : control.MD.Style.onSurfaceColor
87 stateLayerOpacity: MD.Tokens.checkBox.focusStateLayerOpacity
88 }
89 },
90 State {
91 name: "pressed"
92 when: control.down && control.enabled
93
94 PropertyChanges {
95 target: state
96 outlineColor: control.error ? control.MD.Style.errorColor : control.checkState === Qt.Checked ? MD.Style.onSurfaceVariantColor : control.MD.Style.onSurfaceColor
97 stateLayerColor: control.error ? control.MD.Style.errorColor : control.checkState === Qt.Checked ? control.MD.Style.primaryColor : control.MD.Style.onSurfaceColor
98 stateLayerOpacity: MD.Tokens.checkBox.pressedStateLayerOpacity
99 }
100 }
101 ]
102
103 indicator: MD.CheckIndicator {
104 x: control.text ? (control.mirrored ? control.width - width - control.rightPadding : control.leftPadding) : control.leftPadding + (control.availableWidth - width) / 2
105 y: control.topPadding + (control.availableHeight - height) / 2
106
107 control: control
108 color: state.iconColor
109 opacity: state.containerOpacity
110 backgroundColor: state.containerColor
111 outlineColor: state.outlineColor
112 outlineWidth: state.outlineWidth
113
114 MD.Ripple {
115 anchors.centerIn: parent
116 width: MD.Tokens.checkBox.stateLayerSize
117 height: MD.Tokens.checkBox.stateLayerSize
118 radius: width / 2
119 pressX: control.pressX
120 pressY: control.pressY
121 pressed: control.pressed
122 stateOpacity: state.stateLayerOpacity
123 color: state.stateLayerColor
124 }
125 }
126
127 contentItem: MD.Label {
128 leftPadding: control.indicator && !control.mirrored ? control.indicator.width + control.spacing : 0
129 rightPadding: control.indicator && control.mirrored ? control.indicator.width + control.spacing : 0
130
131 text: control.text
132 typescale: control.typescale
133 color: state.labelColor
134 opacity: state.contentOpacity
135 elide: Text.ElideRight
136 verticalAlignment: Text.AlignVCenter
137 }
138}
Material Design 3 design tokens.
Definition tokens.h:65
Displays text using a selectable Material 3 type scale.
Definition Label.qml:15