diff --git a/cockatrice/src/client/tabs/tab_supervisor.cpp b/cockatrice/src/client/tabs/tab_supervisor.cpp index a2711e5e0..0da46029b 100644 --- a/cockatrice/src/client/tabs/tab_supervisor.cpp +++ b/cockatrice/src/client/tabs/tab_supervisor.cpp @@ -281,7 +281,15 @@ void TabSupervisor::initStartupTabs() checkAndTrigger(aTabVisualDeckStorage, SettingsCache::instance().getVisualDeckStorageShowOnLoad()); } -int TabSupervisor::myAddTab(Tab *tab) +/** + * Adds the tab to the TabSupervisor's tab bar. + * + * @param tab The Tab to add + * @param manager The menu action that corresponds to this tab, if this is a single-instance managed tab. Pass in + * nullptr if this is not a managed tab. + * @return The index of the added tab in the tab widget's tab menu + */ +int TabSupervisor::myAddTab(Tab *tab, QAction *manager) { connect(tab, &TabGame::userEvent, this, &TabSupervisor::tabUserEvent); connect(tab, &TabGame::tabTextChanged, this, &TabSupervisor::updateTabText); @@ -290,17 +298,30 @@ int TabSupervisor::myAddTab(Tab *tab) int idx = addTab(tab, sanitizeTabName(tabText)); setTabToolTip(idx, sanitizeHtml(tabText)); - addCloseButtonToTab(tab, idx); + addCloseButtonToTab(tab, idx, manager); return idx; } -void TabSupervisor::addCloseButtonToTab(Tab *tab, int tabIndex) +/** + * Adds a usable close button to the tab. + * + * @param tab The Tab + * @param tabIndex The tab bar index of the tab + * @param manager The menu action that corresponds to this tab, if this is a single-instance managed tab. Pass in + * nullptr if this is not a managed tab. + */ +void TabSupervisor::addCloseButtonToTab(Tab *tab, int tabIndex, QAction *manager) { auto closeSide = static_cast( tabBar()->style()->styleHint(QStyle::SH_TabBar_CloseButtonPosition, nullptr, tabBar())); auto *closeButton = new CloseButton(tab); - connect(closeButton, &CloseButton::clicked, tab, [tab] { tab->closeRequest(); }); + if (manager) { + // If managed, all close requests should go through the menu action + connect(closeButton, &CloseButton::clicked, this, [manager] { checkAndTrigger(manager, false); }); + } else { + connect(closeButton, &CloseButton::clicked, tab, [tab] { tab->closeRequest(); }); + } tabBar()->setTabButton(tabIndex, closeSide, closeButton); } @@ -432,7 +453,7 @@ void TabSupervisor::actTabVisualDeckStorage(bool checked) { if (checked && !tabVisualDeckStorage) { tabVisualDeckStorage = new TabDeckStorageVisual(this, client); - myAddTab(tabVisualDeckStorage); + myAddTab(tabVisualDeckStorage, aTabVisualDeckStorage); setCurrentWidget(tabVisualDeckStorage); connect(tabVisualDeckStorage, &Tab::closed, this, [this] { tabVisualDeckStorage = nullptr; @@ -448,7 +469,7 @@ void TabSupervisor::actTabServer(bool checked) if (checked && !tabServer) { tabServer = new TabServer(this, client); connect(tabServer, &TabServer::roomJoined, this, &TabSupervisor::addRoomTab); - myAddTab(tabServer); + myAddTab(tabServer, aTabServer); connect(tabServer, &Tab::closed, this, [this] { tabServer = nullptr; aTabServer->setChecked(false); @@ -465,7 +486,7 @@ void TabSupervisor::actTabAccount(bool checked) connect(tabAccount, &TabAccount::openMessageDialog, this, &TabSupervisor::addMessageTab); connect(tabAccount, &TabAccount::userJoined, this, &TabSupervisor::processUserJoined); connect(tabAccount, &TabAccount::userLeft, this, &TabSupervisor::processUserLeft); - myAddTab(tabAccount); + myAddTab(tabAccount, aTabAccount); connect(tabAccount, &Tab::closed, this, [this] { tabAccount = nullptr; aTabAccount->setChecked(false); @@ -480,7 +501,7 @@ void TabSupervisor::actTabDeckStorage(bool checked) if (checked && !tabDeckStorage) { tabDeckStorage = new TabDeckStorage(this, client); connect(tabDeckStorage, &TabDeckStorage::openDeckEditor, this, &TabSupervisor::addDeckEditorTab); - myAddTab(tabDeckStorage); + myAddTab(tabDeckStorage, aTabDeckStorage); connect(tabDeckStorage, &Tab::closed, this, [this] { tabDeckStorage = nullptr; aTabDeckStorage->setChecked(false); @@ -495,7 +516,7 @@ void TabSupervisor::actTabReplays(bool checked) if (checked && !tabReplays) { tabReplays = new TabReplays(this, client); connect(tabReplays, &TabReplays::openReplay, this, &TabSupervisor::openReplay); - myAddTab(tabReplays); + myAddTab(tabReplays, aTabReplays); connect(tabReplays, &Tab::closed, this, [this] { tabReplays = nullptr; aTabReplays->setChecked(false); @@ -510,7 +531,7 @@ void TabSupervisor::actTabAdmin(bool checked) if (checked && !tabAdmin) { tabAdmin = new TabAdmin(this, client, (userInfo->user_level() & ServerInfo_User::IsAdmin)); connect(tabAdmin, &TabAdmin::adminLockChanged, this, &TabSupervisor::adminLockChanged); - myAddTab(tabAdmin); + myAddTab(tabAdmin, aTabAdmin); connect(tabAdmin, &Tab::closed, this, [this] { tabAdmin = nullptr; aTabAdmin->setChecked(false); @@ -524,7 +545,7 @@ void TabSupervisor::actTabLog(bool checked) { if (checked && !tabLog) { tabLog = new TabLog(this, client); - myAddTab(tabLog); + myAddTab(tabLog, aTabLog); connect(tabLog, &Tab::closed, this, [this] { tabLog = nullptr; aTabAdmin->setChecked(false); diff --git a/cockatrice/src/client/tabs/tab_supervisor.h b/cockatrice/src/client/tabs/tab_supervisor.h index 40d030845..548dc7479 100644 --- a/cockatrice/src/client/tabs/tab_supervisor.h +++ b/cockatrice/src/client/tabs/tab_supervisor.h @@ -89,8 +89,8 @@ private: void initStartupTabs(); - int myAddTab(Tab *tab); - void addCloseButtonToTab(Tab *tab, int tabIndex); + int myAddTab(Tab *tab, QAction *manager = nullptr); + void addCloseButtonToTab(Tab *tab, int tabIndex, QAction *manager); QString sanitizeTabName(QString dirty) const; QString sanitizeHtml(QString dirty) const; void resetTabsMenu();