Revision: 29463
          http://sourceforge.net/p/bibdesk/svn/29463
Author:   hofman
Date:     2025-08-25 09:16:43 +0000 (Mon, 25 Aug 2025)
Log Message:
-----------
observe canGoBack canGoForward and loading of webView

Modified Paths:
--------------
    trunk/bibdesk/BDSKTextImportController.m

Modified: trunk/bibdesk/BDSKTextImportController.m
===================================================================
--- trunk/bibdesk/BDSKTextImportController.m    2025-08-25 08:53:06 UTC (rev 
29462)
+++ trunk/bibdesk/BDSKTextImportController.m    2025-08-25 09:16:43 UTC (rev 
29463)
@@ -93,6 +93,8 @@
     BDSKWebViewCustomMenuActionDownloadLink
 };
 
+static char BDSKTextImportWebViewObservationContext;
+
 #if MAC_OS_X_VERSION_MAX_ALLOWED < 110300
 @class WKDownload;
 #define WKNavigationActionPolicyDownload 2
@@ -136,8 +138,6 @@
 - (void)updateColumnWidths;
 - (void)updateLinkedFileUI;
 
-- (void)setLoading:(BOOL)loading;
-
 - (void)cancelDownload;
 - (void)setLocalUrlFromDownload;
 - (void)setDownloading:(BOOL)downloading;
@@ -656,14 +656,26 @@
     [backButton setAction:@selector(goBack:)];
     [forwardButton setTarget:webView];
     [forwardButton setAction:@selector(goForward:)];
+    
+    [webView addObserver:self forKeyPath:@"canGoBack" options:0 
context:&BDSKTextImportWebViewObservationContext];
+    [webView addObserver:self forKeyPath:@"canGoForward" options:0 
context:&BDSKTextImportWebViewObservationContext];
+    [webView addObserver:self forKeyPath:@"loading" options:0 
context:&BDSKTextImportWebViewObservationContext];
 }
 
 - (void)clearWebView {
     [webView setNavigationDelegate:nil];
     [webView setUIDelegate:nil];
+    
     [[[webView configuration] userContentController] 
removeScriptMessageHandlerForName:@"hoverLink"];
     [[[webView configuration] userContentController] 
removeScriptMessageHandlerForName:@"selectionChanged"];
     [[[webView configuration] userContentController] removeAllUserScripts];
+    
+    @try {
+        [webView removeObserver:self forKeyPath:@"canGoBack" 
context:&BDSKTextImportWebViewObservationContext];
+        [webView removeObserver:self forKeyPath:@"canGoForward" 
context:&BDSKTextImportWebViewObservationContext];
+        [webView removeObserver:self forKeyPath:@"loading" 
context:&BDSKTextImportWebViewObservationContext];
+    }
+    @catch (id e) {}
 }
 
 - (void)loadPasteboardData{
@@ -981,20 +993,12 @@
 #pragma mark WKNavigationDelegate protocol
 
 - (void)webView:(WKWebView *)aWebView 
didStartProvisionalNavigation:(WKNavigation *)navigation {
-    [self setLoading:YES];
 }
 
 - (void)webView:(WKWebView *)aWebView didFinishNavigation:(WKNavigation 
*)navigation {
-    [self setLoading:[webView isLoading]];
-    [backButton setEnabled:[webView canGoBack]];
-    [forwardButton setEnabled:[webView canGoForward]];
 }
 
 - (void)webView:(WKWebView *)aWebView didFailNavigation:(WKNavigation 
*)navigation withError:(NSError *)error {
-    [self setLoading:[webView isLoading]];
-    [backButton setEnabled:[webView canGoBack]];
-    [forwardButton setEnabled:[webView canGoForward]];
-    
     if ([[error domain] isEqualToString:NSURLErrorDomain] == NO || [error 
code] != NSURLErrorCancelled) {
         NSURL *url = [webView URL];
         NSString *errorHTML = [NSString 
stringWithFormat:@"<html><head><title>%@</title></head><body><h1>%@</h1></body></html>",
 NSLocalizedString(@"Error", @"Placeholder web group label"), [error 
localizedDescription]];
@@ -1627,6 +1631,21 @@
     }
 }
 
+#pragma mark KVO Observing
+
+- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object 
change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
+    if (context == &BDSKTextImportWebViewObservationContext) {
+        if ([keyPath isEqualToString:@"loading"])
+            [self setLoading:[webView isLoading]];
+        else if ([keyPath isEqualToString:@"canGoBack"])
+            [backButton setEnabled:[webView canGoBack]];
+        else if ([keyPath isEqualToString:@"canGoForward"])
+            [forwardButton setEnabled:[webView canGoForward]];
+    } else {
+        [super observeValueForKeyPath:keyPath ofObject:object change:change 
context:context];
+    }
+}
+
 #pragma mark Touch Bar
 
 - (NSTouchBar *)makeTouchBar {

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



_______________________________________________
Bibdesk-commit mailing list
Bibdesk-commit@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/bibdesk-commit

Reply via email to