2022-03-27 10:08:47 +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 "menu/menu_mute.h"
|
|
|
|
|
2022-04-04 21:00:58 +00:00
|
|
|
#include "boxes/ringtones_box.h"
|
2022-03-27 10:08:47 +00:00
|
|
|
#include "data/data_session.h"
|
2022-10-13 17:34:04 +00:00
|
|
|
#include "data/data_thread.h"
|
2022-04-01 22:02:34 +00:00
|
|
|
#include "data/notify/data_notify_settings.h"
|
2022-03-27 10:08:47 +00:00
|
|
|
#include "info/profile/info_profile_values.h"
|
|
|
|
#include "lang/lang_keys.h"
|
2022-04-01 22:02:34 +00:00
|
|
|
#include "main/main_session.h"
|
|
|
|
#include "main/main_session_settings.h"
|
2022-03-28 19:21:45 +00:00
|
|
|
#include "ui/boxes/choose_time.h"
|
2022-04-15 09:55:16 +00:00
|
|
|
#include "ui/boxes/confirm_box.h"
|
2022-04-01 22:02:34 +00:00
|
|
|
#include "ui/boxes/time_picker_box.h"
|
2022-03-27 10:08:47 +00:00
|
|
|
#include "ui/effects/animation_value.h"
|
2022-03-28 11:22:11 +00:00
|
|
|
#include "ui/layers/generic_box.h"
|
2022-04-01 16:07:17 +00:00
|
|
|
#include "ui/text/format_values.h"
|
2022-03-27 13:02:27 +00:00
|
|
|
#include "ui/widgets/checkbox.h"
|
2022-03-27 10:08:47 +00:00
|
|
|
#include "ui/widgets/menu/menu_action.h"
|
|
|
|
#include "ui/widgets/popup_menu.h"
|
2022-09-16 20:23:27 +00:00
|
|
|
#include "ui/painter.h"
|
2022-03-28 11:22:11 +00:00
|
|
|
#include "styles/style_boxes.h"
|
|
|
|
#include "styles/style_info.h" // infoTopBarMenu
|
|
|
|
#include "styles/style_layers.h"
|
2022-03-27 10:08:47 +00:00
|
|
|
#include "styles/style_menu_icons.h"
|
|
|
|
|
|
|
|
namespace MuteMenu {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2022-03-28 19:21:45 +00:00
|
|
|
constexpr auto kMuteDurSecondsDefault = crl::time(8) * 3600;
|
|
|
|
|
2022-04-01 22:02:34 +00:00
|
|
|
class IconWithText final : public Ui::Menu::Action {
|
|
|
|
public:
|
|
|
|
using Ui::Menu::Action::Action;
|
|
|
|
|
|
|
|
void setData(const QString &text, const QPoint &iconPosition);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
void paintEvent(QPaintEvent *e) override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
QPoint _iconPosition;
|
|
|
|
QString _text;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
void IconWithText::setData(const QString &text, const QPoint &iconPosition) {
|
|
|
|
_iconPosition = iconPosition;
|
|
|
|
_text = text;
|
|
|
|
}
|
|
|
|
|
|
|
|
void IconWithText::paintEvent(QPaintEvent *e) {
|
|
|
|
Ui::Menu::Action::paintEvent(e);
|
|
|
|
|
2022-09-16 20:23:27 +00:00
|
|
|
auto p = QPainter(this);
|
2022-04-01 22:02:34 +00:00
|
|
|
p.setFont(st::menuIconMuteForAnyTextFont);
|
|
|
|
p.setPen(st::menuIconColor);
|
|
|
|
p.drawText(_iconPosition, _text);
|
|
|
|
}
|
|
|
|
|
2022-03-27 10:08:47 +00:00
|
|
|
class MuteItem final : public Ui::Menu::Action {
|
|
|
|
public:
|
|
|
|
MuteItem(
|
|
|
|
not_null<RpWidget*> parent,
|
|
|
|
const style::Menu &st,
|
2022-10-13 17:34:04 +00:00
|
|
|
not_null<Data::Thread*> thread);
|
2022-03-27 10:08:47 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
void paintEvent(QPaintEvent *e) override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
const QPoint _itemIconPosition;
|
|
|
|
Ui::Animations::Simple _animation;
|
|
|
|
bool _isMuted = false;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
MuteItem::MuteItem(
|
|
|
|
not_null<RpWidget*> parent,
|
|
|
|
const style::Menu &st,
|
2022-10-13 17:34:04 +00:00
|
|
|
not_null<Data::Thread*> thread)
|
2022-03-27 10:08:47 +00:00
|
|
|
: Ui::Menu::Action(
|
|
|
|
parent,
|
|
|
|
st,
|
|
|
|
Ui::CreateChild<QAction>(parent.get()),
|
|
|
|
nullptr,
|
|
|
|
nullptr)
|
|
|
|
, _itemIconPosition(st.itemIconPosition)
|
2022-10-13 17:34:04 +00:00
|
|
|
, _isMuted(thread->owner().notifySettings().isMuted(thread)) {
|
2022-03-27 10:08:47 +00:00
|
|
|
Info::Profile::NotificationsEnabledValue(
|
2022-10-13 17:34:04 +00:00
|
|
|
thread
|
2022-03-27 10:08:47 +00:00
|
|
|
) | rpl::start_with_next([=](bool isUnmuted) {
|
|
|
|
const auto isMuted = !isUnmuted;
|
|
|
|
action()->setText(isMuted
|
|
|
|
? tr::lng_mute_menu_duration_unmute(tr::now)
|
|
|
|
: tr::lng_mute_menu_duration_forever(tr::now));
|
|
|
|
if (isMuted == _isMuted) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_isMuted = isMuted;
|
|
|
|
_animation.start(
|
|
|
|
[=] { update(); },
|
|
|
|
isMuted ? 0. : 1.,
|
|
|
|
isMuted ? 1. : 0.,
|
|
|
|
st::defaultPopupMenu.showDuration);
|
|
|
|
}, lifetime());
|
|
|
|
|
2022-10-19 10:59:37 +00:00
|
|
|
const auto weak = base::make_weak(thread);
|
2022-03-27 10:08:47 +00:00
|
|
|
setClickedCallback([=] {
|
2022-10-13 17:34:04 +00:00
|
|
|
if (const auto strong = weak.get()) {
|
|
|
|
strong->owner().notifySettings().update(
|
|
|
|
strong,
|
|
|
|
{ .unmute = _isMuted, .forever = !_isMuted });
|
|
|
|
}
|
2022-03-27 10:08:47 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void MuteItem::paintEvent(QPaintEvent *e) {
|
|
|
|
Painter p(this);
|
|
|
|
|
|
|
|
const auto progress = _animation.value(_isMuted ? 1. : 0.);
|
|
|
|
const auto color = anim::color(
|
2022-03-30 16:52:33 +00:00
|
|
|
st::menuIconAttentionColor,
|
2022-03-27 10:08:47 +00:00
|
|
|
st::settingsIconBg2,
|
|
|
|
progress);
|
|
|
|
p.setPen(color);
|
2022-04-16 23:12:49 +00:00
|
|
|
|
|
|
|
Action::paintBackground(p, Action::isSelected());
|
|
|
|
RippleButton::paintRipple(p, 0, 0);
|
2022-03-27 10:08:47 +00:00
|
|
|
Action::paintText(p);
|
|
|
|
|
|
|
|
const auto &icon = _isMuted ? st::menuIconUnmute : st::menuIconMute;
|
|
|
|
icon.paint(p, _itemIconPosition, width(), color);
|
|
|
|
}
|
|
|
|
|
2022-10-13 17:34:04 +00:00
|
|
|
void MuteBox(not_null<Ui::GenericBox*> box, not_null<Data::Thread*> thread) {
|
2022-03-28 11:22:11 +00:00
|
|
|
struct State {
|
2022-03-28 19:21:45 +00:00
|
|
|
int lastSeconds = 0;
|
2022-03-28 11:22:11 +00:00
|
|
|
};
|
|
|
|
|
2022-03-28 19:21:45 +00:00
|
|
|
auto chooseTimeResult = ChooseTimeWidget(box, kMuteDurSecondsDefault);
|
|
|
|
box->addRow(std::move(chooseTimeResult.widget));
|
2022-03-28 11:22:11 +00:00
|
|
|
|
2022-03-30 21:34:21 +00:00
|
|
|
const auto state = box->lifetime().make_state<State>();
|
2022-03-28 11:22:11 +00:00
|
|
|
|
|
|
|
box->setTitle(tr::lng_mute_box_title());
|
|
|
|
|
2022-03-30 21:34:21 +00:00
|
|
|
auto confirmText = std::move(
|
|
|
|
chooseTimeResult.secondsValue
|
|
|
|
) | rpl::map([=](int seconds) {
|
2022-03-28 19:21:45 +00:00
|
|
|
state->lastSeconds = seconds;
|
|
|
|
return !seconds
|
2022-03-28 11:22:11 +00:00
|
|
|
? tr::lng_mute_menu_unmute()
|
|
|
|
: tr::lng_mute_menu_mute();
|
|
|
|
}) | rpl::flatten_latest();
|
2022-10-13 17:34:04 +00:00
|
|
|
|
2022-10-19 10:59:37 +00:00
|
|
|
const auto weak = base::make_weak(thread);
|
2022-04-15 09:55:16 +00:00
|
|
|
Ui::ConfirmBox(box, {
|
|
|
|
.confirmed = [=] {
|
2022-10-13 17:34:04 +00:00
|
|
|
if (const auto strong = weak.get()) {
|
|
|
|
strong->owner().notifySettings().update(
|
|
|
|
strong,
|
|
|
|
{ .period = state->lastSeconds });
|
|
|
|
}
|
2022-04-15 09:55:16 +00:00
|
|
|
box->getDelegate()->hideLayer();
|
|
|
|
},
|
|
|
|
.confirmText = std::move(confirmText),
|
|
|
|
.cancelText = tr::lng_cancel(),
|
2022-04-01 16:07:17 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-10-13 17:34:04 +00:00
|
|
|
void PickMuteBox(
|
|
|
|
not_null<Ui::GenericBox*> box,
|
|
|
|
not_null<Data::Thread*> thread) {
|
2022-04-01 16:07:17 +00:00
|
|
|
struct State {
|
|
|
|
base::unique_qptr<Ui::PopupMenu> menu;
|
|
|
|
};
|
2022-09-01 19:24:46 +00:00
|
|
|
const auto seconds = Ui::DefaultTimePickerValues();
|
2022-04-01 16:07:17 +00:00
|
|
|
const auto phrases = ranges::views::all(
|
|
|
|
seconds
|
|
|
|
) | ranges::views::transform(Ui::FormatMuteFor) | ranges::to_vector;
|
|
|
|
|
|
|
|
const auto state = box->lifetime().make_state<State>();
|
|
|
|
|
|
|
|
const auto pickerCallback = TimePickerBox(box, seconds, phrases, 0);
|
|
|
|
|
2022-10-19 10:59:37 +00:00
|
|
|
const auto weak = base::make_weak(thread);
|
2022-04-15 09:55:16 +00:00
|
|
|
Ui::ConfirmBox(box, {
|
|
|
|
.confirmed = [=] {
|
|
|
|
const auto muteFor = pickerCallback();
|
2022-10-13 17:34:04 +00:00
|
|
|
if (const auto strong = weak.get()) {
|
|
|
|
strong->owner().notifySettings().update(
|
|
|
|
strong,
|
|
|
|
{ .period = muteFor });
|
|
|
|
strong->session().settings().addMutePeriod(muteFor);
|
|
|
|
strong->session().saveSettings();
|
|
|
|
}
|
2022-04-15 09:55:16 +00:00
|
|
|
box->closeBox();
|
|
|
|
},
|
|
|
|
.confirmText = tr::lng_mute_menu_mute(),
|
|
|
|
.cancelText = tr::lng_cancel(),
|
2022-03-28 11:22:11 +00:00
|
|
|
});
|
2022-04-01 16:07:17 +00:00
|
|
|
|
|
|
|
box->setTitle(tr::lng_mute_box_title());
|
|
|
|
|
|
|
|
const auto top = box->addTopButton(st::infoTopBarMenu);
|
|
|
|
top->setClickedCallback([=] {
|
|
|
|
if (state->menu) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
state->menu = base::make_unique_q<Ui::PopupMenu>(
|
|
|
|
top,
|
|
|
|
st::popupMenuWithIcons);
|
|
|
|
state->menu->addAction(
|
|
|
|
tr::lng_manage_messages_ttl_after_custom(tr::now),
|
2022-10-13 17:34:04 +00:00
|
|
|
[=] {
|
|
|
|
if (const auto strong = weak.get()) {
|
|
|
|
box->getDelegate()->show(Box(MuteBox, strong));
|
|
|
|
}
|
|
|
|
},
|
2022-04-01 16:07:17 +00:00
|
|
|
&st::menuIconCustomize);
|
|
|
|
state->menu->setDestroyedCallback(crl::guard(top, [=] {
|
|
|
|
top->setForceRippled(false);
|
|
|
|
}));
|
|
|
|
top->setForceRippled(true);
|
|
|
|
state->menu->popup(QCursor::pos());
|
|
|
|
});
|
2022-03-28 11:22:11 +00:00
|
|
|
}
|
|
|
|
|
2022-03-27 10:08:47 +00:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
void FillMuteMenu(
|
|
|
|
not_null<Ui::PopupMenu*> menu,
|
2022-10-13 17:34:04 +00:00
|
|
|
not_null<Data::Thread*> thread,
|
|
|
|
std::shared_ptr<Ui::Show> show) {
|
2022-10-19 10:59:37 +00:00
|
|
|
const auto weak = base::make_weak(thread);
|
2022-10-13 17:34:04 +00:00
|
|
|
const auto with = [=](Fn<void(not_null<Data::Thread*> thread)> handler) {
|
|
|
|
return [=] {
|
|
|
|
if (const auto strong = weak.get()) {
|
|
|
|
handler(strong);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
2022-03-27 10:08:47 +00:00
|
|
|
|
2022-04-04 21:00:58 +00:00
|
|
|
menu->addAction(
|
|
|
|
tr::lng_mute_menu_sound_select(tr::now),
|
2022-10-13 17:34:04 +00:00
|
|
|
with([=](not_null<Data::Thread*> thread) {
|
|
|
|
show->showBox(Box(ThreadRingtonesBox, thread));
|
|
|
|
}),
|
2022-04-04 21:00:58 +00:00
|
|
|
&st::menuIconSoundSelect);
|
|
|
|
|
2022-10-13 17:34:04 +00:00
|
|
|
const auto notifySettings = &thread->owner().notifySettings();
|
|
|
|
const auto soundIsNone = notifySettings->sound(thread).none;
|
2022-03-30 21:34:21 +00:00
|
|
|
menu->addAction(
|
|
|
|
soundIsNone
|
|
|
|
? tr::lng_mute_menu_sound_on(tr::now)
|
|
|
|
: tr::lng_mute_menu_sound_off(tr::now),
|
2022-10-13 17:34:04 +00:00
|
|
|
with([=](not_null<Data::Thread*> thread) {
|
|
|
|
auto sound = notifySettings->sound(thread);
|
2022-04-04 21:10:15 +00:00
|
|
|
sound.none = !sound.none;
|
2022-10-13 17:34:04 +00:00
|
|
|
notifySettings->update(thread, {}, {}, sound);
|
|
|
|
}),
|
2022-03-30 21:34:21 +00:00
|
|
|
soundIsNone ? &st::menuIconSoundOn : &st::menuIconSoundOff);
|
2022-03-27 13:02:27 +00:00
|
|
|
|
2022-04-01 22:02:34 +00:00
|
|
|
const auto &st = menu->st().menu;
|
|
|
|
const auto iconTextPosition = st.itemIconPosition
|
|
|
|
+ st::menuIconMuteForAnyTextPosition;
|
2022-10-13 17:34:04 +00:00
|
|
|
for (const auto muteFor : thread->session().settings().mutePeriods()) {
|
|
|
|
const auto callback = with([=](not_null<Data::Thread*> thread) {
|
|
|
|
notifySettings->update(thread, { .period = muteFor });
|
|
|
|
});
|
2022-04-01 22:02:34 +00:00
|
|
|
|
|
|
|
auto item = base::make_unique_q<IconWithText>(
|
|
|
|
menu,
|
|
|
|
st,
|
|
|
|
Ui::Menu::CreateAction(
|
|
|
|
menu->menu().get(),
|
|
|
|
tr::lng_mute_menu_duration_any(
|
|
|
|
tr::now,
|
|
|
|
lt_duration,
|
|
|
|
Ui::FormatMuteFor(muteFor)),
|
|
|
|
callback),
|
|
|
|
&st::menuIconMuteForAny,
|
|
|
|
&st::menuIconMuteForAny);
|
|
|
|
item->setData(Ui::FormatMuteForTiny(muteFor), iconTextPosition);
|
|
|
|
menu->addAction(std::move(item));
|
|
|
|
}
|
|
|
|
|
2022-03-28 11:22:11 +00:00
|
|
|
menu->addAction(
|
|
|
|
tr::lng_mute_menu_duration(tr::now),
|
2022-10-13 17:34:04 +00:00
|
|
|
with([=](not_null<Data::Thread*> thread) {
|
|
|
|
DEBUG_LOG(("Mute Info: PickMuteBox called."));
|
|
|
|
show->showBox(Box(PickMuteBox, thread));
|
|
|
|
}),
|
2022-03-28 11:22:11 +00:00
|
|
|
&st::menuIconMuteFor);
|
|
|
|
|
2022-03-27 10:08:47 +00:00
|
|
|
menu->addAction(
|
2022-10-13 17:34:04 +00:00
|
|
|
base::make_unique_q<MuteItem>(menu, menu->st().menu, thread));
|
2022-03-27 10:08:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SetupMuteMenu(
|
|
|
|
not_null<Ui::RpWidget*> parent,
|
|
|
|
rpl::producer<> triggers,
|
2022-10-13 17:34:04 +00:00
|
|
|
Fn<Data::Thread*()> makeThread,
|
|
|
|
std::shared_ptr<Ui::Show> show) {
|
2022-03-27 10:08:47 +00:00
|
|
|
struct State {
|
|
|
|
base::unique_qptr<Ui::PopupMenu> menu;
|
|
|
|
};
|
|
|
|
const auto state = parent->lifetime().make_state<State>();
|
|
|
|
std::move(
|
|
|
|
triggers
|
|
|
|
) | rpl::start_with_next([=] {
|
|
|
|
if (state->menu) {
|
|
|
|
return;
|
2022-10-13 17:34:04 +00:00
|
|
|
} else if (const auto thread = makeThread()) {
|
|
|
|
state->menu = base::make_unique_q<Ui::PopupMenu>(
|
|
|
|
parent,
|
|
|
|
st::popupMenuWithIcons);
|
|
|
|
FillMuteMenu(state->menu.get(), thread, show);
|
|
|
|
state->menu->popup(QCursor::pos());
|
2022-03-27 10:08:47 +00:00
|
|
|
}
|
|
|
|
}, parent->lifetime());
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace MuteMenu
|