Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ To note a change, add a line under Unreleased, in a group such as Added, Changed

### Added

- Documentation for the persistence and media APIs, built with DocC: doc comments in their headers, which Xcode shows in Quick Help, and an article with examples for each.
- Documentation for the whole API, built with DocC: doc comments in the public headers, which Xcode shows in Quick Help, and an article with examples for each of CoreZen's three parts: persistence, media and utilities.
- `-[ZENFrameRenderer renderExactFrameAtSeconds:width:height:completion:]`, which renders the frame a player shows at a time, rather than the keyframe at or before it.

### Fixed
Expand Down
12 changes: 10 additions & 2 deletions CoreZen/Categories/NSFileManager+CoreZen.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,24 @@ NS_ASSUME_NONNULL_BEGIN

@interface NSFileManager (CoreZen)

// nil, with outError set, if the directory can't be found or created
/// Finds a standard folder, such as Application Support, with a path component
/// appended if one is given, and creates it if asked to.
///
/// It returns nil, with the error set, if the folder can't be found or created.
- (nullable NSURL *)zen_findOrCreateURLForDirectory:(NSSearchPathDirectory)searchPathDirectory
inDomain:(NSSearchPathDomainMask)domain
appendPathComponent:(nullable NSString *)appendComponent
create:(BOOL)create
error:(NSError *_Nullable *_Nullable)outError;

/// The app's own folder in the user's Application Support folder, named after
/// its executable, which this creates if there's none.
///
/// It returns nil, and logs an error, if the folder can't be found or created.
- (nullable NSURL *)zen_applicationSupportDirectory;

// nil if the main bundle doesn't name its executable
/// The name of the main bundle's executable, or nil if the bundle doesn't name
/// it.
+ (nullable NSString *)zen_executableName;

@end
Expand Down
3 changes: 3 additions & 0 deletions CoreZen/Categories/NSNumber+CoreZen.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

@interface NSNumber (CoreZen)

/// A random integer, from a cryptographically secure generator.
///
/// It can be any `NSInteger` value, negative ones included.
+ (NSInteger)zen_randomInteger;

@end
36 changes: 26 additions & 10 deletions CoreZen/Categories/NSURL+CoreZen.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,46 @@ NS_ASSUME_NONNULL_BEGIN

@interface NSURL (CoreZen)

// outVolumeName and outVolumeURL are required; outVolumeUUID and outError may be NULL
/// Gets the name, URL and UUID of the volume this URL is on.
///
/// The name and URL pointers are required; the UUID and error pointers may be
/// NULL. It returns NO, with the error set, if the volume's details can't be
/// read. The UUID can be nil even when it returns YES, because some volumes,
/// like many network shares, have none.
- (BOOL)zen_volumeName:(NSString *_Nullable *_Nonnull)outVolumeName
volumeURL:(NSURL *_Nullable *_Nonnull)outVolumeURL
volumeUUID:(NSUUID *_Nullable *_Nullable)outVolumeUUID
error:(NSError *_Nullable *_Nullable)outError;

/// Finds the mounted volume with a UUID, and gets its name and URL.
///
/// It returns NO if no mounted volume that isn't hidden has that UUID, or if
/// its details can't be read.
+ (BOOL)zen_volumeInfoForUUID:(NSUUID *)volumeUUID
volumeName:(NSString *_Nullable *_Nonnull)outVolumeName
volumeURL:(NSURL *_Nullable *_Nonnull)outVolumeURL;

// Path of url relative to this URL, or nil if url is neither this URL nor
// inside it. The result keeps the separator after this URL's path, e.g.
// /Volumes/Media to /Volumes/Media/Movies/a.mp4 gives @"/Movies/a.mp4". From
// the root URL "/" it has no leading separator, and for the same path it's @"".
/// The path of a URL relative to this one, or nil if the URL is neither this
/// one nor inside it.
///
/// The result keeps the separator after this URL's path: /Volumes/Media to
/// /Volumes/Media/Movies/a.mp4 gives @"/Movies/a.mp4". From the root URL "/" it
/// has no leading separator, and for the same path it's @"". A path only counts
/// as inside this one at a separator, so /Volumes/Media2 isn't inside
/// /Volumes/Media.
- (nullable NSString *)zen_relativePathToURL:(NSURL *)url;

// Inverse of zen_relativePathToURL:. Appends relativePath to this URL's path as
// a file system path, so a leading separator and characters like " ", "#" and
// "%" are taken literally, not as URL syntax. /Volumes/Media with
// @"/Movies/a.mp4" gives /Volumes/Media/Movies/a.mp4. Returns nil if this URL
// has no path or relativePath is nil.
/// Appends a relative path to this URL's path, undoing
/// `zen_relativePathToURL:`.
///
/// The relative path is a file system path, so a leading separator and
/// characters like " ", "#" and "%" are taken literally, not as URL syntax.
/// /Volumes/Media with @"/Movies/a.mp4" gives /Volumes/Media/Movies/a.mp4. It
/// returns nil if this URL has no path, or the relative path is nil.
- (nullable NSURL *)zen_URLByAppendingRelativePath:(nullable NSString *)relativePath
isDirectory:(BOOL)isDirectory;

/// The size in bytes of the file at this URL, or 0 if it can't be read.
- (NSUInteger)zen_fileSize;

@end
Expand Down
6 changes: 6 additions & 0 deletions CoreZen/Categories/NSView+CoreZen.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ NS_ASSUME_NONNULL_BEGIN

@interface NSView (CoreZen)

/// Adds constraints that make a subview fill this view.
///
/// They tie the subview's left and top edges, width and height to this view's.
/// The subview's autoresizing mask would add constraints that conflict with
/// these, so a subview made in code needs
/// `translatesAutoresizingMaskIntoConstraints` set to NO.
- (void)zen_addConstraintsForEqualSizes:(NSView *)other;

@end
Expand Down
6 changes: 6 additions & 0 deletions CoreZen/CoreZen.docc/CoreZen.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,9 @@ CoreZen's [README](https://github.com/znelson/CoreZen#readme) says how to get it

- <doc:Persistence>
- <doc:Media>
- <doc:Utilities>

### Framework Version

- ``CoreZenVersionNumber``
- ``CoreZenVersionString``
73 changes: 73 additions & 0 deletions CoreZen/CoreZen.docc/Utilities.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# Utilities

A work queue whose work can be canceled, a tree of nodes with sizes, a preferences window, views loaded from nibs, and additions to Foundation and AppKit classes.

## Overview

- ``ZENWorkQueue`` is a serial queue whose async work can be canceled, and which can be terminated.
- ``ZENNode`` is a tree of named nodes with sizes, where every node's size stays the total of its children's.
- ``ZENPreferencesWindowController`` is a preferences window with a toolbar item for each pane.
- ``ZENArchivedView-c.class`` is a view that loads its contents from a nib its subclass names.

### Cancel Queued Work

```objc
ZENWorkQueue *queue = [ZENWorkQueue workQueue:@"com.example.thumbnails"];
ZENWorkQueueToken *work = [queue async:^(ZENWorkQueueToken *token) {
if (token.canceled) {
return;
}
// Do the work
}];

// If the work is no longer needed
[work cancel];

// Before the app quits
[queue terminate:^{
// Runs once the work already queued has run
}];
```

A block that's queued always runs, so it checks its token to learn whether it should skip its work.

### Total Sizes in a Tree

```objc
ZENNode *movies = [[ZENNode alloc] initWithName:@"Movies"];
[movies addChildNode:[[ZENNode alloc] initWithName:@"a.mp4" size:700]];
[movies addChildNode:[[ZENNode alloc] initWithName:@"b.mp4" size:300]];
// movies.size is 1000
```

### Foundation and AppKit Additions

CoreZen adds methods, named with a `zen_` prefix, to a few Foundation and AppKit classes. Xcode's Quick Help shows their documentation, but they have no pages here, because Xcode's documentation build leaves out categories on other frameworks' classes.

- term `NSURL`: `zen_volumeName:volumeURL:volumeUUID:error:` gets the name, URL and UUID of the volume a URL is on, and `zen_volumeInfoForUUID:volumeName:volumeURL:` finds a volume again by its UUID. Some volumes, like many network shares, have no UUID. `zen_relativePathToURL:` gives a URL's path relative to another URL, such as its volume's, and `zen_URLByAppendingRelativePath:isDirectory:` turns it back into a URL. `zen_fileSize` gives a file's size.
- term `NSFileManager`: `zen_findOrCreateURLForDirectory:inDomain:appendPathComponent:create:error:` finds or creates a standard folder, `zen_applicationSupportDirectory` gives the app's own folder in Application Support, and `zen_executableName` gives the name of the app's executable.
- term `NSView`: `zen_addConstraintsForEqualSizes:` makes a subview fill a view.
- term `NSNumber`: `zen_randomInteger` gives a random integer.

## Topics

### Work Queues

- ``ZENWorkQueue``
- ``ZENWorkQueueToken``
- ``ZENWorkQueueBlock``
- ``ZENWorkQueueCancelBlock``

### Trees

- ``ZENNode``

### Preferences

- ``ZENPreferencesWindowController``
- ``ZENPreferenceViewController``

### Views

- ``ZENArchivedView-c.class``
- ``ZENArchivedView-c.protocol``
49 changes: 49 additions & 0 deletions CoreZen/CoreZen.docc/ZENNode.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# ``ZENNode``

## Topics

### Creating a Node

- ``initWithName:``
- ``initWithName:size:``
- ``copyChildren:``

### Identifying a Node

- ``nodeID``
- ``name``

### Size

- ``size``
- ``resetSize``

### The Tree

- ``parent``
- ``children``
- ``childCount``
- ``isChildless``
- ``rootNode``
- ``countLeaves``
- ``countChildrenAndDescendants``

### Adding and Removing Nodes

- ``addChildNode:``
- ``removeChildNode:``

### Enumerating Nodes

- ``enumerateChildrenWithBlock:``
- ``enumerateParentsWithBlock:``
- ``enumerateDepthFirstUsingBlock:``
- ``enumerateBreadthFirstUsingBlock:``
- ``ZENNodeEnumerateBlock``

### Learning About Changes

- ``childWasAdded:``
- ``childWasRemoved:``
- ``descendantWasAdded:``
- ``descendantWasRemoved:``
80 changes: 73 additions & 7 deletions CoreZen/Node/Node.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,53 +10,119 @@
@class ZENNode;
NS_ASSUME_NONNULL_BEGIN

/// A block called for each node an enumeration visits, with its place in the
/// order they're visited, from 0.
///
/// Set `stop` to YES to end the enumeration.
typedef void (^ZENNodeEnumerateBlock)(id node, NSUInteger index, BOOL *stop);

/// A node in a tree of named nodes with sizes.
///
/// A leaf has its own size, and a node with children is as big as they are
/// together. Adding, removing and moving nodes keeps every total in the tree up
/// to date. A subclass can learn about changes below a node by overriding
/// ``childWasAdded:`` and the methods like it. Copying a node copies the nodes
/// below it too.
@interface ZENNode : NSObject <NSCopying>

/// A unique identifier, made with the node. A copy gets a new one.
@property (nonatomic, readonly, strong) NSUUID *nodeID;

/// The node's name.
@property (nonatomic, readonly, strong, nullable) NSString *name;

// A leaf's own size, from init. A node with children is as big as they are
// together, so a leaf's own size gives way when it gets its first child. Adding,
// removing and moving nodes keeps every total in the tree up to date.
/// The node's size: a leaf's own, or else the total of its children's.
///
/// A leaf's own size comes from init. A node with children is as big as they
/// are together, so a leaf's own size gives way when it gets its first child.
/// Adding, removing and moving nodes keeps every total in the tree up to date.
@property (nonatomic, readonly) NSInteger size;

/// The node's parent, or nil if it has none.
@property (nonatomic, readonly, weak, nullable) ZENNode *parent;

/// The node's children, in the order they were added.
@property (nonatomic, readonly, strong) NSArray *children;

/// The number of the node's children.
@property (nonatomic, readonly) NSUInteger childCount;

/// Whether the node has no children.
@property (nonatomic, readonly) BOOL isChildless;

/// Makes a node with a name, and a size of 0.
- (instancetype)initWithName:(nullable NSString *)name;

/// Makes a node with a name and a size.
- (instancetype)initWithName:(nullable NSString *)name
size:(NSInteger)size;

/// The top of the node's tree: its highest ancestor, or the node itself if it
/// has no parent.
- (id)rootNode;

/// The number of leaves below the node.
- (NSUInteger)countLeaves;

/// The number of nodes below the node.
- (NSUInteger)countChildrenAndDescendants;

/// Adds a node as the last child, moving it from its old parent if it has one.
///
/// Adding one of the node's children again does nothing.
- (void)addChildNode:(id)child;

/// Removes a child, if it's one of the node's children.
- (void)removeChildNode:(id)child;

/// Adds a copy of each of another node's children to this node.
- (void)copyChildren:(id)node;

// For subclasses to override, calling super. They run after a child of this node,
// or a node deeper below it, is added or removed, when sizes are already updated.
/// Called after a child is added to this node.
///
/// A subclass can override it, calling super. Sizes are already updated when
/// it's called.
- (void)childWasAdded:(id)child;

/// Called after a child is removed from this node.
///
/// A subclass can override it, calling super. Sizes are already updated when
/// it's called.
- (void)childWasRemoved:(id)child;

/// Called after a node is added deeper below this node, to one of its
/// descendants.
///
/// A subclass can override it, calling super. Sizes are already updated when
/// it's called.
- (void)descendantWasAdded:(id)descendant;

/// Called after a node is removed from deeper below this node, from one of its
/// descendants.
///
/// A subclass can override it, calling super. Sizes are already updated when
/// it's called.
- (void)descendantWasRemoved:(id)descendant;

// Sets the size to the total of the children's sizes, and updates the ancestors'
// totals to match. On a leaf, this drops the size it was made with.
/// Sets the size to the total of the children's sizes, and updates the
/// ancestors' totals to match.
///
/// On a leaf, this drops the size it was made with.
- (void)resetSize;

/// Calls a block for each of the node's children, in order.
- (void)enumerateChildrenWithBlock:(ZENNodeEnumerateBlock)block;

/// Calls a block for each of the node's ancestors, from its parent up to the
/// top of the tree.
- (void)enumerateParentsWithBlock:(ZENNodeEnumerateBlock)block;

/// Calls a block for every node below this one, depth first, visiting each node
/// after the nodes below it.
- (void)enumerateDepthFirstUsingBlock:(ZENNodeEnumerateBlock)block;

/// Calls a block for every node below this one, breadth first: the children,
/// then the grandchildren, and so on.
- (void)enumerateBreadthFirstUsingBlock:(ZENNodeEnumerateBlock)block;

@end
Expand Down
Loading