Revision: 15035
          http://sourceforge.net/p/skim-app/code/15035
Author:   hofman
Date:     2025-03-28 16:27:11 +0000 (Fri, 28 Mar 2025)
Log Message:
-----------
Always set bezelWidth variable, calculate when it changes. Send KVO 
notifications when selectedIndex changes.

Modified Paths:
--------------
    trunk/SKColorSwatch.h
    trunk/SKColorSwatch.m

Modified: trunk/SKColorSwatch.h
===================================================================
--- trunk/SKColorSwatch.h       2025-03-28 10:11:19 UTC (rev 15034)
+++ trunk/SKColorSwatch.h       2025-03-28 16:27:11 UTC (rev 15035)
@@ -47,8 +47,8 @@
 @interface SKColorSwatch : NSControl <NSDraggingSource, NSAccessibilityGroup> {
     NSMutableArray<SKColorSwatchItemView *> *itemViews;
     NSControl *backgroundView;
+    CGFloat bezelWidth;
     CGFloat bezelHeight;
-    CGFloat bezelWidth;
     
     NSInteger clickedIndex;
     NSInteger selectedIndex;

Modified: trunk/SKColorSwatch.m
===================================================================
--- trunk/SKColorSwatch.m       2025-03-28 10:11:19 UTC (rev 15034)
+++ trunk/SKColorSwatch.m       2025-03-28 16:27:11 UTC (rev 15035)
@@ -54,7 +54,8 @@
 #define SELECTS_KEY     @"selects"
 #define BEZELHEIGHT_KEY @"bezelHeight"
 
-#define COLOR_KEY       @"color"
+#define COLOR_KEY                 @"color"
+#define SELECTEDCOLORINDEX_KEY    @"selectedColorIndex"
 
 #define BEZEL_INSET_LEFT    1.0
 #define BEZEL_INSET_RIGHT   1.0
@@ -108,6 +109,7 @@
 
 @interface SKColorSwatch ()
 @property (nonatomic) NSInteger selectedColorIndex;
+@property (nonatomic, readonly) CGFloat contentWidth;
 @property (nonatomic) CGFloat bezelWidth;
 - (NSRect)frameForItemViewAtIndex:(NSInteger)anIndex;
 - (void)setColor:(NSColor *)color atIndex:(NSInteger)i 
fromPanel:(BOOL)fromPanel;
@@ -116,7 +118,7 @@
 @implementation SKColorSwatch
 
 @synthesize autoResizes, selects, alternate, clickedColorIndex=clickedIndex, 
selectedColorIndex=selectedIndex, bezelWidth;
-@dynamic colors, color;
+@dynamic colors, color, contentWidth;
 
 + (id)defaultAnimationForKey:(NSAnimatablePropertyKey)key {
     if ([key isEqualToString:@"bezelWidth"]) {
@@ -140,7 +142,7 @@
     selectedIndex = -1;
     draggedIndex = -1;
     
-    bezelWidth = 0.0;
+    bezelWidth = [self contentWidth];
     
     [self registerForDraggedTypes:[NSColor 
readableTypesForPasteboard:[NSPasteboard 
pasteboardWithName:NSPasteboardNameDrag]]];
 }
@@ -154,8 +156,6 @@
             autoResizes = YES;
         selects = NO;
         bezelHeight = 22.0;
-
-        [self commonInit];
         
         backgroundView = [[SKColorSwatchBackgroundView alloc] 
initWithFrame:[self bounds]];
         [backgroundView setTranslatesAutoresizingMaskIntoConstraints:NO];
@@ -172,6 +172,8 @@
         [itemView setColor:[NSColor whiteColor]];
         [self addSubview:itemView];
         itemViews = [[NSMutableArray alloc] initWithObjects:itemView, nil];
+
+        [self commonInit];
     }
     return self;
 }
@@ -183,10 +185,7 @@
         selects = [decoder decodeBoolForKey:SELECTS_KEY];
         bezelHeight = [decoder decodeDoubleForKey:BEZELHEIGHT_KEY];
 
-        [self commonInit];
-        
         itemViews = [[NSMutableArray alloc] init];
-        
         for (NSView *view in [self subviews]) {
             if ([view isKindOfClass:[SKColorSwatchBackgroundView class]])
                 backgroundView = (SKColorSwatchBackgroundView *)view;
@@ -193,6 +192,8 @@
             else if ([view isKindOfClass:[SKColorSwatchItemView class]])
                 [itemViews addObject:(SKColorSwatchItemView *)view];
         }
+
+        [self commonInit];
     }
     return self;
 }
@@ -263,8 +264,13 @@
     return COLOR_INSET + [itemViews count] * DISTANCE_BETWEEN_COLORS;
 }
 
+- (void)setBezelWidth:(CGFloat)width {
+    bezelWidth = width;
+    [self invalidateIntrinsicContentSize];
+}
+
 - (NSSize)intrinsicContentSize {
-    return NSMakeSize(bezelWidth ?: [self contentWidth], bezelHeight);
+    return NSMakeSize(bezelWidth, bezelHeight);
 }
 
 - (NSSize)intrinsicFrameSize {
@@ -290,8 +296,8 @@
     CGFloat height = [backgroundView intrinsicContentSize].height;
     if (fabs(height - bezelHeight) > 0.0) {
         bezelHeight = height;
+        [self setBezelWidth:[self contentWidth]];
         [self updateItemViewFramesAnimating:NO];
-        [self invalidateIntrinsicContentSize];
         if (autoResizes)
             [self sizeToFit];
     }
@@ -489,8 +495,8 @@
     }
     if (autoResizes && [newColors count] != oldCount)
         [self sizeToFit];
+    [self setBezelWidth:[self contentWidth]];
     [self updateItemViewFramesAnimating:NO];
-    [self invalidateIntrinsicContentSize];
     [[NSNotificationCenter defaultCenter] 
postNotificationName:SKColorSwatchColorsChangedNotification object:self];
 }
 
@@ -504,11 +510,6 @@
     [super setEnabled:enabled];
 }
 
-- (void)setBezelWidth:(CGFloat)width {
-    bezelWidth = width;
-    [self invalidateIntrinsicContentSize];
-}
-
 #pragma mark Modification
 
 - (void)selectColorAtIndex:(NSInteger)idx {
@@ -612,8 +613,11 @@
         else
             [self addSubview:itemView positioned:NSWindowAbove relativeTo:nil];
         [itemViews insertObject:itemView atIndex:i];
-        if (selectedIndex >= i)
+        if (selectedIndex >= i) {
+            [self willChangeValueForKey:SELECTEDCOLORINDEX_KEY];
             selectedIndex++;
+            [self didChangeValueForKey:SELECTEDCOLORINDEX_KEY];
+        }
         if (focusedIndex >= i)
             focusedIndex++;
         [self didChangeColors];
@@ -625,9 +629,6 @@
                     [[self animator] setFrameSize:[self intrinsicFrameSize]];
             }
             completionHandler:^{
-                [self setBezelWidth:0.0];
-                if (autoResizes)
-                    [self sizeToFit];
                 [self noteFocusRingMaskChanged];
             }];
     }
@@ -635,16 +636,20 @@
 
 - (void)removeColorAtIndex:(NSInteger)i {
     if (i >= 0 && i < (NSInteger)[itemViews count] && [itemViews count] > 1) {
-        if (selectedIndex > i)
-            selectedIndex--;
-        else if (selectedIndex == i)
+        if (selectedIndex == i)
             [self deactivate];
-        if (focusedIndex > i)
-            focusedIndex--;
         [self willChangeColors];
         bezelWidth = [self contentWidth];
         SKColorSwatchItemView *itemView = [itemViews objectAtIndex:i];
         [itemViews removeObjectAtIndex:i];
+        if (selectedIndex > i) {
+            [self willChangeValueForKey:SELECTEDCOLORINDEX_KEY];
+            selectedIndex--;
+            [self didChangeValueForKey:SELECTEDCOLORINDEX_KEY];
+        }
+        if (focusedIndex > i ||
+            focusedIndex == (NSInteger)[itemViews count] - 1)
+            focusedIndex--;
         [self didChangeColors];
         [self noteFocusRingMaskChanged];
         [NSAnimationContext runAnimationGroup:^(NSAnimationContext *context){
@@ -656,9 +661,6 @@
             }
             completionHandler:^{
                 [itemView removeFromSuperview];
-                [self setBezelWidth:0.0];
-                if (autoResizes)
-                    [self sizeToFit];
                 [self noteFocusRingMaskChanged];
             }];
     }
@@ -666,12 +668,22 @@
 
 - (void)moveColorAtIndex:(NSInteger)from toIndex:(NSInteger)to {
     if (from >= 0 && to >= 0 && from != to) {
-        if (selectedIndex == from)
-            selectedIndex = to;
-        else if (selectedIndex > from && selectedIndex <= to)
-            selectedIndex--;
-        else if (selectedIndex < from && selectedIndex >= to)
-            selectedIndex++;
+        [self willChangeColors];
+        SKColorSwatchItemView *itemView = [itemViews objectAtIndex:from];
+        [itemViews removeObjectAtIndex:from];
+        [itemViews insertObject:itemView atIndex:to];
+        if (to > from)
+            [self addSubview:itemView positioned:NSWindowAbove 
relativeTo:[itemViews objectAtIndex:to - 1]];
+        if (selectedIndex >= MIN(from, to) && selectedIndex <= MAX(from, to)) {
+            [self willChangeValueForKey:SELECTEDCOLORINDEX_KEY];
+            if (selectedIndex == from)
+                selectedIndex = to;
+            else if (selectedIndex > from)
+                selectedIndex--;
+            else
+                selectedIndex++;
+            [self didChangeValueForKey:SELECTEDCOLORINDEX_KEY];
+        }
         if (focusedIndex == from)
             focusedIndex = to;
         else if (focusedIndex > from && focusedIndex <= to)
@@ -678,12 +690,6 @@
             focusedIndex--;
         else if (focusedIndex < from && focusedIndex >= to)
             focusedIndex++;
-        [self willChangeColors];
-        SKColorSwatchItemView *itemView = [itemViews objectAtIndex:from];
-        [itemViews removeObjectAtIndex:from];
-        [itemViews insertObject:itemView atIndex:to];
-        if (to > from)
-            [self addSubview:itemView positioned:NSWindowAbove 
relativeTo:[itemViews objectAtIndex:to - 1]];
         [self didChangeColors];
         [self noteFocusRingMaskChanged];
         [NSAnimationContext runAnimationGroup:^(NSAnimationContext *context){
@@ -1084,4 +1090,3 @@
 }
 
 @end
-

This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.



_______________________________________________
Skim-app-commit mailing list
Skim-app-commit@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/skim-app-commit

Reply via email to