2021-01-15 16:18:35 +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
|
|
|
|
*/
|
2021-04-22 09:13:13 +00:00
|
|
|
#include "calls/group/calls_volume_item.h"
|
2021-01-15 16:18:35 +00:00
|
|
|
|
2021-04-22 09:13:13 +00:00
|
|
|
#include "calls/group/calls_group_common.h"
|
2021-01-16 20:10:15 +00:00
|
|
|
#include "ui/effects/animation_value.h"
|
2021-01-16 15:11:43 +00:00
|
|
|
#include "ui/effects/cross_line.h"
|
2021-01-15 16:18:35 +00:00
|
|
|
#include "ui/widgets/continuous_sliders.h"
|
|
|
|
#include "styles/style_calls.h"
|
|
|
|
|
2021-01-17 21:44:03 +00:00
|
|
|
#include "ui/paint/arcs.h"
|
|
|
|
|
2021-01-15 16:18:35 +00:00
|
|
|
namespace Calls {
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
constexpr auto kMaxVolumePercent = 200;
|
|
|
|
|
2021-01-28 12:58:42 +00:00
|
|
|
const auto kSpeakerThreshold = std::vector<float>{
|
2021-01-17 21:44:03 +00:00
|
|
|
10.0f / kMaxVolumePercent,
|
|
|
|
50.0f / kMaxVolumePercent,
|
|
|
|
150.0f / kMaxVolumePercent };
|
|
|
|
|
2024-04-11 15:56:11 +00:00
|
|
|
constexpr auto kVolumeStickedValues
|
|
|
|
= std::array<std::pair<float64, float64>, 7>{{
|
2021-01-26 21:44:39 +00:00
|
|
|
{ 25. / kMaxVolumePercent, 2. / kMaxVolumePercent },
|
|
|
|
{ 50. / kMaxVolumePercent, 2. / kMaxVolumePercent },
|
|
|
|
{ 75. / kMaxVolumePercent, 2. / kMaxVolumePercent },
|
2021-06-17 12:42:50 +00:00
|
|
|
{ 100. / kMaxVolumePercent, 10. / kMaxVolumePercent },
|
2021-01-26 21:44:39 +00:00
|
|
|
{ 125. / kMaxVolumePercent, 2. / kMaxVolumePercent },
|
|
|
|
{ 150. / kMaxVolumePercent, 2. / kMaxVolumePercent },
|
|
|
|
{ 175. / kMaxVolumePercent, 2. / kMaxVolumePercent },
|
|
|
|
}};
|
|
|
|
|
2021-01-15 16:18:35 +00:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
MenuVolumeItem::MenuVolumeItem(
|
|
|
|
not_null<RpWidget*> parent,
|
|
|
|
const style::Menu &st,
|
|
|
|
rpl::producer<Group::ParticipantState> participantState,
|
|
|
|
int startVolume,
|
|
|
|
int maxVolume,
|
|
|
|
bool muted)
|
|
|
|
: Ui::Menu::ItemBase(parent, st)
|
|
|
|
, _maxVolume(maxVolume)
|
|
|
|
, _cloudMuted(muted)
|
|
|
|
, _localMuted(muted)
|
|
|
|
, _slider(base::make_unique_q<Ui::MediaSlider>(
|
|
|
|
this,
|
2021-01-24 22:45:26 +00:00
|
|
|
st::groupCallMenuVolumeSlider))
|
2021-01-15 16:18:35 +00:00
|
|
|
, _dummyAction(new QAction(parent))
|
2021-01-16 15:11:43 +00:00
|
|
|
, _st(st)
|
|
|
|
, _stCross(st::groupCallMuteCrossLine)
|
2021-01-17 21:44:03 +00:00
|
|
|
, _crossLineMute(std::make_unique<Ui::CrossLineAnimation>(_stCross, true))
|
|
|
|
, _arcs(std::make_unique<Ui::Paint::ArcsAnimation>(
|
|
|
|
st::groupCallSpeakerArcsAnimation,
|
|
|
|
kSpeakerThreshold,
|
|
|
|
_localMuted ? 0. : (startVolume / float(maxVolume)),
|
2021-01-30 10:26:46 +00:00
|
|
|
Ui::Paint::ArcsAnimation::Direction::Right)) {
|
2021-01-15 16:18:35 +00:00
|
|
|
|
|
|
|
initResizeHook(parent->sizeValue());
|
|
|
|
enableMouseSelecting();
|
2021-01-27 19:16:21 +00:00
|
|
|
enableMouseSelecting(_slider.get());
|
2021-01-15 16:18:35 +00:00
|
|
|
|
2021-01-24 22:45:26 +00:00
|
|
|
_slider->setAlwaysDisplayMarker(true);
|
|
|
|
|
2021-01-16 15:18:03 +00:00
|
|
|
sizeValue(
|
|
|
|
) | rpl::start_with_next([=](const QSize &size) {
|
|
|
|
const auto geometry = QRect(QPoint(), size);
|
2021-06-17 12:22:51 +00:00
|
|
|
_itemRect = geometry - st::groupCallMenuVolumePadding;
|
2021-01-16 15:18:03 +00:00
|
|
|
_speakerRect = QRect(_itemRect.topLeft(), _stCross.icon.size());
|
2021-01-17 21:44:03 +00:00
|
|
|
_arcPosition = _speakerRect.center()
|
|
|
|
+ QPoint(0, st::groupCallMenuSpeakerArcsSkip);
|
2021-06-17 12:22:51 +00:00
|
|
|
_slider->setGeometry(
|
|
|
|
st::groupCallMenuVolumeMargin.left(),
|
2021-01-27 17:04:30 +00:00
|
|
|
_speakerRect.y(),
|
2021-06-17 12:22:51 +00:00
|
|
|
(geometry.width()
|
|
|
|
- st::groupCallMenuVolumeMargin.left()
|
|
|
|
- st::groupCallMenuVolumeMargin.right()),
|
2021-01-27 17:04:30 +00:00
|
|
|
_speakerRect.height());
|
2021-01-16 15:18:03 +00:00
|
|
|
}, lifetime());
|
2021-01-16 15:11:43 +00:00
|
|
|
|
2021-01-17 21:44:03 +00:00
|
|
|
setCloudVolume(startVolume);
|
|
|
|
|
2021-01-15 16:18:35 +00:00
|
|
|
paintRequest(
|
|
|
|
) | rpl::start_with_next([=](const QRect &clip) {
|
2022-09-16 20:23:27 +00:00
|
|
|
auto p = QPainter(this);
|
2021-01-15 16:18:35 +00:00
|
|
|
|
2021-01-24 22:45:26 +00:00
|
|
|
const auto volume = _localMuted
|
|
|
|
? 0
|
2021-09-27 08:13:57 +00:00
|
|
|
: base::SafeRound(_slider->value() * kMaxVolumePercent);
|
2024-04-11 15:56:11 +00:00
|
|
|
const auto muteProgress
|
|
|
|
= _crossLineAnimation.value((!volume) ? 1. : 0.);
|
2021-01-16 20:10:15 +00:00
|
|
|
|
2021-01-15 16:18:35 +00:00
|
|
|
const auto selected = isSelected();
|
|
|
|
p.fillRect(clip, selected ? st.itemBgOver : st.itemBg);
|
2021-01-16 20:10:15 +00:00
|
|
|
|
|
|
|
const auto mutePen = anim::color(
|
|
|
|
unmuteColor(),
|
|
|
|
muteColor(),
|
|
|
|
muteProgress);
|
2021-01-16 15:11:43 +00:00
|
|
|
|
|
|
|
_crossLineMute->paint(
|
|
|
|
p,
|
2021-01-16 15:18:03 +00:00
|
|
|
_speakerRect.topLeft(),
|
2021-01-16 20:10:15 +00:00
|
|
|
muteProgress,
|
2021-06-17 12:22:51 +00:00
|
|
|
(muteProgress > 0) ? std::make_optional(mutePen) : std::nullopt);
|
2021-01-15 16:18:35 +00:00
|
|
|
|
2021-01-17 21:44:03 +00:00
|
|
|
{
|
|
|
|
p.translate(_arcPosition);
|
|
|
|
_arcs->paint(p);
|
|
|
|
}
|
|
|
|
}, lifetime());
|
2021-01-15 16:18:35 +00:00
|
|
|
|
|
|
|
_slider->setChangeProgressCallback([=](float64 value) {
|
|
|
|
const auto newMuted = (value == 0);
|
|
|
|
if (_localMuted != newMuted) {
|
|
|
|
_localMuted = newMuted;
|
|
|
|
_toggleMuteLocallyRequests.fire_copy(newMuted);
|
2021-01-16 15:11:43 +00:00
|
|
|
|
|
|
|
_crossLineAnimation.start(
|
2021-06-17 12:22:51 +00:00
|
|
|
[=] { update(_speakerRect); },
|
2021-01-16 15:11:43 +00:00
|
|
|
_localMuted ? 0. : 1.,
|
|
|
|
_localMuted ? 1. : 0.,
|
|
|
|
st::callPanelDuration);
|
2021-01-15 16:18:35 +00:00
|
|
|
}
|
|
|
|
if (value > 0) {
|
|
|
|
_changeVolumeLocallyRequests.fire(value * _maxVolume);
|
|
|
|
}
|
2021-01-17 21:44:03 +00:00
|
|
|
_arcs->setValue(value);
|
2021-06-17 12:22:51 +00:00
|
|
|
updateSliderColor(value);
|
2021-01-15 16:18:35 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const auto returnVolume = [=] {
|
|
|
|
_changeVolumeLocallyRequests.fire_copy(_cloudVolume);
|
|
|
|
};
|
|
|
|
|
|
|
|
_slider->setChangeFinishedCallback([=](float64 value) {
|
2021-09-27 08:13:57 +00:00
|
|
|
const auto newVolume = base::SafeRound(value * _maxVolume);
|
2021-01-15 16:18:35 +00:00
|
|
|
const auto muted = (value == 0);
|
|
|
|
|
|
|
|
if (!_cloudMuted && muted) {
|
|
|
|
returnVolume();
|
|
|
|
_localMuted = true;
|
|
|
|
_toggleMuteRequests.fire(true);
|
|
|
|
}
|
|
|
|
if (_cloudMuted && muted) {
|
|
|
|
returnVolume();
|
|
|
|
}
|
|
|
|
if (_cloudMuted && !muted) {
|
|
|
|
_waitingForUpdateVolume = true;
|
|
|
|
_localMuted = false;
|
|
|
|
_toggleMuteRequests.fire(false);
|
|
|
|
}
|
|
|
|
if (!_cloudMuted && !muted) {
|
|
|
|
_changeVolumeRequests.fire_copy(newVolume);
|
|
|
|
}
|
2021-06-17 12:22:51 +00:00
|
|
|
updateSliderColor(value);
|
2021-01-15 16:18:35 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
std::move(
|
|
|
|
participantState
|
|
|
|
) | rpl::start_with_next([=](const Group::ParticipantState &state) {
|
|
|
|
const auto newMuted = state.mutedByMe;
|
|
|
|
const auto newVolume = state.volume.value_or(0);
|
|
|
|
|
|
|
|
_cloudMuted = _localMuted = newMuted;
|
|
|
|
|
|
|
|
if (!newVolume) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (_waitingForUpdateVolume) {
|
2024-04-11 15:56:11 +00:00
|
|
|
const auto localVolume
|
|
|
|
= base::SafeRound(_slider->value() * _maxVolume);
|
2021-01-15 16:18:35 +00:00
|
|
|
if ((localVolume != newVolume)
|
|
|
|
&& (_cloudVolume == newVolume)) {
|
2021-01-26 08:15:00 +00:00
|
|
|
_changeVolumeRequests.fire(int(localVolume));
|
2021-01-15 16:18:35 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
setCloudVolume(newVolume);
|
|
|
|
}
|
|
|
|
_waitingForUpdateVolume = false;
|
|
|
|
}, lifetime());
|
2021-01-17 21:44:03 +00:00
|
|
|
|
2021-01-26 21:44:39 +00:00
|
|
|
_slider->setAdjustCallback([=](float64 value) {
|
|
|
|
for (const auto &snap : kVolumeStickedValues) {
|
|
|
|
if (value > (snap.first - snap.second)
|
|
|
|
&& value < (snap.first + snap.second)) {
|
|
|
|
return snap.first;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
});
|
|
|
|
|
2021-01-17 21:44:03 +00:00
|
|
|
initArcsAnimation();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MenuVolumeItem::initArcsAnimation() {
|
2021-01-27 17:04:30 +00:00
|
|
|
const auto lastTime = lifetime().make_state<int>(0);
|
2021-01-17 21:44:03 +00:00
|
|
|
_arcsAnimation.init([=](crl::time now) {
|
|
|
|
_arcs->update(now);
|
|
|
|
update(_speakerRect);
|
|
|
|
});
|
|
|
|
|
|
|
|
_arcs->startUpdateRequests(
|
|
|
|
) | rpl::start_with_next([=] {
|
|
|
|
if (!_arcsAnimation.animating()) {
|
2021-01-27 17:04:30 +00:00
|
|
|
*lastTime = crl::now();
|
2021-01-17 21:44:03 +00:00
|
|
|
_arcsAnimation.start();
|
|
|
|
}
|
|
|
|
}, lifetime());
|
|
|
|
|
|
|
|
_arcs->stopUpdateRequests(
|
|
|
|
) | rpl::start_with_next([=] {
|
|
|
|
_arcsAnimation.stop();
|
|
|
|
}, lifetime());
|
2021-01-15 16:18:35 +00:00
|
|
|
}
|
|
|
|
|
2021-01-16 20:10:15 +00:00
|
|
|
QColor MenuVolumeItem::unmuteColor() const {
|
|
|
|
return (isSelected()
|
|
|
|
? _st.itemFgOver
|
|
|
|
: isEnabled()
|
|
|
|
? _st.itemFg
|
|
|
|
: _st.itemFgDisabled)->c;
|
|
|
|
}
|
|
|
|
|
|
|
|
QColor MenuVolumeItem::muteColor() const {
|
|
|
|
return (isSelected()
|
|
|
|
? st::attentionButtonFgOver
|
|
|
|
: st::attentionButtonFg)->c;
|
|
|
|
}
|
|
|
|
|
2021-01-15 16:18:35 +00:00
|
|
|
void MenuVolumeItem::setCloudVolume(int volume) {
|
|
|
|
if (_cloudVolume == volume) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_cloudVolume = volume;
|
|
|
|
if (!_slider->isChanging()) {
|
2021-01-24 23:44:27 +00:00
|
|
|
setSliderVolume(_cloudMuted ? 0. : volume);
|
2021-01-15 16:18:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MenuVolumeItem::setSliderVolume(int volume) {
|
2021-06-17 12:22:51 +00:00
|
|
|
const auto value = float64(volume) / _maxVolume;
|
|
|
|
_slider->setValue(value);
|
|
|
|
updateSliderColor(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MenuVolumeItem::updateSliderColor(float64 value) {
|
|
|
|
value = std::clamp(value, 0., 1.);
|
|
|
|
const auto color = [](int rgb) {
|
|
|
|
return QColor(
|
|
|
|
int((rgb & 0xFF0000) >> 16),
|
|
|
|
int((rgb & 0x00FF00) >> 8),
|
|
|
|
int(rgb & 0x0000FF));
|
|
|
|
};
|
|
|
|
const auto colors = std::array<QColor, 4>{ {
|
|
|
|
color(0xF66464),
|
|
|
|
color(0xD0B738),
|
|
|
|
color(0x24CD80),
|
|
|
|
color(0x3BBCEC),
|
|
|
|
} };
|
|
|
|
_slider->setActiveFgOverride((value < 0.25)
|
|
|
|
? anim::color(colors[0], colors[1], value / 0.25)
|
|
|
|
: (value < 0.5)
|
|
|
|
? anim::color(colors[1], colors[2], (value - 0.25) / 0.25)
|
|
|
|
: anim::color(colors[2], colors[3], (value - 0.5) / 0.5));
|
2021-01-15 16:18:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
not_null<QAction*> MenuVolumeItem::action() const {
|
|
|
|
return _dummyAction;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MenuVolumeItem::isEnabled() const {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int MenuVolumeItem::contentHeight() const {
|
2021-06-17 12:22:51 +00:00
|
|
|
return st::groupCallMenuVolumePadding.top()
|
|
|
|
+ st::groupCallMenuVolumePadding.bottom()
|
|
|
|
+ _stCross.icon.height();
|
2021-01-15 16:18:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rpl::producer<bool> MenuVolumeItem::toggleMuteRequests() const {
|
|
|
|
return _toggleMuteRequests.events();
|
|
|
|
}
|
|
|
|
|
|
|
|
rpl::producer<bool> MenuVolumeItem::toggleMuteLocallyRequests() const {
|
|
|
|
return _toggleMuteLocallyRequests.events();
|
|
|
|
}
|
|
|
|
|
|
|
|
rpl::producer<int> MenuVolumeItem::changeVolumeRequests() const {
|
|
|
|
return _changeVolumeRequests.events();
|
|
|
|
}
|
|
|
|
|
|
|
|
rpl::producer<int> MenuVolumeItem::changeVolumeLocallyRequests() const {
|
|
|
|
return _changeVolumeLocallyRequests.events();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Calls
|