From 454e8f887a4bcfa95344a1b7ea5346ce5ea6f731 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Haris=20Gu=C5=A1i=C4=87?= Date: Mon, 11 Oct 2021 03:27:23 +0200 Subject: [PATCH] Workaround for focus problem (#1958) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Haris Gušić --- src/widgets/capture/capturetoolbutton.cpp | 1 + src/widgets/capture/capturewidget.cpp | 40 +++++++++++++++-------- src/widgets/capture/capturewidget.h | 3 +- 3 files changed, 30 insertions(+), 14 deletions(-) diff --git a/src/widgets/capture/capturetoolbutton.cpp b/src/widgets/capture/capturetoolbutton.cpp index a54d8595..37205e48 100644 --- a/src/widgets/capture/capturetoolbutton.cpp +++ b/src/widgets/capture/capturetoolbutton.cpp @@ -97,6 +97,7 @@ QIcon CaptureToolButton::icon() const void CaptureToolButton::mousePressEvent(QMouseEvent* e) { + activateWindow(); if (e->button() == Qt::LeftButton) { emit pressedButton(this); emit pressed(); diff --git a/src/widgets/capture/capturewidget.cpp b/src/widgets/capture/capturewidget.cpp index c2408268..19b049d4 100644 --- a/src/widgets/capture/capturewidget.cpp +++ b/src/widgets/capture/capturewidget.cpp @@ -135,7 +135,7 @@ CaptureWidget::CaptureWidget(uint id, move(currentScreen->geometry().x(), currentScreen->geometry().y()); resize(currentScreen->size()); #else -// Call cmake with -DFLAMESHOT_DEBUG_CAPTURE=true to enable easier debugging +// Call cmake with -DFLAMESHOT_DEBUG_CAPTURE=ON to enable easier debugging #if !defined(FLAMESHOT_DEBUG_CAPTURE) setWindowFlags(Qt::BypassWindowManagerHint | Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint | Qt::Tool); @@ -399,8 +399,17 @@ void CaptureWidget::paintEvent(QPaintEvent* paintEvent) // draw inactive region drawInactiveRegion(&painter); - if (m_configError || m_configErrorResolved) { - drawConfigErrorMessage(&painter); + if (!isActiveWindow()) { + drawErrorMessage( + tr("Flameshot has lost focus. Keyboard shortcuts won't " + "work until you click somewhere."), + &painter); + } else if (m_configError) { + drawErrorMessage(ConfigHandler().errorMessage(), &painter); + } else if (m_configErrorResolved) { + drawErrorMessage(tr("Configuration error resolved. Launch `flameshot " + "gui` again to apply it."), + &painter); } } @@ -497,6 +506,7 @@ int CaptureWidget::selectToolItemAtPos(const QPoint& pos) void CaptureWidget::mousePressEvent(QMouseEvent* e) { + activateWindow(); m_startMove = false; m_startMovePos = QPoint(); m_mousePressedPos = e->pos(); @@ -783,6 +793,16 @@ void CaptureWidget::moveEvent(QMoveEvent* e) m_context.widgetOffset = mapToGlobal(QPoint(0, 0)); } +void CaptureWidget::changeEvent(QEvent* e) +{ + if (e->type() == QEvent::ActivationChange) { + QPoint bottomRight = rect().bottomRight(); + // Update the message in the bottom right corner. A rough estimate is + // used for the update rect + update(QRect(bottomRight - QPoint(1000, 200), bottomRight)); + } +} + void CaptureWidget::initContext(const QString& savePath, bool fullscreen) { m_context.color = m_config.drawColor(); @@ -1532,19 +1552,13 @@ QRect CaptureWidget::paddedUpdateRect(const QRect& r) const } } -void CaptureWidget::drawConfigErrorMessage(QPainter* painter) +void CaptureWidget::drawErrorMessage(const QString& msg, QPainter* painter) { - QString msg; - if (m_configError) { - msg = ConfigHandler().errorMessage(); - } else if (m_configErrorResolved) { - msg = tr("Configuration error resolved. Launch `flameshot " - "gui` again to apply it."); - } - auto textRect = painter->fontMetrics().boundingRect(msg); int w = textRect.width(), h = textRect.height(); - textRect = { size().width() - w, size().height() - h, w + 100, h + 100 }; + textRect = { + size().width() - w - 10, size().height() - h - 5, w + 100, h + 100 + }; QScreen* currentScreen = QGuiAppCurrentScreen().currentScreen(); if (!textRect.contains(QCursor::pos(currentScreen))) { diff --git a/src/widgets/capture/capturewidget.h b/src/widgets/capture/capturewidget.h index 4d1b4b23..dd2e9559 100644 --- a/src/widgets/capture/capturewidget.h +++ b/src/widgets/capture/capturewidget.h @@ -94,6 +94,7 @@ protected: void wheelEvent(QWheelEvent* wheelEvent) override; void resizeEvent(QResizeEvent* resizeEvent) override; void moveEvent(QMoveEvent* moveEvent) override; + void changeEvent(QEvent* changeEvent) override; private: void loadDrawThickness(); @@ -122,7 +123,7 @@ private: QRect extendedSelection() const; QRect extendedRect(const QRect& r) const; QRect paddedUpdateRect(const QRect& r) const; - void drawConfigErrorMessage(QPainter* painter); + void drawErrorMessage(const QString& msg, QPainter* painter); void drawInactiveRegion(QPainter* painter); void drawToolsData(); void drawObjectSelection();