From 333b2eb3223ceb4c321602a30e0ba5627fd22825 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Cl=C3=A9ment=20Fauconnier?= <fc86@outlook.fr>
Date: Thu, 24 Feb 2022 15:17:07 +0100
Subject: [PATCH] prevent the same error to popup again and again if something
 went wrong while trying to open the virtual keyboard

---
 src/board/UBBoardController.cpp       | 6 ++++--
 src/frameworks/UBPlatformUtils.cpp    | 4 +++-
 src/frameworks/UBPlatformUtils.h      | 2 ++
 src/frameworks/UBPlatformUtils_mac.mm | 7 +++++++
 4 files changed, 16 insertions(+), 3 deletions(-)

diff --git a/src/board/UBBoardController.cpp b/src/board/UBBoardController.cpp
index 3d1e4fa3..25b7ab8c 100644
--- a/src/board/UBBoardController.cpp
+++ b/src/board/UBBoardController.cpp
@@ -875,7 +875,6 @@ void UBBoardController::showKeyboard(bool show)
         UBPlatformUtils::showOSK(show);
     else
         mPaletteManager->showVirtualKeyboard(show);
-
 }
 
 
@@ -2171,7 +2170,10 @@ void UBBoardController::stylusToolChanged(int tool)
         if(eTool != UBStylusTool::Selector && eTool != UBStylusTool::Text)
         {
             if(mPaletteManager->mKeyboardPalette->m_isVisible)
-                UBApplication::mainWindow->actionVirtualKeyboard->activate(QAction::Trigger);
+            {
+                if (!UBPlatformUtils::errorOpeningVirtualKeyboard)
+                    UBApplication::mainWindow->actionVirtualKeyboard->activate(QAction::Trigger);
+            }
         }
     }
 
diff --git a/src/frameworks/UBPlatformUtils.cpp b/src/frameworks/UBPlatformUtils.cpp
index b5335f8f..74a17b95 100644
--- a/src/frameworks/UBPlatformUtils.cpp
+++ b/src/frameworks/UBPlatformUtils.cpp
@@ -66,7 +66,9 @@ UBKeyboardLocale::~UBKeyboardLocale()
 
 int UBPlatformUtils::nKeyboardLayouts;
 UBKeyboardLocale** UBPlatformUtils::keyboardLayouts;
-
+#ifdef Q_OS_OSX
+bool UBPlatformUtils::errorOpeningVirtualKeyboard = false;
+#endif
 UBKeyboardLocale** UBPlatformUtils::getKeyboardLayouts(int& nCount)
 {
     nCount = nKeyboardLayouts;
diff --git a/src/frameworks/UBPlatformUtils.h b/src/frameworks/UBPlatformUtils.h
index 8e6b91ed..6d8ad70c 100644
--- a/src/frameworks/UBPlatformUtils.h
+++ b/src/frameworks/UBPlatformUtils.h
@@ -213,6 +213,8 @@ public:
 #ifdef Q_OS_OSX
         static void SetMacLocaleByIdentifier(const QString& id);
         static void toggleFinder(const bool on);
+
+        static bool errorOpeningVirtualKeyboard;
 #endif
 };
 
diff --git a/src/frameworks/UBPlatformUtils_mac.mm b/src/frameworks/UBPlatformUtils_mac.mm
index 34fe2d72..0b208c06 100644
--- a/src/frameworks/UBPlatformUtils_mac.mm
+++ b/src/frameworks/UBPlatformUtils_mac.mm
@@ -684,6 +684,7 @@ void UBPlatformUtils::showOSK(bool show)
                 tell application \"System Events\"\n\
                     tell application process \"TextInputMenuAgent\"\n\
                         tell menu 1 of menu bar item 1 of menu bar 2\n\
+                            delay 0.2\n\
                             click menu item 2\n\
                         end tell\n\
                     end tell\n\
@@ -696,6 +697,8 @@ void UBPlatformUtils::showOSK(bool show)
 
         if(errorInfo!=nil)
         {
+            errorOpeningVirtualKeyboard = true;
+
             NSAlert *alert = [[NSAlert alloc] init];
 
             if (alert != nil)
@@ -711,5 +714,9 @@ void UBPlatformUtils::showOSK(bool show)
                     UBApplication::mainWindow->actionVirtualKeyboard->setChecked(true);
             }
         }
+        else
+        {
+            errorOpeningVirtualKeyboard = false;
+        }
     }
 }