Skip to content

Commit 4d7f6ba

Browse files
committed
DatePicker: add accessors for the DatePicker
Add the `date` property and `setDate(_:animated:)` function to provide access to the value selected by the `DatePicker` control. Fixes: #677
1 parent e5bd338 commit 4d7f6ba

File tree

3 files changed

+68
-1
lines changed

3 files changed

+68
-1
lines changed

Sources/SwiftWin32/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ target_sources(SwiftWin32 PRIVATE
144144
Platform/WindowsHandle.swift)
145145
target_sources(SwiftWin32 PRIVATE
146146
Support/Array+Extensions.swift
147+
Support/Date+Extensions.swift
147148
Support/IndexPath+UIExtensions.swift
148149
Support/Logging.swift
149150
Support/Rect+UIExtensions.swift

Sources/SwiftWin32/Views and Controls/DatePicker.swift

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
import WinSDK
55

6+
import struct Foundation.Date
7+
68
private let SwiftDatePickerProc: SUBCLASSPROC = { (hWnd, uMsg, wParam, lParam, uIdSubclass, dwRefData) in
79
let datepicker: DatePicker? =
810
unsafeBitCast(dwRefData, to: AnyObject.self) as? DatePicker
@@ -22,7 +24,40 @@ public class DatePicker: Control {
2224
private static let style: WindowStyle =
2325
(base: WS_POPUP | WS_TABSTOP, extended: 0)
2426

25-
/// Configuring the Date Picker Style
27+
// MARK - Managing the Date and Calendar
28+
29+
/// The date displayed by the date picker.
30+
public var date: Date {
31+
get {
32+
var stDateTime: SYSTEMTIME = SYSTEMTIME()
33+
// FIXME(compnerd) ensure that GDT_VALID is returned
34+
_ = withUnsafeMutablePointer(to: &stDateTime) {
35+
SendMessageW(self.hWnd, UINT(DTM_GETSYSTEMTIME),
36+
WPARAM(0), LPARAM(UInt(bitPattern: $0)))
37+
}
38+
39+
let ftDateTime: FILETIME = FILETIME(stDateTime)
40+
return Date(timeIntervalSince1970: ftDateTime.timeIntervalSince1970)
41+
}
42+
set { self.setDate(newValue, animated: false) }
43+
}
44+
45+
/// Sets the date to display in the date picker, with an option to animate the
46+
/// setting.
47+
public func setDate(_ date: Date, animated: Bool) {
48+
assert(!animated, "not yet implemented")
49+
50+
var ftSystemTime: FILETIME =
51+
FILETIME(timeIntervalSince1970: date.timeIntervalSince1970)
52+
let stSystemTime: SYSTEMTIME = SYSTEMTIME(ftSystemTime)
53+
54+
_ = withUnsafePointer(to: stSystemTime) {
55+
SendMessageW(self.hWnd, UINT(DTM_SETSYSTEMTIME),
56+
WPARAM(GDT_VALID), LPARAM(UInt(bitPattern: $0)))
57+
}
58+
}
59+
60+
// MARK - Configuring the Date Picker Style
2661

2762
public private(set) var datePickerStyle: DatePickerStyle = .inline
2863
public private(set) var preferredDatePickerStyle: DatePickerStyle = .automatic {
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright © 2021 Saleem Abdulrasool <[email protected]>
2+
// SPDX-License-Identifier: BSD-3-Clause
3+
4+
import XCTest
5+
import WinSDK
6+
import Foundation
7+
@testable import SwiftWin32
8+
9+
final class DatePickerTests: XCTestCase {
10+
func testDateAccessors() {
11+
var ftSystemTime: FILETIME = FILETIME()
12+
GetSystemTimeAsFileTime(&ftSystemTime)
13+
var ftLocalSystemTime: FILETIME = FILETIME()
14+
FileTimeToLocalFileTime(&ftSystemTime, &ftLocalSystemTime)
15+
16+
let datepicker: DatePicker = DatePicker(frame: .zero)
17+
18+
let time: Date =
19+
Date(timeIntervalSince1970: ftLocalSystemTime.timeIntervalSince1970)
20+
// FIXME(compnerd) can we use a tigher bounds?
21+
XCTAssertTrue(datepicker.date.distance(to: time) <= 1.0)
22+
23+
datepicker.date = Date(timeIntervalSinceReferenceDate: 410220000)
24+
XCTAssertEqual(datepicker.date,
25+
Date(timeIntervalSinceReferenceDate: 410220000))
26+
}
27+
28+
static var allTests = [
29+
("testDateAccessors", testDateAccessors),
30+
]
31+
}

0 commit comments

Comments
 (0)