Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions app/qml/Main.qml
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,11 @@ ApplicationWindow {
function onReopenRequested() {
if (!mailApp.opened) shellAdapter.summon(manifestAdapter.value.id, "{}")
}
// The host saw the platform close chord before Qt did — on macOS it takes
// Cmd+W off the event stream ahead of the input method, which otherwise
// swallows the chord while a composition is pending. Same exit as the
// Shortcut below.
function onCloseRequested() { mailApp.requestClose() }
}

Connections {
Expand Down
5 changes: 5 additions & 0 deletions app/src/application_host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,11 @@ void ApplicationHost::hide()
emit hideRequested();
}

void ApplicationHost::requestClose()
{
emit closeRequested();
}

void ApplicationHost::quit()
{
QCoreApplication::quit();
Expand Down
4 changes: 4 additions & 0 deletions app/src/application_host.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ class ApplicationHost : public QObject {
Q_INVOKABLE QVariantMap takePendingNotificationActivation();
Q_INVOKABLE void hide();
Q_INVOKABLE void quit();
// The platform close chord arrived at the native layer, ahead of Qt's key
// delivery. The window decides what leaving means; the host only relays.
void requestClose();
// The application became active while every window was hidden — a Dock
// or taskbar click on a process whose window was shut.
void handleApplicationStateChanged(Qt::ApplicationState state);
Expand All @@ -65,6 +68,7 @@ class ApplicationHost : public QObject {
void settingsChanged();
void hideRequested();
void reopenRequested();
void closeRequested();
void notificationErrorChanged();
void pendingNotificationActivationChanged();
void notificationActivated(const QString &accountId, const QString &messageId);
Expand Down
7 changes: 6 additions & 1 deletion app/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "app_icon.h"
#include "application_host.h"
#include "notifications.h"
#include "resource_check.h"
#include "window.h"
Expand Down Expand Up @@ -96,9 +97,13 @@ int main(int argc, char *argv[])
: QUrl::fromLocalFile(paths.standaloneQml);
engine.load(mainUrl);
if (engine.rootObjects().isEmpty()) return 1;
auto *host = engine.singletonInstance<ApplicationHost *>(
QStringLiteral("Omamail.Native"), QStringLiteral("NativeHost"));
for (QObject *object : engine.rootObjects()) {
if (auto *window = qobject_cast<QWindow *>(object))
if (auto *window = qobject_cast<QWindow *>(object)) {
prepareNativeWindow(window);
if (host) watchNativeCloseChord(window, [host] { host->requestClose(); });
}
}
return application.exec();
}
12 changes: 12 additions & 0 deletions app/src/window.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
#pragma once

#include <functional>

class QWindow;

// Platform window setup that QML cannot reach. On macOS this keeps the
// existing backing store during live move/resize so the window server does
// not wait on a full Qt Quick frame for every pointer event.
void prepareNativeWindow(QWindow *window);

// Watch for the platform close chord on `window` at the native layer and call
// `onClose` in its place. On macOS Cocoa hands Cmd+W to Qt through keyDown:,
// after the key window's performKeyEquivalent: (which Qt answers only for
// Ctrl+Tab) and the main menu. Qt then tries it as a shortcut only while no
// input method composition is pending; otherwise the chord goes to the input
// method and is dropped or delivered as plain text. A native app's Close is a
// menu key equivalent and wins before any of that, which is what this
// restores. Elsewhere this is a no-op: the QML Shortcut is enough.
void watchNativeCloseChord(QWindow *window, std::function<void()> onClose);
25 changes: 25 additions & 0 deletions app/src/window_macos.mm
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "window.h"

#include <QPointer>
#include <QWindow>

#import <AppKit/AppKit.h>
Expand All @@ -13,3 +14,27 @@ void prepareNativeWindow(QWindow *window)
if (!nativeWindow) return;
nativeWindow.preservesContentDuringLiveResize = YES;
}

void watchNativeCloseChord(QWindow *window, std::function<void()> onClose)
{
if (!window || !onClose) return;
QPointer<QWindow> watched(window);
[NSEvent addLocalMonitorForEventsMatchingMask:NSEventMaskKeyDown
handler:^NSEvent *(NSEvent *event) {
if (!watched) return event;
// Cmd+W and nothing else held; Caps Lock does not change a chord.
const NSEventModifierFlags flags = event.modifierFlags
& NSEventModifierFlagDeviceIndependentFlagsMask
& ~NSEventModifierFlagCapsLock;
if (flags != NSEventModifierFlagCommand) return event;
if (![[event.charactersIgnoringModifiers lowercaseString] isEqualToString:@"w"])
return event;
// Only the watched window's own chord: a panel or another window that
// is key keeps its Cmd+W, the way a menu's Close is validated against
// the responder chain.
auto *view = reinterpret_cast<NSView *>(watched->winId());
if (!view || !view.window || NSApp.keyWindow != view.window) return event;
onClose();
return nil;
}];
}
2 changes: 2 additions & 0 deletions app/src/window_stub.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include "window.h"

void prepareNativeWindow(QWindow *) {}

void watchNativeCloseChord(QWindow *, std::function<void()>) {}
1 change: 1 addition & 0 deletions app/tests/qml/HostFixture.qml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ QtObject {
property var watched: ({})
signal notificationActivated(string accountId, string messageId)
signal reopenRequested()
signal closeRequested()
signal changed(string path)
signal failed(string path, string error)

Expand Down
38 changes: 38 additions & 0 deletions app/tests/qml/tst_standalone_composition.qml
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,44 @@ TestCase {
assertHiddenNotQuit(composition)
}

function test_close_chord_stays_live_while_the_app_menu_is_open() {
var composition = createTemporaryObject(compositionComponent, testCase)
verify(composition)
verify(composition.app.opened)
var menu = findChild(composition, "app-menu")
verify(menu, "the composed app must expose the window menu")
activateComposition(composition)
menu.openAt(10, 10)
tryCompare(menu, "opened", true)
keySequence(StandardKey.Close)
assertHiddenNotQuit(composition)
}

function test_close_chord_stays_live_while_a_tooltip_is_showing() {
var composition = createTemporaryObject(compositionComponent, testCase)
verify(composition)
verify(composition.app.opened)
var tip = findChild(composition, "omamail-tooltip")
verify(tip, "the composed app must show a styled tooltip somewhere")
activateComposition(composition)
tip.visible = true
tryCompare(tip, "opened", true, Style.tooltipDelay + 1000)
keySequence(StandardKey.Close)
assertHiddenNotQuit(composition)
}

// macOS delivers Cmd+W to Qt only after the input method has had it, so
// the native host catches the chord itself and asks the window to leave.
function test_native_close_chord_from_the_host_shuts_the_window() {
var composition = createTemporaryObject(compositionComponent, testCase)
verify(composition)
verify(composition.app.opened)
host.closeRequested()
assertHiddenNotQuit(composition)
host.reopenRequested()
compare(composition.app.opened, true)
}

function test_close_chord_stays_live_in_calendar_context() {
var composition = createTemporaryObject(compositionComponent, testCase)
verify(composition)
Expand Down
Loading