Skip to content

Commit 9a6e5ad

Browse files
committed
PickerView: add an initial implementation of PickerView
This adds a constrained implementation of PickerView. It sketches out the various pieces that are needed to get a working picker view. Ideally, we would use the `CBS_OWNERDRAWVARIABLE` to render the views individually sized, but that seems to cause an issue with the draw operation, where the draw is never invoked for each item. This currently restricts us to equal sized views rather than variable sized views. The other important missing piece here is the lack of support for multi-component pickers. This should be possible to do in practice since the backing widget tree is a ListBox and an Edit container. We should be able to render more complicated views accordingly. Resolves: #425
1 parent eda5c37 commit 9a6e5ad

File tree

3 files changed

+484
-2
lines changed

3 files changed

+484
-2
lines changed

Examples/UICatalog/UICatalog.swift

+36-2
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ final class UICatalog: ApplicationDelegate, SceneDelegate {
6060
TableView(frame: Rect(x: 4.0, y: 310.0, width: 254.0, height: 48.0),
6161
style: .plain)
6262

63+
lazy var pickerview: PickerView =
64+
PickerView(frame: Rect(x: 4.0, y: 362.0, width: 256.0, height: 24.0))
65+
6366
lazy var imageview: ImageView = {
6467
#if SWIFT_PACKAGE
6568
let bundle: Bundle = Bundle.module
@@ -72,7 +75,7 @@ final class UICatalog: ApplicationDelegate, SceneDelegate {
7275
}
7376
let image: Image? = Image(contentsOfFile: resource.path)
7477
let view = ImageView(image: image)
75-
view.frame = Rect(x: 64.0, y: 362.0, width: 128.0, height: 128.0)
78+
view.frame = Rect(x: 64.0, y: 394.0, width: 128.0, height: 128.0)
7679
return view
7780
}()
7881

@@ -82,7 +85,7 @@ final class UICatalog: ApplicationDelegate, SceneDelegate {
8285

8386
// Set the preferred window size and restrict resizing by setting the
8487
// minimum and maximum to the same value.
85-
let size: Size = Size(width: 265, height: 494)
88+
let size: Size = Size(width: 265, height: 530)
8689
windowScene.sizeRestrictions?.minimumSize = size
8790
windowScene.sizeRestrictions?.maximumSize = size
8891

@@ -103,6 +106,7 @@ final class UICatalog: ApplicationDelegate, SceneDelegate {
103106
window.addSubview(self.stepperLabel)
104107
window.addSubview(self.stepper)
105108
window.addSubview(self.tableview)
109+
window.addSubview(self.pickerview)
106110
window.addSubview(self.imageview)
107111

108112
self.label.font = Font(name: "Consolas", size: 10)!
@@ -137,6 +141,10 @@ Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deseru
137141

138142
self.tableview.dataSource = self
139143

144+
self.pickerview.dataSource = self
145+
self.pickerview.delegate = self
146+
self.pickerview.reloadAllComponents()
147+
140148
window.makeKeyAndVisible()
141149
}
142150

@@ -175,3 +183,29 @@ extension UICatalog: TableViewDataSource {
175183
return cell
176184
}
177185
}
186+
187+
extension UICatalog: PickerViewDataSource {
188+
public func numberOfComponents(in pickerView: PickerView) -> Int {
189+
return 1
190+
}
191+
192+
public func pickerView(_ pickerView: PickerView,
193+
numberOfRowsInComponent component: Int) -> Int {
194+
return 7
195+
}
196+
}
197+
198+
extension UICatalog: PickerViewDelegate {
199+
public func pickerView(_ pickerView: PickerView, titleForRow row: Int,
200+
forComponent component: Int) -> String? {
201+
return [
202+
"Sunday",
203+
"Monday",
204+
"Tuesday",
205+
"Wednesday",
206+
"Thursday",
207+
"Friday",
208+
"Saturday",
209+
][row]
210+
}
211+
}

Sources/SwiftWin32/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ target_sources(SwiftWin32 PRIVATE
107107
"Views and Controls/EdgeInsets.swift"
108108
"Views and Controls/ImageView.swift"
109109
"Views and Controls/Label.swift"
110+
"Views and Controls/PickerView.swift"
110111
"Views and Controls/ProgressView.swift"
111112
"Views and Controls/Slider.swift"
112113
"Views and Controls/Stepper.swift"

0 commit comments

Comments
 (0)