@@ -856,27 +856,29 @@ class NSSplitViewResizingDelegate: NSObject, NSSplitViewDelegate {
856856 var minimumLeadingWidth = 0
857857 var maximumLeadingWidth = 0
858858 var isFirstUpdate = true
859- var systemIsResizing = false
859+ /// Tracks whether AppKit is resizing the side bar (as opposed to the user resizing it).
860+ var appKitIsResizing = false
860861
861862 func setResizeHandler( _ handler: @escaping ( _ paneWidths: [ Int ] ) -> Void ) {
862863 resizeHandler = handler
863864 }
864865
865866 func splitView( _ splitView: NSSplitView , shouldAdjustSizeOfSubview view: NSView ) -> Bool {
866- systemIsResizing = true
867+ appKitIsResizing = true
867868 return true
868869 }
869870
870871 func splitViewDidResizeSubviews( _ notification: Notification ) {
871- let systemWasResizing = systemIsResizing
872- systemIsResizing = false
872+ appKitIsResizing = false
873873 let splitView = notification. object! as! NSSplitView
874874 let paneWidths = splitView. subviews. map ( \. frame. width) . map { width in
875875 Int ( width. rounded ( ) )
876876 }
877877 let previousWidth = leadingWidth
878878 leadingWidth = paneWidths [ 0 ]
879- if !systemWasResizing && leadingWidth != previousWidth {
879+
880+ // Only call the handler if the side bar has actually changed size.
881+ if leadingWidth != previousWidth {
880882 resizeHandler ? ( paneWidths)
881883 }
882884 }
@@ -912,12 +914,19 @@ class NSSplitViewResizingDelegate: NSObject, NSSplitViewDelegate {
912914 splitView. setPosition ( max ( 200 , CGFloat ( minimumLeadingWidth) ) , ofDividerAt: 0 )
913915 isFirstUpdate = false
914916 } else {
915- if systemIsResizing {
917+ let newWidth = splitView. subviews [ 0 ] . frame. width
918+ // If AppKit is trying to automatically resize our side bar (e.g. because the split
919+ // view has changed size), only let it do so if not doing so would put out side bar
920+ // outside of the allowed bounds.
921+ if appKitIsResizing
922+ && leadingWidth >= minimumLeadingWidth
923+ && leadingWidth <= maximumLeadingWidth
924+ {
916925 splitView. setPosition ( CGFloat ( leadingWidth) , ofDividerAt: 0 )
917926 } else {
918927 // Magic! Thanks https://stackoverflow.com/a/30494691. This one line fixed all
919928 // of the split view resizing issues.
920- splitView. setPosition ( splitView . subviews [ 0 ] . frame . width , ofDividerAt: 0 )
929+ splitView. setPosition ( newWidth , ofDividerAt: 0 )
921930 }
922931 }
923932 }
0 commit comments