// SPDX-FileCopyrightText: 2026 Matvey Shcherbinin // SPDX-License-Identifier: BSD-2-Clause #include #include #include #include #include #include #include // Where the emoji panel is put, and how big it is. // // Telegram's panel is a fixed 345 wide, takes three quarters of the room the // compose button leaves it, keeps a 10 pixel margin off the window, and hangs // off the trailing edge of the compose row (see chat_helpers.style and // TabbedPanel). KOutNet's was sized off the window and aligned by the button's // left edge, which put a panel many times the button's width in the corner. // // This is a reduction of EmojiPopup.qml, the same way EmojiPanelScrollTest is a // reduction of EmojiPicker.qml: the real popup cannot be created on its own. // The geometry is copied unchanged and Kirigami is imported for real; keep this // in step with EmojiPopup.qml if the figures change. static const char *sceneQml = R"QML( import QtQuick import QtQuick.Controls as QQC2 import org.kde.kirigami as Kirigami // stands in for the overlay the panel is parented to QQC2.ApplicationWindow { id: window width: 800 height: 600 visible: true // read back by the test, so that the figures below are compared against the // unit they were written in rather than one assumed for them readonly property real gridUnit: Kirigami.Units.gridUnit // stands in for the composer's emoji button Item { id: opener objectName: "opener" width: 16 height: 16 } Item { id: overlay objectName: "overlay" anchors.fill: parent } // the popup itself, with the popup's own id: root.width and root.height are // the panel's own size, and the window behind them is root.parent Rectangle { id: root objectName: "panel" color: "transparent" parent: overlay // its own margin off the window, not merely something to clamp against readonly property real panelMargin: Kirigami.Units.gridUnit * 10 / 18 // the same measurement EmojiPopup.qml takes of the button it was opened // from: the coordinates named for the dependency they register and not // added to the result, for the reason given there readonly property real openerTop: { opener.x opener.y return opener.mapToItem(root.parent, 0, 0).y } readonly property real openerRight: { opener.x opener.y return opener.mapToItem(root.parent, 0, 0).x + opener.width } readonly property real openerHeight: opener.height readonly property real overlayWidth: root.parent ? root.parent.width : 0 readonly property real overlayHeight: root.parent ? root.parent.height : 0 // 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. Its // figures are its own, 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) 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; a button nearer the // window's edge than the panel is wide does not drag it off 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)) } width: root.panelWidth height: root.panelHeight x: root.panelX y: root.panelY } } )QML"; class EmojiPopupPositionTest : public QObject { Q_OBJECT static void settle(int rounds = 4) { for (int i = 0; i < rounds; ++i) { QCoreApplication::processEvents(QEventLoop::AllEvents, 20); } } QTemporaryDir *m_dir = nullptr; QQmlApplicationEngine *m_view = nullptr; QObject *m_scene = nullptr; QVariant property(const char *name) const { return m_scene->property(name); } QQuickItem *item(const char *name) const { return m_scene->findChild(QString::fromLatin1(name)); } QObject *panel() const { return item("panel"); } // The grid unit every figure in the panel is written in, read off Kirigami // rather than assumed: a different unit scale has to move the panel with it, // since the figures are Telegram's divided by eighteen to begin with. qreal gridUnit() const { return m_scene->property("gridUnit").toReal(); } // Telegram's figures in our units. Eighteen times 19.2 is 345.6, which is // neither a whole number of pixels nor of grid units, so the panel's sizes // are reals throughout and the assertions are to within one of them. qreal telegramWidth() const { return gridUnit() * 19.2; } qreal telegramMaxHeight() const { return gridUnit() * 35.6; } qreal telegramMinHeight() const { return gridUnit() * 15.4; } qreal margin() const { return panel()->property("panelMargin").toReal(); } void near(qreal actual, qreal wanted) { QVERIFY2(qAbs(actual - wanted) < 0.5, qPrintable(QStringLiteral("the panel has %1, Telegram asks for %2").arg(actual).arg(wanted))); } // Put the window at a given size and the button where the composer leaves // it: on the bottom row, at the trailing edge of the row. void place(int windowWidth, int windowHeight, int buttonLeft, int buttonTop, int buttonSize = 16) { QVERIFY(m_scene->setProperty("width", windowWidth)); QVERIFY(m_scene->setProperty("height", windowHeight)); auto *opener = item("opener"); QVERIFY(opener); QVERIFY(opener->setProperty("x", buttonLeft)); QVERIFY(opener->setProperty("y", buttonTop)); QVERIFY(opener->setProperty("width", buttonSize)); QVERIFY(opener->setProperty("height", buttonSize)); settle(); } qreal panelWidth() const { return panel()->property("width").toReal(); } qreal panelHeight() const { return panel()->property("height").toReal(); } // the panel's trailing edge, which is the edge Telegram lines the compose // row's up with qreal panelRight() const { return panel()->property("x").toReal() + panelWidth(); } qreal panelLeft() const { return panel()->property("x").toReal(); } qreal panelTop() const { return panel()->property("y").toReal(); } bool opensBelow() const { return panel()->property("opensBelow").toBool(); } private Q_SLOTS: void initTestCase() { m_dir = new QTemporaryDir; QVERIFY(m_dir->isValid()); QFile scene(m_dir->filePath(QStringLiteral("scene.qml"))); QVERIFY(scene.open(QIODevice::WriteOnly)); QCOMPARE(scene.write(sceneQml), qint64(qstrlen(sceneQml))); } void cleanupTestCase() { delete m_dir; } void init() { delete m_view; m_view = new QQmlApplicationEngine; QQmlComponent c(m_view, QUrl::fromLocalFile(m_dir->filePath(QStringLiteral("scene.qml")))); if (c.isError()) { for (const auto &e : c.errors()) { qWarning("%s", qPrintable(e.toString())); } QFAIL("the scene does not load"); } m_scene = c.create(); QVERIFY(m_scene); m_scene->setParent(m_view); auto *window = qobject_cast(m_scene); QVERIFY(window); window->show(); settle(); QVERIFY(window->isVisible()); } void cleanup() { delete m_view; m_view = nullptr; m_scene = nullptr; } void thePanelIsTeletgramsWidth() { place(1200, 900, 1100, 800); near(panelWidth(), telegramWidth()); // and a roomier window does not make it any wider, which is what sizing // it off the window used to do place(1600, 1000, 1500, 900); near(panelWidth(), telegramWidth()); } void aWindowTooNarrowForThePanelStillShowsIt() { // a fifth of a Telegram panel's width: the panel is pulled in to the // margins on both sides rather than left hanging off the window const auto windowWidth = qRound(telegramWidth() / 5); place(windowWidth, 600, windowWidth - 20, 520); QVERIFY(panelWidth() < telegramWidth()); near(panelLeft(), margin()); near(panelRight(), windowWidth - margin()); } void thePanelTakesThreeQuartersOfTheRoomTheButtonLeaves() { // the button on the bottom row of a six hundred of window, so the panel // opens above it out of the conversation place(800, 600, 700, 560); QVERIFY(!opensBelow()); const auto room = 560 - margin(); near(panelHeight(), qRound(room * 0.75)); // which here is inside both of Telegram's bounds, or the clamp would // have decided the size instead QVERIFY(panelHeight() > telegramMinHeight()); QVERIFY(panelHeight() < telegramMaxHeight()); } void thePanelStopsAtTeletgramsUpperBound() { // three quarters of a tall window's room is more than Telegram's 640, and // the panel takes 640 of it rather than the lot place(1000, 1800, 900, 1740); near(panelHeight(), telegramMaxHeight()); } void thePanelStartsAtTeletgramsLowerBound() { // and three quarters of a small room is less than Telegram's 278, which // the panel is not shrunk below place(800, 420, 700, 370); near(panelHeight(), telegramMinHeight()); } void thePanelIsNoTallerThanTheRoomItWasGiven() { // a window with barely a strip of conversation above the button: the // three quarters of that is less than Telegram's lower bound, so the room // decides, and the panel cannot hang off the top of the window place(800, 200, 700, 150); const auto room = 150 - margin(); near(panelHeight(), room); QVERIFY(panelHeight() < telegramMinHeight()); // a panel as tall as the room opens hard against the button, and the // margin is what is left above it near(panelTop(), margin()); } void thePanelHangsOffTheTrailingEdgeOfTheButton_data() { QTest::addColumn("buttonLeft"); QTest::addColumn("expectedRight"); // Telegram's moveHorizontally takes the compose row's trailing edge, and // a row narrower than the panel does not pull the panel away from the // window's edge after it QTest::newRow("button at the window's edge") << 748 << 788; QTest::newRow("button just inside the edge") << 700 << 740; QTest::newRow("button nearer the middle") << 400 << 440; } void thePanelHangsOffTheTrailingEdgeOfTheButton() { QFETCH(int, buttonLeft); QFETCH(int, expectedRight); place(800, 600, buttonLeft, 560, 40); near(panelRight(), expectedRight); } void thePanelIsHeldInByTheLeftMargin() { // a button in the middle of the row: the panel's trailing edge would put // its leading edge off the side of the window, and the margin holds it // where the button then no longer lines up with place(800, 600, 200, 560, 40); near(panelLeft(), margin()); near(panelRight(), margin() + panelWidth()); } void thePanelOpensAboveTheButton() { // the composer is on the bottom row, so the panel is above it with the // margin between the two place(800, 600, 700, 560, 40); QVERIFY(!opensBelow()); near(panelTop() + panelHeight() + margin(), 560); } void thePanelFlipsBelowWhenTheButtonIsNearTheTop() { // a button on the top row: almost nothing above it and the whole window // below, so the panel goes down rather than up place(800, 600, 700, 40, 40); QVERIFY(opensBelow()); near(panelTop(), 40 + 40 + margin()); } }; QTEST_MAIN(EmojiPopupPositionTest) #include "EmojiPopupPositionTest.moc"