diff --git a/Examples/OutlineViewDraggingExample/OutlineViewDraggingExample/FileItemView.swift b/Examples/OutlineViewDraggingExample/OutlineViewDraggingExample/FileItemView.swift index 9212fea..a1ca511 100644 --- a/Examples/OutlineViewDraggingExample/OutlineViewDraggingExample/FileItemView.swift +++ b/Examples/OutlineViewDraggingExample/OutlineViewDraggingExample/FileItemView.swift @@ -8,7 +8,7 @@ import Cocoa class FileItemView: NSTableCellView { - init(fileItem: FileItem) { + init(fileItem: FileItem, textColor: NSColor = .textColor) { let field = NSTextField(string: fileItem.description) field.isEditable = false field.isSelectable = false @@ -17,6 +17,7 @@ class FileItemView: NSTableCellView { field.usesSingleLineMode = false field.cell?.wraps = true field.cell?.isScrollable = false + field.textColor = textColor super.init(frame: .zero) diff --git a/Examples/OutlineViewExample/OutlineViewExample/ContentView.swift b/Examples/OutlineViewExample/OutlineViewExample/ContentView.swift index 24cc426..159dce9 100644 --- a/Examples/OutlineViewExample/OutlineViewExample/ContentView.swift +++ b/Examples/OutlineViewExample/OutlineViewExample/ContentView.swift @@ -54,6 +54,9 @@ struct ContentView: View { @State var separatorColor: Color = Color(NSColor.separatorColor) @State var separatorEnabled = false + @State var rootTextColor: Color = Color(NSColor.textColor) + @State var childTextColor: Color = Color(NSColor.textColor) + var body: some View { VStack { outlineView @@ -66,6 +69,10 @@ struct ContentView: View { : Color.clear ) } + + var rootIds: [UUID] { + data.map(\.id) + } var outlineView: some View { OutlineView( diff --git a/Examples/OutlineViewReloadExample/OutlineViewReloadExample.xcodeproj/project.pbxproj b/Examples/OutlineViewReloadExample/OutlineViewReloadExample.xcodeproj/project.pbxproj new file mode 100644 index 0000000..9cf3f89 --- /dev/null +++ b/Examples/OutlineViewReloadExample/OutlineViewReloadExample.xcodeproj/project.pbxproj @@ -0,0 +1,361 @@ +// !$*UTF8*$! +{ + archiveVersion = 1; + classes = { + }; + objectVersion = 52; + objects = { + +/* Begin PBXBuildFile section */ + 336C5FE225CC9F1600230C37 /* OutlineViewReloadExampleApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 336C5FE125CC9F1600230C37 /* OutlineViewReloadExampleApp.swift */; }; + 336C5FE425CC9F1600230C37 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 336C5FE325CC9F1600230C37 /* ContentView.swift */; }; + 336C5FE625CC9F1800230C37 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 336C5FE525CC9F1800230C37 /* Assets.xcassets */; }; + 336C5FE925CC9F1800230C37 /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 336C5FE825CC9F1800230C37 /* Preview Assets.xcassets */; }; + 336C5FFD25CCA8DA00230C37 /* FileItemView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 336C5FFC25CCA8D900230C37 /* FileItemView.swift */; }; + 336C600325CCAEE700230C37 /* OutlineView in Frameworks */ = {isa = PBXBuildFile; productRef = 336C600225CCAEE700230C37 /* OutlineView */; }; +/* End PBXBuildFile section */ + +/* Begin PBXFileReference section */ + 336C5FDE25CC9F1600230C37 /* OutlineViewReloadExample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = OutlineViewReloadExample.app; sourceTree = BUILT_PRODUCTS_DIR; }; + 336C5FE125CC9F1600230C37 /* OutlineViewReloadExampleApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OutlineViewReloadExampleApp.swift; sourceTree = ""; }; + 336C5FE325CC9F1600230C37 /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = ""; }; + 336C5FE525CC9F1800230C37 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; + 336C5FE825CC9F1800230C37 /* Preview Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = "Preview Assets.xcassets"; sourceTree = ""; }; + 336C5FEA25CC9F1800230C37 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; + 336C5FEB25CC9F1800230C37 /* OutlineViewReloadExample.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = OutlineViewReloadExample.entitlements; sourceTree = ""; }; + 336C5FFC25CCA8D900230C37 /* FileItemView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FileItemView.swift; sourceTree = ""; }; + 336C600025CCAEDA00230C37 /* OutlineView */ = {isa = PBXFileReference; lastKnownFileType = folder; name = OutlineView; path = ../..; sourceTree = ""; }; + 94C6EB1229D870C6007A19EB /* OutlineViewReloadExample.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; name = OutlineViewReloadExample.xcconfig; path = ../../../../../../../../../Desktop/OutlineViewReloadExample.xcconfig; sourceTree = ""; }; +/* End PBXFileReference section */ + +/* Begin PBXFrameworksBuildPhase section */ + 336C5FDB25CC9F1500230C37 /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + 336C600325CCAEE700230C37 /* OutlineView in Frameworks */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXFrameworksBuildPhase section */ + +/* Begin PBXGroup section */ + 336C5FD525CC9F1500230C37 = { + isa = PBXGroup; + children = ( + 336C600025CCAEDA00230C37 /* OutlineView */, + 336C5FE025CC9F1600230C37 /* OutlineViewReloadExample */, + 336C5FDF25CC9F1600230C37 /* Products */, + 336C5FF825CC9F7600230C37 /* Frameworks */, + ); + sourceTree = ""; + }; + 336C5FDF25CC9F1600230C37 /* Products */ = { + isa = PBXGroup; + children = ( + 336C5FDE25CC9F1600230C37 /* OutlineViewReloadExample.app */, + ); + name = Products; + sourceTree = ""; + }; + 336C5FE025CC9F1600230C37 /* OutlineViewReloadExample */ = { + isa = PBXGroup; + children = ( + 336C5FE125CC9F1600230C37 /* OutlineViewReloadExampleApp.swift */, + 336C5FE325CC9F1600230C37 /* ContentView.swift */, + 336C5FFC25CCA8D900230C37 /* FileItemView.swift */, + 336C5FE525CC9F1800230C37 /* Assets.xcassets */, + 336C5FEA25CC9F1800230C37 /* Info.plist */, + 336C5FEB25CC9F1800230C37 /* OutlineViewReloadExample.entitlements */, + 336C5FE725CC9F1800230C37 /* Preview Content */, + 94C6EB1229D870C6007A19EB /* OutlineViewReloadExample.xcconfig */, + ); + path = OutlineViewReloadExample; + sourceTree = ""; + }; + 336C5FE725CC9F1800230C37 /* Preview Content */ = { + isa = PBXGroup; + children = ( + 336C5FE825CC9F1800230C37 /* Preview Assets.xcassets */, + ); + path = "Preview Content"; + sourceTree = ""; + }; + 336C5FF825CC9F7600230C37 /* Frameworks */ = { + isa = PBXGroup; + children = ( + ); + name = Frameworks; + sourceTree = ""; + }; +/* End PBXGroup section */ + +/* Begin PBXNativeTarget section */ + 336C5FDD25CC9F1500230C37 /* OutlineViewReloadExample */ = { + isa = PBXNativeTarget; + buildConfigurationList = 336C5FEE25CC9F1800230C37 /* Build configuration list for PBXNativeTarget "OutlineViewReloadExample" */; + buildPhases = ( + 336C5FDA25CC9F1500230C37 /* Sources */, + 336C5FDB25CC9F1500230C37 /* Frameworks */, + 336C5FDC25CC9F1500230C37 /* Resources */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = OutlineViewReloadExample; + packageProductDependencies = ( + 336C600225CCAEE700230C37 /* OutlineView */, + ); + productName = OutlineViewExample; + productReference = 336C5FDE25CC9F1600230C37 /* OutlineViewReloadExample.app */; + productType = "com.apple.product-type.application"; + }; +/* End PBXNativeTarget section */ + +/* Begin PBXProject section */ + 336C5FD625CC9F1500230C37 /* Project object */ = { + isa = PBXProject; + attributes = { + LastSwiftUpdateCheck = 1220; + LastUpgradeCheck = 1220; + TargetAttributes = { + 336C5FDD25CC9F1500230C37 = { + CreatedOnToolsVersion = 12.2; + }; + }; + }; + buildConfigurationList = 336C5FD925CC9F1500230C37 /* Build configuration list for PBXProject "OutlineViewReloadExample" */; + compatibilityVersion = "Xcode 9.3"; + developmentRegion = en; + hasScannedForEncodings = 0; + knownRegions = ( + en, + Base, + ); + mainGroup = 336C5FD525CC9F1500230C37; + productRefGroup = 336C5FDF25CC9F1600230C37 /* Products */; + projectDirPath = ""; + projectRoot = ""; + targets = ( + 336C5FDD25CC9F1500230C37 /* OutlineViewReloadExample */, + ); + }; +/* End PBXProject section */ + +/* Begin PBXResourcesBuildPhase section */ + 336C5FDC25CC9F1500230C37 /* Resources */ = { + isa = PBXResourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 336C5FE925CC9F1800230C37 /* Preview Assets.xcassets in Resources */, + 336C5FE625CC9F1800230C37 /* Assets.xcassets in Resources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXResourcesBuildPhase section */ + +/* Begin PBXSourcesBuildPhase section */ + 336C5FDA25CC9F1500230C37 /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 336C5FFD25CCA8DA00230C37 /* FileItemView.swift in Sources */, + 336C5FE425CC9F1600230C37 /* ContentView.swift in Sources */, + 336C5FE225CC9F1600230C37 /* OutlineViewReloadExampleApp.swift in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXSourcesBuildPhase section */ + +/* Begin XCBuildConfiguration section */ + 336C5FED25CC9F1800230C37 /* Release */ = { + isa = XCBuildConfiguration; + baseConfigurationReference = 94C6EB1229D870C6007A19EB /* OutlineViewReloadExample.xcconfig */; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_ANALYZER_NONNULL = YES; + CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_ENABLE_OBJC_WEAK = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; + ENABLE_NS_ASSERTIONS = NO; + ENABLE_STRICT_OBJC_MSGSEND = YES; + GCC_C_LANGUAGE_STANDARD = gnu11; + GCC_NO_COMMON_BLOCKS = YES; + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + MACOSX_DEPLOYMENT_TARGET = 11.0; + MTL_ENABLE_DEBUG_INFO = NO; + MTL_FAST_MATH = YES; + SDKROOT = macosx; + SWIFT_COMPILATION_MODE = wholemodule; + SWIFT_OPTIMIZATION_LEVEL = "-O"; + }; + name = Release; + }; + 336C5FF025CC9F1800230C37 /* Release */ = { + isa = XCBuildConfiguration; + baseConfigurationReference = 94C6EB1229D870C6007A19EB /* OutlineViewReloadExample.xcconfig */; + buildSettings = { + ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; + CODE_SIGN_ENTITLEMENTS = OutlineViewReloadExample/OutlineViewReloadExample.entitlements; + CODE_SIGN_STYLE = Automatic; + COMBINE_HIDPI_IMAGES = YES; + DEVELOPMENT_ASSET_PATHS = "\"OutlineViewReloadExample/Preview Content\""; + ENABLE_HARDENED_RUNTIME = YES; + ENABLE_PREVIEWS = YES; + INFOPLIST_FILE = OutlineViewReloadExample/Info.plist; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/../Frameworks", + ); + MACOSX_DEPLOYMENT_TARGET = 11.0; + PRODUCT_BUNDLE_IDENTIFIER = "com.example.OutlineViewReloadExample${SAMPLE_CODE_DISAMBIGUATOR}"; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 5.0; + }; + name = Release; + }; + 94C6EB1029D86FFB007A19EB /* Debug */ = { + isa = XCBuildConfiguration; + baseConfigurationReference = 94C6EB1229D870C6007A19EB /* OutlineViewReloadExample.xcconfig */; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_ANALYZER_NONNULL = YES; + CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_ENABLE_OBJC_WEAK = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; + ENABLE_NS_ASSERTIONS = NO; + ENABLE_STRICT_OBJC_MSGSEND = YES; + GCC_C_LANGUAGE_STANDARD = gnu11; + GCC_NO_COMMON_BLOCKS = YES; + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + MACOSX_DEPLOYMENT_TARGET = 11.0; + MTL_ENABLE_DEBUG_INFO = NO; + MTL_FAST_MATH = YES; + SDKROOT = macosx; + SWIFT_COMPILATION_MODE = wholemodule; + SWIFT_OPTIMIZATION_LEVEL = "-O"; + }; + name = Debug; + }; + 94C6EB1129D86FFB007A19EB /* Debug */ = { + isa = XCBuildConfiguration; + baseConfigurationReference = 94C6EB1229D870C6007A19EB /* OutlineViewReloadExample.xcconfig */; + buildSettings = { + ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; + CODE_SIGN_ENTITLEMENTS = OutlineViewReloadExample/OutlineViewReloadExample.entitlements; + CODE_SIGN_STYLE = Automatic; + COMBINE_HIDPI_IMAGES = YES; + DEVELOPMENT_ASSET_PATHS = "\"OutlineViewReloadExample/Preview Content\""; + ENABLE_HARDENED_RUNTIME = YES; + ENABLE_PREVIEWS = YES; + INFOPLIST_FILE = OutlineViewReloadExample/Info.plist; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/../Frameworks", + ); + MACOSX_DEPLOYMENT_TARGET = 11.0; + PRODUCT_BUNDLE_IDENTIFIER = "com.example.OutlineViewReloadExample${SAMPLE_CODE_DISAMBIGUATOR}"; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 5.0; + }; + name = Debug; + }; +/* End XCBuildConfiguration section */ + +/* Begin XCConfigurationList section */ + 336C5FD925CC9F1500230C37 /* Build configuration list for PBXProject "OutlineViewReloadExample" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 336C5FED25CC9F1800230C37 /* Release */, + 94C6EB1029D86FFB007A19EB /* Debug */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + 336C5FEE25CC9F1800230C37 /* Build configuration list for PBXNativeTarget "OutlineViewReloadExample" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 336C5FF025CC9F1800230C37 /* Release */, + 94C6EB1129D86FFB007A19EB /* Debug */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; +/* End XCConfigurationList section */ + +/* Begin XCSwiftPackageProductDependency section */ + 336C600225CCAEE700230C37 /* OutlineView */ = { + isa = XCSwiftPackageProductDependency; + productName = OutlineView; + }; +/* End XCSwiftPackageProductDependency section */ + }; + rootObject = 336C5FD625CC9F1500230C37 /* Project object */; +} diff --git a/Examples/OutlineViewReloadExample/OutlineViewReloadExample.xcodeproj/xcshareddata/xcschemes/OutlineViewReloadExample.xcscheme b/Examples/OutlineViewReloadExample/OutlineViewReloadExample.xcodeproj/xcshareddata/xcschemes/OutlineViewReloadExample.xcscheme new file mode 100644 index 0000000..754ee61 --- /dev/null +++ b/Examples/OutlineViewReloadExample/OutlineViewReloadExample.xcodeproj/xcshareddata/xcschemes/OutlineViewReloadExample.xcscheme @@ -0,0 +1,77 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Examples/OutlineViewReloadExample/OutlineViewReloadExample/Assets.xcassets/AccentColor.colorset/Contents.json b/Examples/OutlineViewReloadExample/OutlineViewReloadExample/Assets.xcassets/AccentColor.colorset/Contents.json new file mode 100644 index 0000000..eb87897 --- /dev/null +++ b/Examples/OutlineViewReloadExample/OutlineViewReloadExample/Assets.xcassets/AccentColor.colorset/Contents.json @@ -0,0 +1,11 @@ +{ + "colors" : [ + { + "idiom" : "universal" + } + ], + "info" : { + "author" : "xcode", + "version" : 1 + } +} diff --git a/Examples/OutlineViewReloadExample/OutlineViewReloadExample/Assets.xcassets/AppIcon.appiconset/Contents.json b/Examples/OutlineViewReloadExample/OutlineViewReloadExample/Assets.xcassets/AppIcon.appiconset/Contents.json new file mode 100644 index 0000000..3f00db4 --- /dev/null +++ b/Examples/OutlineViewReloadExample/OutlineViewReloadExample/Assets.xcassets/AppIcon.appiconset/Contents.json @@ -0,0 +1,58 @@ +{ + "images" : [ + { + "idiom" : "mac", + "scale" : "1x", + "size" : "16x16" + }, + { + "idiom" : "mac", + "scale" : "2x", + "size" : "16x16" + }, + { + "idiom" : "mac", + "scale" : "1x", + "size" : "32x32" + }, + { + "idiom" : "mac", + "scale" : "2x", + "size" : "32x32" + }, + { + "idiom" : "mac", + "scale" : "1x", + "size" : "128x128" + }, + { + "idiom" : "mac", + "scale" : "2x", + "size" : "128x128" + }, + { + "idiom" : "mac", + "scale" : "1x", + "size" : "256x256" + }, + { + "idiom" : "mac", + "scale" : "2x", + "size" : "256x256" + }, + { + "idiom" : "mac", + "scale" : "1x", + "size" : "512x512" + }, + { + "idiom" : "mac", + "scale" : "2x", + "size" : "512x512" + } + ], + "info" : { + "author" : "xcode", + "version" : 1 + } +} diff --git a/Examples/OutlineViewReloadExample/OutlineViewReloadExample/Assets.xcassets/Contents.json b/Examples/OutlineViewReloadExample/OutlineViewReloadExample/Assets.xcassets/Contents.json new file mode 100644 index 0000000..73c0059 --- /dev/null +++ b/Examples/OutlineViewReloadExample/OutlineViewReloadExample/Assets.xcassets/Contents.json @@ -0,0 +1,6 @@ +{ + "info" : { + "author" : "xcode", + "version" : 1 + } +} diff --git a/Examples/OutlineViewReloadExample/OutlineViewReloadExample/ContentView.swift b/Examples/OutlineViewReloadExample/OutlineViewReloadExample/ContentView.swift new file mode 100644 index 0000000..87ee777 --- /dev/null +++ b/Examples/OutlineViewReloadExample/OutlineViewReloadExample/ContentView.swift @@ -0,0 +1,131 @@ +// +// ContentView.swift +// OutlineViewExample +// +// Created by Samar Sunkaria on 2/4/21. +// + +import SwiftUI +import OutlineView +import Cocoa + +struct FileItem: Hashable, Identifiable, CustomStringConvertible { + var id = UUID() + var name: String + var children: [FileItem]? = nil + var description: String { + switch children { + case nil: + return "📄 \(name)" + case .some(let children): + return children.isEmpty ? "📂 \(name)" : "📁 \(name)" + } + } +} + +let data = [ + FileItem(name: "doc001.txt"), + FileItem( + name: "users", + children: [ + FileItem( + name: "user1234", + children: [ + FileItem( + name: "Photos", + children: [ + FileItem(name: "photo001.jpg"), + FileItem(name: "photo002.jpg")]), + FileItem( + name: "Movies", + children: [FileItem(name: "movie001.mp4")]), + FileItem(name: "Documents", children: [])]), + FileItem( + name: "newuser", + children: [FileItem(name: "Documents", children: [])]) + ] + ) +] + +struct ContentView: View { + @Environment(\.colorScheme) var colorScheme + + @State var selection: FileItem? + @State var separatorColor: Color = Color(NSColor.separatorColor) + @State var separatorEnabled = false + + @State var rootTextColor: Color = Color(NSColor.textColor) + @State var childTextColor: Color = Color(NSColor.textColor) + + @State var outlineId = UUID() + + var body: some View { + VStack { + outlineView + Divider() + configBar + } + .background( + colorScheme == .light + ? Color(NSColor.textBackgroundColor) + : Color.clear + ) + } + + var rootIds: [UUID] { + data.map(\.id) + } + + var outlineView: some View { + OutlineView( + data, + selection: $selection, + children: \.children, + separatorInsets: { fileItem in + NSEdgeInsets( + top: 0, + left: 23, + bottom: 0, + right: 0) + } + ) { fileItem in + FileItemView(fileItem: fileItem, textColor: NSColor(rootIds.contains(fileItem.id) ? rootTextColor : childTextColor)) + } + .outlineViewStyle(.inset) + .outlineViewIndentation(20) + .rowSeparator(separatorEnabled ? .visible : .hidden) + .rowSeparatorColor(NSColor(separatorColor)) + .reloadIdentifier(outlineId) + .onChange(of: rootTextColor) { _ in + triggerReloadOfOutlineView(id: outlineId, itemIds: rootIds) + } + .onChange(of: childTextColor) { _ in + triggerReloadOfOutlineView(id: outlineId) + } + } + + var configBar: some View { + HStack { + ColorPicker( + "Root Text Color:", + selection: $rootTextColor) + Button( + "Set Child Text Color", + action: { self.childTextColor = self.rootTextColor }) + Spacer() + ColorPicker( + "Set separator color:", + selection: $separatorColor) + Button( + "Toggle separator", + action: { separatorEnabled.toggle() }) + } + .padding([.leading, .bottom, .trailing], 8) + } +} + +struct ContentView_Previews: PreviewProvider { + static var previews: some View { + ContentView() + } +} diff --git a/Examples/OutlineViewReloadExample/OutlineViewReloadExample/FileItemView.swift b/Examples/OutlineViewReloadExample/OutlineViewReloadExample/FileItemView.swift new file mode 100644 index 0000000..8f1c2b9 --- /dev/null +++ b/Examples/OutlineViewReloadExample/OutlineViewReloadExample/FileItemView.swift @@ -0,0 +1,38 @@ +// +// FileItemView.swift +// OutlineViewExample +// +// Created by Samar Sunkaria on 2/4/21. +// + +import Cocoa + +class FileItemView: NSTableCellView { + init(fileItem: FileItem, textColor: NSColor) { + let field = NSTextField(string: fileItem.description) + field.isEditable = false + field.isSelectable = false + field.isBezeled = false + field.drawsBackground = false + field.usesSingleLineMode = false + field.cell?.wraps = true + field.cell?.isScrollable = false + field.textColor = textColor + + super.init(frame: .zero) + + addSubview(field) + field.translatesAutoresizingMaskIntoConstraints = false + field.setContentCompressionResistancePriority(.defaultLow, for: .horizontal) + NSLayoutConstraint.activate([ + field.leadingAnchor.constraint(equalTo: leadingAnchor), + field.trailingAnchor.constraint(equalTo: trailingAnchor), + field.topAnchor.constraint(equalTo: topAnchor, constant: 4), + field.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -4), + ]) + } + + required init?(coder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } +} diff --git a/Examples/OutlineViewReloadExample/OutlineViewReloadExample/Info.plist b/Examples/OutlineViewReloadExample/OutlineViewReloadExample/Info.plist new file mode 100644 index 0000000..69c84ae --- /dev/null +++ b/Examples/OutlineViewReloadExample/OutlineViewReloadExample/Info.plist @@ -0,0 +1,24 @@ + + + + + CFBundleDevelopmentRegion + $(DEVELOPMENT_LANGUAGE) + CFBundleExecutable + $(EXECUTABLE_NAME) + CFBundleIdentifier + $(PRODUCT_BUNDLE_IDENTIFIER) + CFBundleInfoDictionaryVersion + 6.0 + CFBundleName + $(PRODUCT_NAME) + CFBundlePackageType + $(PRODUCT_BUNDLE_PACKAGE_TYPE) + CFBundleShortVersionString + 1.0 + CFBundleVersion + 1 + LSMinimumSystemVersion + $(MACOSX_DEPLOYMENT_TARGET) + + diff --git a/Examples/OutlineViewReloadExample/OutlineViewReloadExample/OutlineViewReloadExample.entitlements b/Examples/OutlineViewReloadExample/OutlineViewReloadExample/OutlineViewReloadExample.entitlements new file mode 100644 index 0000000..f2ef3ae --- /dev/null +++ b/Examples/OutlineViewReloadExample/OutlineViewReloadExample/OutlineViewReloadExample.entitlements @@ -0,0 +1,10 @@ + + + + + com.apple.security.app-sandbox + + com.apple.security.files.user-selected.read-only + + + diff --git a/Examples/OutlineViewReloadExample/OutlineViewReloadExample/OutlineViewReloadExampleApp.swift b/Examples/OutlineViewReloadExample/OutlineViewReloadExample/OutlineViewReloadExampleApp.swift new file mode 100644 index 0000000..a01f178 --- /dev/null +++ b/Examples/OutlineViewReloadExample/OutlineViewReloadExample/OutlineViewReloadExampleApp.swift @@ -0,0 +1,17 @@ +// +// OutlineViewExampleApp.swift +// OutlineViewExample +// +// Created by Samar Sunkaria on 2/4/21. +// + +import SwiftUI + +@main +struct OutlineViewExampleApp: App { + var body: some Scene { + WindowGroup { + ContentView() + } + } +} diff --git a/Examples/OutlineViewReloadExample/OutlineViewReloadExample/Preview Content/Preview Assets.xcassets/Contents.json b/Examples/OutlineViewReloadExample/OutlineViewReloadExample/Preview Content/Preview Assets.xcassets/Contents.json new file mode 100644 index 0000000..73c0059 --- /dev/null +++ b/Examples/OutlineViewReloadExample/OutlineViewReloadExample/Preview Content/Preview Assets.xcassets/Contents.json @@ -0,0 +1,6 @@ +{ + "info" : { + "author" : "xcode", + "version" : 1 + } +} diff --git a/Sources/OutlineView/OutlineView.swift b/Sources/OutlineView/OutlineView.swift index 35de294..4664493 100644 --- a/Sources/OutlineView/OutlineView.swift +++ b/Sources/OutlineView/OutlineView.swift @@ -25,6 +25,7 @@ where Drop.DataElement == Data.Element { @Binding var selection: Data.Element? var content: (Data.Element) -> NSView var separatorInsets: ((Data.Element) -> NSEdgeInsets)? + var reloadID = UUID() /// Outline view style is unavailable on macOS 10.15 and below. /// Stored as `Any` to make the property available on all platforms. @@ -75,6 +76,8 @@ where Drop.DataElement == Data.Element { outlineController.setDragSourceWriter(dragDataSource) outlineController.setDropReceiver(dropReceiver) outlineController.setAcceptedDragTypes(acceptedDropTypes) + + outlineController.setReloadID(reloadID) } } @@ -141,6 +144,15 @@ public extension OutlineView { mutableSelf.dragDataSource = writer return mutableSelf } + + /// Sets the unique reload identifier for the `OutlineView`, which can be + /// used to force data reloads using the static function + /// `triggerReloadOfOutlineView(id:itemIds:)` + func reloadIdentifier(_ reloadID: UUID) -> Self { + var mutableSelf = self + mutableSelf.reloadID = reloadID + return mutableSelf + } } // MARK: - Initializers for macOS 10.15 and higher. diff --git a/Sources/OutlineView/OutlineViewController.swift b/Sources/OutlineView/OutlineViewController.swift index d8a9dee..b01c6ab 100644 --- a/Sources/OutlineView/OutlineViewController.swift +++ b/Sources/OutlineView/OutlineViewController.swift @@ -1,4 +1,5 @@ import Cocoa +import Combine @available(macOS 10.15, *) public class OutlineViewController: NSViewController @@ -6,9 +7,11 @@ where Drop.DataElement == Data.Element { let outlineView = NSOutlineView() let scrollView = NSScrollView(frame: NSRect(x: 0, y: 0, width: 400, height: 400)) + var reloadID = UUID() let dataSource: OutlineViewDataSource let delegate: OutlineViewDelegate let updater = OutlineViewUpdater() + var reloadListener: AnyCancellable? let childrenSource: ChildSource @@ -56,6 +59,17 @@ where Drop.DataElement == Data.Element { scrollView.topAnchor.constraint(equalTo: view.topAnchor), scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor), ]) + + reloadListener = NotificationCenter + .default + .publisher(for: .OutlineViewReload) + .filter { [weak self] in + $0.outlineId == self?.reloadID + } + .sink { [weak self] note in + let itemIds = note.outlineItemIds(as: Data.Element.ID.self) + self?.reloadRowData(itemIds: itemIds) + } } required init?(coder: NSCoder) { @@ -128,6 +142,39 @@ extension OutlineViewController { outlineView.gridColor = color outlineView.reloadData() } + + /// Calls `reloadData()` on the outlineView. Currently does not + /// use individual itemIds, although if implemented you could + /// reload individual rows. OutlineViewController and DataSource + /// would need to be changed in that case. + func reloadRowData(itemIds: [Data.Element.ID]?) { + // If no itemIds are given, reload everything + guard let itemIds, + !itemIds.isEmpty + else { + outlineView.reloadData() + return + } + + var itemsToReload = Set(itemIds) + let rowCount = outlineView.numberOfRows + var currentRow = 0 + + // Reload individual rows while IDs are available + while currentRow < rowCount, + !itemsToReload.isEmpty + { + if let rowItem = outlineView.item(atRow: currentRow), + let identifiableItem = rowItem as? any Identifiable, + let itemId = identifiableItem.id as? Data.Element.ID, + itemsToReload.contains(itemId) + { + outlineView.reloadItem(rowItem, reloadChildren: outlineView.isItemExpanded(rowItem)) + itemsToReload.remove(itemId) + } + currentRow += 1 + } + } func setDragSourceWriter(_ writer: DragSourceWriter?) { dataSource.dragWriter = writer @@ -145,4 +192,8 @@ extension OutlineViewController { outlineView.registerForDraggedTypes(acceptedTypes) } } + + func setReloadID(_ newID: UUID) { + self.reloadID = newID + } } diff --git a/Sources/OutlineView/OutlineViewReloading.swift b/Sources/OutlineView/OutlineViewReloading.swift new file mode 100644 index 0000000..70ea28b --- /dev/null +++ b/Sources/OutlineView/OutlineViewReloading.swift @@ -0,0 +1,51 @@ + +import Combine +import Foundation + +extension Notification.Name { + static var OutlineViewReload: Self { + .init("ReloadOutlineViewNotification") + } +} + +/// Forces an `OutlineView` to reload its row data, which may +/// be necessary if normal state property changes don't cause +/// data updates. +/// +/// - Parameter id: The reloadIdentifier of the `OutlineView` to reload. +public func triggerReloadOfOutlineView(id: UUID) { + NotificationCenter.default.post( + name: .OutlineViewReload, + object: nil, + userInfo: ["id" : id] + ) +} + +/// Forces an `OutlineView` to reload its row data for a given group +/// of rows by id, which may be necessary if normal state property +/// changes don't cause data updates. +/// +/// - Parameters: +/// - id: The reloadIdentifier of the `OutlineView` to reload. +/// - itemIds: Array of ids of items in the `OutlineView` that need to +/// be reloaded. +public func triggerReloadOfOutlineView(id: UUID, itemIds: [L]) { + NotificationCenter.default.post( + name: .OutlineViewReload, + object: nil, + userInfo: [ + "id" : id, + "items" : itemIds + ] + ) +} + +extension Notification { + var outlineId: UUID? { + userInfo?["id"] as? UUID + } + + func outlineItemIds(as type: L.Type) -> [L]? { + userInfo?["items"] as? [L] + } +}