/
tabs.h
306 lines (239 loc) · 9.41 KB
/
tabs.h
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
// Aseprite
// Copyright (C) 2001-2017 David Capello
//
// This program is distributed under the terms of
// the End-User License Agreement for Aseprite.
#ifndef APP_UI_TABS_H_INCLUDED
#define APP_UI_TABS_H_INCLUDED
#pragma once
#include "app/ui/animated_widget.h"
#include "base/shared_ptr.h"
#include "base/unique_ptr.h"
#include "ui/mouse_buttons.h"
#include "ui/timer.h"
#include "ui/widget.h"
#include <vector>
namespace ui {
class Graphics;
class Overlay;
}
namespace app {
class Tabs;
enum class TabIcon {
NONE,
HOME,
};
// Required interface to be implemented by each new tab that is added
// in the Tabs widget.
class TabView {
public:
virtual ~TabView() { }
// Returns the text to be shown in the tab.
virtual std::string getTabText() = 0;
// Returns the icon to be shown in the tab
virtual TabIcon getTabIcon() = 0;
};
enum class DropTabResult {
// The operation should be handled inside the Tabs widget (if the
// tab was dropped in the same Tabs, it will be moved or copied
// depending on what the user wants).
NOT_HANDLED,
// The tab was docked in other place, so it must be removed from
// the specific Tabs widget.
REMOVE,
// The operation was already handled, but the tab must not be
// removed from the Tabs (e.g. because it was cloned).
DONT_REMOVE,
};
enum class DropViewPreviewResult {
DROP_IN_PANEL,
DROP_IN_TABS,
FLOATING,
};
// Interface used to control notifications from the Tabs widget.
class TabsDelegate {
public:
virtual ~TabsDelegate() { }
// Returns true if the tab represent a modified document.
virtual bool isTabModified(Tabs* tabs, TabView* tabView) = 0;
// Returns true if the tab can be cloned.
virtual bool canCloneTab(Tabs* tabs, TabView* tabView) = 0;
// Called when the user selected the tab with the left mouse button.
virtual void onSelectTab(Tabs* tabs, TabView* tabView) = 0;
// When the tab close button is pressed (or middle mouse button is used to close it).
virtual void onCloseTab(Tabs* tabs, TabView* tabView) = 0;
// When the tab is cloned (this only happens when the user
// drag-and-drop the tab into the same Tabs with the Ctrl key).
virtual void onCloneTab(Tabs* tabs, TabView* tabView, int pos) = 0;
// When the right-click is pressed in the tab.
virtual void onContextMenuTab(Tabs* tabs, TabView* tabView) = 0;
// When the tab bar background is double-clicked.
virtual void onTabsContainerDoubleClicked(Tabs* tabs) = 0;
// Called when the mouse is over a tab (the data can be null if the
// mouse just leave all tabs)
virtual void onMouseOverTab(Tabs* tabs, TabView* tabView) = 0;
// Called when the user is dragging a tab outside the Tabs
// bar.
virtual DropViewPreviewResult onFloatingTab(Tabs* tabs, TabView* tabView, const gfx::Point& pos) = 0;
// Called when the user is dragging a tab inside the Tabs bar.
virtual void onDockingTab(Tabs* tabs, TabView* tabView) = 0;
virtual DropTabResult onDropTab(Tabs* tabs, TabView* tabView, const gfx::Point& pos, bool clone) = 0;
};
// Tabs control. Used to show opened documents.
class Tabs : public ui::Widget
, public AnimatedWidget {
struct Tab {
TabView* view;
std::string text;
TabIcon icon;
int x, width;
int oldX, oldWidth;
bool modified;
Tab(TabView* view) : view(view) {
ASSERT(view);
text = view->getTabText();
icon = view->getTabIcon();
x = width = oldX = oldWidth =
#if _DEBUG
0xfefefefe;
#else
0;
#endif
modified = false;
}
};
typedef base::SharedPtr<Tab> TabPtr;
typedef std::vector<TabPtr> TabsList;
typedef TabsList::iterator TabsListIterator;
enum Ani : int {
ANI_NONE,
ANI_ADDING_TAB,
ANI_REMOVING_TAB,
ANI_REORDER_TABS
};
public:
static ui::WidgetType Type();
Tabs(TabsDelegate* delegate);
~Tabs();
TabsDelegate* getDelegate() { return m_delegate; }
void addTab(TabView* tabView, bool from_drop, int pos = -1);
void removeTab(TabView* tabView, bool with_animation);
void updateTabs();
// Returns true if the user can select other tab.
bool canSelectOtherTab() const;
void selectTab(TabView* tabView);
void selectNextTab();
void selectPreviousTab();
TabView* getSelectedTab();
void setDockedStyle();
// Drop TabViews into this Tabs widget
void setDropViewPreview(const gfx::Point& pos, TabView* view);
void removeDropViewPreview();
int getDropTabIndex() const { return m_dropNewIndex; }
protected:
bool onProcessMessage(ui::Message* msg) override;
void onInitTheme(ui::InitThemeEvent& ev) override;
void onPaint(ui::PaintEvent& ev) override;
void onResize(ui::ResizeEvent& ev) override;
void onSizeHint(ui::SizeHintEvent& ev) override;
void onAnimationFrame() override;
void onAnimationStop(int animation) override;
private:
void resetOldPositions();
void resetOldPositions(double t);
void selectTabInternal(TabPtr& tab);
void drawTab(ui::Graphics* g, const gfx::Rect& box, Tab* tab, int dy, bool hover, bool selected);
void drawFiller(ui::Graphics* g, const gfx::Rect& box);
TabsListIterator getTabIteratorByView(TabView* tabView);
TabPtr getTabByView(TabView* tabView);
int getMaxScrollX();
void makeTabVisible(Tab* tab);
void calculateHot();
gfx::Rect getTabCloseButtonBounds(Tab* tab, const gfx::Rect& box);
void startDrag();
void stopDrag(DropTabResult result);
gfx::Rect getTabBounds(Tab* tab);
void startReorderTabsAnimation();
void startRemoveDragTabAnimation();
void createFloatingOverlay(Tab* tab);
void destroyFloatingTab();
void destroyFloatingOverlay();
void updateMouseCursor();
void updateDragTabIndexes(int mouseX, bool force_animation);
void updateDragCopyCursor(ui::Message* msg);
// Specific variables about the style
int m_border; // Pixels used from the left side to draw the first tab
bool m_docked; // True if tabs are inside the workspace (not the main tabs panel)
int m_tabsHeight; // Number of pixels in Y-axis for each Tab
int m_tabsBottomHeight; // Number of pixels in the bottom part of Tabs widget
// List of tabs (pointers to Tab instances).
TabsList m_list;
// Which tab has the mouse over.
TabPtr m_hot;
// True if the mouse is above the close button of m_hot tab.
bool m_hotCloseButton;
// True if the user clicked over the close button of m_hot.
bool m_clickedCloseButton;
// Current active tab. When this tab changes, the
// TabsDelegate::onSelectTab() is called.
TabPtr m_selected;
// Delegate of notifications
TabsDelegate* m_delegate;
// Variables for animation purposes
TabPtr m_removedTab;
////////////////////////////////////////
// Drag-and-drop
// True when the user is dragging a tab (the mouse must be
// captured when this flag is true). The dragging process doesn't
// start immediately, when the user clicks a tab the m_selected
// tab is changed, and then there is a threshold after we start
// dragging the tab.
bool m_isDragging;
// True when the user is dragging the tab as a copy inside this
// same Tabs (e.g. using Ctrl key) This can be true only if
// TabsDelegate::canCloneTab() returns true for the tab being
// dragged.
bool m_dragCopy;
// A copy of m_selected used only in the dragging process. This
// tab is not pointing to the same tab as m_selected. It's a copy
// because if m_dragCopy is true, we've to paint m_selected and
// m_dragTab separately (as two different tabs).
TabPtr m_dragTab;
// Initial X position where m_dragTab/m_selected was when we
// started (mouse down) the drag process.
int m_dragTabX;
// Initial mouse position when we start the dragging process.
gfx::Point m_dragMousePos;
// New position where m_selected is being dragged. It's used to
// change the position of m_selected inside the m_list vector in
// real-time while the mouse is moved.
int m_dragTabIndex;
// New position where a copyt of m_dragTab will be dropped. It
// only makes sense when m_dragCopy is true (the user is pressing
// Ctrl key and want to create a copy of the tab).
int m_dragCopyIndex;
// Null if we are dragging the m_dragTab inside this Tabs widget,
// or non-null (equal to m_selected indeed), if we're moving the
// tab outside the Tabs widget (e.g. to dock the tabs in other
// location).
TabPtr m_floatingTab;
// Overlay used to show the floating tab outside the Tabs widget
// (this overlay floats next to the mouse cursor). It's destroyed
// and recreated every time the tab is put inside or outside the
// Tabs widget.
base::UniquePtr<ui::Overlay> m_floatingOverlay;
// Relative mouse position inside the m_dragTab (used to adjust
// the m_floatingOverlay precisely).
gfx::Point m_floatingOffset;
////////////////////////////////////////
// Drop new tabs
// Non-null when a foreign tab (a "TabView" from other "Tabs"
// widget), will be dropped in this specific Tabs widget. It's
// used only for feedback/UI purposes when the
// setDropViewPreview() is called.
TabView* m_dropNewTab;
int m_dropNewIndex;
int m_dropNewPosX;
};
} // namespace app
#endif