Skip to content

Commit d8e0749

Browse files
committed
Fix buttons and toggles
1 parent 09b4645 commit d8e0749

File tree

8 files changed

+32
-7
lines changed

8 files changed

+32
-7
lines changed

Sources/AppKitBackend/AppKitBackend.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ public final class AppKitBackend: AppBackend {
1818
public let defaultTableRowContentHeight = 20
1919
public let defaultTableCellVerticalPadding = 4
2020
public let defaultPaddingAmount = 10
21+
public let requiresToggleSwitchSpacer = false
22+
public let defaultToggleStyle = ToggleStyle.button
2123

2224
public var scrollBarWidth: Int {
2325
// We assume that all scrollers have their controlSize set to `.regular` by default.

Sources/GtkBackend/GtkBackend.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ public final class GtkBackend: AppBackend {
2727
public let defaultTableCellVerticalPadding = 4
2828
public let defaultPaddingAmount = 10
2929
public let scrollBarWidth = 0
30+
public let requiresToggleSwitchSpacer = false
31+
public let defaultToggleStyle = ToggleStyle.button
3032

3133
var gtkApp: Application
3234

Sources/SwiftCrossUI/Backend/AppBackend.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ public protocol AppBackend {
6767
/// ensure that the configured root environment change handler gets called so
6868
/// that SwiftCrossUI can update the app's layout accordingly.
6969
var scrollBarWidth: Int { get }
70+
/// If `true`, a toggle in the ``ToggleStyle/switch`` style grows to fill its parent container.
71+
var requiresToggleSwitchSpacer: Bool { get }
72+
/// The default style for toggles.
73+
var defaultToggleStyle: ToggleStyle { get }
7074

7175
/// Often in UI frameworks (such as Gtk), code is run in a callback
7276
/// after starting the app, and hence this generic root window creation

Sources/SwiftCrossUI/Views/Toggle.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
public struct Toggle: View {
2+
@Environment(\.backend) var backend
3+
24
/// The style of toggle shown.
35
var selectedToggleStyle: ToggleStyle
46
/// The label to be shown on or beside the toggle.
@@ -8,7 +10,7 @@ public struct Toggle: View {
810

911
/// Creates a toggle that displays a custom label.
1012
public init(_ label: String, active: Binding<Bool>) {
11-
self.selectedToggleStyle = .button
13+
self.selectedToggleStyle = backend.defaultToggleStyle
1214
self.label = label
1315
self.active = active
1416
}
@@ -18,6 +20,11 @@ public struct Toggle: View {
1820
case .switch:
1921
HStack {
2022
Text(label)
23+
24+
if backend.requiresToggleSwitchSpacer {
25+
Spacer()
26+
}
27+
2128
ToggleSwitch(active: active)
2229
}
2330
case .button:

Sources/UIKitBackend/UIKitBackend+Control.swift

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,13 @@ internal final class ButtonWidget: WrapperWidget<UIButton> {
1717
}
1818

1919
init() {
20-
super.init(child: UIButton())
20+
let type: UIButton.ButtonType
21+
#if os(tvOS)
22+
type = .system
23+
#else
24+
type = .custom
25+
#endif
26+
super.init(child: UIButton(type: type))
2127
child.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
2228
}
2329
}
@@ -216,7 +222,8 @@ extension UIKitBackend {
216222
) {
217223
let buttonWidget = button as! ButtonWidget
218224
buttonWidget.child.setAttributedTitle(
219-
UIKitBackend.attributedString(text: label, environment: environment),
225+
UIKitBackend.attributedString(
226+
text: label, environment: environment, defaultForegroundColor: .link),
220227
for: .normal
221228
)
222229
buttonWidget.onTap = action

Sources/UIKitBackend/UIKitBackend+Passive.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import UIKit
1111
extension UIKitBackend {
1212
internal static func attributedString(
1313
text: String,
14-
environment: EnvironmentValues
14+
environment: EnvironmentValues,
15+
defaultForegroundColor: UIColor = .label
1516
) -> NSAttributedString {
1617
let paragraphStyle = NSMutableParagraphStyle()
1718
paragraphStyle.alignment =
@@ -29,7 +30,7 @@ extension UIKitBackend {
2930
string: text,
3031
attributes: [
3132
.font: environment.font.uiFont,
32-
.foregroundColor: UIColor(color: environment.suggestedForegroundColor),
33+
.foregroundColor: environment.foregroundColor?.uiColor ?? defaultForegroundColor,
3334
.paragraphStyle: paragraphStyle,
3435
]
3536
)

Sources/UIKitBackend/UIKitBackend.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ internal var hasReturnedAWindow = false
2020
public final class UIKitBackend: AppBackend {
2121
public let scrollBarWidth = 0
2222
public let defaultPaddingAmount = 15
23+
public let requiresToggleSwitchSpacer = true
24+
public let defaultToggleStyle = ToggleStyle.switch
2325

2426
// TODO: When tables are supported, update these
2527
public let defaultTableRowContentHeight = -1
@@ -63,8 +65,6 @@ public final class UIKitBackend: AppBackend {
6365
break
6466
}
6567

66-
environment.foregroundColor = Color(UIColor.label)
67-
6868
return environment
6969
}
7070

Sources/WinUIBackend/WinUIBackend.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ public struct WinUIBackend: AppBackend {
3535
public let defaultTableRowContentHeight = 20
3636
public let defaultTableCellVerticalPadding = 4
3737
public let defaultPaddingAmount = 10
38+
public let requiresToggleSwitchSpacer = false
39+
public let defaultToggleStyle = ToggleStyle.button
3840

3941
public var scrollBarWidth: Int {
4042
12

0 commit comments

Comments
 (0)