Changeset View
Changeset View
Standalone View
Standalone View
kstyle/breezehelper.cpp
Show All 21 Lines | |||||
22 | #include "breeze.h" | 22 | #include "breeze.h" | ||
23 | #include "breezestyleconfigdata.h" | 23 | #include "breezestyleconfigdata.h" | ||
24 | 24 | | |||
25 | #include <KColorUtils> | 25 | #include <KColorUtils> | ||
26 | #include <KIconLoader> | 26 | #include <KIconLoader> | ||
27 | #include <KWindowSystem> | 27 | #include <KWindowSystem> | ||
28 | 28 | | |||
29 | #include <QApplication> | 29 | #include <QApplication> | ||
30 | #include <QDBusConnection> | ||||
31 | #include <QFileInfo> | ||||
30 | #include <QPainter> | 32 | #include <QPainter> | ||
33 | #include <QMainWindow> | ||||
34 | #include <QMenuBar> | ||||
35 | #include <QMdiArea> | ||||
36 | #include <QDockWidget> | ||||
37 | #include <QWindow> | ||||
31 | 38 | | |||
32 | #if BREEZE_HAVE_X11 | 39 | #if BREEZE_HAVE_X11 | ||
33 | #include <QX11Info> | 40 | #include <QX11Info> | ||
34 | #endif | 41 | #endif | ||
35 | 42 | | |||
36 | #include <algorithm> | 43 | #include <algorithm> | ||
37 | 44 | | |||
38 | namespace Breeze | 45 | namespace Breeze | ||
39 | { | 46 | { | ||
40 | 47 | | |||
41 | //* contrast for arrow and treeline rendering | 48 | //* contrast for arrow and treeline rendering | ||
42 | static const qreal arrowShade = 0.15; | 49 | static const qreal arrowShade = 0.15; | ||
43 | 50 | | |||
44 | //____________________________________________________________________ | 51 | //____________________________________________________________________ | ||
45 | Helper::Helper( KSharedConfig::Ptr config ): | 52 | Helper::Helper( KSharedConfig::Ptr config, QObject *parent ) : | ||
46 | _config( std::move( config ) ) | 53 | _config( std::move( config ) ), | ||
47 | {} | 54 | _kwinConfig( KSharedConfig::openConfig("kwinrc") ), | ||
55 | _decorationConfig( new InternalSettings() ), | ||||
56 | QObject ( parent ) | ||||
57 | { | ||||
58 | if (qApp) { | ||||
59 | connect(qApp, &QApplication::paletteChanged, this, [=]() { | ||||
60 | if (qApp->property("KDE_COLOR_SCHEME_PATH").isValid()) { | ||||
61 | const auto path = qApp->property("KDE_COLOR_SCHEME_PATH").toString(); | ||||
62 | KConfig config(path, KConfig::SimpleConfig); | ||||
63 | KConfigGroup group( config.group("WM") ); | ||||
64 | const QPalette palette( QApplication::palette() ); | ||||
65 | _activeTitleBarColor = group.readEntry( "activeBackground", palette.color( QPalette::Active, QPalette::Highlight ) ); | ||||
66 | _activeTitleBarTextColor = group.readEntry( "activeForeground", palette.color( QPalette::Active, QPalette::HighlightedText ) ); | ||||
67 | _inactiveTitleBarColor = group.readEntry( "inactiveBackground", palette.color( QPalette::Disabled, QPalette::Highlight ) ); | ||||
68 | _inactiveTitleBarTextColor = group.readEntry( "inactiveForeground", palette.color( QPalette::Disabled, QPalette::HighlightedText ) ); | ||||
69 | } | ||||
70 | }); | ||||
71 | } | ||||
72 | } | ||||
48 | 73 | | |||
49 | //____________________________________________________________________ | 74 | //____________________________________________________________________ | ||
50 | KSharedConfig::Ptr Helper::config() const | 75 | KSharedConfig::Ptr Helper::config() const | ||
51 | { return _config; } | 76 | { return _config; } | ||
52 | 77 | | |||
78 | | ||||
79 | //____________________________________________________________________ | ||||
80 | QSharedPointer<InternalSettings> Helper::decorationConfig() const | ||||
81 | { return _decorationConfig; } | ||||
82 | | ||||
53 | //____________________________________________________________________ | 83 | //____________________________________________________________________ | ||
54 | void Helper::loadConfig() | 84 | void Helper::loadConfig() | ||
55 | { | 85 | { | ||
56 | _viewFocusBrush = KStatefulBrush( KColorScheme::View, KColorScheme::FocusColor ); | 86 | _viewFocusBrush = KStatefulBrush( KColorScheme::View, KColorScheme::FocusColor ); | ||
57 | _viewHoverBrush = KStatefulBrush( KColorScheme::View, KColorScheme::HoverColor ); | 87 | _viewHoverBrush = KStatefulBrush( KColorScheme::View, KColorScheme::HoverColor ); | ||
58 | _viewNegativeTextBrush = KStatefulBrush( KColorScheme::View, KColorScheme::NegativeText ); | 88 | _viewNegativeTextBrush = KStatefulBrush( KColorScheme::View, KColorScheme::NegativeText ); | ||
59 | 89 | | |||
60 | const QPalette palette( QApplication::palette() ); | 90 | const QPalette palette( QApplication::palette() ); | ||
61 | const KConfigGroup group( _config->group( "WM" ) ); | 91 | _config->reparseConfiguration(); | ||
62 | _activeTitleBarColor = group.readEntry( "activeBackground", palette.color( QPalette::Active, QPalette::Highlight ) ); | 92 | _kwinConfig->reparseConfiguration(); | ||
63 | _activeTitleBarTextColor = group.readEntry( "activeForeground", palette.color( QPalette::Active, QPalette::HighlightedText ) ); | 93 | _cachedAutoValid = false; | ||
64 | _inactiveTitleBarColor = group.readEntry( "inactiveBackground", palette.color( QPalette::Disabled, QPalette::Highlight ) ); | 94 | _decorationConfig->load(); | ||
65 | _inactiveTitleBarTextColor = group.readEntry( "inactiveForeground", palette.color( QPalette::Disabled, QPalette::HighlightedText ) ); | 95 | KConfig config(qApp->property("KDE_COLOR_SCHEME_PATH").toString(), KConfig::SimpleConfig); | ||
96 | KConfigGroup appGroup( config.group("WM") ); | ||||
97 | KConfigGroup globalGroup( _config->group("WM") ); | ||||
98 | _activeTitleBarColor = appGroup.readEntry( "activeBackground", globalGroup.readEntry( "activeBackground", palette.color( QPalette::Active, QPalette::Highlight ) ) ); | ||||
99 | _activeTitleBarTextColor = appGroup.readEntry( "activeForeground", globalGroup.readEntry( "activeForeground", palette.color( QPalette::Active, QPalette::HighlightedText ) ) ); | ||||
100 | _inactiveTitleBarColor = appGroup.readEntry( "inactiveBackground", globalGroup.readEntry( "inactiveBackground", palette.color( QPalette::Disabled, QPalette::Highlight ) ) ); | ||||
101 | _inactiveTitleBarTextColor = appGroup.readEntry( "inactiveForeground", globalGroup.readEntry( "inactiveForeground", palette.color( QPalette::Disabled, QPalette::HighlightedText ) ) ); | ||||
66 | } | 102 | } | ||
67 | 103 | | |||
68 | //____________________________________________________________________ | 104 | //____________________________________________________________________ | ||
69 | QColor Helper::frameOutlineColor( const QPalette& palette, bool mouseOver, bool hasFocus, qreal opacity, AnimationMode mode ) const | 105 | QColor Helper::frameOutlineColor( const QPalette& palette, bool mouseOver, bool hasFocus, qreal opacity, AnimationMode mode ) const | ||
70 | { | 106 | { | ||
71 | 107 | | |||
72 | QColor outline( KColorUtils::mix( palette.color( QPalette::Window ), palette.color( QPalette::WindowText ), 0.25 ) ); | 108 | QColor outline( KColorUtils::mix( palette.color( QPalette::Window ), palette.color( QPalette::WindowText ), 0.25 ) ); | ||
73 | 109 | | |||
▲ Show 20 Lines • Show All 1517 Lines • ▼ Show 20 Line(s) | 1364 | } | |||
1591 | { return compositingActive() && widget && widget->testAttribute( Qt::WA_TranslucentBackground ); } | 1627 | { return compositingActive() && widget && widget->testAttribute( Qt::WA_TranslucentBackground ); } | ||
1592 | 1628 | | |||
1593 | //______________________________________________________________________________________ | 1629 | //______________________________________________________________________________________ | ||
1594 | qreal Helper::devicePixelRatio( const QPixmap& pixmap ) const | 1630 | qreal Helper::devicePixelRatio( const QPixmap& pixmap ) const | ||
1595 | { | 1631 | { | ||
1596 | return pixmap.devicePixelRatio(); | 1632 | return pixmap.devicePixelRatio(); | ||
1597 | } | 1633 | } | ||
1598 | 1634 | | |||
1599 | QPixmap Helper::coloredIcon(const QIcon& icon, const QPalette& palette, const QSize &size, QIcon::Mode mode, QIcon::State state) | 1635 | bool Helper::isInToolsArea(const QWidget* widget) const | ||
1600 | { | 1636 | { | ||
1601 | const QPalette activePalette = KIconLoader::global()->customPalette(); | 1637 | if (!shouldDrawToolsArea(widget)) return false; | ||
1602 | const bool changePalette = activePalette != palette; | 1638 | | ||
1603 | if (changePalette) { | 1639 | auto grabMainWindow = [](const QWidget *widget) { | ||
1604 | KIconLoader::global()->setCustomPalette(palette); | 1640 | auto window = qobject_cast<QMainWindow*>(widget->window()); | ||
1605 | } | 1641 | return window; | ||
1606 | const QPixmap pixmap = icon.pixmap(size, mode, state); | 1642 | }; | ||
1607 | if (changePalette) { | 1643 | auto checkToolbarInToolsArea = [this, grabMainWindow](const QWidget* widget) { | ||
1608 | if (activePalette == QPalette()) { | 1644 | auto toolbar = qobject_cast<const QToolBar*>(widget); | ||
1609 | KIconLoader::global()->resetPalette(); | 1645 | if (!toolbar) return false; | ||
1646 | | ||||
1647 | QMainWindow* window = grabMainWindow(widget); | ||||
1648 | if (window) { | ||||
1649 | auto rect = toolsAreaToolbarsRect(widget); | ||||
1650 | if (widget->parentWidget() != widget->window()) return false; | ||||
1651 | if (toolbar->isFloating()) return false; | ||||
1652 | if (toolbar->orientation() == Qt::Vertical) return false; | ||||
1653 | if (window->toolBarArea(const_cast<QToolBar*>(toolbar)) != Qt::TopToolBarArea) return false; | ||||
1654 | if (window->width() != rect.width()) return false; | ||||
1655 | } | ||||
1656 | | ||||
1657 | return true; | ||||
1658 | }; | ||||
1659 | auto checkMenubarInToolsArea = [grabMainWindow](const QWidget *widget) { | ||||
1660 | QMainWindow* window = grabMainWindow(widget); | ||||
1661 | if (window) { | ||||
1662 | if (window->menuWidget() == widget) { | ||||
1663 | return true; | ||||
1664 | } | ||||
1665 | } | ||||
1666 | | ||||
1667 | return false; | ||||
1668 | }; | ||||
1669 | | ||||
1670 | if (!widget) return false; | ||||
1671 | | ||||
1672 | if (!widget->isVisible()) { | ||||
1673 | return false; | ||||
1674 | } | ||||
1675 | if (widget->window()->windowType() == Qt::Dialog) { | ||||
1676 | return false; | ||||
1677 | } | ||||
1678 | | ||||
1679 | auto parent = widget; | ||||
1680 | while (parent != nullptr) { | ||||
1681 | if (qobject_cast<const QMdiArea*>(parent) || qobject_cast<const QDockWidget*>(parent)) { | ||||
1682 | return false; | ||||
1683 | } | ||||
1684 | if (checkToolbarInToolsArea(parent)) { | ||||
1685 | return true; | ||||
1686 | } | ||||
1687 | if (checkMenubarInToolsArea(parent)) { | ||||
1688 | return true; | ||||
1689 | } | ||||
1690 | parent = parent->parentWidget(); | ||||
1691 | } | ||||
1692 | | ||||
1693 | return false; | ||||
1694 | } | ||||
1695 | | ||||
1696 | QRect Helper::toolsAreaToolbarsRect (const QWidget* widget) const { | ||||
1697 | auto window = qobject_cast<QMainWindow*>(widget->window()); | ||||
1698 | if (!window) return QRect(); | ||||
1699 | | ||||
1700 | auto handle = window->windowHandle(); | ||||
1701 | if (handle) { | ||||
1702 | if (_invalidateCachedRects) { | ||||
1703 | _cachedRects.clear(); | ||||
1704 | _invalidateCachedRects = false; | ||||
1705 | } | ||||
1706 | auto key = _cachedRects.constFind(handle); | ||||
1707 | if (key != _cachedRects.constEnd()) { | ||||
1708 | return *key; | ||||
1709 | } | ||||
1710 | } | ||||
1711 | | ||||
1712 | QList<QToolBar*> widgets = window->findChildren<QToolBar*>(QString(), Qt::FindDirectChildrenOnly); | ||||
1713 | QRect rect = QRect(); | ||||
1714 | for (auto toolbar : widgets) { | ||||
1715 | auto isInSubWindow = false; | ||||
1716 | QWidget* parent = toolbar->parentWidget(); | ||||
1717 | while (parent != nullptr) { | ||||
1718 | if (qobject_cast<QMdiArea*>(parent) || qobject_cast<QDockWidget*>(parent)) { | ||||
1719 | isInSubWindow = true; | ||||
1720 | break; | ||||
1721 | } | ||||
1722 | parent = parent->parentWidget(); | ||||
1723 | } | ||||
1724 | if (!isInSubWindow) { | ||||
1725 | if (window->toolBarArea(toolbar) == Qt::TopToolBarArea) { | ||||
1726 | rect = rect.united(toolbar->geometry()); | ||||
1727 | } | ||||
1728 | } | ||||
1729 | } | ||||
1730 | QList<QMenuBar*> menuWidgets = window->findChildren<QMenuBar*>(QString(), Qt::FindDirectChildrenOnly); | ||||
1731 | for (auto menubar : menuWidgets) { | ||||
1732 | rect = rect.united(menubar->geometry()); | ||||
1733 | } | ||||
1734 | | ||||
1735 | if (handle) { | ||||
1736 | _cachedRects.insert(handle, rect); | ||||
1737 | } | ||||
1738 | return rect; | ||||
1739 | } | ||||
1740 | | ||||
1741 | bool Helper::toolsAreaHasToolBar (const QWidget* widget) const { | ||||
1742 | if (!shouldDrawToolsArea(widget)) return false; | ||||
1743 | | ||||
1744 | auto mainWindow = qobject_cast<QMainWindow*>(widget->window()); | ||||
1745 | if (mainWindow == nullptr) { | ||||
1746 | return false; | ||||
1747 | } | ||||
1748 | | ||||
1749 | QList<QToolBar*> widgets = mainWindow->findChildren<QToolBar*>(QString(), Qt::FindDirectChildrenOnly); | ||||
1750 | for (auto widget : widgets) { | ||||
1751 | if (isInToolsArea(widget) == true) { | ||||
1752 | return true; | ||||
1753 | } | ||||
1754 | } | ||||
1755 | | ||||
1756 | return false; | ||||
1757 | } | ||||
1758 | | ||||
1759 | QToolBar* Helper::grabToolBarForToolsArea(const QWidget *widget) const { | ||||
1760 | auto mainWindow = qobject_cast<QMainWindow*>(widget->window()); | ||||
1761 | if (mainWindow == nullptr) { | ||||
1762 | return nullptr; | ||||
1763 | } | ||||
1764 | | ||||
1765 | QList<QToolBar*> widgets = mainWindow->findChildren<QToolBar*>(QString(), Qt::FindDirectChildrenOnly); | ||||
1766 | for (auto widget : widgets) { | ||||
1767 | if (isInToolsArea(widget)) { | ||||
1768 | return widget; | ||||
1769 | } | ||||
1770 | } | ||||
1771 | | ||||
1772 | return nullptr; | ||||
1773 | } | ||||
1774 | | ||||
1775 | bool Helper::shouldDrawToolsArea(const QWidget* widget) const { | ||||
1776 | if (!widget) return false; | ||||
1777 | if (!_toolsAreaEnabled) return false; | ||||
1778 | static bool isAuto; | ||||
1779 | static QString borderSize; | ||||
1780 | if (!_cachedAutoValid) { | ||||
1781 | KConfigGroup kdecorationGroup(_kwinConfig->group("org.kde.kdecoration2")); | ||||
1782 | isAuto = kdecorationGroup.readEntry("BorderSizeAuto", true); | ||||
1783 | borderSize = kdecorationGroup.readEntry("BorderSize", "Normal"); | ||||
1784 | _cachedAutoValid = true; | ||||
1785 | } | ||||
1786 | if (isAuto) { | ||||
1787 | auto window = widget->window(); | ||||
1788 | if (window) { | ||||
1789 | auto handle = window->windowHandle(); | ||||
1790 | if (handle) { | ||||
1791 | if (handle->frameGeometry().width() != toolsAreaToolbarsRect(widget).width()) { | ||||
1792 | return false; | ||||
1793 | } else { | ||||
1794 | auto toolbar = qobject_cast<const QToolBar*>(widget); | ||||
1795 | if (toolbar) { | ||||
1796 | if (toolbar->isFloating()) { | ||||
1797 | return false; | ||||
1798 | } | ||||
1799 | } | ||||
1800 | return true; | ||||
1801 | } | ||||
1802 | } | ||||
1610 | } else { | 1803 | } else { | ||
1611 | KIconLoader::global()->setCustomPalette(palette); | 1804 | return false; | ||
1805 | } | ||||
1806 | } | ||||
1807 | if (borderSize != "None" && borderSize != "NoSides") { | ||||
1808 | return false; | ||||
1809 | } | ||||
1810 | auto toolbar = qobject_cast<const QToolBar*>(widget); | ||||
1811 | if (toolbar) { | ||||
1812 | if (toolbar->isFloating()) { | ||||
1813 | return false; | ||||
1814 | } | ||||
1815 | } | ||||
1816 | return true; | ||||
1817 | } | ||||
1818 | | ||||
1819 | bool Helper::toolsAreaHasContents(const QWidget* widget) const { | ||||
1820 | QList<QWidget*> widgets = widget->window()->findChildren<QWidget*>(QString(), Qt::FindDirectChildrenOnly); | ||||
1821 | for (auto widget : widgets) { | ||||
1822 | if (isInToolsArea(widget)) { | ||||
1823 | return true; | ||||
1612 | } | 1824 | } | ||
1613 | } | 1825 | } | ||
1614 | return pixmap; | 1826 | return false; | ||
1827 | } | ||||
1828 | | ||||
1829 | QColor Helper::toolsAreaBorderColor(const QWidget* widget) const { | ||||
1830 | QColor border( | ||||
1831 | KColorUtils::mix( | ||||
1832 | titleBarColor(widget->isActiveWindow()), | ||||
1833 | titleBarTextColor(widget->isActiveWindow()), | ||||
1834 | 0.2 | ||||
1835 | ) | ||||
1836 | ); | ||||
1837 | border.setAlpha(255); | ||||
1838 | return border; | ||||
1615 | } | 1839 | } | ||
1616 | } | 1840 | } |