@legendapp/spark 0.0.3 → 0.0.5

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