blob: 2b04f9b13dce76b3bbbd1c7e7d540f99b934eeca [file] [log] [blame]
Sébastien Blin1f915762020-08-03 13:27:42 -04001
2/*
3 * Copyright (C) 2020 by Savoir-faire Linux
4 * Author: Mingrui Zhang <mingrui.zhang@savoirfairelinux.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19import QtQuick 2.14
20import QtQuick.Controls 2.14
21import net.jami.Models 1.0
Sébastien Blin8940f3c2020-07-23 17:03:11 -040022import QtGraphicalEffects 1.15
Sébastien Blin1f915762020-08-03 13:27:42 -040023
24
25/*
26 * HoverableButton contains the following configurable properties:
27 * 1. Color changes on different button state
28 * 2. Radius control (rounded)
29 * 3. Text content or image content
30 * 4. Can use OnClicked slot to implement some click logic
31 */
32Button {
33 id: hoverableButton
34
Sébastien Blin8940f3c2020-07-23 17:03:11 -040035 checkable: true
36 checked: false
37
Sébastien Blin1f915762020-08-03 13:27:42 -040038 property int fontPointSize: 9
39 property int buttonImageHeight: hoverableButtonBackground.height - 10
40 property int buttonImageWidth: hoverableButtonBackground.width - 10
41
42 property string backgroundColor: JamiTheme.releaseColor
43 property string onPressColor: JamiTheme.pressColor
44 property string onReleaseColor: JamiTheme.releaseColor
45 property string onEnterColor: JamiTheme.hoverColor
46 property string onExitColor: JamiTheme.releaseColor
47
48 property alias radius: hoverableButtonBackground.radius
49 property alias source: hoverableButtonImage.source
Sébastien Blin8940f3c2020-07-23 17:03:11 -040050 property var checkedImage: null
51 property var baseImage: null
52 property var checkedColor: null
53 property var baseColor: null
54 property alias color: hoverableButton.baseColor
Sébastien Blin1f915762020-08-03 13:27:42 -040055
56 font.pointSize: fontPointSize
57
58 hoverEnabled: true
59
60 background: Rectangle {
61 id: hoverableButtonBackground
62
63 color: backgroundColor
64
65 Image {
66 id: hoverableButtonImage
67
68 anchors.centerIn: hoverableButtonBackground
69
70 height: buttonImageHeight
71 width: buttonImageWidth
72
73 fillMode: Image.PreserveAspectFit
74 mipmap: true
75 asynchronous: true
Sébastien Blin8940f3c2020-07-23 17:03:11 -040076
77 source: hoverableButton.checked && checkedImage? checkedImage : baseImage
78
79 layer {
80 enabled: true
81 effect: ColorOverlay {
82 id: overlay
83 color: hoverableButton.checked && checkedColor?
84 checkedColor :
85 (baseColor? baseColor : "transparent")
86 }
87 }
Sébastien Blin1f915762020-08-03 13:27:42 -040088 }
89
90 MouseArea {
91 anchors.fill: parent
92
Sébastien Blin8940f3c2020-07-23 17:03:11 -040093 hoverEnabled: false
Sébastien Blin1f915762020-08-03 13:27:42 -040094
95 onPressed: {
96 hoverableButtonBackground.color = onPressColor
97 }
98 onReleased: {
99 hoverableButtonBackground.color = onReleaseColor
Sébastien Blin8940f3c2020-07-23 17:03:11 -0400100 hoverableButton.toggle()
Sébastien Blin1f915762020-08-03 13:27:42 -0400101 hoverableButton.clicked()
102 }
103 onEntered: {
104 hoverableButtonBackground.color = onEnterColor
105 }
106 onExited: {
107 hoverableButtonBackground.color = onExitColor
108 }
109 }
110 }
111}