12 #include "TitleBarWidget_p.h"
13 #include "../DragController_p.h"
14 #include "../Frame_p.h"
15 #include "../FloatingWindow_p.h"
16 #include "../Logging_p.h"
17 #include "../WindowBeingDragged_p.h"
18 #include "../Utils_p.h"
19 #include "../DockRegistry_p.h"
23 #include <QHBoxLayout>
25 #include <QMouseEvent>
26 #include <QStyleOption>
40 if (isEnabled() && underMouse()) {
55 const QList<QSize> iconSizes = opt.icon.availableSizes();
59 const qreal logicalFactor = logicalDpiX() / 96.0;
66 #if defined(Q_OS_LINUX)
67 const qreal dpr = devicePixelRatioF();
68 const qreal combinedFactor = logicalFactor * dpr;
70 if (scalingFactorIsSupported(combinedFactor))
71 opt.iconSize = opt.iconSize * combinedFactor;
72 #elif defined(Q_OS_WIN) && QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
75 && scalingFactorIsSupported(logicalFactor))
76 opt.iconSize = opt.iconSize * logicalFactor;
78 Q_UNUSED(logicalFactor);
85 QSize Button::sizeHint()
const
95 TitleBarWidget::TitleBarWidget(Frame *parent)
102 TitleBarWidget::TitleBarWidget(FloatingWindow *parent)
109 TitleBarWidget::TitleBarWidget(
QWidget *parent)
117 void TitleBarWidget::init()
119 qCDebug(creation) <<
"TitleBarWidget" <<
this;
120 m_dockWidgetIcon =
new QLabel(
this);
121 m_layout->addWidget(m_dockWidgetIcon);
123 m_layout->addStretch();
126 auto factory = Config::self().frameworkWidgetFactory();
128 m_maximizeButton = factory->createTitleBarButton(
this, TitleBarButtonType::Maximize);
129 m_minimizeButton = factory->createTitleBarButton(
this, TitleBarButtonType::Minimize);
130 m_floatButton = factory->createTitleBarButton(
this, TitleBarButtonType::Float);
131 m_closeButton = factory->createTitleBarButton(
this, TitleBarButtonType::Close);
132 m_autoHideButton = factory->createTitleBarButton(
this, TitleBarButtonType::AutoHide);
134 m_layout->addWidget(m_autoHideButton);
135 m_layout->addWidget(m_minimizeButton);
136 m_layout->addWidget(m_maximizeButton);
137 m_layout->addWidget(m_floatButton);
138 m_layout->addWidget(m_closeButton);
140 m_autoHideButton->setVisible(
false);
148 updateMaximizeButton();
149 updateMinimizeButton();
151 m_minimizeButton->setToolTip(tr(
"Minimize"));
152 m_closeButton->setToolTip(tr(
"Close"));
154 connect(
this, &TitleBar::titleChanged,
this, [
this] {
158 connect(
this, &TitleBar::iconChanged,
this, [
this] {
159 if (icon().isNull()) {
160 m_dockWidgetIcon->setPixmap(
QPixmap());
163 m_dockWidgetIcon->setPixmap(pix);
168 m_closeButton->setEnabled(closeButtonEnabled());
173 m_floatButton->setVisible(floatButtonVisible());
174 m_floatButton->setToolTip(floatButtonToolTip());
176 connect(DockRegistry::self(), &DockRegistry::windowChangedScreen,
this, [
this](
QWindow *w) {
177 if (w == window()->windowHandle())
182 void TitleBarWidget::updateMargins()
184 const qreal factor = logicalDpiFactor(
this);
185 m_layout->setContentsMargins(
QMargins(2, 2, 2, 2) * factor);
186 m_layout->setSpacing(
int(2 * factor));
189 QSize TitleBarWidget::sizeHint()
const
198 return QSize(0, height);
201 QRect TitleBarWidget::iconRect()
const
203 if (icon().isNull()) {
204 return QRect(0, 0, 0, 0);
206 return QRect(3, 3, 30, 30);
210 int TitleBarWidget::buttonAreaWidth()
const
212 int smallestX = width();
214 for (
auto button : { m_autoHideButton, m_minimizeButton, m_floatButton, m_maximizeButton, m_closeButton }) {
215 if (button->isVisible() && button->x() < smallestX)
216 smallestX = button->x();
219 return width() - smallestX;
222 TitleBarWidget::~TitleBarWidget()
225 for (
auto button : { m_autoHideButton, m_minimizeButton, m_floatButton, m_maximizeButton, m_closeButton }) {
226 button->setParent(
nullptr);
227 button->deleteLater();
231 void TitleBarWidget::mouseDoubleClickEvent(
QMouseEvent *e)
237 QWidget *TitleBarWidget::closeButton()
const
239 return m_closeButton;
249 titleOpt.title = title();
250 titleOpt.rect = iconRect().isEmpty() ? rect().adjusted(2, 0, -buttonAreaWidth(), 0)
251 : rect().adjusted(iconRect().
right(), 0, -buttonAreaWidth(), 0);
255 p.fillRect(rect().adjusted(1, 1, -1, 0), c);
261 void TitleBarWidget::updateMinimizeButton()
263 m_minimizeButton->setVisible(supportsMinimizeButton());
266 void TitleBarWidget::updateAutoHideButton()
268 if (Config::self().flags() & Config::Flag_AutoHideSupport) {
269 auto factory = Config::self().frameworkWidgetFactory();
270 if (
const Frame *f = frame()) {
271 if (f->isInMainWindow()) {
272 m_autoHideButton->setIcon(factory->iconForButtonType(TitleBarButtonType::AutoHide, devicePixelRatioF()));
273 m_autoHideButton->setToolTip(tr(
"Auto-hide"));
274 }
else if (f->isOverlayed()) {
275 m_autoHideButton->setIcon(factory->iconForButtonType(TitleBarButtonType::UnautoHide, devicePixelRatioF()));
276 m_autoHideButton->setToolTip(tr(
"Disable auto-hide"));
281 m_autoHideButton->setVisible(supportsAutoHideButton());
284 void TitleBarWidget::updateMaximizeButton()
286 if (
auto fw = floatingWindow()) {
287 auto factory = Config::self().frameworkWidgetFactory();
288 const TitleBarButtonType iconType = fw->isMaximizedOverride() ? TitleBarButtonType::Normal
289 : TitleBarButtonType::Maximize;
290 m_maximizeButton->setIcon(factory->iconForButtonType(iconType, devicePixelRatioF()));
292 m_maximizeButton->setVisible(supportsMaximizeButton());
293 m_maximizeButton->setToolTip(fw->isMaximizedOverride() ? tr(
"Restore") : tr(
"Maximize"));
295 m_maximizeButton->setVisible(
false);
299 #ifdef DOCKS_DEVELOPER_MODE
301 bool TitleBarWidget::isCloseButtonVisible()
const
303 return m_closeButton->isVisible();
306 bool TitleBarWidget::isCloseButtonEnabled()
const
308 return m_closeButton->isEnabled();
311 bool TitleBarWidget::isFloatButtonVisible()
const
313 return m_floatButton->isVisible();
316 bool TitleBarWidget::isFloatButtonEnabled()
const
318 return m_floatButton->isEnabled();