Don't get cursor position twice for open with menu

This commit is contained in:
Ilya Fedin 2023-01-20 09:16:51 +04:00 committed by John Preston
parent a65e25b8ae
commit 6f89413c76
7 changed files with 12 additions and 23 deletions

View file

@ -138,9 +138,9 @@ void OpenEmailLink(const QString &email) {
}); });
} }
void OpenWith(const QString &filepath, QPoint menuPosition) { void OpenWith(const QString &filepath) {
InvokeQueued(QCoreApplication::instance(), [=] { InvokeQueued(QCoreApplication::instance(), [=] {
if (!Platform::File::UnsafeShowOpenWithDropdown(filepath, menuPosition)) { if (!Platform::File::UnsafeShowOpenWithDropdown(filepath)) {
Ui::PreventDelayedActivation(); Ui::PreventDelayedActivation();
if (!Platform::File::UnsafeShowOpenWith(filepath)) { if (!Platform::File::UnsafeShowOpenWith(filepath)) {
Platform::File::UnsafeLaunch(filepath); Platform::File::UnsafeLaunch(filepath);

View file

@ -34,7 +34,7 @@ namespace File {
// Those functions are async wrappers to Platform::File::Unsafe* calls. // Those functions are async wrappers to Platform::File::Unsafe* calls.
void OpenUrl(const QString &url); void OpenUrl(const QString &url);
void OpenEmailLink(const QString &email); void OpenEmailLink(const QString &email);
void OpenWith(const QString &filepath, QPoint menuPosition); void OpenWith(const QString &filepath);
void Launch(const QString &filepath); void Launch(const QString &filepath);
void ShowInFolder(const QString &filepath); void ShowInFolder(const QString &filepath);

View file

@ -152,7 +152,7 @@ void DocumentOpenWithClickHandler::Open(
data->saveFromDataSilent(); data->saveFromDataSilent();
const auto path = data->filepath(true); const auto path = data->filepath(true);
if (!path.isEmpty()) { if (!path.isEmpty()) {
File::OpenWith(path, QCursor::pos()); File::OpenWith(path);
} else { } else {
DocumentSaveClickHandler::Save( DocumentSaveClickHandler::Save(
origin, origin,

View file

@ -16,7 +16,7 @@ inline QString UrlToLocal(const QUrl &url) {
return ::File::internal::UrlToLocalDefault(url); return ::File::internal::UrlToLocalDefault(url);
} }
inline bool UnsafeShowOpenWithDropdown(const QString &filepath, QPoint menuPosition) { inline bool UnsafeShowOpenWithDropdown(const QString &filepath) {
return false; return false;
} }

View file

@ -11,9 +11,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "lang/lang_keys.h" #include "lang/lang_keys.h"
#include "styles/style_window.h" #include "styles/style_window.h"
#include <QtWidgets/QApplication>
#include <QtGui/QScreen>
#include <Cocoa/Cocoa.h> #include <Cocoa/Cocoa.h>
#include <CoreFoundation/CFURL.h> #include <CoreFoundation/CFURL.h>
@ -68,7 +65,7 @@ QString strNeedToRefresh2() {
} }
- (id) init:(NSString *)file; - (id) init:(NSString *)file;
- (BOOL) popupAtX:(int)x andY:(int)y; - (BOOL) popupAt:(NSPoint)point;
- (void) itemChosen:(id)sender; - (void) itemChosen:(id)sender;
- (void) dealloc; - (void) dealloc;
@ -174,7 +171,7 @@ QString strNeedToRefresh2() {
return self; return self;
} }
- (BOOL) popupAtX:(int)x andY:(int)y { - (BOOL) popupAt:(NSPoint)point {
if (![apps count] && !defName) return NO; if (![apps count] && !defName) return NO;
menu = [[NSMenu alloc] initWithTitle:@"Open With"]; menu = [[NSMenu alloc] initWithTitle:@"Open With"];
@ -197,7 +194,7 @@ QString strNeedToRefresh2() {
NSMenuItem *item = [menu insertItemWithTitle:Q2NSString(tr::lng_mac_choose_program_menu(tr::now)) action:@selector(itemChosen:) keyEquivalent:@"" atIndex:index++]; NSMenuItem *item = [menu insertItemWithTitle:Q2NSString(tr::lng_mac_choose_program_menu(tr::now)) action:@selector(itemChosen:) keyEquivalent:@"" atIndex:index++];
[item setTarget:self]; [item setTarget:self];
[menu popUpMenuPositioningItem:nil atLocation:CGPointMake(x, y) inView:nil]; [menu popUpMenuPositioningItem:nil atLocation:point inView:nil];
return YES; return YES;
} }
@ -392,20 +389,13 @@ QString UrlToLocal(const QUrl &url) {
return result; return result;
} }
bool UnsafeShowOpenWithDropdown(const QString &filepath, QPoint menuPosition) { bool UnsafeShowOpenWithDropdown(const QString &filepath) {
@autoreleasepool { @autoreleasepool {
NSString *file = Q2NSString(filepath); NSString *file = Q2NSString(filepath);
@try { @try {
OpenFileWithInterface *menu = [[[OpenFileWithInterface alloc] init:file] autorelease]; OpenFileWithInterface *menu = [[[OpenFileWithInterface alloc] init:file] autorelease];
const auto screen = QGuiApplication::screenAt(menuPosition); return !![menu popupAt:[NSEvent mouseLocation]];
if (!screen) {
return false;
}
const auto r = screen->geometry();
auto x = menuPosition.x();
auto y = r.y() + r.height() - menuPosition.y();
return !![menu popupAtX:x andY:y];
} }
@catch (NSException *exception) { @catch (NSException *exception) {
} }

View file

@ -17,7 +17,7 @@ QString UrlToLocal(const QUrl &url);
// All these functions may enter a nested event loop. Use with caution. // All these functions may enter a nested event loop. Use with caution.
void UnsafeOpenUrl(const QString &url); void UnsafeOpenUrl(const QString &url);
void UnsafeOpenEmailLink(const QString &email); void UnsafeOpenEmailLink(const QString &email);
bool UnsafeShowOpenWithDropdown(const QString &filepath, QPoint menuPosition); bool UnsafeShowOpenWithDropdown(const QString &filepath);
bool UnsafeShowOpenWith(const QString &filepath); bool UnsafeShowOpenWith(const QString &filepath);
void UnsafeLaunch(const QString &filepath); void UnsafeLaunch(const QString &filepath);

View file

@ -142,7 +142,7 @@ void UnsafeOpenEmailLink(const QString &email) {
} }
} }
bool UnsafeShowOpenWithDropdown(const QString &filepath, QPoint menuPosition) { bool UnsafeShowOpenWithDropdown(const QString &filepath) {
if (!Dlls::SHAssocEnumHandlers || !Dlls::SHCreateItemFromParsingName) { if (!Dlls::SHAssocEnumHandlers || !Dlls::SHCreateItemFromParsingName) {
return false; return false;
} }
@ -230,7 +230,6 @@ bool UnsafeShowOpenWithDropdown(const QString &filepath, QPoint menuPosition) {
menuInfo.dwTypeData = nameArr; menuInfo.dwTypeData = nameArr;
InsertMenuItem(menu, GetMenuItemCount(menu), TRUE, &menuInfo); InsertMenuItem(menu, GetMenuItemCount(menu), TRUE, &menuInfo);
// menuPosition is incorrect because of devicePixelRatio :(
POINT position; POINT position;
GetCursorPos(&position); GetCursorPos(&position);
int sel = TrackPopupMenu(menu, TPM_LEFTALIGN | TPM_TOPALIGN | TPM_LEFTBUTTON | TPM_RETURNCMD, position.x, position.y, 0, parentHWND, 0); int sel = TrackPopupMenu(menu, TPM_LEFTALIGN | TPM_TOPALIGN | TPM_LEFTBUTTON | TPM_RETURNCMD, position.x, position.y, 0, parentHWND, 0);