Repository navigation
Expand file tree
/
Copy pathTabContainer.cpp
More file actions
840 lines (687 loc) · 21.8 KB
/
Copy pathTabContainer.cpp
File metadata and controls
840 lines (687 loc) · 21.8 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
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
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
// Copyright (C) Explorer++ Project
// SPDX-License-Identifier: GPL-3.0-only
// See LICENSE in the top level directory
#include "stdafx.h"
#include "TabContainer.h"
#include "AppServices.h"
#include "AsyncIconFetcher.h"
#include "BrowserWindow.h"
#include "Config.h"
#include "MainTabView.h"
#include "PopupMenuRunner.h"
#include "PreservedTab.h"
#include "RuntimeHelper.h"
#include "ShellBrowser/NavigateParams.h"
#include "ShellBrowser/NavigationEvents.h"
#include "ShellBrowser/PreservedHistoryEntry.h"
#include "ShellBrowser/ShellBrowser.h"
#include "ShellBrowser/ShellBrowserEvents.h"
#include "ShellBrowser/ShellBrowserFactory.h"
#include "ShellBrowser/ShellNavigationController.h"
#include "TabContainerBackgroundContextMenu.h"
#include "TabContextMenu.h"
#include "TabEvents.h"
#include "TabStorage.h"
#include "../Helper/DpiCompatibility.h"
#include "../Helper/ScopedStopSource.h"
#include "../Helper/ShellHelper.h"
#include "../Helper/WeakPtrFactory.h"
#include <boost/algorithm/string.hpp>
#include <glog/logging.h>
#include <memory>
#include <ranges>
using namespace std::chrono_literals;
namespace
{
class MainTabViewItem : public TabViewItem
{
public:
MainTabViewItem(Tab *tab, TabEvents *tabEvents, ShellBrowserEvents *shellBrowserEvents,
NavigationEvents *navigationEvents, const Runtime *runtime, AsyncIconFetcher *iconFetcher,
MainTabViewImageListManager *imageListManager) :
m_tab(tab),
m_runtime(runtime),
m_iconFetcher(iconFetcher),
m_imageListManager(imageListManager)
{
m_connections.push_back(
tabEvents->AddUpdatedObserver(std::bind_front(&MainTabViewItem::OnTabUpdated, this),
TabEventScope::ForBrowser(*tab->GetBrowser())));
m_connections.push_back(shellBrowserEvents->AddDirectoryPropertiesChangedObserver(
std::bind(&MainTabViewItem::OnDisplayPropertiesUpdated, this),
ShellBrowserEventScope::ForShellBrowser(*tab->GetShellBrowser())));
m_connections.push_back(navigationEvents->AddCommittedObserver(
std::bind(&MainTabViewItem::OnDisplayPropertiesUpdated, this),
NavigationEventScope::ForShellBrowser(*tab->GetShellBrowser())));
}
std::wstring GetText() const override
{
std::wstring name = m_tab->GetName();
boost::replace_all(name, L"&", L"&&");
return name;
}
std::wstring GetTooltipText() const override
{
const auto &pidlDirectory = m_tab->GetShellBrowser()->GetDirectory();
return GetFolderPathForDisplayWithFallback(pidlDirectory.Raw());
}
std::optional<int> GetIconIndex() const override
{
if (m_tab->IsLocked())
{
return m_imageListManager->GetLockIconIndex();
}
if (!m_iconIndex)
{
const auto &pidl = m_tab->GetShellBrowser()->GetDirectory();
auto cachedSystemIconIndex = m_iconFetcher->MaybeGetCachedIconIndex(pidl.Raw());
int iconIndex;
if (cachedSystemIconIndex)
{
iconIndex = m_imageListManager->AddIconFromSystemImageList(*cachedSystemIconIndex);
}
else
{
iconIndex = m_imageListManager->GetDefaultFolderIconIndex();
}
if (!cachedSystemIconIndex)
{
m_scopedStopSource = std::make_unique<ScopedStopSource>();
RetrieveUpdatedIcon(m_weakPtrFactory.GetMutableWeakPtr(), pidl);
}
m_iconIndex = iconIndex;
}
return *m_iconIndex;
}
Tab *GetTab()
{
return m_tab;
}
private:
void OnTabUpdated(const Tab &tab, Tab::PropertyType propertyType)
{
UNREFERENCED_PARAMETER(propertyType);
if (&tab == m_tab)
{
OnDisplayPropertiesUpdated();
}
}
void OnDisplayPropertiesUpdated()
{
m_scopedStopSource = std::make_unique<ScopedStopSource>();
m_iconIndex.reset();
NotifyParentOfUpdate();
}
static concurrencpp::null_result RetrieveUpdatedIcon(WeakPtr<MainTabViewItem> weakSelf,
PidlAbsolute pidl)
{
auto *runtime = weakSelf->m_runtime;
auto iconFetcher = weakSelf->m_iconFetcher;
auto stopToken = weakSelf->m_scopedStopSource->GetToken();
auto iconInfo = co_await iconFetcher->GetIconIndexAsync(pidl.Raw(), stopToken);
if (!iconInfo)
{
co_return;
}
co_await ResumeOnUiThread(runtime);
if (stopToken.stop_requested() || !weakSelf)
{
co_return;
}
weakSelf->OnIconLoaded(iconInfo->iconIndex);
}
void OnIconLoaded(int iconIndex)
{
m_iconIndex = m_imageListManager->AddIconFromSystemImageList(iconIndex);
NotifyParentOfUpdate();
}
Tab *const m_tab;
const Runtime *const m_runtime;
AsyncIconFetcher *const m_iconFetcher;
MainTabViewImageListManager *const m_imageListManager;
mutable std::optional<int> m_iconIndex;
mutable std::unique_ptr<ScopedStopSource> m_scopedStopSource;
std::vector<boost::signals2::scoped_connection> m_connections;
WeakPtrFactory<MainTabViewItem> m_weakPtrFactory{ this };
};
}
TabContainer *TabContainer::Create(MainTabView *view, BrowserWindow *browser,
ShellBrowserFactory *shellBrowserFactory, AppServices *appServices)
{
return new TabContainer(view, browser, shellBrowserFactory, appServices);
}
TabContainer::TabContainer(MainTabView *view, BrowserWindow *browser,
ShellBrowserFactory *shellBrowserFactory, AppServices *appServices) :
ShellDropTargetWindow(view->GetHWND()),
m_view(view),
m_browser(browser),
m_shellBrowserFactory(shellBrowserFactory),
m_appServices(appServices),
m_tabEvents(appServices->GetTabEvents()),
m_timerManager(m_hwnd),
m_config(appServices->GetConfig()),
m_iPreviousTabSelectionId(-1)
{
Initialize(GetParent(m_view->GetHWND()));
}
void TabContainer::Initialize(HWND parent)
{
m_view->SetDelegate(this);
m_view->windowDestroyedSignal.AddObserver(
std::bind_front(&TabContainer::OnWindowDestroyed, this));
m_windowSubclasses.push_back(std::make_unique<WindowSubclass>(parent,
std::bind_front(&TabContainer::ParentWndProc, this)));
}
void TabContainer::OnTabDoubleClicked(Tab *tab, const MouseEvent &event)
{
UNREFERENCED_PARAMETER(event);
if (m_config->doubleClickTabClose)
{
CloseTab(*tab);
}
}
void TabContainer::OnTabMiddleClicked(Tab *tab, const MouseEvent &event)
{
UNREFERENCED_PARAMETER(event);
CloseTab(*tab);
}
void TabContainer::OnTabRightClicked(Tab *tab, const MouseEvent &event)
{
POINT ptScreen = event.ptClient;
BOOL res = ClientToScreen(m_hwnd, &ptScreen);
CHECK(res);
PopupMenuRunner popupRunner(m_hwnd, m_browser);
TabContextMenu menu(popupRunner.GetView(), m_appServices->GetAcceleratorManager(),
tab->GetWeakPtr(), this, m_tabEvents, m_appServices->GetResourceLoader());
popupRunner.Show(ptScreen);
}
LRESULT TabContainer::ParentWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_LBUTTONDBLCLK:
CreateNewTabInDefaultDirectory({ .selected = true });
break;
case WM_RBUTTONUP:
{
POINT ptClient;
POINTSTOPOINT(ptClient, MAKEPOINTS(lParam));
ShowBackgroundContextMenu(ptClient);
}
break;
}
return DefSubclassProc(hwnd, uMsg, wParam, lParam);
}
void TabContainer::ShowBackgroundContextMenu(const POINT &ptClient)
{
POINT ptScreen = ptClient;
ClientToScreen(m_hwnd, &ptScreen);
PopupMenuRunner popupRunner(m_hwnd, m_browser);
TabContainerBackgroundContextMenu menu(popupRunner.GetView(),
m_appServices->GetAcceleratorManager(), this, m_appServices->GetTabRestorer(),
m_appServices->GetBookmarkTree(), m_browser, m_appServices->GetResourceLoader(),
m_appServices->GetPlatformContext());
popupRunner.Show(ptScreen);
}
void TabContainer::OnTabSelected(const Tab &tab)
{
if (m_iPreviousTabSelectionId != -1)
{
m_tabSelectionHistory.push_back(m_iPreviousTabSelectionId);
}
m_iPreviousTabSelectionId = tab.GetId();
m_tabEvents->NotifySelected(tab);
}
MainTabView *TabContainer::GetView()
{
return m_view;
}
void TabContainer::CreateNewTabInDefaultDirectory(const TabSettings &tabSettings)
{
CreateNewTab(m_config->defaultTabDirectory, tabSettings);
}
// Note that although it's guaranteed that the tab will be created, it's not guaranteed that it will
// be navigated to the directory that's provided. For example, the directory may not exist, or the
// user may not have access to it, or the path may be temporarily unavailable (e.g. because a
// network location has been disconnected). Therefore, it's not safe to make any assumptions about
// the actual directory the tab is in once it's been created.
Tab &TabContainer::CreateNewTab(const std::wstring &directory, const TabSettings &tabSettings,
const FolderSettings *folderSettings, const FolderColumns *initialColumns)
{
unique_pidl_absolute pidl;
HRESULT hr = ParseDisplayNameForNavigation(directory.c_str(), pidl);
if (FAILED(hr))
{
hr = ParseDisplayNameForNavigation(m_config->defaultTabDirectory.c_str(), pidl);
if (FAILED(hr))
{
hr = ParseDisplayNameForNavigation(m_config->defaultTabDirectoryStatic.c_str(), pidl);
}
}
auto navigateParams = NavigateParams::Normal(pidl.get());
return CreateNewTab(navigateParams, tabSettings, folderSettings, initialColumns);
}
Tab &TabContainer::CreateNewTab(const PreservedTab &preservedTab)
{
const auto &preservedShellBrowser = preservedTab.preservedShellBrowser;
auto shellBrowser = m_shellBrowserFactory->CreateFromPreserved(preservedShellBrowser);
Tab::InitialData initialTabData{ .useCustomName = preservedTab.useCustomName,
.customName = preservedTab.customName,
.lockState = preservedTab.lockState };
auto tab = std::make_unique<Tab>(std::move(shellBrowser), m_browser, this, m_tabEvents,
initialTabData);
auto *rawTab = tab.get();
m_tabs.insert({ tab->GetId(), std::move(tab) });
int finalIndex;
if (preservedTab.browserId == m_browser->GetId())
{
finalIndex = preservedTab.index;
}
else
{
// This tab is being restored from a different browser, so its index isn't relevant and it
// should simply be added to the end of the current set of tabs.
finalIndex = static_cast<int>(m_tabs.size()) - 1;
}
TabSettings tabSettings{ .index = finalIndex, .selected = true };
const auto *entry = preservedShellBrowser.history[preservedShellBrowser.currentEntry].get();
auto navigateParams =
NavigateParams::Normal(entry->GetPidl().Raw(), HistoryEntryType::ReplaceCurrentEntry);
return SetUpNewTab(*rawTab, navigateParams, tabSettings);
}
Tab &TabContainer::CreateNewTab(NavigateParams &navigateParams, const TabSettings &tabSettings,
const FolderSettings *folderSettings, const FolderColumns *initialColumns)
{
FolderSettings folderSettingsFinal;
if (folderSettings)
{
folderSettingsFinal = *folderSettings;
}
else
{
folderSettingsFinal = m_config->defaultFolderSettings;
}
auto shellBrowser =
m_shellBrowserFactory->Create(navigateParams.pidl, folderSettingsFinal, initialColumns);
Tab::InitialData initialTabData;
if (tabSettings.lockState)
{
initialTabData.lockState = *tabSettings.lockState;
}
if (tabSettings.name)
{
initialTabData.useCustomName = true;
initialTabData.customName = *tabSettings.name;
}
auto tab = std::make_unique<Tab>(std::move(shellBrowser), m_browser, this, m_tabEvents,
initialTabData);
auto *rawTab = tab.get();
m_tabs.insert({ tab->GetId(), std::move(tab) });
return SetUpNewTab(*rawTab, navigateParams, tabSettings);
}
Tab &TabContainer::SetUpNewTab(Tab &tab, NavigateParams &navigateParams,
const TabSettings &tabSettings)
{
int index;
if (tabSettings.index)
{
index = *tabSettings.index;
}
else
{
// When the application is first started, the number of tabs will be 0 initially, so there
// won't be any selected tab. In that case, the openNewTabNextToCurrent setting should
// effectively be ignored.
if (m_config->openNewTabNextToCurrent && m_view->MaybeGetSelectedIndex())
{
index = GetSelectedTabIndex() + 1;
}
else
{
index = m_view->GetNumTabs();
}
}
auto tabItem =
std::make_unique<MainTabViewItem>(&tab, m_tabEvents, m_appServices->GetShellBrowserEvents(),
m_appServices->GetNavigationEvents(), m_appServices->GetRuntime(),
m_appServices->GetAsyncIconFetcher(), m_view->GetImageListManager());
tabItem->SetDoubleClickedCallback(
std::bind_front(&TabContainer::OnTabDoubleClicked, this, &tab));
tabItem->SetMiddleClickedCallback(
std::bind_front(&TabContainer::OnTabMiddleClicked, this, &tab));
tabItem->SetRightClickedCallback(std::bind_front(&TabContainer::OnTabRightClicked, this, &tab));
/* Browse folder sends a message back to the main window, which
attempts to contact the new tab (needs to be created before browsing
the folder). */
index = m_view->AddTab(std::move(tabItem), index);
m_tabEvents->NotifyCreated(tab);
bool selected = false;
if (tabSettings.selected)
{
selected = *tabSettings.selected;
}
if (selected)
{
SelectTabAtIndex(index);
}
if (m_tabs.size() == 1)
{
// The tab control will implicitly select the first tab that's created, so a selection event
// needs to be synthesized here.
CHECK_EQ(m_view->GetSelectedIndex(), index);
OnTabSelected(tab);
}
tab.GetShellBrowser()->GetNavigationController()->Navigate(navigateParams);
return tab;
}
void TabContainer::CloseAllTabs()
{
int numTabs = GetNumTabs();
for (int i = numTabs - 1; i >= 0; i--)
{
bool closed = CloseTab(GetTabByIndex(i), CloseMode::Force);
DCHECK(closed);
}
}
bool TabContainer::CloseTab(const Tab &tab)
{
return CloseTab(tab, CloseMode::Normal);
}
bool TabContainer::CloseTab(const Tab &tab, CloseMode closeMode)
{
if (tab.IsLocked() && closeMode == CloseMode::Normal)
{
return false;
}
m_tabEvents->NotifyPreRemoval(tab, GetTabIndex(tab));
RemoveTabFromControl(tab);
// Taking ownership of the tab here will ensure it's still live when the observers are notified
// below.
auto itr = m_tabs.find(tab.GetId());
CHECK(itr != m_tabs.end());
auto ownedTab = std::move(*itr);
m_tabs.erase(itr);
m_tabEvents->NotifyRemoved(tab);
return true;
}
void TabContainer::RemoveTabFromControl(const Tab &tab)
{
m_tabSelectionHistory.erase(
std::remove(m_tabSelectionHistory.begin(), m_tabSelectionHistory.end(), tab.GetId()),
m_tabSelectionHistory.end());
const int index = GetTabIndex(tab);
if (IsTabSelected(tab) && m_tabs.size() > 1)
{
int newIndex;
/* If there was a previously active tab, the focus
should be switched back to it. */
if (!m_tabSelectionHistory.empty())
{
const int lastTabId = m_tabSelectionHistory.back();
m_tabSelectionHistory.pop_back();
const Tab &lastTab = GetTab(lastTabId);
newIndex = GetTabIndex(lastTab);
}
else
{
// If the last tab in the control is what's being closed,
// the tab before it will be selected.
if (index == (GetNumTabs() - 1))
{
newIndex = index - 1;
}
else
{
newIndex = index + 1;
}
}
SelectTabAtIndex(newIndex);
// This is somewhat hacky. Switching the tab will cause the
// previously selected tab (i.e. the tab that's about to be
// closed) to be added to the history list. That's not
// desirable, so the last entry will be removed here.
m_tabSelectionHistory.pop_back();
}
m_view->RemoveTab(index);
}
Tab &TabContainer::GetTab(int tabId) const
{
auto *tab = MaybeGetTab(tabId);
CHECK(tab);
return *tab;
}
Tab *TabContainer::MaybeGetTab(int tabId) const
{
auto itr = m_tabs.find(tabId);
if (itr == m_tabs.end())
{
return nullptr;
}
return itr->second.get();
}
void TabContainer::SelectTab(const Tab &tab)
{
int index = GetTabIndex(tab);
SelectTabAtIndex(index);
}
void TabContainer::SelectAdjacentTab(SelectionDirection selectionDirection)
{
int numTabs = GetNumTabs();
CHECK(numTabs > 0);
int selectedIndex = GetSelectedTabIndex();
int step = (selectionDirection == SelectionDirection::Next) ? 1 : -1;
int newIndex = (selectedIndex + step + numTabs) % numTabs;
SelectTabAtIndex(newIndex);
}
void TabContainer::SelectTabAtIndex(int index)
{
m_view->SelectTabAtIndex(index);
}
Tab &TabContainer::GetSelectedTab() const
{
int index = GetSelectedTabIndex();
return GetTabByIndex(index);
}
int TabContainer::GetSelectedTabIndex() const
{
return m_view->GetSelectedIndex();
}
bool TabContainer::IsTabSelected(const Tab &tab) const
{
const Tab &selectedTab = GetSelectedTab();
return tab.GetId() == selectedTab.GetId();
}
Tab &TabContainer::GetTabByIndex(int index) const
{
auto *tabViewItem = static_cast<MainTabViewItem *>(m_view->GetTabAtIndex(index));
return *tabViewItem->GetTab();
}
int TabContainer::GetTabIndex(const Tab &tab) const
{
int numTabs = m_view->GetNumTabs();
for (int i = 0; i < numTabs; i++)
{
const auto ¤tTab = GetTabByIndex(i);
if (currentTab.GetId() == tab.GetId())
{
return i;
}
}
// All internal tab objects should have an index.
LOG(FATAL) << "Couldn't determine index for tab";
}
int TabContainer::GetNumTabs() const
{
return static_cast<int>(m_tabs.size());
}
int TabContainer::MoveTab(const Tab &tab, int newIndex)
{
int index = GetTabIndex(tab);
return m_view->MoveTab(index, newIndex);
}
const std::unordered_map<int, std::unique_ptr<Tab>> &TabContainer::GetAllTabs() const
{
return m_tabs;
}
std::vector<Tab *> TabContainer::GetAllTabsInOrder() const
{
std::vector<Tab *> sortedTabs;
for (const auto &tab : m_tabs | std::views::values)
{
sortedTabs.push_back(tab.get());
}
// The Tab class is non-copyable, so there are essentially two ways of retrieving a sorted list
// of tabs, as far as I can tell:
//
// 1. The first is to maintain a sorted list of tabs while the program is running. I generally
// don't think that's a good idea, since it would be redundant (the tab control already
// stores that information) and it risks possible issues (if the two sets get out of sync).
//
// 2. The second is to sort the tabs when needed. Because they're non-copyable, that can't be
// done directly, so pointers are used instead.
std::sort(sortedTabs.begin(), sortedTabs.end(), [this](const auto *tab1, const auto *tab2)
{ return GetTabIndex(*tab1) < GetTabIndex(*tab2); });
return sortedTabs;
}
Tab &TabContainer::DuplicateTab(const Tab &tab)
{
PreservedTab preservedTab(tab, GetTabIndex(tab) + 1);
return CreateNewTab(preservedTab);
}
void TabContainer::OnTabMoved(int fromIndex, int toIndex)
{
const Tab &tab = GetTabByIndex(toIndex);
m_tabEvents->NotifyMoved(tab, fromIndex, toIndex);
}
bool TabContainer::ShouldRemoveIcon(int iconIndex)
{
return !m_view->GetImageListManager()->IsDefaultIcon(iconIndex);
}
void TabContainer::OnSelectionChanged()
{
OnTabSelected(GetSelectedTab());
}
int TabContainer::GetDropTargetItem(const POINT &pt)
{
POINT ptClient = pt;
BOOL res = ScreenToClient(m_hwnd, &ptClient);
if (!res)
{
return -1;
}
TCHITTESTINFO hitTestInfo;
hitTestInfo.pt = ptClient;
return TabCtrl_HitTest(m_hwnd, &hitTestInfo);
}
unique_pidl_absolute TabContainer::GetPidlForTargetItem(int targetItem)
{
if (targetItem == -1)
{
return nullptr;
}
const auto &tab = GetTabByIndex(targetItem);
return unique_pidl_absolute(ILCloneFull(tab.GetShellBrowser()->GetDirectory().Raw()));
}
IUnknown *TabContainer::GetSiteForTargetItem(PCIDLIST_ABSOLUTE targetItemPidl)
{
UNREFERENCED_PARAMETER(targetItemPidl);
return nullptr;
}
bool TabContainer::IsTargetSourceOfDrop(int targetItem)
{
UNREFERENCED_PARAMETER(targetItem);
return false;
}
void TabContainer::UpdateUiForDrop(int targetItem, const POINT &pt)
{
UpdateUiForTargetItem(targetItem);
ScrollTabControlForDrop(pt);
}
void TabContainer::UpdateUiForTargetItem(int targetItem)
{
if (!m_dropTargetContext)
{
m_dropTargetContext = DropTargetContext(&m_timerManager);
}
if (targetItem == -1)
{
m_dropTargetContext->switchTabTimer.Stop();
}
else if (m_dropTargetContext->targetIndex != targetItem)
{
m_dropTargetContext->switchTabTimer.Start(500ms,
std::bind(&TabContainer::OnDropSwitchTabTimer, this));
}
m_dropTargetContext->targetIndex = targetItem;
}
void TabContainer::ScrollTabControlForDrop(const POINT &pt)
{
POINT ptClient = pt;
BOOL res = ScreenToClient(m_hwnd, &ptClient);
if (!res)
{
return;
}
RECT rc;
res = GetClientRect(m_hwnd, &rc);
if (!res)
{
return;
}
UINT dpi = DpiCompatibility::GetInstance().GetDpiForWindow(m_hwnd);
std::optional<TabView::ScrollDirection> scrollDirection;
if (ptClient.x < MulDiv(DROP_SCROLL_MARGIN_X_96DPI, dpi, USER_DEFAULT_SCREEN_DPI))
{
scrollDirection = TabView::ScrollDirection::Left;
}
else if (ptClient.x
> (rc.right - MulDiv(DROP_SCROLL_MARGIN_X_96DPI, dpi, USER_DEFAULT_SCREEN_DPI)))
{
scrollDirection = TabView::ScrollDirection::Right;
}
if (!scrollDirection)
{
m_dropTargetContext->scrollTimer.Stop();
return;
}
if (!m_dropTargetContext->scrollDirection
|| scrollDirection != *m_dropTargetContext->scrollDirection)
{
m_dropTargetContext->scrollDirection = scrollDirection;
m_dropTargetContext->scrollTimer.Start(1s,
std::bind(&TabContainer::OnDropScrollTimer, this));
}
}
void TabContainer::ResetDropUiState()
{
m_dropTargetContext.reset();
}
void TabContainer::OnDropSwitchTabTimer()
{
CHECK(m_dropTargetContext);
CHECK_NE(m_dropTargetContext->targetIndex, -1);
if (m_dropTargetContext->targetIndex != GetSelectedTabIndex())
{
SelectTabAtIndex(m_dropTargetContext->targetIndex);
}
}
void TabContainer::OnDropScrollTimer()
{
CHECK(m_dropTargetContext);
CHECK(m_dropTargetContext->scrollDirection);
m_view->Scroll(*m_dropTargetContext->scrollDirection);
m_dropTargetContext->scrollDirection.reset();
}
std::vector<TabStorageData> TabContainer::GetStorageData() const
{
std::vector<TabStorageData> tabListStorageData;
for (const auto *tab : GetAllTabsInOrder())
{
tabListStorageData.push_back(tab->GetStorageData());
}
return tabListStorageData;
}
void TabContainer::OnWindowDestroyed()
{
delete this;
}