File indexing completed on 2024-04-28 03:57:09

0001 /*
0002     This file is part of the KDE libraries and the Kate part.
0003     SPDX-FileCopyrightText: 2013-2016 Simon St James <kdedevel@etotheipiplusone.com>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "emulatedcommandbarsetupandteardown.h"
0009 
0010 #include <inputmode/kateviinputmode.h>
0011 #include <kateconfig.h>
0012 #include <kateview.h>
0013 #include <vimode/emulatedcommandbar/emulatedcommandbar.h>
0014 
0015 #include <QApplication>
0016 #include <QMainWindow>
0017 #include <QMetaObject>
0018 #include <QTest>
0019 
0020 // BEGIN: WindowKeepActive
0021 
0022 WindowKeepActive::WindowKeepActive(QMainWindow *mainWindow)
0023     : m_mainWindow(mainWindow)
0024 {
0025     /* There's nothing to do here. */
0026 }
0027 
0028 bool WindowKeepActive::eventFilter(QObject *object, QEvent *event)
0029 {
0030     Q_UNUSED(object);
0031 
0032     if (event->type() == QEvent::WindowDeactivate) {
0033         // With some combinations of Qt and Xvfb, invoking/ dismissing a popup
0034         // will deactivate the m_mainWindow, preventing it from receiving shortcuts.
0035         // If we detect this, set it back to being the active window again.
0036         event->ignore();
0037         m_mainWindow->activateWindow();
0038         return true;
0039     }
0040     return false;
0041 }
0042 
0043 // END: WindowKeepActive
0044 
0045 // BEGIN: EmulatedCommandBarSetUpAndTearDown
0046 
0047 EmulatedCommandBarSetUpAndTearDown::EmulatedCommandBarSetUpAndTearDown(KateViInputMode *inputMode, KTextEditor::ViewPrivate *view, QMainWindow *window)
0048     : m_view(view)
0049     , m_window(window)
0050     , m_windowKeepActive(window)
0051     , m_viInputMode(inputMode)
0052 {
0053     m_window->show();
0054     m_view->show();
0055     QApplication::setActiveWindow(m_window);
0056     m_view->setFocus();
0057     QApplication::processEvents();
0058     KateViewConfig::global()->setValue(KateViewConfig::ViInputModeStealKeys, true);
0059     m_window->installEventFilter(&m_windowKeepActive);
0060 
0061     (void)QTest::qWaitForWindowExposed(m_view);
0062     QTest::qWait(100); // For whatever reason needed
0063 }
0064 EmulatedCommandBarSetUpAndTearDown::~EmulatedCommandBarSetUpAndTearDown()
0065 {
0066     m_window->removeEventFilter(&m_windowKeepActive);
0067     // Use invokeMethod to avoid having to export KateViewBar for testing.
0068     QMetaObject::invokeMethod(m_viInputMode->viModeEmulatedCommandBar(), "hideMe");
0069     m_view->hide();
0070     m_window->hide();
0071     KateViewConfig::global()->setValue(KateViewConfig::ViInputModeStealKeys, false);
0072     QApplication::processEvents();
0073 }
0074 
0075 // END: EmulatedCommandBarSetUpAndTearDown
0076 
0077 #include "moc_emulatedcommandbarsetupandteardown.cpp"