// // DefaultMenu.swift // MacBackend // // Created by david-swift on 10.09.2024. // import AppKit /// A services submenu. public struct ServicesMenu: MenuWidget { /// The label of the submenu. var label: String /// Initialize a submenu. /// - Parameter label: The submenu's label. public init(_ label: String) { self.label = label } /// The view storage. /// - Parameters: /// - data: The widget data. /// - type: The type of the views. /// - Returns: The view storage. public func container( data: WidgetData, type: Data.Type ) -> ViewStorage where Data: ViewRenderData { let item = NSMenuItem() item.submenu = .init() NSApp.servicesMenu = item.submenu let storage = ViewStorage(item) update(storage, data: data, updateProperties: true, type: type) return storage } /// Update the stored content. /// - Parameters: /// - storage: The storage to update. /// - data: The widget data. /// - updateProperties: Whether to update the properties. /// - type: The type of the views. public func update( _ storage: ViewStorage, data: WidgetData, updateProperties: Bool, type: Data.Type ) where Data: ViewRenderData { guard updateProperties, let item = storage.pointer as? NSMenuItem else { return } let previousState = storage.previousState as? Self if previousState?.label != label { item.title = label } storage.previousState = self } }