// SPDX-FileCopyrightText: 2022 Tobias Fella // SPDX-FileCopyrightText: 2026 Matvey Shcherbinin // SPDX-License-Identifier: GPL-2.0-or-later // // Adapted from NeoChat's src/chatbar/EmojiPicker.qml. // // this replaces the hand-written grid over a hardcoded table that used to be // here. Categories, search over the CLDR short names, a recently-used list and // press-and-hold skin tones all come from upstream; the table behind them is // NeoChat's generated emojis.h, ported alongside it. // // Dropped from upstream: the Emojis/Stickers NavigationTabBar and everything it // switched to - ImagePacksModel, StickerModel, EmoticonFilterModel and the // currentRoom they hang off are Matrix image packs, which this project has no // equivalent of. What is left is the emoji half, unchanged in shape. // // the arrangement round it is Telegram's, from // Telegram/SourceFiles/chat_helpers/emoji_picker_overlay.cpp and // ui/controls/tabbed_search.cpp: a strip of recently used emoji along the top // (EmojiPickerOverlay::Strip, stickersEmojiPickerStripHeight), then the search // field and the categories on one row beside each other (SearchWithGroups puts // _field and _groups at the same y, the strip of icons starting to the right of // the field at _fadeLeftStart + defaultFieldWidth), and the grid under both. // Upstream lists the recently used emoji in that strip rather than as a tab of // its own, so the first of the model's categories is left out of the tabs here // too. Each category also keeps the scroll top it was left at, the way // TabbedSelector::Tab::saveScrollTop does, while a new search starts at the top // of the grid, the way EmojiPickerOverlay::refreshGridEmojis does. // // this is the panel, not the window round it: EmojiPopup.qml is what the // composer opens, the same way upstream keeps EmojiDialog separate. pragma ComponentBehavior: Bound import QtQuick import QtQuick.Controls as QQC2 import QtQuick.Layouts import org.kde.kirigami as Kirigami import koutnet.app ColumnLayout { id: root readonly property var currentEmojiModel: EmojiModel.categories readonly property alias categoryCount: categories.count // History is the strip above, not a tab, so the tabs start one along. readonly property int tabCount: Math.max(0, root.currentEmojiModel.length - 1) readonly property var currentCategory: categories.currentIndex >= 0 ? root.currentEmojiModel[categories.currentIndex + 1].category : EmojiModel.Smileys // Telegram's grid is a square of stickersEmojiPickerItemSize with // stickersEmojiPickerItemSkip of room around each, and the columns are what // that leaves over: (width + skip) / (size + skip). readonly property int cellSize: Math.round(Kirigami.Units.gridUnit * 1.7) readonly property int cellSkip: Kirigami.Units.smallSpacing readonly property int categoryIconSize: Math.round(Kirigami.Units.gridUnit * 2.5) // the grid's model, named so that the grid is given a new one only after // currentCategory has been announced, which is what lets showCategory() read // the top of the category being left before the grid has lost it readonly property var gridModel: searchField.text.length === 0 ? EmojiModel.emojis(root.currentCategory) : EmojiModel.filterModel(searchField.text, false) // The closing cross, asked for by the shape that hosts this picker: the // side panel wants one, because its opener never closes it, and the // pop-up over the composer does not, because a click outside is its way // back. Built into the search row, where it stands over nothing. property bool showCloser: false signal closeRequested() // where each category was last left, keyed by its category so it survives // the model being handed over again property var scrollTops: ({}) // the category the grid is actually showing, which is the one the scroll top // belongs to while a search is in the way of the tabs. A number, not a // string: currentCategory is a number out of the model, and QML's === does // not turn "3" into 3, so a string here would compare unequal to everything // and the guard below would never hold. property var shownCategory: -1 signal chosen(string emoji) onActiveFocusChanged: if (activeFocus) { searchField.forceActiveFocus(); } spacing: 0 // Telegram's strip, and empty rather than shown when nothing has been picked // yet - there is no history to put in it. ListView { id: recent visible: count > 0 Layout.fillWidth: true Layout.preferredHeight: root.cellSize Layout.topMargin: Kirigami.Units.smallSpacing Layout.leftMargin: Kirigami.Units.smallSpacing Layout.rightMargin: Kirigami.Units.smallSpacing clip: true orientation: ListView.Horizontal spacing: root.cellSkip interactive: false model: EmojiModel.emojis(EmojiModel.History) // no tone variants here: the strip is a row of what was picked, and // holding one open to recolour it is a grid's job delegate: EmojiDelegate { required property var modelData width: root.cellSize height: root.cellSize emoji: modelData.unicode name: modelData.shortName description: modelData.description Keys.onEnterPressed: clicked() Keys.onReturnPressed: clicked() onClicked: root.chosen(modelData.unicode) } } RowLayout { id: searchRow Layout.fillWidth: true Layout.margins: Kirigami.Units.smallSpacing spacing: Kirigami.Units.smallSpacing // the field and the categories share a row, the way SearchWithGroups puts // the field and the groups strip on the same line Kirigami.SearchField { id: searchField Layout.preferredWidth: Math.round(Kirigami.Units.gridUnit * 7) Layout.maximumWidth: Math.round(Kirigami.Units.gridUnit * 12) // the focus is managed by the parent and we don't want to use the // standard shortcut as it could block other SearchFields from using it. focusSequence: "" onTextChanged: if (text.length === 0) { root.restoreScrollTop(); } else { emojiGrid.contentY = 0; } Keys.onDownPressed: if (emojiGrid.count > 0) { emojiGrid.forceActiveFocus(); } } // The host's closing cross, last in the row: standing in furniture // rather than over content, so it is always exactly where a closer // belongs. QQC2.ToolButton { id: closeChip objectName: "emojiCloseChip" visible: root.showCloser Layout.alignment: Qt.AlignVCenter display: QQC2.AbstractButton.IconOnly icon.name: "window-close" text: i18nc("@action:button close the emoji panel", "Close the emoji panel") QQC2.ToolTip.visible: hovered QQC2.ToolTip.delay: Kirigami.Units.toolTipDelay QQC2.ToolTip.text: text Accessible.name: text onClicked: root.closeRequested() } ListView { id: categories Layout.fillWidth: true Layout.preferredHeight: root.categoryIconSize clip: true focus: true orientation: ListView.Horizontal spacing: Kirigami.Units.smallSpacing KeyNavigation.down: emojiGrid.count > 0 ? emojiGrid : categories KeyNavigation.tab: emojiGrid.count > 0 ? emojiGrid : categories keyNavigationEnabled: true keyNavigationWraps: true Keys.forwardTo: searchField // a wheel over the categories scrolls them, not the grid below interactive: width !== contentWidth model: root.tabCount visible: count !== 0 // the chosen tab is brought into view, which is what // SearchWithGroups::scrollGroupsToIcon does after a category is picked onCurrentIndexChanged: if (currentIndex >= 0) { positionViewAtIndex(currentIndex, ListView.Contain) } delegate: Kirigami.NavigationTabButton { id: categoryButton required property int index width: root.categoryIconSize height: width checked: categories.currentIndex === index Accessible.name: root.currentEmojiModel[index + 1].name QQC2.ToolTip.text: root.currentEmojiModel[index + 1].name QQC2.ToolTip.delay: Kirigami.Units.toolTipDelay QQC2.ToolTip.visible: hovered // A contentItem of its own, for the reason ReactionPicker gives: // the button draws its text in the interface font, and a desktop // UI font has no emoji in it, so the categories came out as a row // of empty tabs. An icon would not do either - it looks its name // up among the themed ones. contentItem: QQC2.Label { text: root.currentEmojiModel[index + 1].emoji horizontalAlignment: Text.AlignHCenter verticalAlignment: Text.AlignVCenter font.family: "Noto Color Emoji, Segoe UI Emoji, Apple Color Emoji, emoji" font.pixelSize: Math.round(height * 0.7) elide: Text.ElideNone wrapMode: Text.NoWrap textFormat: Text.PlainText } onClicked: { categories.currentIndex = index; categories.forceActiveFocus(); } } } } EmojiGrid { id: emojiGrid targetIconSize: root.cellSize cellSkip: root.cellSkip model: root.gridModel Layout.fillWidth: true Layout.fillHeight: true onChosen: unicode => root.chosen(unicode) header: searchField } // each category keeps the scroll top it was left at, and the grid is put back // where that category was rather than at the top of the one it came from. // The top is taken from here rather than from a handler on the grid's model // because this runs first: a grid handed a shorter model puts itself back at // the top there and then, and a top read after that is the top of the // category just arrived at rather than the one being left behind. function showCategory() { if (root.currentCategory === root.shownCategory) { return; } root.rememberScrollTop(); root.shownCategory = root.currentCategory; root.restoreScrollTop(); } function rememberScrollTop() { if (root.shownCategory >= 0) { root.scrollTops[root.shownCategory] = emojiGrid.contentY; } } // after the model has been through the grid again, or the height it restores // to is not there yet and the assignment is quietly clamped back to the top function restoreScrollTop() { const saved = root.scrollTops[root.shownCategory]; Qt.callLater(() => { emojiGrid.contentY = saved === undefined ? 0 : saved; }); } onCurrentCategoryChanged: showCategory() function clearSearchField() { searchField.text = ""; } }