@@ -85,20 +85,46 @@ public final class SplitView: View {
8585 dividerControl. isEnabled = self . canResizeSidebar
8686 }
8787 }
88- public var dividerXOffset : Float = 300 {
88+ public var sidebarWidth : Float = 300 {
8989 didSet {
9090 self . setNeedsUpdateConstraints ( )
9191 }
9292 }
9393
94+ public var canResizeDetail : Bool = true {
95+ didSet {
96+ dividerControl2. isEnabled = self . canResizeDetail
97+ }
98+ }
99+ public var detailWidth : Float = 300 {
100+ didSet {
101+ self . setNeedsUpdateConstraints ( )
102+ }
103+ }
104+
105+ weak var sidebarView : View ? = nil
106+ weak var contentView : View ? = nil
107+ weak var detailView : View ? = nil
108+
94109 let dividerControl : SplitViewDividerControl = SplitViewDividerControl ( )
95- public func addSidebarView( _ sidebarView: View , contentView: View ) {
110+ let dividerControl2 : SplitViewDividerControl = SplitViewDividerControl ( )
111+
112+ public func addSidebarView( _ sidebarView: View , contentView: View , detailView: View ? ) {
96113 for subview in subviews {
97114 subview. removeFromSuperview ( )
98115 }
116+ self . sidebarView = sidebarView
99117 self . addSubview ( sidebarView)
118+ self . contentView = contentView
100119 self . addSubview ( contentView)
120+ if let detailView {
121+ self . detailView = detailView
122+ self . addSubview ( detailView)
123+ }
101124 self . addSubview ( dividerControl)
125+ if detailView != nil {
126+ self . addSubview ( dividerControl2)
127+ }
102128 }
103129
104130 public override init ( size: Size2 ? = nil ) {
@@ -115,7 +141,7 @@ public final class SplitView: View {
115141 if dividerControl. isDragging {
116142 if cursor. button ( . button1) . isPressed {
117143 if let new = cursor. locationInView ( self ) ? . x {
118- dividerXOffset = new
144+ sidebarWidth = new
119145 }
120146 }
121147 }
@@ -128,19 +154,41 @@ public final class SplitView: View {
128154 dividerControl. topAnchor. constrain ( to: self . topAnchor)
129155 dividerControl. bottomAnchor. constrain ( to: self . bottomAnchor)
130156 dividerControl. widthAnchor. constrain ( to: 8 )
131- dividerControl. leadingAnchor. constrain ( dividerXOffset - 4 , from: self . leadingAnchor, priority: . high)
157+ dividerControl. leadingAnchor. constrain ( sidebarWidth - 4 , from: self . leadingAnchor, priority: . high)
158+
159+ dividerControl2. layoutConstraints. removeAllConstraints ( )
160+ if detailView != nil {
161+ dividerControl2. topAnchor. constrain ( to: self . topAnchor)
162+ dividerControl2. bottomAnchor. constrain ( to: self . bottomAnchor)
163+ dividerControl2. widthAnchor. constrain ( to: 8 )
164+ dividerControl2. leadingAnchor. constrain ( - detailWidth - 4 , from: self . trailingAnchor, priority: . high)
165+ }
132166
133167 self . subviews [ 0 ] . layoutConstraints. removeAllConstraints ( )
134168 self . subviews [ 0 ] . topAnchor. constrain ( to: self . topAnchor)
135169 self . subviews [ 0 ] . leadingAnchor. constrain ( to: self . leadingAnchor)
136170 self . subviews [ 0 ] . bottomAnchor. constrain ( to: self . bottomAnchor)
137- self . subviews [ 0 ] . widthAnchor. constrain ( to: dividerXOffset )
171+ self . subviews [ 0 ] . widthAnchor. constrain ( to: sidebarWidth )
138172
139- self . subviews [ 1 ] . layoutConstraints. removeAllConstraints ( )
140- self . subviews [ 1 ] . topAnchor. constrain ( to: self . topAnchor)
141- self . subviews [ 1 ] . leadingAnchor. constrain ( dividerXOffset + 1 , from: self . leadingAnchor)
142- self . subviews [ 1 ] . bottomAnchor. constrain ( to: self . bottomAnchor)
143- self . subviews [ 1 ] . trailingAnchor. constrain ( to: self . trailingAnchor)
173+ if detailView != nil {
174+ self . subviews [ 1 ] . layoutConstraints. removeAllConstraints ( )
175+ self . subviews [ 1 ] . topAnchor. constrain ( to: self . topAnchor)
176+ self . subviews [ 1 ] . leadingAnchor. constrain ( sidebarWidth + 1 , from: self . leadingAnchor)
177+ self . subviews [ 1 ] . bottomAnchor. constrain ( to: self . bottomAnchor)
178+ self . subviews [ 1 ] . trailingAnchor. constrain ( - detailWidth, from: self . trailingAnchor)
179+
180+ self . subviews [ 2 ] . layoutConstraints. removeAllConstraints ( )
181+ self . subviews [ 2 ] . topAnchor. constrain ( to: self . topAnchor)
182+ self . subviews [ 2 ] . leadingAnchor. constrain ( - detailWidth, from: self . trailingAnchor)
183+ self . subviews [ 2 ] . bottomAnchor. constrain ( to: self . bottomAnchor)
184+ self . subviews [ 2 ] . widthAnchor. constrain ( to: detailWidth)
185+ } else {
186+ self . subviews [ 1 ] . layoutConstraints. removeAllConstraints ( )
187+ self . subviews [ 1 ] . topAnchor. constrain ( to: self . topAnchor)
188+ self . subviews [ 1 ] . leadingAnchor. constrain ( sidebarWidth + 1 , from: self . leadingAnchor)
189+ self . subviews [ 1 ] . bottomAnchor. constrain ( to: self . bottomAnchor)
190+ self . subviews [ 1 ] . trailingAnchor. constrain ( to: self . trailingAnchor)
191+ }
144192 }
145193}
146194
@@ -154,16 +202,32 @@ open class SplitViewController: ViewController {
154202
155203 open override func viewDidLoad( ) {
156204 super. viewDidLoad ( )
157- self . splitView. addSidebarView ( children [ 0 ] . view, contentView: children [ 1 ] . view)
205+ if detailViewController != nil {
206+ self . splitView. addSidebarView ( children [ 0 ] . view, contentView: children [ 1 ] . view, detailView: children [ 2 ] . view)
207+ } else {
208+ self . splitView. addSidebarView ( children [ 0 ] . view, contentView: children [ 1 ] . view, detailView: nil )
209+ }
158210 }
159211
160212 public var splitView : SplitView {
161213 return self . view as! SplitView
162214 }
163215
164- public init ( sideBar: ViewController , content: ViewController ) {
216+ weak var sidebarViewController : ViewController ? = nil
217+ weak var contentViewController : ViewController ? = nil
218+ weak var detailViewController : ViewController ? = nil
219+
220+ public init ( sideBar: ViewController , content: ViewController , detail: ViewController ? = nil ) {
165221 super. init ( )
166222 self . addChildViewController ( sideBar)
223+ self . sidebarViewController = sideBar
224+
167225 self . addChildViewController ( content)
226+ self . contentViewController = content
227+
228+ if let detail {
229+ self . detailViewController = detail
230+ self . addChildViewController ( detail)
231+ }
168232 }
169233}
0 commit comments