Skip to content
Snippets Groups Projects
MaterialButton.qml 5.11 KiB
Newer Older
Sébastien Blin's avatar
Sébastien Blin committed
/*
 * Copyright (C) 2020 by Savoir-faire Linux
 * Author: Sébastien blin <sebastien.blin@savoirfairelinux.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

import QtQuick 2.14
import QtQuick.Controls 2.14
import QtGraphicalEffects 1.14
import QtQuick.Window 2.14
Sébastien Blin's avatar
Sébastien Blin committed

Button {
    id: root

    property alias fontCapitalization: buttonText.font.capitalization
    property alias source: buttonImage.source
Sébastien Blin's avatar
Sébastien Blin committed
    property string toolTipText: ""
    property var color: "transparent"
    property var hoveredColor: undefined
    property var pressedColor: undefined
Sébastien Blin's avatar
Sébastien Blin committed
    property var outlined: false
    property string animatedImageSource: ""
    property var preferredWidth: 400
    property var preferredHeight: 36
    property var minimumIconTextSpacing: 10
    property var iconPreferredHeight: 18
    property var iconPreferredWidth: 18
    property bool enableElide: true
    property int elide: Text.ElideRight
    property int textActualWidth: buttonTextMetrics.boundingRect.width
Sébastien Blin's avatar
Sébastien Blin committed
    font.kerning: true
    font.pointSize: JamiTheme.textFontSize
    hoverEnabled: hoveredColor !== undefined
Sébastien Blin's avatar
Sébastien Blin committed

    contentItem: Item {
        Rectangle {
            anchors.fill: parent
            color: "transparent"
            AnimatedImage {
                id: buttonAnimatedImage

                anchors.verticalCenter: parent.verticalCenter
                anchors.left: parent.left
                anchors.leftMargin: JamiTheme.preferredMarginSize / 2
                height: iconPreferredHeight
                width: iconPreferredWidth

                source: animatedImageSource
                playing: true
                paused: false
                fillMode: Image.PreserveAspectFit
                mipmap: true
                visible: animatedImageSource.length !== 0
            }

                id: buttonImage

                anchors.verticalCenter: parent.verticalCenter
                anchors.left: parent.left
                anchors.leftMargin: JamiTheme.preferredMarginSize / 2

                height: iconPreferredHeight
                width: iconPreferredWidth

                visible: source.toString().length !== 0
                color: {
                    if (!outlined)
                        return "white"
                    if (hovered && root.hoveredColor)
                        return root.hoveredColor
                    if (checked && root.pressedColor)
                        return root.pressedColor
                    return root.color
Sébastien Blin's avatar
Sébastien Blin committed
                }
            }

            Text {
                id: buttonText

                anchors.centerIn: parent

                width: {
                    var iconWidth = (buttonAnimatedImage.visible || buttonImage.visible) ?
                                iconPreferredWidth : 0
                    return (parent.width / 2 - iconWidth -
                            JamiTheme.preferredMarginSize / 2 - minimumIconTextSpacing) * 2
                }

                TextMetrics {
                    id: buttonTextMetrics

                    font: buttonText.font
                    text: buttonText.text
                }

                text: root.text
                elide: enableElide ? root.elide : Text.ElideNone
                color: {
                    if (!outlined)
                        return "white"
                    if (hovered && root.hoveredColor)
                        return root.hoveredColor
                    if (checked && root.pressedColor)
                        return root.pressedColor
                    return root.color
                font: root.font
                horizontalAlignment: Text.AlignHCenter
Sébastien Blin's avatar
Sébastien Blin committed
            }
        }
    }

    ToolTip.delay: Qt.styleHints.mousePressAndHoldInterval
    ToolTip.visible: hovered && (toolTipText.length > 0)
    ToolTip.text: toolTipText

    background: Rectangle {
        id: backgroundRect
        anchors.fill: parent
        color: {
            if (outlined)
                return "transparent"
            if (hovered && root.hoveredColor)
                return root.hoveredColor
            if (checked && root.pressedColor)
                return root.pressedColor
            return root.color
        }
        border.color: {
            if (!outlined)
                return "transparent"
            if (hovered && root.hoveredColor)
                return root.hoveredColor
            if (checked && root.pressedColor)
                return root.pressedColor
            return root.color
        }
Sébastien Blin's avatar
Sébastien Blin committed
        radius: 4
    }