Skip to content

Commit 9cd4256

Browse files
committed
Implement temporary _buttonWidth modifier for Buttons and Menus
1 parent 57a53ed commit 9cd4256

File tree

4 files changed

+40
-6
lines changed

4 files changed

+40
-6
lines changed

Sources/AppKitBackend/AppKitBackend.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,10 @@ public final class AppKitBackend: AppBackend {
420420
environment: EnvironmentValues
421421
) {
422422
let button = button as! NSButton
423-
button.attributedTitle = Self.attributedString(for: label, in: environment)
423+
button.attributedTitle = Self.attributedString(
424+
for: label,
425+
in: environment.with(\.multilineTextAlignment, .center)
426+
)
424427
button.bezelStyle = .regularSquare
425428
button.appearance = environment.colorScheme.nsAppearance
426429
button.onAction = { _ in

Sources/SwiftCrossUI/Backend/AppBackend.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ public protocol AppBackend {
288288
/// Creates a labelled button with an action triggered on click. Predominantly used
289289
/// by ``Button``.
290290
func createButton() -> Widget
291-
/// Sets a button's label and action. The action replaces any existing actions..
291+
/// Sets a button's label and action. The action replaces any existing actions.
292292
func updateButton(
293293
_ button: Widget,
294294
label: String,

Sources/SwiftCrossUI/Views/Button.swift

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ public struct Button: ElementaryView, View {
44
var label: String
55
/// The action to be performed when the button is clicked.
66
var action: () -> Void
7+
/// The button's forced width if provided.
8+
var width: Int?
79

810
/// Creates a button that displays a custom label.
911
public init(_ label: String, action: @escaping () -> Void = {}) {
@@ -32,7 +34,25 @@ public struct Button: ElementaryView, View {
3234
// refresh), but the reason Gtk 3 doesn't like it is that the window gets set smaller
3335
// than its content I think.
3436
// See: https://github.com/stackotter/swift-cross-ui/blob/27f50579c52e79323c3c368512d37e95af576c25/Sources/SwiftCrossUI/Scenes/WindowGroupNode.swift#L140
35-
backend.updateButton(widget, label: label, action: action, environment: environment)
36-
return ViewSize(fixedSize: backend.naturalSize(of: widget))
37+
backend.updateButton(
38+
widget,
39+
label: label,
40+
action: action,
41+
environment: environment
42+
)
43+
let naturalSize = backend.naturalSize(of: widget)
44+
let size = SIMD2(
45+
width ?? naturalSize.x,
46+
naturalSize.y
47+
)
48+
backend.setSize(of: widget, to: size)
49+
return ViewSize(fixedSize: size)
50+
}
51+
52+
/// A temporary button width solution until arbitrary labels are supported.
53+
public func _buttonWidth(_ width: Int?) -> Button {
54+
var button = self
55+
button.width = width
56+
return button
3757
}
3858
}

Sources/SwiftCrossUI/Views/Menu.swift

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ public struct Menu: TypeSafeView {
22
public var label: String
33
public var items: [MenuItem]
44

5+
var buttonWidth: Int?
6+
57
public var body = EmptyView()
68

79
public init(_ label: String, @MenuItemsBuilder items: () -> [MenuItem]) {
@@ -65,10 +67,10 @@ public struct Menu: TypeSafeView {
6567
) -> ViewSize {
6668
// TODO: Store popped menu in view graph node children so that we can
6769
// continue updating it even once it's open.
68-
let size = backend.naturalSize(of: widget)
70+
var size = backend.naturalSize(of: widget)
71+
size.x = buttonWidth ?? size.x
6972

7073
let content = resolve().content
71-
7274
backend.updateButton(
7375
widget,
7476
label: label,
@@ -86,9 +88,18 @@ public struct Menu: TypeSafeView {
8688
},
8789
environment: environment
8890
)
91+
backend.setSize(of: widget, to: size)
92+
8993
children.updateMenuIfShown(content: content, environment: environment, backend: backend)
9094
return ViewSize(fixedSize: size)
9195
}
96+
97+
/// A temporary button width solution until arbitrary labels are supported.
98+
public func _buttonWidth(_ width: Int?) -> Menu {
99+
var menu = self
100+
menu.buttonWidth = width
101+
return menu
102+
}
92103
}
93104

94105
class MenuStorage: ViewGraphNodeChildren {

0 commit comments

Comments
 (0)