Fix raising window when attaching to an existing instance on Wayland

When the second instance is launched it receives an activation token per environment variable

We need to pass that token to the first instance so that it can use it to raise itself

Also properly use startup ids for this on X11 instead of relying on forceActiveWindow, which is a hack
remotes/origin/work/svuorela/remove-write-only-variable-1
Nicolas Fella 3 years ago
parent 4be119f22c
commit 0501c5d8c4
  1. 4
      .gitlab-ci.yml
  2. 5
      CMakeLists.txt
  3. 8
      autotests/CMakeLists.txt
  4. 3
      config-okular.h.cmake
  5. 4
      shell/CMakeLists.txt
  6. 24
      shell/okular_main.cpp
  7. 11
      shell/shell.cpp
  8. 2
      shell/shell.h

@ -19,7 +19,7 @@ build_ubuntu_22_04:
- apt-get update
- apt-get install --yes eatmydata
- eatmydata apt-get build-dep --yes --no-install-recommends okular
- eatmydata apt-get install --yes --no-install-recommends ninja-build qtbase5-private-dev
- eatmydata apt-get install --yes --no-install-recommends ninja-build qtbase5-private-dev libqt5x11extras5-dev
script:
- mkdir -p build && cd build
- cmake -DOKULAR_UI=desktop -G Ninja ..
@ -37,7 +37,7 @@ build_clazy_clang_tidy:
- apt-get update
- apt-get install --yes eatmydata
- eatmydata apt-get build-dep --yes --no-install-recommends okular
- eatmydata apt-get install --yes --no-install-recommends ninja-build clazy clang clang-tidy qtbase5-private-dev libkf5crash-dev libkf5purpose-dev kirigami2-dev libegl-dev jq
- eatmydata apt-get install --yes --no-install-recommends ninja-build clazy clang clang-tidy qtbase5-private-dev libqt5x11extras5-dev libkf5crash-dev libkf5purpose-dev kirigami2-dev libegl-dev jq
script:
- srcdir=`pwd` && mkdir -p /tmp/okular_build && cd /tmp/okular_build && CC=clang CXX=clazy CXXFLAGS="-Werror -Wno-deprecated-declarations" cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -G Ninja $srcdir && cat compile_commands.json | jq '[.[] | select(.file | contains("'"$srcdir"'"))]' > compile_commands.aux.json && cat compile_commands.aux.json | jq '[.[] | select(.file | contains("/synctex/")| not)]' > compile_commands.json && cp "$srcdir/.clang-tidy" .

@ -185,6 +185,11 @@ if(NOT WIN32 AND NOT ANDROID AND NOT APPLE)
DESCRIPTION "Activities interface library"
URL "https://api.kde.org/frameworks/kactivities/html/"
PURPOSE "Required for Activities integration.")
find_package(Qt5X11Extras REQUIRED)
set(HAVE_X11 TRUE)
else()
set(HAVE_X11 FALSE)
endif()
find_package(Phonon4Qt5 CONFIG REQUIRED)

@ -115,6 +115,10 @@ if(KF5Activities_FOUND AND BUILD_DESKTOP)
LINK_LIBRARIES Qt5::Test KF5::Activities okularpart okularcore
)
target_compile_definitions(mainshelltest PRIVATE OKULAR_BINARY="$<TARGET_FILE:okular>")
if (HAVE_X11)
target_link_libraries(mainshelltest Qt5::X11Extras)
endif()
endif()
if(BUILD_DESKTOP)
@ -122,6 +126,10 @@ if(BUILD_DESKTOP)
TEST_NAME "annotationtoolbartest"
LINK_LIBRARIES Qt5::Test okularpart
)
if (HAVE_X11)
target_link_libraries(annotationtoolbartest Qt5::X11Extras)
endif()
endif()
ecm_add_test(generatorstest.cpp

@ -6,3 +6,6 @@
/* Defines whether the malloc_trim method from malloc.h is available */
#cmakedefine01 HAVE_MALLOC_TRIM
/* Defines whether we are building with X11 support */
#cmakedefine01 HAVE_X11

@ -30,6 +30,10 @@ if(TARGET KF5::Activities)
target_link_libraries(okular KF5::Activities)
endif()
if (HAVE_X11)
target_link_libraries(okular Qt5::X11Extras)
endif()
install(TARGETS okular ${KDE_INSTALL_TARGETS_DEFAULT_ARGS})

@ -19,8 +19,28 @@
#include <QDBusInterface>
#include <QTextStream>
#include "config-okular.h"
#if HAVE_X11
#include <QX11Info>
#endif
#include <iostream>
static QString startupId()
{
QString result;
if (KWindowSystem::isPlatformWayland()) {
result = qEnvironmentVariable("XDG_ACTIVATION_TOKEN");
qunsetenv("XDG_ACTIVATION_TOKEN");
} else if (KWindowSystem::isPlatformX11()) {
#if HAVE_X11
result = QString::fromUtf8(QX11Info::nextStartupId());
#endif
}
return result;
}
static bool attachUniqueInstance(const QStringList &paths, const QString &serializedOptions)
{
if (!ShellUtils::unique(serializedOptions) || paths.count() != 1) {
@ -42,7 +62,7 @@ static bool attachUniqueInstance(const QStringList &paths, const QString &serial
const QString page = ShellUtils::page(serializedOptions);
iface.call(QStringLiteral("openDocument"), ShellUtils::urlFromArg(paths[0], ShellUtils::qfileExistFunc(), page).url(), serializedOptions);
if (!ShellUtils::noRaise(serializedOptions)) {
iface.call(QStringLiteral("tryRaise"));
iface.call(QStringLiteral("tryRaise"), startupId());
}
return true;
@ -136,7 +156,7 @@ static bool attachExistingInstance(const QStringList &paths, const QString &seri
exit(1);
}
bestService->call(QStringLiteral("tryRaise"));
bestService->call(QStringLiteral("tryRaise"), startupId());
return true;
}

@ -29,6 +29,7 @@
#include <KRecentFilesAction>
#include <KSharedConfig>
#include <KStandardAction>
#include <KStartupInfo>
#include <KToggleFullScreenAction>
#include <KToolBar>
#include <KUrlMimeData>
@ -660,9 +661,15 @@ void Shell::fileOpen()
}
}
void Shell::tryRaise()
void Shell::tryRaise(const QString &startupId)
{
KWindowSystem::forceActiveWindow(window()->effectiveWinId());
if (KWindowSystem::isPlatformWayland()) {
KWindowSystem::setCurrentXdgActivationToken(startupId);
} else if (KWindowSystem::isPlatformX11()) {
KStartupInfo::setNewStartupId(window()->windowHandle(), startupId.toUtf8());
}
KWindowSystem::activateWindow(window()->windowHandle());
}
// only called when starting the program

@ -74,7 +74,7 @@ public:
bool openDocument(const QUrl &url, const QString &serializedOptions);
public Q_SLOTS:
Q_SCRIPTABLE Q_NOREPLY void tryRaise();
Q_SCRIPTABLE Q_NOREPLY void tryRaise(const QString &startupId);
Q_SCRIPTABLE bool openDocument(const QString &urlString, const QString &serializedOptions = QString());
Q_SCRIPTABLE bool canOpenDocs(int numDocs, int desktop);

Loading…
Cancel
Save