Skip to content

Commit cd737cb

Browse files
committed
SwiftWin32: add Label.textAlignment property
Add the missing `textAlignment` property for the partial cases which are trivial to map. This allows for better control over the rendering of the text.
1 parent 30623a4 commit cd737cb

File tree

5 files changed

+70
-8
lines changed

5 files changed

+70
-8
lines changed

Sources/SwiftWin32/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ target_sources(SwiftWin32 PRIVATE
117117
"Views and Controls/Slider.swift"
118118
"Views and Controls/Stepper.swift"
119119
"Views and Controls/Switch.swift"
120+
"Views and Controls/TextAlignment.swift"
120121
"Views and Controls/TextField.swift"
121122
"Views and Controls/TextView.swift"
122123
"Views and Controls/View.swift"

Sources/SwiftWin32/Views and Controls/Label.swift

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,37 @@ public class Label: Control {
4646
}
4747
}
4848

49+
public var textAlignment: TextAlignment {
50+
get {
51+
switch GWL_STYLE & SS_TYPEMASK {
52+
case SS_CENTER:
53+
return .center
54+
case SS_RIGHT:
55+
return .right
56+
case SS_LEFTNOWORDWRAP, SS_LEFT:
57+
return .left
58+
default:
59+
fatalError("unknown alignment for WC_STATIC")
60+
}
61+
}
62+
63+
set {
64+
var lAlignment = GWL_STYLE & ~SS_TYPEMASK
65+
switch newValue {
66+
case .left: lAlignment |= SS_LEFTNOWORDWRAP
67+
case .right: lAlignment |= SS_RIGHT
68+
case .center: lAlignment |= SS_CENTER
69+
case .justified, .natural:
70+
log.error("TextAlignment.\(newValue) is not supported")
71+
}
72+
GWL_STYLE = lAlignment
73+
if !SetWindowPos(self.hWnd_, nil, 0, 0, 0, 0,
74+
UINT(SWP_NOACTIVATE | SWP_NOCOPYBITS | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_DRAWFRAME)) {
75+
log.warning("SetWindowPos: \(Error(win32: GetLastError()))")
76+
}
77+
}
78+
}
79+
4980
public override var frame: Rect {
5081
didSet {
5182
let size = self.frame.size
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright © 2023 Saleem Abdulrasool <[email protected]>
2+
// SPDX-License-Identifier: BSD-3-Clause
3+
4+
/// Constants that specify text alignment.
5+
public enum TextAlignment: Int {
6+
/// Text uses the default alignment for the current localization of the app.
7+
case natural
8+
/// Text is left-aligned.
9+
case left
10+
/// Text is right-aligned.
11+
case right
12+
/// Text is center-aligned.
13+
case center
14+
/// Text is justified.
15+
case justified
16+
}

Sources/SwiftWin32/Views and Controls/TextField.swift

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,6 @@ private let SwiftTextFieldProc: SUBCLASSPROC = { (hWnd, uMsg, wParam, lParam, uI
5050
return DefSubclassProc(hWnd, uMsg, wParam, lParam)
5151
}
5252

53-
public enum TextAlignment: Int {
54-
case natural
55-
case left
56-
case right
57-
case center
58-
case justified
59-
}
60-
6153
public class TextField: Control {
6254
private static let `class`: WindowClass = WindowClass(named: MSFTEDIT_CLASS)
6355
private static let style: WindowStyle =

Tests/UICoreTests/LabelTests.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright © 2023 Saleem Abdulrasool <[email protected]>
2+
// SPDX-License-Identifier: BSD-3-Clause
3+
4+
import XCTest
5+
@testable import SwiftWin32
6+
7+
final class LabelTests: XCTestCase {
8+
func testAlignment() {
9+
let label: Label = Label(frame: .zero)
10+
11+
// FIXME(compnerd) this should be `natural`
12+
XCTAssertEqual(label.textAlignment, .left)
13+
14+
label.textAlignment = .right
15+
XCTAssertEqual(label.textAlignment, .right)
16+
}
17+
18+
static var allTests = [
19+
("testAlignment", testAlignment),
20+
]
21+
}
22+

0 commit comments

Comments
 (0)