Skip to content

Commit e47ad76

Browse files
CopilotSaadnajmi
andcommitted
Add text input fields to macOS Configure Bundler dialog
Co-authored-by: Saadnajmi <[email protected]>
1 parent 8eb316c commit e47ad76

File tree

1 file changed

+85
-8
lines changed

1 file changed

+85
-8
lines changed

packages/react-native/React/CoreModules/RCTDevMenu.mm

Lines changed: 85 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,8 @@ + (void)initialize
127127
{
128128
#if !TARGET_OS_OSX // [macOS]
129129
// We're swizzling here because it's poor form to override methods in a category,
130-
RCTOriginalUIWindowMotionEndedWithEventImp = (MotionEndedWithEventImpType) RCTSwapInstanceMethods([UIWindow class], @selector(motionEnded:withEvent:), @selector(RCT_motionEnded:withEvent:)); // [macOS]
130+
RCTOriginalUIWindowMotionEndedWithEventImp = (MotionEndedWithEventImpType)RCTSwapInstanceMethods(
131+
[UIWindow class], @selector(motionEnded:withEvent:), @selector(RCT_motionEnded:withEvent:)); // [macOS]
131132
#endif // [macOS]
132133
}
133134

@@ -402,9 +403,74 @@ - (void)setDefaultJSBundle
402403
NSAlert *alert = [NSAlert new];
403404
[alert setMessageText:@"Change packager location"];
404405
[alert setInformativeText:@"Input packager IP, port and entrypoint"];
405-
[alert addButtonWithTitle:@"Use bundled JS"];
406+
407+
// Create accessory view with text fields
408+
NSView *accessoryView = [[NSView alloc] initWithFrame:NSMakeRect(0.0, 0.0, 300.0, 90.0)];
409+
410+
// IP Address text field
411+
NSTextField *ipTextField = [[NSTextField alloc] initWithFrame:NSMakeRect(0.0, 60.0, 300.0, 22.0)];
412+
ipTextField.placeholderString = @"0.0.0.0";
413+
ipTextField.cell.scrollable = YES;
414+
[accessoryView addSubview:ipTextField];
415+
416+
// Port text field
417+
NSTextField *portTextField =
418+
[[NSTextField alloc] initWithFrame:NSMakeRect(0.0, 30.0, 300.0, 22.0)];
419+
portTextField.placeholderString = @"8081";
420+
portTextField.cell.scrollable = YES;
421+
[accessoryView addSubview:portTextField];
422+
423+
// Entrypoint text field
424+
NSTextField *entrypointTextField =
425+
[[NSTextField alloc] initWithFrame:NSMakeRect(0.0, 0.0, 300.0, 22.0)];
426+
entrypointTextField.placeholderString = @"index";
427+
entrypointTextField.cell.scrollable = YES;
428+
[accessoryView addSubview:entrypointTextField];
429+
430+
alert.accessoryView = accessoryView;
431+
432+
[alert addButtonWithTitle:@"Apply Changes"];
433+
[alert addButtonWithTitle:@"Reset to Default"];
434+
[alert addButtonWithTitle:@"Cancel"];
406435
[alert setAlertStyle:NSAlertStyleWarning];
407-
[alert beginSheetModalForWindow:[NSApp keyWindow] completionHandler:nil];
436+
437+
[alert beginSheetModalForWindow:[NSApp keyWindow]
438+
completionHandler:^(NSModalResponse response) {
439+
if (response == NSAlertFirstButtonReturn) {
440+
// Apply Changes
441+
NSString *ipAddress = ipTextField.stringValue;
442+
NSString *port = portTextField.stringValue;
443+
NSString *bundleRoot = entrypointTextField.stringValue;
444+
445+
if (ipAddress.length == 0 && port.length == 0) {
446+
[weakSelf setDefaultJSBundle];
447+
return;
448+
}
449+
450+
NSNumberFormatter *formatter = [NSNumberFormatter new];
451+
formatter.numberStyle = NSNumberFormatterDecimalStyle;
452+
NSNumber *portNumber = [formatter numberFromString:port];
453+
if (portNumber == nil) {
454+
portNumber = [NSNumber numberWithInt:RCT_METRO_PORT];
455+
}
456+
457+
[RCTBundleURLProvider sharedSettings].jsLocation =
458+
[NSString stringWithFormat:@"%@:%d", ipAddress, portNumber.intValue];
459+
460+
if (bundleRoot.length == 0) {
461+
[bundleManager resetBundleURL];
462+
} else {
463+
bundleManager.bundleURL = [[RCTBundleURLProvider sharedSettings]
464+
jsBundleURLForBundleRoot:bundleRoot];
465+
}
466+
467+
RCTTriggerReloadCommandListeners(@"Dev menu - apply changes");
468+
} else if (response == NSAlertSecondButtonReturn) {
469+
// Reset to Default
470+
[weakSelf setDefaultJSBundle];
471+
}
472+
// Cancel - do nothing
473+
}];
408474
#endif // macOS]
409475
}]];
410476

@@ -452,7 +518,15 @@ - (void)setDefaultJSBundle
452518
#else // [macOS
453519
NSMenu *menu = [self menu];
454520
NSWindow *window = [NSApp keyWindow];
455-
NSEvent *event = [NSEvent mouseEventWithType:NSEventTypeLeftMouseUp location:CGPointMake(0, 0) modifierFlags:0 timestamp:NSTimeIntervalSince1970 windowNumber:[window windowNumber] context:nil eventNumber:0 clickCount:0 pressure:0.1];
521+
NSEvent *event = [NSEvent mouseEventWithType:NSEventTypeLeftMouseUp
522+
location:CGPointMake(0, 0)
523+
modifierFlags:0
524+
timestamp:NSTimeIntervalSince1970
525+
windowNumber:[window windowNumber]
526+
context:nil
527+
eventNumber:0
528+
clickCount:0
529+
pressure:0.1];
456530
[NSMenu popUpContextMenu:menu withEvent:event forView:[window contentView]];
457531
#endif // macOS]
458532

@@ -484,8 +558,9 @@ - (NSMenu *)menu
484558

485559
menu = [NSMenu new];
486560

487-
NSMutableAttributedString *attributedTitle = [[NSMutableAttributedString alloc]initWithString:title];
488-
[attributedTitle setAttributes: @{ NSFontAttributeName : [NSFont menuFontOfSize:0] } range: NSMakeRange(0, [attributedTitle length])];
561+
NSMutableAttributedString *attributedTitle = [[NSMutableAttributedString alloc] initWithString:title];
562+
[attributedTitle setAttributes:@{NSFontAttributeName : [NSFont menuFontOfSize:0]}
563+
range:NSMakeRange(0, [attributedTitle length])];
489564
NSMenuItem *titleItem = [NSMenuItem new];
490565
[titleItem setAttributedTitle:attributedTitle];
491566
[menu addItem:titleItem];
@@ -494,7 +569,9 @@ - (NSMenu *)menu
494569

495570
NSArray<RCTDevMenuItem *> *items = [self _menuItemsToPresent];
496571
for (RCTDevMenuItem *item in items) {
497-
NSMenuItem *menuItem = [[NSMenuItem alloc] initWithTitle:[item title] action:@selector(menuItemSelected:) keyEquivalent:@""];
572+
NSMenuItem *menuItem = [[NSMenuItem alloc] initWithTitle:[item title]
573+
action:@selector(menuItemSelected:)
574+
keyEquivalent:@""];
498575
[menuItem setTarget:self];
499576
[menuItem setRepresentedObject:item];
500577
[menu addItem:menuItem];
@@ -505,7 +582,7 @@ - (NSMenu *)menu
505582
return nil;
506583
}
507584

508-
-(void)menuItemSelected:(id)sender
585+
- (void)menuItemSelected:(id)sender
509586
{
510587
NSMenuItem *menuItem = (NSMenuItem *)sender;
511588
RCTDevMenuItem *item = (RCTDevMenuItem *)[menuItem representedObject];

0 commit comments

Comments
 (0)