2020-08-09 11:28:39 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
|
|
|
the official desktop application for the Telegram messaging service.
|
|
|
|
|
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
|
|
|
*/
|
|
|
|
#include "chat_helpers/send_context_menu.h"
|
|
|
|
|
|
|
|
#include "api/api_common.h"
|
|
|
|
#include "base/event_filter.h"
|
2020-08-09 11:50:32 +00:00
|
|
|
#include "boxes/abstract_box.h"
|
2020-08-09 11:28:39 +00:00
|
|
|
#include "core/shortcuts.h"
|
2020-08-09 11:50:32 +00:00
|
|
|
#include "history/view/history_view_schedule_box.h"
|
2020-08-09 11:28:39 +00:00
|
|
|
#include "lang/lang_keys.h"
|
|
|
|
#include "ui/widgets/popup_menu.h"
|
2021-08-12 07:06:16 +00:00
|
|
|
#include "data/data_peer.h"
|
|
|
|
#include "main/main_session.h"
|
|
|
|
#include "apiwrap.h"
|
2021-12-09 17:56:24 +00:00
|
|
|
#include "styles/style_menu_icons.h"
|
2020-08-09 11:28:39 +00:00
|
|
|
|
|
|
|
#include <QtWidgets/QApplication>
|
|
|
|
|
2020-08-10 12:22:54 +00:00
|
|
|
namespace SendMenu {
|
|
|
|
|
2020-08-09 11:50:32 +00:00
|
|
|
Fn<void()> DefaultSilentCallback(Fn<void(Api::SendOptions)> send) {
|
|
|
|
return [=] { send({ .silent = true }); };
|
|
|
|
}
|
|
|
|
|
|
|
|
Fn<void()> DefaultScheduleCallback(
|
|
|
|
not_null<Ui::RpWidget*> parent,
|
2020-08-10 12:22:54 +00:00
|
|
|
Type type,
|
2020-08-09 11:50:32 +00:00
|
|
|
Fn<void(Api::SendOptions)> send) {
|
|
|
|
const auto weak = Ui::MakeWeak(parent);
|
|
|
|
return [=] {
|
|
|
|
Ui::show(
|
|
|
|
HistoryView::PrepareScheduleBox(
|
|
|
|
weak,
|
|
|
|
type,
|
|
|
|
[=](Api::SendOptions options) { send(options); }),
|
|
|
|
Ui::LayerOption::KeepOther);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-08-09 11:28:39 +00:00
|
|
|
FillMenuResult FillSendMenu(
|
|
|
|
not_null<Ui::PopupMenu*> menu,
|
2020-11-19 03:18:45 +00:00
|
|
|
Type type,
|
2020-08-09 11:28:39 +00:00
|
|
|
Fn<void()> silent,
|
|
|
|
Fn<void()> schedule) {
|
|
|
|
if (!silent && !schedule) {
|
|
|
|
return FillMenuResult::None;
|
|
|
|
}
|
2020-11-19 03:18:45 +00:00
|
|
|
const auto now = type;
|
2020-08-10 12:22:54 +00:00
|
|
|
if (now == Type::Disabled
|
|
|
|
|| (!silent && now == Type::SilentOnly)) {
|
2020-08-09 11:28:39 +00:00
|
|
|
return FillMenuResult::None;
|
|
|
|
}
|
|
|
|
|
2020-08-10 12:22:54 +00:00
|
|
|
if (silent && now != Type::Reminder) {
|
2021-12-09 17:56:24 +00:00
|
|
|
menu->addAction(
|
|
|
|
tr::lng_send_silent_message(tr::now),
|
|
|
|
silent,
|
|
|
|
&st::menuIconMute);
|
2020-08-09 11:28:39 +00:00
|
|
|
}
|
2020-08-10 12:22:54 +00:00
|
|
|
if (schedule && now != Type::SilentOnly) {
|
2020-08-09 11:28:39 +00:00
|
|
|
menu->addAction(
|
2020-08-10 12:22:54 +00:00
|
|
|
(now == Type::Reminder
|
2020-08-09 11:28:39 +00:00
|
|
|
? tr::lng_reminder_message(tr::now)
|
|
|
|
: tr::lng_schedule_message(tr::now)),
|
2021-12-09 17:56:24 +00:00
|
|
|
schedule,
|
|
|
|
&st::menuIconSchedule);
|
2020-08-09 11:28:39 +00:00
|
|
|
}
|
|
|
|
return FillMenuResult::Success;
|
|
|
|
}
|
|
|
|
|
2020-08-10 12:22:54 +00:00
|
|
|
void SetupMenuAndShortcuts(
|
2020-08-09 11:28:39 +00:00
|
|
|
not_null<Ui::RpWidget*> button,
|
2020-08-10 12:22:54 +00:00
|
|
|
Fn<Type()> type,
|
2020-08-09 11:28:39 +00:00
|
|
|
Fn<void()> silent,
|
|
|
|
Fn<void()> schedule) {
|
|
|
|
if (!silent && !schedule) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto menu = std::make_shared<base::unique_qptr<Ui::PopupMenu>>();
|
|
|
|
const auto showMenu = [=] {
|
2021-12-09 17:56:24 +00:00
|
|
|
*menu = base::make_unique_q<Ui::PopupMenu>(
|
|
|
|
button,
|
|
|
|
st::popupMenuWithIcons);
|
2020-11-19 03:18:45 +00:00
|
|
|
const auto result = FillSendMenu(*menu, type(), silent, schedule);
|
2020-08-09 11:28:39 +00:00
|
|
|
const auto success = (result == FillMenuResult::Success);
|
|
|
|
if (success) {
|
|
|
|
(*menu)->popup(QCursor::pos());
|
|
|
|
}
|
|
|
|
return success;
|
|
|
|
};
|
|
|
|
base::install_event_filter(button, [=](not_null<QEvent*> e) {
|
|
|
|
if (e->type() == QEvent::ContextMenu && showMenu()) {
|
|
|
|
return base::EventFilterResult::Cancel;
|
|
|
|
}
|
|
|
|
return base::EventFilterResult::Continue;
|
|
|
|
});
|
|
|
|
|
|
|
|
Shortcuts::Requests(
|
|
|
|
) | rpl::start_with_next([=](not_null<Shortcuts::Request*> request) {
|
|
|
|
using Command = Shortcuts::Command;
|
|
|
|
|
|
|
|
const auto now = type();
|
2020-08-10 12:22:54 +00:00
|
|
|
if (now == Type::Disabled
|
|
|
|
|| (!silent && now == Type::SilentOnly)) {
|
2020-08-09 11:28:39 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
(silent
|
2020-08-10 12:22:54 +00:00
|
|
|
&& (now != Type::Reminder)
|
2020-08-09 11:28:39 +00:00
|
|
|
&& request->check(Command::SendSilentMessage)
|
|
|
|
&& request->handle([=] {
|
|
|
|
silent();
|
|
|
|
return true;
|
|
|
|
}))
|
|
|
|
||
|
|
|
|
(schedule
|
2020-08-10 12:22:54 +00:00
|
|
|
&& (now != Type::SilentOnly)
|
2020-08-09 11:28:39 +00:00
|
|
|
&& request->check(Command::ScheduleMessage)
|
|
|
|
&& request->handle([=] {
|
|
|
|
schedule();
|
|
|
|
return true;
|
|
|
|
}))
|
|
|
|
||
|
|
|
|
(request->check(Command::JustSendMessage) && request->handle([=] {
|
|
|
|
const auto post = [&](QEvent::Type type) {
|
|
|
|
QApplication::postEvent(
|
|
|
|
button,
|
|
|
|
new QMouseEvent(
|
|
|
|
type,
|
|
|
|
QPointF(0, 0),
|
|
|
|
Qt::LeftButton,
|
|
|
|
Qt::LeftButton,
|
|
|
|
Qt::NoModifier));
|
|
|
|
};
|
|
|
|
post(QEvent::MouseButtonPress);
|
|
|
|
post(QEvent::MouseButtonRelease);
|
|
|
|
return true;
|
|
|
|
}));
|
|
|
|
}, button->lifetime());
|
|
|
|
}
|
2020-08-10 12:22:54 +00:00
|
|
|
|
2021-08-12 07:06:16 +00:00
|
|
|
void SetupUnreadMentionsMenu(
|
|
|
|
not_null<Ui::RpWidget*> button,
|
|
|
|
Fn<PeerData*()> currentPeer) {
|
|
|
|
struct State {
|
|
|
|
base::unique_qptr<Ui::PopupMenu> menu;
|
|
|
|
base::flat_set<not_null<PeerData*>> sentForPeers;
|
|
|
|
};
|
|
|
|
const auto state = std::make_shared<State>();
|
|
|
|
const auto showMenu = [=] {
|
|
|
|
const auto peer = currentPeer();
|
|
|
|
if (!peer) {
|
|
|
|
return;
|
|
|
|
}
|
2021-12-09 17:56:24 +00:00
|
|
|
state->menu = base::make_unique_q<Ui::PopupMenu>(
|
|
|
|
button,
|
|
|
|
st::popupMenuWithIcons);
|
2021-08-12 07:06:16 +00:00
|
|
|
const auto text = tr::lng_context_mark_read_mentions_all(tr::now);
|
|
|
|
state->menu->addAction(text, [=] {
|
|
|
|
if (!state->sentForPeers.emplace(peer).second) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
peer->session().api().request(MTPmessages_ReadMentions(
|
|
|
|
peer->input
|
|
|
|
)).done([=](const MTPmessages_AffectedHistory &result) {
|
|
|
|
state->sentForPeers.remove(peer);
|
|
|
|
peer->session().api().applyAffectedHistory(peer, result);
|
2021-11-26 20:46:53 +00:00
|
|
|
}).fail([=] {
|
2021-08-12 07:06:16 +00:00
|
|
|
state->sentForPeers.remove(peer);
|
|
|
|
}).send();
|
2021-12-09 17:56:24 +00:00
|
|
|
}, &st::menuIconMarkRead);
|
2021-08-12 07:06:16 +00:00
|
|
|
state->menu->popup(QCursor::pos());
|
|
|
|
};
|
|
|
|
|
|
|
|
base::install_event_filter(button, [=](not_null<QEvent*> e) {
|
|
|
|
if (e->type() == QEvent::ContextMenu) {
|
|
|
|
showMenu();
|
|
|
|
return base::EventFilterResult::Cancel;
|
|
|
|
}
|
|
|
|
return base::EventFilterResult::Continue;
|
|
|
|
});
|
2022-01-26 16:01:40 +00:00
|
|
|
}
|
2021-08-12 07:06:16 +00:00
|
|
|
|
2022-01-26 16:01:40 +00:00
|
|
|
void SetupUnreadReactionsMenu(
|
|
|
|
not_null<Ui::RpWidget*> button,
|
|
|
|
Fn<PeerData*()> currentPeer) {
|
2021-08-12 07:06:16 +00:00
|
|
|
}
|
|
|
|
|
2020-08-10 12:22:54 +00:00
|
|
|
} // namespace SendMenu
|