/* windows.cpp SPDX-FileCopyrightText: 1997 Patrick Dowler SPDX-FileCopyrightText: 2001 Waldo Bastian SPDX-License-Identifier: GPL-2.0-or-later */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include "kwinoptions_settings.h" #include "windows.h" #include #include "kwinoptions_kdeglobals_settings.h" #include "kwinoptions_settings.h" #define CLICK_TO_FOCUS 0 #define CLICK_TO_FOCUS_MOUSE_PRECEDENT 1 #define FOCUS_FOLLOWS_MOUSE 2 #define FOCUS_FOLLOWS_MOUSE_PRECEDENT 3 #define FOCUS_UNDER_MOUSE 4 #define FOCUS_STRICTLY_UNDER_MOUSE 5 namespace { constexpr int defaultFocusPolicyIndex = CLICK_TO_FOCUS; } KWinFocusConfigForm::KWinFocusConfigForm(QWidget *parent) : QWidget(parent) { setupUi(parent); } KFocusConfig::KFocusConfig(bool _standAlone, KWinOptionsSettings *settings, QWidget *parent) : KCModule(parent, KPluginMetaData()) , standAlone(_standAlone) , m_ui(new KWinFocusConfigForm(widget())) { if (settings) { initialize(settings); } } void KFocusConfig::initialize(KWinOptionsSettings *settings) { m_settings = settings; addConfig(m_settings, widget()); connect(m_ui->windowFocusPolicy, qOverload(&QComboBox::currentIndexChanged), this, &KFocusConfig::focusPolicyChanged); connect(m_ui->windowFocusPolicy, qOverload(&QComboBox::currentIndexChanged), this, &KFocusConfig::updateDefaultIndicator); connect(this, SIGNAL(defaultsIndicatorsVisibleChanged(bool)), this, SLOT(updateDefaultIndicator())); connect(qApp, &QGuiApplication::screenAdded, this, &KFocusConfig::updateMultiScreen); connect(qApp, &QGuiApplication::screenRemoved, this, &KFocusConfig::updateMultiScreen); updateMultiScreen(); } void KFocusConfig::updateMultiScreen() { m_ui->multiscreenBehaviorLabel->setVisible(QApplication::screens().count() > 1); m_ui->kcfg_SeparateScreenFocus->setVisible(QApplication::screens().count() > 1); } void KFocusConfig::updateDefaultIndicator() { const bool isDefault = m_ui->windowFocusPolicy->currentIndex() == defaultFocusPolicyIndex; m_ui->windowFocusPolicy->setProperty("_kde_highlight_neutral", defaultsIndicatorsVisible() && !isDefault); m_ui->windowFocusPolicy->update(); } void KFocusConfig::updateFocusPolicyExplanatoryText() { const int focusPolicy = m_ui->windowFocusPolicy->currentIndex(); switch (focusPolicy) { case CLICK_TO_FOCUS: m_ui->windowFocusPolicyDescriptionLabel->setText(i18n("Click to focus: A window becomes active when you click into it. This behavior is common on other operating systems and likely what you want.")); break; case CLICK_TO_FOCUS_MOUSE_PRECEDENT: m_ui->windowFocusPolicyDescriptionLabel->setText(i18n("Click to focus (mouse precedence): Mostly the same as Click to focus. If an active window has to be chosen by the system (eg. because the currently active one was closed) the window under the mouse is the preferred candidate. Unusual, but possible variant of Click to focus.")); break; case FOCUS_FOLLOWS_MOUSE: m_ui->windowFocusPolicyDescriptionLabel->setText(i18n("Focus follows mouse: Moving the mouse onto a window will activate it. Eg. windows randomly appearing under the mouse will not gain the focus. Focus stealing prevention takes place as usual. Think as Click to focus just without having to actually click.")); break; case FOCUS_FOLLOWS_MOUSE_PRECEDENT: m_ui->windowFocusPolicyDescriptionLabel->setText(i18n("This is mostly the same as Focus follows mouse. If an active window has to be chosen by the system (eg. because the currently active one was closed) the window under the mouse is the preferred candidate. Choose this, if you want a hover controlled focus.")); break; case FOCUS_UNDER_MOUSE: m_ui->windowFocusPolicyDescriptionLabel->setText(i18n("Focus under mouse: The focus always remains on the window under the mouse.
Warning: Focus stealing prevention and the tabbox ('Alt+Tab') contradict the activation policy and will not work. You very likely want to use Focus follows mouse (mouse precedence) instead!")); break; case FOCUS_STRICTLY_UNDER_MOUSE: m_ui->windowFocusPolicyDescriptionLabel->setText(i18n("Focus strictly under mouse: The focus is always on the window under the mouse (in doubt nowhere) very much like the focus behavior in an unmanaged legacy X11 environment.
Warning: Focus stealing prevention and the tabbox ('Alt+Tab') contradict the activation policy and will not work. You very likely want to use Focus follows mouse (mouse precedence) instead!")); break; } } void KFocusConfig::focusPolicyChanged() { int selectedFocusPolicy = 0; bool selectedNextFocusPrefersMouseItem = false; const bool loadedNextFocusPrefersMouseItem = m_settings->nextFocusPrefersMouse(); updateFocusPolicyExplanatoryText(); int focusPolicy = m_ui->windowFocusPolicy->currentIndex(); switch (focusPolicy) { case CLICK_TO_FOCUS: selectedFocusPolicy = KWinOptionsSettings::EnumFocusPolicy::ClickToFocus; break; case CLICK_TO_FOCUS_MOUSE_PRECEDENT: selectedFocusPolicy = KWinOptionsSettings::EnumFocusPolicy::ClickToFocus; selectedNextFocusPrefersMouseItem = true; break; case FOCUS_FOLLOWS_MOUSE: selectedFocusPolicy = KWinOptionsSettings::EnumFocusPolicy::FocusFollowsMouse; break; case FOCUS_FOLLOWS_MOUSE_PRECEDENT: selectedFocusPolicy = KWinOptionsSettings::EnumFocusPolicy::FocusFollowsMouse; selectedNextFocusPrefersMouseItem = true; break; case FOCUS_UNDER_MOUSE: selectedFocusPolicy = KWinOptionsSettings::EnumFocusPolicy::FocusUnderMouse; break; case FOCUS_STRICTLY_UNDER_MOUSE: selectedFocusPolicy = KWinOptionsSettings::EnumFocusPolicy::FocusStrictlyUnderMouse; break; } unmanagedWidgetChangeState(m_settings->focusPolicy() != selectedFocusPolicy || loadedNextFocusPrefersMouseItem != selectedNextFocusPrefersMouseItem); unmanagedWidgetDefaultState(focusPolicy == defaultFocusPolicyIndex); // the auto raise related widgets are: autoRaise m_ui->kcfg_AutoRaise->setEnabled(focusPolicy != CLICK_TO_FOCUS && focusPolicy != CLICK_TO_FOCUS_MOUSE_PRECEDENT); m_ui->kcfg_FocusStealingPreventionLevel->setDisabled(focusPolicy == FOCUS_UNDER_MOUSE || focusPolicy == FOCUS_STRICTLY_UNDER_MOUSE); // the delayed focus related widgets are: delayFocus m_ui->delayFocusOnLabel->setEnabled(focusPolicy != CLICK_TO_FOCUS); m_ui->kcfg_DelayFocusInterval->setEnabled(focusPolicy != CLICK_TO_FOCUS); } void KFocusConfig::load(void) { KCModule::load(); const bool loadedNextFocusPrefersMouseItem = m_settings->nextFocusPrefersMouse(); int focusPolicy = m_settings->focusPolicy(); switch (focusPolicy) { // the ClickToFocus and FocusFollowsMouse have special values when // NextFocusPrefersMouse is true case KWinOptionsSettings::EnumFocusPolicy::ClickToFocus: m_ui->windowFocusPolicy->setCurrentIndex(CLICK_TO_FOCUS + loadedNextFocusPrefersMouseItem); break; case KWinOptionsSettings::EnumFocusPolicy::FocusFollowsMouse: m_ui->windowFocusPolicy->setCurrentIndex(FOCUS_FOLLOWS_MOUSE + loadedNextFocusPrefersMouseItem); break; default: // +2 to ignore the two special values m_ui->windowFocusPolicy->setCurrentIndex(focusPolicy + 2); break; } updateFocusPolicyExplanatoryText(); } void KFocusConfig::save(void) { KCModule::save(); int idxFocusPolicy = m_ui->windowFocusPolicy->currentIndex(); switch (idxFocusPolicy) { case CLICK_TO_FOCUS: case CLICK_TO_FOCUS_MOUSE_PRECEDENT: m_settings->setFocusPolicy(KWinOptionsSettings::EnumFocusPolicy::ClickToFocus); break; case FOCUS_FOLLOWS_MOUSE: case FOCUS_FOLLOWS_MOUSE_PRECEDENT: // the ClickToFocus and FocusFollowsMouse have special values when // NextFocusPrefersMouse is true m_settings->setFocusPolicy(KWinOptionsSettings::EnumFocusPolicy::FocusFollowsMouse); break; case FOCUS_UNDER_MOUSE: m_settings->setFocusPolicy(KWinOptionsSettings::EnumFocusPolicy::FocusUnderMouse); break; case FOCUS_STRICTLY_UNDER_MOUSE: m_settings->setFocusPolicy(KWinOptionsSettings::EnumFocusPolicy::FocusStrictlyUnderMouse); break; } m_settings->setNextFocusPrefersMouse(idxFocusPolicy == CLICK_TO_FOCUS_MOUSE_PRECEDENT || idxFocusPolicy == FOCUS_FOLLOWS_MOUSE_PRECEDENT); m_settings->save(); if (standAlone) { // Send signal to all kwin instances QDBusMessage message = QDBusMessage::createSignal("/KWin", "org.kde.KWin", "reloadConfig"); QDBusConnection::sessionBus().send(message); } } void KFocusConfig::defaults() { KCModule::defaults(); m_ui->windowFocusPolicy->setCurrentIndex(defaultFocusPolicyIndex); } KWinAdvancedConfigForm::KWinAdvancedConfigForm(QWidget *parent) : QWidget(parent) { setupUi(parent); } KAdvancedConfig::KAdvancedConfig(bool _standAlone, KWinOptionsSettings *settings, KWinOptionsKDEGlobalsSettings *globalSettings, QWidget *parent) : KCModule(parent, KPluginMetaData()) , standAlone(_standAlone) , m_ui(new KWinAdvancedConfigForm(widget())) { if (settings && globalSettings) { initialize(settings, globalSettings); } } void KAdvancedConfig::initialize(KWinOptionsSettings *settings, KWinOptionsKDEGlobalsSettings *globalSettings) { m_settings = settings; addConfig(m_settings, widget()); addConfig(globalSettings, widget()); m_ui->kcfg_Placement->setItemData(KWinOptionsSettings::PlacementChoices::Smart, "Smart"); m_ui->kcfg_Placement->setItemData(KWinOptionsSettings::PlacementChoices::Maximizing, "Maximizing"); m_ui->kcfg_Placement->setItemData(KWinOptionsSettings::PlacementChoices::Random, "Random"); m_ui->kcfg_Placement->setItemData(KWinOptionsSettings::PlacementChoices::Centered, "Centered"); m_ui->kcfg_Placement->setItemData(KWinOptionsSettings::PlacementChoices::ZeroCornered, "ZeroCornered"); m_ui->kcfg_Placement->setItemData(KWinOptionsSettings::PlacementChoices::UnderMouse, "UnderMouse"); // Don't show the option to prevent apps from remembering their window // positions on Wayland because it doesn't work on Wayland and the feature // will eventually be implemented in a different way there. // This option lives in the kdeglobals file because it is consumed by // kxmlgui. m_ui->kcfg_AllowKDEAppsToRememberWindowPositions->setVisible(KWindowSystem::isPlatformX11()); m_ui->kcfg_ActivationDesktopPolicy->setItemData(KWinOptionsSettings::ActivationDesktopPolicyChoices::SwitchToOtherDesktop, "SwitchToOtherDesktop"); m_ui->kcfg_ActivationDesktopPolicy->setItemData(KWinOptionsSettings::ActivationDesktopPolicyChoices::BringToCurrentDesktop, "BringToCurrentDesktop"); } void KAdvancedConfig::save(void) { KCModule::save(); if (standAlone) { // Send signal to all kwin instances QDBusMessage message = QDBusMessage::createSignal("/KWin", "org.kde.KWin", "reloadConfig"); QDBusConnection::sessionBus().send(message); } } KWinMovingConfigForm::KWinMovingConfigForm(QWidget *parent) : QWidget(parent) { setupUi(parent); } KMovingConfig::KMovingConfig(bool _standAlone, KWinOptionsSettings *settings, QWidget *parent) : KCModule(parent, KPluginMetaData()) , standAlone(_standAlone) , m_ui(new KWinMovingConfigForm(widget())) { if (settings) { initialize(settings); } } void KMovingConfig::initialize(KWinOptionsSettings *settings) { m_settings = settings; addConfig(m_settings, widget()); } void KMovingConfig::save(void) { KCModule::save(); if (standAlone) { // Send signal to all kwin instances QDBusMessage message = QDBusMessage::createSignal("/KWin", "org.kde.KWin", "reloadConfig"); QDBusConnection::sessionBus().send(message); } } #include "moc_windows.cpp"