@geastack/apple 0.2.15 → 0.2.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,7 @@
1
+ #include <cstdio>
2
+ #include "host/timers.h"
3
+ #include "host/worker.h"
4
+ #include "ui/tree_events.h"
1
5
  #import <Cocoa/Cocoa.h>
2
6
  #import <GameController/GameController.h>
3
7
  #import <QuartzCore/CADisplayLink.h>
@@ -9,9 +13,31 @@ extern void gea_macos_debugger_start();
9
13
  #include "font_registry.h"
10
14
  #include "macos_native_shell.h"
11
15
  #include "macos_renderer.h"
16
+ #include "macos_background_test.h"
12
17
  #include "press_bridge.h"
13
18
  #include "thermalright_hid_display.h"
14
19
 
20
+ // Optional app-owned process cleanup, before the native runtime/static teardown.
21
+ // No callback on a skin/resident-app replacement; this is actual process exit.
22
+ extern "C" __attribute__((weak)) void gea_native_application_terminate() {}
23
+ namespace {
24
+ void geaRetireNativeProcess() noexcept {
25
+ static bool retired = false; // Actual process exit on the application thread.
26
+ if (retired) return;
27
+ retired = true;
28
+ try { gea_native_application_terminate(); }
29
+ catch (...) { std::fputs("Gea: application termination callback failed; retiring SDK callbacks\n", stderr); }
30
+ try { gea::host::retireScheduledCallbacks(); }
31
+ catch (...) { std::fputs("Gea: scheduled callback retirement failed\n", stderr); }
32
+ gea::embedded::ui::retireNativeEventRealm();
33
+ try { gea::host::workers::retireMainContext(); }
34
+ catch (...) { std::fputs("Gea: main Context retirement failed\n", stderr); }
35
+ }
36
+ struct GeaNativeTerminationGuard {
37
+ ~GeaNativeTerminationGuard(){geaRetireNativeProcess();}
38
+ };
39
+ }
40
+
15
41
  namespace gea::macos { void installAppLauncherPlatform(const char *currentAppId); }
16
42
  namespace gea::macos { void installWifiDriver(); int nodeIdForView(NSView *view); }
17
43
 
@@ -72,6 +98,20 @@ static int geaCustomInputRoot()
72
98
  if (std::strcmp(tree.getAttribute(i,"data-gea-inputs"),"true")==0) return i;
73
99
  return -1;
74
100
  }
101
+ // Keyboard routing is available to ordinary mounted native apps as well as
102
+ // app-owned custom editors. Do not widen the pointer/custom-editor contract.
103
+ static int geaKeyboardInputRoot() {
104
+ const int custom=geaCustomInputRoot();
105
+ return custom>=0?custom:gea::embedded::ui::Tree::instance().mountedRoot();
106
+ }
107
+ static int geaKeyboardTarget(int root) {
108
+ auto &tree=gea::embedded::ui::Tree::instance();
109
+ const int active=tree.activeInputId();
110
+ for(int id=active;id>=0 && id<tree.nodeCount();id=tree.node(id).parent)
111
+ if(id==root)return active;
112
+ return root;
113
+ }
114
+ #include "macos_key_event.h"
75
115
  // Authored silhouettes own their press layer; a rectangular engine fill would
76
116
  // paint outside their transparent corners. Ancestors cover image/text hits.
77
117
  static bool geaOwnsContourPress(int x, int y) {
@@ -82,6 +122,7 @@ static bool geaOwnsContourPress(int x, int y) {
82
122
  }
83
123
  @interface GeaContentView : NSView <NSTextInputClient>
84
124
  @property(nonatomic,copy) NSAttributedString *geaMarkedText;
125
+ @property(nonatomic) BOOL geaInterpretingDispatchedKey;
85
126
  @property(nonatomic,strong) GCController *geaActiveController;
86
127
  @property(nonatomic) unsigned int geaGamepadButtons;
87
128
  @property(nonatomic) NSTimeInterval geaGamepadRepeat;
@@ -212,19 +253,50 @@ static bool geaOwnsContourPress(int x, int y) {
212
253
  for(int i=0;i<15;i++) if(pressed & (1u<<i)) [self geaSendKey:2000+i];
213
254
  }
214
255
  - (BOOL)isFlipped { return NO; }
215
- - (BOOL)acceptsFirstResponder { return geaCustomInputRoot()>=0; }
256
+ - (BOOL)acceptsFirstResponder { return geaKeyboardInputRoot()>=0; }
257
+ - (BOOL)becomeFirstResponder {const BOOL accepted=[super becomeFirstResponder];if(accepted)gea::macos::nativeRootFocusChanged(self,true,geaKeyboardTarget(geaKeyboardInputRoot()));return accepted;}
258
+ - (BOOL)resignFirstResponder {const BOOL accepted=[super resignFirstResponder];if(accepted)gea::macos::nativeRootFocusChanged(self,false,-1);return accepted;}
259
+
260
+ - (BOOL)geaDispatchKey:(gea::framework::events::PointerEvent &)event {
261
+ const int root=geaKeyboardInputRoot();if(root<0)return NO;
262
+ event.targetId=geaKeyboardTarget(root);
263
+ event.target=gea::framework::events::EventTarget(event.targetId);
264
+ event.currentTargetId=root;
265
+ event.currentTarget=gea::framework::events::EventTarget(root);
266
+ gea::embedded::ui::dispatchRootKeyEvent(event, geaCustomInputRoot()>=0);
267
+ return event.defaultPrevented;
268
+ }
216
269
  - (void)geaSendKey:(int)code {
217
- const int root=geaCustomInputRoot();if(root<0)return;
218
270
  gea::framework::events::PointerEvent event;
219
- event.type=gea::framework::events::PointerEventType::KeyDown;
220
- event.targetId=root;event.keyCode=code;
221
- if(!gea::embedded::ui::dispatchDocumentKeyDown(event)) gea::embedded::ui::Tree::instance().dispatchEvent(event);
271
+ event.type=gea::framework::events::PointerEventType::KeyDown;event.keyCode=code;
272
+ [self geaDispatchKey:event];
273
+ }
274
+ - (void)keyDown:(NSEvent *)native {
275
+ if(geaKeyboardInputRoot()<0){[super keyDown:native];return;}
276
+ auto event=geaNativeKey(native,[self hasMarkedText],false);
277
+ // Preserve the app-owned editor's historical select-all command only there.
278
+ const bool selectAll=geaCustomInputRoot()>=0 && event.metaKey && event.key=="a";
279
+ if(selectAll)event.keyCode=1001;
280
+ if([self geaDispatchKey:event] || selectAll)return;
281
+ // Ordinary native text controls own AppKit text services themselves. Only
282
+ // the explicit app-owned editor root interprets text into Gea attributes.
283
+ if(geaCustomInputRoot()<0) {
284
+ // Unhandled navigation and commands retain AppKit's responder defaults.
285
+ // Handled Gea events returned above and cannot also move native focus.
286
+ if(native.keyCode==48) {
287
+ if(event.shiftKey)[self.window selectPreviousKeyView:self];
288
+ else [self.window selectNextKeyView:self];
289
+ } else [super keyDown:native];
290
+ return;
291
+ }
292
+ self.geaInterpretingDispatchedKey=YES;
293
+ @try { [self interpretKeyEvents:@[native]]; }
294
+ @finally { self.geaInterpretingDispatchedKey=NO; }
222
295
  }
223
- - (void)keyDown:(NSEvent *)event {
224
- if(geaCustomInputRoot()<0){[super keyDown:event];return;}
225
- if((event.modifierFlags & NSEventModifierFlagCommand) && [[event.charactersIgnoringModifiers lowercaseString] isEqualToString:@"a"]){[self geaSendKey:1001];return;}
226
- if([event.charactersIgnoringModifiers isEqualToString:@" "]){[self geaSendKey:32];if(![self hasMarkedText])[self insertText:@" " replacementRange:NSMakeRange(NSNotFound,0)];return;}
227
- [self interpretKeyEvents:@[event]];
296
+ - (void)keyUp:(NSEvent *)native {
297
+ if(geaKeyboardInputRoot()<0){[super keyUp:native];return;}
298
+ auto event=geaNativeKey(native,[self hasMarkedText],true);
299
+ [self geaDispatchKey:event];
228
300
  }
229
301
  - (void)insertText:(id)text replacementRange:(NSRange)range {
230
302
  (void)range;const int root=geaCustomInputRoot();if(root<0)return;
@@ -247,7 +319,7 @@ static bool geaOwnsContourPress(int x, int y) {
247
319
  else if(selector==@selector(insertNewline:))code=13;
248
320
  else if(selector==@selector(insertTab:))code=9;
249
321
  else if(selector==@selector(cancelOperation:))code=27;
250
- if(code)[self geaSendKey:code];
322
+ if(code && !self.geaInterpretingDispatchedKey)[self geaSendKey:code];
251
323
  }
252
324
  - (void)paste:(id)sender { (void)sender;NSString *value=[[NSPasteboard generalPasteboard] stringForType:NSPasteboardTypeString];if(value)[self insertText:value replacementRange:NSMakeRange(NSNotFound,0)]; }
253
325
  - (void)copy:(id)sender {
@@ -451,8 +523,7 @@ static __weak AppDelegate *gGeaAppDelegate = nil;
451
523
  self.rootView.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
452
524
  [self.rootView setWantsLayer:YES];
453
525
  [self.window setContentView:self.rootView];
454
- [self.window makeKeyAndOrderFront:nil];
455
- [NSApp activateIgnoringOtherApps:YES];
526
+ gea::macos::background_test::present(self.window);
456
527
 
457
528
  NSSize size = self.rootView.bounds.size;
458
529
  if (gea::macos::thermalright::enabled()) {
@@ -577,10 +648,12 @@ static __weak AppDelegate *gGeaAppDelegate = nil;
577
648
  [self.frameDisplayLink invalidate];
578
649
  self.frameDisplayLink = nil;
579
650
  }
651
+ geaRetireNativeProcess();
580
652
  }
581
653
 
582
654
  - (void)tick
583
655
  {
656
+ gea::macos::background_test::checkApplication(NSApp);
584
657
  if (getenv("GEA_MACOS_TICK_DEBUG")) {
585
658
  static int tickCount = 0;
586
659
  tickCount++;
@@ -865,13 +938,18 @@ static __weak AppDelegate *gGeaAppDelegate = nil;
865
938
  view.frame = [self.window.contentView bounds];
866
939
  view.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
867
940
  [self.window setContentView:view];
868
- [self.window makeKeyAndOrderFront:nil];
869
- [NSApp activateIgnoringOtherApps:YES];
941
+ gea::macos::background_test::present(self.window);
942
+ }
943
+
944
+ - (void)windowDidResignKey:(NSNotification *)notification
945
+ {
946
+ if (notification.object != self.window) return;
947
+ gea::embedded::ui::dispatchWindowLifecycleEvent(gea::framework::events::PointerEventType::WindowBlur);
870
948
  }
871
949
 
872
950
  - (void)windowDidResize:(NSNotification *)notification
873
951
  {
874
- (void)notification;
952
+ if (notification.object != self.window) return;
875
953
  if (gea::macos::thermalright::enabled()) return;
876
954
  if (!self.splitShell) {
877
955
  const NSSize size = self.rootView.bounds.size;
@@ -884,6 +962,7 @@ static __weak AppDelegate *gGeaAppDelegate = nil;
884
962
  gea::embedded::ui::setViewportMetrics(width, height, 1.0);
885
963
  }
886
964
  }
965
+ gea::embedded::ui::dispatchWindowLifecycleEvent(gea::framework::events::PointerEventType::WindowResize);
887
966
  // Sync immediately on resize end too; the live-mode timer covers the
888
967
  // drag, this handles the final snap.
889
968
  [self tick];
@@ -958,8 +1037,7 @@ void gea::apple::AppKit::installRootViewController(gea::apple::AppKit::NSViewCon
958
1037
  [delegate.window setContentSize:wantedContent];
959
1038
  [delegate.window center];
960
1039
  }
961
- [delegate.window makeKeyAndOrderFront:nil];
962
- [NSApp activateIgnoringOtherApps:YES];
1040
+ gea::macos::background_test::present(delegate.window);
963
1041
  // Clear the initial first responder so an editable NSTextField/NSTextView
964
1042
  // in the content doesn't grab focus (drawing a blue focus ring + selecting
965
1043
  // its text) the instant the window opens — matching a freshly launched
@@ -1128,14 +1206,17 @@ void gea::apple::AppKit::setScrollDocumentTopAligned(gea::apple::AppKit::NSScrol
1128
1206
 
1129
1207
  int main(int argc, const char *argv[])
1130
1208
  {
1209
+ GeaNativeTerminationGuard terminationGuard;
1131
1210
  (void)argc;
1132
1211
  (void)argv;
1133
1212
  @autoreleasepool {
1134
1213
  gea_macos_smoke_log("[gea-macos] main entered");
1214
+ const bool backgroundTest = gea::macos::background_test::enabled();
1135
1215
  NSApplication *app = [NSApplication sharedApplication];
1136
1216
  AppDelegate *delegate = [[AppDelegate alloc] init];
1137
1217
  [app setDelegate:delegate];
1138
- [app setActivationPolicy:gea::macos::thermalright::enabled()
1218
+ if (backgroundTest) gea::macos::background_test::configure(app);
1219
+ else [app setActivationPolicy:gea::macos::thermalright::enabled()
1139
1220
  ? NSApplicationActivationPolicyProhibited
1140
1221
  : NSApplicationActivationPolicyRegular];
1141
1222
  gea_macos_smoke_log("[gea-macos] before finishLaunching");
@@ -12,6 +12,8 @@ namespace gea::macos {
12
12
 
13
13
  // Publish arranged geometry for app-owned Gea controls before dispatch.
14
14
  void publishControlEventBounds(int nodeId);
15
+ // Called only after AppKit accepts the actual root responder transition.
16
+ void nativeRootFocusChanged(NSView*view,bool focused,int nodeId);
15
17
 
16
18
  class MacosRenderer {
17
19
  public: