浏览代码

KonvergoWindow: mark current screen in list

Tricky, because we need to make sure the marking reflects the current
state. (We don't really know when the UI will use this, so it needs to
be always up-to-date.)
Vincent Lang 8 年之前
父节点
当前提交
d1878fdf5b
共有 2 个文件被更改,包括 44 次插入12 次删除
  1. 41 11
      src/ui/KonvergoWindow.cpp
  2. 3 1
      src/ui/KonvergoWindow.h

+ 41 - 11
src/ui/KonvergoWindow.cpp

@@ -66,6 +66,17 @@ KonvergoWindow::KonvergoWindow(QWindow* parent) :
   connect(this, &KonvergoWindow::visibilityChanged,
           this, &KonvergoWindow::onVisibilityChanged);
 
+  connect(this, &KonvergoWindow::screenChanged,
+          this, &KonvergoWindow::updateCurrentScreen);
+  connect(this, &KonvergoWindow::xChanged,
+          this, &KonvergoWindow::updateCurrentScreen);
+  connect(this, &KonvergoWindow::yChanged,
+          this, &KonvergoWindow::updateCurrentScreen);
+  connect(this, &KonvergoWindow::visibilityChanged,
+          this, &KonvergoWindow::updateCurrentScreen);
+  connect(this, &KonvergoWindow::windowStateChanged,
+          this, &KonvergoWindow::updateCurrentScreen);
+
   connect(this, &KonvergoWindow::enableVideoWindowSignal,
           this, &KonvergoWindow::enableVideoWindow, Qt::QueuedConnection);
 
@@ -439,18 +450,24 @@ public:
 };
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
-QScreen* KonvergoWindow::findRealScreen()
+QScreen* KonvergoWindow::findCurrentScreen()
 {
-#ifdef Q_OS_WIN32
+  // Return the screen that contains most of the window. Quite possible that
+  // screen() would be sufficient, at least once the Qt bug returning a wrong
+  // QScreen on Windows is fixed.
+  QScreen *best = nullptr;
+  qint64 bestArea = 0;
   for(QScreen* screen : qApp->screens())
   {
-    if (screen->geometry() == geometry())
-      return screen;
+    QRect areaRC = screen->geometry().intersected(geometry());
+    qint64 area = areaRC.width() * (qint64)areaRC.height();
+    if (!best || area > bestArea)
+    {
+      best = screen;
+      bestArea = area;
+    }
   }
-  return nullptr;
-#else
-  return screen();
-#endif
+  return best ? best : screen();
 }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -461,8 +478,8 @@ void KonvergoWindow::onVisibilityChanged(QWindow::Visibility visibility)
 #ifdef Q_OS_WIN32
   if (visibility == QWindow::Windowed)
   {
-    QScreen* realScreen = findRealScreen();
-    if (realScreen)
+    QScreen* realScreen = findCurrentScreen();
+    if (realScreen && realScreen->geometry() == geometry())
     {
       QLOG_DEBUG() << "winging it!";
       setScreen(realScreen);
@@ -678,6 +695,7 @@ QString KonvergoWindow::webUrl()
 /////////////////////////////////////////////////////////////////////////////////////////
 void KonvergoWindow::updateScreens()
 {
+  QScreen* windowScreen = findCurrentScreen();
   QString screenName = SettingsComponent::Get().value(SETTINGS_SECTION_MAIN, "forceFSScreen").toString();
 
   QVariantList settingList;
@@ -697,7 +715,8 @@ void KonvergoWindow::updateScreens()
     entry["value"] = screen->name();
     entry["title"] =
       QString("%1,%2 %3x%4").arg(rc.left()).arg(rc.top()).arg(rc.right()).arg(rc.bottom()) +
-      " (" + screen->name() + ")";
+      " (" + screen->name() + ")" +
+      ((screen == windowScreen) ? " *" : "");
 
     settingList << entry;
 
@@ -715,6 +734,8 @@ void KonvergoWindow::updateScreens()
   }
 
   SettingsComponent::Get().updatePossibleValues(SETTINGS_SECTION_MAIN, "forceFSScreen", settingList);
+
+  m_currentScreenName = windowScreen ? windowScreen->name() : "";
 }
 
 /////////////////////////////////////////////////////////////////////////////////////////
@@ -733,3 +754,12 @@ void KonvergoWindow::onScreenRemoved(QScreen *screen)
 {
   updateScreens();
 }
+
+/////////////////////////////////////////////////////////////////////////////////////////
+void KonvergoWindow::updateCurrentScreen()
+{
+  QScreen* current = findCurrentScreen();
+  QString currentName = current ? current->name() : "";
+  if (currentName != m_currentScreenName)
+    updateScreens();
+}

+ 3 - 1
src/ui/KonvergoWindow.h

@@ -148,6 +148,7 @@ private slots:
   void showUpdateDialog();
   void onScreenAdded(QScreen *screen);
   void onScreenRemoved(QScreen *screen);
+  void updateCurrentScreen();
 
 private:
   void notifyScale(const QSize& size);
@@ -158,7 +159,7 @@ private:
   QScreen* loadLastScreen();
   void updateScreens();
   void updateForcedScreen();
-  QScreen* findRealScreen();
+  QScreen* findCurrentScreen();
 
   bool m_debugLayer;
   qreal m_lastScale;
@@ -169,6 +170,7 @@ private:
   bool m_showedUpdateDialog;
 
   unsigned long m_osxPresentationOptions;
+  QString m_currentScreenName;
 
   void setWebMode(bool newDesktopMode, bool fullscreen);
 };