Changeset View
Changeset View
Standalone View
Standalone View
kstyle/breezetoolsareamanager.cpp
- This file was added.
1 | #include "breezetoolsareamanager.h" | ||||
---|---|---|---|---|---|
2 | #include <QObject> | ||||
3 | #include <QWidget> | ||||
4 | #include <QToolBar> | ||||
5 | #include <QWindow> | ||||
6 | #include <QMainWindow> | ||||
7 | #include <QDebug> | ||||
8 | | ||||
9 | namespace Breeze { | ||||
10 | ToolsAreaManager::ToolsAreaManager(Helper *helper, QObject *parent) : QObject(parent), _helper(helper) {} | ||||
11 | | ||||
12 | ToolsAreaManager::~ToolsAreaManager() {} | ||||
13 | | ||||
14 | void ToolsAreaManager::updateAnimations() { | ||||
15 | for (auto entry : animationMap) { | ||||
16 | entry.foregroundColorAnimation->setStartValue(_helper->titleBarTextColor(false)); | ||||
17 | entry.foregroundColorAnimation->setEndValue(_helper->titleBarTextColor(true)); | ||||
18 | | ||||
19 | entry.backgroundColorAnimation->setStartValue(_helper->titleBarColor(false)); | ||||
20 | entry.backgroundColorAnimation->setEndValue(_helper->titleBarColor(true)); | ||||
21 | | ||||
22 | entry.foregroundColorAnimation->setDuration( | ||||
23 | _helper->decorationConfig()->animationsEnabled() ? | ||||
24 | _helper->decorationConfig()->animationsDuration() : | ||||
25 | 0 | ||||
26 | ); | ||||
27 | entry.backgroundColorAnimation->setDuration( | ||||
28 | _helper->decorationConfig()->animationsEnabled() ? | ||||
29 | _helper->decorationConfig()->animationsDuration() : | ||||
30 | 0 | ||||
31 | ); | ||||
32 | } | ||||
33 | } | ||||
34 | | ||||
35 | void ToolsAreaManager::registerAnimation(QWidget *widget) { | ||||
36 | auto window = widget->window()->windowHandle(); | ||||
37 | if (window && !animationMap.contains(window)) { | ||||
38 | | ||||
39 | auto foregroundColorAnimation = new QVariantAnimation(this); | ||||
40 | connect(foregroundColorAnimation, &QVariantAnimation::valueChanged, | ||||
41 | this, &ToolsAreaManager::toolbarUpdated); | ||||
42 | | ||||
43 | auto backgroundColorAnimation = new QVariantAnimation(this); | ||||
44 | connect(backgroundColorAnimation, &QVariantAnimation::valueChanged, | ||||
45 | this, &ToolsAreaManager::toolbarUpdated); | ||||
46 | | ||||
47 | foregroundColorAnimation->setStartValue(_helper->titleBarTextColor(false)); | ||||
48 | foregroundColorAnimation->setEndValue(_helper->titleBarTextColor(true)); | ||||
49 | | ||||
50 | backgroundColorAnimation->setStartValue(_helper->titleBarColor(false)); | ||||
51 | backgroundColorAnimation->setEndValue(_helper->titleBarColor(true)); | ||||
52 | | ||||
53 | foregroundColorAnimation->setDuration( | ||||
54 | _helper->decorationConfig()->animationsEnabled() ? | ||||
55 | _helper->decorationConfig()->animationsDuration() : | ||||
56 | 0 | ||||
57 | ); | ||||
58 | backgroundColorAnimation->setDuration( | ||||
59 | _helper->decorationConfig()->animationsEnabled() ? | ||||
60 | _helper->decorationConfig()->animationsDuration() : | ||||
61 | 0 | ||||
62 | ); | ||||
63 | | ||||
64 | animationMap[window] = ToolsAreaAnimation{ | ||||
65 | foregroundColorAnimation, | ||||
66 | backgroundColorAnimation, | ||||
67 | window->isActive(), | ||||
68 | }; | ||||
69 | | ||||
70 | connect(window, &QWindow::activeChanged, | ||||
71 | this, [=]() { | ||||
72 | if (animationMap[window].foregroundColorAnimation.isNull() || animationMap[window].backgroundColorAnimation.isNull()) return; | ||||
73 | | ||||
74 | auto prevActive = animationMap[window].prevActive; | ||||
75 | if (prevActive && !window->isActive()) { | ||||
76 | animationMap[window].foregroundColorAnimation->setDirection(QAbstractAnimation::Backward); | ||||
77 | animationMap[window].backgroundColorAnimation->setDirection(QAbstractAnimation::Backward); | ||||
78 | | ||||
79 | animationMap[window].foregroundColorAnimation->start(); | ||||
80 | animationMap[window].backgroundColorAnimation->start(); | ||||
81 | } else if (!prevActive && window->isActive()) { | ||||
82 | animationMap[window].foregroundColorAnimation->setDirection(QAbstractAnimation::Forward); | ||||
83 | animationMap[window].backgroundColorAnimation->setDirection(QAbstractAnimation::Forward); | ||||
84 | | ||||
85 | animationMap[window].foregroundColorAnimation->start(); | ||||
86 | animationMap[window].backgroundColorAnimation->start(); | ||||
87 | } | ||||
88 | animationMap[window].prevActive = window->isActive(); | ||||
89 | }); | ||||
90 | | ||||
91 | } | ||||
92 | } | ||||
93 | | ||||
94 | bool ToolsAreaManager::animationRunning(const QWidget *widget) { | ||||
95 | auto window = widget->window()->windowHandle(); | ||||
96 | if (window && animationMap.contains(window)) { | ||||
97 | return ( | ||||
98 | animationMap[window].foregroundColorAnimation->state() == QAbstractAnimation::Running | ||||
99 | && | ||||
100 | animationMap[window].backgroundColorAnimation->state() == QAbstractAnimation::Running | ||||
101 | ); | ||||
102 | } | ||||
103 | return false; | ||||
104 | } | ||||
105 | | ||||
106 | QColor ToolsAreaManager::foreground(const QWidget *widget) { | ||||
107 | auto window = widget->window()->windowHandle(); | ||||
108 | if (window && animationMap.contains(window) && animationMap[window].foregroundColorAnimation) { | ||||
109 | return animationMap[window].foregroundColorAnimation->currentValue().value<QColor>(); | ||||
110 | } | ||||
111 | return QColor(); | ||||
112 | } | ||||
113 | | ||||
114 | QColor ToolsAreaManager::background(const QWidget *widget) { | ||||
115 | auto window = widget->window()->windowHandle(); | ||||
116 | if (window && animationMap.contains(window) && animationMap[window].backgroundColorAnimation) { | ||||
117 | return animationMap[window].backgroundColorAnimation->currentValue().value<QColor>(); | ||||
118 | } | ||||
119 | return QColor(); | ||||
120 | } | ||||
121 | | ||||
122 | void ToolsAreaManager::registerWindow(QWindow *window) | ||||
123 | { | ||||
124 | if (!_registeredWindows.contains(window)) { | ||||
125 | auto geoUpdate = [=]() { | ||||
126 | _helper->_invalidateCachedRects = true; | ||||
127 | emit toolbarUpdated(); | ||||
128 | }; | ||||
129 | connect(window, &QWindow::widthChanged, geoUpdate); | ||||
130 | connect(window, &QWindow::heightChanged, geoUpdate); | ||||
131 | connect(window, &QWindow::destroyed, geoUpdate); | ||||
132 | _registeredWindows << window; | ||||
133 | } | ||||
134 | } | ||||
135 | | ||||
136 | void ToolsAreaManager::registerWidget(QWidget *widget) | ||||
137 | { | ||||
138 | auto win = widget->window(); | ||||
139 | if (win) { | ||||
140 | auto handle = win->windowHandle(); | ||||
141 | if (handle) { | ||||
142 | registerWindow(handle); | ||||
143 | } | ||||
144 | } | ||||
145 | auto window = qobject_cast<QMainWindow*> (widget); | ||||
146 | if (window) { | ||||
147 | connect(this, &ToolsAreaManager::toolbarUpdated, | ||||
148 | window, [this, window]() { | ||||
149 | if (_helper->toolsAreaHasContents(window)) { | ||||
150 | window->setContentsMargins(0,0,0,0); | ||||
151 | } else { | ||||
152 | window->setContentsMargins(0,1,0,0); | ||||
153 | } | ||||
154 | }); | ||||
155 | } | ||||
156 | connect(this, &ToolsAreaManager::toolbarUpdated, | ||||
157 | widget, [widget, this]() { | ||||
158 | widget->update(); | ||||
159 | auto win = widget->window(); | ||||
160 | if (win) { | ||||
161 | auto handle = win->windowHandle(); | ||||
162 | if (handle) { | ||||
163 | _helper->_cachedRects.remove(handle); | ||||
164 | } | ||||
165 | } | ||||
166 | }); | ||||
167 | auto toolbar = qobject_cast<QToolBar*>(widget); | ||||
168 | if (toolbar) { | ||||
169 | connect(this, &ToolsAreaManager::toolbarUpdated, | ||||
170 | widget, [=]() { | ||||
171 | const auto rect = _helper->toolsAreaToolbarsRect(widget); | ||||
172 | if (rect.bottom() != widget->geometry().bottom()) { | ||||
173 | toolbar->setContentsMargins(0,0,0,0); | ||||
174 | } else { | ||||
175 | toolbar->setContentsMargins(0,0,0,4); | ||||
176 | } | ||||
177 | }); | ||||
178 | connect(toolbar, &QToolBar::visibilityChanged, | ||||
179 | this, [this]() { | ||||
180 | emit toolbarUpdated(); | ||||
181 | }); | ||||
182 | connect(toolbar, &QToolBar::orientationChanged, | ||||
183 | this, [this]() { | ||||
184 | emit toolbarUpdated(); | ||||
185 | }); | ||||
186 | connect(toolbar, &QToolBar::topLevelChanged, | ||||
187 | this, [this]() { | ||||
188 | emit toolbarUpdated(); | ||||
189 | }); | ||||
190 | } | ||||
191 | connect(widget, &QObject::destroyed, | ||||
192 | this, [this, widget]() { | ||||
193 | unregisterWidget(widget); | ||||
194 | }); | ||||
195 | registerAnimation(widget); | ||||
196 | _registeredWidgets << widget; | ||||
197 | emit toolbarUpdated(); | ||||
198 | } | ||||
199 | | ||||
200 | bool ToolsAreaManager::widgetHasCorrectPaletteSet(const QWidget *widget) | ||||
201 | { | ||||
202 | if (animationRunning(widget)) return true; | ||||
203 | return ( | ||||
204 | widget->palette().color(QPalette::Window) == background(widget) | ||||
205 | && | ||||
206 | widget->palette().color(QPalette::WindowText) == foreground(widget) | ||||
207 | ); | ||||
208 | } | ||||
209 | | ||||
210 | void ToolsAreaManager::unregisterWidget(QWidget *widget) | ||||
211 | { | ||||
212 | if (qobject_cast<QToolBar*>(widget)) widget->setContentsMargins(0,0,0,0); | ||||
213 | _registeredWidgets.remove(widget); | ||||
214 | QList<QWindow*> toRemove; | ||||
215 | for (auto window : animationMap.keys()) { | ||||
216 | if (std::none_of(_registeredWidgets.begin(), _registeredWidgets.end(), [window](QWidget *widget) { | ||||
217 | return window == widget->window()->windowHandle(); | ||||
218 | })) { | ||||
219 | delete animationMap[window].foregroundColorAnimation; | ||||
220 | delete animationMap[window].backgroundColorAnimation; | ||||
221 | toRemove << window; | ||||
222 | } | ||||
223 | } | ||||
224 | for (auto entry : toRemove) { | ||||
225 | animationMap.remove(entry); | ||||
226 | } | ||||
227 | } | ||||
228 | } |