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 void TitleBarWidget::init()
111 qCDebug(creation) <<
"TitleBarWidget" <<
this;
112 m_dockWidgetIcon =
new QLabel(
this);
113 m_layout->addWidget(m_dockWidgetIcon);
115 m_layout->addStretch();
118 auto factory = Config::self().frameworkWidgetFactory();
120 m_maximizeButton = factory->createTitleBarButton(
this, TitleBarButtonType::Maximize);
121 m_minimizeButton = factory->createTitleBarButton(
this, TitleBarButtonType::Minimize);
122 m_floatButton = factory->createTitleBarButton(
this, TitleBarButtonType::Float);
123 m_closeButton = factory->createTitleBarButton(
this, TitleBarButtonType::Close);
124 m_autoHideButton = factory->createTitleBarButton(
this, TitleBarButtonType::AutoHide);
126 m_layout->addWidget(m_autoHideButton);
127 m_layout->addWidget(m_minimizeButton);
128 m_layout->addWidget(m_maximizeButton);
129 m_layout->addWidget(m_floatButton);
130 m_layout->addWidget(m_closeButton);
132 m_autoHideButton->setVisible(
false);
140 updateMaximizeButton();
141 updateMinimizeButton();
143 m_minimizeButton->setToolTip(tr(
"Minimize"));
144 m_closeButton->setToolTip(tr(
"Close"));
146 connect(
this, &TitleBar::titleChanged,
this, [
this] {
150 connect(
this, &TitleBar::iconChanged,
this, [
this] {
151 if (icon().isNull()) {
152 m_dockWidgetIcon->setPixmap(
QPixmap());
155 m_dockWidgetIcon->setPixmap(pix);
160 m_closeButton->setEnabled(closeButtonEnabled());
165 m_floatButton->setVisible(floatButtonVisible());
166 m_floatButton->setToolTip(floatButtonToolTip());
168 connect(DockRegistry::self(), &DockRegistry::windowChangedScreen,
this, [
this](
QWindow *w) {
169 if (w == window()->windowHandle())
174 void TitleBarWidget::updateMargins()
176 const qreal factor = logicalDpiFactor(
this);
177 m_layout->setContentsMargins(
QMargins(2, 2, 2, 2) * factor);
178 m_layout->setSpacing(
int(2 * factor));
181 QSize TitleBarWidget::sizeHint()
const
190 return QSize(0, height);
193 QRect TitleBarWidget::iconRect()
const
195 if (icon().isNull()) {
196 return QRect(0, 0, 0, 0);
198 return QRect(3, 3, 30, 30);
202 int TitleBarWidget::buttonAreaWidth()
const
204 int smallestX = width();
206 for (
auto button : { m_autoHideButton, m_minimizeButton, m_floatButton, m_maximizeButton, m_closeButton }) {
207 if (button->isVisible() && button->x() < smallestX)
208 smallestX = button->x();
211 return width() - smallestX;
214 TitleBarWidget::~TitleBarWidget()
217 for (
auto button : { m_autoHideButton, m_minimizeButton, m_floatButton, m_maximizeButton, m_closeButton }) {
218 button->setParent(
nullptr);
219 button->deleteLater();
223 void TitleBarWidget::mouseDoubleClickEvent(
QMouseEvent *e)
229 QWidget *TitleBarWidget::closeButton()
const
231 return m_closeButton;
241 titleOpt.title = title();
242 titleOpt.rect = iconRect().isEmpty() ? rect().adjusted(2, 0, -buttonAreaWidth(), 0)
243 : rect().adjusted(iconRect().
right(), 0, -buttonAreaWidth(), 0);
247 p.fillRect(rect().adjusted(1, 1, -1, 0), c);
253 void TitleBarWidget::updateMinimizeButton()
255 m_minimizeButton->setVisible(supportsMinimizeButton());
258 void TitleBarWidget::updateAutoHideButton()
260 if (Config::self().flags() & Config::Flag_AutoHideSupport) {
261 auto factory = Config::self().frameworkWidgetFactory();
262 if (
const Frame *f = frame()) {
263 if (f->isInMainWindow()) {
264 m_autoHideButton->setIcon(factory->iconForButtonType(TitleBarButtonType::AutoHide, devicePixelRatioF()));
265 m_autoHideButton->setToolTip(tr(
"Auto-hide"));
266 }
else if (f->isOverlayed()) {
267 m_autoHideButton->setIcon(factory->iconForButtonType(TitleBarButtonType::UnautoHide, devicePixelRatioF()));
268 m_autoHideButton->setToolTip(tr(
"Disable auto-hide"));
273 m_autoHideButton->setVisible(supportsAutoHideButton());
276 void TitleBarWidget::updateMaximizeButton()
278 if (
auto fw = floatingWindow()) {
279 auto factory = Config::self().frameworkWidgetFactory();
280 const TitleBarButtonType iconType = fw->isMaximizedOverride() ? TitleBarButtonType::Normal
281 : TitleBarButtonType::Maximize;
282 m_maximizeButton->setIcon(factory->iconForButtonType(iconType, devicePixelRatioF()));
284 m_maximizeButton->setVisible(supportsMaximizeButton());
285 m_maximizeButton->setToolTip(fw->isMaximizedOverride() ? tr(
"Restore") : tr(
"Maximize"));
287 m_maximizeButton->setVisible(
false);
291 #ifdef DOCKS_DEVELOPER_MODE
293 bool TitleBarWidget::isCloseButtonVisible()
const
295 return m_closeButton->isVisible();
298 bool TitleBarWidget::isCloseButtonEnabled()
const
300 return m_closeButton->isEnabled();
303 bool TitleBarWidget::isFloatButtonVisible()
const
305 return m_floatButton->isVisible();
308 bool TitleBarWidget::isFloatButtonEnabled()
const
310 return m_floatButton->isEnabled();