game_launcher 1.1.0
Modern Linux game launcher with Steam / Proton / Wine integration
Loading...
Searching...
No Matches
toolbar_drag_handle.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// ==============================================================================
3// qt/toolbar_drag_handle.cpp
4// ==============================================================================
5
7#include "theme_manager.h"
8
9#include <QApplication>
10
11ToolbarDragHandle::ToolbarDragHandle(QWidget* leftWidget, QWidget* rightWidget, QWidget* parent,
12 ThemeManager* themeManager)
13 : QFrame(parent), leftWidget_(leftWidget), rightWidget_(rightWidget),
14 themeManager_(themeManager ? themeManager : &ThemeManager::instance())
15{
16 setFrameShape(QFrame::VLine);
17 setFixedWidth(5);
18 // Only show resize cursor if we have a widget to resize
19 if (leftWidget_ || rightWidget_)
20 setCursor(Qt::SplitHCursor);
21 setMouseTracking(true);
22 updateStyle();
23 // Keep the handle colour in sync when the user switches themes —
24 // updateStyle() otherwise only re-runs on hover/press/release state
25 // changes, so a mid-hover theme swap would leave the old accent frozen.
26 connect(themeManager_, &ThemeManager::themeChanged, this, &ToolbarDragHandle::updateStyle);
27}
28
29void ToolbarDragHandle::setLeftWidgetConstraints(int minWidth, int maxWidth)
30{
31 minWidth_ = minWidth;
32 maxWidth_ = maxWidth;
33}
34
35void ToolbarDragHandle::updateStyle()
36{
37 const ThemeColors& c = themeManager_->colors();
38
39 QString color;
40 if (dragging_)
41 color = c.accent.name();
42 else if (hovered_ && (leftWidget_ || rightWidget_))
43 color = c.accent.lighter(130).name();
44 else
45 color = c.border.name();
46
47 setStyleSheet(QString("QFrame { background-color: %1; border: none; }").arg(color));
48}
49
50void ToolbarDragHandle::mousePressEvent(QMouseEvent* event)
51{
52 if (event->button() == Qt::LeftButton && (leftWidget_ || rightWidget_))
53 {
54 dragging_ = true;
55 dragStartX_ = event->globalPosition().toPoint().x();
56 // Get original width from whichever widget we're resizing
57 if (leftWidget_)
58 originalWidth_ = leftWidget_->width();
59 else if (rightWidget_)
60 originalWidth_ = rightWidget_->width();
61 updateStyle();
62 event->accept();
63 }
64 else
65 {
66 QFrame::mousePressEvent(event);
67 }
68}
69
70void ToolbarDragHandle::mouseMoveEvent(QMouseEvent* event)
71{
72 if (dragging_)
73 {
74 int delta = event->globalPosition().toPoint().x() - dragStartX_;
75
76 if (leftWidget_)
77 {
78 // Dragging right makes left widget wider
79 int newWidth = qBound(minWidth_, originalWidth_ + delta, maxWidth_);
80 leftWidget_->setMinimumWidth(newWidth);
81 leftWidget_->setMaximumWidth(newWidth);
82 emit widthChanged(newWidth);
83 }
84 else if (rightWidget_)
85 {
86 // Dragging right makes right widget narrower (inverse)
87 int newWidth = qBound(minWidth_, originalWidth_ - delta, maxWidth_);
88 rightWidget_->setMinimumWidth(newWidth);
89 rightWidget_->setMaximumWidth(newWidth);
90 emit widthChanged(newWidth);
91 }
92 event->accept();
93 }
94 else
95 {
96 QFrame::mouseMoveEvent(event);
97 }
98}
99
101{
102 if (event->button() == Qt::LeftButton)
103 {
104 dragging_ = false;
105 updateStyle();
106 event->accept();
107 }
108 else
109 {
110 QFrame::mouseReleaseEvent(event);
111 }
112}
113
114void ToolbarDragHandle::enterEvent(QEnterEvent* event)
115{
116 hovered_ = true;
117 updateStyle();
118 QFrame::enterEvent(event);
119}
120
122{
123 hovered_ = false;
124 updateStyle();
125 QFrame::leaveEvent(event);
126}
void themeChanged()
Emitted when theme or accent color changes.
const ThemeColors & colors() const
Get effective colors (with accent customization applied).
void widthChanged(int newWidth)
void mouseReleaseEvent(QMouseEvent *event) override
void mouseMoveEvent(QMouseEvent *event) override
void setLeftWidgetConstraints(int minWidth, int maxWidth)
void enterEvent(QEnterEvent *event) override
ToolbarDragHandle(QWidget *leftWidget, QWidget *rightWidget, QWidget *parent=nullptr, ThemeManager *themeManager=nullptr)
void mousePressEvent(QMouseEvent *event) override
void leaveEvent(QEvent *event) override