Revision: 15032
http://sourceforge.net/p/skim-app/code/15032
Author: hofman
Date: 2025-03-27 22:38:45 +0000 (Thu, 27 Mar 2025)
Log Message:
-----------
Store colors for color swatch only in item view, no need to also keep them in
an array
Modified Paths:
--------------
trunk/SKColorSwatch.h
trunk/SKColorSwatch.m
Modified: trunk/SKColorSwatch.h
===================================================================
--- trunk/SKColorSwatch.h 2025-03-27 17:37:30 UTC (rev 15031)
+++ trunk/SKColorSwatch.h 2025-03-27 22:38:45 UTC (rev 15032)
@@ -45,7 +45,6 @@
@class SKColorSwatchItemView;
@interface SKColorSwatch : NSControl <NSDraggingSource, NSAccessibilityGroup> {
- NSMutableArray<NSColor *> *colors;
NSMutableArray<SKColorSwatchItemView *> *itemViews;
NSControl *backgroundView;
CGFloat bezelHeight;
Modified: trunk/SKColorSwatch.m
===================================================================
--- trunk/SKColorSwatch.m 2025-03-27 17:37:30 UTC (rev 15031)
+++ trunk/SKColorSwatch.m 2025-03-27 22:38:45 UTC (rev 15032)
@@ -115,8 +115,8 @@
@implementation SKColorSwatch
-@synthesize colors, autoResizes, selects, alternate,
clickedColorIndex=clickedIndex, selectedColorIndex=selectedIndex, bezelWidth;
-@dynamic color;
+@synthesize autoResizes, selects, alternate, clickedColorIndex=clickedIndex,
selectedColorIndex=selectedIndex, bezelWidth;
+@dynamic colors, color;
+ (id)defaultAnimationForKey:(NSAnimatablePropertyKey)key {
if ([key isEqualToString:@"bezelWidth"]) {
@@ -148,7 +148,6 @@
- (instancetype)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (self) {
- colors = [[NSMutableArray alloc] initWithObjects:[NSColor whiteColor],
nil];
autoResizes = YES;
selects = NO;
bezelHeight = 22.0;
@@ -177,7 +176,6 @@
- (instancetype)initWithCoder:(NSCoder *)decoder {
self = [super initWithCoder:decoder];
if (self) {
- colors = [[NSMutableArray alloc] initWithArray:[decoder
decodeObjectForKey:COLORS_KEY]];
autoResizes = [decoder decodeBoolForKey:AUTORESIZES_KEY];
selects = [decoder decodeBoolForKey:SELECTS_KEY];
bezelHeight = [decoder decodeDoubleForKey:BEZELHEIGHT_KEY];
@@ -198,7 +196,6 @@
- (void)encodeWithCoder:(NSCoder *)coder {
[super encodeWithCoder:coder];
- [coder encodeObject:colors forKey:COLORS_KEY];
[coder encodeBool:autoResizes forKey:AUTORESIZES_KEY];
[coder encodeBool:selects forKey:SELECTS_KEY];
[coder encodeDouble:bezelHeight forKey:BEZELHEIGHT_KEY];
@@ -236,7 +233,7 @@
- (NSInteger)colorIndexAtPoint:(NSPoint)point {
NSRect rect = [self frameForColorAtIndex:0];
- NSInteger i, count = [colors count];
+ NSInteger i, count = [itemViews count];
for (i = 0; i < count; i++) {
if (NSMouseInRect(point, rect, [self isFlipped]))
@@ -249,7 +246,7 @@
- (NSInteger)insertionIndexAtPoint:(NSPoint)point {
NSRect rect = [self frameForColorAtIndex:0];
CGFloat x = NSMidX(rect);
- NSInteger i, count = [colors count];
+ NSInteger i, count = [itemViews count];
for (i = 0; i < count; i++) {
if (point.x < x)
@@ -260,7 +257,7 @@
}
- (CGFloat)contentWidth {
- return COLOR_INSET + [colors count] * DISTANCE_BETWEEN_COLORS;
+ return COLOR_INSET + [itemViews count] * DISTANCE_BETWEEN_COLORS;
}
- (NSSize)intrinsicContentSize {
@@ -384,7 +381,7 @@
draggedIndex = i;
- NSColor *color = [colors objectAtIndex:i];
+ NSColor *color = [[itemViews objectAtIndex:i] color];
CGFloat r = cornerRadius(NSControlSizeRegular) - 0.5;
@@ -449,7 +446,7 @@
}
- (void)moveRight:(id)sender {
- if (++focusedIndex >= (NSInteger)[colors count])
+ if (++focusedIndex >= (NSInteger)[itemViews count])
focusedIndex = 0;
[self noteFocusRingMaskChanged];
NSAccessibilityPostNotification(self,
NSAccessibilityFocusedUIElementChangedNotification);
@@ -457,7 +454,7 @@
- (void)moveLeft:(id)sender {
if (--focusedIndex < 0)
- focusedIndex = (NSInteger)[colors count] - 1;
+ focusedIndex = (NSInteger)[itemViews count] - 1;
[self noteFocusRingMaskChanged];
NSAccessibilityPostNotification(self,
NSAccessibilityFocusedUIElementChangedNotification);
}
@@ -465,16 +462,12 @@
#pragma mark Accessors
- (NSArray *)colors {
- return [colors copy];
+ return [itemViews valueForKey:COLOR_KEY];
}
- (void)setColors:(NSArray *)newColors {
- NSArray *oldColors = [self colors];
- NSUInteger i, iMax = [newColors count];
+ NSUInteger i, iMax = [newColors count], oldCount = [itemViews count];
[self deactivate];
- [colors setArray:newColors];
- if (autoResizes && [newColors count] != [oldColors count])
- [self sizeToFit];
for (i = 0; i < iMax; i++) {
SKColorSwatchItemView *itemView;
if (i < [itemViews count]) {
@@ -491,6 +484,8 @@
[[itemViews objectAtIndex:iMax] removeFromSuperview];
[itemViews removeObjectAtIndex:iMax];
}
+ if (autoResizes && [newColors count] != oldCount)
+ [self sizeToFit];
[self updateItemViewFrames];
[self invalidateIntrinsicContentSize];
[[NSNotificationCenter defaultCenter]
postNotificationName:SKColorSwatchColorsChangedNotification object:self];
@@ -497,7 +492,7 @@
}
- (NSColor *)color {
- return clickedIndex == -1 ? nil : [colors objectAtIndex:clickedIndex];
+ return clickedIndex == -1 ? nil : [[itemViews objectAtIndex:clickedIndex]
color];
}
- (void)setEnabled:(BOOL)enabled {
@@ -532,7 +527,7 @@
[[itemViews objectAtIndex:selectedIndex] setSelected:NO];
[self setSelectedColorIndex:idx];
[[itemViews objectAtIndex:selectedIndex] setSelected:YES];
- [colorPanel setColor:[[self colors] objectAtIndex:selectedIndex]];
+ [colorPanel setColor:[[itemViews objectAtIndex:selectedIndex] color]];
[colorPanel orderFront:nil];
[nc addObserver:self selector:@selector(handleColorPanelColorChanged:)
name:NSColorPanelColorDidChangeNotification object:colorPanel];
}
@@ -568,7 +563,7 @@
id observedObject = [info objectForKey:NSObservedObjectKey];
NSString *observedKeyPath = [info objectForKey:NSObservedKeyPathKey];
if (observedObject && observedKeyPath) {
- id value = [colors copy];
+ id value = [self colors];
NSValueTransformer *valueTransformer = [[info
objectForKey:NSOptionsKey] objectForKey:NSValueTransformerBindingOption];
if (valueTransformer == nil || [valueTransformer isEqual:[NSNull
null]]) {
NSString *transformerName = [[info objectForKey:NSOptionsKey]
objectForKey:NSValueTransformerNameBindingOption];
@@ -584,9 +579,8 @@
}
- (void)setColor:(NSColor *)color atIndex:(NSInteger)i
fromPanel:(BOOL)fromPanel {
- if (color && i >= 0 && i < (NSInteger)[colors count]) {
+ if (color && i >= 0 && i < (NSInteger)[itemViews count]) {
[self willChangeColors];
- [colors replaceObjectAtIndex:i withObject:color];
[[itemViews objectAtIndex:i] setColor:color];
NSAccessibilityPostNotification([itemViews objectAtIndex:i],
NSAccessibilityValueChangedNotification);
[self didChangeColors];
@@ -613,10 +607,9 @@
}
- (void)insertColor:(NSColor *)color atIndex:(NSInteger)i {
- if (color && i >= 0 && i <= (NSInteger)[colors count]) {
+ if (color && i >= 0 && i <= (NSInteger)[itemViews count]) {
[self willChangeColors];
bezelWidth = [self contentWidth];
- [colors insertObject:color atIndex:i];
SKColorSwatchItemView *itemView = [[SKColorSwatchItemView alloc]
initWithFrame:[self frameForCollapsedItemViewAtIndex:i]];
[itemView setColor:color];
if (i < (NSInteger)[itemViews count])
@@ -646,7 +639,7 @@
}
- (void)removeColorAtIndex:(NSInteger)i {
- if (i >= 0 && i < (NSInteger)[colors count] && [colors count] > 1) {
+ if (i >= 0 && i < (NSInteger)[itemViews count] && [itemViews count] > 1) {
if (selectedIndex > i)
selectedIndex--;
else if (selectedIndex == i)
@@ -655,7 +648,6 @@
focusedIndex--;
[self willChangeColors];
bezelWidth = [self contentWidth];
- [colors removeObjectAtIndex:i];
SKColorSwatchItemView *itemView = [itemViews objectAtIndex:i];
[itemViews removeObjectAtIndex:i];
[self didChangeColors];
@@ -679,7 +671,6 @@
- (void)moveColorAtIndex:(NSInteger)from toIndex:(NSInteger)to {
if (from >= 0 && to >= 0 && from != to) {
- NSColor *color = [colors objectAtIndex:from];
if (selectedIndex == from)
selectedIndex = to;
else if (selectedIndex > from && selectedIndex <= to)
@@ -693,8 +684,6 @@
else if (focusedIndex < from && focusedIndex >= to)
focusedIndex++;
[self willChangeColors];
- [colors removeObjectAtIndex:from];
- [colors insertObject:color atIndex:to];
SKColorSwatchItemView *itemView = [itemViews objectAtIndex:from];
[itemViews removeObjectAtIndex:from];
[itemViews insertObject:itemView atIndex:to];
@@ -716,7 +705,7 @@
#pragma mark NSDraggingSource protocol
- (NSDragOperation)draggingSession:(NSDraggingSession *)session
sourceOperationMaskForDraggingContext:(NSDraggingContext)context {
- return context == NSDraggingContextWithinApplication ?
NSDragOperationGeneric : [colors count] > 1 ? NSDragOperationDelete :
NSDragOperationNone;
+ return context == NSDraggingContextWithinApplication ?
NSDragOperationGeneric : [itemViews count] > 1 ? NSDragOperationDelete :
NSDragOperationNone;
}
- (void)draggingSession:(NSDraggingSession *)session
endedAtPoint:(NSPoint)screenPoint operation:(NSDragOperation)operation {
@@ -839,7 +828,7 @@
}
- (id)accessibilityFocusedUIElement {
- if (focusedIndex != -1 && focusedIndex < (NSInteger)[colors count])
+ if (focusedIndex != -1 && focusedIndex < (NSInteger)[itemViews count])
return NSAccessibilityUnignoredAncestor([itemViews
objectAtIndex:focusedIndex]);
else
return NSAccessibilityUnignoredAncestor(self);
@@ -852,7 +841,7 @@
- (void)itemView:(SKColorSwatchItemView *)itemView setFocused:(BOOL)focused {
if (focused) {
NSUInteger anIndex = [itemViews indexOfObject:itemView];
- if (anIndex < [[self colors] count]) {
+ if (anIndex < [itemViews count]) {
focusedIndex = anIndex;
[self noteFocusRingMaskChanged];
}
@@ -867,7 +856,7 @@
- (void)pressItemView:(SKColorSwatchItemView *)itemView
alternate:(BOOL)isAlternate {
NSUInteger anIndex = [itemViews indexOfObject:itemView];
- if (anIndex < [[self colors] count]) {
+ if (anIndex < [itemViews count]) {
alternate = isAlternate;
[self performClickAtIndex:anIndex];
alternate = NO;
This was sent by the SourceForge.net collaborative development platform, the
world's largest Open Source development site.
_______________________________________________
Skim-app-commit mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/skim-app-commit