Commit 97022f2f authored by Kai Uwe Broulik's avatar Kai Uwe Broulik 🍇
Browse files

[Notifications] Don't steal mouse clicks from TextEdit in SelectableLabel

Ensures built-in functionality like double- and triple-clicking text works.

Also lets us get rid of the custom text selection handling, replacing it with
an even filter class.

BUG: 431398
FIXED-IN: 5.22.0
parent 24796653
......@@ -4,6 +4,7 @@ set(notificationapplet_SRCS
notificationapplet.cpp
filemenu.cpp
globalshortcuts.cpp
texteditclickhandler.cpp
thumbnailer.cpp
)
......
......@@ -41,6 +41,7 @@
#include "filemenu.h"
#include "globalshortcuts.h"
#include "texteditclickhandler.h"
#include "thumbnailer.h"
NotificationApplet::NotificationApplet(QObject *parent, const QVariantList &data)
......@@ -51,6 +52,7 @@ NotificationApplet::NotificationApplet(QObject *parent, const QVariantList &data
const char uri[] = "org.kde.plasma.private.notifications";
qmlRegisterType<FileMenu>(uri, 2, 0, "FileMenu");
qmlRegisterType<GlobalShortcuts>(uri, 2, 0, "GlobalShortcuts");
qmlRegisterType<TextEditClickHandler>(uri, 2, 0, "TextEditClickHandler");
qmlRegisterType<Thumbnailer>(uri, 2, 0, "Thumbnailer");
qmlProtectModule(uri, 2);
s_typesRegistered = true;
......
......@@ -96,7 +96,7 @@ ColumnLayout {
|| (jobLoader.item && jobLoader.item.dragging)
property bool replying: false
signal bodyClicked(var mouse)
signal bodyClicked
signal closeClicked
signal configureClicked
signal dismissClicked
......
......@@ -179,8 +179,9 @@ PlasmaCore.Dialog {
timeout: timer.running ? timer.interval : 0
closable: true
onBodyClicked: {
if (area.acceptedButtons & mouse.button) {
if (area.acceptedButtons & Qt.LeftButton) {
area.clicked(null /*mouse*/);
}
}
......
......@@ -27,6 +27,8 @@ import org.kde.plasma.core 2.0 as PlasmaCore
import org.kde.plasma.extras 2.0 as PlasmaExtras
import org.kde.kirigami 2.11 as Kirigami
import org.kde.plasma.private.notifications 2.0 as Notifications
// NOTE This wrapper item is needed for QQC ScrollView to work
// In NotificationItem we just do SelectableLabel {} and then it gets confused
// as to which is the "contentItem"
......@@ -96,12 +98,20 @@ Item {
flick.contentY = cursorRectangle.y + cursorRectangle.height - flick.height
}
}
MouseArea {
property int selectionStart
property point mouseDownPos: Qt.point(-999, -999);
// Handle left-click
Notifications.TextEditClickHandler {
target: bodyText
onClicked: {
bodyTextContainer.clicked(null);
}
}
// Handle right-click and cursorShape
MouseArea {
anchors.fill: parent
acceptedButtons: Qt.RightButton | Qt.LeftButton
acceptedButtons: Qt.RightButton
cursorShape: {
if (bodyText.hoveredLink) {
return Qt.PointingHandCursor;
......@@ -111,58 +121,16 @@ Item {
return bodyTextContainer.cursorShape || Qt.IBeamCursor;
}
}
preventStealing: true // don't let us accidentally drag the Flickable
onPressed: {
if (mouse.button === Qt.RightButton) {
contextMenu = contextMenuComponent.createObject(bodyText);
contextMenu.link = bodyText.linkAt(mouse.x, mouse.y);
contextMenu.closed.connect(function() {
contextMenu.destroy();
contextMenu = null;
});
contextMenu.open(mouse.x, mouse.y);
return;
}
mouseDownPos = Qt.point(mouse.x, mouse.y);
selectionStart = bodyText.positionAt(mouse.x, mouse.y);
var pos = bodyText.positionAt(mouse.x, mouse.y);
// deselect() would scroll to the end which we don't want
bodyText.select(pos, pos);
}
onReleased: {
// emulate "onClicked"
var manhattanLength = Math.abs(mouseDownPos.x - mouse.x) + Math.abs(mouseDownPos.y - mouse.y);
if (manhattanLength <= Qt.styleHints.startDragDistance) {
var link = bodyText.linkAt(mouse.x, mouse.y);
if (link) {
Qt.openUrlExternally(link);
} else {
bodyTextContainer.clicked(mouse);
}
}
// emulate selection clipboard
if (bodyText.selectedText) {
plasmoid.nativeInterface.setSelectionClipboardText(bodyText.selectedText);
}
mouseDownPos = Qt.point(-999, -999);
}
// HACK to be able to select text whilst still getting all mouse events to the MouseArea
onPositionChanged: {
if (pressed) {
var pos = bodyText.positionAt(mouseX, mouseY);
if (selectionStart < pos) {
bodyText.select(selectionStart, pos);
} else {
bodyText.select(pos, selectionStart);
}
}
contextMenu = contextMenuComponent.createObject(bodyText);
contextMenu.link = bodyText.linkAt(mouse.x, mouse.y);
contextMenu.closed.connect(function() {
contextMenu.destroy();
contextMenu = null;
});
contextMenu.open(mouse.x, mouse.y);
}
}
}
......
/*
Copyright (C) 2021 Kai Uwe Broulik <kde@broulik.de>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "texteditclickhandler.h"
#include <QGuiApplication>
#include <QMouseEvent>
#include <QQuickItem>
#include <QStyleHints>
TextEditClickHandler::TextEditClickHandler(QObject *parent)
: QObject(parent)
{
}
TextEditClickHandler::~TextEditClickHandler() = default;
QQuickItem *TextEditClickHandler::target() const
{
return m_target.data();
}
void TextEditClickHandler::setTarget(QQuickItem *target)
{
if (m_target.data() == target) {
return;
}
if (m_target) {
m_target->removeEventFilter(this);
}
m_target = target;
m_target->installEventFilter(this);
Q_EMIT targetChanged(target);
}
bool TextEditClickHandler::eventFilter(QObject *watched, QEvent *event)
{
Q_ASSERT(watched == m_target.data());
if (event->type() == QEvent::MouseButtonPress) {
const auto *e = static_cast<QMouseEvent *>(event);
m_pressPos = e->pos();
} else if (event->type() == QEvent::MouseButtonRelease) {
const auto *e = static_cast<QMouseEvent *>(event);
if (m_pressPos.x() > -1 && m_pressPos.y() > -1
&& (m_pressPos - e->pos()).manhattanLength() < qGuiApp->styleHints()->startDragDistance()) {
Q_EMIT clicked();
}
}
return false;
}
/*
Copyright (C) 2021 Kai Uwe Broulik <kde@broulik.de>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#pragma once
#include <QObject>
#include <QPointer>
#include <QPointF>
class QQuickItem;
/**
* Simple event filter that emits a clicked() signal when clicking
* on a TextEdit while still letting the user select text like normal.
*
* It's just for this very specific use case, which is also why it has
* no acceptedButtons, MouseEvent argument on clicked signal, etc.
*/
class TextEditClickHandler : public QObject
{
Q_OBJECT
Q_PROPERTY(QQuickItem *target READ target WRITE setTarget NOTIFY targetChanged)
public:
explicit TextEditClickHandler(QObject *parent = nullptr);
~TextEditClickHandler() override;
QQuickItem *target() const;
void setTarget(QQuickItem *target);
Q_SIGNAL void targetChanged(QQuickItem *target);
bool eventFilter(QObject *watched, QEvent *event) override;
Q_SIGNALS:
void clicked();
private:
QPointer<QQuickItem> m_target;
QPointF m_pressPos{-1, -1};
};
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment