/
main_window.cpp
586 lines (489 loc) · 15.2 KB
/
main_window.cpp
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
// Aseprite
// Copyright (C) 2001-2017 David Capello
//
// This program is distributed under the terms of
// the End-User License Agreement for Aseprite.
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "app/ui/main_window.h"
#include "app/app.h"
#include "app/app_menus.h"
#include "app/commands/commands.h"
#include "app/ini_file.h"
#include "app/modules/editors.h"
#include "app/notification_delegate.h"
#include "app/pref/preferences.h"
#include "app/ui/browser_view.h"
#include "app/ui/color_bar.h"
#include "app/ui/context_bar.h"
#include "app/ui/document_view.h"
#include "app/ui/editor/editor.h"
#include "app/ui/editor/editor_view.h"
#include "app/ui/home_view.h"
#include "app/ui/main_menu_bar.h"
#include "app/ui/notifications.h"
#include "app/ui/preview_editor.h"
#include "app/ui/skin/skin_property.h"
#include "app/ui/skin/skin_theme.h"
#include "app/ui/status_bar.h"
#include "app/ui/timeline/timeline.h"
#include "app/ui/toolbar.h"
#include "app/ui/workspace.h"
#include "app/ui/workspace_tabs.h"
#include "app/ui_context.h"
#include "base/bind.h"
#include "base/fs.h"
#include "she/display.h"
#include "she/system.h"
#include "ui/message.h"
#include "ui/splitter.h"
#include "ui/system.h"
#include "ui/view.h"
#ifdef ENABLE_SCRIPTING
#include "app/ui/devconsole_view.h"
#endif
namespace app {
using namespace ui;
class ScreenScalePanic : public INotificationDelegate {
public:
std::string notificationText() override {
return "Reset Scale!";
}
void notificationClick() override {
auto& pref = Preferences::instance();
const int newScreenScale = 2;
const int newUIScale = 1;
if (pref.general.screenScale() != newScreenScale)
pref.general.screenScale(newScreenScale);
if (pref.general.uiScale() != newUIScale)
pref.general.uiScale(newUIScale);
pref.save();
ui::set_theme(ui::get_theme(), newUIScale);
Manager* manager = Manager::getDefault();
she::Display* display = manager->getDisplay();
display->setScale(newScreenScale);
manager->setDisplay(display);
}
};
MainWindow::MainWindow()
: m_mode(NormalMode)
, m_homeView(nullptr)
, m_scalePanic(nullptr)
, m_browserView(nullptr)
#ifdef ENABLE_SCRIPTING
, m_devConsoleView(nullptr)
#endif
{
// Load all menus by first time.
AppMenus::instance()->reload();
m_menuBar = new MainMenuBar();
m_notifications = new Notifications();
m_contextBar = new ContextBar();
m_statusBar = new StatusBar();
m_colorBar = new ColorBar(colorBarPlaceholder()->align());
m_toolBar = new ToolBar();
m_tabsBar = new WorkspaceTabs(this);
m_workspace = new Workspace();
m_previewEditor = new PreviewEditorWindow();
m_timeline = new Timeline();
m_workspace->setTabsBar(m_tabsBar);
m_workspace->ActiveViewChanged.connect(&MainWindow::onActiveViewChange, this);
// configure all widgets to expansives
m_menuBar->setExpansive(true);
m_contextBar->setExpansive(true);
m_contextBar->setVisible(false);
m_statusBar->setExpansive(true);
m_colorBar->setExpansive(true);
m_toolBar->setExpansive(true);
m_tabsBar->setExpansive(true);
m_timeline->setExpansive(true);
m_workspace->setExpansive(true);
m_notifications->setVisible(false);
// Setup the menus
m_menuBar->setMenu(AppMenus::instance()->getRootMenu());
// Add the widgets in the boxes
menuBarPlaceholder()->addChild(m_menuBar);
menuBarPlaceholder()->addChild(m_notifications);
contextBarPlaceholder()->addChild(m_contextBar);
colorBarPlaceholder()->addChild(m_colorBar);
toolBarPlaceholder()->addChild(m_toolBar);
statusBarPlaceholder()->addChild(m_statusBar);
tabsPlaceholder()->addChild(m_tabsBar);
workspacePlaceholder()->addChild(m_workspace);
timelinePlaceholder()->addChild(m_timeline);
// Default splitter positions
colorBarSplitter()->setPosition(m_colorBar->sizeHint().w);
timelineSplitter()->setPosition(75);
// Reconfigure workspace when the timeline position is changed.
auto& pref = Preferences::instance();
pref.general.timelinePosition
.AfterChange.connect(base::Bind<void>(&MainWindow::configureWorkspaceLayout, this));
pref.general.showMenuBar
.AfterChange.connect(base::Bind<void>(&MainWindow::configureWorkspaceLayout, this));
// Prepare the window
remapWindow();
AppMenus::instance()->rebuildRecentList();
}
MainWindow::~MainWindow()
{
delete m_scalePanic;
#ifdef ENABLE_SCRIPTING
if (m_devConsoleView) {
if (m_devConsoleView->parent())
m_workspace->removeView(m_devConsoleView);
delete m_devConsoleView;
}
#endif
if (m_browserView) {
if (m_browserView->parent())
m_workspace->removeView(m_browserView);
delete m_browserView;
}
if (m_homeView) {
if (m_homeView->parent())
m_workspace->removeView(m_homeView);
delete m_homeView;
}
delete m_contextBar;
delete m_previewEditor;
// Destroy the workspace first so ~Editor can dettach slots from
// ColorBar. TODO this is a terrible hack for slot/signal stuff,
// connections should be handle in a better/safer way.
delete m_workspace;
// Remove the root-menu from the menu-bar (because the rootmenu
// module should destroy it).
m_menuBar->setMenu(NULL);
}
DocumentView* MainWindow::getDocView()
{
return dynamic_cast<DocumentView*>(m_workspace->activeView());
}
HomeView* MainWindow::getHomeView()
{
if (!m_homeView)
m_homeView = new HomeView;
return m_homeView;
}
#ifdef ENABLE_UPDATER
CheckUpdateDelegate* MainWindow::getCheckUpdateDelegate()
{
return getHomeView();
}
#endif
void MainWindow::reloadMenus()
{
m_menuBar->reload();
layout();
invalidate();
}
void MainWindow::showNotification(INotificationDelegate* del)
{
m_notifications->addLink(del);
m_notifications->setVisible(true);
m_notifications->parent()->layout();
}
void MainWindow::showHomeOnOpen()
{
// Don't open Home tab
if (!Preferences::instance().general.showHome()) {
configureWorkspaceLayout();
return;
}
if (!getHomeView()->parent()) {
TabView* selectedTab = m_tabsBar->getSelectedTab();
// Show "Home" tab in the first position, and select it only if
// there is no other view selected.
m_workspace->addView(m_homeView, 0);
if (selectedTab)
m_tabsBar->selectTab(selectedTab);
else
m_tabsBar->selectTab(m_homeView);
}
}
void MainWindow::showHome()
{
if (!getHomeView()->parent()) {
m_workspace->addView(m_homeView, 0);
}
m_tabsBar->selectTab(m_homeView);
}
bool MainWindow::isHomeSelected()
{
return (m_tabsBar->getSelectedTab() == m_homeView && m_homeView);
}
void MainWindow::showBrowser(const std::string& filename)
{
if (!m_browserView)
m_browserView = new BrowserView;
m_browserView->loadFile(filename);
if (!m_browserView->parent()) {
m_workspace->addView(m_browserView);
m_tabsBar->selectTab(m_browserView);
}
}
void MainWindow::showDevConsole()
{
#ifdef ENABLE_SCRIPTING
if (!m_devConsoleView)
m_devConsoleView = new DevConsoleView;
if (!m_devConsoleView->parent()) {
m_workspace->addView(m_devConsoleView);
m_tabsBar->selectTab(m_devConsoleView);
}
#endif
}
void MainWindow::setMode(Mode mode)
{
// Check if we already are in the given mode.
if (m_mode == mode)
return;
m_mode = mode;
configureWorkspaceLayout();
}
bool MainWindow::getTimelineVisibility() const
{
return Preferences::instance().general.visibleTimeline();
}
void MainWindow::setTimelineVisibility(bool visible)
{
Preferences::instance().general.visibleTimeline(visible);
configureWorkspaceLayout();
}
void MainWindow::popTimeline()
{
Preferences& preferences = Preferences::instance();
if (!preferences.general.autoshowTimeline())
return;
if (!getTimelineVisibility())
setTimelineVisibility(true);
}
void MainWindow::showDataRecovery(crash::DataRecovery* dataRecovery)
{
getHomeView()->showDataRecovery(dataRecovery);
}
bool MainWindow::onProcessMessage(ui::Message* msg)
{
if (msg->type() == kOpenMessage)
showHomeOnOpen();
return Window::onProcessMessage(msg);
}
void MainWindow::onInitTheme(ui::InitThemeEvent& ev)
{
app::gen::MainWindow::onInitTheme(ev);
if (m_previewEditor)
m_previewEditor->initTheme();
}
void MainWindow::onSaveLayout(SaveLayoutEvent& ev)
{
// Invert the timeline splitter position before we save the setting.
if (Preferences::instance().general.timelinePosition() ==
gen::TimelinePosition::LEFT) {
timelineSplitter()->setPosition(100 - timelineSplitter()->getPosition());
}
Window::onSaveLayout(ev);
}
void MainWindow::onResize(ui::ResizeEvent& ev)
{
app::gen::MainWindow::onResize(ev);
she::Display* display = manager()->getDisplay();
if ((display) &&
(display->scale()*ui::guiscale() > 2) &&
(!m_scalePanic) &&
(ui::display_w()/ui::guiscale() < 320 ||
ui::display_h()/ui::guiscale() < 260)) {
showNotification(m_scalePanic = new ScreenScalePanic);
}
}
// When the active view is changed from methods like
// Workspace::splitView(), this function is called, and we have to
// inform to the UIContext that the current view has changed.
void MainWindow::onActiveViewChange()
{
if (DocumentView* docView = getDocView())
UIContext::instance()->setActiveView(docView);
else
UIContext::instance()->setActiveView(nullptr);
configureWorkspaceLayout();
}
bool MainWindow::isTabModified(Tabs* tabs, TabView* tabView)
{
if (DocumentView* docView = dynamic_cast<DocumentView*>(tabView)) {
Document* document = docView->document();
return document->isModified();
}
else {
return false;
}
}
bool MainWindow::canCloneTab(Tabs* tabs, TabView* tabView)
{
ASSERT(tabView)
WorkspaceView* view = dynamic_cast<WorkspaceView*>(tabView);
return view->canCloneWorkspaceView();
}
void MainWindow::onSelectTab(Tabs* tabs, TabView* tabView)
{
if (!tabView)
return;
WorkspaceView* view = dynamic_cast<WorkspaceView*>(tabView);
if (m_workspace->activeView() != view)
m_workspace->setActiveView(view);
}
void MainWindow::onCloseTab(Tabs* tabs, TabView* tabView)
{
WorkspaceView* view = dynamic_cast<WorkspaceView*>(tabView);
ASSERT(view);
if (view)
m_workspace->closeView(view, false);
}
void MainWindow::onCloneTab(Tabs* tabs, TabView* tabView, int pos)
{
EditorView::SetScrollUpdateMethod(EditorView::KeepOrigin);
WorkspaceView* view = dynamic_cast<WorkspaceView*>(tabView);
WorkspaceView* clone = view->cloneWorkspaceView();
ASSERT(clone);
m_workspace->addViewToPanel(
static_cast<WorkspaceTabs*>(tabs)->panel(), clone, true, pos);
clone->onClonedFrom(view);
}
void MainWindow::onContextMenuTab(Tabs* tabs, TabView* tabView)
{
WorkspaceView* view = dynamic_cast<WorkspaceView*>(tabView);
ASSERT(view);
if (view)
view->onTabPopup(m_workspace);
}
void MainWindow::onTabsContainerDoubleClicked(Tabs* tabs)
{
WorkspacePanel* mainPanel = m_workspace->mainPanel();
WorkspaceView* oldActiveView = mainPanel->activeView();
app::Document* oldDoc = static_cast<app::Document*>(UIContext::instance()->activeDocument());
Command* command = CommandsModule::instance()->getCommandByName(CommandId::NewFile);
UIContext::instance()->executeCommand(command);
app::Document* newDoc = static_cast<app::Document*>(UIContext::instance()->activeDocument());
if (newDoc != oldDoc) {
WorkspacePanel* doubleClickedPanel =
static_cast<WorkspaceTabs*>(tabs)->panel();
// TODO move this code to workspace?
// Put the new sprite in the double-clicked tabs control
if (doubleClickedPanel != mainPanel) {
WorkspaceView* newView = m_workspace->activeView();
m_workspace->removeView(newView);
m_workspace->addViewToPanel(doubleClickedPanel, newView, false, -1);
// Re-activate the old view in the main panel
mainPanel->setActiveView(oldActiveView);
doubleClickedPanel->setActiveView(newView);
}
}
}
void MainWindow::onMouseOverTab(Tabs* tabs, TabView* tabView)
{
// Note: tabView can be NULL
if (DocumentView* docView = dynamic_cast<DocumentView*>(tabView)) {
Document* document = docView->document();
std::string name;
if (Preferences::instance().general.showFullPath())
name = document->filename();
else
name = base::get_file_name(document->filename());
m_statusBar->setStatusText(250, "%s", name.c_str());
}
else {
m_statusBar->clearText();
}
}
DropViewPreviewResult MainWindow::onFloatingTab(Tabs* tabs, TabView* tabView, const gfx::Point& pos)
{
return m_workspace->setDropViewPreview(pos,
dynamic_cast<WorkspaceView*>(tabView),
static_cast<WorkspaceTabs*>(tabs));
}
void MainWindow::onDockingTab(Tabs* tabs, TabView* tabView)
{
m_workspace->removeDropViewPreview();
}
DropTabResult MainWindow::onDropTab(Tabs* tabs, TabView* tabView, const gfx::Point& pos, bool clone)
{
m_workspace->removeDropViewPreview();
DropViewAtResult result =
m_workspace->dropViewAt(pos, dynamic_cast<WorkspaceView*>(tabView), clone);
if (result == DropViewAtResult::MOVED_TO_OTHER_PANEL)
return DropTabResult::REMOVE;
else if (result == DropViewAtResult::CLONED_VIEW)
return DropTabResult::DONT_REMOVE;
else
return DropTabResult::NOT_HANDLED;
}
void MainWindow::configureWorkspaceLayout()
{
const auto& pref = Preferences::instance();
bool normal = (m_mode == NormalMode);
bool isDoc = (getDocView() != nullptr);
if (she::instance()->menus() == nullptr ||
pref.general.showMenuBar()) {
if (!m_menuBar->parent())
menuBarPlaceholder()->insertChild(0, m_menuBar);
}
else {
if (m_menuBar->parent())
menuBarPlaceholder()->removeChild(m_menuBar);
}
m_menuBar->setVisible(normal);
m_tabsBar->setVisible(normal);
colorBarPlaceholder()->setVisible(normal && isDoc);
m_toolBar->setVisible(normal && isDoc);
m_statusBar->setVisible(normal);
m_contextBar->setVisible(
isDoc &&
(m_mode == NormalMode ||
m_mode == ContextBarAndTimelineMode));
// Configure timeline
{
auto timelinePosition = pref.general.timelinePosition();
bool invertWidgets = false;
int align = VERTICAL;
switch (timelinePosition) {
case gen::TimelinePosition::LEFT:
align = HORIZONTAL;
invertWidgets = true;
break;
case gen::TimelinePosition::RIGHT:
align = HORIZONTAL;
break;
case gen::TimelinePosition::BOTTOM:
break;
}
timelineSplitter()->setAlign(align);
timelinePlaceholder()->setVisible(
isDoc &&
(m_mode == NormalMode ||
m_mode == ContextBarAndTimelineMode) &&
pref.general.visibleTimeline());
bool invertSplitterPos = false;
if (invertWidgets) {
if (timelineSplitter()->firstChild() == workspacePlaceholder() &&
timelineSplitter()->lastChild() == timelinePlaceholder()) {
timelineSplitter()->removeChild(workspacePlaceholder());
timelineSplitter()->addChild(workspacePlaceholder());
invertSplitterPos = true;
}
}
else {
if (timelineSplitter()->firstChild() == timelinePlaceholder() &&
timelineSplitter()->lastChild() == workspacePlaceholder()) {
timelineSplitter()->removeChild(timelinePlaceholder());
timelineSplitter()->addChild(timelinePlaceholder());
invertSplitterPos = true;
}
}
if (invertSplitterPos)
timelineSplitter()->setPosition(100 - timelineSplitter()->getPosition());
}
if (m_contextBar->isVisible()) {
m_contextBar->updateForActiveTool();
}
layout();
invalidate();
}
} // namespace app