Revision: 12897
          http://sourceforge.net/p/skim-app/code/12897
Author:   hofman
Date:     2022-05-07 17:16:35 +0000 (Sat, 07 May 2022)
Log Message:
-----------
Move findbar add/remove code to main window controller, code add and remove 
separately

Modified Paths:
--------------
    trunk/SKFindController.h
    trunk/SKFindController.m
    trunk/SKMainWindowController.h
    trunk/SKMainWindowController.m

Modified: trunk/SKFindController.h
===================================================================
--- trunk/SKFindController.h    2022-05-07 09:43:33 UTC (rev 12896)
+++ trunk/SKFindController.h    2022-05-07 17:16:35 UTC (rev 12897)
@@ -50,7 +50,6 @@
     NSString *findString;
     NSInteger lastChangeCount;
     BOOL didChange;
-    BOOL animating;
 }
 
 @property (nonatomic, retain) IBOutlet NSSearchField *findField;
@@ -61,8 +60,6 @@
 @property (nonatomic, assign) id <SKFindControllerDelegate> delegate;
 @property (nonatomic, retain) NSString *findString;
 
-- (void)toggleAboveView:(NSView *)view;
-
 - (void)findForward:(BOOL)forward;
 - (void)updateFindPboard;
 
@@ -70,10 +67,12 @@
 - (IBAction)remove:(id)sender;
 - (IBAction)toggleCaseInsensitiveFind:(id)sender;
 
+- (void)didAddFindBar;
+
 @end
 
 
 @protocol SKFindControllerDelegate <NSObject>
 - (BOOL)findString:(NSString *)string forward:(BOOL)forward;
-- (void)findControllerWillBeRemoved:(SKFindController *)findController;
+- (void)removeFindController:(SKFindController *)findController;
 @end

Modified: trunk/SKFindController.m
===================================================================
--- trunk/SKFindController.m    2022-05-07 09:43:33 UTC (rev 12896)
+++ trunk/SKFindController.m    2022-05-07 17:16:35 UTC (rev 12897)
@@ -105,94 +105,12 @@
     }
 }
 
-- (void)toggleAboveView:(NSView *)view {
-    if (animating)
-        return;
-    
-    BOOL animate = NO == [[NSUserDefaults standardUserDefaults] 
boolForKey:SKDisableAnimationsKey];
-    NSView *findBar = [self view];
-    
-    if (view == nil) {
-        NSArray *subviews = [[findBar superview] subviews];
-        for (view in subviews) {
-            if (view != findBar && NSMaxY([view frame]) >= NSMinY([findBar 
frame]))
-                break;
-        }
-    }
-    
-    NSView *contentView = [view superview];
-    BOOL covering = NSMaxY([contentView convertRect:[contentView bounds] 
toView:nil]) > NSMaxY([[contentView window] contentLayoutRect]);
-    BOOL visible = (nil == [findBar superview]);
-    NSLayoutConstraint *topConstraint = nil;
-    CGFloat barHeight = NSHeight([findBar frame]);
-    NSArray *constraints = nil;
-    CGFloat inset = 0.0;
-    if (covering)
-        inset += NSHeight([[contentView window] frame]) - 
NSHeight([[contentView window] contentLayoutRect]);
-    
-    if (visible) {
-        [contentView addSubview:findBar];
-        constraints = [NSArray arrayWithObjects:
-            [NSLayoutConstraint constraintWithItem:findBar 
attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual 
toItem:contentView attribute:NSLayoutAttributeLeading multiplier:1.0 
constant:0.0],
-            [NSLayoutConstraint constraintWithItem:contentView 
attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual 
toItem:findBar attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0.0],
-            [NSLayoutConstraint constraintWithItem:findBar 
attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual 
toItem:contentView attribute:NSLayoutAttributeTop multiplier:1.0 
constant:animate ? inset - barHeight : inset], nil];
-        [NSLayoutConstraint activateConstraints:constraints];
-        if (covering == NO) {
-            [[contentView constraintWithFirstItem:view 
firstAttribute:NSLayoutAttributeTop] setActive:NO];
-            [[NSLayoutConstraint constraintWithItem:view 
attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:findBar 
attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0] setActive:YES];
-        }
-        [contentView layoutSubtreeIfNeeded];
-        topConstraint = [constraints lastObject];
-        
-        [[NSNotificationCenter defaultCenter] addObserver:self 
selector:@selector(windowDidBecomeKey:) name:NSWindowDidBecomeKeyNotification 
object:[findBar window]];
-        [[NSNotificationCenter defaultCenter] addObserver:self 
selector:@selector(windowDidResignKey:) name:NSWindowDidResignKeyNotification 
object:[findBar window]];
-        [self windowDidBecomeKey:nil];
-    } else {
-        topConstraint = [contentView constraintWithFirstItem:findBar 
firstAttribute:NSLayoutAttributeTop];
-        if (covering == NO)
-            constraints = [NSArray arrayWithObjects:
-                [NSLayoutConstraint constraintWithItem:view 
attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual 
toItem:contentView attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0], 
nil];
-        
-        [[NSNotificationCenter defaultCenter] removeObserver:self 
name:NSWindowDidBecomeKeyNotification object:[findBar window]];
-        [[NSNotificationCenter defaultCenter] removeObserver:self 
name:NSWindowDidResignKeyNotification object:[findBar window]];
-        [self windowDidResignKey:nil];
-        [delegate findControllerWillBeRemoved:self];
-    }
-    
+- (void)didAddFindBar {
+    NSWindow *window = [[self view] window];
+    [[NSNotificationCenter defaultCenter] addObserver:self 
selector:@selector(windowDidBecomeKey:) name:NSWindowDidBecomeKeyNotification 
object:window];
+    [[NSNotificationCenter defaultCenter] addObserver:self 
selector:@selector(windowDidResignKey:) name:NSWindowDidResignKeyNotification 
object:window];
+    [self windowDidBecomeKey:nil];
     [messageField setHidden:YES];
-    
-    if (covering) {
-        NSScrollView *scrollView = [view descendantOfClass:[NSScrollView 
class]];
-        [scrollView setAutomaticallyAdjustsContentInsets:visible == NO];
-        if (visible)
-            [scrollView setContentInsets:NSEdgeInsetsMake(barHeight + inset, 
0.0, 0.0, 0.0)];
-    }
-    
-    if (animate) {
-        animating = YES;
-        [NSAnimationContext runAnimationGroup:^(NSAnimationContext *context){
-                [context setDuration:0.5 * [context duration]];
-                [[topConstraint animator] setConstant:visible ? inset : inset 
- barHeight];
-            }
-            completionHandler:^{
-                if (visible == NO) {
-                    [findBar removeFromSuperview];
-                    if (covering == NO)
-                        [NSLayoutConstraint activateConstraints:constraints];
-                }
-                [[contentView window] recalculateKeyViewLoop];
-                
-                animating = NO;
-            }];
-    } else {
-        if (visible == NO) {
-            [findBar removeFromSuperview];
-            if (covering == NO)
-                [NSLayoutConstraint activateConstraints:constraints];
-        }
-        [contentView layoutSubtreeIfNeeded];
-        [[contentView window] recalculateKeyViewLoop];
-    }
 }
 
 - (void)setDelegate:(id <SKFindControllerDelegate>)newDelegate {
@@ -224,7 +142,17 @@
 }
 
 - (IBAction)remove:(id)sender {
-    [self toggleAboveView:nil];
+    NSWindow *window = [[self view] window];
+    if (window == nil)
+        return;
+    
+    [[NSNotificationCenter defaultCenter] removeObserver:self 
name:NSWindowDidBecomeKeyNotification object:window];
+    [[NSNotificationCenter defaultCenter] removeObserver:self 
name:NSWindowDidResignKeyNotification object:window];
+    [self windowDidResignKey:nil];
+    
+    [messageField setHidden:YES];
+    
+    [delegate removeFindController:self];
 }
 
 - (IBAction)toggleCaseInsensitiveFind:(id)sender {

Modified: trunk/SKMainWindowController.h
===================================================================
--- trunk/SKMainWindowController.h      2022-05-07 09:43:33 UTC (rev 12896)
+++ trunk/SKMainWindowController.h      2022-05-07 17:16:35 UTC (rev 12897)
@@ -192,6 +192,7 @@
         unsigned int isEditingPDF:1;
         unsigned int isEditingTable:1;
         unsigned int isSwitchingFullScreen:1;
+        unsigned int isAnimatingFindBar:1;
         unsigned int wantsPresentation:1;
         unsigned int recentInfoNeedsUpdate:1;
         unsigned int hasCropped:1;

Modified: trunk/SKMainWindowController.m
===================================================================
--- trunk/SKMainWindowController.m      2022-05-07 09:43:33 UTC (rev 12896)
+++ trunk/SKMainWindowController.m      2022-05-07 17:16:35 UTC (rev 12897)
@@ -1820,9 +1820,45 @@
        }
 }
 
-- (void)findControllerWillBeRemoved:(SKFindController *)aFindController {
-    if ([[[self window] firstResponder] isDescendantOf:[aFindController view]])
-        [[self window] makeFirstResponder:[self hasOverview] ? overviewView : 
[self pdfView]];
+- (void)removeFindController:(SKFindController *)aFindController {
+    if (mwcFlags.isAnimatingFindBar)
+        return;
+    
+    BOOL animate = NO == [[NSUserDefaults standardUserDefaults] 
boolForKey:SKDisableAnimationsKey];
+    NSView *findBar = [findController view];
+    NSView *contentView = [findBar superview];
+    NSLayoutConstraint *newTopConstraint = nil;
+    CGFloat barHeight = NSHeight([findBar frame]);
+    
+    if (mwcFlags.fullSizeContent == NO)
+        newTopConstraint = [NSLayoutConstraint 
constraintWithItem:mwcFlags.fullSizeContent ? pdfView : pdfSplitView 
attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual 
toItem:contentView attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0];
+    
+    if ([[mainWindow firstResponder] isDescendantOf:findBar])
+        [mainWindow makeFirstResponder:pdfView];
+    
+    if (mwcFlags.fullSizeContent)
+        [[pdfView scrollView] setAutomaticallyAdjustsContentInsets:YES];
+    
+    if (animate) {
+        NSLayoutConstraint *topConstraint = [contentView 
constraintWithFirstItem:findBar firstAttribute:NSLayoutAttributeTop];
+        mwcFlags.isAnimatingFindBar = YES;
+        [NSAnimationContext runAnimationGroup:^(NSAnimationContext *context){
+                [context setDuration:0.5 * [context duration]];
+                [[topConstraint animator] setConstant:titleBarHeight - 
barHeight];
+            }
+            completionHandler:^{
+                [findBar removeFromSuperview];
+                [newTopConstraint setActive:YES];
+                [mainWindow recalculateKeyViewLoop];
+                
+                mwcFlags.isAnimatingFindBar = NO;
+            }];
+    } else {
+        [findBar removeFromSuperview];
+        [newTopConstraint setActive:YES];
+        [contentView layoutSubtreeIfNeeded];
+        [mainWindow recalculateKeyViewLoop];
+    }
 }
 
 - (void)showFindBar {
@@ -1830,9 +1866,57 @@
         findController = [[SKFindController alloc] init];
         [findController setDelegate:self];
     }
-    if ([[findController view] window] == nil)
-        [findController toggleAboveView:mwcFlags.fullSizeContent ? pdfView : 
pdfSplitView];
-    [[findController findField] selectText:nil];
+    
+    NSView *findBar = [findController view];
+    NSTextField *findField = [findController findField];
+    
+    if ([findBar window]) {
+        [findField selectText:nil];
+    } else if (mwcFlags.isAnimatingFindBar == 0) {
+        
+        BOOL animate = NO == [[NSUserDefaults standardUserDefaults] 
boolForKey:SKDisableAnimationsKey];
+        NSView *contentView = mwcFlags.fullSizeContent ? pdfContentView : 
centerContentView;
+        CGFloat barHeight = NSHeight([findBar frame]);
+        NSArray *constraints = nil;
+        
+        [contentView addSubview:findBar];
+        constraints = [NSArray arrayWithObjects:
+            [NSLayoutConstraint constraintWithItem:findBar 
attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual 
toItem:contentView attribute:NSLayoutAttributeLeading multiplier:1.0 
constant:0.0],
+            [NSLayoutConstraint constraintWithItem:contentView 
attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual 
toItem:findBar attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0.0],
+            [NSLayoutConstraint constraintWithItem:findBar 
attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual 
toItem:contentView attribute:NSLayoutAttributeTop multiplier:1.0 
constant:animate ? titleBarHeight - barHeight : titleBarHeight], nil];
+        [NSLayoutConstraint activateConstraints:constraints];
+        if (mwcFlags.fullSizeContent == NO) {
+            [[contentView constraintWithFirstItem:pdfSplitView 
firstAttribute:NSLayoutAttributeTop] setActive:NO];
+            [[NSLayoutConstraint constraintWithItem:pdfSplitView 
attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:findBar 
attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0] setActive:YES];
+        }
+        [contentView layoutSubtreeIfNeeded];
+        
+        [findController didAddFindBar];
+        
+        if (mwcFlags.fullSizeContent) {
+            NSScrollView *scrollView = [pdfView scrollView];
+            [scrollView setAutomaticallyAdjustsContentInsets:NO];
+            [scrollView setContentInsets:NSEdgeInsetsMake(barHeight + 
titleBarHeight, 0.0, 0.0, 0.0)];
+        }
+        
+        if (animate) {
+            mwcFlags.isAnimatingFindBar = YES;
+            [NSAnimationContext runAnimationGroup:^(NSAnimationContext 
*context){
+                    [context setDuration:0.5 * [context duration]];
+                    [[[constraints lastObject] animator] 
setConstant:titleBarHeight];
+                }
+                completionHandler:^{
+                    [mainWindow recalculateKeyViewLoop];
+                    [findField selectText:nil];
+                    
+                    mwcFlags.isAnimatingFindBar = NO;
+                }];
+        } else {
+            [contentView layoutSubtreeIfNeeded];
+            [mainWindow recalculateKeyViewLoop];
+            [findField selectText:nil];
+        }
+    }
 }
 
 #define FIND_RESULT_MARGIN 50.0

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