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"
|
2021-01-10 03:52:18 +00:00
|
|
|
#include "platform/platform_specific.h"
|
2021-07-25 21:30:56 +00:00
|
|
|
#include "ui/platform/ui_platform_window.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"
|
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-06 11:20:21 +00:00
|
|
|
#include "window/window_controller.h"
|
2019-07-24 11:45:24 +00:00
|
|
|
#include "main/main_account.h" // Account::sessionValue.
|
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"
|
2020-06-18 12:47:09 +00:00
|
|
|
#include "main/main_session_settings.h"
|
2019-09-16 11:14:06 +00:00
|
|
|
#include "base/crc32hash.h"
|
2019-09-20 08:06:00 +00:00
|
|
|
#include "ui/toast/toast.h"
|
2020-07-07 13:54:39 +00:00
|
|
|
#include "ui/widgets/shadow.h"
|
2022-02-22 12:50:19 +00:00
|
|
|
#include "ui/controls/window_outdated_bar.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"
|
2021-01-22 12:16:18 +00:00
|
|
|
#include "mainwidget.h" // session->content()->windowShown().
|
2019-09-13 06:06:02 +00:00
|
|
|
#include "facades.h"
|
2021-02-03 10:43:31 +00:00
|
|
|
#include "styles/style_widgets.h"
|
2018-06-03 13:30:40 +00:00
|
|
|
#include "styles/style_window.h"
|
2016-11-04 11:14:47 +00:00
|
|
|
|
2019-09-04 07:19:15 +00:00
|
|
|
#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
|
|
|
|
|
2021-11-02 13:10:40 +00:00
|
|
|
const QImage &Logo() {
|
|
|
|
static const auto result = QImage(u":/gui/art/logo_256.png"_q);
|
|
|
|
return result;
|
2017-05-12 15:27:19 +00:00
|
|
|
}
|
|
|
|
|
2021-11-02 13:10:40 +00:00
|
|
|
const QImage &LogoNoMargin() {
|
|
|
|
static const auto result = QImage(u":/gui/art/logo_256_no_margin.png"_q);
|
|
|
|
return result;
|
2017-05-12 15:27:19 +00:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-22 14:04:05 +00:00
|
|
|
QIcon CreateOfficialIcon(Main::Session *session) {
|
2022-01-22 13:11:12 +00:00
|
|
|
const auto support = (session && session->supportMode());
|
2022-01-22 14:04:05 +00:00
|
|
|
if (!support) {
|
2022-01-22 13:11:12 +00:00
|
|
|
return QIcon();
|
|
|
|
}
|
2021-11-02 13:10:40 +00:00
|
|
|
auto image = Logo();
|
2022-01-22 14:04:05 +00:00
|
|
|
ConvertIconToBlack(image);
|
2021-05-07 14:33:53 +00:00
|
|
|
return QIcon(Ui::PixmapFromImage(std::move(image)));
|
2017-05-12 15:27:19 +00:00
|
|
|
}
|
|
|
|
|
2022-01-22 13:11:12 +00:00
|
|
|
QIcon CreateIcon(Main::Session *session, bool returnNullIfDefault) {
|
2022-01-22 14:04:05 +00:00
|
|
|
const auto officialIcon = CreateOfficialIcon(session);
|
|
|
|
if (!officialIcon.isNull() || returnNullIfDefault) {
|
|
|
|
return officialIcon;
|
|
|
|
}
|
|
|
|
|
2022-01-22 20:26:10 +00:00
|
|
|
auto result = QIcon(Ui::PixmapFromImage(base::duplicate(Logo())));
|
2022-01-22 14:04:05 +00:00
|
|
|
|
2020-06-13 01:03:23 +00:00
|
|
|
#if defined Q_OS_UNIX && !defined Q_OS_MAC
|
2021-03-26 21:37:25 +00:00
|
|
|
const auto iconFromTheme = QIcon::fromTheme(
|
|
|
|
Platform::GetIconName(),
|
|
|
|
result);
|
|
|
|
|
|
|
|
result = QIcon();
|
|
|
|
|
|
|
|
static const auto iconSizes = {
|
|
|
|
16,
|
|
|
|
22,
|
|
|
|
32,
|
|
|
|
48,
|
|
|
|
64,
|
|
|
|
128,
|
|
|
|
256,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Qt's standard QIconLoaderEngine sets availableSizes
|
|
|
|
// to XDG directories sizes, since svg icons are scalable,
|
|
|
|
// they could be only in one XDG folder (like 48x48)
|
|
|
|
// and Qt will set only a 48px icon to the window
|
|
|
|
// even though the icon could be scaled to other sizes.
|
|
|
|
// Thus, scale it manually to the most widespread sizes.
|
|
|
|
for (const auto iconSize : iconSizes) {
|
|
|
|
// We can't use QIcon::actualSize here
|
|
|
|
// since it works incorrectly with svg icon themes
|
|
|
|
const auto iconPixmap = iconFromTheme.pixmap(iconSize);
|
|
|
|
|
|
|
|
const auto iconPixmapSize = iconPixmap.size()
|
|
|
|
/ iconPixmap.devicePixelRatio();
|
|
|
|
|
|
|
|
// Not a svg icon, don't scale it
|
|
|
|
if (iconPixmapSize.width() != iconSize) {
|
|
|
|
return iconFromTheme;
|
|
|
|
}
|
|
|
|
|
|
|
|
result.addPixmap(iconPixmap);
|
|
|
|
}
|
2020-02-26 11:38:31 +00:00
|
|
|
#endif
|
2022-01-22 14:04:05 +00:00
|
|
|
|
2017-05-12 15:27:19 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-11-02 13:10:40 +00:00
|
|
|
QImage GenerateCounterLayer(CounterLayerArgs &&args) {
|
|
|
|
// platform/linux/main_window_linux depends on count used the same
|
|
|
|
// way for all the same (count % 1000) values.
|
|
|
|
const auto count = args.count.value();
|
|
|
|
const auto text = (count < 1000)
|
|
|
|
? QString::number(count)
|
|
|
|
: u"..%1"_q.arg(count % 100, 2, 10, QChar('0'));
|
|
|
|
const auto textSize = text.size();
|
|
|
|
|
|
|
|
struct Dimensions {
|
|
|
|
int size = 0;
|
|
|
|
int font = 0;
|
|
|
|
int delta = 0;
|
|
|
|
int radius = 0;
|
|
|
|
};
|
|
|
|
const auto d = [&]() -> Dimensions {
|
|
|
|
switch (args.size.value()) {
|
|
|
|
case 16:
|
|
|
|
return {
|
|
|
|
.size = 16,
|
|
|
|
.font = ((textSize < 2) ? 11 : (textSize < 3) ? 11 : 8),
|
|
|
|
.delta = ((textSize < 2) ? 5 : (textSize < 3) ? 2 : 1),
|
|
|
|
.radius = ((textSize < 2) ? 8 : (textSize < 3) ? 7 : 3),
|
|
|
|
};
|
|
|
|
case 20:
|
|
|
|
return {
|
|
|
|
.size = 20,
|
|
|
|
.font = ((textSize < 2) ? 14 : (textSize < 3) ? 13 : 10),
|
|
|
|
.delta = ((textSize < 2) ? 6 : (textSize < 3) ? 2 : 1),
|
|
|
|
.radius = ((textSize < 2) ? 10 : (textSize < 3) ? 9 : 5),
|
|
|
|
};
|
|
|
|
case 24:
|
|
|
|
return {
|
|
|
|
.size = 24,
|
|
|
|
.font = ((textSize < 2) ? 17 : (textSize < 3) ? 16 : 12),
|
|
|
|
.delta = ((textSize < 2) ? 7 : (textSize < 3) ? 3 : 1),
|
|
|
|
.radius = ((textSize < 2) ? 12 : (textSize < 3) ? 11 : 6),
|
|
|
|
};
|
|
|
|
default:
|
|
|
|
return {
|
|
|
|
.size = 32,
|
|
|
|
.font = ((textSize < 2) ? 22 : (textSize < 3) ? 20 : 16),
|
|
|
|
.delta = ((textSize < 2) ? 9 : (textSize < 3) ? 4 : 2),
|
|
|
|
.radius = ((textSize < 2) ? 16 : (textSize < 3) ? 14 : 8),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}();
|
|
|
|
|
|
|
|
auto result = QImage(d.size, d.size, QImage::Format_ARGB32);
|
|
|
|
result.fill(Qt::transparent);
|
|
|
|
|
|
|
|
auto p = QPainter(&result);
|
|
|
|
auto hq = PainterHighQualityEnabler(p);
|
|
|
|
const auto f = style::font{ d.font, 0, 0 };
|
|
|
|
const auto w = f->width(text);
|
|
|
|
|
|
|
|
p.setBrush(args.bg.value());
|
|
|
|
p.setPen(Qt::NoPen);
|
|
|
|
p.drawRoundedRect(
|
|
|
|
QRect(
|
|
|
|
d.size - w - d.delta * 2,
|
|
|
|
d.size - f->height,
|
|
|
|
w + d.delta * 2,
|
|
|
|
f->height),
|
|
|
|
d.radius,
|
|
|
|
d.radius);
|
|
|
|
|
|
|
|
p.setFont(f);
|
|
|
|
p.setPen(args.fg.value());
|
|
|
|
p.drawText(d.size - w - d.delta, d.size - f->height + f->ascent, text);
|
|
|
|
p.end();
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
QImage WithSmallCounter(QImage image, CounterLayerArgs &&args) {
|
|
|
|
const auto count = args.count.value();
|
|
|
|
const auto text = (count < 100)
|
|
|
|
? QString::number(count)
|
|
|
|
: QString("..%1").arg(count % 10, 1, 10, QChar('0'));
|
|
|
|
const auto textSize = text.size();
|
|
|
|
|
|
|
|
struct Dimensions {
|
|
|
|
int size = 0;
|
|
|
|
int font = 0;
|
|
|
|
int delta = 0;
|
|
|
|
int radius = 0;
|
|
|
|
};
|
|
|
|
const auto d = [&]() -> Dimensions {
|
|
|
|
switch (args.size.value()) {
|
|
|
|
case 16:
|
|
|
|
return {
|
|
|
|
.size = 16,
|
|
|
|
.font = 8,
|
|
|
|
.delta = ((textSize < 2) ? 2 : 1),
|
|
|
|
.radius = ((textSize < 2) ? 4 : 3),
|
|
|
|
};
|
|
|
|
case 32:
|
|
|
|
return {
|
|
|
|
.size = 32,
|
|
|
|
.font = 12,
|
|
|
|
.delta = ((textSize < 2) ? 5 : 2),
|
|
|
|
.radius = ((textSize < 2) ? 8 : 7),
|
|
|
|
};
|
|
|
|
default:
|
|
|
|
return {
|
|
|
|
.size = 64,
|
|
|
|
.font = 22,
|
|
|
|
.delta = ((textSize < 2) ? 9 : 4),
|
|
|
|
.radius = ((textSize < 2) ? 16 : 14),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}();
|
|
|
|
|
|
|
|
auto p = QPainter(&image);
|
|
|
|
auto hq = PainterHighQualityEnabler(p);
|
|
|
|
const auto f = style::font{ d.font, 0, 0 };
|
|
|
|
const auto w = f->width(text);
|
|
|
|
|
|
|
|
p.setBrush(args.bg.value());
|
|
|
|
p.setPen(Qt::NoPen);
|
|
|
|
p.drawRoundedRect(
|
|
|
|
QRect(
|
|
|
|
d.size - w - d.delta * 2,
|
|
|
|
d.size - f->height,
|
|
|
|
w + d.delta * 2,
|
|
|
|
f->height),
|
|
|
|
d.radius,
|
|
|
|
d.radius);
|
|
|
|
|
|
|
|
p.setFont(f);
|
|
|
|
p.setPen(args.fg.value());
|
|
|
|
p.drawText(d.size - w - d.delta, d.size - f->height + f->ascent, text);
|
|
|
|
p.end();
|
|
|
|
|
|
|
|
return image;
|
|
|
|
}
|
|
|
|
|
2019-06-06 11:20:21 +00:00
|
|
|
MainWindow::MainWindow(not_null<Controller*> controller)
|
|
|
|
: _controller(controller)
|
|
|
|
, _positionUpdatedTimer([=] { savePosition(); })
|
2022-02-22 12:50:19 +00:00
|
|
|
, _outdated(Ui::CreateOutdatedBar(body(), cWorkingDir()))
|
2021-07-25 21:30:56 +00:00
|
|
|
, _body(body()) {
|
2021-07-15 11:12:38 +00:00
|
|
|
style::PaletteChanged(
|
|
|
|
) | rpl::start_with_next([=] {
|
|
|
|
updatePalette();
|
|
|
|
}, lifetime());
|
2020-06-18 11:17:58 +00:00
|
|
|
|
|
|
|
Core::App().unreadBadgeChanges(
|
|
|
|
) | rpl::start_with_next([=] {
|
2018-10-07 11:45:46 +00:00
|
|
|
updateUnreadCounter();
|
2020-06-18 11:17:58 +00:00
|
|
|
}, lifetime());
|
|
|
|
|
2021-05-27 19:45:52 +00:00
|
|
|
Core::App().settings().workModeChanges(
|
2021-05-27 22:11:16 +00:00
|
|
|
) | rpl::start_with_next([=](Core::Settings::WorkMode mode) {
|
2018-10-07 11:45:46 +00:00
|
|
|
workmodeUpdated(mode);
|
2021-05-27 19:45:52 +00:00
|
|
|
}, lifetime());
|
2019-06-06 09:37:12 +00:00
|
|
|
|
2022-02-20 11:25:44 +00:00
|
|
|
if (isPrimary()) {
|
|
|
|
Ui::Toast::SetDefaultParent(_body.data());
|
|
|
|
}
|
2019-09-20 08:06:00 +00:00
|
|
|
|
2021-11-10 08:53:29 +00:00
|
|
|
body()->sizeValue(
|
|
|
|
) | rpl::start_with_next([=](QSize size) {
|
|
|
|
updateControlsGeometry();
|
|
|
|
}, 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());
|
|
|
|
}
|
2016-11-04 11:14:47 +00:00
|
|
|
}
|
|
|
|
|
2019-06-06 11:59:00 +00:00
|
|
|
Main::Account &MainWindow::account() const {
|
|
|
|
return _controller->account();
|
|
|
|
}
|
|
|
|
|
2022-01-04 12:29:40 +00:00
|
|
|
PeerData *MainWindow::singlePeer() const {
|
|
|
|
return _controller->singlePeer();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MainWindow::isPrimary() const {
|
|
|
|
return _controller->isPrimary();
|
|
|
|
}
|
|
|
|
|
2019-06-06 11:20:21 +00:00
|
|
|
Window::SessionController *MainWindow::sessionController() const {
|
|
|
|
return _controller->sessionController();
|
|
|
|
}
|
|
|
|
|
2016-12-31 15:19:22 +00:00
|
|
|
bool MainWindow::hideNoQuit() {
|
2022-01-27 12:48:51 +00:00
|
|
|
if (Core::Quitting()) {
|
2017-08-08 09:56:10 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-05-27 19:45:52 +00:00
|
|
|
const auto workMode = Core::App().settings().workMode();
|
2021-05-27 22:11:16 +00:00
|
|
|
if (workMode == Core::Settings::WorkMode::TrayOnly
|
|
|
|
|| workMode == Core::Settings::WorkMode::WindowAndTray) {
|
2016-12-31 15:19:22 +00:00
|
|
|
if (minimizeToTray()) {
|
2020-06-18 18:04:16 +00:00
|
|
|
if (const auto controller = sessionController()) {
|
|
|
|
Ui::showChatsList(&controller->session());
|
|
|
|
}
|
2016-12-31 15:19:22 +00:00
|
|
|
return true;
|
|
|
|
}
|
2021-09-12 23:13:13 +00:00
|
|
|
}
|
|
|
|
if (Platform::IsMac() || Core::App().settings().closeToTaskbar()) {
|
|
|
|
if (Platform::IsMac()) {
|
|
|
|
closeWithoutDestroy();
|
|
|
|
} else {
|
|
|
|
setWindowState(window()->windowState() | Qt::WindowMinimized);
|
|
|
|
}
|
2020-06-17 09:36:25 +00:00
|
|
|
controller().updateIsActiveBlur();
|
2016-12-31 15:19:22 +00:00
|
|
|
updateGlobalMenu();
|
2020-06-18 18:04:16 +00:00
|
|
|
if (const auto controller = sessionController()) {
|
|
|
|
Ui::showChatsList(&controller->session());
|
|
|
|
}
|
2016-12-31 15:19:22 +00:00
|
|
|
return true;
|
|
|
|
}
|
2017-01-01 16:45:20 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MainWindow::clearWidgets() {
|
|
|
|
clearWidgetsHook();
|
|
|
|
updateGlobalMenu();
|
|
|
|
}
|
|
|
|
|
2020-06-17 09:36:25 +00:00
|
|
|
void MainWindow::updateIsActive() {
|
2021-06-18 15:22:36 +00:00
|
|
|
const auto isActive = computeIsActive();
|
|
|
|
if (_isActive != isActive) {
|
|
|
|
_isActive = isActive;
|
|
|
|
activeChangedHook();
|
|
|
|
}
|
2017-01-01 16:45:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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() {
|
2020-06-15 16:25:02 +00:00
|
|
|
const auto session = sessionController()
|
|
|
|
? &sessionController()->session()
|
|
|
|
: nullptr;
|
|
|
|
const auto supportIcon = session && session->supportMode();
|
2019-06-06 11:59:00 +00:00
|
|
|
if (supportIcon != _usingSupportIcon || _icon.isNull()) {
|
2020-06-15 16:25:02 +00:00
|
|
|
_icon = CreateIcon(session);
|
2018-10-07 11:45:46 +00:00
|
|
|
_usingSupportIcon = supportIcon;
|
|
|
|
}
|
2017-05-12 15:27:19 +00:00
|
|
|
setWindowIcon(_icon);
|
|
|
|
}
|
|
|
|
|
2021-02-25 15:12:51 +00:00
|
|
|
QRect MainWindow::desktopRect() const {
|
|
|
|
const auto now = crl::now();
|
2021-03-13 10:37:58 +00:00
|
|
|
if (!_monitorLastGot || now >= _monitorLastGot + crl::time(1000)) {
|
2021-02-25 15:12:51 +00:00
|
|
|
_monitorLastGot = now;
|
|
|
|
_monitorRect = computeDesktopRect();
|
|
|
|
}
|
|
|
|
return _monitorRect;
|
|
|
|
}
|
|
|
|
|
2016-11-04 11:14:47 +00:00
|
|
|
void MainWindow::init() {
|
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); });
|
2020-09-18 09:09:05 +00:00
|
|
|
connect(
|
|
|
|
windowHandle(),
|
|
|
|
&QWindow::visibleChanged,
|
|
|
|
this,
|
|
|
|
[=](bool visible) { handleVisibleChanged(visible); });
|
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
|
|
|
|
2021-07-25 21:30:56 +00:00
|
|
|
if (Ui::Platform::NativeWindowFrameSupported()) {
|
2020-07-07 13:54:39 +00:00
|
|
|
Core::App().settings().nativeWindowFrameChanges(
|
|
|
|
) | rpl::start_with_next([=](bool native) {
|
|
|
|
refreshTitleWidget();
|
|
|
|
recountGeometryConstraints();
|
|
|
|
}, lifetime());
|
2016-11-08 14:07:25 +00:00
|
|
|
}
|
2020-07-07 13:54:39 +00:00
|
|
|
refreshTitleWidget();
|
2016-11-04 11:14:47 +00:00
|
|
|
|
2021-08-10 12:41:25 +00:00
|
|
|
initGeometry();
|
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);
|
2020-08-28 15:43:23 +00:00
|
|
|
updateControlsGeometry();
|
2020-06-17 09:36:25 +00:00
|
|
|
if (state == Qt::WindowMinimized) {
|
|
|
|
controller().updateIsActiveBlur();
|
|
|
|
} else {
|
|
|
|
controller().updateIsActiveFocus();
|
|
|
|
}
|
2019-03-10 13:40:44 +00:00
|
|
|
Core::App().updateNonIdle();
|
2021-05-27 22:11:16 +00:00
|
|
|
using WorkMode = Core::Settings::WorkMode;
|
2021-05-27 19:45:52 +00:00
|
|
|
if (state == Qt::WindowMinimized
|
2021-05-27 22:11:16 +00:00
|
|
|
&& (Core::App().settings().workMode() == WorkMode::TrayOnly)) {
|
2017-05-19 14:02:55 +00:00
|
|
|
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
|
|
|
}
|
2021-09-03 13:04:31 +00:00
|
|
|
InvokeQueued(this, [=] {
|
2017-11-15 12:46:34 +00:00
|
|
|
handleActiveChangedHook();
|
2017-05-19 14:02:55 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-09-18 09:09:05 +00:00
|
|
|
void MainWindow::handleVisibleChanged(bool visible) {
|
|
|
|
if (visible) {
|
|
|
|
if (_maximizedBeforeHide) {
|
|
|
|
DEBUG_LOG(("Window Pos: Window was maximized before hidding, setting maximized."));
|
|
|
|
setWindowState(Qt::WindowMaximized);
|
|
|
|
}
|
|
|
|
} else {
|
2021-02-17 15:20:05 +00:00
|
|
|
_maximizedBeforeHide = Core::App().settings().windowPosition().maximized;
|
2020-09-18 09:09:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handleVisibleChangedHook(visible);
|
|
|
|
}
|
|
|
|
|
2021-01-22 12:16:18 +00:00
|
|
|
void MainWindow::showFromTray() {
|
2021-09-03 13:04:31 +00:00
|
|
|
InvokeQueued(this, [=] {
|
2021-01-22 12:16:18 +00:00
|
|
|
updateGlobalMenu();
|
|
|
|
});
|
|
|
|
activate();
|
|
|
|
updateUnreadCounter();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MainWindow::quitFromTray() {
|
2022-01-27 12:48:51 +00:00
|
|
|
Core::Quit();
|
2021-01-22 12:16:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MainWindow::activate() {
|
|
|
|
bool wasHidden = !isVisible();
|
|
|
|
setWindowState(windowState() & ~Qt::WindowMinimized);
|
|
|
|
setVisible(true);
|
|
|
|
psActivateProcess();
|
2021-03-01 07:47:27 +00:00
|
|
|
raise();
|
|
|
|
activateWindow();
|
2021-01-22 12:16:18 +00:00
|
|
|
controller().updateIsActiveFocus();
|
|
|
|
if (wasHidden) {
|
|
|
|
if (const auto session = sessionController()) {
|
|
|
|
session->content()->windowShown();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-03-03 12:07:22 +00:00
|
|
|
int MainWindow::computeMinWidth() const {
|
|
|
|
auto result = st::windowMinWidth;
|
|
|
|
if (const auto session = _controller->sessionController()) {
|
|
|
|
if (const auto add = session->filtersWidth()) {
|
|
|
|
result += add;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (_rightColumn) {
|
|
|
|
result += _rightColumn->width();
|
|
|
|
}
|
2021-07-25 21:30:56 +00:00
|
|
|
return result;
|
2020-03-03 12:07:22 +00:00
|
|
|
}
|
|
|
|
|
2019-06-03 14:41:23 +00:00
|
|
|
int MainWindow::computeMinHeight() const {
|
2019-06-03 22:34:34 +00:00
|
|
|
const auto outdated = [&] {
|
|
|
|
if (!_outdated) {
|
|
|
|
return 0;
|
|
|
|
}
|
2021-07-25 21:30:56 +00:00
|
|
|
_outdated->resizeToWidth(st::windowMinWidth);
|
2019-06-03 22:34:34 +00:00
|
|
|
return _outdated->height();
|
|
|
|
}();
|
2021-07-25 21:30:56 +00:00
|
|
|
return outdated + st::windowMinHeight;
|
2019-06-03 14:41:23 +00:00
|
|
|
}
|
|
|
|
|
2020-07-07 13:54:39 +00:00
|
|
|
void MainWindow::refreshTitleWidget() {
|
2021-07-25 21:30:56 +00:00
|
|
|
if (Ui::Platform::NativeWindowFrameSupported()
|
2020-07-07 13:54:39 +00:00
|
|
|
&& Core::App().settings().nativeWindowFrame()) {
|
2021-07-25 21:30:56 +00:00
|
|
|
setNativeFrame(true);
|
2020-07-07 13:54:39 +00:00
|
|
|
if (Platform::NativeTitleRequiresShadow()) {
|
|
|
|
_titleShadow.create(this);
|
|
|
|
_titleShadow->show();
|
|
|
|
}
|
2021-07-25 21:30:56 +00:00
|
|
|
} else {
|
|
|
|
setNativeFrame(false);
|
2020-07-07 13:54:39 +00:00
|
|
|
_titleShadow.destroy();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MainWindow::updateMinimumSize() {
|
2022-02-22 11:06:29 +00:00
|
|
|
setMinimumSize(QSize(computeMinWidth(), computeMinHeight()));
|
2020-07-07 13:54:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MainWindow::recountGeometryConstraints() {
|
|
|
|
updateMinimumSize();
|
|
|
|
updateControlsGeometry();
|
|
|
|
fixOrder();
|
|
|
|
}
|
|
|
|
|
2021-08-10 12:41:25 +00:00
|
|
|
Core::WindowPosition MainWindow::positionFromSettings() const {
|
2021-02-17 15:20:05 +00:00
|
|
|
auto position = Core::App().settings().windowPosition();
|
|
|
|
DEBUG_LOG(("Window Pos: Initializing first %1, %2, %3, %4 "
|
|
|
|
"(scale %5%, maximized %6)")
|
2021-01-25 23:57:50 +00:00
|
|
|
.arg(position.x)
|
|
|
|
.arg(position.y)
|
|
|
|
.arg(position.w)
|
|
|
|
.arg(position.h)
|
|
|
|
.arg(position.scale)
|
|
|
|
.arg(Logs::b(position.maximized)));
|
|
|
|
|
2021-08-10 12:41:25 +00:00
|
|
|
if (!position.scale) {
|
|
|
|
return position;
|
2021-01-25 23:57:50 +00:00
|
|
|
}
|
2021-08-10 12:41:25 +00:00
|
|
|
const auto scaleFactor = cScale() / float64(position.scale);
|
2021-09-16 10:16:13 +00:00
|
|
|
if (scaleFactor != 1.) {
|
|
|
|
// Change scale while keeping the position center in place.
|
|
|
|
position.x += position.w / 2;
|
|
|
|
position.y += position.h / 2;
|
|
|
|
position.w *= scaleFactor;
|
|
|
|
position.h *= scaleFactor;
|
|
|
|
position.x -= position.w / 2;
|
|
|
|
position.y -= position.h / 2;
|
|
|
|
}
|
2021-08-10 12:41:25 +00:00
|
|
|
return position;
|
|
|
|
}
|
2017-07-03 12:23:41 +00:00
|
|
|
|
2021-08-10 12:41:25 +00:00
|
|
|
QRect MainWindow::countInitialGeometry(Core::WindowPosition position) {
|
2020-02-10 15:28:41 +00:00
|
|
|
const auto primaryScreen = QGuiApplication::primaryScreen();
|
2021-08-10 12:41:25 +00:00
|
|
|
const auto primaryAvailable = primaryScreen
|
2020-02-10 15:28:41 +00:00
|
|
|
? primaryScreen->availableGeometry()
|
|
|
|
: QRect(0, 0, st::windowDefaultWidth, st::windowDefaultHeight);
|
2020-06-25 12:16:09 +00:00
|
|
|
const auto initialWidth = Core::Settings::ThirdColumnByDefault()
|
2020-02-15 18:45:50 +00:00
|
|
|
? st::windowBigDefaultWidth
|
|
|
|
: st::windowDefaultWidth;
|
2020-06-25 12:16:09 +00:00
|
|
|
const auto initialHeight = Core::Settings::ThirdColumnByDefault()
|
2020-02-15 18:45:50 +00:00
|
|
|
? st::windowBigDefaultHeight
|
|
|
|
: st::windowDefaultHeight;
|
2021-08-10 12:41:25 +00:00
|
|
|
const auto initial = QRect(
|
|
|
|
primaryAvailable.x() + std::max(
|
|
|
|
(primaryAvailable.width() - initialWidth) / 2,
|
2018-10-20 19:04:10 +00:00
|
|
|
0),
|
2021-08-10 12:41:25 +00:00
|
|
|
primaryAvailable.y() + std::max(
|
|
|
|
(primaryAvailable.height() - initialHeight) / 2,
|
2018-10-20 19:04:10 +00:00
|
|
|
0),
|
2020-02-15 18:45:50 +00:00
|
|
|
initialWidth,
|
|
|
|
initialHeight);
|
2021-08-10 12:41:25 +00:00
|
|
|
if (!position.w || !position.h) {
|
|
|
|
return initial;
|
|
|
|
}
|
|
|
|
const auto screen = [&]() -> QScreen* {
|
|
|
|
for (const auto screen : QGuiApplication::screens()) {
|
2017-07-03 12:23:41 +00:00
|
|
|
if (position.moncrc == screenNameChecksum(screen->name())) {
|
2021-08-10 12:41:25 +00:00
|
|
|
return screen;
|
2016-11-04 11:14:47 +00:00
|
|
|
}
|
|
|
|
}
|
2021-08-10 12:41:25 +00:00
|
|
|
return nullptr;
|
|
|
|
}();
|
|
|
|
if (!screen) {
|
|
|
|
return initial;
|
|
|
|
}
|
2021-09-16 07:32:16 +00:00
|
|
|
const auto frame = frameMargins();
|
2021-08-10 12:41:25 +00:00
|
|
|
const auto screenGeometry = screen->geometry();
|
|
|
|
const auto availableGeometry = screen->availableGeometry();
|
2021-09-16 07:32:16 +00:00
|
|
|
const auto spaceForInner = availableGeometry.marginsRemoved(frame);
|
2021-08-10 12:41:25 +00:00
|
|
|
DEBUG_LOG(("Window Pos: "
|
2021-09-16 07:32:16 +00:00
|
|
|
"Screen found, screen geometry: %1, %2, %3, %4, "
|
|
|
|
"available: %5, %6, %7, %8"
|
2021-08-10 12:41:25 +00:00
|
|
|
).arg(screenGeometry.x()
|
|
|
|
).arg(screenGeometry.y()
|
|
|
|
).arg(screenGeometry.width()
|
2021-09-16 07:32:16 +00:00
|
|
|
).arg(screenGeometry.height()
|
|
|
|
).arg(availableGeometry.x()
|
|
|
|
).arg(availableGeometry.y()
|
|
|
|
).arg(availableGeometry.width()
|
|
|
|
).arg(availableGeometry.height()));
|
|
|
|
DEBUG_LOG(("Window Pos: "
|
|
|
|
"Window frame margins: %1, %2, %3, %4, "
|
|
|
|
"available space for inner geometry: %5, %6, %7, %8"
|
|
|
|
).arg(frame.left()
|
|
|
|
).arg(frame.top()
|
|
|
|
).arg(frame.right()
|
|
|
|
).arg(frame.bottom()
|
|
|
|
).arg(spaceForInner.x()
|
|
|
|
).arg(spaceForInner.y()
|
|
|
|
).arg(spaceForInner.width()
|
|
|
|
).arg(spaceForInner.height()));
|
2021-08-10 12:41:25 +00:00
|
|
|
|
|
|
|
const auto x = spaceForInner.x() - screenGeometry.x();
|
|
|
|
const auto y = spaceForInner.y() - screenGeometry.y();
|
|
|
|
const auto w = spaceForInner.width();
|
|
|
|
const auto h = spaceForInner.height();
|
|
|
|
if (w < st::windowMinWidth || h < st::windowMinHeight) {
|
|
|
|
return initial;
|
|
|
|
}
|
|
|
|
if (position.x < x) position.x = x;
|
|
|
|
if (position.y < y) position.y = y;
|
|
|
|
if (position.w > w) position.w = w;
|
|
|
|
if (position.h > h) position.h = h;
|
|
|
|
const auto rightPoint = position.x + position.w;
|
|
|
|
const auto screenRightPoint = x + w;
|
|
|
|
if (rightPoint > screenRightPoint) {
|
|
|
|
const auto distance = rightPoint - screenRightPoint;
|
|
|
|
const auto newXPos = position.x - distance;
|
|
|
|
if (newXPos >= x) {
|
|
|
|
position.x = newXPos;
|
|
|
|
} else {
|
|
|
|
position.x = x;
|
|
|
|
const auto newRightPoint = position.x + position.w;
|
|
|
|
const auto newDistance = newRightPoint - screenRightPoint;
|
|
|
|
position.w -= newDistance;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const auto bottomPoint = position.y + position.h;
|
|
|
|
const auto screenBottomPoint = y + h;
|
|
|
|
if (bottomPoint > screenBottomPoint) {
|
|
|
|
const auto distance = bottomPoint - screenBottomPoint;
|
|
|
|
const auto newYPos = position.y - distance;
|
|
|
|
if (newYPos >= y) {
|
|
|
|
position.y = newYPos;
|
|
|
|
} else {
|
|
|
|
position.y = y;
|
|
|
|
const auto newBottomPoint = position.y + position.h;
|
|
|
|
const auto newDistance = newBottomPoint - screenBottomPoint;
|
|
|
|
position.h -= newDistance;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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())) {
|
|
|
|
return initial;
|
|
|
|
}
|
|
|
|
DEBUG_LOG(("Window Pos: Resulting geometry is %1, %2, %3, %4"
|
|
|
|
).arg(position.x
|
|
|
|
).arg(position.y
|
|
|
|
).arg(position.w
|
|
|
|
).arg(position.h));
|
|
|
|
return QRect(position.x, position.y, position.w, position.h);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MainWindow::initGeometry() {
|
|
|
|
updateMinimumSize();
|
|
|
|
if (initGeometryFromSystem()) {
|
|
|
|
return;
|
2016-11-04 11:14:47 +00:00
|
|
|
}
|
2022-01-04 16:36:33 +00:00
|
|
|
// #TODO windows
|
|
|
|
const auto geometry = countInitialGeometry(isPrimary()
|
|
|
|
? positionFromSettings()
|
|
|
|
: Core::WindowPosition());
|
2021-08-10 12:41:25 +00:00
|
|
|
DEBUG_LOG(("Window Pos: Setting first %1, %2, %3, %4"
|
|
|
|
).arg(geometry.x()
|
|
|
|
).arg(geometry.y()
|
|
|
|
).arg(geometry.width()
|
|
|
|
).arg(geometry.height()));
|
2020-02-10 15:28:41 +00:00
|
|
|
setGeometry(geometry);
|
2016-11-04 11:14:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MainWindow::positionUpdated() {
|
2018-06-05 18:14:38 +00:00
|
|
|
_positionUpdatedTimer.callOnce(kSaveWindowPositionTimeout);
|
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) {
|
2020-01-29 09:44:37 +00:00
|
|
|
icon->setToolTip(AppName.utf16());
|
2019-04-12 13:21:01 +00:00
|
|
|
connect(icon, &QSystemTrayIcon::activated, this, [=](
|
|
|
|
QSystemTrayIcon::ActivationReason reason) {
|
|
|
|
Core::Sandbox::Instance().customEnterFromEventLoop([&] {
|
|
|
|
handleTrayIconActication(reason);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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() {
|
2021-07-25 21:30:56 +00:00
|
|
|
const auto inner = body()->rect();
|
2020-08-28 12:07:29 +00:00
|
|
|
auto bodyLeft = inner.x();
|
|
|
|
auto bodyTop = inner.y();
|
|
|
|
auto bodyWidth = inner.width();
|
2020-07-07 13:54:39 +00:00
|
|
|
if (_titleShadow) {
|
2020-08-28 12:07:29 +00:00
|
|
|
_titleShadow->setGeometry(inner.x(), bodyTop, inner.width(), st::lineWidth);
|
2020-07-07 13:54:39 +00:00
|
|
|
}
|
2019-06-03 22:34:34 +00:00
|
|
|
if (_outdated) {
|
|
|
|
Ui::SendPendingMoveResizeEvents(_outdated.data());
|
2020-08-28 12:07:29 +00:00
|
|
|
_outdated->resizeToWidth(inner.width());
|
|
|
|
_outdated->moveToLeft(inner.x(), bodyTop);
|
2019-06-03 22:34:34 +00:00
|
|
|
bodyTop += _outdated->height();
|
|
|
|
}
|
2017-02-03 20:07:26 +00:00
|
|
|
if (_rightColumn) {
|
|
|
|
bodyWidth -= _rightColumn->width();
|
2020-08-28 12:07:29 +00:00
|
|
|
_rightColumn->setGeometry(bodyWidth, bodyTop, inner.width() - bodyWidth, inner.height() - (bodyTop - inner.y()));
|
2017-02-03 20:07:26 +00:00
|
|
|
}
|
2020-08-28 12:07:29 +00:00
|
|
|
_body->setGeometry(bodyLeft, bodyTop, bodyWidth, inner.height() - (bodyTop - inner.y()));
|
2016-11-04 11:14:47 +00:00
|
|
|
}
|
|
|
|
|
2016-11-09 08:34:38 +00:00
|
|
|
void MainWindow::updateUnreadCounter() {
|
2022-01-27 12:48:51 +00:00
|
|
|
if (Core::Quitting()) {
|
2020-06-18 18:04:16 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-11-09 08:34:38 +00:00
|
|
|
|
2020-06-15 16:25:02 +00:00
|
|
|
const auto counter = Core::App().unreadBadge();
|
2021-07-25 21:30:56 +00:00
|
|
|
setTitle((counter > 0) ? qsl("Telegram (%1)").arg(counter) : qsl("Telegram"));
|
2016-11-09 08:34:38 +00:00
|
|
|
|
|
|
|
unreadCounterChangedHook();
|
|
|
|
}
|
|
|
|
|
2021-02-25 15:12:51 +00:00
|
|
|
QRect MainWindow::computeDesktopRect() const {
|
2021-10-19 13:00:21 +00:00
|
|
|
return (screen() ? screen() : QApplication::primaryScreen())->availableGeometry();
|
2021-02-25 15:12:51 +00:00
|
|
|
}
|
|
|
|
|
2016-11-04 11:14:47 +00:00
|
|
|
void MainWindow::savePosition(Qt::WindowState state) {
|
2020-09-18 09:09:05 +00:00
|
|
|
if (state == Qt::WindowActive) {
|
|
|
|
state = windowHandle()->windowState();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state == Qt::WindowMinimized
|
|
|
|
|| !isVisible()
|
2022-01-04 16:36:33 +00:00
|
|
|
|| !isPrimary() // #TODO windows
|
2020-09-18 09:09:05 +00:00
|
|
|
|| !positionInited()) {
|
|
|
|
return;
|
|
|
|
}
|
2016-11-04 11:14:47 +00:00
|
|
|
|
2021-02-17 15:20:05 +00:00
|
|
|
const auto &savedPosition = Core::App().settings().windowPosition();
|
2017-07-03 12:23:41 +00:00
|
|
|
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;
|
2020-02-12 09:03:08 +00:00
|
|
|
DEBUG_LOG(("Window Pos: Saving maximized position."));
|
2016-11-04 11:14:47 +00:00
|
|
|
} else {
|
2021-07-25 21:30:56 +00:00
|
|
|
auto r = body()->mapToGlobal(body()->rect());
|
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();
|
2021-01-25 23:57:50 +00:00
|
|
|
realPosition.scale = cScale();
|
2017-07-03 12:23:41 +00:00
|
|
|
realPosition.maximized = 0;
|
|
|
|
realPosition.moncrc = 0;
|
2020-02-12 09:03:08 +00:00
|
|
|
|
|
|
|
DEBUG_LOG(("Window Pos: Saving non-maximized position: %1, %2, %3, %4").arg(realPosition.x).arg(realPosition.y).arg(realPosition.w).arg(realPosition.h));
|
|
|
|
|
|
|
|
auto centerX = realPosition.x + realPosition.w / 2;
|
|
|
|
auto centerY = realPosition.y + realPosition.h / 2;
|
|
|
|
int minDelta = 0;
|
|
|
|
QScreen *chosen = nullptr;
|
2021-03-13 20:08:07 +00:00
|
|
|
const auto screens = QGuiApplication::screens();
|
|
|
|
for (auto screen : screens) {
|
2020-02-12 09:03:08 +00:00
|
|
|
auto delta = (screen->geometry().center() - QPoint(centerX, centerY)).manhattanLength();
|
|
|
|
if (!chosen || delta < minDelta) {
|
|
|
|
minDelta = delta;
|
|
|
|
chosen = screen;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (chosen) {
|
|
|
|
auto screenGeometry = chosen->geometry();
|
2021-02-17 15:20:05 +00:00
|
|
|
DEBUG_LOG(("Window Pos: Screen found, geometry: %1, %2, %3, %4"
|
|
|
|
).arg(screenGeometry.x()
|
|
|
|
).arg(screenGeometry.y()
|
|
|
|
).arg(screenGeometry.width()
|
|
|
|
).arg(screenGeometry.height()));
|
2020-02-12 09:03:08 +00:00
|
|
|
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
|
2021-01-25 23:57:50 +00:00
|
|
|
|| realPosition.scale != savedPosition.scale
|
2017-07-03 12:23:41 +00:00
|
|
|
|| realPosition.moncrc != savedPosition.moncrc
|
|
|
|
|| realPosition.maximized != savedPosition.maximized) {
|
2021-01-25 23:57:50 +00:00
|
|
|
DEBUG_LOG(("Window Pos: Writing: %1, %2, %3, %4 (scale %5%, maximized %6)")
|
|
|
|
.arg(realPosition.x)
|
|
|
|
.arg(realPosition.y)
|
|
|
|
.arg(realPosition.w)
|
|
|
|
.arg(realPosition.h)
|
|
|
|
.arg(realPosition.scale)
|
|
|
|
.arg(Logs::b(realPosition.maximized)));
|
2021-02-17 15:20:05 +00:00
|
|
|
Core::App().settings().setWindowPosition(realPosition);
|
|
|
|
Core::App().saveSettingsDelayed();
|
2016-11-04 11:14:47 +00:00
|
|
|
}
|
|
|
|
}
|
2016-06-16 12:59:54 +00:00
|
|
|
}
|
|
|
|
|
2016-12-31 15:19:22 +00:00
|
|
|
bool MainWindow::minimizeToTray() {
|
2022-01-27 12:48:51 +00:00
|
|
|
if (Core::Quitting() || !hasTrayIcon()) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-12-31 15:19:22 +00:00
|
|
|
|
|
|
|
closeWithoutDestroy();
|
2020-06-17 09:36:25 +00:00
|
|
|
controller().updateIsActiveBlur();
|
2016-12-31 15:19:22 +00:00
|
|
|
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() {
|
2020-06-13 01:03:23 +00:00
|
|
|
#if defined Q_OS_UNIX && !defined Q_OS_MAC
|
2021-02-25 15:12:51 +00:00
|
|
|
const auto weak = Ui::MakeWeak(this);
|
2018-06-05 18:14:38 +00:00
|
|
|
const auto reActivate = [=] {
|
2021-02-25 15:12:51 +00:00
|
|
|
if (const auto w = weak.data()) {
|
2018-06-05 18:14:38 +00:00
|
|
|
if (auto f = QApplication::focusWidget()) {
|
|
|
|
f->clearFocus();
|
|
|
|
}
|
|
|
|
w->activate();
|
|
|
|
if (auto f = QApplication::focusWidget()) {
|
|
|
|
f->clearFocus();
|
|
|
|
}
|
|
|
|
w->setInnerFocus();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
crl::on_main(this, reActivate);
|
2019-09-26 10:55:35 +00:00
|
|
|
base::call_delayed(200, this, reActivate);
|
2020-06-13 01:03:23 +00:00
|
|
|
#endif // Q_OS_UNIX && !Q_OS_MAC
|
2018-06-05 18:14:38 +00:00
|
|
|
}
|
|
|
|
|
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) {
|
2021-07-25 21:30:56 +00:00
|
|
|
_rightColumn->setParent(body());
|
2017-02-03 20:07:26 +00:00
|
|
|
_rightColumn->show();
|
|
|
|
_rightColumn->setFocus();
|
2021-02-25 15:12:51 +00:00
|
|
|
} else {
|
|
|
|
setInnerFocus();
|
2017-02-03 20:07:26 +00:00
|
|
|
}
|
2019-09-05 05:18:21 +00:00
|
|
|
const auto nowRightWidth = _rightColumn ? _rightColumn->width() : 0;
|
2019-09-09 15:37:04 +00:00
|
|
|
const auto wasMinimumWidth = minimumWidth();
|
2020-03-03 12:07:22 +00:00
|
|
|
const auto nowMinimumWidth = computeMinWidth();
|
2019-09-09 15:37:04 +00:00
|
|
|
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 {
|
2021-10-19 13:00:21 +00:00
|
|
|
auto desktop = (screen() ? screen() : QApplication::primaryScreen())->availableGeometry();
|
2021-07-25 21:30:56 +00:00
|
|
|
return std::max(desktop.width() - body()->width(), 0);
|
2017-04-09 18:06:06 +00:00
|
|
|
}
|
|
|
|
|
2017-11-16 16:43:52 +00:00
|
|
|
bool MainWindow::canExtendNoMove(int extendBy) const {
|
2021-10-19 13:00:21 +00:00
|
|
|
auto desktop = (screen() ? screen() : QApplication::primaryScreen())->availableGeometry();
|
2021-07-25 21:30:56 +00:00
|
|
|
auto inner = body()->mapToGlobal(body()->rect());
|
2017-11-16 16:43:52 +00:00
|
|
|
auto innerRight = (inner.x() + inner.width() + extendBy);
|
|
|
|
auto desktopRight = (desktop.x() + desktop.width());
|
|
|
|
return innerRight <= desktopRight;
|
|
|
|
}
|
|
|
|
|
|
|
|
int MainWindow::tryToExtendWidthBy(int addToWidth) {
|
2021-10-19 13:00:21 +00:00
|
|
|
auto desktop = (screen() ? screen() : QApplication::primaryScreen())->availableGeometry();
|
2021-07-25 21:30:56 +00:00
|
|
|
auto inner = body()->mapToGlobal(body()->rect());
|
2017-11-16 16:43:52 +00:00
|
|
|
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) {
|
2021-07-25 21:30:56 +00:00
|
|
|
setGeometry(QRect(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
|
|
|
}
|
|
|
|
|
2021-05-25 12:42:26 +00:00
|
|
|
void MainWindow::launchDrag(
|
|
|
|
std::unique_ptr<QMimeData> data,
|
|
|
|
Fn<void()> &&callback) {
|
2021-12-07 23:40:44 +00:00
|
|
|
// Qt destroys this QDrag automatically after the drag is finished
|
|
|
|
// We must not delete this at the end of this function, as this breaks DnD on Linux
|
|
|
|
auto drag = new QDrag(this);
|
2017-06-21 21:38:31 +00:00
|
|
|
drag->setMimeData(data.release());
|
|
|
|
drag->exec(Qt::CopyAction);
|
|
|
|
|
|
|
|
// We don't receive mouseReleaseEvent when drag is finished.
|
|
|
|
ClickHandler::unpressed();
|
2021-05-25 12:42:26 +00:00
|
|
|
callback();
|
2017-06-21 21:38:31 +00:00
|
|
|
}
|
|
|
|
|
2020-07-07 17:33:34 +00:00
|
|
|
MainWindow::~MainWindow() {
|
2020-10-01 14:45:25 +00:00
|
|
|
// Otherwise:
|
|
|
|
// ~QWidget
|
|
|
|
// QWidgetPrivate::close_helper
|
|
|
|
// QWidgetPrivate::setVisible
|
|
|
|
// QWidgetPrivate::hide_helper
|
|
|
|
// QWidgetPrivate::hide_sys
|
|
|
|
// QWindowPrivate::setVisible
|
|
|
|
// QMetaObject::activate
|
|
|
|
// Window::MainWindow::handleVisibleChanged on a destroyed MainWindow.
|
|
|
|
hide();
|
2020-07-07 17:33:34 +00:00
|
|
|
}
|
2016-12-31 15:19:22 +00:00
|
|
|
|
2016-06-16 12:59:54 +00:00
|
|
|
} // namespace Window
|