@legendapp/spark 0.0.3 → 0.0.4

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.
Files changed (186) hide show
  1. package/init-template.cjs +7 -1
  2. package/package.json +10 -5
  3. package/patches/dependencies.json +4 -4
  4. package/patches/react-native-mmkv@4.3.2.patch +2 -2
  5. package/patches/react-native-nitro-zxing@0.3.1.patch +22 -0
  6. package/patches/registry.json +615 -0
  7. package/patches/workspace/expo-audio@55.0.18.patch +159 -0
  8. package/patches/workspace/op-engineering-op-sqlite@18.2.1.patch +796 -0
  9. package/patches/workspace/react-native-nitro-modules@0.37.0.patch +1348 -0
  10. package/patches/workspace/react-native-runtimes-core@0.1.0-alpha.2.patch +276 -0
  11. package/patches/workspace/react-native-webview@16.0.0.patch +208 -0
  12. package/postinstall.cjs +10 -1
  13. package/skills/spark/SKILL.md +37 -4
  14. package/skills/spark/UPGRADING.md +116 -1
  15. package/skills/spark/ai-customize.md +39 -0
  16. package/skills/spark/app-and-documents.md +56 -0
  17. package/skills/spark/external-libraries.md +87 -0
  18. package/skills/spark/files.md +64 -0
  19. package/skills/spark/integrations.md +69 -0
  20. package/skills/spark/keyboard.md +54 -0
  21. package/skills/spark/library-compat.md +76 -0
  22. package/skills/spark/menus-tray-dock.md +83 -0
  23. package/skills/spark/storage-settings.md +85 -0
  24. package/skills/spark/ui.md +57 -0
  25. package/skills/spark/windows.md +91 -0
  26. package/ui-popover.ts +1 -0
  27. package/ui-progress.ts +1 -0
  28. package/ui-sheet.ts +1 -0
  29. package/ui-split-view.ts +1 -0
  30. package/ui-table.ts +1 -0
  31. package/vendor/node_modules/@legendapp/spark-ai/package.json +3 -3
  32. package/vendor/node_modules/@legendapp/spark-audio/package.json +3 -13
  33. package/vendor/node_modules/@legendapp/spark-auth-session/package.json +3 -10
  34. package/vendor/node_modules/@legendapp/spark-cli/dist/add-desktop.js +5 -0
  35. package/vendor/node_modules/@legendapp/spark-cli/dist/build.js +137 -105
  36. package/vendor/node_modules/@legendapp/spark-cli/dist/cli-arguments.js +4 -1
  37. package/vendor/node_modules/@legendapp/spark-cli/dist/commands.js +16 -7
  38. package/vendor/node_modules/@legendapp/spark-cli/dist/dependency-pins.js +19 -3
  39. package/vendor/node_modules/@legendapp/spark-cli/dist/dev-command.js +2 -0
  40. package/vendor/node_modules/@legendapp/spark-cli/dist/dev.js +3 -0
  41. package/vendor/node_modules/@legendapp/spark-cli/dist/enriched-html-pins.js +2 -0
  42. package/vendor/node_modules/@legendapp/spark-cli/dist/errors.js +1 -1
  43. package/vendor/node_modules/@legendapp/spark-cli/dist/index.js +57 -9
  44. package/vendor/node_modules/@legendapp/spark-cli/dist/init-template.cjs +12 -2
  45. package/vendor/node_modules/@legendapp/spark-cli/dist/init-template.d.cts +1 -1
  46. package/vendor/node_modules/@legendapp/spark-cli/dist/metro.cjs +1 -0
  47. package/vendor/node_modules/@legendapp/spark-cli/dist/migrate.js +65 -0
  48. package/vendor/node_modules/@legendapp/spark-cli/dist/migration-helpers.cjs +4 -4
  49. package/vendor/node_modules/@legendapp/spark-cli/dist/migration-helpers.d.cts +2 -2
  50. package/vendor/node_modules/@legendapp/spark-cli/dist/migrations.cjs +12 -8
  51. package/vendor/node_modules/@legendapp/spark-cli/dist/native-patch-preflight.js +35 -7
  52. package/vendor/node_modules/@legendapp/spark-cli/dist/patch.js +92 -0
  53. package/vendor/node_modules/@legendapp/spark-cli/dist/project-version.js +12 -4
  54. package/vendor/node_modules/@legendapp/spark-cli/dist/project.js +8 -1
  55. package/vendor/node_modules/@legendapp/spark-cli/dist/release.json +20 -20
  56. package/vendor/node_modules/@legendapp/spark-cli/dist/upgrade.js +1 -1
  57. package/vendor/node_modules/@legendapp/spark-cli/dist/windows.js +3 -0
  58. package/vendor/node_modules/@legendapp/spark-cli/package.json +5 -4
  59. package/vendor/node_modules/@legendapp/spark-cli/templates/blank-typescript/package.json +2 -2
  60. package/vendor/node_modules/@legendapp/spark-cli/templates/universal/package.json +2 -2
  61. package/vendor/node_modules/@legendapp/spark-cli/templates/windows/package.json +2 -2
  62. package/vendor/node_modules/@legendapp/spark-clipboard/macos/RNDesktopClipboard.mm +1 -1
  63. package/vendor/node_modules/@legendapp/spark-clipboard/package.json +3 -10
  64. package/vendor/node_modules/@legendapp/spark-clipboard/src/desktop.ts +1 -0
  65. package/vendor/node_modules/@legendapp/spark-codex/package.json +3 -7
  66. package/vendor/node_modules/@legendapp/spark-commands/package.json +5 -5
  67. package/vendor/node_modules/@legendapp/spark-context-menu/package.json +3 -10
  68. package/vendor/node_modules/@legendapp/spark-desktop-app/macos/SparkDesktop.h +3 -1
  69. package/vendor/node_modules/@legendapp/spark-desktop-app/macos/SparkDesktop.mm +51 -10
  70. package/vendor/node_modules/@legendapp/spark-desktop-app/macos/SparkStorage.h +4 -0
  71. package/vendor/node_modules/@legendapp/spark-desktop-app/macos/SparkStorage.mm +82 -0
  72. package/vendor/node_modules/@legendapp/spark-desktop-app/package.json +2 -8
  73. package/vendor/node_modules/@legendapp/spark-desktop-config/config.cjs +6 -8
  74. package/vendor/node_modules/@legendapp/spark-desktop-config/identity.cjs +2 -0
  75. package/vendor/node_modules/@legendapp/spark-desktop-config/native-patches.cjs +300 -71
  76. package/vendor/node_modules/@legendapp/spark-desktop-config/native-patches.d.cts +7 -0
  77. package/vendor/node_modules/@legendapp/spark-desktop-config/package.json +3 -3
  78. package/vendor/node_modules/@legendapp/spark-desktop-config/patch-registry.cjs +318 -0
  79. package/vendor/node_modules/@legendapp/spark-desktop-config/patch-registry.d.cts +20 -0
  80. package/vendor/node_modules/@legendapp/spark-desktop-config/validation.cjs +66 -9
  81. package/vendor/node_modules/@legendapp/spark-desktop-config/windows.plugin.cjs +1 -0
  82. package/vendor/node_modules/@legendapp/spark-desktop-host/AppDelegate.mm +1 -0
  83. package/vendor/node_modules/@legendapp/spark-desktop-host/package.json +2 -2
  84. package/vendor/node_modules/@legendapp/spark-desktop-links/package.json +3 -10
  85. package/vendor/node_modules/@legendapp/spark-desktop-shortcuts/package.json +3 -10
  86. package/vendor/node_modules/@legendapp/spark-desktop-windows/macos/RNDesktopWindows.mm +7 -2
  87. package/vendor/node_modules/@legendapp/spark-desktop-windows/macos/window-manager/RNWindowManager.mm +235 -240
  88. package/vendor/node_modules/@legendapp/spark-desktop-windows/package.json +4 -11
  89. package/vendor/node_modules/@legendapp/spark-desktop-windows/src/api.ts +9 -3
  90. package/vendor/node_modules/@legendapp/spark-desktop-windows/src/macos-adapter.ts +7 -0
  91. package/vendor/node_modules/@legendapp/spark-desktop-windows/src/types.ts +32 -3
  92. package/vendor/node_modules/@legendapp/spark-desktop-windows/src/validation.ts +28 -11
  93. package/vendor/node_modules/@legendapp/spark-desktop-windows/src/windows/createWindowsNavigator.tsx +15 -3
  94. package/vendor/node_modules/@legendapp/spark-desktop-windows/src/windows/primaryWindowLifecycle.ts +12 -3
  95. package/vendor/node_modules/@legendapp/spark-desktop-windows/src/windows/usePrimaryWindowLifecycle.ts +2 -1
  96. package/vendor/node_modules/@legendapp/spark-desktop-windows/src/windows/useWindowFocusEffect.ts +4 -1
  97. package/vendor/node_modules/@legendapp/spark-documents/package.json +7 -7
  98. package/vendor/node_modules/@legendapp/spark-drag-drop/macos/RNDesktopDragView.mm +77 -17
  99. package/vendor/node_modules/@legendapp/spark-drag-drop/package.json +3 -8
  100. package/vendor/node_modules/@legendapp/spark-drag-drop/src/DesktopDragViewNativeComponent.ts +1 -0
  101. package/vendor/node_modules/@legendapp/spark-drag-drop/src/contracts.ts +13 -4
  102. package/vendor/node_modules/@legendapp/spark-drag-drop/src/index.tsx +5 -3
  103. package/vendor/node_modules/@legendapp/spark-drag-drop/windows/SparkDragDrop/SparkDragDrop.h +6 -1
  104. package/vendor/node_modules/@legendapp/spark-expo-compat/metro.cjs +44 -0
  105. package/vendor/node_modules/@legendapp/spark-expo-compat/package.json +20 -0
  106. package/vendor/node_modules/@legendapp/spark-expo-compat/src/clipboard.ts +40 -0
  107. package/vendor/node_modules/@legendapp/spark-expo-compat/src/document-picker.ts +52 -0
  108. package/vendor/node_modules/@legendapp/spark-file-dialog/package.json +3 -10
  109. package/vendor/node_modules/@legendapp/spark-file-dialog/src/options.ts +5 -2
  110. package/vendor/node_modules/@legendapp/spark-file-dialog/src/types.ts +3 -3
  111. package/vendor/node_modules/@legendapp/spark-file-system/macos/RNDesktopFileSystem.mm +1 -1
  112. package/vendor/node_modules/@legendapp/spark-file-system/package.json +3 -11
  113. package/vendor/node_modules/@legendapp/spark-global-shortcuts/package.json +3 -10
  114. package/vendor/node_modules/@legendapp/spark-html/package.json +2 -2
  115. package/vendor/node_modules/@legendapp/spark-markdown/package.json +2 -2
  116. package/vendor/node_modules/@legendapp/spark-message-dialog/package.json +3 -10
  117. package/vendor/node_modules/@legendapp/spark-message-dialog/src/index.ts +1 -0
  118. package/vendor/node_modules/@legendapp/spark-native-menu/package.json +3 -9
  119. package/vendor/node_modules/@legendapp/spark-native-menu/src/hooks.ts +4 -0
  120. package/vendor/node_modules/@legendapp/spark-notifications/package.json +3 -10
  121. package/vendor/node_modules/@legendapp/spark-processes/package.json +3 -10
  122. package/vendor/node_modules/@legendapp/spark-rich-text/package.json +2 -2
  123. package/vendor/node_modules/@legendapp/spark-secure-storage/package.json +3 -10
  124. package/vendor/node_modules/@legendapp/spark-settings/package.json +3 -3
  125. package/vendor/node_modules/@legendapp/spark-settings-ui/package.json +4 -4
  126. package/vendor/node_modules/@legendapp/spark-sqlite/package.json +3 -3
  127. package/vendor/node_modules/@legendapp/spark-system/package.json +3 -10
  128. package/vendor/node_modules/@legendapp/spark-tray/package.json +3 -10
  129. package/vendor/node_modules/@legendapp/spark-tray/src/index.ts +2 -1
  130. package/vendor/node_modules/@legendapp/spark-ui/macos/RNSparkButton.mm +82 -14
  131. package/vendor/node_modules/@legendapp/spark-ui/macos/RNSparkTextInput.h +1 -1
  132. package/vendor/node_modules/@legendapp/spark-ui/macos/RNSparkTextInput.mm +94 -11
  133. package/vendor/node_modules/@legendapp/spark-ui/macos/appkit-split-view/RNSidebarSplitViewComponent.mm +235 -18
  134. package/vendor/node_modules/@legendapp/spark-ui/macos/appkit-split-view/RNSparkPaneSplitView.h +17 -0
  135. package/vendor/node_modules/@legendapp/spark-ui/macos/appkit-split-view/RNSparkPaneSplitView.mm +216 -0
  136. package/vendor/node_modules/@legendapp/spark-ui/macos/appkit-split-view/RNSparkSplitViewComponent.h +4 -0
  137. package/vendor/node_modules/@legendapp/spark-ui/macos/appkit-split-view/RNSparkSplitViewComponent.mm +212 -0
  138. package/vendor/node_modules/@legendapp/spark-ui/macos/appkit-split-view/RNSparkSplitViewUpdates.h +6 -0
  139. package/vendor/node_modules/@legendapp/spark-ui/macos/native-select/RNNativeSegmentedControl.mm +26 -13
  140. package/vendor/node_modules/@legendapp/spark-ui/macos/popover/RNSparkPopover.h +9 -0
  141. package/vendor/node_modules/@legendapp/spark-ui/macos/popover/RNSparkPopover.mm +126 -0
  142. package/vendor/node_modules/@legendapp/spark-ui/macos/progress/RNSparkProgressIndicator.h +9 -0
  143. package/vendor/node_modules/@legendapp/spark-ui/macos/progress/RNSparkProgressIndicator.mm +66 -0
  144. package/vendor/node_modules/@legendapp/spark-ui/macos/radio-group/RNSparkRadioGroup.h +9 -0
  145. package/vendor/node_modules/@legendapp/spark-ui/macos/radio-group/RNSparkRadioGroup.mm +118 -0
  146. package/vendor/node_modules/@legendapp/spark-ui/macos/sf-symbol/RNSFSymbol.mm +4 -12
  147. package/vendor/node_modules/@legendapp/spark-ui/macos/sheet/RNSparkSheet.h +9 -0
  148. package/vendor/node_modules/@legendapp/spark-ui/macos/sheet/RNSparkSheet.mm +109 -0
  149. package/vendor/node_modules/@legendapp/spark-ui/macos/sidebar/RNSidebar.mm +418 -102
  150. package/vendor/node_modules/@legendapp/spark-ui/macos/table/RNSparkTable.h +9 -0
  151. package/vendor/node_modules/@legendapp/spark-ui/macos/table/RNSparkTable.mm +179 -0
  152. package/vendor/node_modules/@legendapp/spark-ui/package.json +9 -8
  153. package/vendor/node_modules/@legendapp/spark-ui/src/SparkButtonNativeComponent.ts +10 -2
  154. package/vendor/node_modules/@legendapp/spark-ui/src/SparkTextInputNativeComponent.ts +2 -0
  155. package/vendor/node_modules/@legendapp/spark-ui/src/appkit-split-view/SidebarSplitViewNativeComponent.ts +9 -0
  156. package/vendor/node_modules/@legendapp/spark-ui/src/appkit-split-view/SplitView.tsx +99 -0
  157. package/vendor/node_modules/@legendapp/spark-ui/src/appkit-split-view/SplitViewNativeComponent.ts +29 -0
  158. package/vendor/node_modules/@legendapp/spark-ui/src/appkit-split-view/index.tsx +34 -17
  159. package/vendor/node_modules/@legendapp/spark-ui/src/availability.ts +5 -4
  160. package/vendor/node_modules/@legendapp/spark-ui/src/checkbox-unavailable.tsx +20 -0
  161. package/vendor/node_modules/@legendapp/spark-ui/src/control.ts +24 -3
  162. package/vendor/node_modules/@legendapp/spark-ui/src/desktop.tsx +63 -13
  163. package/vendor/node_modules/@legendapp/spark-ui/src/index.android.tsx +12 -7
  164. package/vendor/node_modules/@legendapp/spark-ui/src/index.ios.tsx +12 -7
  165. package/vendor/node_modules/@legendapp/spark-ui/src/index.web.tsx +35 -13
  166. package/vendor/node_modules/@legendapp/spark-ui/src/mobile-input.tsx +2 -2
  167. package/vendor/node_modules/@legendapp/spark-ui/src/native-select/NativeSegmentedControlNativeComponent.ts +3 -1
  168. package/vendor/node_modules/@legendapp/spark-ui/src/popover/PopoverNativeComponent.ts +20 -0
  169. package/vendor/node_modules/@legendapp/spark-ui/src/popover/index.tsx +45 -0
  170. package/vendor/node_modules/@legendapp/spark-ui/src/progress/ProgressIndicatorNativeComponent.ts +13 -0
  171. package/vendor/node_modules/@legendapp/spark-ui/src/progress/index.tsx +32 -0
  172. package/vendor/node_modules/@legendapp/spark-ui/src/radio-group/RadioGroupNativeComponent.ts +17 -0
  173. package/vendor/node_modules/@legendapp/spark-ui/src/sheet/SheetNativeComponent.ts +15 -0
  174. package/vendor/node_modules/@legendapp/spark-ui/src/sheet/index.tsx +38 -0
  175. package/vendor/node_modules/@legendapp/spark-ui/src/sidebar/SidebarItemNativeComponent.ts +9 -1
  176. package/vendor/node_modules/@legendapp/spark-ui/src/sidebar/SidebarNativeComponent.ts +17 -2
  177. package/vendor/node_modules/@legendapp/spark-ui/src/sidebar/index.tsx +106 -22
  178. package/vendor/node_modules/@legendapp/spark-ui/src/table/TableNativeComponent.ts +22 -0
  179. package/vendor/node_modules/@legendapp/spark-ui/src/table/index.tsx +118 -0
  180. package/vendor/node_modules/@legendapp/spark-ui/src/types.ts +39 -6
  181. package/vendor/node_modules/@legendapp/spark-ui/uniwind.ts +3 -1
  182. package/vendor/node_modules/@legendapp/spark-ui/uniwind.web.ts +2 -1
  183. package/vendor/node_modules/@legendapp/spark-ui/windows/SparkUI/SparkUI.h +46 -15
  184. package/vendor/node_modules/@legendapp/spark-updates/package.json +3 -9
  185. package/vendor/node_modules/@legendapp/spark-webview/package.json +2 -2
  186. package/vendor/node_modules/@legendapp/spark-window-options/package.json +1 -1
@@ -11,20 +11,76 @@ using namespace facebook::react;
11
11
  #if TARGET_OS_OSX
12
12
  static NSUserInterfaceItemIdentifier const RNSidebarColumnIdentifier = @"RNSidebarColumn";
13
13
  static NSUserInterfaceItemIdentifier const RNSidebarDataCellIdentifier = @"RNSidebarDataCell";
14
+ // Rows inset their title and count by this much from both cell edges; the count keeps this gap from the title.
15
+ static CGFloat const RNSidebarCellInset = 12;
16
+ static CGFloat const RNSidebarCountGap = 6;
14
17
 
15
18
  @class RNSidebar;
19
+ @class RNSidebarItem;
16
20
 
17
- @interface RNSidebarDataItem : NSObject
21
+ // One row of the outline: a data item or a React row, linked to its parent by ID.
22
+ @interface RNSidebarNode : NSObject
18
23
  @property (nonatomic, copy) NSString *itemId;
19
24
  @property (nonatomic, copy) NSString *title;
25
+ @property (nonatomic, copy) NSString *parentId;
20
26
  @property (nonatomic, assign) BOOL selectable;
27
+ @property (nonatomic, assign) BOOL group;
28
+ @property (nonatomic, assign) NSInteger count;
29
+ @property (nonatomic, strong) NSMutableArray<RNSidebarNode *> *children;
30
+ @property (nonatomic, weak) RNSidebarItem *view;
21
31
  @end
22
32
 
23
- @implementation RNSidebarDataItem
33
+ @implementation RNSidebarNode
34
+ - (instancetype)init
35
+ {
36
+ if (self = [super init]) {
37
+ _itemId = @"";
38
+ _title = @"";
39
+ _parentId = @"";
40
+ _selectable = YES;
41
+ _count = -1;
42
+ _children = [NSMutableArray new];
43
+ }
44
+ return self;
45
+ }
24
46
  @end
25
47
 
26
- // A data row's title, inset at both ends and centered vertically, aligned to the row's layout direction.
48
+ static NSTextField *RNSidebarCountField(void)
49
+ {
50
+ NSTextField *field = [NSTextField labelWithString:@""];
51
+ field.font = [NSFont monospacedDigitSystemFontOfSize:13 weight:NSFontWeightRegular];
52
+ field.textColor = NSColor.secondaryLabelColor;
53
+ field.hidden = YES;
54
+ return field;
55
+ }
56
+
57
+ static void RNSidebarSetCount(NSTextField *field, NSInteger count)
58
+ {
59
+ field.hidden = count < 0;
60
+ field.stringValue = count < 0 ? @"" : [NSNumberFormatter localizedStringFromNumber:@(count) numberStyle:NSNumberFormatterDecimalStyle];
61
+ }
62
+
63
+ // The width a visible count takes from the trailing end of a row, including the inset and gap.
64
+ static CGFloat RNSidebarCountReserve(NSTextField *field)
65
+ {
66
+ return field.hidden ? 0 : ceil(field.intrinsicContentSize.width) + RNSidebarCellInset + RNSidebarCountGap;
67
+ }
68
+
69
+ // Places a visible count at the trailing edge, vertically centered.
70
+ static void RNSidebarLayoutCount(NSView *cell, NSTextField *field)
71
+ {
72
+ if (field.hidden) return;
73
+ NSSize size = field.intrinsicContentSize;
74
+ CGFloat width = ceil(size.width);
75
+ BOOL rightToLeft = cell.userInterfaceLayoutDirection == NSUserInterfaceLayoutDirectionRightToLeft;
76
+ CGFloat x = rightToLeft ? RNSidebarCellInset : NSWidth(cell.bounds) - RNSidebarCellInset - width;
77
+ field.frame = NSMakeRect(x, round((NSHeight(cell.bounds) - size.height) / 2), width, size.height);
78
+ }
79
+
80
+ // A data row's title and optional count, inset at both ends and centered vertically, aligned to the row's
81
+ // layout direction.
27
82
  @interface RNSidebarDataCell : NSTableCellView
83
+ @property (nonatomic, strong) NSTextField *countField;
28
84
  @end
29
85
 
30
86
  @implementation RNSidebarDataCell
@@ -32,8 +88,12 @@ static NSUserInterfaceItemIdentifier const RNSidebarDataCellIdentifier = @"RNSid
32
88
  {
33
89
  NSTextField *title = self.textField;
34
90
  CGFloat height = title.intrinsicContentSize.height;
91
+ BOOL rightToLeft = self.userInterfaceLayoutDirection == NSUserInterfaceLayoutDirectionRightToLeft;
92
+ CGFloat reserve = MAX(RNSidebarCellInset, RNSidebarCountReserve(_countField));
35
93
  title.alignment = RNSparkLeadingTextAlignment(self.userInterfaceLayoutDirection);
36
- title.frame = NSMakeRect(12, round((NSHeight(self.bounds) - height) / 2), MAX(0, NSWidth(self.bounds) - 24), height);
94
+ title.frame = NSMakeRect(rightToLeft ? reserve : RNSidebarCellInset, round((NSHeight(self.bounds) - height) / 2),
95
+ MAX(0, NSWidth(self.bounds) - RNSidebarCellInset - reserve), height);
96
+ RNSidebarLayoutCount(self, _countField);
37
97
  }
38
98
 
39
99
  - (void)resizeSubviewsWithOldSize:(NSSize)oldSize
@@ -69,7 +129,7 @@ static NSUserInterfaceItemIdentifier const RNSidebarDataCellIdentifier = @"RNSid
69
129
  }
70
130
  @end
71
131
 
72
- @interface RNSidebarTableView : NSTableView
132
+ @interface RNSidebarTableView : NSOutlineView
73
133
  @property (nonatomic, assign) BOOL rightMouseDown;
74
134
  @end
75
135
 
@@ -95,14 +155,58 @@ static NSUserInterfaceItemIdentifier const RNSidebarDataCellIdentifier = @"RNSid
95
155
 
96
156
  @interface RNSidebarItem () <RCTSidebarItemViewProtocol>
97
157
  @property (nonatomic, copy) NSString *itemId;
158
+ @property (nonatomic, copy) NSString *parentId;
98
159
  @property (nonatomic, assign) BOOL selectable;
160
+ @property (nonatomic, assign) BOOL group;
161
+ @property (nonatomic, assign) NSInteger count;
99
162
  @property (nonatomic, assign) CGFloat rowHeight;
100
163
  @property (nonatomic, assign) BOOL autoHeight;
101
164
  @property (nonatomic, assign) CGFloat measuredHeight;
165
+ @property (nonatomic, assign) CGFloat reportedWidth;
102
166
  @property (nonatomic, weak) RNSidebar *sidebar;
167
+ - (void)reportContentWidth:(CGFloat)width;
103
168
  @end
104
169
 
105
- @interface RNSidebar () <NSTableViewDataSource, NSTableViewDelegate, RCTSidebarViewProtocol>
170
+ // Hosts a React row beside its optional count. The row takes the cell's width less the count, which
171
+ // depends on the outline level, so it is reported to the row for Yoga to apply.
172
+ @interface RNSidebarReactCell : NSTableCellView
173
+ @property (nonatomic, strong) RNSidebarItem *item;
174
+ @property (nonatomic, strong) NSTextField *countField;
175
+ - (void)layoutItem;
176
+ @end
177
+
178
+ @implementation RNSidebarReactCell
179
+ - (instancetype)initWithItem:(RNSidebarItem *)item
180
+ {
181
+ if (self = [super initWithFrame:NSZeroRect]) {
182
+ _item = item;
183
+ _countField = RNSidebarCountField();
184
+ [item removeFromSuperview];
185
+ item.autoresizingMask = NSViewNotSizable;
186
+ [self addSubview:item];
187
+ [self addSubview:_countField];
188
+ RNSidebarSetCount(_countField, item.count);
189
+ }
190
+ return self;
191
+ }
192
+
193
+ - (void)layoutItem
194
+ {
195
+ CGFloat reserve = RNSidebarCountReserve(_countField);
196
+ CGFloat width = MAX(0, NSWidth(self.bounds) - reserve);
197
+ BOOL rightToLeft = self.userInterfaceLayoutDirection == NSUserInterfaceLayoutDirectionRightToLeft;
198
+ _item.frame = NSMakeRect(rightToLeft ? reserve : 0, 0, width, NSHeight(self.bounds));
199
+ RNSidebarLayoutCount(self, _countField);
200
+ if (NSWidth(self.bounds) > 0) [_item reportContentWidth:width];
201
+ }
202
+
203
+ - (void)resizeSubviewsWithOldSize:(NSSize)oldSize
204
+ {
205
+ [self layoutItem];
206
+ }
207
+ @end
208
+
209
+ @interface RNSidebar () <NSOutlineViewDataSource, NSOutlineViewDelegate, RCTSidebarViewProtocol>
106
210
  - (void)sidebarItemDidChange:(RNSidebarItem *)item;
107
211
  - (void)setContextHighlightedItem:(nullable RNSidebarItem *)item;
108
212
  @end
@@ -123,7 +227,9 @@ static NSUserInterfaceItemIdentifier const RNSidebarDataCellIdentifier = @"RNSid
123
227
  _props = std::make_shared<const SidebarItemProps>();
124
228
  #if TARGET_OS_OSX
125
229
  _itemId = @"";
230
+ _parentId = @"";
126
231
  _selectable = YES;
232
+ _count = -1;
127
233
  _rowHeight = 0;
128
234
  _autoHeight = NO;
129
235
  _measuredHeight = 0;
@@ -139,9 +245,20 @@ static NSUserInterfaceItemIdentifier const RNSidebarDataCellIdentifier = @"RNSid
139
245
  return YES;
140
246
  }
141
247
 
248
+ - (void)reportContentWidth:(CGFloat)width
249
+ {
250
+ const auto eventEmitter = std::static_pointer_cast<const SidebarItemEventEmitter>(_eventEmitter);
251
+ if (!eventEmitter || width <= 0 || width == _reportedWidth) {
252
+ return;
253
+ }
254
+
255
+ _reportedWidth = width;
256
+ eventEmitter->onSidebarItemLayout(SidebarItemEventEmitter::OnSidebarItemLayout{.width = width});
257
+ }
258
+
142
259
  - (void)rightMouseDown:(NSEvent *)event
143
260
  {
144
- [_sidebar setContextHighlightedItem:_selectable ? self : nil];
261
+ [_sidebar setContextHighlightedItem:_selectable && !_group ? self : nil];
145
262
  [super rightMouseDown:event];
146
263
  }
147
264
 
@@ -181,11 +298,19 @@ static NSUserInterfaceItemIdentifier const RNSidebarDataCellIdentifier = @"RNSid
181
298
  const auto &newProps = *std::static_pointer_cast<SidebarItemProps const>(props);
182
299
 
183
300
  #if TARGET_OS_OSX
184
- _itemId = [NSString stringWithUTF8String:newProps.itemId.c_str()];
301
+ NSString *itemId = [NSString stringWithUTF8String:newProps.itemId.c_str()];
302
+ NSString *parentId = [NSString stringWithUTF8String:newProps.parentId.c_str()];
303
+ // Style changes (such as the reported width) leave the outline alone; only row properties reload it.
304
+ const BOOL rowChanged = ![itemId isEqualToString:_itemId] || ![parentId isEqualToString:_parentId] || newProps.selectable != _selectable ||
305
+ newProps.group != _group || newProps.count != _count || newProps.rowHeight != _rowHeight || newProps.autoHeight != _autoHeight;
306
+ _itemId = itemId;
307
+ _parentId = parentId;
185
308
  _selectable = newProps.selectable;
309
+ _group = newProps.group;
310
+ _count = newProps.count;
186
311
  _rowHeight = newProps.rowHeight;
187
312
  _autoHeight = newProps.autoHeight;
188
- [_sidebar sidebarItemDidChange:self];
313
+ if (rowChanged) [_sidebar sidebarItemDidChange:self];
189
314
  #endif
190
315
 
191
316
  [super updateProps:props oldProps:oldProps];
@@ -205,8 +330,8 @@ static NSUserInterfaceItemIdentifier const RNSidebarDataCellIdentifier = @"RNSid
205
330
  }
206
331
  }
207
332
 
208
- if (_sidebar && [self.superview isKindOfClass:NSTableCellView.class]) {
209
- self.frame = self.superview.bounds;
333
+ if (_sidebar && [self.superview isKindOfClass:RNSidebarReactCell.class]) {
334
+ [(RNSidebarReactCell *)self.superview layoutItem];
210
335
  }
211
336
  #endif
212
337
  }
@@ -217,10 +342,14 @@ static NSUserInterfaceItemIdentifier const RNSidebarDataCellIdentifier = @"RNSid
217
342
 
218
343
  #if TARGET_OS_OSX
219
344
  _itemId = @"";
345
+ _parentId = @"";
220
346
  _selectable = YES;
347
+ _group = NO;
348
+ _count = -1;
221
349
  _rowHeight = 0;
222
350
  _autoHeight = NO;
223
351
  _measuredHeight = 0;
352
+ _reportedWidth = 0;
224
353
  _sidebar = nil;
225
354
  #endif
226
355
  }
@@ -238,14 +367,20 @@ static NSUserInterfaceItemIdentifier const RNSidebarDataCellIdentifier = @"RNSid
238
367
  RNSidebarTableView *_tableView;
239
368
  NSTableColumn *_tableColumn;
240
369
  NSMutableArray<RNSidebarItem *> *_itemViews;
241
- NSArray<RNSidebarDataItem *> *_dataItems;
242
- NSString *_selectedId;
370
+ NSArray<RNSidebarNode *> *_dataItems;
371
+ NSArray<RNSidebarNode *> *_roots;
372
+ NSArray<NSString *> *_selectedIds;
373
+ NSMutableSet<NSString *> *_collapsedIds;
374
+ NSString *_appliedCollapsedIdsJson;
375
+ NSArray<NSString *> *_scrolledSelectedIds;
376
+ NSDictionary<NSString *, RNSidebarNode *> *_nodesById;
243
377
  CGFloat _contentInsetTop;
244
378
  CGFloat _defaultRowHeight;
245
379
  NSSize _lastReportedSize;
246
- NSInteger _contextHighlightedRow;
380
+ RNSidebarItem *_contextHighlightedItem;
247
381
  CGFloat _lastReportedRowWidth;
248
382
  BOOL _updatingSelection;
383
+ BOOL _applyingExpansion;
249
384
  BOOL _rightToLeft;
250
385
  #else
251
386
  RCTPlatformView *_placeholderView;
@@ -260,11 +395,12 @@ static NSUserInterfaceItemIdentifier const RNSidebarDataCellIdentifier = @"RNSid
260
395
  #if TARGET_OS_OSX
261
396
  _itemViews = [NSMutableArray new];
262
397
  _dataItems = @[];
263
- _selectedId = @"";
398
+ _roots = @[];
399
+ _selectedIds = @[];
400
+ _collapsedIds = [NSMutableSet new];
264
401
  _contentInsetTop = 0;
265
402
  _defaultRowHeight = 28;
266
403
  _lastReportedSize = NSZeroSize;
267
- _contextHighlightedRow = -1;
268
404
  _lastReportedRowWidth = 0;
269
405
  _rightToLeft = NO;
270
406
  self.wantsLayer = YES;
@@ -298,16 +434,19 @@ static NSUserInterfaceItemIdentifier const RNSidebarDataCellIdentifier = @"RNSid
298
434
  _tableView.intercellSpacing = NSMakeSize(0, 0);
299
435
  _tableView.rowHeight = _defaultRowHeight;
300
436
  _tableView.usesAlternatingRowBackgroundColors = NO;
437
+ _tableView.floatsGroupRows = NO;
438
+ _tableView.autoresizesOutlineColumn = NO;
439
+ _tableView.target = self;
440
+ _tableView.doubleAction = @selector(rowDoubleClicked:);
301
441
 
302
- if (@available(macOS 11.0, *)) {
303
- _tableView.style = NSTableViewStyleSourceList;
304
- }
442
+ _tableView.style = NSTableViewStyleSourceList;
305
443
  // Follows the React layout direction (updateLayoutMetrics), not the application's.
306
444
  _tableView.userInterfaceLayoutDirection = NSUserInterfaceLayoutDirectionLeftToRight;
307
445
 
308
446
  _tableColumn = [[NSTableColumn alloc] initWithIdentifier:RNSidebarColumnIdentifier];
309
447
  _tableColumn.resizingMask = NSTableColumnAutoresizingMask;
310
448
  [_tableView addTableColumn:_tableColumn];
449
+ _tableView.outlineTableColumn = _tableColumn;
311
450
 
312
451
  _scrollView = [NSScrollView new];
313
452
  _scrollView.autohidesScrollers = YES;
@@ -321,7 +460,24 @@ static NSUserInterfaceItemIdentifier const RNSidebarDataCellIdentifier = @"RNSid
321
460
  [self addSubview:_scrollView];
322
461
  }
323
462
 
324
- - (NSArray<RNSidebarDataItem *> *)itemsFromJson:(std::string const &)json
463
+ static NSArray<NSString *> *RNSidebarStringsFromJson(std::string const &json)
464
+ {
465
+ NSString *jsonString = [NSString stringWithUTF8String:json.c_str()];
466
+ if (jsonString.length == 0) return @[];
467
+ id parsed = [NSJSONSerialization JSONObjectWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];
468
+ if (![parsed isKindOfClass:NSArray.class]) return @[];
469
+ NSMutableArray<NSString *> *strings = [NSMutableArray new];
470
+ for (id value in (NSArray *)parsed) if ([value isKindOfClass:NSString.class]) [strings addObject:value];
471
+ return strings;
472
+ }
473
+
474
+ static std::string RNSidebarJsonFromStrings(NSArray<NSString *> *strings)
475
+ {
476
+ NSData *data = [NSJSONSerialization dataWithJSONObject:strings options:NSJSONWritingWithoutEscapingSlashes error:nil];
477
+ return data ? std::string((const char *)data.bytes, data.length) : "[]";
478
+ }
479
+
480
+ - (NSArray<RNSidebarNode *> *)itemsFromJson:(std::string const &)json
325
481
  {
326
482
  NSString *jsonString = [NSString stringWithUTF8String:json.c_str()];
327
483
  if (jsonString.length == 0) {
@@ -334,7 +490,7 @@ static NSUserInterfaceItemIdentifier const RNSidebarDataCellIdentifier = @"RNSid
334
490
  return @[];
335
491
  }
336
492
 
337
- NSMutableArray<RNSidebarDataItem *> *items = [NSMutableArray new];
493
+ NSMutableArray<RNSidebarNode *> *items = [NSMutableArray new];
338
494
  for (id rawItem in (NSArray *)parsed) {
339
495
  if (![rawItem isKindOfClass:NSDictionary.class]) {
340
496
  continue;
@@ -347,10 +503,13 @@ static NSUserInterfaceItemIdentifier const RNSidebarDataCellIdentifier = @"RNSid
347
503
  continue;
348
504
  }
349
505
 
350
- RNSidebarDataItem *item = [RNSidebarDataItem new];
506
+ RNSidebarNode *item = [RNSidebarNode new];
351
507
  item.itemId = itemId;
352
508
  item.title = title;
353
509
  item.selectable = ![dictionary[@"selectable"] isKindOfClass:NSNumber.class] || [dictionary[@"selectable"] boolValue];
510
+ item.group = [dictionary[@"group"] isKindOfClass:NSNumber.class] && [dictionary[@"group"] boolValue];
511
+ item.count = [dictionary[@"count"] isKindOfClass:NSNumber.class] ? MAX(-1, [dictionary[@"count"] integerValue]) : -1;
512
+ item.parentId = [dictionary[@"parentId"] isKindOfClass:NSString.class] ? dictionary[@"parentId"] : @"";
354
513
  [items addObject:item];
355
514
  }
356
515
 
@@ -362,37 +521,79 @@ static NSUserInterfaceItemIdentifier const RNSidebarDataCellIdentifier = @"RNSid
362
521
  return _itemViews.count > 0;
363
522
  }
364
523
 
365
- - (NSUInteger)itemCount
524
+ // Links rows to their parents by ID, in row order. A row whose parent is unknown is a root.
525
+ - (NSArray<RNSidebarNode *> *)buildRoots
366
526
  {
367
- return self.usesReactChildren ? _itemViews.count : _dataItems.count;
527
+ NSMutableArray<RNSidebarNode *> *nodes = [NSMutableArray new];
528
+ if (self.usesReactChildren) {
529
+ for (RNSidebarItem *view in _itemViews) {
530
+ RNSidebarNode *node = [RNSidebarNode new];
531
+ node.itemId = view.itemId;
532
+ node.parentId = view.parentId;
533
+ node.selectable = view.selectable;
534
+ node.group = view.group;
535
+ node.count = view.count;
536
+ node.view = view;
537
+ [nodes addObject:node];
538
+ }
539
+ } else {
540
+ for (RNSidebarNode *node in _dataItems) [node.children removeAllObjects];
541
+ [nodes addObjectsFromArray:_dataItems];
542
+ }
543
+
544
+ NSMutableDictionary<NSString *, RNSidebarNode *> *byId = [NSMutableDictionary new];
545
+ NSMutableArray<RNSidebarNode *> *roots = [NSMutableArray new];
546
+ for (RNSidebarNode *node in nodes) {
547
+ RNSidebarNode *parent = node.parentId.length > 0 ? byId[node.parentId] : nil;
548
+ if (parent) [parent.children addObject:node];
549
+ else [roots addObject:node];
550
+ byId[node.itemId] = node;
551
+ }
552
+ _nodesById = byId;
553
+ return roots;
368
554
  }
369
555
 
370
556
  - (NSString *)itemIdAtRow:(NSInteger)row
371
557
  {
372
- if (row < 0 || row >= (NSInteger)self.itemCount) {
373
- return @"";
374
- }
375
-
376
- return self.usesReactChildren ? _itemViews[row].itemId : _dataItems[row].itemId;
558
+ RNSidebarNode *node = [_tableView itemAtRow:row];
559
+ return node ? node.itemId : @"";
377
560
  }
378
561
 
379
562
  - (BOOL)isRowSelectable:(NSInteger)row
380
563
  {
381
- if (row < 0 || row >= (NSInteger)self.itemCount) {
382
- return NO;
383
- }
384
-
385
- return self.usesReactChildren ? _itemViews[row].selectable : _dataItems[row].selectable;
564
+ RNSidebarNode *node = [_tableView itemAtRow:row];
565
+ return node && node.selectable && !node.group;
386
566
  }
387
567
 
388
568
  - (void)reloadRows
389
569
  {
570
+ _roots = [self buildRoots];
390
571
  _tableView.rowHeight = _defaultRowHeight;
391
572
  [_tableView reloadData];
573
+ [self applyExpansion];
392
574
  [self updateSelection];
393
575
  [self setNeedsLayout:YES];
394
576
  }
395
577
 
578
+ // Rows are rebuilt on every reload, so expansion is kept by ID and reapplied, outermost first.
579
+ - (void)applyExpansion
580
+ {
581
+ _applyingExpansion = YES;
582
+ NSMutableArray<RNSidebarNode *> *pending = [_roots mutableCopy];
583
+ while (pending.count > 0) {
584
+ RNSidebarNode *node = pending.firstObject;
585
+ [pending removeObjectAtIndex:0];
586
+ if (node.children.count == 0) continue;
587
+ if ([_collapsedIds containsObject:node.itemId]) {
588
+ [_tableView collapseItem:node];
589
+ } else {
590
+ [_tableView expandItem:node];
591
+ [pending addObjectsFromArray:node.children];
592
+ }
593
+ }
594
+ _applyingExpansion = NO;
595
+ }
596
+
396
597
  - (void)sidebarItemDidChange:(RNSidebarItem *)item
397
598
  {
398
599
  if (![_itemViews containsObject:item]) {
@@ -402,25 +603,24 @@ static NSUserInterfaceItemIdentifier const RNSidebarDataCellIdentifier = @"RNSid
402
603
  [self reloadRows];
403
604
  }
404
605
 
405
- - (void)setContextHighlightedItem:(RNSidebarItem *)item
606
+ - (NSInteger)rowForItemView:(RNSidebarItem *)item
406
607
  {
407
- NSInteger row = item ? [_itemViews indexOfObject:item] : NSNotFound;
408
- NSInteger nextRow = row == NSNotFound ? -1 : row;
409
- NSInteger previousRow = _contextHighlightedRow;
410
- _contextHighlightedRow = nextRow;
411
-
412
- if (previousRow >= 0) {
413
- RNSidebarRowView *previousRowView = (RNSidebarRowView *)[_tableView rowViewAtRow:previousRow makeIfNecessary:NO];
414
- if ([previousRowView isKindOfClass:RNSidebarRowView.class]) {
415
- previousRowView.contextHighlighted = NO;
416
- }
608
+ for (NSInteger row = 0; row < _tableView.numberOfRows; row++) {
609
+ if (((RNSidebarNode *)[_tableView itemAtRow:row]).view == item) return row;
417
610
  }
611
+ return -1;
612
+ }
418
613
 
419
- if (nextRow >= 0) {
420
- RNSidebarRowView *nextRowView = (RNSidebarRowView *)[_tableView rowViewAtRow:nextRow makeIfNecessary:NO];
421
- if ([nextRowView isKindOfClass:RNSidebarRowView.class]) {
422
- nextRowView.contextHighlighted = YES;
423
- }
614
+ - (void)setContextHighlightedItem:(RNSidebarItem *)item
615
+ {
616
+ RNSidebarItem *previous = _contextHighlightedItem;
617
+ _contextHighlightedItem = item;
618
+
619
+ for (RNSidebarItem *view in @[ previous ?: NSNull.null, item ?: NSNull.null ]) {
620
+ if (![view isKindOfClass:RNSidebarItem.class]) continue;
621
+ NSInteger row = [self rowForItemView:view];
622
+ RNSidebarRowView *rowView = row < 0 ? nil : (RNSidebarRowView *)[_tableView rowViewAtRow:row makeIfNecessary:NO];
623
+ if ([rowView isKindOfClass:RNSidebarRowView.class]) rowView.contextHighlighted = view == item;
424
624
  }
425
625
  }
426
626
 
@@ -430,22 +630,38 @@ static NSUserInterfaceItemIdentifier const RNSidebarDataCellIdentifier = @"RNSid
430
630
  return;
431
631
  }
432
632
 
433
- NSInteger selectedRow = -1;
434
- for (NSUInteger index = 0; index < self.itemCount; index += 1) {
435
- if ([[self itemIdAtRow:(NSInteger)index] isEqualToString:_selectedId]) {
436
- selectedRow = (NSInteger)index;
437
- break;
633
+ NSMutableIndexSet *rows = [NSMutableIndexSet new];
634
+ for (NSString *selectedId in _selectedIds) {
635
+ for (NSInteger row = 0; row < _tableView.numberOfRows; row++) {
636
+ if ([[self itemIdAtRow:row] isEqualToString:selectedId]) {
637
+ [rows addIndex:(NSUInteger)row];
638
+ break;
639
+ }
438
640
  }
641
+ if (!_tableView.allowsMultipleSelection) break;
439
642
  }
440
643
 
441
644
  _updatingSelection = YES;
442
- if (selectedRow >= 0) {
443
- [_tableView selectRowIndexes:[NSIndexSet indexSetWithIndex:selectedRow] byExtendingSelection:NO];
444
- [_tableView scrollRowToVisible:selectedRow];
645
+ if (rows.count > 0) {
646
+ [_tableView selectRowIndexes:rows byExtendingSelection:NO];
445
647
  } else {
446
648
  [_tableView deselectAll:nil];
447
649
  }
448
650
  _updatingSelection = NO;
651
+ [self scrollToSelectionIfNeeded];
652
+ }
653
+
654
+ // Reloads reapply an unchanged selection; only a new one scrolls into view. Before the list has a height,
655
+ // scrolling would push the first rows out of view, so it waits for the first layout.
656
+ - (void)scrollToSelectionIfNeeded
657
+ {
658
+ if ([_selectedIds isEqualToArray:_scrolledSelectedIds] || NSHeight(_scrollView.contentView.bounds) <= 0) {
659
+ return;
660
+ }
661
+
662
+ _scrolledSelectedIds = _selectedIds;
663
+ NSInteger lastRow = _tableView.selectedRow;
664
+ if (lastRow >= 0) [_tableView scrollRowToVisible:lastRow];
449
665
  }
450
666
 
451
667
  - (void)reportLayoutIfNeeded
@@ -470,6 +686,7 @@ static NSUserInterfaceItemIdentifier const RNSidebarDataCellIdentifier = @"RNSid
470
686
 
471
687
  // React rows are laid out by Yoga, so their width is reported for the Sidebar to apply: the column the
472
688
  // source-list table fits inside its clip view, which excludes the row insets and a legacy scroller.
689
+ // Rows that are indented or show a count report their own, narrower width (RNSidebarReactCell).
473
690
  - (void)reportRowWidthIfNeeded
474
691
  {
475
692
  CGFloat width = _tableColumn.width;
@@ -482,7 +699,7 @@ static NSUserInterfaceItemIdentifier const RNSidebarDataCellIdentifier = @"RNSid
482
699
  eventEmitter->onSidebarRowLayout(SidebarEventEmitter::OnSidebarRowLayout{.width = width});
483
700
  }
484
701
 
485
- - (void)tableViewColumnDidResize:(NSNotification *)notification
702
+ - (void)outlineViewColumnDidResize:(NSNotification *)notification
486
703
  {
487
704
  [self reportRowWidthIfNeeded];
488
705
  }
@@ -509,6 +726,7 @@ static NSUserInterfaceItemIdentifier const RNSidebarDataCellIdentifier = @"RNSid
509
726
  RNSidebarItem *item = (RNSidebarItem *)childComponentView;
510
727
  [item removeFromSuperview];
511
728
  item.sidebar = nil;
729
+ if (_contextHighlightedItem == item) _contextHighlightedItem = nil;
512
730
  [_itemViews removeObject:item];
513
731
  [self reloadRows];
514
732
  return;
@@ -517,104 +735,185 @@ static NSUserInterfaceItemIdentifier const RNSidebarDataCellIdentifier = @"RNSid
517
735
  [super unmountChildComponentView:childComponentView index:index];
518
736
  }
519
737
 
520
- - (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView
738
+ - (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
739
+ {
740
+ return (NSInteger)(item ? ((RNSidebarNode *)item).children.count : _roots.count);
741
+ }
742
+
743
+ - (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item
744
+ {
745
+ return item ? ((RNSidebarNode *)item).children[index] : _roots[index];
746
+ }
747
+
748
+ - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
749
+ {
750
+ return ((RNSidebarNode *)item).children.count > 0;
751
+ }
752
+
753
+ - (BOOL)outlineView:(NSOutlineView *)outlineView isGroupItem:(id)item
521
754
  {
522
- return (NSInteger)self.itemCount;
755
+ return ((RNSidebarNode *)item).group;
523
756
  }
524
757
 
525
- - (CGFloat)tableView:(NSTableView *)tableView heightOfRow:(NSInteger)row
758
+ - (CGFloat)outlineView:(NSOutlineView *)outlineView heightOfRowByItem:(id)item
526
759
  {
527
- if (!self.usesReactChildren || row < 0 || row >= (NSInteger)_itemViews.count) {
760
+ RNSidebarItem *view = ((RNSidebarNode *)item).view;
761
+ if (!view) {
528
762
  return _defaultRowHeight;
529
763
  }
530
764
 
531
- RNSidebarItem *item = _itemViews[row];
532
- if (item.rowHeight > 0) {
533
- return item.rowHeight;
765
+ if (view.rowHeight > 0) {
766
+ return view.rowHeight;
534
767
  }
535
768
 
536
- if (item.autoHeight) {
537
- if (item.measuredHeight > 0) {
538
- return item.measuredHeight;
769
+ if (view.autoHeight) {
770
+ if (view.measuredHeight > 0) {
771
+ return view.measuredHeight;
539
772
  }
540
773
 
541
- if (item.frame.size.height > 0) {
542
- return item.frame.size.height;
774
+ if (view.frame.size.height > 0) {
775
+ return view.frame.size.height;
543
776
  }
544
777
  }
545
778
 
546
779
  return _defaultRowHeight;
547
780
  }
548
781
 
549
- - (NSView *)tableView:(NSTableView *)tableView
550
- viewForTableColumn:(NSTableColumn *)tableColumn
551
- row:(NSInteger)row
782
+ - (NSView *)outlineView:(NSOutlineView *)outlineView viewForTableColumn:(NSTableColumn *)tableColumn item:(id)item
552
783
  {
553
- if (row < 0 || row >= (NSInteger)self.itemCount) {
554
- return nil;
555
- }
556
-
557
- if (self.usesReactChildren) {
558
- NSTableCellView *cell = [NSTableCellView new];
559
- RNSidebarItem *item = _itemViews[row];
560
- [item removeFromSuperview];
561
- item.frame = cell.bounds;
562
- item.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
563
- [cell addSubview:item];
784
+ RNSidebarNode *node = item;
785
+ if (node.view) {
786
+ RNSidebarReactCell *cell = [[RNSidebarReactCell alloc] initWithItem:node.view];
787
+ cell.userInterfaceLayoutDirection = _tableView.userInterfaceLayoutDirection;
564
788
  return cell;
565
789
  }
566
790
 
567
- RNSidebarDataCell *cell = [tableView makeViewWithIdentifier:RNSidebarDataCellIdentifier owner:self];
791
+ RNSidebarDataCell *cell = [outlineView makeViewWithIdentifier:RNSidebarDataCellIdentifier owner:self];
568
792
  if (!cell) {
569
793
  cell = [RNSidebarDataCell new];
570
794
  cell.identifier = RNSidebarDataCellIdentifier;
571
795
 
572
796
  NSTextField *textField = [NSTextField labelWithString:@""];
573
- textField.font = [NSFont systemFontOfSize:13 weight:NSFontWeightRegular];
574
797
  textField.lineBreakMode = NSLineBreakByTruncatingTail;
575
- textField.textColor = NSColor.labelColor;
576
798
 
577
799
  cell.textField = textField;
800
+ cell.countField = RNSidebarCountField();
578
801
  [cell addSubview:textField];
802
+ [cell addSubview:cell.countField];
579
803
  }
580
804
 
805
+ // Group rows use the source list's section-header type: 11 pt semibold, secondary label color.
806
+ cell.textField.font = node.group ? [NSFont systemFontOfSize:11 weight:NSFontWeightSemibold] : [NSFont systemFontOfSize:13 weight:NSFontWeightRegular];
807
+ cell.textField.textColor = node.group ? NSColor.secondaryLabelColor : NSColor.labelColor;
581
808
  cell.userInterfaceLayoutDirection = _tableView.userInterfaceLayoutDirection;
582
- cell.textField.stringValue = _dataItems[row].title;
809
+ cell.textField.stringValue = node.title;
810
+ RNSidebarSetCount(cell.countField, node.count);
583
811
  [cell layoutTitle];
584
812
  return cell;
585
813
  }
586
814
 
587
- - (BOOL)tableView:(NSTableView *)tableView shouldSelectRow:(NSInteger)row
815
+ - (BOOL)outlineView:(NSOutlineView *)outlineView shouldSelectItem:(id)item
588
816
  {
589
817
  if (_tableView.rightMouseDown) {
590
818
  return NO;
591
819
  }
592
820
 
593
- return [self isRowSelectable:row];
821
+ RNSidebarNode *node = item;
822
+ return node.selectable && !node.group;
594
823
  }
595
824
 
596
- - (NSTableRowView *)tableView:(NSTableView *)tableView rowViewForRow:(NSInteger)row
825
+ - (NSTableRowView *)outlineView:(NSOutlineView *)outlineView rowViewForItem:(id)item
597
826
  {
598
827
  RNSidebarRowView *rowView = [RNSidebarRowView new];
599
- rowView.contextHighlighted = row == _contextHighlightedRow;
828
+ RNSidebarItem *view = ((RNSidebarNode *)item).view;
829
+ rowView.contextHighlighted = view && view == _contextHighlightedItem;
600
830
  return rowView;
601
831
  }
602
832
 
603
- - (void)tableViewSelectionDidChange:(NSNotification *)notification
833
+ - (void)outlineViewSelectionDidChange:(NSNotification *)notification
604
834
  {
605
835
  if (_updatingSelection) {
606
836
  return;
607
837
  }
608
838
 
609
- NSInteger row = _tableView.selectedRow;
610
- _selectedId = row >= 0 && row < (NSInteger)self.itemCount ? [self itemIdAtRow:row] : @"";
839
+ NSMutableArray<NSString *> *visible = [NSMutableArray new];
840
+ [_tableView.selectedRowIndexes enumerateIndexesUsingBlock:^(NSUInteger row, BOOL *stop) {
841
+ [visible addObject:[self itemIdAtRow:(NSInteger)row]];
842
+ }];
843
+ NSMutableArray<NSString *> *hidden = [NSMutableArray new], *shown = [NSMutableArray new];
844
+ for (NSString *selectedId in _selectedIds) {
845
+ RNSidebarNode *node = _nodesById[selectedId];
846
+ [(node && [_tableView rowForItem:node] < 0 ? hidden : shown) addObject:selectedId];
847
+ }
848
+ // A collapsing section deselects the rows it hides; the selection itself is unchanged and returns on expand.
849
+ if ([[NSSet setWithArray:visible] isEqualToSet:[NSSet setWithArray:shown]]) {
850
+ return;
851
+ }
852
+
853
+ // In multiple selection, rows inside collapsed sections stay selected while the user changes the visible ones.
854
+ NSMutableArray<NSString *> *ids = _tableView.allowsMultipleSelection ? hidden : [NSMutableArray new];
855
+ [ids addObjectsFromArray:visible];
856
+ _selectedIds = ids;
611
857
  const auto eventEmitter = std::static_pointer_cast<const SidebarEventEmitter>(_eventEmitter);
612
858
  if (eventEmitter) {
613
859
  eventEmitter->onSidebarSelectionChange(SidebarEventEmitter::OnSidebarSelectionChange{
614
- .id = std::string(_selectedId.UTF8String),
860
+ .id = std::string([self itemIdAtRow:_tableView.selectedRow].UTF8String),
861
+ .idsJson = RNSidebarJsonFromStrings(ids),
862
+ });
863
+ }
864
+ }
865
+
866
+ - (void)expansionDidChange:(NSNotification *)notification expanded:(BOOL)expanded
867
+ {
868
+ if (_applyingExpansion) {
869
+ return;
870
+ }
871
+
872
+ RNSidebarNode *node = notification.userInfo[@"NSObject"];
873
+ NSString *itemId = node.itemId;
874
+ // AppKit also collapses and restores the rows nested in a section the user toggles; those keep their own
875
+ // state. Only a change of state, on a row whose ancestors are expanded, is the user's.
876
+ if (itemId.length == 0 || expanded != [_collapsedIds containsObject:itemId]) return;
877
+ for (id parent = [_tableView parentForItem:node]; parent; parent = [_tableView parentForItem:parent]) {
878
+ if (![_tableView isItemExpanded:parent]) return;
879
+ }
880
+ if (expanded) [_collapsedIds removeObject:itemId];
881
+ else [_collapsedIds addObject:itemId];
882
+ // Rows inside an expanded section were collapsed with it or never expanded: restore their own state.
883
+ if (expanded) [self applyExpansion];
884
+
885
+ const auto eventEmitter = std::static_pointer_cast<const SidebarEventEmitter>(_eventEmitter);
886
+ if (eventEmitter) {
887
+ eventEmitter->onSidebarExpandedChange(SidebarEventEmitter::OnSidebarExpandedChange{
888
+ .expanded = (bool)expanded,
889
+ .id = std::string(itemId.UTF8String),
615
890
  });
616
891
  }
617
892
  }
893
+
894
+ - (void)outlineViewItemDidExpand:(NSNotification *)notification
895
+ {
896
+ [self expansionDidChange:notification expanded:YES];
897
+ [self updateSelection];
898
+ }
899
+
900
+ - (void)outlineViewItemDidCollapse:(NSNotification *)notification
901
+ {
902
+ [self expansionDidChange:notification expanded:NO];
903
+ }
904
+
905
+ - (void)rowDoubleClicked:(id)sender
906
+ {
907
+ NSInteger row = _tableView.clickedRow;
908
+ const auto eventEmitter = std::static_pointer_cast<const SidebarEventEmitter>(_eventEmitter);
909
+ if (row < 0 || !eventEmitter) {
910
+ return;
911
+ }
912
+
913
+ eventEmitter->onSidebarDoubleClick(SidebarEventEmitter::OnSidebarDoubleClick{
914
+ .id = std::string([self itemIdAtRow:row].UTF8String),
915
+ });
916
+ }
618
917
  #endif
619
918
 
620
919
  - (void)updateProps:(Props::Shared const &)props oldProps:(Props::Shared const &)oldProps
@@ -628,12 +927,24 @@ static NSUserInterfaceItemIdentifier const RNSidebarDataCellIdentifier = @"RNSid
628
927
  _contentInsetTop = newProps.contentInsetTop;
629
928
  _defaultRowHeight = newProps.defaultRowHeight > 0 ? newProps.defaultRowHeight : 28;
630
929
  const BOOL itemsChanged = !previous || newProps.itemsJson != previous->itemsJson;
631
- const BOOL rowsChanged = itemsChanged || newProps.defaultRowHeight != previous->defaultRowHeight;
930
+ // Only a change of the collapsed IDs is applied, so an unchanged prop never undoes a user's toggle.
931
+ // Compared with the last applied value rather than oldProps, which is null when a moved view is re-inserted.
932
+ NSString *collapsedIdsJson = [NSString stringWithUTF8String:newProps.collapsedIdsJson.c_str()];
933
+ const BOOL expansionChanged = ![collapsedIdsJson isEqualToString:_appliedCollapsedIdsJson];
934
+ _appliedCollapsedIdsJson = collapsedIdsJson;
935
+ const BOOL rowsChanged = itemsChanged || expansionChanged || newProps.defaultRowHeight != previous->defaultRowHeight;
632
936
  if (itemsChanged) _dataItems = [self itemsFromJson:newProps.itemsJson];
633
- _selectedId = [NSString stringWithUTF8String:newProps.selectedId.c_str()];
937
+ if (expansionChanged) _collapsedIds = [NSMutableSet setWithArray:RNSidebarStringsFromJson(newProps.collapsedIdsJson)];
938
+ _selectedIds = RNSidebarStringsFromJson(newProps.selectedIdsJson);
939
+ _tableView.allowsMultipleSelection = newProps.allowsMultipleSelection;
634
940
  _scrollView.contentInsets = NSEdgeInsetsMake(_contentInsetTop, 0, 0, 0);
941
+ if (!previous || newProps.listStyle != previous->listStyle) {
942
+ _tableView.style = newProps.listStyle == SidebarListStyle::Plain ? NSTableViewStylePlain : NSTableViewStyleSourceList;
943
+ [self setNeedsLayout:YES];
944
+ }
635
945
  if (rowsChanged) [self reloadRows];
636
- else if (newProps.selectedId != previous->selectedId || newProps.selectionRevision != previous->selectionRevision) [self updateSelection];
946
+ else if (newProps.selectedIdsJson != previous->selectedIdsJson || newProps.selectionRevision != previous->selectionRevision ||
947
+ newProps.allowsMultipleSelection != previous->allowsMultipleSelection) [self updateSelection];
637
948
  if (!previous || newProps.contentInsetTop != previous->contentInsetTop) [self setNeedsLayout:YES];
638
949
  #endif
639
950
 
@@ -670,6 +981,7 @@ static NSUserInterfaceItemIdentifier const RNSidebarDataCellIdentifier = @"RNSid
670
981
  // source-list row insets past its trailing edge.
671
982
  _scrollView.frame = self.bounds;
672
983
  [_scrollView tile];
984
+ [self scrollToSelectionIfNeeded];
673
985
  [self reportLayoutIfNeeded];
674
986
  [self reportRowWidthIfNeeded];
675
987
  #else
@@ -688,13 +1000,17 @@ static NSUserInterfaceItemIdentifier const RNSidebarDataCellIdentifier = @"RNSid
688
1000
  }
689
1001
  [_itemViews removeAllObjects];
690
1002
  _dataItems = @[];
691
- _selectedId = @"";
1003
+ _selectedIds = @[];
1004
+ [_collapsedIds removeAllObjects];
1005
+ _appliedCollapsedIdsJson = nil;
1006
+ _scrolledSelectedIds = nil;
692
1007
  _contentInsetTop = 0;
693
1008
  _defaultRowHeight = 28;
694
1009
  _lastReportedSize = NSZeroSize;
695
1010
  _lastReportedRowWidth = 0;
696
- _contextHighlightedRow = -1;
1011
+ _contextHighlightedItem = nil;
697
1012
  _updatingSelection = NO;
1013
+ _tableView.allowsMultipleSelection = NO;
698
1014
  _scrollView.contentInsets = NSEdgeInsetsZero;
699
1015
  [self reloadRows];
700
1016
  #endif