Skip to content

Commit

Permalink
Shadow note popups shouldn't take focus on click
Browse files Browse the repository at this point in the history
  • Loading branch information
mathesoncalum committed Feb 7, 2025
1 parent 58bb5b8 commit 3f3dabd
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 1 deletion.
15 changes: 15 additions & 0 deletions src/framework/uicomponents/view/popupview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ void PopupView::init()
m_window->init(engine, isDialog(), frameless());
m_window->setOnHidden([this]() { onHidden(); });
m_window->setContent(m_component, m_contentItem);
m_window->setTakeFocusOnClick(m_takeFocusOnClick);

// TODO: Can't use new `connect` syntax because the IPopupWindow::aboutToClose
// has a parameter of type QQuickCloseEvent, which is not public, so we
Expand Down Expand Up @@ -354,6 +355,11 @@ bool PopupView::activateParentOnClose() const
return m_activateParentOnClose;
}

bool PopupView::takeFocusOnClick() const
{
return m_takeFocusOnClick;
}

muse::ui::INavigationControl* PopupView::navigationParentControl() const
{
return m_navigationParentControl;
Expand Down Expand Up @@ -676,6 +682,15 @@ void PopupView::setActivateParentOnClose(bool activateParentOnClose)
emit activateParentOnCloseChanged(m_activateParentOnClose);
}

void PopupView::setTakeFocusOnClick(bool takeFocusOnClick)
{
if (m_takeFocusOnClick == takeFocusOnClick) {
return;
}
m_takeFocusOnClick = takeFocusOnClick;
emit takeFocusOnClickChanged(takeFocusOnClick);
}

QVariantMap PopupView::ret() const
{
return m_ret;
Expand Down
6 changes: 6 additions & 0 deletions src/framework/uicomponents/view/popupview.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ class PopupView : public QObject, public QQmlParserStatus, public Injectable, pu
Q_PROPERTY(
bool activateParentOnClose READ activateParentOnClose WRITE setActivateParentOnClose NOTIFY activateParentOnCloseChanged)

Q_PROPERTY(bool takeFocusOnClick READ takeFocusOnClick WRITE setTakeFocusOnClick NOTIFY takeFocusOnClickChanged)

//! NOTE Used for dialogs, but be here so that dialogs and just popups have one api
Q_PROPERTY(QString title READ title WRITE setTitle NOTIFY titleChanged)
Q_PROPERTY(QString objectId READ objectId WRITE setObjectId NOTIFY objectIdChanged)
Expand Down Expand Up @@ -151,6 +153,7 @@ class PopupView : public QObject, public QQmlParserStatus, public Injectable, pu
Placement placement() const;

bool activateParentOnClose() const;
bool takeFocusOnClick() const;

ui::INavigationControl* navigationParentControl() const;

Expand Down Expand Up @@ -201,6 +204,7 @@ public slots:
void setAnchorItem(QQuickItem* anchorItem);

void setActivateParentOnClose(bool activateParentOnClose);
void setTakeFocusOnClick(bool takeFocusOnClick);

signals:
void parentItemChanged();
Expand Down Expand Up @@ -234,6 +238,7 @@ public slots:
void anchorItemChanged(QQuickItem* anchorItem);

void activateParentOnCloseChanged(bool activateParentOnClose);
void takeFocusOnClickChanged(bool takeFocusOnClick);

void isContentReadyChanged();

Expand Down Expand Up @@ -295,6 +300,7 @@ public slots:
Placement m_placement = { Placement::Default };

bool m_activateParentOnClose = true;
bool m_takeFocusOnClick = true;
ui::INavigationControl* m_navigationParentControl = nullptr;
QString m_objectId;
QString m_title;
Expand Down
1 change: 1 addition & 0 deletions src/framework/uicomponents/view/popupwindow/ipopupwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class IPopupWindow : public QObject
virtual void forceActiveFocus() = 0;

virtual void setOnHidden(const std::function<void()>& callback) = 0;
virtual void setTakeFocusOnClick(bool takeFocusOnClick) = 0;

signals:
void aboutToClose(QQuickCloseEvent* event);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,11 @@ void PopupWindow_QQuickView::setOnHidden(const std::function<void()>& callback)
m_onHidden = callback;
}

void PopupWindow_QQuickView::setTakeFocusOnClick(bool takeFocusOnClick)
{
m_takeFocusOnClick = takeFocusOnClick;
}

bool PopupWindow_QQuickView::eventFilter(QObject* watched, QEvent* event)
{
if (watched == m_view) {
Expand All @@ -272,7 +277,7 @@ bool PopupWindow_QQuickView::eventFilter(QObject* watched, QEvent* event)
m_view->rootObject()->forceActiveFocus();
}

if (event->type() == QEvent::MouseButtonPress) {
if (m_takeFocusOnClick && event->type() == QEvent::MouseButtonPress) {
forceActiveFocus();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ class PopupWindow_QQuickView : public IPopupWindow, public muse::Injectable, pub
void forceActiveFocus() override;

void setOnHidden(const std::function<void()>& callback) override;
void setTakeFocusOnClick(bool takeFocusOnClick) override;

private:
bool eventFilter(QObject* watched, QEvent* event) override;
Expand All @@ -82,6 +83,7 @@ class PopupWindow_QQuickView : public IPopupWindow, public muse::Injectable, pub
bool m_resizable = false;
std::function<void()> m_onHidden;
bool m_activeFocusOnParentOnClose = false;
bool m_takeFocusOnClick = true;

QWindow* m_parentWindow = nullptr;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ StyledPopupView {

showArrow: false
openPolicies: PopupView.NoActivateFocus
takeFocusOnClick: false

padding: 0 // The popup will "steal" mouse events if the padding overlaps with the shadow note area
margins: 0
Expand Down

0 comments on commit 3f3dabd

Please sign in to comment.