diff --git a/src/mumble/CMakeLists.txt b/src/mumble/CMakeLists.txt index 2cdfd7274..34f9d22d3 100644 --- a/src/mumble/CMakeLists.txt +++ b/src/mumble/CMakeLists.txt @@ -185,6 +185,8 @@ set(MUMBLE_SOURCES "PTTButtonWidget.cpp" "PTTButtonWidget.h" "PTTButtonWidget.ui" + "QtWidgetUtils.cpp" + "QtWidgetUtils.h" "RichTextEditor.cpp" "RichTextEditor.h" "RichTextEditorLink.ui" diff --git a/src/mumble/QtWidgetUtils.cpp b/src/mumble/QtWidgetUtils.cpp new file mode 100644 index 000000000..1e2bbff59 --- /dev/null +++ b/src/mumble/QtWidgetUtils.cpp @@ -0,0 +1,31 @@ +// Copyright 2021 The Mumble Developers. All rights reserved. +// Use of this source code is governed by a BSD-style license +// that can be found in the LICENSE file at the root of the +// Mumble source tree or at . + +#include "QtWidgetUtils.h" + +#include +#include + +namespace Mumble { +namespace QtUtils { + QScreen *screenAt(QPoint point) { +#if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0) + // screenAt was only introduced in Qt 5.10 + return QGuiApplication::screenAt(point); +#else + for (QScreen *currentScreen : QGuiApplication::screens()) { + if (currentScreen->availableGeometry().contains(point)) { + return currentScreen; + } + } + + return nullptr; +#endif + } + + bool positionIsOnScreen(QPoint point) { return screenAt(point) != nullptr; } + +}; // namespace QtUtils +}; // namespace Mumble diff --git a/src/mumble/QtWidgetUtils.h b/src/mumble/QtWidgetUtils.h new file mode 100644 index 000000000..14b82a962 --- /dev/null +++ b/src/mumble/QtWidgetUtils.h @@ -0,0 +1,29 @@ +// Copyright 2021 The Mumble Developers. All rights reserved. +// Use of this source code is governed by a BSD-style license +// that can be found in the LICENSE file at the root of the +// Mumble source tree or at . + +#ifndef MUMBLE_MUMBLE_QTWIDGETUTILS_H_ +#define MUMBLE_MUMBLE_QTWIDGETUTILS_H_ + +#include + +class QScreen; + +namespace Mumble { +namespace QtUtils { + + /** + * @returns The QScreen the given position is on or nullptr if there is no such screen + */ + QScreen *screenAt(QPoint position); + + /** + * @returns Whether the given position is on any screen (as opposed to off-screen) + */ + bool positionIsOnScreen(QPoint position); + +}; // namespace QtUtils +}; // namespace Mumble + +#endif // MUMBLE_MUMBLE_QTWIDGETUTILS_H_ diff --git a/src/mumble/main.cpp b/src/mumble/main.cpp index d034423f2..0a8aae9a7 100644 --- a/src/mumble/main.cpp +++ b/src/mumble/main.cpp @@ -37,6 +37,7 @@ #include "NetworkConfig.h" #include "PluginInstaller.h" #include "PluginManager.h" +#include "QtWidgetUtils.h" #include "SSL.h" #include "SocketRPC.h" #include "TalkingUI.h" @@ -72,29 +73,10 @@ void throw_exception(std::exception const &) { extern void os_init(); extern char *os_lang; -QScreen *screenAt(QPoint point) { -#if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0) - // screenAt was only introduced in Qt 5.10 - return QGuiApplication::screenAt(point); -#else - for (QScreen *currentScreen : QGuiApplication::screens()) { - if (currentScreen->availableGeometry().contains(point)) { - return currentScreen; - } - } - - return nullptr; -#endif -} - -bool positionIsOnScreen(QPoint point) { - return screenAt(point) != nullptr; -} - QPoint getTalkingUIPosition() { QPoint talkingUIPos = QPoint(0, 0); if (Global::get().s.qpTalkingUI_Position != Settings::UNSPECIFIED_POSITION - && positionIsOnScreen(Global::get().s.qpTalkingUI_Position)) { + && Mumble::QtUtils::positionIsOnScreen(Global::get().s.qpTalkingUI_Position)) { // Restore last position talkingUIPos = Global::get().s.qpTalkingUI_Position; } else { @@ -104,7 +86,7 @@ QPoint getTalkingUIPosition() { const QPoint defaultPos = QPoint(mainWindowPos.x() + Global::get().mw->size().width() + horizontalBuffer, mainWindowPos.y()); - if (positionIsOnScreen(defaultPos)) { + if (Mumble::QtUtils::positionIsOnScreen(defaultPos)) { talkingUIPos = defaultPos; } } @@ -115,18 +97,18 @@ QPoint getTalkingUIPosition() { const QSize talkingUISize = Global::get().talkingUI->size(); // The screen should always be found at this point as we have chosen to pos to be on a screen - const QScreen *screen = screenAt(talkingUIPos); + const QScreen *screen = Mumble::QtUtils::screenAt(talkingUIPos); const QRect screenGeom = screen ? screen->availableGeometry() : QRect(0, 0, 0, 0); // Check whether the TalkingUI fits on the screen in x-direction - if (!positionIsOnScreen(talkingUIPos + QPoint(talkingUISize.width(), 0))) { + if (!Mumble::QtUtils::positionIsOnScreen(talkingUIPos + QPoint(talkingUISize.width(), 0))) { int overlap = talkingUIPos.x() + talkingUISize.width() - screenGeom.x() - screenGeom.width(); // Correct the x coordinate but don't move it below 0 talkingUIPos.setX(std::max(talkingUIPos.x() - overlap, 0)); } // Check whether the TalkingUI fits on the screen in y-direction - if (!positionIsOnScreen(talkingUIPos + QPoint(0, talkingUISize.height()))) { + if (!Mumble::QtUtils::positionIsOnScreen(talkingUIPos + QPoint(0, talkingUISize.height()))) { int overlap = talkingUIPos.y() + talkingUISize.height() - screenGeom.y() - screenGeom.height(); // Correct the x coordinate but don't move it below 0