// SPDX-FileCopyrightText: 2026 Matvey Shcherbinin // SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL import QtQuick import QtQuick.Controls as QQC2 import org.kde.kirigami as Kirigami import koutnet.app // separate from EmojiPicker because that one is a port and this one is not: keeping // them apart is what stops the port from being edited every time the popup wants a // different width. QQC2.Popup { id: root signal picked(string emoji) // See Main.qml: reparented into the overlay, which is its own theme chain. Kirigami.Theme.inherit: false // What the popup sits above when there is something to sit above - the // composer's emoji button in the chat, the status field in the settings. // A popup in the overlay cannot reach its opener through anchors, so the // opener is named here and its position is measured, and an unset one falls // back to the far corner of the window. property Item anchorItem: null // Measured through the popup's own parent, which is the overlay it was // reparented into and is never null. It used to be measured through // Overlay.overlay instead, which is an attached property that answers for // whichever window is being asked and is nothing at all in some of them; a // measurement that comes back empty takes the width and the height with it, // and a popup with no size is a popup that never appears. // // The opener's coordinates are read alongside the mapToItem call on purpose: // the call itself registers no dependency, so a binding that mentions only // anchorItem.width would not re-measure when the opener merely moved - the // page row moving its pages during a resize being exactly that case. Naming // the coordinates puts them back in the set of properties the binding watches. // // They are named, not added. They used to be added to what mapToItem reports, // which counts the opener's place inside its own row a second time: the // button's x and y are where it sits in the composer, and mapToItem has // already put it in the overlay's coordinates. The panel was that far from // the button that opened it, in the direction the button had been moved from // the top left, which put it in the corner. readonly property real openerLeft: { if (!root.anchorItem || !root.parent) { return root.parent ? root.parent.width : 0 } root.anchorItem.x root.anchorItem.y return root.anchorItem.mapToItem(root.parent, 0, 0).x } readonly property real openerTop: { if (!root.anchorItem || !root.parent) { return root.parent ? root.parent.height : 0 } root.anchorItem.x root.anchorItem.y return root.anchorItem.mapToItem(root.parent, 0, 0).y } readonly property real openerRight: root.anchorItem && root.parent ? root.openerLeft + root.anchorItem.width : (root.parent ? root.parent.width : 0) readonly property real openerHeight: root.anchorItem ? root.anchorItem.height : 0 readonly property real overlayWidth: root.parent ? root.parent.width : 0 readonly property real overlayHeight: root.parent ? root.parent.height : 0 // The panel keeps Telegram's own ten pixel margin off the window on every // side, which is what that margin is for and not just something to clamp // against: emojiPanMargins in chat_helpers/chat_helpers.style, on the same // eighteen pixel grid unit as the figures below. readonly property real panelMargin: Kirigami.Units.gridUnit * 10 / 18 // Telegram's emoji panel is a fixed 345 wide whatever the window is, and grows // out of the room it is given rather than out of the button. The figures are // its own - emojiPanWidth, emojiPanMinHeight, emojiPanMaxHeight and // emojiPanHeightRatio in chat_helpers/chat_helpers.style, applied by // TabbedPanel::updateContentHeight() - read off an 18 pixel grid unit and kept // in ours, so the panel is the same size here as it is there at the same // scale. A window too narrow for it gets it as far in as it goes. readonly property real panelWidth: Math.min(Kirigami.Units.gridUnit * 19.2, root.overlayWidth - root.panelMargin * 2) // Above the opener, where the eye already is, and below it where there is no // room: the composer sits at the bottom of a chat, so a panel opened for it // is one that has to be tall enough to fit in the conversation above. readonly property real roomAbove: root.openerTop - root.panelMargin readonly property real roomBelow: root.overlayHeight - root.openerTop - root.openerHeight - root.panelMargin readonly property bool opensBelow: root.roomBelow > root.roomAbove readonly property real room: root.opensBelow ? root.roomBelow : root.roomAbove // Three quarters of the room, as Telegram asks for, between the two bounds it // clamps that to, and never taller than the room itself so the panel cannot // end up hanging off the side of the window. readonly property real panelHeight: Math.max(Math.min(Kirigami.Units.gridUnit * 15.4, root.room), Math.min(Kirigami.Units.gridUnit * 35.6, Math.round(root.room * 0.75))) // The panel hangs off the trailing edge of the button, the way Telegram's // hangs off the trailing edge of the compose row: aligned by its leading edge // instead, as this was, a panel many times the width of the button started well // to the left of it, under the attachment and the field, which is what put it // in the corner. A button nearer the window's edge than the panel is wide does // not drag the panel off the edge after it. readonly property real panelX: Math.max(root.panelMargin, Math.min(root.openerRight - root.width, root.overlayWidth - root.width - root.panelMargin)) readonly property real panelY: { const above = root.openerTop - root.height - root.panelMargin const below = root.openerTop + root.openerHeight + root.panelMargin const wanted = root.opensBelow ? below : above return Math.max(root.panelMargin, Math.min(root.overlayHeight - root.height - root.panelMargin, wanted)) } parent: QQC2.Overlay.overlay x: root.panelX y: root.panelY modal: true focus: true width: root.panelWidth height: root.panelHeight padding: Kirigami.Units.smallSpacing // Otherwise the next open shows the tail of somebody else's search. onOpened: { picker.clearSearchField() picker.forceActiveFocus() } contentItem: EmojiPicker { id: picker onChosen: (emoji) => { root.picked(emoji) root.close() } } }