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"
|
|
|
|
|
|
|
|
#include "data/data_peer.h"
|
|
|
|
#include "data/data_session.h"
|
|
|
|
#include "info/profile/info_profile_values.h"
|
|
|
|
#include "lang/lang_keys.h"
|
2022-03-28 19:21:45 +00:00
|
|
|
#include "ui/boxes/choose_time.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-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-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-03-27 10:08:47 +00:00
|
|
|
class MuteItem final : public Ui::Menu::Action {
|
|
|
|
public:
|
|
|
|
MuteItem(
|
|
|
|
not_null<RpWidget*> parent,
|
|
|
|
const style::Menu &st,
|
|
|
|
not_null<PeerData*> peer);
|
|
|
|
|
|
|
|
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,
|
|
|
|
not_null<PeerData*> peer)
|
|
|
|
: Ui::Menu::Action(
|
|
|
|
parent,
|
|
|
|
st,
|
|
|
|
Ui::CreateChild<QAction>(parent.get()),
|
|
|
|
nullptr,
|
|
|
|
nullptr)
|
|
|
|
, _itemIconPosition(st.itemIconPosition)
|
|
|
|
, _isMuted(peer->owner().notifyIsMuted(peer)) {
|
|
|
|
|
|
|
|
Info::Profile::NotificationsEnabledValue(
|
|
|
|
peer
|
|
|
|
) | 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());
|
|
|
|
|
|
|
|
setClickedCallback([=] {
|
|
|
|
peer->owner().updateNotifySettings(
|
|
|
|
peer,
|
|
|
|
_isMuted ? 0 : Data::NotifySettings::kDefaultMutePeriod);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
Action::paintText(p);
|
|
|
|
|
|
|
|
const auto &icon = _isMuted ? st::menuIconUnmute : st::menuIconMute;
|
|
|
|
icon.paint(p, _itemIconPosition, width(), color);
|
|
|
|
}
|
|
|
|
|
2022-03-28 11:22:11 +00:00
|
|
|
void MuteBox(not_null<Ui::GenericBox*> box, not_null<PeerData*> peer) {
|
|
|
|
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();
|
|
|
|
const auto confirm = box->addButton(std::move(confirmText), [=] {
|
2022-03-30 21:34:21 +00:00
|
|
|
peer->owner().updateNotifySettings(peer, state->lastSeconds);
|
2022-03-28 11:22:11 +00:00
|
|
|
box->closeBox();
|
|
|
|
});
|
|
|
|
box->addButton(tr::lng_cancel(), [=] { box->closeBox(); });
|
|
|
|
}
|
|
|
|
|
2022-03-27 10:08:47 +00:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
void FillMuteMenu(
|
|
|
|
not_null<Ui::PopupMenu*> menu,
|
2022-03-28 11:22:11 +00:00
|
|
|
Args args) {
|
|
|
|
const auto peer = args.peer;
|
2022-03-27 10:08:47 +00:00
|
|
|
|
2022-03-30 21:34:21 +00:00
|
|
|
const auto soundIsNone = peer->owner().notifySoundIsNone(peer);
|
|
|
|
menu->addAction(
|
|
|
|
soundIsNone
|
|
|
|
? tr::lng_mute_menu_sound_on(tr::now)
|
|
|
|
: tr::lng_mute_menu_sound_off(tr::now),
|
|
|
|
[=] {
|
|
|
|
const auto soundIsNone = peer->owner().notifySoundIsNone(peer);
|
|
|
|
peer->owner().updateNotifySettings(peer, {}, {}, !soundIsNone);
|
|
|
|
},
|
|
|
|
soundIsNone ? &st::menuIconSoundOn : &st::menuIconSoundOff);
|
2022-03-27 13:02:27 +00:00
|
|
|
|
2022-03-28 11:22:11 +00:00
|
|
|
menu->addAction(
|
|
|
|
tr::lng_mute_menu_duration(tr::now),
|
|
|
|
[=, show = args.show] { show->showBox(Box(MuteBox, peer)); },
|
|
|
|
&st::menuIconMuteFor);
|
|
|
|
|
2022-03-27 10:08:47 +00:00
|
|
|
menu->addAction(
|
|
|
|
base::make_unique_q<MuteItem>(menu, menu->st().menu, peer));
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetupMuteMenu(
|
|
|
|
not_null<Ui::RpWidget*> parent,
|
|
|
|
rpl::producer<> triggers,
|
2022-03-28 11:22:11 +00:00
|
|
|
Args args) {
|
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;
|
|
|
|
}
|
|
|
|
state->menu = base::make_unique_q<Ui::PopupMenu>(
|
|
|
|
parent,
|
|
|
|
st::popupMenuWithIcons);
|
2022-03-28 11:22:11 +00:00
|
|
|
FillMuteMenu(state->menu.get(), args);
|
2022-03-27 10:08:47 +00:00
|
|
|
state->menu->popup(QCursor::pos());
|
|
|
|
}, parent->lifetime());
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace MuteMenu
|