2016-06-16 12:59:54 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 10:23:14 +00:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2016-06-16 12:59:54 +00:00
|
|
|
|
2018-01-03 10:23:14 +00:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2016-06-16 12:59:54 +00:00
|
|
|
*/
|
|
|
|
#include "window/main_window.h"
|
|
|
|
|
2017-03-04 10:23:56 +00:00
|
|
|
#include "storage/localstorage.h"
|
2016-11-04 11:14:47 +00:00
|
|
|
#include "platform/platform_window_title.h"
|
2019-09-17 16:13:12 +00:00
|
|
|
#include "base/platform/base_platform_info.h"
|
2018-01-13 12:45:11 +00:00
|
|
|
#include "history/history.h"
|
2017-02-03 20:07:26 +00:00
|
|
|
#include "window/themes/window_theme.h"
|
2019-06-06 10:21:40 +00:00
|
|
|
#include "window/window_session_controller.h"
|
2018-06-03 13:30:40 +00:00
|
|
|
#include "window/window_lock_widgets.h"
|
2019-06-03 22:34:34 +00:00
|
|
|
#include "window/window_outdated_bar.h"
|
2019-06-06 11:20:21 +00:00
|
|
|
#include "window/window_controller.h"
|
2018-06-03 13:30:40 +00:00
|
|
|
#include "boxes/confirm_box.h"
|
2019-07-24 11:45:24 +00:00
|
|
|
#include "main/main_account.h" // Account::sessionValue.
|
2018-06-03 13:30:40 +00:00
|
|
|
#include "core/click_handler_types.h"
|
2019-01-21 13:42:21 +00:00
|
|
|
#include "core/application.h"
|
2019-04-12 13:21:01 +00:00
|
|
|
#include "core/sandbox.h"
|
2018-06-03 13:30:40 +00:00
|
|
|
#include "lang/lang_keys.h"
|
2019-01-03 12:36:01 +00:00
|
|
|
#include "data/data_session.h"
|
2019-07-24 11:45:24 +00:00
|
|
|
#include "main/main_session.h"
|
2019-09-16 11:14:06 +00:00
|
|
|
#include "base/crc32hash.h"
|
2019-09-13 12:22:54 +00:00
|
|
|
#include "ui/ui_utility.h"
|
2018-06-03 13:30:40 +00:00
|
|
|
#include "apiwrap.h"
|
2016-12-31 15:19:22 +00:00
|
|
|
#include "mainwindow.h"
|
2019-09-13 06:06:02 +00:00
|
|
|
#include "facades.h"
|
|
|
|
#include "app.h"
|
2018-06-03 13:30:40 +00:00
|
|
|
#include "styles/style_window.h"
|
2019-09-18 11:19:05 +00:00
|
|
|
#include "styles/style_layers.h"
|
2016-11-04 11:14:47 +00:00
|
|
|
|
2019-09-04 07:19:15 +00:00
|
|
|
#include <QtWidgets/QDesktopWidget>
|
|
|
|
#include <QtCore/QMimeData>
|
|
|
|
#include <QtGui/QGuiApplication>
|
|
|
|
#include <QtGui/QWindow>
|
|
|
|
#include <QtGui/QScreen>
|
|
|
|
#include <QtGui/QDrag>
|
|
|
|
|
2016-06-16 12:59:54 +00:00
|
|
|
namespace Window {
|
2019-06-03 14:41:23 +00:00
|
|
|
namespace {
|
2016-06-16 12:59:54 +00:00
|
|
|
|
2019-02-19 06:57:53 +00:00
|
|
|
constexpr auto kSaveWindowPositionTimeout = crl::time(1000);
|
2017-06-21 21:38:31 +00:00
|
|
|
|
2019-06-03 14:41:23 +00:00
|
|
|
} // namespace
|
|
|
|
|
2017-05-12 15:27:19 +00:00
|
|
|
QImage LoadLogo() {
|
|
|
|
return QImage(qsl(":/gui/art/logo_256.png"));
|
|
|
|
}
|
|
|
|
|
|
|
|
QImage LoadLogoNoMargin() {
|
|
|
|
return QImage(qsl(":/gui/art/logo_256_no_margin.png"));
|
|
|
|
}
|
|
|
|
|
2018-10-07 11:45:46 +00:00
|
|
|
void ConvertIconToBlack(QImage &image) {
|
|
|
|
if (image.format() != QImage::Format_ARGB32_Premultiplied) {
|
|
|
|
image = std::move(image).convertToFormat(
|
|
|
|
QImage::Format_ARGB32_Premultiplied);
|
|
|
|
}
|
|
|
|
//const auto gray = red * 0.299 + green * 0.587 + blue * 0.114;
|
|
|
|
//const auto result = (gray - 100 < 0) ? 0 : (gray - 100) * 255 / 155;
|
|
|
|
constexpr auto scale = 255 / 155.;
|
|
|
|
constexpr auto red = 0.299;
|
|
|
|
constexpr auto green = 0.587;
|
|
|
|
constexpr auto blue = 0.114;
|
|
|
|
static constexpr auto shift = (1 << 24);
|
|
|
|
auto shifter = [](double value) {
|
|
|
|
return uint32(value * shift);
|
|
|
|
};
|
|
|
|
constexpr auto iscale = shifter(scale);
|
|
|
|
constexpr auto ired = shifter(red);
|
|
|
|
constexpr auto igreen = shifter(green);
|
|
|
|
constexpr auto iblue = shifter(blue);
|
|
|
|
constexpr auto threshold = 100;
|
|
|
|
constexpr auto ithreshold = shifter(threshold);
|
|
|
|
|
|
|
|
const auto width = image.width();
|
|
|
|
const auto height = image.height();
|
|
|
|
const auto data = reinterpret_cast<uint32*>(image.bits());
|
|
|
|
const auto intsPerLine = image.bytesPerLine() / 4;
|
|
|
|
const auto intsPerLineAdded = intsPerLine - width;
|
|
|
|
|
|
|
|
auto pixel = data;
|
|
|
|
for (auto j = 0; j != height; ++j) {
|
|
|
|
for (auto i = 0; i != width; ++i) {
|
|
|
|
const auto value = *pixel;
|
|
|
|
const auto gray = (((value >> 16) & 0xFF) * ired
|
|
|
|
+ ((value >> 8) & 0xFF) * igreen
|
|
|
|
+ (value & 0xFF) * iblue) >> 24;
|
|
|
|
const auto small = gray - threshold;
|
|
|
|
const auto test = ~small;
|
|
|
|
const auto result = (test >> 31) * small * iscale;
|
|
|
|
const auto component = (result >> 24) & 0xFF;
|
|
|
|
*pixel++ = (value & 0xFF000000U)
|
|
|
|
| (component << 16)
|
|
|
|
| (component << 8)
|
|
|
|
| component;
|
|
|
|
}
|
|
|
|
pixel += intsPerLineAdded;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-06 11:59:00 +00:00
|
|
|
QIcon CreateOfficialIcon(Main::Account *account) {
|
2019-02-04 13:34:50 +00:00
|
|
|
auto image = Core::IsAppLaunched() ? Core::App().logo() : LoadLogo();
|
2019-06-06 11:59:00 +00:00
|
|
|
if (account
|
|
|
|
&& account->sessionExists()
|
|
|
|
&& account->session().supportMode()) {
|
2018-10-07 11:45:46 +00:00
|
|
|
ConvertIconToBlack(image);
|
2017-05-12 15:27:19 +00:00
|
|
|
}
|
2018-10-07 11:45:46 +00:00
|
|
|
return QIcon(App::pixmapFromImageInPlace(std::move(image)));
|
2017-05-12 15:27:19 +00:00
|
|
|
}
|
|
|
|
|
2019-06-06 11:59:00 +00:00
|
|
|
QIcon CreateIcon(Main::Account *account) {
|
|
|
|
auto result = CreateOfficialIcon(account);
|
2019-06-03 14:41:23 +00:00
|
|
|
if (Platform::IsLinux()) {
|
2017-05-12 15:27:19 +00:00
|
|
|
return QIcon::fromTheme("telegram", result);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-06-06 11:20:21 +00:00
|
|
|
MainWindow::MainWindow(not_null<Controller*> controller)
|
|
|
|
: _controller(controller)
|
|
|
|
, _positionUpdatedTimer([=] { savePosition(); })
|
2019-06-03 22:34:34 +00:00
|
|
|
, _outdated(CreateOutdatedBar(this))
|
2016-11-09 08:34:38 +00:00
|
|
|
, _body(this)
|
2017-06-21 21:38:31 +00:00
|
|
|
, _titleText(qsl("Telegram")) {
|
2018-10-07 11:45:46 +00:00
|
|
|
subscribe(Theme::Background(), [=](
|
|
|
|
const Theme::BackgroundUpdate &data) {
|
2016-12-23 13:21:01 +00:00
|
|
|
if (data.paletteChanged()) {
|
2016-12-31 13:34:41 +00:00
|
|
|
updatePalette();
|
2016-11-04 11:14:47 +00:00
|
|
|
}
|
|
|
|
});
|
2018-10-07 11:45:46 +00:00
|
|
|
subscribe(Global::RefUnreadCounterUpdate(), [=] {
|
|
|
|
updateUnreadCounter();
|
|
|
|
});
|
|
|
|
subscribe(Global::RefWorkMode(), [=](DBIWorkMode mode) {
|
|
|
|
workmodeUpdated(mode);
|
|
|
|
});
|
2019-06-06 09:37:12 +00:00
|
|
|
|
2019-01-21 13:42:21 +00:00
|
|
|
Core::App().termsLockValue(
|
2018-06-03 13:30:40 +00:00
|
|
|
) | rpl::start_with_next([=] {
|
|
|
|
checkLockByTerms();
|
|
|
|
}, lifetime());
|
|
|
|
|
2019-06-03 22:34:34 +00:00
|
|
|
if (_outdated) {
|
|
|
|
_outdated->heightValue(
|
|
|
|
) | rpl::filter([=] {
|
|
|
|
return window()->windowHandle() != nullptr;
|
|
|
|
}) | rpl::start_with_next([=](int height) {
|
|
|
|
if (!height) {
|
|
|
|
crl::on_main(this, [=] { _outdated.destroy(); });
|
|
|
|
}
|
|
|
|
updateControlsGeometry();
|
|
|
|
}, _outdated->lifetime());
|
|
|
|
}
|
|
|
|
|
2017-06-21 21:38:31 +00:00
|
|
|
_isActiveTimer.setCallback([this] { updateIsActive(0); });
|
2016-11-04 11:14:47 +00:00
|
|
|
}
|
|
|
|
|
2019-06-06 11:59:00 +00:00
|
|
|
Main::Account &MainWindow::account() const {
|
|
|
|
return _controller->account();
|
|
|
|
}
|
|
|
|
|
2019-06-06 11:20:21 +00:00
|
|
|
Window::SessionController *MainWindow::sessionController() const {
|
|
|
|
return _controller->sessionController();
|
|
|
|
}
|
|
|
|
|
2018-06-03 13:30:40 +00:00
|
|
|
void MainWindow::checkLockByTerms() {
|
2019-01-21 13:42:21 +00:00
|
|
|
const auto data = Core::App().termsLocked();
|
2019-06-06 11:59:00 +00:00
|
|
|
if (!data || !account().sessionExists()) {
|
2018-06-03 13:30:40 +00:00
|
|
|
if (_termsBox) {
|
|
|
|
_termsBox->closeBox();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Ui::hideSettingsAndLayer(anim::type::instant);
|
|
|
|
const auto box = Ui::show(Box<TermsBox>(
|
|
|
|
*data,
|
2019-06-18 16:53:27 +00:00
|
|
|
tr::lng_terms_agree(),
|
|
|
|
tr::lng_terms_decline()));
|
2018-06-03 13:30:40 +00:00
|
|
|
|
|
|
|
box->setCloseByEscape(false);
|
|
|
|
box->setCloseByOutsideClick(false);
|
|
|
|
|
|
|
|
const auto id = data->id;
|
|
|
|
box->agreeClicks(
|
|
|
|
) | rpl::start_with_next([=] {
|
|
|
|
const auto mention = box ? box->lastClickedMention() : QString();
|
2019-06-06 11:59:00 +00:00
|
|
|
if (account().sessionExists()) {
|
|
|
|
account().session().api().acceptTerms(id);
|
2018-06-03 13:30:40 +00:00
|
|
|
if (!mention.isEmpty()) {
|
2018-07-09 18:13:48 +00:00
|
|
|
MentionClickHandler(mention).onClick({});
|
2018-06-03 13:30:40 +00:00
|
|
|
}
|
|
|
|
}
|
2019-01-21 13:42:21 +00:00
|
|
|
Core::App().unlockTerms();
|
2018-06-03 13:30:40 +00:00
|
|
|
}, box->lifetime());
|
|
|
|
|
|
|
|
box->cancelClicks(
|
|
|
|
) | rpl::start_with_next([=] {
|
|
|
|
showTermsDecline();
|
|
|
|
}, box->lifetime());
|
|
|
|
|
|
|
|
connect(box, &QObject::destroyed, [=] {
|
|
|
|
crl::on_main(this, [=] { checkLockByTerms(); });
|
|
|
|
});
|
|
|
|
|
|
|
|
_termsBox = box;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MainWindow::showTermsDecline() {
|
|
|
|
const auto box = Ui::show(
|
|
|
|
Box<Window::TermsBox>(
|
2019-06-19 15:09:03 +00:00
|
|
|
TextWithEntities{ tr::lng_terms_update_sorry(tr::now) },
|
2019-06-18 16:53:27 +00:00
|
|
|
tr::lng_terms_decline_and_delete(),
|
|
|
|
tr::lng_terms_back(),
|
2018-06-03 13:30:40 +00:00
|
|
|
true),
|
2019-09-18 11:19:05 +00:00
|
|
|
Ui::LayerOption::KeepOther);
|
2018-06-03 13:30:40 +00:00
|
|
|
|
|
|
|
box->agreeClicks(
|
|
|
|
) | rpl::start_with_next([=] {
|
|
|
|
if (box) {
|
|
|
|
box->closeBox();
|
|
|
|
}
|
|
|
|
showTermsDelete();
|
|
|
|
}, box->lifetime());
|
|
|
|
|
|
|
|
box->cancelClicks(
|
|
|
|
) | rpl::start_with_next([=] {
|
|
|
|
if (box) {
|
|
|
|
box->closeBox();
|
|
|
|
}
|
|
|
|
}, box->lifetime());
|
|
|
|
}
|
|
|
|
|
|
|
|
void MainWindow::showTermsDelete() {
|
2019-09-18 11:19:05 +00:00
|
|
|
const auto box = std::make_shared<QPointer<Ui::BoxContent>>();
|
2019-03-27 08:37:25 +00:00
|
|
|
const auto deleteByTerms = [=] {
|
2019-06-06 11:59:00 +00:00
|
|
|
if (account().sessionExists()) {
|
|
|
|
account().session().termsDeleteNow();
|
2019-03-27 08:37:25 +00:00
|
|
|
} else {
|
|
|
|
Ui::hideLayer();
|
|
|
|
}
|
|
|
|
};
|
2018-06-03 13:30:40 +00:00
|
|
|
*box = Ui::show(
|
|
|
|
Box<ConfirmBox>(
|
2019-06-19 15:09:03 +00:00
|
|
|
tr::lng_terms_delete_warning(tr::now),
|
|
|
|
tr::lng_terms_delete_now(tr::now),
|
2018-06-03 13:30:40 +00:00
|
|
|
st::attentionBoxButton,
|
2019-03-27 08:37:25 +00:00
|
|
|
deleteByTerms,
|
2018-06-03 13:30:40 +00:00
|
|
|
[=] { if (*box) (*box)->closeBox(); }),
|
2019-09-18 11:19:05 +00:00
|
|
|
Ui::LayerOption::KeepOther);
|
2018-06-03 13:30:40 +00:00
|
|
|
}
|
|
|
|
|
2016-12-31 15:19:22 +00:00
|
|
|
bool MainWindow::hideNoQuit() {
|
2017-08-08 09:56:10 +00:00
|
|
|
if (App::quitting()) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-03-04 19:36:59 +00:00
|
|
|
if (Global::WorkMode().value() == dbiwmTrayOnly || Global::WorkMode().value() == dbiwmWindowAndTray) {
|
2016-12-31 15:19:22 +00:00
|
|
|
if (minimizeToTray()) {
|
|
|
|
Ui::showChatsList();
|
|
|
|
return true;
|
|
|
|
}
|
2019-06-03 14:41:23 +00:00
|
|
|
} else if (Platform::IsMac()) {
|
2016-12-31 15:19:22 +00:00
|
|
|
closeWithoutDestroy();
|
|
|
|
updateIsActive(Global::OfflineBlurTimeout());
|
|
|
|
updateGlobalMenu();
|
|
|
|
Ui::showChatsList();
|
|
|
|
return true;
|
|
|
|
}
|
2017-01-01 16:45:20 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MainWindow::clearWidgets() {
|
|
|
|
clearWidgetsHook();
|
|
|
|
updateGlobalMenu();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MainWindow::updateIsActive(int timeout) {
|
2017-06-21 21:38:31 +00:00
|
|
|
if (timeout > 0) {
|
|
|
|
return _isActiveTimer.callOnce(timeout);
|
2017-01-01 16:45:20 +00:00
|
|
|
}
|
|
|
|
_isActive = computeIsActive();
|
|
|
|
updateIsActiveHook();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MainWindow::computeIsActive() const {
|
|
|
|
return isActiveWindow() && isVisible() && !(windowState() & Qt::WindowMinimized);
|
2016-12-31 15:19:22 +00:00
|
|
|
}
|
|
|
|
|
2017-05-12 15:27:19 +00:00
|
|
|
void MainWindow::updateWindowIcon() {
|
2019-06-06 11:59:00 +00:00
|
|
|
const auto supportIcon = account().sessionExists()
|
|
|
|
&& account().session().supportMode();
|
|
|
|
if (supportIcon != _usingSupportIcon || _icon.isNull()) {
|
|
|
|
_icon = CreateIcon(&account());
|
2018-10-07 11:45:46 +00:00
|
|
|
_usingSupportIcon = supportIcon;
|
|
|
|
}
|
2017-05-12 15:27:19 +00:00
|
|
|
setWindowIcon(_icon);
|
|
|
|
}
|
|
|
|
|
2016-11-04 11:14:47 +00:00
|
|
|
void MainWindow::init() {
|
2017-10-16 14:16:01 +00:00
|
|
|
Expects(!windowHandle());
|
2018-06-05 18:14:38 +00:00
|
|
|
|
2017-10-16 14:16:01 +00:00
|
|
|
createWinId();
|
|
|
|
|
2016-11-08 14:07:25 +00:00
|
|
|
initHook();
|
2017-05-12 15:27:19 +00:00
|
|
|
updateWindowIcon();
|
2016-11-08 14:07:25 +00:00
|
|
|
|
2019-04-05 10:13:54 +00:00
|
|
|
// Non-queued activeChanged handlers must use QtSignalProducer.
|
|
|
|
connect(
|
|
|
|
windowHandle(),
|
|
|
|
&QWindow::activeChanged,
|
|
|
|
this,
|
|
|
|
[=] { handleActiveChanged(); },
|
|
|
|
Qt::QueuedConnection);
|
|
|
|
connect(
|
|
|
|
windowHandle(),
|
|
|
|
&QWindow::windowStateChanged,
|
|
|
|
this,
|
|
|
|
[=](Qt::WindowState state) { handleStateChanged(state); });
|
2017-05-19 14:02:55 +00:00
|
|
|
|
2016-12-31 13:34:41 +00:00
|
|
|
updatePalette();
|
2016-11-04 11:14:47 +00:00
|
|
|
|
2016-11-08 14:07:25 +00:00
|
|
|
if ((_title = Platform::CreateTitleWidget(this))) {
|
|
|
|
_title->init();
|
|
|
|
}
|
2016-11-04 11:14:47 +00:00
|
|
|
|
2016-11-07 15:24:28 +00:00
|
|
|
initSize();
|
2016-11-15 11:56:49 +00:00
|
|
|
updateUnreadCounter();
|
2016-11-04 11:14:47 +00:00
|
|
|
}
|
|
|
|
|
2017-05-19 14:02:55 +00:00
|
|
|
void MainWindow::handleStateChanged(Qt::WindowState state) {
|
|
|
|
stateChangedHook(state);
|
|
|
|
updateIsActive((state == Qt::WindowMinimized) ? Global::OfflineBlurTimeout() : Global::OnlineFocusTimeout());
|
2019-03-10 13:40:44 +00:00
|
|
|
Core::App().updateNonIdle();
|
2017-05-19 14:02:55 +00:00
|
|
|
if (state == Qt::WindowMinimized && Global::WorkMode().value() == dbiwmTrayOnly) {
|
|
|
|
minimizeToTray();
|
|
|
|
}
|
|
|
|
savePosition(state);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MainWindow::handleActiveChanged() {
|
2017-08-08 09:31:48 +00:00
|
|
|
if (isActiveWindow()) {
|
2019-01-21 13:42:21 +00:00
|
|
|
Core::App().checkMediaViewActivation();
|
2017-05-19 14:02:55 +00:00
|
|
|
}
|
|
|
|
App::CallDelayed(1, this, [this] {
|
|
|
|
updateTrayMenu();
|
2017-11-15 12:46:34 +00:00
|
|
|
handleActiveChangedHook();
|
2017-05-19 14:02:55 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-12-31 13:34:41 +00:00
|
|
|
void MainWindow::updatePalette() {
|
2018-06-26 21:33:30 +00:00
|
|
|
Ui::ForceFullRepaint(this);
|
|
|
|
|
2016-12-31 13:34:41 +00:00
|
|
|
auto p = palette();
|
|
|
|
p.setColor(QPalette::Window, st::windowBg->c);
|
|
|
|
setPalette(p);
|
|
|
|
}
|
|
|
|
|
2016-11-04 11:14:47 +00:00
|
|
|
HitTestResult MainWindow::hitTest(const QPoint &p) const {
|
|
|
|
auto titleResult = _title ? _title->hitTest(p - _title->geometry().topLeft()) : Window::HitTestResult::None;
|
|
|
|
if (titleResult != Window::HitTestResult::None) {
|
|
|
|
return titleResult;
|
|
|
|
} else if (rect().contains(p)) {
|
|
|
|
return Window::HitTestResult::Client;
|
|
|
|
}
|
|
|
|
return Window::HitTestResult::None;
|
|
|
|
}
|
|
|
|
|
2019-06-03 14:41:23 +00:00
|
|
|
int MainWindow::computeMinHeight() const {
|
|
|
|
const auto title = _title ? _title->height() : 0;
|
2019-06-03 22:34:34 +00:00
|
|
|
const auto outdated = [&] {
|
|
|
|
if (!_outdated) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
_outdated->resizeToWidth(st::windowMinWidth);
|
|
|
|
return _outdated->height();
|
|
|
|
}();
|
|
|
|
return title + outdated + st::windowMinHeight;
|
2019-06-03 14:41:23 +00:00
|
|
|
}
|
|
|
|
|
2016-11-04 11:14:47 +00:00
|
|
|
void MainWindow::initSize() {
|
|
|
|
setMinimumWidth(st::windowMinWidth);
|
2019-06-03 14:41:23 +00:00
|
|
|
setMinimumHeight(computeMinHeight());
|
2016-11-04 11:14:47 +00:00
|
|
|
|
2017-07-03 12:23:41 +00:00
|
|
|
auto position = cWindowPos();
|
|
|
|
DEBUG_LOG(("Window Pos: Initializing first %1, %2, %3, %4 (maximized %5)").arg(position.x).arg(position.y).arg(position.w).arg(position.h).arg(Logs::b(position.maximized)));
|
|
|
|
|
2016-11-04 11:14:47 +00:00
|
|
|
auto avail = QDesktopWidget().availableGeometry();
|
|
|
|
bool maximized = false;
|
2018-10-20 19:04:10 +00:00
|
|
|
auto geom = QRect(
|
|
|
|
avail.x() + std::max(
|
|
|
|
(avail.width() - st::windowDefaultWidth) / 2,
|
|
|
|
0),
|
|
|
|
avail.y() + std::max(
|
|
|
|
(avail.height() - st::windowDefaultHeight) / 2,
|
|
|
|
0),
|
|
|
|
st::windowDefaultWidth,
|
|
|
|
st::windowDefaultHeight);
|
2017-07-03 12:23:41 +00:00
|
|
|
if (position.w && position.h) {
|
2016-11-04 11:14:47 +00:00
|
|
|
for (auto screen : QGuiApplication::screens()) {
|
2017-07-03 12:23:41 +00:00
|
|
|
if (position.moncrc == screenNameChecksum(screen->name())) {
|
2016-11-04 11:14:47 +00:00
|
|
|
auto screenGeometry = screen->geometry();
|
2017-07-03 12:23:41 +00:00
|
|
|
DEBUG_LOG(("Window Pos: Screen found, screen geometry: %1, %2, %3, %4").arg(screenGeometry.x()).arg(screenGeometry.y()).arg(screenGeometry.width()).arg(screenGeometry.height()));
|
|
|
|
|
2016-11-04 11:14:47 +00:00
|
|
|
auto w = screenGeometry.width(), h = screenGeometry.height();
|
|
|
|
if (w >= st::windowMinWidth && h >= st::windowMinHeight) {
|
2017-07-18 16:38:11 +00:00
|
|
|
if (position.x < 0) position.x = 0;
|
|
|
|
if (position.y < 0) position.y = 0;
|
2017-07-03 12:23:41 +00:00
|
|
|
if (position.w > w) position.w = w;
|
|
|
|
if (position.h > h) position.h = h;
|
|
|
|
position.x += screenGeometry.x();
|
|
|
|
position.y += screenGeometry.y();
|
|
|
|
if (position.x + st::windowMinWidth <= screenGeometry.x() + screenGeometry.width() &&
|
|
|
|
position.y + st::windowMinHeight <= screenGeometry.y() + screenGeometry.height()) {
|
|
|
|
DEBUG_LOG(("Window Pos: Resulting geometry is %1, %2, %3, %4").arg(position.x).arg(position.y).arg(position.w).arg(position.h));
|
|
|
|
geom = QRect(position.x, position.y, position.w, position.h);
|
2016-11-04 11:14:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-07-03 12:23:41 +00:00
|
|
|
maximized = position.maximized;
|
2016-11-04 11:14:47 +00:00
|
|
|
}
|
2017-07-03 12:23:41 +00:00
|
|
|
DEBUG_LOG(("Window Pos: Setting first %1, %2, %3, %4").arg(geom.x()).arg(geom.y()).arg(geom.width()).arg(geom.height()));
|
2016-11-04 11:14:47 +00:00
|
|
|
setGeometry(geom);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MainWindow::positionUpdated() {
|
2018-06-05 18:14:38 +00:00
|
|
|
_positionUpdatedTimer.callOnce(kSaveWindowPositionTimeout);
|
2016-11-04 11:14:47 +00:00
|
|
|
}
|
|
|
|
|
2016-11-09 08:34:38 +00:00
|
|
|
bool MainWindow::titleVisible() const {
|
|
|
|
return _title && !_title->isHidden();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MainWindow::setTitleVisible(bool visible) {
|
2016-11-08 14:07:25 +00:00
|
|
|
if (_title && (_title->isHidden() == visible)) {
|
|
|
|
_title->setVisible(visible);
|
|
|
|
updateControlsGeometry();
|
|
|
|
}
|
2016-11-09 08:34:38 +00:00
|
|
|
titleVisibilityChangedHook();
|
2016-11-08 14:07:25 +00:00
|
|
|
}
|
|
|
|
|
2016-11-04 11:14:47 +00:00
|
|
|
int32 MainWindow::screenNameChecksum(const QString &name) const {
|
2019-09-16 11:14:06 +00:00
|
|
|
const auto bytes = name.toUtf8();
|
|
|
|
return base::crc32(bytes.constData(), bytes.size());
|
2016-11-04 11:14:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MainWindow::setPositionInited() {
|
|
|
|
_positionInited = true;
|
|
|
|
}
|
|
|
|
|
2019-04-12 13:21:01 +00:00
|
|
|
void MainWindow::attachToTrayIcon(not_null<QSystemTrayIcon*> icon) {
|
|
|
|
icon->setToolTip(str_const_toString(AppName));
|
|
|
|
connect(icon, &QSystemTrayIcon::activated, this, [=](
|
|
|
|
QSystemTrayIcon::ActivationReason reason) {
|
|
|
|
Core::Sandbox::Instance().customEnterFromEventLoop([&] {
|
|
|
|
handleTrayIconActication(reason);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
App::wnd()->updateTrayMenu();
|
|
|
|
}
|
|
|
|
|
2016-11-04 11:14:47 +00:00
|
|
|
void MainWindow::resizeEvent(QResizeEvent *e) {
|
2016-11-08 14:07:25 +00:00
|
|
|
updateControlsGeometry();
|
|
|
|
}
|
|
|
|
|
2017-11-21 10:27:37 +00:00
|
|
|
rpl::producer<> MainWindow::leaveEvents() const {
|
|
|
|
return _leaveEvents.events();
|
|
|
|
}
|
|
|
|
|
2018-06-03 13:30:40 +00:00
|
|
|
void MainWindow::leaveEventHook(QEvent *e) {
|
2017-11-21 10:27:37 +00:00
|
|
|
_leaveEvents.fire({});
|
|
|
|
}
|
|
|
|
|
2016-11-08 14:07:25 +00:00
|
|
|
void MainWindow::updateControlsGeometry() {
|
2016-11-04 11:14:47 +00:00
|
|
|
auto bodyTop = 0;
|
2017-02-03 20:07:26 +00:00
|
|
|
auto bodyWidth = width();
|
2016-11-08 14:07:25 +00:00
|
|
|
if (_title && !_title->isHidden()) {
|
|
|
|
_title->setGeometry(0, bodyTop, width(), _title->height());
|
2016-11-04 11:14:47 +00:00
|
|
|
bodyTop += _title->height();
|
|
|
|
}
|
2019-06-03 22:34:34 +00:00
|
|
|
if (_outdated) {
|
|
|
|
Ui::SendPendingMoveResizeEvents(_outdated.data());
|
|
|
|
_outdated->resizeToWidth(width());
|
|
|
|
_outdated->moveToLeft(0, bodyTop);
|
|
|
|
bodyTop += _outdated->height();
|
|
|
|
}
|
2017-02-03 20:07:26 +00:00
|
|
|
if (_rightColumn) {
|
|
|
|
bodyWidth -= _rightColumn->width();
|
|
|
|
_rightColumn->setGeometry(bodyWidth, bodyTop, width() - bodyWidth, height() - bodyTop);
|
|
|
|
}
|
|
|
|
_body->setGeometry(0, bodyTop, bodyWidth, height() - bodyTop);
|
2016-11-04 11:14:47 +00:00
|
|
|
}
|
|
|
|
|
2016-11-09 08:34:38 +00:00
|
|
|
void MainWindow::updateUnreadCounter() {
|
|
|
|
if (!Global::started() || App::quitting()) return;
|
|
|
|
|
2019-06-06 11:59:00 +00:00
|
|
|
const auto counter = account().sessionExists()
|
|
|
|
? account().session().data().unreadBadge()
|
2018-12-04 10:32:06 +00:00
|
|
|
: 0;
|
2016-11-09 08:34:38 +00:00
|
|
|
_titleText = (counter > 0) ? qsl("Telegram (%1)").arg(counter) : qsl("Telegram");
|
|
|
|
|
|
|
|
unreadCounterChangedHook();
|
|
|
|
}
|
|
|
|
|
2016-11-04 11:14:47 +00:00
|
|
|
void MainWindow::savePosition(Qt::WindowState state) {
|
|
|
|
if (state == Qt::WindowActive) state = windowHandle()->windowState();
|
|
|
|
if (state == Qt::WindowMinimized || !positionInited()) return;
|
|
|
|
|
2017-07-03 12:23:41 +00:00
|
|
|
auto savedPosition = cWindowPos();
|
|
|
|
auto realPosition = savedPosition;
|
2016-11-04 11:14:47 +00:00
|
|
|
|
|
|
|
if (state == Qt::WindowMaximized) {
|
2017-07-03 12:23:41 +00:00
|
|
|
realPosition.maximized = 1;
|
2016-11-04 11:14:47 +00:00
|
|
|
} else {
|
2016-11-04 19:50:35 +00:00
|
|
|
auto r = geometry();
|
2017-07-03 12:23:41 +00:00
|
|
|
realPosition.x = r.x();
|
|
|
|
realPosition.y = r.y();
|
|
|
|
realPosition.w = r.width() - (_rightColumn ? _rightColumn->width() : 0);
|
|
|
|
realPosition.h = r.height();
|
|
|
|
realPosition.maximized = 0;
|
|
|
|
realPosition.moncrc = 0;
|
2016-11-04 11:14:47 +00:00
|
|
|
}
|
2017-07-03 12:23:41 +00:00
|
|
|
DEBUG_LOG(("Window Pos: Saving position: %1, %2, %3, %4 (maximized %5)").arg(realPosition.x).arg(realPosition.y).arg(realPosition.w).arg(realPosition.h).arg(Logs::b(realPosition.maximized)));
|
2016-11-04 11:14:47 +00:00
|
|
|
|
2017-07-03 12:23:41 +00:00
|
|
|
auto centerX = realPosition.x + realPosition.w / 2;
|
|
|
|
auto centerY = realPosition.y + realPosition.h / 2;
|
2016-11-04 11:14:47 +00:00
|
|
|
int minDelta = 0;
|
2017-07-03 12:23:41 +00:00
|
|
|
QScreen *chosen = nullptr;
|
2016-11-04 11:14:47 +00:00
|
|
|
auto screens = QGuiApplication::screens();
|
|
|
|
for (auto screen : QGuiApplication::screens()) {
|
2017-07-03 12:23:41 +00:00
|
|
|
auto delta = (screen->geometry().center() - QPoint(centerX, centerY)).manhattanLength();
|
2016-11-04 11:14:47 +00:00
|
|
|
if (!chosen || delta < minDelta) {
|
|
|
|
minDelta = delta;
|
|
|
|
chosen = screen;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (chosen) {
|
2017-07-03 12:23:41 +00:00
|
|
|
auto screenGeometry = chosen->geometry();
|
|
|
|
DEBUG_LOG(("Window Pos: Screen found, geometry: %1, %2, %3, %4").arg(screenGeometry.x()).arg(screenGeometry.y()).arg(screenGeometry.width()).arg(screenGeometry.height()));
|
|
|
|
realPosition.x -= screenGeometry.x();
|
|
|
|
realPosition.y -= screenGeometry.y();
|
|
|
|
realPosition.moncrc = screenNameChecksum(chosen->name());
|
2016-11-04 11:14:47 +00:00
|
|
|
}
|
|
|
|
|
2017-07-03 12:23:41 +00:00
|
|
|
if (realPosition.w >= st::windowMinWidth && realPosition.h >= st::windowMinHeight) {
|
|
|
|
if (realPosition.x != savedPosition.x
|
|
|
|
|| realPosition.y != savedPosition.y
|
|
|
|
|| realPosition.w != savedPosition.w
|
|
|
|
|| realPosition.h != savedPosition.h
|
|
|
|
|| realPosition.moncrc != savedPosition.moncrc
|
|
|
|
|| realPosition.maximized != savedPosition.maximized) {
|
|
|
|
DEBUG_LOG(("Window Pos: Writing: %1, %2, %3, %4 (maximized %5)").arg(realPosition.x).arg(realPosition.y).arg(realPosition.w).arg(realPosition.h).arg(Logs::b(realPosition.maximized)));
|
|
|
|
cSetWindowPos(realPosition);
|
2016-11-04 11:14:47 +00:00
|
|
|
Local::writeSettings();
|
|
|
|
}
|
|
|
|
}
|
2016-06-16 12:59:54 +00:00
|
|
|
}
|
|
|
|
|
2016-12-31 15:19:22 +00:00
|
|
|
bool MainWindow::minimizeToTray() {
|
2017-01-01 16:45:20 +00:00
|
|
|
if (App::quitting() || !hasTrayIcon()) return false;
|
2016-12-31 15:19:22 +00:00
|
|
|
|
|
|
|
closeWithoutDestroy();
|
|
|
|
updateIsActive(Global::OfflineBlurTimeout());
|
|
|
|
updateTrayMenu();
|
|
|
|
updateGlobalMenu();
|
2017-01-01 16:45:20 +00:00
|
|
|
showTrayTooltip();
|
2016-12-31 15:19:22 +00:00
|
|
|
return true;
|
2016-06-16 12:59:54 +00:00
|
|
|
}
|
|
|
|
|
2018-06-05 18:14:38 +00:00
|
|
|
void MainWindow::reActivateWindow() {
|
|
|
|
#if defined Q_OS_LINUX32 || defined Q_OS_LINUX64
|
|
|
|
const auto reActivate = [=] {
|
|
|
|
if (const auto w = App::wnd()) {
|
|
|
|
if (auto f = QApplication::focusWidget()) {
|
|
|
|
f->clearFocus();
|
|
|
|
}
|
|
|
|
w->activate();
|
|
|
|
if (auto f = QApplication::focusWidget()) {
|
|
|
|
f->clearFocus();
|
|
|
|
}
|
|
|
|
w->setInnerFocus();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
crl::on_main(this, reActivate);
|
|
|
|
App::CallDelayed(200, this, reActivate);
|
|
|
|
#endif // Q_OS_LINUX32 || Q_OS_LINUX64
|
|
|
|
}
|
|
|
|
|
2017-02-03 20:07:26 +00:00
|
|
|
void MainWindow::showRightColumn(object_ptr<TWidget> widget) {
|
2019-09-05 05:18:21 +00:00
|
|
|
const auto wasWidth = width();
|
|
|
|
const auto wasRightWidth = _rightColumn ? _rightColumn->width() : 0;
|
2017-02-21 13:45:56 +00:00
|
|
|
_rightColumn = std::move(widget);
|
2017-02-03 20:07:26 +00:00
|
|
|
if (_rightColumn) {
|
|
|
|
_rightColumn->setParent(this);
|
|
|
|
_rightColumn->show();
|
|
|
|
_rightColumn->setFocus();
|
|
|
|
} else if (App::wnd()) {
|
|
|
|
App::wnd()->setInnerFocus();
|
|
|
|
}
|
2019-09-05 05:18:21 +00:00
|
|
|
const auto nowRightWidth = _rightColumn ? _rightColumn->width() : 0;
|
|
|
|
const auto wasMaximized = isMaximized();
|
2019-09-09 15:37:04 +00:00
|
|
|
const auto wasMinimumWidth = minimumWidth();
|
|
|
|
const auto nowMinimumWidth = st::windowMinWidth + nowRightWidth;
|
|
|
|
const auto firstResize = (nowMinimumWidth < wasMinimumWidth);
|
|
|
|
if (firstResize) {
|
|
|
|
setMinimumWidth(nowMinimumWidth);
|
|
|
|
}
|
2017-02-03 20:07:26 +00:00
|
|
|
if (!isMaximized()) {
|
2017-04-09 18:06:06 +00:00
|
|
|
tryToExtendWidthBy(wasWidth + nowRightWidth - wasRightWidth - width());
|
|
|
|
} else {
|
|
|
|
updateControlsGeometry();
|
|
|
|
}
|
2019-09-09 15:37:04 +00:00
|
|
|
if (!firstResize) {
|
|
|
|
setMinimumWidth(nowMinimumWidth);
|
|
|
|
}
|
2017-04-09 18:06:06 +00:00
|
|
|
}
|
|
|
|
|
2017-11-16 16:43:52 +00:00
|
|
|
int MainWindow::maximalExtendBy() const {
|
2017-04-09 18:06:06 +00:00
|
|
|
auto desktop = QDesktopWidget().availableGeometry(this);
|
2017-11-16 16:43:52 +00:00
|
|
|
return std::max(desktop.width() - geometry().width(), 0);
|
2017-04-09 18:06:06 +00:00
|
|
|
}
|
|
|
|
|
2017-11-16 16:43:52 +00:00
|
|
|
bool MainWindow::canExtendNoMove(int extendBy) const {
|
2017-04-09 18:06:06 +00:00
|
|
|
auto desktop = QDesktopWidget().availableGeometry(this);
|
2017-11-16 16:43:52 +00:00
|
|
|
auto inner = geometry();
|
|
|
|
auto innerRight = (inner.x() + inner.width() + extendBy);
|
|
|
|
auto desktopRight = (desktop.x() + desktop.width());
|
|
|
|
return innerRight <= desktopRight;
|
|
|
|
}
|
|
|
|
|
|
|
|
int MainWindow::tryToExtendWidthBy(int addToWidth) {
|
|
|
|
auto desktop = QDesktopWidget().availableGeometry(this);
|
|
|
|
auto inner = geometry();
|
|
|
|
accumulate_min(
|
|
|
|
addToWidth,
|
|
|
|
std::max(desktop.width() - inner.width(), 0));
|
|
|
|
auto newWidth = inner.width() + addToWidth;
|
|
|
|
auto newLeft = std::min(
|
|
|
|
inner.x(),
|
|
|
|
desktop.x() + desktop.width() - newWidth);
|
|
|
|
if (inner.x() != newLeft || inner.width() != newWidth) {
|
|
|
|
setGeometry(newLeft, inner.y(), newWidth, inner.height());
|
2017-02-03 20:07:26 +00:00
|
|
|
} else {
|
|
|
|
updateControlsGeometry();
|
|
|
|
}
|
2017-11-16 16:43:52 +00:00
|
|
|
return addToWidth;
|
2017-02-03 20:07:26 +00:00
|
|
|
}
|
|
|
|
|
2017-06-21 21:38:31 +00:00
|
|
|
void MainWindow::launchDrag(std::unique_ptr<QMimeData> data) {
|
|
|
|
auto weak = QPointer<MainWindow>(this);
|
|
|
|
auto drag = std::make_unique<QDrag>(App::wnd());
|
|
|
|
drag->setMimeData(data.release());
|
|
|
|
drag->exec(Qt::CopyAction);
|
|
|
|
|
|
|
|
// We don't receive mouseReleaseEvent when drag is finished.
|
|
|
|
ClickHandler::unpressed();
|
|
|
|
if (weak) {
|
|
|
|
weak->dragFinished().notify();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-16 12:44:20 +00:00
|
|
|
MainWindow::~MainWindow() = default;
|
2016-12-31 15:19:22 +00:00
|
|
|
|
2016-06-16 12:59:54 +00:00
|
|
|
} // namespace Window
|