Skip to content

Commit 01b095c

Browse files
authored
feat: 🎸 add isInMenu environment key and a misc test (#1207)
1 parent 1eb71fe commit 01b095c

File tree

3 files changed

+109
-4
lines changed

3 files changed

+109
-4
lines changed

Apps/Examples/Examples/FioriSwiftUICore/FioriButton/FioriButtonContentView.swift

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import FioriSwiftUICore
12
import Foundation
23
import SwiftUI
34

@@ -12,7 +13,98 @@ struct FioriButtonContentView: View {
1213
NavigationLink("Button In List - Multiple Lines", destination: LazyView(FioriButtonInListMultipleLineExample()))
1314
NavigationLink("Custom Button", destination: LazyView(FioriButtonCustomButtonExample()))
1415
NavigationLink("In-Place Loading Button", destination: LazyView(InPlaceLoadingContentView()))
16+
NavigationLink("Misc Tests", destination: LazyView(MiscTestsView()))
1517
}
1618
.navigationTitle("FioriButton")
1719
}
1820
}
21+
22+
typealias Action = () -> Void
23+
24+
struct ButtonView: View {
25+
@Environment(\.isInMenu) private var isInMenu
26+
27+
var title: String
28+
var action: Action
29+
init(title: String, action: @escaping Action) {
30+
self.title = title
31+
self.action = action
32+
}
33+
34+
var body: some View {
35+
FioriButton { _ in
36+
print("ButtonView \"\(self.title)\" tappeded, isInMenu: \(self.isInMenu)")
37+
if self.isInMenu {
38+
self.action()
39+
}
40+
} label: { _ in
41+
Text(self.title)
42+
}.highPriorityGesture(TapGesture().onEnded {
43+
print("ButtonView \"\(self.title)\" tapped and triggered highPriorityGesture(TapGesture()), isInMenu: \(self.isInMenu)")
44+
if !self.isInMenu {
45+
self.action()
46+
}
47+
})
48+
}
49+
}
50+
51+
struct MiscTestsView: View {
52+
var body: some View {
53+
List {
54+
ButtonView(title: "Button") {
55+
print("ButtonView action called")
56+
}
57+
58+
Menu {
59+
ButtonView(title: "Button in a Menu") {
60+
print("ButtonView action called")
61+
}.environment(\.isInMenu, true)
62+
} label: {
63+
Text("Menu Button Test")
64+
}
65+
66+
VStack(alignment: .leading, spacing: 0) {
67+
Card {
68+
Text("Title")
69+
} subtitle: {
70+
Text("Subtitle that goes to multiple lines before truncating just like that")
71+
} icons: {
72+
Image(systemName: "exclamationmark.triangle.fill")
73+
.font(.fiori(forTextStyle: .subheadline))
74+
.foregroundColor(.preferredColor(.negativeLabel))
75+
Image(systemName: "paperclip")
76+
.font(.fiori(forTextStyle: .subheadline))
77+
.foregroundColor(.preferredColor(.quaternaryLabel))
78+
Text("1")
79+
.font(.fiori(forTextStyle: .subheadline))
80+
.foregroundColor(.preferredColor(.quaternaryLabel))
81+
} headerAction: {
82+
ButtonView(title: "Button in the card") {
83+
print("ButtonView action called")
84+
}
85+
} counter: {
86+
Text("1 of 3")
87+
}
88+
89+
.onTapGesture {
90+
print("Card tapped")
91+
}
92+
93+
CardFooter {
94+
ButtonView(title: "Primary") {
95+
print("ButtonView action called")
96+
}
97+
} secondaryAction: {
98+
ButtonView(title: "Secondary") {
99+
print("ButtonView action called")
100+
}
101+
} tertiaryAction: {
102+
ButtonView(title: "Tertiary") {
103+
print("ButtonView action called")
104+
}
105+
}
106+
}
107+
}
108+
.listRowSpacing(16)
109+
}
110+
}

Sources/FioriSwiftUICore/Utils/EnvironmentValues.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,16 @@ public extension EnvironmentValues {
2020
}
2121
}
2222
}
23+
24+
/// The key for storing whether the component is in a `Menu` in the environment. Default value is false.
25+
public struct IsInMenuKey: EnvironmentKey {
26+
public static let defaultValue: Bool = false
27+
}
28+
29+
public extension EnvironmentValues {
30+
/// get / set `isInMenu` value in the environment. Can be used to determine if the component such as a `FioriButton` is put inside a `Menu`.
31+
var isInMenu: Bool {
32+
get { self[IsInMenuKey.self] }
33+
set { self[IsInMenuKey.self] = newValue }
34+
}
35+
}

Sources/FioriSwiftUICore/_FioriStyles/CardFooterStyle.fiori.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -216,13 +216,13 @@ public struct CardFooterBaseStyle: CardFooterStyle {
216216
Menu {
217217
if self.numButtonsDisplayInOverflow == 1 {
218218
if !configuration.tertiaryAction.isEmpty {
219-
configuration.tertiaryAction
219+
configuration.tertiaryAction.environment(\.isInMenu, true)
220220
} else {
221-
configuration.secondaryAction
221+
configuration.secondaryAction.environment(\.isInMenu, true)
222222
}
223223
} else if self.numButtonsDisplayInOverflow == 2 {
224-
configuration.secondaryAction
225-
configuration.tertiaryAction
224+
configuration.secondaryAction.environment(\.isInMenu, true)
225+
configuration.tertiaryAction.environment(\.isInMenu, true)
226226
}
227227
} label: {
228228
configuration.overflowAction

0 commit comments

Comments
 (0)