KeyboardShortcuts icon indicating copy to clipboard operation
KeyboardShortcuts copied to clipboard

Investigating why RecorderCocoa field's close button not working for me

Open jpmhouston opened this issue 9 months ago • 1 comments

In RecorderCocoa.swift, is this return nil why clicking the "x" button in a KeyboardShortcuts.RecorderCocoa view doesn't work in my app? I'm just talking about when the field is focussed, LocalEventMonitor is picking up the click on the button and it seems to end up ignored (although when the field isn't focussed the button isn't working for me either).

233				guard event.isKeyEvent else {
234					return nil
235				}

Admittedly I am using a super old version of this package, but I'm looking through the fixed bugs and the commits to see if there's signs of a change that's likely to fix this, which I'll then consider updating to once I know the implications for my currently ancient system requirements. Oh and also when I attempted to quickly try with a newer version something else wasn't working, I didn't want to have to solve that before asking this question. BTW the line number included above is in respect to the most recent commit of this file.

Does the field's close button work in anybody's app using the most recent code, when the field is focussed and not? (if there's anyone like me not using swiftui that is)

I don't think it's clear what reutrning nil from this LocalEventMonitor callback closure is supposed to mean, as the meaning of a nil result isn't mentioned in its header doc (or docc or whatever) comment and in the implementation of the CFRunLoopObserver closure the result is copied to a var named handledEvent but the way it's used seems to indicate the opposite ¯\_(ツ)_/¯

jpmhouston avatar Apr 09 '25 01:04 jpmhouston

Answering my own question... When upgrading to newer versions of KeyboardShortcuts, my RecorderCocoa views were getting zero size when I did startHotkeyContainerView.addSubview(startHotkeyRecorder), but changing this to use a little subfunction to add constraints manually made things work again:

func addSubviewWithManualConstraints(_ par: NSView, _ sub: NSView) {
    par.translatesAutoresizingMaskIntoConstraints = false
    sub.translatesAutoresizingMaskIntoConstraints = false
    par.addSubview(sub)
    par.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[s]|", metrics: nil, views: ["s": sub]))
    par.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[s]|", metrics: nil, views: ["s": sub]))
}

addSubviewWithManualConstraints(startHotkeyContainerView, startHotkeyRecorder)

This seemed to be something to do with the v1.9.0 change to do with translatesAutoresizingMaskIntoConstraints, before that version addSubview seemed to work well, but not in this version and after.

I briefly tried reverting to older versions to catch when I'd be able to reproduce the close box not worked again, but versions I saw it in when I was using addSubview now the close box worked fine. Perhaps this too was some layout problem all along, the control having zero bounds and so not catching events, but was drawing outside its bounds seemingly correctly.

jpmhouston avatar Apr 10 '25 23:04 jpmhouston