2022-03-23 15:59:53 +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 "tray.h"
|
|
|
|
|
2022-04-20 22:39:01 +00:00
|
|
|
#include "core/application.h"
|
2022-06-11 00:07:22 +00:00
|
|
|
#include "core/core_settings.h"
|
2022-06-23 05:29:11 +00:00
|
|
|
#include "platform/platform_specific.h"
|
2022-04-20 22:39:01 +00:00
|
|
|
|
2022-04-18 13:34:59 +00:00
|
|
|
#include <QtWidgets/QApplication>
|
|
|
|
|
2022-03-23 15:59:53 +00:00
|
|
|
namespace Core {
|
|
|
|
|
|
|
|
Tray::Tray() {
|
|
|
|
}
|
|
|
|
|
2022-04-20 22:39:01 +00:00
|
|
|
void Tray::create() {
|
|
|
|
rebuildMenu();
|
2022-06-23 05:29:11 +00:00
|
|
|
using WorkMode = Settings::WorkMode;
|
|
|
|
if (Platform::TrayIconSupported()
|
|
|
|
&& (Core::App().settings().workMode() != WorkMode::WindowOnly)) {
|
2022-04-20 22:39:01 +00:00
|
|
|
_tray.createIcon();
|
|
|
|
}
|
|
|
|
|
|
|
|
Core::App().settings().workModeValue(
|
|
|
|
) | rpl::combine_previous(
|
2022-06-23 05:29:11 +00:00
|
|
|
) | rpl::start_with_next([=](WorkMode previous, WorkMode state) {
|
|
|
|
if (!Platform::TrayIconSupported()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto wasHasIcon = (previous != WorkMode::WindowOnly);
|
|
|
|
const auto nowHasIcon = (state != WorkMode::WindowOnly);
|
2022-04-20 22:39:01 +00:00
|
|
|
if (wasHasIcon != nowHasIcon) {
|
|
|
|
if (nowHasIcon) {
|
|
|
|
_tray.createIcon();
|
|
|
|
} else {
|
|
|
|
_tray.destroyIcon();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, _tray.lifetime());
|
|
|
|
|
|
|
|
Core::App().passcodeLockChanges(
|
|
|
|
) | rpl::start_with_next([=] {
|
|
|
|
rebuildMenu();
|
|
|
|
}, _tray.lifetime());
|
2022-04-18 13:34:59 +00:00
|
|
|
|
|
|
|
_tray.iconClicks(
|
|
|
|
) | rpl::start_with_next([=] {
|
|
|
|
const auto skipTrayClick = (_lastTrayClickTime > 0)
|
|
|
|
&& (crl::now() - _lastTrayClickTime
|
|
|
|
< QApplication::doubleClickInterval());
|
|
|
|
if (!skipTrayClick) {
|
|
|
|
_activeForTrayIconAction = Core::App().isActiveForTrayMenu();
|
|
|
|
_minimizeMenuItemClicks.fire({});
|
|
|
|
_lastTrayClickTime = crl::now();
|
|
|
|
}
|
|
|
|
}, _tray.lifetime());
|
2022-04-20 22:39:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Tray::rebuildMenu() {
|
|
|
|
_tray.destroyMenu();
|
|
|
|
_tray.createMenu();
|
|
|
|
|
|
|
|
{
|
|
|
|
auto minimizeText = _textUpdates.events(
|
|
|
|
) | rpl::map([=] {
|
|
|
|
_activeForTrayIconAction = Core::App().isActiveForTrayMenu();
|
|
|
|
return _activeForTrayIconAction
|
|
|
|
? tr::lng_minimize_to_tray(tr::now)
|
|
|
|
: tr::lng_open_from_tray(tr::now);
|
|
|
|
});
|
|
|
|
|
|
|
|
_tray.addAction(
|
|
|
|
std::move(minimizeText),
|
|
|
|
[=] { _minimizeMenuItemClicks.fire({}); });
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Core::App().passcodeLocked()) {
|
|
|
|
auto notificationsText = _textUpdates.events(
|
|
|
|
) | rpl::map([=] {
|
|
|
|
return Core::App().settings().desktopNotify()
|
|
|
|
? tr::lng_disable_notifications_from_tray(tr::now)
|
|
|
|
: tr::lng_enable_notifications_from_tray(tr::now);
|
|
|
|
});
|
|
|
|
|
|
|
|
_tray.addAction(
|
|
|
|
std::move(notificationsText),
|
|
|
|
[=] { toggleSoundNotifications(); });
|
|
|
|
}
|
|
|
|
|
|
|
|
_tray.addAction(tr::lng_quit_from_tray(), [] { Core::Quit(); });
|
|
|
|
|
|
|
|
updateMenuText();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Tray::updateMenuText() {
|
|
|
|
_textUpdates.fire({});
|
|
|
|
}
|
|
|
|
|
|
|
|
void Tray::updateIconCounters() {
|
|
|
|
_tray.updateIcon();
|
|
|
|
}
|
|
|
|
|
|
|
|
rpl::producer<> Tray::aboutToShowRequests() const {
|
|
|
|
return _tray.aboutToShowRequests();
|
|
|
|
}
|
|
|
|
|
|
|
|
rpl::producer<> Tray::showFromTrayRequests() const {
|
|
|
|
return rpl::merge(
|
|
|
|
_tray.showFromTrayRequests(),
|
|
|
|
_minimizeMenuItemClicks.events() | rpl::filter([=] {
|
|
|
|
return !_activeForTrayIconAction;
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
rpl::producer<> Tray::hideToTrayRequests() const {
|
2022-04-18 13:34:59 +00:00
|
|
|
auto triggers = rpl::merge(
|
2022-04-20 22:39:01 +00:00
|
|
|
_tray.hideToTrayRequests(),
|
|
|
|
_minimizeMenuItemClicks.events() | rpl::filter([=] {
|
|
|
|
return _activeForTrayIconAction;
|
|
|
|
})
|
|
|
|
);
|
2022-04-18 13:34:59 +00:00
|
|
|
if (_tray.hasTrayMessageSupport()) {
|
|
|
|
return std::move(triggers) | rpl::map([=]() -> rpl::empty_value {
|
|
|
|
_tray.showTrayMessage();
|
|
|
|
return {};
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return triggers;
|
|
|
|
}
|
2022-04-20 22:39:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Tray::toggleSoundNotifications() {
|
|
|
|
auto soundNotifyChanged = false;
|
|
|
|
auto flashBounceNotifyChanged = false;
|
|
|
|
auto &settings = Core::App().settings();
|
|
|
|
settings.setDesktopNotify(!settings.desktopNotify());
|
|
|
|
if (settings.desktopNotify()) {
|
|
|
|
if (settings.rememberedSoundNotifyFromTray()
|
|
|
|
&& !settings.soundNotify()) {
|
|
|
|
settings.setSoundNotify(true);
|
|
|
|
settings.setRememberedSoundNotifyFromTray(false);
|
|
|
|
soundNotifyChanged = true;
|
|
|
|
}
|
|
|
|
if (settings.rememberedFlashBounceNotifyFromTray()
|
|
|
|
&& !settings.flashBounceNotify()) {
|
|
|
|
settings.setFlashBounceNotify(true);
|
|
|
|
settings.setRememberedFlashBounceNotifyFromTray(false);
|
|
|
|
flashBounceNotifyChanged = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (settings.soundNotify()) {
|
|
|
|
settings.setSoundNotify(false);
|
|
|
|
settings.setRememberedSoundNotifyFromTray(true);
|
|
|
|
soundNotifyChanged = true;
|
|
|
|
} else {
|
|
|
|
settings.setRememberedSoundNotifyFromTray(false);
|
|
|
|
}
|
|
|
|
if (settings.flashBounceNotify()) {
|
|
|
|
settings.setFlashBounceNotify(false);
|
|
|
|
settings.setRememberedFlashBounceNotifyFromTray(true);
|
|
|
|
flashBounceNotifyChanged = true;
|
|
|
|
} else {
|
|
|
|
settings.setRememberedFlashBounceNotifyFromTray(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Core::App().saveSettingsDelayed();
|
|
|
|
using Change = Window::Notifications::ChangeType;
|
|
|
|
auto ¬ifications = Core::App().notifications();
|
|
|
|
notifications.notifySettingsChanged(Change::DesktopEnabled);
|
|
|
|
if (soundNotifyChanged) {
|
|
|
|
notifications.notifySettingsChanged(Change::SoundEnabled);
|
|
|
|
}
|
|
|
|
if (flashBounceNotifyChanged) {
|
|
|
|
notifications.notifySettingsChanged(Change::FlashBounceEnabled);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-23 05:29:11 +00:00
|
|
|
bool Tray::has() const {
|
|
|
|
return _tray.hasIcon();
|
|
|
|
}
|
|
|
|
|
2022-03-23 15:59:53 +00:00
|
|
|
} // namespace Core
|