Skip to content

Commit

Permalink
[ApplicationWindow] Add extended mouse events for go back
Browse files Browse the repository at this point in the history
  • Loading branch information
neochapay committed Oct 11, 2023
1 parent 657f96b commit d1656db
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 6 deletions.
72 changes: 67 additions & 5 deletions src/controls/nemowindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,19 @@

NemoWindow::NemoWindow(QWindow* parent)
: QQuickWindow(parent)
, m_filter(new EditFilter())
, m_screen(screen())
, m_mousePressed(false)
, m_mouseEventTriggered(false)
, m_firstPoint(QPointF())
{
m_filter = new EditFilter();
this->installEventFilter(m_filter);
if (qEnvironmentVariableIsSet("NEMO_DISABLE_EXTENDED_EVENTS")) {
m_allowExtendedEvents = false;
} else {
m_allowExtendedEvents = true;
}

m_screen = this->screen();
installEventFilter(m_filter);
connect(m_screen, &QScreen::orientationChanged, this, &NemoWindow::orientationChanged);
}

Expand All @@ -37,14 +45,68 @@ Qt::ScreenOrientation NemoWindow::primaryOrientation() const
return m_screen->primaryOrientation();
}

void NemoWindow::keyPressEvent(QKeyEvent *event)
void NemoWindow::keyPressEvent(QKeyEvent* event)
{
switch (event->key()) {
case Qt::Key_Escape:
case Qt::Key_Return:
case Qt::Key_Backspace:
case Qt::Key_Left:
emit goBack();
if (allowExtendedEvents()) {
emit goBack();
}
break;
}
}

void NemoWindow::mousePressEvent(QMouseEvent*)
{
m_mousePressed = true;
}

void NemoWindow::mouseReleaseEvent(QMouseEvent*)
{
m_mousePressed = false;
m_firstPoint = QPointF();
if (m_mouseEventTriggered && allowExtendedEvents()) {
emit goBack();
}
}

void NemoWindow::mouseMoveEvent(QMouseEvent* event)
{
if (m_mousePressed) {
if (m_firstPoint.isNull()) {
m_firstPoint = event->points().first().position();
}

if (event->points().first().position().x() - m_firstPoint.x() > width() / 4
&& m_firstPoint.x() < width() / 4) {
m_mouseEventTriggered = true;
} else {
m_mouseEventTriggered = false;
}
}
}

bool NemoWindow::allowExtendedEvents() const
{
if (qEnvironmentVariableIsSet("NEMO_DISABLE_EXTENDED_EVENTS")) {
return false;
}
return m_allowExtendedEvents;
}

void NemoWindow::setAllowExtendedEvents(bool newAllowExtendedEvents)
{
if (qEnvironmentVariableIsSet("NEMO_DISABLE_EXTENDED_EVENTS") && newAllowExtendedEvents) {
qWarning() << "Extended events disabled in env";
return;
}

if (m_allowExtendedEvents == newAllowExtendedEvents) {
return;
}
m_allowExtendedEvents = newAllowExtendedEvents;
emit allowExtendedEventsChanged();
}
17 changes: 16 additions & 1 deletion src/controls/nemowindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,35 @@

class NemoWindow : public QQuickWindow {
Q_OBJECT
Q_PROPERTY(bool allowExtendedEvents READ allowExtendedEvents WRITE setAllowExtendedEvents NOTIFY allowExtendedEventsChanged)

public:
explicit NemoWindow(QWindow* parent = 0);
Qt::ScreenOrientation primaryOrientation() const;

bool allowExtendedEvents() const;
void setAllowExtendedEvents(bool newAllowExtendedEvents);

protected:
void keyPressEvent(QKeyEvent *event) override;
void keyPressEvent(QKeyEvent* event) override;
void mousePressEvent(QMouseEvent*) override;
void mouseReleaseEvent(QMouseEvent*) override;
void mouseMoveEvent(QMouseEvent* event) override;

signals:
void orientationChanged(Qt::ScreenOrientation orientation);
void goBack();

void allowExtendedEventsChanged();

private:
EditFilter* m_filter;
QScreen* m_screen;

bool m_mousePressed;
bool m_mouseEventTriggered;
QPointF m_firstPoint;
bool m_allowExtendedEvents;
};

#endif // NEMOWINDOW_H

0 comments on commit d1656db

Please sign in to comment.