Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .github/workflows/build-test-and-docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,7 @@ jobs:
buildtarget PathsExample
buildtarget ControlsExample
buildtarget RandomNumberGeneratorExample
# TODO test whether this works on Catalyst
# buildtarget SplitExample
buildtarget SplitExample

windows:
runs-on: windows-latest
Expand Down
4 changes: 4 additions & 0 deletions Examples/Bundler.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ identifier = 'dev.swiftcrossui.SplitExample'
product = 'SplitExample'
version = '0.1.0'

[[apps.SplitExample.overlays]]
condition = "platform(macCatalyst)"
interface_idiom = "mac"

[apps.SpreadsheetExample]
identifier = 'dev.swiftcrossui.SpreadsheetExample'
product = 'SpreadsheetExample'
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftCrossUI/Views/ForEach.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ extension ForEach where Items == [Int] {
self.elements = Array(range)
self.child = child
}

/// Creates a view that creates child views on demand based on a given Range
@_disfavoredOverload
public init(
Expand Down
68 changes: 57 additions & 11 deletions Sources/UIKitBackend/UIKitBackend+SplitView.swift
Original file line number Diff line number Diff line change
@@ -1,20 +1,71 @@
import UIKit

#if os(iOS)
#if os(iOS) || targetEnvironment(macCatalyst)
final class SplitWidget: WrapperControllerWidget<UISplitViewController>,
UISplitViewControllerDelegate
{
var resizeHandler: (() -> Void)?
private let sidebarContainer: ContainerWidget
private let mainContainer: ContainerWidget
private final class ColumnView: UIView {
unowned var splitWidget: SplitWidget!

@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) is not used for this view")
}

init() {
super.init(frame: .zero)
}

override func layoutSubviews() {
super.layoutSubviews()
if !splitWidget.hasCalledResizeHandler {
splitWidget.resizeHandler?()
splitWidget.hasCalledResizeHandler = true
}
}
}

private final class ColumnWidget: ContainerWidget {
let columnView = ColumnView()

override func loadView() {
view = columnView
}
}

var resizeHandler: (() -> Void)? {
didSet {
hasCalledResizeHandler = false
}
}

// This is just a flag so that we don't call resizeHandler twice in one pass through the run loop.
var hasCalledResizeHandler = false {
willSet {
if newValue {
DispatchQueue.main.async { [weak self] in
self?.hasCalledResizeHandler = false
}
}
}
}

private let sidebarContainer: ColumnWidget
private let mainContainer: ColumnWidget

init(sidebarWidget: some WidgetProtocol, mainWidget: some WidgetProtocol) {
// UISplitViewController requires its children to be controllers, not views
sidebarContainer = ContainerWidget(child: sidebarWidget)
mainContainer = ContainerWidget(child: mainWidget)
sidebarContainer = ColumnWidget(child: sidebarWidget)
mainContainer = ColumnWidget(child: mainWidget)

super.init(child: UISplitViewController())

sidebarContainer.parentWidget = self
mainContainer.parentWidget = self
childWidgets = [sidebarContainer, mainContainer]
sidebarContainer.columnView.splitWidget = self
mainContainer.columnView.splitWidget = self

child.delegate = self

child.preferredDisplayMode = .oneBesideSecondary
Expand Down Expand Up @@ -45,11 +96,6 @@ import UIKit

super.viewDidLoad()
}

override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
resizeHandler?()
}
}

extension UIKitBackend {
Expand Down
Loading