From 08c858ba6e1e88188df823a925c951d738b45576 Mon Sep 17 00:00:00 2001 From: Sergio Martins Date: Wed, 31 Jul 2019 22:16:25 +0100 Subject: [PATCH] Add a Config.h public header, to configure behaviour Like enabling aero snap or native title bar --- src/CMakeLists.txt | 4 ++- src/Config.cpp | 80 ++++++++++++++++++++++++++++++++++++++++++++++ src/Config.h | 73 ++++++++++++++++++++++++++++++++++++++++++ src/Utils_p.h | 7 ++-- 4 files changed, 160 insertions(+), 4 deletions(-) create mode 100644 src/Config.cpp create mode 100644 src/Config.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 26979310..8ebed3db 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -2,6 +2,8 @@ cmake_policy(SET CMP0043 NEW) set(DOCKSLIBS_SRCS + Config.cpp + Config.h DockWidget.cpp DragController.cpp Draggable.cpp @@ -42,7 +44,7 @@ else() set(IS_CLANG_BUILD FALSE) endif() -set (DOCKS_INSTALLABLE_INCLUDES docks_export.h DockWidget.h MainWindow.h LayoutSaver.h Draggable_p.h KDDockWidgets.h) +set (DOCKS_INSTALLABLE_INCLUDES docks_export.h DockWidget.h MainWindow.h LayoutSaver.h Draggable_p.h KDDockWidgets.h Config.h) qt5_add_resources(RESOURCES ${CMAKE_CURRENT_SOURCE_DIR}/resources.qrc) diff --git a/src/Config.cpp b/src/Config.cpp new file mode 100644 index 00000000..eb74fa00 --- /dev/null +++ b/src/Config.cpp @@ -0,0 +1,80 @@ +/* + This file is part of KDDockWidgets. + + Copyright (C) 2019 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com + Author: Sérgio Martins + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +/** + * @file + * @brief Application wide config to tune certain beahviours of the framework. + * + * @author Sérgio Martins \ + */ + +#include "Config.h" +#include +#include + +using namespace KDDockWidgets; + +class Config::Private +{ +public: + Private() + { + } + + Flags m_flags = Flag_Default; + +}; + + +Config::Config() + : d(new Private()) +{ +} + +Config& Config::instance() +{ + static Config config; + return config; +} + +Config::~Config() +{ + delete d; +} + +Config::Flags Config::flags() const +{ + return d->m_flags; +} + +void Config::setFlags(Flags f) +{ + if (!qApp) { + qWarning() << Q_FUNC_INFO << "Only use this function before creating the QApplication"; + return; + } + + if ((f & Flag_AeroSnapWithClientDecos) && (f & Flag_NativeTitleBar)) { + // We're either using native or client decorations + f = f & ~Flag_AeroSnapWithClientDecos; + } + + d->m_flags = f; +} diff --git a/src/Config.h b/src/Config.h new file mode 100644 index 00000000..6d9dfe0b --- /dev/null +++ b/src/Config.h @@ -0,0 +1,73 @@ +/* + This file is part of KDDockWidgets. + + Copyright (C) 2019 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com + Author: Sérgio Martins + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +/** + * @file + * @brief Application wide config to tune certain beahviours of the framework. + * + * @author Sérgio Martins \ + */ + +#ifndef KD_DOCKWIDGETS_CONFIG_H +#define KD_DOCKWIDGETS_CONFIG_H + +#include "docks_export.h" + +namespace KDDockWidgets +{ + +/** + * @brief Singleton to allow to choose certain behaviours for the framework. + * + * The setters should only be used before creating a QApplication. + */ +class DOCKS_EXPORT Config +{ +public: + + ///@brief returns the singleton Config instance + static Config &instance(); + + ~Config(); + + enum Flag { + Flag_None = 0, + Flag_NativeTitleBar = 1, ///> Enables the Native OS title bar on OSes that support it (Windows 10, macOS), ignored otherwise. This is mutually exclusive with Flag_AeroSnap + Flag_AeroSnapWithClientDecos = 2, ///> Enables AeroSnap even if we're not using the native title bar. Only supported on Windows 10. + Flag_Default = Flag_AeroSnapWithClientDecos ///> The defaults if nothing is set + }; + Q_DECLARE_FLAGS(Flags, Flag) + + ///@brief getter for the flags + Flags flags() const; + + ///@brief setter for the flags + void setFlags(Flags); + +private: + Q_DISABLE_COPY(Config) + Config(); + class Private; + Private *const d; +}; + +} + +#endif diff --git a/src/Utils_p.h b/src/Utils_p.h index 36644c74..cdd3d1f5 100644 --- a/src/Utils_p.h +++ b/src/Utils_p.h @@ -21,6 +21,8 @@ #ifndef KD_UTILS_P_H #define KD_UTILS_P_H +#include "Config.h" + #include #include @@ -41,8 +43,7 @@ inline bool usesNativeTitleBar() // On Linux, dragging the title bar of a window doesn't generate NonClientMouseEvents return false; #else - // But let's return false for Windows and macOS too, since our fake resizing seems to be working fine - return false; + return Config::instance().flags() & Config::Flag_NativeTitleBar; #endif } @@ -50,7 +51,7 @@ inline bool usesAeroSnapWithCustomDecos() { #ifdef Q_OS_WIN // Aero-snap is supported since Windows 10 - return QOperatingSystemVersion::current().majorVersion() >= 10; + return (Config::instance().flags() & Config::Flag_AeroSnapWithClientDecos) && QOperatingSystemVersion::current().majorVersion() >= 10; #else return false; #endif