ample/Ample/MediaViewController.m

547 lines
12 KiB
Mathematica
Raw Normal View History

2020-08-21 22:38:02 +00:00
//
// MediaViewController.m
2020-08-30 03:24:49 +00:00
// Ample
2020-08-21 22:38:02 +00:00
//
// Created by Kelvin Sherlock on 8/20/2020.
// Copyright © 2020 Kelvin Sherlock. All rights reserved.
//
#import "MediaViewController.h"
2020-08-22 04:13:23 +00:00
@implementation TablePathView
@end
@protocol MediaNode
-(BOOL)isGroupItem;
-(BOOL)isExpandable;
-(NSInteger) count;
-(NSString *)viewIdentifier;
-(void)prepareView: (NSTableCellView *)view;
-(CGFloat)height;
-(NSInteger)index;
2020-08-22 04:13:23 +00:00
@end
@interface MediaCategory : NSObject <MediaNode> {
2020-08-21 22:38:02 +00:00
}
@property NSInteger validCount;
@property NSArray *children; // URLs?
@property NSString *title;
@property NSInteger index;
2020-08-21 22:38:02 +00:00
-(NSInteger)count;
-(id)objectAtIndex:(NSInteger)index;
-(BOOL)isGroupItem;
@end
2020-08-25 00:23:56 +00:00
@interface MediaItem : NSObject <MediaNode>
@property NSURL *url;
@property BOOL valid;
@property NSInteger index;
2020-08-25 00:23:56 +00:00
-(NSInteger)count;
-(id)objectAtIndex:(NSInteger)index;
-(BOOL)isGroupItem;
-(void)invalidate;
@end
2020-08-21 22:38:02 +00:00
@implementation MediaCategory
-(instancetype)initWithTitle: (NSString *)title {
[self setTitle: title];
return self;
}
-(NSInteger) count {
return [_children count];
}
-(id)objectAtIndex:(NSInteger)index {
return [_children objectAtIndex: index];
}
-(BOOL)isGroupItem {
return YES;
}
-(BOOL)isExpandable {
return YES;
}
2020-08-22 04:13:23 +00:00
-(NSString *)viewIdentifier {
return @"CategoryView";
}
-(void)prepareView: (NSTableCellView *)view {
[[view textField] setStringValue: _title];
}
-(CGFloat)height {
return 17;
}
2020-08-21 22:38:02 +00:00
2020-08-25 00:23:56 +00:00
-(BOOL)setItemCount: (unsigned)newCount {
2020-08-25 04:35:40 +00:00
if (newCount == _validCount) {
return NO;
}
unsigned count = (unsigned)[_children count];
2020-08-25 00:23:56 +00:00
NSMutableArray *tmp = [NSMutableArray arrayWithArray: _children];
_validCount = newCount;
2020-08-25 04:35:40 +00:00
for (unsigned i = count; i < newCount; ++i) {
MediaItem *item = [MediaItem new];
[item setIndex: i];
[tmp addObject: item];
2020-08-25 00:23:56 +00:00
}
2020-08-25 00:23:56 +00:00
// delete excess items, if blank. otherwise, mark invalid.
unsigned ix = 0;
for(MediaItem *item in tmp) {
2020-08-25 04:35:40 +00:00
[item setValid: ix++ < newCount];
2020-08-25 00:23:56 +00:00
}
2020-08-25 04:35:40 +00:00
for (unsigned i = newCount; i < count; ++i) {
2020-08-25 00:23:56 +00:00
MediaItem *item = [tmp lastObject];
if ([item url]) break;
[tmp removeLastObject];
}
2020-08-21 22:38:02 +00:00
2020-08-25 00:23:56 +00:00
[self setChildren: tmp];
return YES;
2020-08-21 22:38:02 +00:00
}
2020-08-25 04:35:40 +00:00
-(BOOL)pruneChildren {
NSUInteger count = [_children count];
BOOL delta = NO;
if (_validCount == count) return NO;
NSMutableArray *tmp = [NSMutableArray arrayWithArray: _children];
for (NSInteger i = _validCount; i < count; ++i) {
MediaItem *item = [tmp lastObject];
if ([item url]) break;
[tmp removeLastObject];
delta = YES;
}
if (delta) {
[self setChildren: tmp];
return YES;
}
return NO;
}
2020-08-21 22:38:02 +00:00
@end
2020-08-25 00:23:56 +00:00
2020-08-21 22:38:02 +00:00
@implementation MediaItem
-(instancetype)initWithURL: (NSURL *)url {
[self setUrl: url];
return self;
}
-(NSInteger) count {
return 0;
}
-(id)objectAtIndex:(NSInteger)index {
return nil;
}
-(BOOL)isGroupItem {
return NO;
}
-(BOOL)isExpandable {
return NO;
}
2020-08-22 04:13:23 +00:00
-(NSString *)viewIdentifier {
return @"ItemView";
}
-(void)prepareView: (TablePathView *)view {
NSPathControl *pc = [view pathControl];
NSButton *button = [view ejectButton];
2020-08-22 04:13:23 +00:00
[pc unbind: @"value"];
[pc bind: @"value" toObject: self withKeyPath: @"url" options: nil];
2020-08-25 04:35:40 +00:00
[button unbind: @"enabled"];
NSValueTransformer *t = [NSValueTransformer valueTransformerForName: NSIsNotNilTransformerName];
NSDictionary *options = @{ NSValueTransformerBindingOption: t};
[button bind: @"enabled" toObject: self withKeyPath: @"url" options: options];
2020-08-25 04:35:40 +00:00
NSColor *tintColor = nil;
if (!_valid) tintColor = [NSColor redColor];
[button setContentTintColor: tintColor];
2020-08-21 22:38:02 +00:00
}
2020-08-22 04:13:23 +00:00
-(CGFloat)height {
return 27;
}
2020-08-21 22:38:02 +00:00
2020-08-25 00:23:56 +00:00
-(void)invalidate {
_valid = NO;
}
2020-08-21 22:38:02 +00:00
@end
@interface MediaViewController () {
2020-08-25 00:23:56 +00:00
MediaCategory *_data[5];
2020-08-21 22:38:02 +00:00
NSArray *_root;
2020-08-25 04:35:40 +00:00
NSDictionary *_media;
2020-08-21 22:38:02 +00:00
}
@end
@implementation MediaViewController
-(void)awakeFromNib {
2020-08-22 04:13:23 +00:00
static unsigned first = 0;
if (first) return;
first++;
2020-08-25 00:23:56 +00:00
MediaCategory *a, *b, *c, *d, *e;
2020-08-21 22:38:02 +00:00
a = [[MediaCategory alloc] initWithTitle: @"5.25\" Floppies"];
b = [[MediaCategory alloc] initWithTitle: @"3.5\" Floppies"];
c = [[MediaCategory alloc] initWithTitle: @"Hard Drives"];
2020-08-25 00:23:56 +00:00
d = [[MediaCategory alloc] initWithTitle: @"CD-ROMs"];
e = [[MediaCategory alloc] initWithTitle: @"Casettes"];
2020-08-21 22:38:02 +00:00
_data[0] = a;
_data[1] = b;
_data[2] = c;
_data[3] = d;
2020-08-25 00:23:56 +00:00
_data[4] = e;
2020-08-25 04:35:40 +00:00
_root = @[];
2020-08-21 22:38:02 +00:00
2020-08-25 00:23:56 +00:00
}
enum {
kIndexFloppy_5_25 = 0,
kIndexFloppy_3_5,
kIndex_HardDrive,
kIndexCDROM,
kIndexCassette
};
2020-08-25 04:35:40 +00:00
2020-08-26 00:13:37 +00:00
-(void)rebuildArgs {
static char* prefix[] = {
"flop", "flop", "hard", "cdrm", "cass"
};
NSMutableArray *args = [NSMutableArray new];
unsigned counts[5] = { 0, 0, 0, 0, 0 };
for (unsigned j = 0; j < 5; ++j) {
MediaCategory *cat = _data[j];
NSInteger valid = [cat validCount];
for (NSInteger i = 0; i < valid; ++i) {
counts[j]++;
MediaItem *item = [cat objectAtIndex: i];
NSURL *url = [item url];
if (!url) continue;
[args addObject: [NSString stringWithFormat: @"-%s%u", prefix[j], counts[j]]];
NSString *s = [NSString stringWithCString: [url fileSystemRepresentation] encoding: NSUTF8StringEncoding];
[args addObject: s];
}
if (j == 0) counts[1] = counts[0]; // 3.5/5.25
}
[self setArgs: args];
}
2020-08-25 04:35:40 +00:00
-(void)rebuildRoot {
NSMutableArray *tmp = [NSMutableArray new];
int ix = 0;
2020-08-25 04:35:40 +00:00
for (unsigned j = 0 ; j < 5; ++j) {
MediaCategory *cat = _data[j];
[cat setIndex: -1];
if ([cat count]) {
[cat setIndex: ix++];
[tmp addObject: cat];
}
2020-08-25 04:35:40 +00:00
}
_root = tmp;
[_outlineView reloadData];
[_outlineView expandItem: nil expandChildren: YES];
}
2020-08-25 00:23:56 +00:00
-(void)setMedia: (NSDictionary *)media {
static NSString *Keys[] = {
@"flop_5_25",
@"flop_3_5",
@"hard",
@"cdrm",
@"cass"
};
2020-08-25 04:35:40 +00:00
2020-08-25 00:23:56 +00:00
NSNumber *o;
MediaCategory *cat;
unsigned i;
BOOL delta = NO;
2020-08-25 04:35:40 +00:00
if (_media == media) return;
_media = media;
2020-08-25 00:23:56 +00:00
for (unsigned j = 0; j < 5; ++j) {
o = [media objectForKey: Keys[j]];
i = [o unsignedIntValue];
cat = _data[j];
delta |= [cat setItemCount: i];
}
2020-08-26 00:13:37 +00:00
if (delta) {
[self rebuildRoot];
[self rebuildArgs];
}
2020-08-21 22:38:02 +00:00
}
static NSString *kDragType = @"private.ample.media";
2020-08-21 22:38:02 +00:00
- (void)viewDidLoad {
[super viewDidLoad];
//NSOutlineView *view = [self view];
//[view expandItem: nil expandChildren: YES];
// Do view setup here.
2020-09-04 23:22:18 +00:00
2020-08-25 04:35:40 +00:00
[_outlineView reloadData];
2020-08-22 04:13:23 +00:00
[_outlineView expandItem: nil expandChildren: YES];
[_outlineView registerForDraggedTypes: @[kDragType]];
2020-08-21 22:38:02 +00:00
}
#pragma mark - NSOutlineViewDelegate
2020-08-22 04:13:23 +00:00
2020-08-21 22:38:02 +00:00
- (void)outlineView:(NSOutlineView *)outlineView didAddRowView:(NSTableRowView *)rowView forRow:(NSInteger)row {
}
- (void)outlineView:(NSOutlineView *)outlineView didRemoveRowView:(NSTableRowView *)rowView forRow:(NSInteger)row {
}
//- (NSTableRowView *)outlineView:(NSOutlineView *)outlineView rowViewForItem:(id)item;
2020-08-22 04:13:23 +00:00
- (NSView *)outlineView:(NSOutlineView *)outlineView viewForTableColumn:(NSTableColumn *)tableColumn item:(id<MediaNode>)item {
2020-08-21 22:38:02 +00:00
2020-08-22 04:13:23 +00:00
NSString *ident = [item viewIdentifier];
if (!ident) return nil;
NSTableCellView *v = [outlineView makeViewWithIdentifier: ident owner: self];
[(id<MediaNode>)item prepareView: v];
2020-08-21 22:38:02 +00:00
return v;
}
2020-08-22 04:13:23 +00:00
- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id<MediaNode>)item {
2020-08-21 22:38:02 +00:00
return [item isExpandable];
}
- (BOOL)outlineView:(NSOutlineView *)outlineView isGroupItem:(id)item {
2020-08-22 19:55:16 +00:00
return NO; //[item isGroupItem];
2020-08-21 22:38:02 +00:00
}
- (BOOL)outlineView:(NSOutlineView *)outlineView shouldShowOutlineCellForItem:(id)item {
2020-08-22 04:13:23 +00:00
return NO;
2020-08-21 22:38:02 +00:00
}
2020-08-22 04:13:23 +00:00
/*
2020-08-21 22:38:02 +00:00
- (BOOL)outlineView:(NSOutlineView *)outlineView shouldShowCellExpansionForTableColumn:(NSTableColumn *)tableColumn item:(id)item {
2020-08-22 04:13:23 +00:00
return NO;
2020-08-21 22:38:02 +00:00
}
2020-08-22 04:13:23 +00:00
*/
2020-08-21 22:38:02 +00:00
2020-08-22 04:13:23 +00:00
-(BOOL)outlineView:(NSOutlineView *)outlineView shouldCollapseItem:(id)item {
return NO;
}
2020-08-21 22:38:02 +00:00
- (NSCell *)outlineView:(NSOutlineView *)outlineView dataCellForTableColumn:(NSTableColumn *)tableColumn item:(id)item {
2020-08-22 04:13:23 +00:00
//return nil;
return [[item cellClass] new];
2020-08-21 22:38:02 +00:00
}
#pragma mark - NSOutlineViewDataSource
// nil item indicates the root.
- (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item {
if (item == nil)
return [_root count];
return [item count];
}
- (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item {
if (item == nil) {
return [_root objectAtIndex: index];
}
return [item objectAtIndex: index];
}
2020-08-22 04:13:23 +00:00
-(CGFloat)outlineView:(NSOutlineView *)outlineView heightOfRowByItem:(id<MediaNode>)item {
return [item height];
}
#if 0
- (id<NSPasteboardWriting>)outlineView:(NSOutlineView *)outlineView pasteboardWriterForItem:(id<MediaNode>)item {
if ([item isGroupItem]) return nil;
NSPasteboardItem *pb = [NSPasteboardItem new];
return pb;
}
#endif
- (BOOL)outlineView:(NSOutlineView *)outlineView writeItems:(NSArray *)items toPasteboard:(NSPasteboard *)pasteboard {
if ([items count] > 1) return NO;
NSLog(@"%s", sel_getName(_cmd));
MediaItem *item = [items firstObject];
if (![item isKindOfClass: [MediaItem class]]) return NO;
// find the category. only allow if more than 1 item in the category.
MediaCategory *cat = nil;
for (MediaCategory *c in _root) {
NSUInteger ix = [[c children] indexOfObject: item];
if (ix != NSNotFound){
cat = c;
break;
}
}
if (!cat) return NO;
if ([cat count] < 2) return NO;
NSInteger indexes[2] = { 0, 0 };
indexes[0] = [cat index];
indexes[1] = [item index];
NSData *data =[NSData dataWithBytes: indexes length: sizeof(indexes)];
[pasteboard setData: data forType: kDragType];
return YES;
}
/*
* IF item is present, it's a MediaCategory and index is the index of the MediaItem it would be inserted as.
* IF item is nil, index is the MediaCategory index, which should be converted to moving to the end.
* IF index < 0, dragging far beyond the category list, so NOPE it.
*
*/
- (NSDragOperation)outlineView:(NSOutlineView *)outlineView validateDrop:(id<NSDraggingInfo>)info proposedItem:(id)item proposedChildIndex:(NSInteger)index {
if (index < 0) return NSDragOperationNone;
// item is the parent (MediaCategory) or nil
// index is the proposed child index.
NSLog(@"%s", sel_getName(_cmd));
//NSLog(@"%@", info);
NSLog(@"%@", item);
NSLog(@"%d", (int)index);
NSPasteboard *pb = [info draggingPasteboard];
NSData *data = [pb dataForType: kDragType];
if (!data) return NSDragOperationNone;
NSInteger indexes[2];
if ([data length] != sizeof(indexes)) return NSDragOperationNone;
[data getBytes: &indexes length: sizeof(indexes)];
NSLog(@"%d - %d", (int)indexes[0], (int)indexes[1]);
if (!item) {
// move to the END of the previous category.
if (index == 0) return NSDragOperationNone;
item = [_root objectAtIndex: index - 1];
index = [(MediaItem *)item count]; // -1; - interferes w/ -1 logic below.
}
NSLog(@"%d - %d", (int)[(MediaCategory *)item index], (int)index);
if ([(MediaCategory *)item index] != indexes[0]) return NSDragOperationNone;
if (indexes[1] == index) return NSDragOperationNone;
if (indexes[1] == index-1) return NSDragOperationNone;
return NSDragOperationMove;
}
- (BOOL)outlineView:(NSOutlineView *)outlineView acceptDrop:(id<NSDraggingInfo>)info item:(id)item childIndex:(NSInteger)index {
return NO;
}
2020-08-21 22:38:02 +00:00
2020-08-22 04:13:23 +00:00
#pragma mark - IBActions
- (IBAction)ejectAction:(id)sender {
2020-08-22 04:13:23 +00:00
NSInteger row = [_outlineView rowForView: sender];
if (row < 0) return;
2020-08-21 22:38:02 +00:00
2020-08-22 04:13:23 +00:00
//TablePathView *pv = [_outlineView viewAtColumn: 0 row: row makeIfNecessary: NO];
MediaItem *item = [_outlineView itemAtRow: row];
[item setUrl: nil];
//[[pv pathControl] setURL: nil];
2020-08-25 04:35:40 +00:00
// if item is invalid, should attempt to remove...
if (![item valid]) {
MediaCategory *cat = [_outlineView parentForItem: item];
if ([cat pruneChildren]) [self rebuildRoot];
}
2020-08-26 00:13:37 +00:00
[self rebuildArgs];
}
- (IBAction)pathAction:(id)sender {
// need to update the eject button...
2020-08-26 00:13:37 +00:00
[self rebuildArgs];
2020-08-22 04:13:23 +00:00
}
2020-08-21 22:38:02 +00:00
@end