matrixion/qml/emoji/StickerPicker.qml

161 lines
5.7 KiB
QML
Raw Permalink Normal View History

2021-07-15 21:37:52 +03:00
// SPDX-FileCopyrightText: 2021 Nheko Contributors
// SPDX-FileCopyrightText: 2022 Nheko Contributors
2021-07-15 21:37:52 +03:00
// SPDX-License-Identifier: GPL-3.0-or-later
import "../"
import QtQuick 2.9
import QtQuick.Controls 2.3
import QtQuick.Layouts 1.3
import im.nheko
2021-07-15 21:37:52 +03:00
Menu {
id: stickerPopup
property var callback
property var colors
2022-04-11 05:18:16 +03:00
property real highlightHue: timelineRoot.palette.highlight.hslHue
property real highlightLight: timelineRoot.palette.highlight.hslLightness
2022-04-16 03:13:01 +03:00
property real highlightSat: timelineRoot.palette.highlight.hslSaturation
property alias model: gridView.model
property string roomid
2021-07-15 21:37:52 +03:00
readonly property int stickerDim: 128
readonly property int stickerDimPad: 128 + Nheko.paddingSmall
readonly property int stickersPerRow: 3
2022-04-16 03:13:01 +03:00
property var textArea
2021-07-15 21:37:52 +03:00
2021-07-19 18:45:55 +03:00
function show(showAt, roomid_, callback) {
2021-07-15 21:37:52 +03:00
console.debug("Showing sticker picker");
2021-07-19 18:45:55 +03:00
roomid = roomid_;
2021-07-15 21:37:52 +03:00
stickerPopup.callback = callback;
popup(showAt ? showAt : null);
}
bottomPadding: 1
2022-04-16 03:13:01 +03:00
closePolicy: Popup.CloseOnEscape | Popup.CloseOnPressOutside
focus: true
2021-07-15 21:37:52 +03:00
leftPadding: 1
2022-04-16 03:13:01 +03:00
margins: 0
2021-07-15 21:37:52 +03:00
modal: true
2022-04-16 03:13:01 +03:00
rightPadding: 1
2021-07-15 21:37:52 +03:00
width: stickersPerRow * stickerDimPad + 20
Rectangle {
2022-04-11 05:18:16 +03:00
color: timelineRoot.palette.window
2021-07-15 21:37:52 +03:00
height: columnView.implicitHeight + 4
width: stickersPerRow * stickerDimPad + 20
ColumnLayout {
id: columnView
anchors.bottom: parent.bottom
anchors.left: parent.left
2022-04-16 03:13:01 +03:00
anchors.leftMargin: 3
2021-07-15 21:37:52 +03:00
anchors.right: parent.right
2022-04-16 03:13:01 +03:00
anchors.rightMargin: 3
2021-07-15 21:37:52 +03:00
anchors.topMargin: 2
2022-04-16 03:13:01 +03:00
spacing: 0
2021-07-15 21:37:52 +03:00
// Search field
TextField {
id: emojiSearch
Layout.preferredWidth: stickersPerRow * stickerDimPad + 20 - 6
2022-04-16 03:13:01 +03:00
Layout.topMargin: 3
2021-07-15 21:37:52 +03:00
background: null
2022-04-11 05:18:16 +03:00
color: timelineRoot.palette.text
2022-04-16 03:13:01 +03:00
palette: timelineRoot.palette
2021-07-15 21:37:52 +03:00
placeholderText: qsTr("Search")
2022-04-16 03:13:01 +03:00
placeholderTextColor: timelineRoot.palette.placeholderText
2021-07-15 21:37:52 +03:00
rightPadding: clearSearch.width
2022-04-16 03:13:01 +03:00
selectByMouse: true
2021-07-15 21:37:52 +03:00
onTextChanged: searchTimer.restart()
onVisibleChanged: {
if (visible)
forceActiveFocus();
else
clear();
2021-07-15 21:37:52 +03:00
}
Timer {
id: searchTimer
interval: 350 // tweak as needed?
2022-04-16 03:13:01 +03:00
2021-07-15 21:37:52 +03:00
onTriggered: stickerPopup.model.searchString = emojiSearch.text
}
ToolButton {
id: clearSearch
2022-04-16 03:13:01 +03:00
background: null
2021-07-15 21:37:52 +03:00
focusPolicy: Qt.NoFocus
hoverEnabled: true
2022-04-16 03:13:01 +03:00
icon.source: "image://colorimage/:/icons/icons/ui/round-remove-button.svg?" + (clearSearch.hovered ? timelineRoot.palette.highlight : timelineRoot.palette.placeholderText)
visible: emojiSearch.text !== ''
onClicked: emojiSearch.clear()
2021-07-15 21:37:52 +03:00
anchors {
right: parent.right
2022-04-16 03:13:01 +03:00
verticalCenter: parent.verticalCenter
2021-07-15 21:37:52 +03:00
}
// clear the default hover effects.
Image {
height: parent.height - 2 * Nheko.paddingSmall
2022-04-15 06:53:41 +03:00
source: "image://colorimage/:/icons/icons/ui/round-remove-button.svg?" + (clearSearch.hovered ? timelineRoot.palette.highlight : timelineRoot.palette.placeholderText)
2022-04-16 03:13:01 +03:00
width: height
2021-07-15 21:37:52 +03:00
anchors {
margins: Nheko.paddingSmall
2022-04-16 03:13:01 +03:00
right: parent.right
verticalCenter: parent.verticalCenter
2021-07-15 21:37:52 +03:00
}
}
}
}
// emoji grid
GridView {
id: gridView
2022-04-16 03:13:01 +03:00
Layout.leftMargin: 4
2021-07-15 21:37:52 +03:00
Layout.preferredHeight: cellHeight * 3.5
Layout.preferredWidth: stickersPerRow * stickerDimPad + 20
boundsBehavior: Flickable.StopAtBounds
2022-04-16 03:13:01 +03:00
cacheBuffer: 500
cellHeight: stickerDimPad
cellWidth: stickerDimPad
2021-07-15 21:37:52 +03:00
clip: true
currentIndex: -1 // prevent sorting from stealing focus
2022-04-16 03:13:01 +03:00
model: roomid ? TimelineManager.completerFor("stickers", roomid) : null
2021-07-15 21:37:52 +03:00
2022-04-16 03:13:01 +03:00
ScrollBar.vertical: ScrollBar {
id: emojiScroll
}
2021-07-19 15:57:10 +03:00
2021-07-15 21:37:52 +03:00
// Individual emoji
delegate: AbstractButton {
ToolTip.text: ":" + model.shortcode + ": - " + model.body
ToolTip.visible: hovered
2022-04-16 03:13:01 +03:00
height: stickerDim
hoverEnabled: true
width: stickerDim
2021-07-15 21:37:52 +03:00
background: Rectangle {
anchors.fill: parent
2022-04-11 05:18:16 +03:00
color: hovered ? timelineRoot.palette.highlight : 'transparent'
2021-07-15 21:37:52 +03:00
radius: 5
}
2022-04-16 03:13:01 +03:00
contentItem: Image {
fillMode: Image.PreserveAspectFit
height: stickerDim
source: model.url.replace("mxc://", "image://MxcImage/") + "?scale"
width: stickerDim
}
2021-07-15 21:37:52 +03:00
2022-04-16 03:13:01 +03:00
// TODO: maybe add favorites at some point?
onClicked: {
console.debug("Picked " + model.shortcode);
stickerPopup.close();
callback(model.originalRow);
}
2021-07-15 21:37:52 +03:00
}
}
}
}
}