-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathRCTMenuItem.swift
More file actions
142 lines (124 loc) · 4.97 KB
/
RCTMenuItem.swift
File metadata and controls
142 lines (124 loc) · 4.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
//
// RCTMenuItem.swift
// react-native-menu
//
// Created by Jesse Katsumata on 11/8/20.
//
import UIKit;
@available(iOS 13.0, *)
class RCTMenuAction {
var identifier: UIAction.Identifier?;
var title: String;
var subtitle: String?
var displayInline: Bool
var image: UIImage?
var attributes: UIAction.Attributes = []
var state: UIAction.State = .off
var subactions: [RCTMenuAction] = []
// Only available in iOS 16+
var preferredElementSizeString: String?
init(details: NSDictionary){
if let identifier = details["id"] as? NSString {
self.identifier = UIAction.Identifier(rawValue: identifier as String);
}
if let image = details["image"] as? NSString {
self.image = UIImage(systemName: image as String);
if self.image === nil {
self.image = UIImage(named: image as String)
}
if let imageColor = details["imageColor"] {
let uiColor = RCTConvert.uiColor(imageColor)
// Only apply tint if color is valid and not transparent (alpha > 0)
// processColor(undefined) returns transparent color (0,0,0,0) which makes icons invisible
if let color = uiColor {
var red: CGFloat = 0
var green: CGFloat = 0
var blue: CGFloat = 0
var alpha: CGFloat = 0
if color.getRed(&red, green: &green, blue: &blue, alpha: &alpha) && alpha > 0 {
self.image = self.image?.withTintColor(color, renderingMode: .alwaysOriginal)
}
}
}
}
if let title = details["title"] as? NSString {
self.title = title as String;
} else {
self.title = "";
}
if let subtitle = details["subtitle"] as? NSString {
self.subtitle = subtitle as String;
}
if let displayInline = details["displayInline"] as? Bool {
self.displayInline = displayInline as Bool;
} else {
self.displayInline = false;
}
if let attributes = details["attributes"] as? NSDictionary {
if (attributes["destructive"] as? Bool) == true {
self.attributes.update(with: .destructive)
}
if (attributes["disabled"] as? Bool) == true {
self.attributes.update(with: .disabled)
}
if (attributes["hidden"] as? Bool) == true {
self.attributes.update(with: .hidden)
}
if(attributes["keepsMenuPresented"] as? Bool) == true {
if #available(iOS 16.0, *) {
self.attributes.update(with: .keepsMenuPresented)
}
}
}
if let state = details["state"] as? NSString {
if state=="on" {
self.state = .on
}
if state=="off" {
self.state = .off
}
if state=="mixed" {
self.state = .mixed
}
}
if let subactions = details["subactions"] as? NSArray {
if subactions.count > 0 {
for subaction in subactions {
self.subactions.append(RCTMenuAction(details: subaction as! NSDictionary))
}
}
}
if let preferredElementSizeString = details["preferredElementSize"] as? String {
self.preferredElementSizeString = preferredElementSizeString
}
}
func createUIMenuElement(_ handler: @escaping UIActionHandler) -> UIMenuElement {
if subactions.count > 0 {
var subMenuActions: [UIMenuElement] = []
subactions.forEach { subaction in
subMenuActions.append(subaction.createUIMenuElement(handler))
}
var menu: UIMenu;
if self.displayInline {
menu = UIMenu(title: title, image: image, options: .displayInline, children: subMenuActions)
} else {
menu = UIMenu(title: title, image: image, children: subMenuActions)
}
if #available(iOS 16.0, *) {
if(preferredElementSizeString == "small") {
menu.preferredElementSize = .small
} else if(preferredElementSizeString == "medium") {
menu.preferredElementSize = .medium
} else if(preferredElementSizeString == "large") {
menu.preferredElementSize = .large
}
}
return menu
}
if #available(iOS 15, *) {
return UIAction(title: title, subtitle: subtitle, image: image, identifier: identifier, attributes: attributes, state: state, handler: handler)
} else {
return UIAction(title: title, image: image, identifier: identifier, discoverabilityTitle: subtitle, attributes: attributes, state: state, handler: handler)
}
}
}