Repository navigation
Expand file tree
/
Copy pathsessiontabs.cpp
More file actions
231 lines (204 loc) · 8.17 KB
/
Copy pathsessiontabs.cpp
File metadata and controls
231 lines (204 loc) · 8.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#include "sessiontabs.h"
#include <QEvent>
#include <QHBoxLayout>
#include <QIcon>
#include <QScrollArea>
#include <QScrollBar>
#include <QStackedWidget>
#include <QTabBar>
#include <QTimer>
#include <QToolButton>
#include <QVBoxLayout>
#include <QWheelEvent>
namespace {
constexpr int kAddGap = 6;
constexpr int kAddWidth = 30;
constexpr int kDockWidth = 36;
constexpr int kScrollGap = 2;
} // namespace
SessionTabs::SessionTabs(QWidget *parent) : QWidget(parent) {
auto *layout = new QVBoxLayout(this);
layout->setContentsMargins(0, 0, 0, 0);
layout->setSpacing(0);
auto *strip = new QWidget(this);
strip->setObjectName(QStringLiteral("sessionTabStrip"));
auto *stripLayout = new QHBoxLayout(strip);
stripLayout->setContentsMargins(0, 0, 0, 0);
stripLayout->setSpacing(0);
layout->addWidget(strip);
// Match the tabs' height, excluding any scrollbar below them.
leadingHost_ = new QWidget(strip);
leadingLayout_ = new QHBoxLayout(leadingHost_);
leadingLayout_->setContentsMargins(0, 0, 0, 0);
stripLayout->addWidget(leadingHost_, 0, Qt::AlignTop);
tabControls_ = new QWidget(strip);
auto *controlsLayout = new QHBoxLayout(tabControls_);
controlsLayout->setContentsMargins(0, 0, 0, 0);
controlsLayout->setSpacing(0);
stripLayout->addWidget(tabControls_, 1);
scrollArea_ = new QScrollArea(tabControls_);
scrollArea_->setObjectName(QStringLiteral("sessionTabScrollArea"));
scrollArea_->setFrameShape(QFrame::NoFrame);
scrollArea_->setAlignment(Qt::AlignLeft | Qt::AlignTop);
scrollArea_->viewport()->setObjectName(QStringLiteral("sessionTabViewport"));
scrollArea_->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
scrollArea_->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
scrollArea_->setFocusPolicy(Qt::NoFocus);
scrollArea_->setMinimumWidth(0);
scrollArea_->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Fixed);
auto *scrollBar = scrollArea_->horizontalScrollBar();
scrollBar->setObjectName(QStringLiteral("sessionTabScrollBar"));
scrollBar->setAccessibleName(tr("横向滚动会话标签"));
scrollBar->setSingleStep(32);
controlsLayout->addWidget(scrollArea_, 1, Qt::AlignTop);
content_ = new QWidget;
// QScrollArea enables autoFillBackground on its content widget.
content_->setObjectName(QStringLiteral("sessionTabContent"));
auto *contentLayout = new QHBoxLayout(content_);
contentLayout->setContentsMargins(0, 0, 0, 0);
contentLayout->setSpacing(kAddGap);
bar_ = new QTabBar(content_);
bar_->setDrawBase(false);
bar_->setExpanding(false);
bar_->setUsesScrollButtons(false);
bar_->setElideMode(Qt::ElideNone);
bar_->setTabsClosable(true);
bar_->setIconSize(QSize(18, 18));
contentLayout->addWidget(bar_);
auto makeAddButton = [this](QWidget *parent) {
auto *button = new QToolButton(parent);
button->setObjectName(QStringLiteral("tabBarAddButton"));
button->setAutoRaise(true);
button->setIcon(QIcon(QStringLiteral(":/icons/add.png")));
button->setIconSize(QSize(18, 18));
button->setFixedSize(kAddWidth, kAddWidth);
button->setCursor(Qt::PointingHandCursor);
button->setAccessibleName(tr("新建本地终端"));
button->setToolTip(tr("新建本地终端 (Ctrl+Shift+T)"));
connect(button, &QToolButton::clicked, this, &SessionTabs::newTabRequested);
return button;
};
inlineAdd_ = makeAddButton(content_);
contentLayout->addWidget(inlineAdd_, 0, Qt::AlignVCenter);
scrollArea_->setWidget(content_);
dock_ = new QWidget(tabControls_);
dock_->setFixedWidth(kDockWidth);
auto *dockLayout = new QHBoxLayout(dock_);
dockLayout->setContentsMargins(3, 0, 3, 0);
dockLayout->addWidget(makeAddButton(dock_), 0, Qt::AlignVCenter);
controlsLayout->addWidget(dock_, 0, Qt::AlignTop);
dock_->hide();
pages_ = new QStackedWidget(this);
pages_->setObjectName(QStringLiteral("sessionTabPages"));
layout->addWidget(pages_, 1);
connect(bar_, &QTabBar::tabCloseRequested, this,
&SessionTabs::tabCloseRequested);
connect(bar_, &QTabBar::currentChanged, this, [this](int index) {
pages_->setCurrentIndex(index);
revealCurrentTab();
});
connect(pages_, &QStackedWidget::widgetRemoved, this, [this](int index) {
bar_->removeTab(index);
updateStripSize();
setCurrentIndex(bar_->currentIndex());
});
tabControls_->installEventFilter(this);
bar_->installEventFilter(this);
updateStripSize();
}
void SessionTabs::setLeadingWidget(QWidget *widget) {
leadingLayout_->addWidget(widget, 0, Qt::AlignVCenter);
}
int SessionTabs::count() const { return pages_->count(); }
int SessionTabs::indexOf(QWidget *page) const { return pages_->indexOf(page); }
QWidget *SessionTabs::widget(int index) const { return pages_->widget(index); }
int SessionTabs::addTab(QWidget *page, const QIcon &icon, const QString &title) {
return insertTab(count(), page, icon, title);
}
int SessionTabs::insertTab(int index, QWidget *page, const QIcon &icon,
const QString &title) {
index = pages_->insertWidget(index, page);
bar_->insertTab(index, icon, title);
updateStripSize();
setCurrentIndex(bar_->currentIndex());
return index;
}
void SessionTabs::removeTab(int index) {
if (auto *page = pages_->widget(index)) {
pages_->removeWidget(page);
}
}
void SessionTabs::setCurrentIndex(int index) {
if (index < 0 || index >= count()) {
return;
}
bar_->setCurrentIndex(index);
pages_->setCurrentIndex(index);
revealCurrentTab();
}
QString SessionTabs::tabText(int index) const { return bar_->tabText(index); }
QIcon SessionTabs::tabIcon(int index) const { return bar_->tabIcon(index); }
QString SessionTabs::tabToolTip(int index) const { return bar_->tabToolTip(index); }
void SessionTabs::setTabToolTip(int index, const QString &tooltip) {
bar_->setTabToolTip(index, tooltip);
}
bool SessionTabs::eventFilter(QObject *watched, QEvent *event) {
if (watched == tabControls_ && event->type() == QEvent::Resize) {
updateStripSize();
} else if (watched == bar_ && (event->type() == QEvent::FontChange ||
event->type() == QEvent::StyleChange)) {
QTimer::singleShot(0, this, [this]() { updateStripSize(); });
} else if (watched == bar_ && event->type() == QEvent::Wheel) {
// Qt normally uses the wheel to select another tab. Pan the strip instead.
auto *wheel = static_cast<QWheelEvent *>(event);
QPoint delta = wheel->pixelDelta();
if (delta.isNull()) {
delta = wheel->angleDelta();
delta /= 2;
}
auto *scrollBar = scrollArea_->horizontalScrollBar();
scrollBar->setValue(scrollBar->value() - (delta.x() ? delta.x() : delta.y()));
wheel->accept();
return true;
}
return QWidget::eventFilter(watched, event);
}
void SessionTabs::updateStripSize() {
const QSize tabSize = bar_->sizeHint();
const int tabWidth = qMax(0, tabSize.width());
const int stripHeight = qMax(kAddWidth, tabSize.height());
const int inlineWidth = tabWidth + (tabWidth ? kAddGap : 0) + kAddWidth;
const int available = tabControls_->width();
const bool docked = inlineWidth > available;
const int contentWidth = docked ? tabWidth : inlineWidth;
const int viewportWidth = qMax(0, available - (docked ? kDockWidth : 0));
inlineAdd_->setVisible(!docked);
dock_->setVisible(docked);
leadingHost_->setFixedHeight(stripHeight);
dock_->setFixedHeight(stripHeight);
bar_->setFixedSize(tabWidth, stripHeight);
content_->setFixedSize(contentWidth, stripHeight);
const int scrollBarHeight =
scrollArea_->horizontalScrollBar()->sizeHint().height();
const bool hasScrollBar = contentWidth > viewportWidth;
// Keep a small white gap between the tab row and the scrollbar.
scrollArea_->setFixedHeight(
stripHeight +
(hasScrollBar ? kScrollGap + scrollBarHeight : 0));
}
void SessionTabs::revealCurrentTab() {
const QRect rect = bar_->tabRect(bar_->currentIndex());
if (!rect.isValid()) {
return;
}
auto *scrollBar = scrollArea_->horizontalScrollBar();
const int width = scrollArea_->viewport()->width();
const int offset = scrollBar->value();
const int start = rect.left();
const int end = rect.right() + 1;
if (start < offset || rect.width() >= width) {
scrollBar->setValue(start);
} else if (end > offset + width) {
scrollBar->setValue(end - width);
}
}