Skip to content

Comments

Install Return shortcut on dialogs#68

Closed
lynn wants to merge 5 commits intojorio:masterfrom
lynn:lynn/return
Closed

Install Return shortcut on dialogs#68
lynn wants to merge 5 commits intojorio:masterfrom
lynn:lynn/return

Conversation

@lynn
Copy link
Contributor

@lynn lynn commented Sep 4, 2025

Second take on #66. Apparently Return is supposed to work on these dialogues, but it doesn't quite work on GNOME. This makes it work everywhere.

I added both Return and Ctrl+Return. It seems like Qt cleverly figures out that Return in a multi-line text box (such as a long commit message) should not activate a shortcut on the parent widget, and in this case, it's nice for Ctrl+Return to also work.

@jorio, do you know if we can test shortcuts?

Copy link
Owner

@jorio jorio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's a test for Ctrl+Return in CommitDialog:
(Tip: you can use test.py --visual -k nameofyourtest and sprinkle the test with pause() calls to see what you're doing as you're writing your unit test)

def testCommitDialogCtrlReturn(tempDir, mainWindow):
    wd = unpackRepo(tempDir)
    rw = mainWindow.openRepo(wd)

    triggerMenuAction(mainWindow.menuBar(), r"repo/commit")
    acceptQMessageBox(rw, "create an empty commit anyway")

    commitDialog: CommitDialog = findQDialog(rw, r"commit")

    # Widget focus is finicky in offscreen tests unless we force the dialog to be active first
    commitDialog.activateWindow()
    waitUntilTrue(commitDialog.isActiveWindow)

    commitDialog.ui.summaryEditor.setText("hello from ctrl+return")

    # Make sure the shortcut works even when a QPlainTextEdit (multiline editor) has focus
    commitDialog.ui.descriptionEditor.setFocus()
    assert commitDialog.ui.descriptionEditor.hasFocus()
    QTest.keySequence(commitDialog.focusWidget(), "Ctrl+Return")

    assert rw.repo.head_commit_message.strip() == "hello from ctrl+return"

I don't know if it's worth testing just "Return", because offscreen tests behave like KDE regardless of the host system's environment, so it wouldn't make a difference.

@jorio
Copy link
Owner

jorio commented Sep 5, 2025

The test appears to be flaky in offscreen mode, but I think this will solve it:
Pass makeWidgetShortcut(..., context=Qt.ShortcutContext.WindowShortcut)
And then waitForSignal(commitDialog.destroyed) might not be necessary.

(Some of the CI jobs failed because of outdated APT packages on the CI runners, that's an unrelated problem)

@jorio
Copy link
Owner

jorio commented Sep 5, 2025

This rabbit hole keeps getting deeper...

I never noticed before, but KDE already lets you click "OK" by hitting Ctrl+Return, in any dialog, in any Qt application! KDE seems to be adding a QShortcut("Ctrl+Return") to any dialog's default button after QDialog.show().

The kicker is, if create our own Ctrl+Return shortcut on top of that, it will conflict with KDE's own shortcut. Pressing Ctrl+Return will emit the activatedAmbiguously signal instead of activated and nothing will happen!

My suggestion is to move the installDialogReturnShortcut calls after dialog.show() so KDE gets to install its shortcuts first. Then, in installDialogReturnShortcut, see if okButton has any shortcuts already. If it does, we're likely running on KDE so we can just bail. For example:

    if okButton.findChildren(QShortcut):  # Don't conflict with KDE's shortcuts
        return

Does this approach still work in GNOME?

jorio pushed a commit that referenced this pull request Sep 7, 2025
@jorio
Copy link
Owner

jorio commented Sep 7, 2025

I merged your changes manually and made a few tweaks for KDE compat. Sorry for hijacking the PR and thanks for your contribution!

@jorio jorio closed this Sep 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants