progrium / darwinkit

Native Mac APIs for Go. Previously known as MacDriver
MIT License
4.42k stars 147 forks source link

An error occurs when running the layout example. #246

Closed kokonjac closed 1 month ago

kokonjac commented 6 months ago

go run ./macos/_examples/layout/main.go Error:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Content hugging priorities must be positive.'
*** First throw call stack:
(
        0   CoreFoundation                      0x00007ff801a4ed86 __exceptionPreprocess + 242
        1   libobjc.A.dylib                     0x00007ff801541321 objc_exception_throw + 48
        2   CoreFoundation                      0x00007ff801a4ec26 +[NSException raise:format:] + 214
        3   AppKit                              0x00007ff805ed1a52 -[NSView(NSConstraintBasedLayoutInternal) setContentHuggingPriority:forOrientation:] + 96
        4   libffi.dylib                        0x00007ff812d5d892 ffi_call_unix64 + 82
        5   main                                0x0000000100f7f4f0 runtime.zerobase + 0
)
libc++abi: terminating due to uncaught exception of type NSException
SIGABRT: abort
PC=0x7ff8018c77a2 m=0 sigcode=0
signal arrived during cgo execution
...

The cause of this error may be setting a negative priority when using Auto Layout. But there seems to be no problem with the parameters of SetContentHuggingPriorityForOrientation. Wondering why.

    gridView.SetContentHuggingPriorityForOrientation(appkit.LayoutPriorityDefaultHigh, appkit.LayoutConstraintOrientationHorizontal) // (750.0,0)
hsiafan commented 5 months ago

I found that the data type of LayoutPriority is not correct. LayoutPriority should be 32-bit floating point number, while appkit go enum define it as 64-bit:

type LayoutPriority float64

Changing it to float 32 fixes crash for SetContentHuggingPriorityForOrientation.

progrium commented 4 months ago

@hsiafan Thanks for figuring this out. I thought I had tried this before, but I guess not. I would love a PR to fix the generation code on this if you're willing.

progrium commented 1 month ago

Turns out we were treating all floats the same, but it's been fixed. Thanks all!