From 64ed23e933773c1da0ac70f0b2eec7886e49d81f Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Sun, 2 Aug 2015 11:25:39 -0400 Subject: [PATCH] Changed the Auto Layout generation function to use a structure of parameters. This will make adding parameters easier. --- redo/osxaltest/box.m | 28 ++++++++++++++---------- redo/osxaltest/button.m | 17 +++++--------- redo/osxaltest/osxaltest.h | 18 ++++++++++----- redo/osxaltest/spinbox.m | 17 +++++--------- redo/osxaltest/window.m | 45 +++++++++++++++++++------------------- 5 files changed, 63 insertions(+), 62 deletions(-) diff --git a/redo/osxaltest/box.m b/redo/osxaltest/box.m index 490c2dcb..bd6fcb46 100644 --- a/redo/osxaltest/box.m +++ b/redo/osxaltest/box.m @@ -45,32 +45,35 @@ [self tRelayout]; } -- (void)tFillAutoLayoutHorz:(NSMutableArray *)horz - vert:(NSMutableArray *)vert - extra:(NSMutableArray *)extra - extraVert:(NSMutableArray *)extraVert - views:(NSMutableDictionary *)views - first:(uintmax_t *)n +- (void)tFillAutoLayout:(tAutoLayoutParams *)p { NSMutableArray *subhorz, *subvert; uintmax_t *first; NSUInteger i; NSMutableString *out; + tAutoLayoutParams pp; first = (uintmax_t *) malloc([self->children count] * sizeof (uintmax_t)); if (first == NULL) abort(); subhorz = [NSMutableArray new]; subvert = [NSMutableArray new]; + + pp.horz = subhorz; + pp.vert = subvert; + pp.extra = p->extra; + pp.extraVert = p->extraVert; + pp.views = p->views; + pp.n = p->n; for (i = 0; i < [self->children count]; i++) { id cur; - first[i] = *n; + first[i] = pp.n; cur = (id) [self->children objectAtIndex:i]; - [cur tFillAutoLayoutHorz:subhorz vert:subvert - extra:extra extraVert:extraVert - views:views first:n]; + [cur tFillAutoLayout:&pp]; } + p->n = pp.n; + // TODO vertical out = [NSMutableString new]; [subhorz enumerateObjectsUsingBlock:^(id obj, NSUInteger index, BOOL *stop) { @@ -78,8 +81,9 @@ //TODO [out appendString:@"-"]; [out appendString:((NSString *) obj)]; }]; - [horz addObject:out]; - [vert addObjectsFromArray:subvert]; + [p->horz addObject:out]; + [p->vert addObjectsFromArray:subvert]; + [subhorz release]; [subvert release]; free(first); diff --git a/redo/osxaltest/button.m b/redo/osxaltest/button.m index 4598e93a..6eb1ced6 100644 --- a/redo/osxaltest/button.m +++ b/redo/osxaltest/button.m @@ -30,20 +30,15 @@ [self tRelayout]; } -- (void)tFillAutoLayoutHorz:(NSMutableArray *)horz - vert:(NSMutableArray *)vert - extra:(NSMutableArray *)extra - extraVert:(NSMutableArray *)extraVert - views:(NSMutableDictionary *)views - first:(uintmax_t *)n +- (void)tFillAutoLayout:(tAutoLayoutParams *)p { NSString *key; - key = tAutoLayoutKey(*n); - (*n)++; - [horz addObject:[NSString stringWithFormat:@"[%@]", key]]; - [vert addObject:[NSString stringWithFormat:@"[%@]", key]]; - [views setObject:self->b forKey:key]; + key = tAutoLayoutKey(p->n); + p->n++; + [p->horz addObject:[NSString stringWithFormat:@"[%@]", key]]; + [p->vert addObject:[NSString stringWithFormat:@"[%@]", key]]; + [p->views setObject:self->b forKey:key]; } - (void)tRelayout diff --git a/redo/osxaltest/osxaltest.h b/redo/osxaltest/osxaltest.h index 5dbb282a..8f14143e 100644 --- a/redo/osxaltest/osxaltest.h +++ b/redo/osxaltest/osxaltest.h @@ -2,15 +2,21 @@ #import #import +typedef struct tAutoLayoutParams tAutoLayoutParams; + +struct tAutoLayoutParams { + NSMutableArray *horz; + NSMutableArray *vert; + NSMutableArray *extra; // TODO make extraHorz and return BOOL NSNumber logic + NSMutableArray *extraVert; + NSMutableDictionary *views; + uintmax_t n; +}; + @protocol tControl @required - (void)tSetParent:(id)p addToView:(NSView *)v; -- (void)tFillAutoLayoutHorz:(NSMutableArray *)horz - vert:(NSMutableArray *)vert - extra:(NSMutableArray *)extra - extraVert:(NSMutableArray *)extraVert - views:(NSMutableDictionary *)views - first:(uintmax_t *)n; +- (void)tFillAutoLayout:(tAutoLayoutParams *)p; - (void)tRelayout; @end diff --git a/redo/osxaltest/spinbox.m b/redo/osxaltest/spinbox.m index 5f1b8d7b..5971332a 100644 --- a/redo/osxaltest/spinbox.m +++ b/redo/osxaltest/spinbox.m @@ -73,20 +73,15 @@ [v addSubview:self->c]; } -- (void)tFillAutoLayoutHorz:(NSMutableArray *)horz - vert:(NSMutableArray *)vert - extra:(NSMutableArray *)extra - extraVert:(NSMutableArray *)extraVert - views:(NSMutableDictionary *)views - first:(uintmax_t *)n +- (void)tFillAutoLayout:(tAutoLayoutParams *)p { NSString *key; - key = tAutoLayoutKey(*n); - (*n)++; - [horz addObject:[NSString stringWithFormat:@"[%@]", key]]; - [vert addObject:[NSString stringWithFormat:@"[%@]", key]]; - [views setObject:self->c forKey:key]; + key = tAutoLayoutKey(p->n); + p->n++; + [p->horz addObject:[NSString stringWithFormat:@"[%@]", key]]; + [p->vert addObject:[NSString stringWithFormat:@"[%@]", key]]; + [p->views setObject:self->c forKey:key]; } - (void)tRelayout diff --git a/redo/osxaltest/window.m b/redo/osxaltest/window.m index 66fa60a1..21276384 100644 --- a/redo/osxaltest/window.m +++ b/redo/osxaltest/window.m @@ -42,48 +42,49 @@ - (void)tRelayout { NSView *contentView; - NSMutableArray *horz, *vert; - NSMutableArray *extra, *extraVert; - NSMutableDictionary *views; + tAutoLayoutParams p; NSUInteger i; NSString *margin; - uintmax_t n; if (self->c == nil) return; + contentView = [self->w contentView]; [contentView removeConstraints:[contentView constraints]]; - horz = [NSMutableArray new]; - vert = [NSMutableArray new]; - extra = [NSMutableArray new]; - extraVert = [NSMutableArray new]; - views = [NSMutableDictionary new]; - n = 0; - [self->c tFillAutoLayoutHorz:horz vert:vert extra:extra extraVert:extraVert views:views first:&n]; + + p.horz = [NSMutableArray new]; + p.vert = [NSMutableArray new]; + p.extra = [NSMutableArray new]; + p.extraVert = [NSMutableArray new]; + p.views = [NSMutableDictionary new]; + p.n = 0; + [self->c tFillAutoLayout:&p]; + margin = @""; if (self->margined) margin = @"-"; - [horz enumerateObjectsUsingBlock:^(id obj, NSUInteger index, BOOL *stop) { - [extra addObject:[NSString stringWithFormat:@"|%@%@%@|", margin, obj, margin]]; - [extraVert addObject:@NO]; + [p.horz enumerateObjectsUsingBlock:^(id obj, NSUInteger index, BOOL *stop) { + [p.extra addObject:[NSString stringWithFormat:@"|%@%@%@|", margin, obj, margin]]; + [p.extraVert addObject:@NO]; }]; - [vert enumerateObjectsUsingBlock:^(id obj, NSUInteger index, BOOL *stop) { - [extra addObject:[NSString stringWithFormat:@"|%@%@%@|", margin, obj, margin]]; - [extraVert addObject:@YES]; + [p.vert enumerateObjectsUsingBlock:^(id obj, NSUInteger index, BOOL *stop) { + [p.extra addObject:[NSString stringWithFormat:@"|%@%@%@|", margin, obj, margin]]; + [p.extraVert addObject:@YES]; }]; - for (i = 0; i < [extra count]; i++) { + for (i = 0; i < [p.extra count]; i++) { NSString *constraint; NSNumber *vertical; NSArray *constraints; - vertical = (NSNumber *) [extraVert objectAtIndex:i]; + vertical = (NSNumber *) [p.extraVert objectAtIndex:i]; if ([vertical boolValue]) - constraint = [NSString stringWithFormat:@"V:%@", [extra objectAtIndex:i]]; + constraint = [NSString stringWithFormat:@"V:%@", [p.extra objectAtIndex:i]]; else - constraint = [NSString stringWithFormat:@"H:%@", [extra objectAtIndex:i]]; - constraints = [NSLayoutConstraint constraintsWithVisualFormat:constraint options:0 metrics:nil views:views]; + constraint = [NSString stringWithFormat:@"H:%@", [p.extra objectAtIndex:i]]; + constraints = [NSLayoutConstraint constraintsWithVisualFormat:constraint options:0 metrics:nil views:p.views]; [contentView addConstraints:constraints]; } + // TODO release everything }