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
|
|
|
*/
|
2016-06-16 15:17:39 +00:00
|
|
|
#include "platform/mac/main_window_mac.h"
|
2016-06-16 12:59:54 +00:00
|
|
|
|
2019-06-10 21:39:58 +00:00
|
|
|
#include "data/data_session.h"
|
2016-11-04 08:23:50 +00:00
|
|
|
#include "styles/style_window.h"
|
2016-06-16 12:59:54 +00:00
|
|
|
#include "mainwindow.h"
|
|
|
|
#include "mainwidget.h"
|
2019-01-21 13:42:21 +00:00
|
|
|
#include "core/application.h"
|
2019-09-08 20:04:21 +00:00
|
|
|
#include "core/sandbox.h"
|
2019-07-24 11:45:24 +00:00
|
|
|
#include "main/main_session.h"
|
2018-01-25 14:19:14 +00:00
|
|
|
#include "history/history.h"
|
2017-06-29 10:27:09 +00:00
|
|
|
#include "history/history_widget.h"
|
2017-04-08 15:05:59 +00:00
|
|
|
#include "history/history_inner_widget.h"
|
2020-06-16 11:04:04 +00:00
|
|
|
#include "main/main_account.h"
|
2020-06-18 18:04:16 +00:00
|
|
|
#include "main/main_domain.h" // Domain::activeSessionValue
|
2019-06-10 21:39:58 +00:00
|
|
|
#include "media/player/media_player_instance.h"
|
|
|
|
#include "media/audio/media_audio.h"
|
2017-03-04 10:23:56 +00:00
|
|
|
#include "storage/localstorage.h"
|
2016-10-02 13:54:27 +00:00
|
|
|
#include "window/notifications_manager_default.h"
|
2019-07-27 13:20:28 +00:00
|
|
|
#include "window/window_session_controller.h"
|
2020-06-24 07:56:16 +00:00
|
|
|
#include "window/window_controller.h"
|
2017-11-15 12:46:34 +00:00
|
|
|
#include "window/themes/window_theme.h"
|
2020-07-09 21:12:06 +00:00
|
|
|
#include "platform/mac/touchbar/mac_touchbar_manager.h"
|
2021-01-10 03:52:18 +00:00
|
|
|
#include "platform/platform_specific.h"
|
2016-11-06 18:45:50 +00:00
|
|
|
#include "platform/platform_notifications_manager.h"
|
2019-09-17 16:13:12 +00:00
|
|
|
#include "base/platform/base_platform_info.h"
|
2017-08-14 12:53:49 +00:00
|
|
|
#include "boxes/peer_list_controllers.h"
|
2017-04-06 14:38:10 +00:00
|
|
|
#include "boxes/about_box.h"
|
2017-04-13 08:27:10 +00:00
|
|
|
#include "lang/lang_keys.h"
|
2019-09-26 10:20:34 +00:00
|
|
|
#include "base/platform/mac/base_utilities_mac.h"
|
2019-06-10 21:39:58 +00:00
|
|
|
#include "ui/widgets/input_fields.h"
|
2019-09-13 06:06:02 +00:00
|
|
|
#include "facades.h"
|
|
|
|
#include "app.h"
|
2016-06-16 12:59:54 +00:00
|
|
|
|
2019-09-08 20:04:21 +00:00
|
|
|
#include <QtWidgets/QApplication>
|
2019-09-04 07:19:15 +00:00
|
|
|
#include <QtGui/QClipboard>
|
|
|
|
|
2016-06-16 12:59:54 +00:00
|
|
|
#include <Cocoa/Cocoa.h>
|
|
|
|
#include <CoreFoundation/CFURL.h>
|
2016-11-08 14:07:25 +00:00
|
|
|
#include <IOKit/IOKitLib.h>
|
2016-06-16 12:59:54 +00:00
|
|
|
#include <IOKit/hidsystem/ev_keymap.h>
|
2016-11-08 14:07:25 +00:00
|
|
|
#include <SPMediaKeyTap.h>
|
|
|
|
|
2017-12-11 11:06:05 +00:00
|
|
|
@interface MainWindowObserver : NSObject {
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) init:(MainWindow::Private*)window;
|
|
|
|
- (void) activeSpaceDidChange:(NSNotification *)aNotification;
|
|
|
|
- (void) darkModeChanged:(NSNotification *)aNotification;
|
|
|
|
- (void) screenIsLocked:(NSNotification *)aNotification;
|
|
|
|
- (void) screenIsUnlocked:(NSNotification *)aNotification;
|
|
|
|
- (void) windowWillEnterFullScreen:(NSNotification *)aNotification;
|
|
|
|
- (void) windowWillExitFullScreen:(NSNotification *)aNotification;
|
2019-10-20 10:23:02 +00:00
|
|
|
- (void) windowDidExitFullScreen:(NSNotification *)aNotification;
|
2017-12-11 11:06:05 +00:00
|
|
|
|
|
|
|
@end // @interface MainWindowObserver
|
|
|
|
|
|
|
|
namespace Platform {
|
2017-04-12 11:58:37 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
// When we close a window that is fullscreen we first leave the fullscreen
|
|
|
|
// mode and after that hide the window. This is a timeout for elaving the
|
|
|
|
// fullscreen mode, after that we'll hide the window no matter what.
|
|
|
|
constexpr auto kHideAfterFullscreenTimeoutMs = 3000;
|
|
|
|
|
2017-11-15 12:46:34 +00:00
|
|
|
id FindClassInSubviews(NSView *parent, NSString *className) {
|
|
|
|
for (NSView *child in [parent subviews]) {
|
|
|
|
if ([child isKindOfClass:NSClassFromString(className)]) {
|
|
|
|
return child;
|
|
|
|
} else if (id inchild = FindClassInSubviews(child, className)) {
|
|
|
|
return inchild;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
2017-12-11 11:06:05 +00:00
|
|
|
#ifndef OS_MAC_OLD
|
2017-04-12 11:58:37 +00:00
|
|
|
|
2017-12-11 11:06:05 +00:00
|
|
|
class LayerCreationChecker : public QObject {
|
|
|
|
public:
|
2018-06-04 15:35:11 +00:00
|
|
|
LayerCreationChecker(NSView * __weak view, Fn<void()> callback)
|
2017-12-11 11:06:05 +00:00
|
|
|
: _weakView(view)
|
|
|
|
, _callback(std::move(callback)) {
|
2019-09-04 07:19:15 +00:00
|
|
|
QCoreApplication::instance()->installEventFilter(this);
|
2017-12-11 11:06:05 +00:00
|
|
|
}
|
2016-11-08 14:07:25 +00:00
|
|
|
|
2017-12-11 11:06:05 +00:00
|
|
|
protected:
|
|
|
|
bool eventFilter(QObject *object, QEvent *event) override {
|
|
|
|
if (!_weakView || [_weakView layer] != nullptr) {
|
|
|
|
_callback();
|
|
|
|
}
|
|
|
|
return QObject::eventFilter(object, event);
|
|
|
|
}
|
2016-11-08 14:07:25 +00:00
|
|
|
|
2017-12-11 11:06:05 +00:00
|
|
|
private:
|
|
|
|
NSView * __weak _weakView = nil;
|
2018-06-04 15:35:11 +00:00
|
|
|
Fn<void()> _callback;
|
2016-06-16 12:59:54 +00:00
|
|
|
|
2017-12-11 11:06:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // OS_MAC_OLD
|
|
|
|
|
2019-06-07 11:30:30 +00:00
|
|
|
class EventFilter : public QAbstractNativeEventFilter {
|
|
|
|
public:
|
|
|
|
EventFilter(not_null<MainWindow*> window) : _window(window) {
|
|
|
|
}
|
|
|
|
|
|
|
|
bool nativeEventFilter(
|
|
|
|
const QByteArray &eventType,
|
|
|
|
void *message,
|
|
|
|
long *result) {
|
|
|
|
return Core::Sandbox::Instance().customEnterFromEventLoop([&] {
|
|
|
|
return _window->psFilterNativeEvent(message);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
not_null<MainWindow*> _window;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2020-11-02 14:13:25 +00:00
|
|
|
[[nodiscard]] QImage TrayIconBack(bool darkMode) {
|
|
|
|
static const auto WithColor = [](QColor color) {
|
|
|
|
return st::macTrayIcon.instance(color, 100);
|
|
|
|
};
|
|
|
|
static const auto DarkModeResult = WithColor({ 255, 255, 255 });
|
|
|
|
static const auto LightModeResult = WithColor({ 0, 0, 0, 180 });
|
|
|
|
auto result = darkMode ? DarkModeResult : LightModeResult;
|
|
|
|
result.detach();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-12-11 11:06:05 +00:00
|
|
|
} // namespace
|
2016-06-16 12:59:54 +00:00
|
|
|
|
2016-11-08 14:07:25 +00:00
|
|
|
class MainWindow::Private {
|
|
|
|
public:
|
2019-06-07 11:30:30 +00:00
|
|
|
explicit Private(not_null<MainWindow*> window);
|
2016-11-08 14:07:25 +00:00
|
|
|
|
2017-11-15 12:46:34 +00:00
|
|
|
void setNativeWindow(NSWindow *window, NSView *view);
|
2020-07-09 21:12:06 +00:00
|
|
|
void initTouchBar(
|
|
|
|
NSWindow *window,
|
|
|
|
not_null<Window::Controller*> controller,
|
|
|
|
rpl::producer<bool> canApplyMarkdown);
|
2016-11-08 14:07:25 +00:00
|
|
|
void setWindowBadge(const QString &str);
|
2017-11-15 12:46:34 +00:00
|
|
|
void setWindowTitle(const QString &str);
|
|
|
|
void updateNativeTitle();
|
2016-11-08 14:07:25 +00:00
|
|
|
|
|
|
|
void enableShadow(WId winId);
|
|
|
|
|
|
|
|
bool filterNativeEvent(void *event);
|
|
|
|
|
|
|
|
void willEnterFullScreen();
|
|
|
|
void willExitFullScreen();
|
2019-10-20 10:23:02 +00:00
|
|
|
void didExitFullScreen();
|
2016-11-08 14:07:25 +00:00
|
|
|
|
2017-03-05 20:18:27 +00:00
|
|
|
bool clipboardHasText();
|
2016-11-08 14:07:25 +00:00
|
|
|
~Private();
|
|
|
|
|
|
|
|
private:
|
2017-11-15 12:46:34 +00:00
|
|
|
void initCustomTitle();
|
|
|
|
void refreshWeakTitleReferences();
|
2019-10-20 10:23:02 +00:00
|
|
|
void enforceCorrectStyleMask();
|
2017-11-15 12:46:34 +00:00
|
|
|
|
2019-06-07 11:30:30 +00:00
|
|
|
not_null<MainWindow*> _public;
|
2016-11-08 14:07:25 +00:00
|
|
|
friend class MainWindow;
|
|
|
|
|
2017-11-15 12:46:34 +00:00
|
|
|
#ifdef OS_MAC_OLD
|
|
|
|
NSWindow *_nativeWindow = nil;
|
|
|
|
NSView *_nativeView = nil;
|
|
|
|
#else // OS_MAC_OLD
|
|
|
|
NSWindow * __weak _nativeWindow = nil;
|
|
|
|
NSView * __weak _nativeView = nil;
|
|
|
|
id __weak _nativeTitleWrapWeak = nil;
|
|
|
|
id __weak _nativeTitleWeak = nil;
|
2017-12-11 11:06:05 +00:00
|
|
|
std::unique_ptr<LayerCreationChecker> _layerCreationChecker;
|
2017-11-15 12:46:34 +00:00
|
|
|
#endif // !OS_MAC_OLD
|
|
|
|
bool _useNativeTitle = false;
|
|
|
|
bool _inFullScreen = false;
|
|
|
|
|
2019-06-07 11:30:30 +00:00
|
|
|
MainWindowObserver *_observer = nullptr;
|
2017-03-05 20:18:27 +00:00
|
|
|
NSPasteboard *_generalPasteboard = nullptr;
|
|
|
|
int _generalPasteboardChangeCount = -1;
|
|
|
|
bool _generalPasteboardHasText = false;
|
2016-11-08 14:07:25 +00:00
|
|
|
|
2019-06-07 11:30:30 +00:00
|
|
|
EventFilter _nativeEventFilter;
|
|
|
|
|
2016-11-08 14:07:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace Platform
|
|
|
|
|
|
|
|
@implementation MainWindowObserver {
|
2017-04-12 11:58:37 +00:00
|
|
|
MainWindow::Private *_private;
|
2016-11-08 14:07:25 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) init:(MainWindow::Private*)window {
|
|
|
|
if (self = [super init]) {
|
|
|
|
_private = window;
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) activeSpaceDidChange:(NSNotification *)aNotification {
|
2016-06-16 12:59:54 +00:00
|
|
|
}
|
|
|
|
|
2016-11-08 14:07:25 +00:00
|
|
|
- (void) darkModeChanged:(NSNotification *)aNotification {
|
2020-06-30 13:41:55 +00:00
|
|
|
Core::Sandbox::Instance().customEnterFromEventLoop([&] {
|
2020-07-23 08:30:20 +00:00
|
|
|
Core::App().settings().setSystemDarkMode(Platform::IsDarkMode());
|
2020-06-30 13:41:55 +00:00
|
|
|
});
|
2016-06-16 12:59:54 +00:00
|
|
|
}
|
|
|
|
|
2016-11-08 14:07:25 +00:00
|
|
|
- (void) screenIsLocked:(NSNotification *)aNotification {
|
|
|
|
Global::SetScreenIsLocked(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) screenIsUnlocked:(NSNotification *)aNotification {
|
|
|
|
Global::SetScreenIsLocked(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) windowWillEnterFullScreen:(NSNotification *)aNotification {
|
|
|
|
_private->willEnterFullScreen();
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) windowWillExitFullScreen:(NSNotification *)aNotification {
|
|
|
|
_private->willExitFullScreen();
|
|
|
|
}
|
|
|
|
|
2019-10-20 10:23:02 +00:00
|
|
|
- (void) windowDidExitFullScreen:(NSNotification *)aNotification {
|
|
|
|
_private->didExitFullScreen();
|
|
|
|
}
|
|
|
|
|
2017-04-12 11:58:37 +00:00
|
|
|
@end // @implementation MainWindowObserver
|
2016-11-08 14:07:25 +00:00
|
|
|
|
|
|
|
namespace Platform {
|
2019-08-20 11:29:21 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
void SendKeySequence(Qt::Key key, Qt::KeyboardModifiers modifiers = Qt::NoModifier) {
|
|
|
|
const auto focused = QApplication::focusWidget();
|
|
|
|
if (qobject_cast<QLineEdit*>(focused) || qobject_cast<QTextEdit*>(focused) || qobject_cast<HistoryInner*>(focused)) {
|
|
|
|
QApplication::postEvent(focused, new QKeyEvent(QEvent::KeyPress, key, modifiers));
|
|
|
|
QApplication::postEvent(focused, new QKeyEvent(QEvent::KeyRelease, key, modifiers));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ForceDisabled(QAction *action, bool disabled) {
|
|
|
|
if (action->isEnabled()) {
|
|
|
|
if (disabled) action->setDisabled(true);
|
|
|
|
} else if (!disabled) {
|
|
|
|
action->setDisabled(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
2016-11-08 14:07:25 +00:00
|
|
|
|
2019-06-07 11:30:30 +00:00
|
|
|
MainWindow::Private::Private(not_null<MainWindow*> window)
|
2016-11-08 14:07:25 +00:00
|
|
|
: _public(window)
|
2019-06-07 11:30:30 +00:00
|
|
|
, _observer([[MainWindowObserver alloc] init:this])
|
|
|
|
, _nativeEventFilter(window) {
|
2017-03-05 20:18:27 +00:00
|
|
|
_generalPasteboard = [NSPasteboard generalPasteboard];
|
|
|
|
|
2016-11-08 14:07:25 +00:00
|
|
|
@autoreleasepool {
|
|
|
|
|
|
|
|
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:_observer selector:@selector(activeSpaceDidChange:) name:NSWorkspaceActiveSpaceDidChangeNotification object:nil];
|
|
|
|
[[NSDistributedNotificationCenter defaultCenter] addObserver:_observer selector:@selector(darkModeChanged:) name:Q2NSString(strNotificationAboutThemeChange()) object:nil];
|
|
|
|
[[NSDistributedNotificationCenter defaultCenter] addObserver:_observer selector:@selector(screenIsLocked:) name:Q2NSString(strNotificationAboutScreenLocked()) object:nil];
|
|
|
|
[[NSDistributedNotificationCenter defaultCenter] addObserver:_observer selector:@selector(screenIsUnlocked:) name:Q2NSString(strNotificationAboutScreenUnlocked()) object:nil];
|
|
|
|
|
|
|
|
#ifndef OS_MAC_STORE
|
|
|
|
// Register defaults for the whitelist of apps that want to use media keys
|
|
|
|
[[NSUserDefaults standardUserDefaults] registerDefaults:[NSDictionary dictionaryWithObjectsAndKeys:[SPMediaKeyTap defaultMediaKeyUserBundleIdentifiers], kMediaKeyUsingBundleIdentifiersDefaultsKey, nil]];
|
|
|
|
#endif // !OS_MAC_STORE
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MainWindow::Private::setWindowBadge(const QString &str) {
|
|
|
|
@autoreleasepool {
|
|
|
|
|
|
|
|
[[NSApp dockTile] setBadgeLabel:Q2NSString(str)];
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-15 12:46:34 +00:00
|
|
|
void MainWindow::Private::setWindowTitle(const QString &str) {
|
|
|
|
_public->setWindowTitle(str);
|
|
|
|
updateNativeTitle();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MainWindow::Private::setNativeWindow(NSWindow *window, NSView *view) {
|
|
|
|
_nativeWindow = window;
|
|
|
|
_nativeView = view;
|
|
|
|
initCustomTitle();
|
|
|
|
}
|
|
|
|
|
2020-07-09 21:12:06 +00:00
|
|
|
void MainWindow::Private::initTouchBar(
|
|
|
|
NSWindow *window,
|
|
|
|
not_null<Window::Controller*> controller,
|
|
|
|
rpl::producer<bool> canApplyMarkdown) {
|
|
|
|
#ifndef OS_OSX
|
|
|
|
if (!IsMac10_13OrGreater()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
[NSApplication sharedApplication]
|
|
|
|
.automaticCustomizeTouchBarMenuItemEnabled = true;
|
|
|
|
|
|
|
|
[window
|
|
|
|
performSelectorOnMainThread:@selector(setTouchBar:)
|
|
|
|
withObject:[[[RootTouchBar alloc]
|
|
|
|
init:std::move(canApplyMarkdown)
|
|
|
|
controller:controller
|
|
|
|
domain:(&Core::App().domain())] autorelease]
|
|
|
|
waitUntilDone:true];
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-11-15 12:46:34 +00:00
|
|
|
void MainWindow::Private::initCustomTitle() {
|
|
|
|
#ifndef OS_MAC_OLD
|
|
|
|
if (![_nativeWindow respondsToSelector:@selector(contentLayoutRect)]
|
|
|
|
|| ![_nativeWindow respondsToSelector:@selector(setTitlebarAppearsTransparent:)]) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
[_nativeWindow setTitlebarAppearsTransparent:YES];
|
|
|
|
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:_observer selector:@selector(windowWillEnterFullScreen:) name:NSWindowWillEnterFullScreenNotification object:_nativeWindow];
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:_observer selector:@selector(windowWillExitFullScreen:) name:NSWindowWillExitFullScreenNotification object:_nativeWindow];
|
2019-10-20 10:23:02 +00:00
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:_observer selector:@selector(windowDidExitFullScreen:) name:NSWindowDidExitFullScreenNotification object:_nativeWindow];
|
2017-11-15 12:46:34 +00:00
|
|
|
|
|
|
|
// Qt has bug with layer-backed widgets containing QOpenGLWidgets.
|
|
|
|
// See https://bugreports.qt.io/browse/QTBUG-64494
|
|
|
|
// Emulate custom title instead (code below).
|
2017-11-22 17:32:42 +00:00
|
|
|
//
|
|
|
|
// Tried to backport a fix, testing.
|
|
|
|
[_nativeWindow setStyleMask:[_nativeWindow styleMask] | NSFullSizeContentViewWindowMask];
|
|
|
|
auto inner = [_nativeWindow contentLayoutRect];
|
|
|
|
auto full = [_nativeView frame];
|
|
|
|
_public->_customTitleHeight = qMax(qRound(full.size.height - inner.size.height), 0);
|
|
|
|
|
2017-12-11 11:06:05 +00:00
|
|
|
// Qt still has some bug with layer-backed widgets containing QOpenGLWidgets.
|
2017-12-11 11:19:13 +00:00
|
|
|
// See https://github.com/telegramdesktop/tdesktop/issues/4150
|
2017-12-11 11:06:05 +00:00
|
|
|
// Tried to workaround it by catching the first moment we have CALayer created
|
|
|
|
// and explicitly setting contentsScale to window->backingScaleFactor there.
|
|
|
|
_layerCreationChecker = std::make_unique<LayerCreationChecker>(_nativeView, [=] {
|
|
|
|
if (_nativeView && _nativeWindow) {
|
|
|
|
if (CALayer *layer = [_nativeView layer]) {
|
|
|
|
LOG(("Window Info: Setting layer scale factor to: %1").arg([_nativeWindow backingScaleFactor]));
|
|
|
|
[layer setContentsScale: [_nativeWindow backingScaleFactor]];
|
|
|
|
_layerCreationChecker = nullptr;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
_layerCreationChecker = nullptr;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-11-22 17:32:42 +00:00
|
|
|
// Disabled for now.
|
|
|
|
//_useNativeTitle = true;
|
|
|
|
//setWindowTitle(qsl("Telegram"));
|
2017-11-15 12:46:34 +00:00
|
|
|
#endif // !OS_MAC_OLD
|
|
|
|
}
|
|
|
|
|
|
|
|
void MainWindow::Private::refreshWeakTitleReferences() {
|
|
|
|
if (!_nativeWindow) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef OS_MAC_OLD
|
|
|
|
@autoreleasepool {
|
|
|
|
|
|
|
|
if (NSView *parent = [[_nativeWindow contentView] superview]) {
|
|
|
|
if (id titleWrap = FindClassInSubviews(parent, Q2NSString(strTitleWrapClass()))) {
|
|
|
|
if ([titleWrap respondsToSelector:@selector(setBackgroundColor:)]) {
|
|
|
|
if (id title = FindClassInSubviews(titleWrap, Q2NSString(strTitleClass()))) {
|
|
|
|
if ([title respondsToSelector:@selector(setAttributedStringValue:)]) {
|
|
|
|
_nativeTitleWrapWeak = titleWrap;
|
|
|
|
_nativeTitleWeak = title;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
#endif // !OS_MAC_OLD
|
|
|
|
}
|
2016-11-08 14:07:25 +00:00
|
|
|
|
2017-11-15 12:46:34 +00:00
|
|
|
void MainWindow::Private::updateNativeTitle() {
|
|
|
|
if (!_useNativeTitle) {
|
|
|
|
return;
|
|
|
|
}
|
2016-11-08 14:07:25 +00:00
|
|
|
#ifndef OS_MAC_OLD
|
2017-11-15 12:46:34 +00:00
|
|
|
if (!_nativeTitleWrapWeak || !_nativeTitleWeak) {
|
|
|
|
refreshWeakTitleReferences();
|
|
|
|
}
|
|
|
|
if (_nativeTitleWrapWeak && _nativeTitleWeak) {
|
|
|
|
@autoreleasepool {
|
|
|
|
|
|
|
|
auto convertColor = [](QColor color) {
|
|
|
|
return [NSColor colorWithDeviceRed:color.redF() green:color.greenF() blue:color.blueF() alpha:color.alphaF()];
|
|
|
|
};
|
|
|
|
auto adjustFg = [](const style::color &st) {
|
|
|
|
// Weird thing with NSTextField taking NSAttributedString with
|
|
|
|
// NSForegroundColorAttributeName set to colorWithDeviceRed:green:blue
|
|
|
|
// with components all equal to 128 - it ignores it and prints black text!
|
|
|
|
auto color = st->c;
|
|
|
|
return (color.red() == 128 && color.green() == 128 && color.blue() == 128)
|
|
|
|
? QColor(129, 129, 129, color.alpha())
|
|
|
|
: color;
|
|
|
|
};
|
|
|
|
|
|
|
|
auto active = _public->isActiveWindow();
|
|
|
|
auto bgColor = (active ? st::titleBgActive : st::titleBg)->c;
|
|
|
|
auto fgColor = adjustFg(active ? st::titleFgActive : st::titleFg);
|
|
|
|
|
|
|
|
auto bgConverted = convertColor(bgColor);
|
|
|
|
auto fgConverted = convertColor(fgColor);
|
|
|
|
[_nativeTitleWrapWeak setBackgroundColor:bgConverted];
|
|
|
|
|
|
|
|
auto title = Q2NSString(_public->windowTitle());
|
|
|
|
NSDictionary *attributes = _inFullScreen
|
|
|
|
? nil
|
|
|
|
: [NSDictionary dictionaryWithObjectsAndKeys: fgConverted, NSForegroundColorAttributeName, bgConverted, NSBackgroundColorAttributeName, nil];
|
|
|
|
NSAttributedString *string = [[NSAttributedString alloc] initWithString:title attributes:attributes];
|
|
|
|
[_nativeTitleWeak setAttributedStringValue:string];
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2016-11-08 14:07:25 +00:00
|
|
|
#endif // !OS_MAC_OLD
|
|
|
|
}
|
|
|
|
|
2017-03-05 20:18:27 +00:00
|
|
|
bool MainWindow::Private::clipboardHasText() {
|
|
|
|
auto currentChangeCount = static_cast<int>([_generalPasteboard changeCount]);
|
|
|
|
if (_generalPasteboardChangeCount != currentChangeCount) {
|
|
|
|
_generalPasteboardChangeCount = currentChangeCount;
|
2019-09-04 07:19:15 +00:00
|
|
|
_generalPasteboardHasText = !QGuiApplication::clipboard()->text().isEmpty();
|
2017-03-05 20:18:27 +00:00
|
|
|
}
|
|
|
|
return _generalPasteboardHasText;
|
|
|
|
}
|
|
|
|
|
2016-11-08 14:07:25 +00:00
|
|
|
void MainWindow::Private::willEnterFullScreen() {
|
2017-11-15 12:46:34 +00:00
|
|
|
_inFullScreen = true;
|
2016-11-09 08:34:38 +00:00
|
|
|
_public->setTitleVisible(false);
|
2016-11-08 14:07:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MainWindow::Private::willExitFullScreen() {
|
2017-11-15 12:46:34 +00:00
|
|
|
_inFullScreen = false;
|
2016-11-09 08:34:38 +00:00
|
|
|
_public->setTitleVisible(true);
|
2019-10-20 10:23:02 +00:00
|
|
|
enforceCorrectStyleMask();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MainWindow::Private::didExitFullScreen() {
|
|
|
|
enforceCorrectStyleMask();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MainWindow::Private::enforceCorrectStyleMask() {
|
|
|
|
if (_nativeWindow && _public->_customTitleHeight > 0) {
|
|
|
|
[_nativeWindow setStyleMask:[_nativeWindow styleMask] | NSFullSizeContentViewWindowMask];
|
|
|
|
}
|
2016-11-08 14:07:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MainWindow::Private::enableShadow(WId winId) {
|
|
|
|
// [[(NSView*)winId window] setStyleMask:NSBorderlessWindowMask];
|
|
|
|
// [[(NSView*)winId window] setHasShadow:YES];
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MainWindow::Private::filterNativeEvent(void *event) {
|
|
|
|
NSEvent *e = static_cast<NSEvent*>(event);
|
|
|
|
if (e && [e type] == NSSystemDefined && [e subtype] == SPSystemDefinedEventMediaKeys) {
|
|
|
|
#ifndef OS_MAC_STORE
|
|
|
|
// If event tap is not installed, handle events that reach the app instead
|
|
|
|
if (![SPMediaKeyTap usesGlobalMediaKeyTap]) {
|
|
|
|
return objc_handleMediaKeyEvent(e);
|
|
|
|
}
|
|
|
|
#else // !OS_MAC_STORE
|
|
|
|
return objc_handleMediaKeyEvent(e);
|
|
|
|
#endif // else for !OS_MAC_STORE
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
MainWindow::Private::~Private() {
|
|
|
|
[_observer release];
|
|
|
|
}
|
|
|
|
|
2019-06-06 11:20:21 +00:00
|
|
|
MainWindow::MainWindow(not_null<Window::Controller*> controller)
|
|
|
|
: Window::MainWindow(controller)
|
|
|
|
, _private(std::make_unique<Private>(this)) {
|
2019-06-07 11:30:30 +00:00
|
|
|
QCoreApplication::instance()->installNativeEventFilter(
|
|
|
|
&_private->_nativeEventFilter);
|
|
|
|
|
2017-11-19 16:31:58 +00:00
|
|
|
#ifndef OS_MAC_OLD
|
2017-10-16 14:16:01 +00:00
|
|
|
auto forceOpenGL = std::make_unique<QOpenGLWidget>(this);
|
2017-11-19 16:31:58 +00:00
|
|
|
#endif // !OS_MAC_OLD
|
2017-10-16 14:16:01 +00:00
|
|
|
|
2017-04-12 11:58:37 +00:00
|
|
|
_hideAfterFullScreenTimer.setCallback([this] { hideAndDeactivate(); });
|
2017-11-15 12:46:34 +00:00
|
|
|
|
|
|
|
subscribe(Window::Theme::Background(), [this](const Window::Theme::BackgroundUpdate &data) {
|
|
|
|
if (data.paletteChanged()) {
|
|
|
|
_private->updateNativeTitle();
|
|
|
|
}
|
|
|
|
});
|
2020-06-29 18:12:40 +00:00
|
|
|
}
|
|
|
|
|
2016-06-16 15:17:39 +00:00
|
|
|
void MainWindow::closeWithoutDestroy() {
|
|
|
|
NSWindow *nsWindow = [reinterpret_cast<NSView*>(winId()) window];
|
2017-02-18 13:32:38 +00:00
|
|
|
|
|
|
|
auto isFullScreen = (([nsWindow styleMask] & NSFullScreenWindowMask) == NSFullScreenWindowMask);
|
2016-06-16 15:17:39 +00:00
|
|
|
if (isFullScreen) {
|
2017-04-12 11:58:37 +00:00
|
|
|
_hideAfterFullScreenTimer.callOnce(kHideAfterFullscreenTimeoutMs);
|
2016-06-16 15:17:39 +00:00
|
|
|
[nsWindow toggleFullScreen:nsWindow];
|
|
|
|
} else {
|
2017-02-20 10:36:25 +00:00
|
|
|
hideAndDeactivate();
|
2016-06-16 15:17:39 +00:00
|
|
|
}
|
2016-06-16 12:59:54 +00:00
|
|
|
}
|
|
|
|
|
2016-06-16 15:17:39 +00:00
|
|
|
void MainWindow::stateChangedHook(Qt::WindowState state) {
|
|
|
|
if (_hideAfterFullScreenTimer.isActive()) {
|
2017-04-12 11:58:37 +00:00
|
|
|
_hideAfterFullScreenTimer.callOnce(0);
|
2016-06-16 15:17:39 +00:00
|
|
|
}
|
2016-06-16 12:59:54 +00:00
|
|
|
}
|
|
|
|
|
2017-11-15 12:46:34 +00:00
|
|
|
void MainWindow::handleActiveChangedHook() {
|
|
|
|
InvokeQueued(this, [this] { _private->updateNativeTitle(); });
|
|
|
|
}
|
|
|
|
|
2016-11-08 14:07:25 +00:00
|
|
|
void MainWindow::initHook() {
|
|
|
|
_customTitleHeight = 0;
|
|
|
|
if (auto view = reinterpret_cast<NSView*>(winId())) {
|
|
|
|
if (auto window = [view window]) {
|
2017-11-15 12:46:34 +00:00
|
|
|
_private->setNativeWindow(window, view);
|
2020-07-09 21:12:06 +00:00
|
|
|
_private->initTouchBar(
|
|
|
|
window,
|
|
|
|
&controller(),
|
|
|
|
_canApplyMarkdown.changes());
|
2016-11-08 14:07:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-12 15:27:19 +00:00
|
|
|
void MainWindow::updateWindowIcon() {
|
|
|
|
}
|
|
|
|
|
2016-11-09 08:34:38 +00:00
|
|
|
void MainWindow::titleVisibilityChangedHook() {
|
|
|
|
updateTitleCounter();
|
|
|
|
}
|
|
|
|
|
2017-02-20 10:36:25 +00:00
|
|
|
void MainWindow::hideAndDeactivate() {
|
|
|
|
hide();
|
2016-06-16 12:59:54 +00:00
|
|
|
}
|
|
|
|
|
2016-06-16 15:17:39 +00:00
|
|
|
void MainWindow::psShowTrayMenu() {
|
2016-06-16 12:59:54 +00:00
|
|
|
}
|
|
|
|
|
2016-06-16 15:17:39 +00:00
|
|
|
void MainWindow::psTrayMenuUpdated() {
|
2020-02-06 11:34:28 +00:00
|
|
|
// On macOS just remove trayIcon menu if the window is not active.
|
|
|
|
// So we will activate the window on click instead of showing the menu.
|
|
|
|
if (isActive()) {
|
|
|
|
if (trayIcon && trayIconMenu
|
|
|
|
&& trayIcon->contextMenu() != trayIconMenu) {
|
|
|
|
trayIcon->setContextMenu(trayIconMenu);
|
|
|
|
}
|
2020-02-11 06:40:50 +00:00
|
|
|
} else if (trayIcon) {
|
|
|
|
trayIcon->setContextMenu(nullptr);
|
2020-01-31 06:34:37 +00:00
|
|
|
}
|
2016-06-16 12:59:54 +00:00
|
|
|
}
|
|
|
|
|
2016-06-16 15:17:39 +00:00
|
|
|
void MainWindow::psSetupTrayIcon() {
|
|
|
|
if (!trayIcon) {
|
|
|
|
trayIcon = new QSystemTrayIcon(this);
|
2020-11-02 14:13:25 +00:00
|
|
|
trayIcon->setIcon(generateIconForTray(
|
|
|
|
Core::App().unreadBadge(),
|
|
|
|
Core::App().unreadBadgeMuted()));
|
2019-04-12 13:21:01 +00:00
|
|
|
attachToTrayIcon(trayIcon);
|
2020-11-02 14:13:25 +00:00
|
|
|
} else {
|
|
|
|
updateIconCounters();
|
2016-06-16 12:59:54 +00:00
|
|
|
}
|
|
|
|
|
2016-06-16 15:17:39 +00:00
|
|
|
trayIcon->show();
|
2016-06-16 12:59:54 +00:00
|
|
|
}
|
|
|
|
|
2017-03-04 19:36:59 +00:00
|
|
|
void MainWindow::workmodeUpdated(DBIWorkMode mode) {
|
2016-06-16 15:17:39 +00:00
|
|
|
psSetupTrayIcon();
|
2017-03-04 19:36:59 +00:00
|
|
|
if (mode == dbiwmWindowOnly) {
|
2016-06-16 15:17:39 +00:00
|
|
|
if (trayIcon) {
|
|
|
|
trayIcon->setContextMenu(0);
|
|
|
|
delete trayIcon;
|
2016-10-02 17:06:34 +00:00
|
|
|
trayIcon = nullptr;
|
2016-06-16 15:17:39 +00:00
|
|
|
}
|
|
|
|
}
|
2016-06-16 12:59:54 +00:00
|
|
|
}
|
|
|
|
|
2016-12-23 13:21:01 +00:00
|
|
|
void _placeCounter(QImage &img, int size, int count, style::color bg, style::color color) {
|
2016-06-16 15:17:39 +00:00
|
|
|
if (!count) return;
|
2016-12-23 13:21:01 +00:00
|
|
|
auto savedRatio = img.devicePixelRatio();
|
|
|
|
img.setDevicePixelRatio(1.);
|
|
|
|
|
|
|
|
{
|
|
|
|
Painter p(&img);
|
|
|
|
PainterHighQualityEnabler hq(p);
|
|
|
|
|
|
|
|
auto cnt = (count < 100) ? QString("%1").arg(count) : QString("..%1").arg(count % 100, 2, 10, QChar('0'));
|
|
|
|
auto cntSize = cnt.size();
|
|
|
|
|
|
|
|
p.setBrush(bg);
|
|
|
|
p.setPen(Qt::NoPen);
|
|
|
|
int32 fontSize, skip;
|
|
|
|
if (size == 22) {
|
|
|
|
skip = 1;
|
|
|
|
fontSize = 8;
|
|
|
|
} else {
|
|
|
|
skip = 2;
|
|
|
|
fontSize = 16;
|
|
|
|
}
|
|
|
|
style::font f(fontSize, 0, 0);
|
|
|
|
int32 w = f->width(cnt), d, r;
|
|
|
|
if (size == 22) {
|
|
|
|
d = (cntSize < 2) ? 3 : 2;
|
|
|
|
r = (cntSize < 2) ? 6 : 5;
|
|
|
|
} else {
|
|
|
|
d = (cntSize < 2) ? 6 : 5;
|
|
|
|
r = (cntSize < 2) ? 9 : 11;
|
|
|
|
}
|
|
|
|
p.drawRoundedRect(QRect(size - w - d * 2 - skip, size - f->height - skip, w + d * 2, f->height), r, r);
|
2016-06-16 12:59:54 +00:00
|
|
|
|
2016-12-23 13:21:01 +00:00
|
|
|
p.setCompositionMode(QPainter::CompositionMode_Source);
|
|
|
|
p.setFont(f);
|
|
|
|
p.setPen(color);
|
|
|
|
p.drawText(size - w - d - skip, size - f->height + f->ascent - skip, cnt);
|
2016-06-16 12:59:54 +00:00
|
|
|
}
|
2016-12-23 13:21:01 +00:00
|
|
|
img.setDevicePixelRatio(savedRatio);
|
2016-06-16 15:17:39 +00:00
|
|
|
}
|
|
|
|
|
2016-11-09 08:34:38 +00:00
|
|
|
void MainWindow::updateTitleCounter() {
|
2017-11-22 17:32:42 +00:00
|
|
|
_private->setWindowTitle(titleVisible() ? QString() : titleText());
|
2016-11-09 08:34:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MainWindow::unreadCounterChangedHook() {
|
|
|
|
updateTitleCounter();
|
|
|
|
updateIconCounters();
|
|
|
|
}
|
2016-06-16 15:17:39 +00:00
|
|
|
|
2016-11-09 08:34:38 +00:00
|
|
|
void MainWindow::updateIconCounters() {
|
2019-01-21 13:42:21 +00:00
|
|
|
const auto counter = Core::App().unreadBadge();
|
|
|
|
const auto muted = Core::App().unreadBadgeMuted();
|
2016-06-16 15:17:39 +00:00
|
|
|
|
2018-12-04 10:32:06 +00:00
|
|
|
const auto string = !counter
|
|
|
|
? QString()
|
|
|
|
: (counter < 1000)
|
|
|
|
? QString("%1").arg(counter)
|
|
|
|
: QString("..%1").arg(counter % 100, 2, 10, QChar('0'));
|
|
|
|
_private->setWindowBadge(string);
|
2016-06-16 15:17:39 +00:00
|
|
|
|
|
|
|
if (trayIcon) {
|
2020-11-02 14:13:25 +00:00
|
|
|
trayIcon->setIcon(generateIconForTray(counter, muted));
|
2016-06-16 12:59:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-02 14:13:25 +00:00
|
|
|
QIcon MainWindow::generateIconForTray(int counter, bool muted) const {
|
|
|
|
auto result = QIcon();
|
|
|
|
auto lightMode = TrayIconBack(false);
|
|
|
|
auto darkMode = TrayIconBack(true);
|
|
|
|
auto lightModeActive = darkMode;
|
|
|
|
auto darkModeActive = darkMode;
|
|
|
|
lightModeActive.detach();
|
|
|
|
darkModeActive.detach();
|
|
|
|
const auto size = 22 * cIntRetinaFactor();
|
|
|
|
const auto &bg = (muted ? st::trayCounterBgMute : st::trayCounterBg);
|
|
|
|
_placeCounter(lightMode, size, counter, bg, st::trayCounterFg);
|
|
|
|
_placeCounter(darkMode, size, counter, bg, muted ? st::trayCounterFgMacInvert : st::trayCounterFg);
|
|
|
|
_placeCounter(lightModeActive, size, counter, st::trayCounterBgMacInvert, st::trayCounterFgMacInvert);
|
|
|
|
_placeCounter(darkModeActive, size, counter, st::trayCounterBgMacInvert, st::trayCounterFgMacInvert);
|
|
|
|
result.addPixmap(App::pixmapFromImageInPlace(std::move(lightMode)), QIcon::Normal, QIcon::Off);
|
|
|
|
result.addPixmap(App::pixmapFromImageInPlace(std::move(darkMode)), QIcon::Normal, QIcon::On);
|
|
|
|
result.addPixmap(App::pixmapFromImageInPlace(std::move(lightModeActive)), QIcon::Active, QIcon::Off);
|
|
|
|
result.addPixmap(App::pixmapFromImageInPlace(std::move(darkModeActive)), QIcon::Active, QIcon::On);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-02-12 08:09:17 +00:00
|
|
|
void MainWindow::initShadows() {
|
2016-11-08 14:07:25 +00:00
|
|
|
_private->enableShadow(winId());
|
2016-11-04 11:14:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MainWindow::createGlobalMenu() {
|
2021-02-03 01:17:03 +00:00
|
|
|
const auto ensureWindowShown = [=] {
|
|
|
|
if (isHidden()) {
|
|
|
|
showFromTray();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-11-04 08:23:50 +00:00
|
|
|
auto main = psMainMenu.addMenu(qsl("Telegram"));
|
2021-02-03 02:55:01 +00:00
|
|
|
{
|
|
|
|
auto callback = [=] {
|
|
|
|
ensureWindowShown();
|
|
|
|
controller().show(Box<AboutBox>());
|
|
|
|
};
|
|
|
|
main->addAction(
|
|
|
|
tr::lng_mac_menu_about_telegram(
|
|
|
|
tr::now,
|
|
|
|
lt_telegram,
|
|
|
|
qsl("Telegram")),
|
|
|
|
std::move(callback))
|
|
|
|
->setMenuRole(QAction::AboutQtRole);
|
|
|
|
}
|
2016-11-04 08:23:50 +00:00
|
|
|
|
2016-06-16 15:17:39 +00:00
|
|
|
main->addSeparator();
|
2021-02-03 01:58:27 +00:00
|
|
|
{
|
|
|
|
auto callback = [=] {
|
|
|
|
ensureWindowShown();
|
|
|
|
controller().showSettings();
|
|
|
|
};
|
|
|
|
main->addAction(
|
|
|
|
tr::lng_mac_menu_preferences(tr::now),
|
|
|
|
this,
|
|
|
|
std::move(callback),
|
|
|
|
QKeySequence(Qt::ControlModifier | Qt::Key_Comma))
|
|
|
|
->setMenuRole(QAction::PreferencesRole);
|
|
|
|
}
|
2016-06-16 15:17:39 +00:00
|
|
|
|
2019-06-19 15:09:03 +00:00
|
|
|
QMenu *file = psMainMenu.addMenu(tr::lng_mac_menu_file(tr::now));
|
2021-02-03 02:49:26 +00:00
|
|
|
{
|
|
|
|
auto callback = [=] {
|
|
|
|
ensureWindowShown();
|
|
|
|
controller().showLogoutConfirmation();
|
|
|
|
};
|
|
|
|
psLogout = file->addAction(
|
|
|
|
tr::lng_mac_menu_logout(tr::now),
|
|
|
|
this,
|
|
|
|
std::move(callback));
|
|
|
|
}
|
2016-06-16 15:17:39 +00:00
|
|
|
|
2019-06-19 15:09:03 +00:00
|
|
|
QMenu *edit = psMainMenu.addMenu(tr::lng_mac_menu_edit(tr::now));
|
|
|
|
psUndo = edit->addAction(tr::lng_mac_menu_undo(tr::now), this, SLOT(psMacUndo()), QKeySequence::Undo);
|
|
|
|
psRedo = edit->addAction(tr::lng_mac_menu_redo(tr::now), this, SLOT(psMacRedo()), QKeySequence::Redo);
|
2016-06-16 15:17:39 +00:00
|
|
|
edit->addSeparator();
|
2019-06-19 15:09:03 +00:00
|
|
|
psCut = edit->addAction(tr::lng_mac_menu_cut(tr::now), this, SLOT(psMacCut()), QKeySequence::Cut);
|
|
|
|
psCopy = edit->addAction(tr::lng_mac_menu_copy(tr::now), this, SLOT(psMacCopy()), QKeySequence::Copy);
|
|
|
|
psPaste = edit->addAction(tr::lng_mac_menu_paste(tr::now), this, SLOT(psMacPaste()), QKeySequence::Paste);
|
|
|
|
psDelete = edit->addAction(tr::lng_mac_menu_delete(tr::now), this, SLOT(psMacDelete()), QKeySequence(Qt::ControlModifier | Qt::Key_Backspace));
|
2019-08-11 16:27:59 +00:00
|
|
|
|
|
|
|
edit->addSeparator();
|
2019-08-20 11:29:21 +00:00
|
|
|
psBold = edit->addAction(tr::lng_menu_formatting_bold(tr::now), this, SLOT(psMacBold()), QKeySequence::Bold);
|
|
|
|
psItalic = edit->addAction(tr::lng_menu_formatting_italic(tr::now), this, SLOT(psMacItalic()), QKeySequence::Italic);
|
|
|
|
psUnderline = edit->addAction(tr::lng_menu_formatting_underline(tr::now), this, SLOT(psMacUnderline()), QKeySequence::Underline);
|
|
|
|
psStrikeOut = edit->addAction(tr::lng_menu_formatting_strike_out(tr::now), this, SLOT(psMacStrikeOut()), Ui::kStrikeOutSequence);
|
|
|
|
psMonospace = edit->addAction(tr::lng_menu_formatting_monospace(tr::now), this, SLOT(psMacMonospace()), Ui::kMonospaceSequence);
|
|
|
|
psClearFormat = edit->addAction(tr::lng_menu_formatting_clear(tr::now), this, SLOT(psMacClearFormat()), Ui::kClearFormatSequence);
|
2019-08-11 16:27:59 +00:00
|
|
|
|
2016-06-16 15:17:39 +00:00
|
|
|
edit->addSeparator();
|
2019-06-19 15:09:03 +00:00
|
|
|
psSelectAll = edit->addAction(tr::lng_mac_menu_select_all(tr::now), this, SLOT(psMacSelectAll()), QKeySequence::SelectAll);
|
2016-06-16 15:17:39 +00:00
|
|
|
|
2019-11-26 21:07:03 +00:00
|
|
|
edit->addSeparator();
|
|
|
|
edit->addAction(tr::lng_mac_menu_emoji_and_symbols(tr::now).replace('&', "&&"), this, SLOT(psMacEmojiAndSymbols()), QKeySequence(Qt::MetaModifier | Qt::ControlModifier | Qt::Key_Space));
|
|
|
|
|
2019-06-19 15:09:03 +00:00
|
|
|
QMenu *window = psMainMenu.addMenu(tr::lng_mac_menu_window(tr::now));
|
|
|
|
psContacts = window->addAction(tr::lng_mac_menu_contacts(tr::now));
|
2019-06-06 11:59:00 +00:00
|
|
|
connect(psContacts, &QAction::triggered, psContacts, crl::guard(this, [=] {
|
|
|
|
if (isHidden()) {
|
2021-01-22 12:16:18 +00:00
|
|
|
showFromTray();
|
2019-06-06 11:59:00 +00:00
|
|
|
}
|
2020-06-26 07:51:05 +00:00
|
|
|
if (!sessionController()) {
|
2019-06-06 11:59:00 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-10-07 17:08:50 +00:00
|
|
|
Ui::show(PrepareContactsBox(sessionController()));
|
2019-06-06 12:01:01 +00:00
|
|
|
}));
|
2021-02-03 01:17:03 +00:00
|
|
|
{
|
|
|
|
auto callback = [=] {
|
|
|
|
Expects(sessionController() != nullptr);
|
|
|
|
ensureWindowShown();
|
|
|
|
sessionController()->showAddContact();
|
|
|
|
};
|
|
|
|
psAddContact = window->addAction(
|
|
|
|
tr::lng_mac_menu_add_contact(tr::now),
|
|
|
|
this,
|
|
|
|
std::move(callback));
|
|
|
|
}
|
2016-06-16 15:17:39 +00:00
|
|
|
window->addSeparator();
|
2021-02-03 01:17:03 +00:00
|
|
|
{
|
|
|
|
auto callback = [=] {
|
|
|
|
Expects(sessionController() != nullptr);
|
|
|
|
ensureWindowShown();
|
|
|
|
sessionController()->showNewGroup();
|
|
|
|
};
|
|
|
|
psNewGroup = window->addAction(
|
|
|
|
tr::lng_mac_menu_new_group(tr::now),
|
|
|
|
this,
|
|
|
|
std::move(callback));
|
|
|
|
}
|
|
|
|
{
|
|
|
|
auto callback = [=] {
|
|
|
|
Expects(sessionController() != nullptr);
|
|
|
|
ensureWindowShown();
|
|
|
|
sessionController()->showNewChannel();
|
|
|
|
};
|
|
|
|
psNewChannel = window->addAction(
|
|
|
|
tr::lng_mac_menu_new_channel(tr::now),
|
|
|
|
this,
|
|
|
|
std::move(callback));
|
|
|
|
}
|
2018-10-05 18:33:17 +00:00
|
|
|
window->addSeparator();
|
2021-02-03 02:58:59 +00:00
|
|
|
psShowTelegram = window->addAction(
|
|
|
|
tr::lng_mac_menu_show(tr::now),
|
|
|
|
this,
|
|
|
|
[=] { showFromTray(); });
|
2017-01-01 16:45:20 +00:00
|
|
|
|
|
|
|
updateGlobalMenu();
|
2016-06-16 12:59:54 +00:00
|
|
|
}
|
|
|
|
|
2016-06-16 15:17:39 +00:00
|
|
|
void MainWindow::psMacUndo() {
|
2019-08-20 11:29:21 +00:00
|
|
|
SendKeySequence(Qt::Key_Z, Qt::ControlModifier);
|
2016-06-16 12:59:54 +00:00
|
|
|
}
|
|
|
|
|
2016-06-16 15:17:39 +00:00
|
|
|
void MainWindow::psMacRedo() {
|
2019-08-20 11:29:21 +00:00
|
|
|
SendKeySequence(Qt::Key_Z, Qt::ControlModifier | Qt::ShiftModifier);
|
2016-06-16 12:59:54 +00:00
|
|
|
}
|
|
|
|
|
2016-06-16 15:17:39 +00:00
|
|
|
void MainWindow::psMacCut() {
|
2019-08-20 11:29:21 +00:00
|
|
|
SendKeySequence(Qt::Key_X, Qt::ControlModifier);
|
2016-06-16 12:59:54 +00:00
|
|
|
}
|
|
|
|
|
2016-06-16 15:17:39 +00:00
|
|
|
void MainWindow::psMacCopy() {
|
2019-08-20 11:29:21 +00:00
|
|
|
SendKeySequence(Qt::Key_C, Qt::ControlModifier);
|
2016-06-16 12:59:54 +00:00
|
|
|
}
|
|
|
|
|
2016-06-16 15:17:39 +00:00
|
|
|
void MainWindow::psMacPaste() {
|
2019-08-20 11:29:21 +00:00
|
|
|
SendKeySequence(Qt::Key_V, Qt::ControlModifier);
|
2016-06-16 12:59:54 +00:00
|
|
|
}
|
|
|
|
|
2016-06-16 15:17:39 +00:00
|
|
|
void MainWindow::psMacDelete() {
|
2019-08-20 11:29:21 +00:00
|
|
|
SendKeySequence(Qt::Key_Delete);
|
2016-06-16 12:59:54 +00:00
|
|
|
}
|
|
|
|
|
2016-06-16 15:17:39 +00:00
|
|
|
void MainWindow::psMacSelectAll() {
|
2019-08-20 11:29:21 +00:00
|
|
|
SendKeySequence(Qt::Key_A, Qt::ControlModifier);
|
|
|
|
}
|
|
|
|
|
2019-11-26 21:07:03 +00:00
|
|
|
void MainWindow::psMacEmojiAndSymbols() {
|
|
|
|
[NSApp orderFrontCharacterPalette:nil];
|
|
|
|
}
|
|
|
|
|
2019-08-20 11:29:21 +00:00
|
|
|
void MainWindow::psMacBold() {
|
|
|
|
SendKeySequence(Qt::Key_B, Qt::ControlModifier);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MainWindow::psMacItalic() {
|
|
|
|
SendKeySequence(Qt::Key_I, Qt::ControlModifier);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MainWindow::psMacUnderline() {
|
|
|
|
SendKeySequence(Qt::Key_U, Qt::ControlModifier);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MainWindow::psMacStrikeOut() {
|
|
|
|
SendKeySequence(Qt::Key_X, Qt::ControlModifier | Qt::ShiftModifier);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MainWindow::psMacMonospace() {
|
|
|
|
SendKeySequence(Qt::Key_M, Qt::ControlModifier | Qt::ShiftModifier);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MainWindow::psMacClearFormat() {
|
|
|
|
SendKeySequence(Qt::Key_N, Qt::ControlModifier | Qt::ShiftModifier);
|
2016-06-16 12:59:54 +00:00
|
|
|
}
|
|
|
|
|
2017-01-01 16:45:20 +00:00
|
|
|
void MainWindow::updateGlobalMenuHook() {
|
2021-02-03 02:58:59 +00:00
|
|
|
if (!positionInited()) {
|
|
|
|
return;
|
|
|
|
}
|
2016-06-16 12:59:54 +00:00
|
|
|
|
2017-01-01 16:45:20 +00:00
|
|
|
auto focused = QApplication::focusWidget();
|
2018-06-03 13:30:40 +00:00
|
|
|
bool canUndo = false, canRedo = false, canCut = false, canCopy = false, canPaste = false, canDelete = false, canSelectAll = false;
|
2017-03-05 20:18:27 +00:00
|
|
|
auto clipboardHasText = _private->clipboardHasText();
|
2020-07-09 21:12:06 +00:00
|
|
|
auto canApplyMarkdown = false;
|
2016-11-04 11:14:47 +00:00
|
|
|
if (auto edit = qobject_cast<QLineEdit*>(focused)) {
|
2016-06-16 15:17:39 +00:00
|
|
|
canCut = canCopy = canDelete = edit->hasSelectedText();
|
|
|
|
canSelectAll = !edit->text().isEmpty();
|
|
|
|
canUndo = edit->isUndoAvailable();
|
|
|
|
canRedo = edit->isRedoAvailable();
|
2017-03-05 20:18:27 +00:00
|
|
|
canPaste = clipboardHasText;
|
2016-11-15 11:56:49 +00:00
|
|
|
} else if (auto edit = qobject_cast<QTextEdit*>(focused)) {
|
2016-06-16 15:17:39 +00:00
|
|
|
canCut = canCopy = canDelete = edit->textCursor().hasSelection();
|
2016-11-15 11:56:49 +00:00
|
|
|
canSelectAll = !edit->document()->isEmpty();
|
|
|
|
canUndo = edit->document()->isUndoAvailable();
|
|
|
|
canRedo = edit->document()->isRedoAvailable();
|
2017-03-05 20:18:27 +00:00
|
|
|
canPaste = clipboardHasText;
|
2019-06-10 21:39:58 +00:00
|
|
|
if (canCopy) {
|
2020-07-09 21:12:06 +00:00
|
|
|
if (const auto inputField = qobject_cast<Ui::InputField*>(
|
|
|
|
focused->parentWidget())) {
|
|
|
|
canApplyMarkdown = inputField->isMarkdownEnabled();
|
2019-06-10 21:39:58 +00:00
|
|
|
}
|
|
|
|
}
|
2016-11-04 11:14:47 +00:00
|
|
|
} else if (auto list = qobject_cast<HistoryInner*>(focused)) {
|
2016-06-16 15:17:39 +00:00
|
|
|
canCopy = list->canCopySelected();
|
|
|
|
canDelete = list->canDeleteSelected();
|
2016-06-16 12:59:54 +00:00
|
|
|
}
|
2020-06-29 20:05:27 +00:00
|
|
|
|
2020-07-09 21:12:06 +00:00
|
|
|
_canApplyMarkdown = canApplyMarkdown;
|
2020-06-29 20:05:27 +00:00
|
|
|
|
2021-02-03 02:58:59 +00:00
|
|
|
updateIsActive();
|
2020-06-26 07:51:05 +00:00
|
|
|
const auto logged = (sessionController() != nullptr);
|
2020-06-24 07:56:16 +00:00
|
|
|
const auto inactive = !logged || controller().locked();
|
2019-06-06 11:59:00 +00:00
|
|
|
const auto support = logged && account().session().supportMode();
|
2020-06-24 07:56:16 +00:00
|
|
|
ForceDisabled(psLogout, !logged && !Core::App().passcodeLocked());
|
2019-08-20 11:29:21 +00:00
|
|
|
ForceDisabled(psUndo, !canUndo);
|
|
|
|
ForceDisabled(psRedo, !canRedo);
|
|
|
|
ForceDisabled(psCut, !canCut);
|
|
|
|
ForceDisabled(psCopy, !canCopy);
|
|
|
|
ForceDisabled(psPaste, !canPaste);
|
|
|
|
ForceDisabled(psDelete, !canDelete);
|
|
|
|
ForceDisabled(psSelectAll, !canSelectAll);
|
|
|
|
ForceDisabled(psContacts, inactive || support);
|
|
|
|
ForceDisabled(psAddContact, inactive);
|
|
|
|
ForceDisabled(psNewGroup, inactive || support);
|
|
|
|
ForceDisabled(psNewChannel, inactive || support);
|
2021-02-03 02:58:59 +00:00
|
|
|
ForceDisabled(psShowTelegram, isActive());
|
2019-08-20 11:29:21 +00:00
|
|
|
|
2020-07-09 21:12:06 +00:00
|
|
|
ForceDisabled(psBold, !canApplyMarkdown);
|
|
|
|
ForceDisabled(psItalic, !canApplyMarkdown);
|
|
|
|
ForceDisabled(psUnderline, !canApplyMarkdown);
|
|
|
|
ForceDisabled(psStrikeOut, !canApplyMarkdown);
|
|
|
|
ForceDisabled(psMonospace, !canApplyMarkdown);
|
|
|
|
ForceDisabled(psClearFormat, !canApplyMarkdown);
|
2016-06-16 12:59:54 +00:00
|
|
|
}
|
|
|
|
|
2016-06-16 15:17:39 +00:00
|
|
|
bool MainWindow::psFilterNativeEvent(void *event) {
|
2016-11-08 14:07:25 +00:00
|
|
|
return _private->filterNativeEvent(event);
|
2016-06-16 12:59:54 +00:00
|
|
|
}
|
|
|
|
|
2016-06-16 15:17:39 +00:00
|
|
|
bool MainWindow::eventFilter(QObject *obj, QEvent *evt) {
|
|
|
|
QEvent::Type t = evt->type();
|
|
|
|
if (t == QEvent::FocusIn || t == QEvent::FocusOut) {
|
2016-11-15 11:56:49 +00:00
|
|
|
if (qobject_cast<QLineEdit*>(obj) || qobject_cast<QTextEdit*>(obj) || qobject_cast<HistoryInner*>(obj)) {
|
2017-01-01 16:45:20 +00:00
|
|
|
updateGlobalMenu();
|
2016-06-16 15:17:39 +00:00
|
|
|
}
|
2016-06-16 12:59:54 +00:00
|
|
|
}
|
2016-06-16 15:17:39 +00:00
|
|
|
return Window::MainWindow::eventFilter(obj, evt);
|
2016-06-16 12:59:54 +00:00
|
|
|
}
|
|
|
|
|
2016-06-16 15:17:39 +00:00
|
|
|
MainWindow::~MainWindow() {
|
2016-06-16 12:59:54 +00:00
|
|
|
}
|
|
|
|
|
2016-06-16 15:17:39 +00:00
|
|
|
} // namespace
|