55namespace Boson \Api \MessageBox \Driver ;
66
77use Boson \Api \MessageBox \Driver \MacOS \LibObjectC ;
8+ use Boson \Api \MessageBox \MessageBoxButton ;
89use Boson \Api \MessageBox \MessageBoxCreateInfo ;
910use Boson \Api \MessageBox \MessageBoxExtensionInterface ;
1011use Boson \Api \MessageBox \MessageBoxIcon ;
1112use FFI \CData ;
12- use React \Promise \PromiseInterface ;
13-
14- use function React \Promise \resolve ;
1513
1614final readonly class MacOSMessageBoxExtension implements MessageBoxExtensionInterface
1715{
1816 private CData $ msgSendId ;
19- private CData $ msgSendString ;
17+ private CData $ msgSendStringGetId ;
2018 private CData $ msgSendLong ;
19+ private CData $ msgSendVoidGetId ;
20+ private CData $ msgSendVoidGetLong ;
2121
2222 public function __construct (
2323 private LibObjectC $ libobjc = new LibObjectC (),
2424 ) {
25- $ this ->msgSendId = $ libobjc ->getMessageSend ('id ' );
26- $ this ->msgSendString = $ libobjc ->getMessageSend ('const char* ' );
27- $ this ->msgSendLong = $ libobjc ->getMessageSend ('long ' );
25+ $ this ->msgSendStringGetId = $ libobjc ->getMessageSend ('id ' , 'const char* ' );
26+ $ this ->msgSendId = $ libobjc ->getMessageSend ('id ' , 'id ' );
27+ $ this ->msgSendLong = $ libobjc ->getMessageSend ('id ' , 'long ' );
28+ $ this ->msgSendVoidGetId = $ libobjc ->getMessageSend ('id ' );
29+ $ this ->msgSendVoidGetLong = $ libobjc ->getMessageSend ('long ' );
2830 }
2931
30- public function create (MessageBoxCreateInfo $ info ): PromiseInterface
32+ public function create (MessageBoxCreateInfo $ info ): ? MessageBoxButton
3133 {
3234 // NSString *titleStr = [NSString stringWithUTF8String:title]
33- $ titleStr = ($ this ->msgSendString )(
35+ $ titleStr = ($ this ->msgSendStringGetId )(
3436 $ this ->libobjc ->objc_getClass ('NSString ' ),
3537 $ this ->libobjc ->sel_registerName ('stringWithUTF8String: ' ),
3638 $ info ->title . "\0" ,
3739 );
3840
3941 // NSString *textStr = [NSString stringWithUTF8String:text]
40- $ textStr = ($ this ->msgSendString )(
42+ $ textStr = ($ this ->msgSendStringGetId )(
4143 $ this ->libobjc ->objc_getClass ('NSString ' ),
4244 $ this ->libobjc ->sel_registerName ('stringWithUTF8String: ' ),
4345 $ info ->text . "\0" ,
4446 );
4547
4648 // NSAlert *alert = [NSAlert new]
47- $ alert = $ this ->libobjc -> objc_msgSend (
49+ $ alert = ( $ this ->msgSendVoidGetId ) (
4850 $ this ->libobjc ->objc_getClass ('NSAlert ' ),
4951 $ this ->libobjc ->sel_registerName ('new ' ),
5052 );
@@ -65,24 +67,52 @@ public function create(MessageBoxCreateInfo $info): PromiseInterface
6567
6668 // Set alert style based on icon
6769 if ($ info ->icon !== null ) {
68- // [alert setAlertStyle:alertStyle];
6970 ($ this ->msgSendLong )(
7071 $ alert ,
7172 $ this ->libobjc ->sel_registerName ('setAlertStyle: ' ),
7273 match ($ info ->icon ) {
73- MessageBoxIcon::Error => 0 , // NSAlertStyleCritical
74- MessageBoxIcon::Warning => 1 , // NSAlertStyleWarning
75- MessageBoxIcon::Info => 2 , // NSAlertStyleInformational
74+ MessageBoxIcon::Error => 2 , // NSAlertStyleCritical
75+ MessageBoxIcon::Warning => 0 , // NSAlertStyleWarning
76+ MessageBoxIcon::Info => 1 , // NSAlertStyleInformational
7677 },
7778 );
7879 }
7980
81+ // Add buttons based on cancel flag
82+ if ($ info ->cancel ) {
83+ // [alert addButtonWithTitle:@"Cancel"];
84+ ($ this ->msgSendId )(
85+ $ alert ,
86+ $ this ->libobjc ->sel_registerName ('addButtonWithTitle: ' ),
87+ ($ this ->msgSendStringGetId )(
88+ $ this ->libobjc ->objc_getClass ('NSString ' ),
89+ $ this ->libobjc ->sel_registerName ('stringWithUTF8String: ' ),
90+ "Cancel \0" ,
91+ ),
92+ );
93+ }
94+
95+ // [alert addButtonWithTitle:@"OK"];
96+ ($ this ->msgSendId )(
97+ $ alert ,
98+ $ this ->libobjc ->sel_registerName ('addButtonWithTitle: ' ),
99+ ($ this ->msgSendStringGetId )(
100+ $ this ->libobjc ->objc_getClass ('NSString ' ),
101+ $ this ->libobjc ->sel_registerName ('stringWithUTF8String: ' ),
102+ "OK \0" ,
103+ ),
104+ );
105+
80106 // [alert runModal];
81- $ this ->libobjc -> objc_msgSend (
107+ $ result = ( $ this ->msgSendVoidGetLong ) (
82108 $ alert ,
83109 $ this ->libobjc ->sel_registerName ('runModal ' ),
84110 );
85111
86- return resolve (null );
112+ return match ($ result ) {
113+ 1000 => $ info ->cancel ? MessageBoxButton::Cancel : MessageBoxButton::Ok,
114+ 1001 => MessageBoxButton::Ok,
115+ default => $ info ->cancel ,
116+ };
87117 }
88118}
0 commit comments