Instantly share code, notes, and snippets.
🏠
Working from home
- Ekaterinburg
- https://achernoprudov.github.io
achernoprudov
/ UIMenu+build-level3.swift
Created
July 27, 2021 12:15
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
UIMenu { | |
UIAction(title: "Open", image: UIImage(systemName: "xmark.octagon")) { _ in | |
... | |
} | |
UIMenu(title: "Edit") { | |
if item.isHidden { | |
UIAction(title: "Show", image: UIImage(systemName: "eye")) { _ in | |
... | |
} | |
} else { |
achernoprudov
/ MenuElementsBuilder.swift
Last active
July 27, 2021 12:13
Final implementation of the MenuElementsBuilder result builder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@resultBuilder | |
enum MenuElementsBuilder { | |
static func buildBlock(_ elements: [UIMenuElement]...) -> [UIMenuElement] { | |
return elements.flatMap { $0 } | |
} | |
static func buildExpression(_ element: UIMenuElement) -> [UIMenuElement] { | |
return [element] | |
} |
achernoprudov
/ MenuElementBuilder-array.swift
Last active
July 27, 2021 12:01
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@resultBuilder | |
enum MenuElementsBuilder { | |
static func buildExpression(_ element: UIMenuElement) -> [UIMenuElement] { | |
return [element] | |
} | |
static func buildBlock(_ elements: [UIMenuElement]...) -> [UIMenuElement] { | |
return elements.flatMap { $0 } | |
} |
achernoprudov
/ UIMenu+BuildOptional.swift
Created
July 27, 2021 11:24
Build optional function for Menu result builder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static func buildOptional(_ elements: [UIMenuElement]?) -> [UIMenuElement] { | |
return elements ?? [] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
UIMenu { | |
UIAction(title: "Open", image: UIImage(systemName: "xmark.octagon")) { _ in | |
... | |
} | |
UIMenu(title: "Edit") { | |
item.isHidden | |
? UIAction(title: "Show", image: UIImage(systemName: "eye")) { _ in | |
... | |
} | |
: UIAction(title: "Hide", image: UIImage(systemName: "eye.slash")) { _ in |
achernoprudov
/ UIMenu+Build.swift
Last active
November 2, 2021 08:23
UIMenu extension with primitive result builder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@resultBuilder | |
enum MenuElementsBuilder { | |
static func buildBlock(_ elements: UIMenuElement...) -> [UIMenuElement] { | |
return elements | |
} | |
} | |
extension UIMenu { | |
convenience init(_ title: String = "", @MenuElementsBuilder builder: () -> [UIMenuElement]) { | |
self.init(title: title, children: builder()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
UIMenu { | |
UIAction("Open", icon "xmark.octagon") { _ in | |
... | |
} | |
UIMenu(title: "Edit") { | |
if item.isHidden { | |
UIAction("Show", icon: "eye") { _ in | |
... | |
} | |
} else { |
achernoprudov
/ UIAction+Build.swift
Last active
July 27, 2021 11:03
UIAction convenience initializer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension UIAction { | |
convenience init(_ title: String, icon systemIconName: String = "", handler: @escaping UIActionHandler) { | |
self.init(title: title, image: UIImage(systemName: systemIconName), handler: handler) | |
} | |
} |
achernoprudov
/ uimenu-build-array.swift
Last active
July 27, 2021 11:03
Building UIMenu in array DSL way
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
UIMenu(children: [ | |
UIAction(title: "Open", image: UIImage(systemName: "xmark.octagon")) { _ in | |
... | |
}, | |
UIMenu(title: "Edit", children: [ | |
item.isHidden | |
? UIAction(title: "Show", image: UIImage(systemName: "eye")) { _ in | |
... | |
} | |
: UIAction(title: "Hide", image: UIImage(systemName: "eye.slash")) { _ in |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var actions: [UIAction] = [] | |
actions.append(UIAction(title: "Open", image: UIImage(systemName: "xmark.octagon")) { _ in | |
... | |
}) | |
// Sub menu | |
var subMenuActions: [UIAction] = [] | |
if item.isHidden { | |
subMenuActions.append(UIAction(title: "Show", image: UIImage(systemName: "eye")) { _ in | |
... | |
}) |
NewerOlder