ResolvedMenu.swift 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /// A generic representation of an application menu, pop-up menu, or context menu.
  2. /// This is what eventually gets passed through to the backend.
  3. ///
  4. /// Referred to as 'resolved' because SwiftCrossUI provides some relatively
  5. /// abstract ways to specify menu items, and this is the format that it resolves
  6. /// all menus to eventually.
  7. public struct ResolvedMenu {
  8. /// The menu's items.
  9. public var items: [Item]
  10. /// Creates a ``ResolvedMenu`` instance.
  11. ///
  12. /// - Parameter items: The menu's items.
  13. public init(items: [ResolvedMenu.Item]) {
  14. self.items = items
  15. }
  16. /// A menu item.
  17. public enum Item {
  18. /// A button.
  19. ///
  20. /// - Parameters:
  21. /// - label: The button's label.
  22. /// - action: The action to perform when the button is activated. `nil`
  23. /// means the button is disabled.
  24. case button(_ label: String, _ action: (@MainActor () -> Void)?)
  25. /// A toggle that manages boolean state.
  26. ///
  27. /// Usually appears as a checkbox.
  28. ///
  29. /// - Parameters:
  30. /// - label: The toggle's label.
  31. /// - value: The toggle's current state.
  32. /// - onChange: Called whenever the user changes the toggle's state.
  33. case toggle(_ label: String, _ value: Bool, onChange: @MainActor (Bool) -> Void)
  34. /// A section separator.
  35. case separator
  36. /// A named submenu.
  37. case submenu(Submenu)
  38. /// A wrapper for a menu item that modifies its environment.
  39. ///
  40. /// - Parameters:
  41. /// - item: The item to modify the environment of.
  42. /// - modification: A function that modifies a given
  43. /// `EnvironmentValues` instance.
  44. indirect case modifiedEnvironment(
  45. _ item: Item,
  46. _ modification: (EnvironmentValues) -> EnvironmentValues
  47. )
  48. }
  49. /// A named submenu.
  50. public struct Submenu {
  51. /// The label of the submenu's entry in its parent menu.
  52. public var label: String
  53. /// The menu displayed when the submenu gets activated.
  54. public var content: ResolvedMenu
  55. /// Creates a ``Submenu`` instance.
  56. ///
  57. /// - Parameters:
  58. /// - label: The label of the submenu's entry in its parent menu.
  59. /// - content: The menu displayed when the submenu gets activated.
  60. public init(label: String, content: ResolvedMenu) {
  61. self.label = label
  62. self.content = content
  63. }
  64. }
  65. }