Allow opening new account in a separate window.

This commit is contained in:
John Preston 2023-01-27 19:56:43 +04:00
parent 42a2f53a11
commit e10964a0bc
6 changed files with 51 additions and 24 deletions

View File

@ -305,14 +305,24 @@ not_null<Main::Account*> Domain::add(MTP::Environment environment) {
return account;
}
void Domain::addActivated(MTP::Environment environment) {
void Domain::addActivated(MTP::Environment environment, bool newWindow) {
const auto added = [&](not_null<Main::Account*> account) {
if (newWindow) {
Core::App().ensureSeparateWindowForAccount(account);
} else if (const auto window = Core::App().separateWindowForAccount(
account)) {
window->activate();
} else {
activate(account);
}
};
if (accounts().size() < maxAccounts()) {
activate(add(environment));
added(add(environment));
} else {
for (auto &[index, account] : accounts()) {
if (!account->sessionExists()
&& account->mtp().environment() == environment) {
activate(account.get());
added(account.get());
break;
}
}

View File

@ -70,7 +70,7 @@ public:
[[nodiscard]] not_null<Main::Account*> add(MTP::Environment environment);
void maybeActivate(not_null<Main::Account*> account);
void activate(not_null<Main::Account*> account);
void addActivated(MTP::Environment environment);
void addActivated(MTP::Environment environment, bool newWindow = false);
// Interface for Storage::Domain.
void accountAddedInStorage(AccountWithIndex accountWithIndex);

View File

@ -1294,7 +1294,7 @@ bool MainWidget::showHistoryInDifferentWindow(
return true;
}
void MainWidget::showPeerHistory(
void MainWidget::showHistory(
PeerId peerId,
const SectionShow &params,
MsgId showAtMsgId) {
@ -1311,6 +1311,7 @@ void MainWidget::showPeerHistory(
Assert(isPrimary());
if (params.activation != anim::activation::background) {
_controller->show(Ui::MakeInformBox(unavailable));
_controller->window().activate();
}
return;
}
@ -1324,9 +1325,13 @@ void MainWidget::showPeerHistory(
return;
}
if (peerId && params.activation != anim::activation::background) {
_controller->window().activate();
}
if (!(_history->peer() && _history->peer()->id == peerId)
&& preventsCloseSection(
[=] { showPeerHistory(peerId, params, showAtMsgId); },
[=] { showHistory(peerId, params, showAtMsgId); },
params)) {
return;
}
@ -1490,7 +1495,7 @@ void MainWidget::showMessage(
return;
}
} else if (_history->peer() == item->history()->peer) {
showPeerHistory(peerId, params, itemId);
showHistory(peerId, params, itemId);
return;
}
}

View File

@ -220,7 +220,7 @@ public:
void toggleChooseChatTheme(not_null<PeerData*> peer);
void showPeerHistory(
void showHistory(
PeerId peer,
const SectionShow &params,
MsgId msgId);

View File

@ -778,23 +778,38 @@ not_null<Ui::SlideWrap<Ui::SettingsButton>*> AccountsList::setupAdd() {
})))->setDuration(0);
const auto button = result->entity();
const auto add = [=](MTP::Environment environment) {
Core::App().preventOrInvoke([=] {
auto &domain = _controller->session().domain();
if (domain.accounts().size() >= domain.maxAccounts()) {
_controller->show(
Box(AccountsLimitBox, &_controller->session()));
} else {
domain.addActivated(environment);
using Environment = MTP::Environment;
const auto add = [=](Environment environment, bool newWindow = false) {
auto &domain = _controller->session().domain();
auto found = false;
for (const auto &[index, account] : domain.accounts()) {
const auto raw = account.get();
if (!raw->sessionExists()
&& raw->mtp().environment() == environment) {
found = true;
}
});
}
if (!found && domain.accounts().size() >= domain.maxAccounts()) {
_controller->show(
Box(AccountsLimitBox, &_controller->session()));
} else if (newWindow) {
domain.addActivated(environment, true);
} else {
_controller->window().preventOrInvoke([=] {
_controller->session().domain().addActivated(environment);
});
}
};
button->setAcceptBoth(true);
button->clicks(
) | rpl::start_with_next([=](Qt::MouseButton which) {
if (which == Qt::LeftButton) {
add(MTP::Environment::Production);
const auto modifiers = button->clickModifiers();
const auto newWindow = (modifiers & Qt::ControlModifier)
&& base::options::lookup<bool>(
Dialogs::kOptionCtrlClickChatNewWindow).value();
add(Environment::Production, newWindow);
return;
} else if (which != Qt::RightButton
|| !IsAltShift(button->clickModifiers())) {
@ -802,10 +817,10 @@ not_null<Ui::SlideWrap<Ui::SettingsButton>*> AccountsList::setupAdd() {
}
_contextMenu = base::make_unique_q<Ui::PopupMenu>(_outer);
_contextMenu->addAction("Production Server", [=] {
add(MTP::Environment::Production);
add(Environment::Production);
});
_contextMenu->addAction("Test Server", [=] {
add(MTP::Environment::Test);
add(Environment::Test);
});
_contextMenu->popup(QCursor::pos());
}, button->lifetime());

View File

@ -1689,10 +1689,7 @@ void SessionController::showPeerHistory(
PeerId peerId,
const SectionShow &params,
MsgId msgId) {
content()->showPeerHistory(peerId, params, msgId);
if (peerId && params.activation != anim::activation::background) {
_window->activate();
}
content()->showHistory(peerId, params, msgId);
}
void SessionController::showMessage(