Sébastien Blin | 1f91576 | 2020-08-03 13:27:42 -0400 | [diff] [blame] | 1 | |
| 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 | */ |
| 19 | import QtQuick 2.14 |
| 20 | import QtQuick.Controls 2.14 |
| 21 | import net.jami.Models 1.0 |
Sébastien Blin | 8940f3c | 2020-07-23 17:03:11 -0400 | [diff] [blame] | 22 | import QtGraphicalEffects 1.15 |
Sébastien Blin | 1f91576 | 2020-08-03 13:27:42 -0400 | [diff] [blame] | 23 | |
| 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 | */ |
| 32 | Button { |
| 33 | id: hoverableButton |
| 34 | |
Sébastien Blin | 8940f3c | 2020-07-23 17:03:11 -0400 | [diff] [blame] | 35 | checkable: true |
| 36 | checked: false |
| 37 | |
Sébastien Blin | 1f91576 | 2020-08-03 13:27:42 -0400 | [diff] [blame] | 38 | 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 Blin | 8940f3c | 2020-07-23 17:03:11 -0400 | [diff] [blame] | 50 | 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 Blin | 1f91576 | 2020-08-03 13:27:42 -0400 | [diff] [blame] | 55 | |
| 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 Blin | 8940f3c | 2020-07-23 17:03:11 -0400 | [diff] [blame] | 76 | |
| 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 Blin | 1f91576 | 2020-08-03 13:27:42 -0400 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | MouseArea { |
| 91 | anchors.fill: parent |
| 92 | |
Sébastien Blin | 8940f3c | 2020-07-23 17:03:11 -0400 | [diff] [blame] | 93 | hoverEnabled: false |
Sébastien Blin | 1f91576 | 2020-08-03 13:27:42 -0400 | [diff] [blame] | 94 | |
| 95 | onPressed: { |
| 96 | hoverableButtonBackground.color = onPressColor |
| 97 | } |
| 98 | onReleased: { |
| 99 | hoverableButtonBackground.color = onReleaseColor |
Sébastien Blin | 8940f3c | 2020-07-23 17:03:11 -0400 | [diff] [blame] | 100 | hoverableButton.toggle() |
Sébastien Blin | 1f91576 | 2020-08-03 13:27:42 -0400 | [diff] [blame] | 101 | hoverableButton.clicked() |
| 102 | } |
| 103 | onEntered: { |
| 104 | hoverableButtonBackground.color = onEnterColor |
| 105 | } |
| 106 | onExited: { |
| 107 | hoverableButtonBackground.color = onExitColor |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | } |