|
5 | 5 | * SPDX-License-Identifier: BSD-3-Clause
|
6 | 6 | **/
|
7 | 7 |
|
| 8 | +import WinSDK |
| 9 | + |
8 | 10 | extension MenuElement {
|
9 | 11 | /// Attributes that determine the style of the menu element.
|
10 | 12 | public struct Attributes: OptionSet {
|
@@ -55,15 +57,60 @@ public class MenuElement {
|
55 | 57 | /// Getting the Element Attributes
|
56 | 58 |
|
57 | 59 | /// The title of the menu element.
|
58 |
| - public var title: String |
| 60 | + public let title: String |
59 | 61 |
|
60 | 62 | /// The image to display alongside the menu element's title.
|
61 |
| - public var image: Image? |
| 63 | + public let image: Image? |
62 | 64 |
|
63 | 65 | /// Creating a Menu Element
|
64 | 66 |
|
65 |
| - internal init(title: String, image: Image?) { |
| 67 | + public init(title: String, image: Image? = nil) { |
66 | 68 | self.title = title
|
67 | 69 | self.image = image
|
68 | 70 | }
|
69 | 71 | }
|
| 72 | + |
| 73 | +internal class WrappedMenuElement { |
| 74 | + internal var info: MENUITEMINFOW |
| 75 | + private var titleW: [WCHAR] |
| 76 | + private let subMenu: WrappedMenu? |
| 77 | + |
| 78 | + private init(title: String, subMenu: WrappedMenu?, fType: Int32) { |
| 79 | + self.titleW = title.LPCWSTR |
| 80 | + let titleSize = titleW.count |
| 81 | + |
| 82 | + self.subMenu = subMenu |
| 83 | + self.info = titleW.withUnsafeMutableBufferPointer { |
| 84 | + MENUITEMINFOW(cbSize: UINT(MemoryLayout<MENUITEMINFOW>.size), |
| 85 | + fMask: UINT(MIIM_FTYPE | MIIM_STATE | MIIM_ID | MIIM_STRING | MIIM_SUBMENU | MIIM_DATA), |
| 86 | + fType: UINT(fType), |
| 87 | + fState: UINT(MFS_ENABLED), |
| 88 | + wID: UInt32.random(in: .min ... .max), |
| 89 | + hSubMenu: subMenu?.hMenu, |
| 90 | + hbmpChecked: nil, |
| 91 | + hbmpUnchecked: nil, |
| 92 | + dwItemData: 0, |
| 93 | + dwTypeData: $0.baseAddress, |
| 94 | + cch: UINT(titleSize), |
| 95 | + hbmpItem: nil) |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + internal static func of(_ element: MenuElement) -> WrappedMenuElement { |
| 100 | + let subMenu: WrappedMenu? |
| 101 | + if let menu = element as? Menu { |
| 102 | + subMenu = WrappedMenu.ofContextMenu(menu) |
| 103 | + } else { |
| 104 | + subMenu = nil |
| 105 | + } |
| 106 | + return WrappedMenuElement(title: element.title, subMenu: subMenu, fType: MFT_STRING) |
| 107 | + } |
| 108 | + |
| 109 | + internal static func ofSeparator() -> WrappedMenuElement { |
| 110 | + return WrappedMenuElement(title: "", subMenu: nil, fType: MFT_SEPARATOR) |
| 111 | + } |
| 112 | + |
| 113 | + internal var isSeparator: Bool { |
| 114 | + info.fType == MFT_SEPARATOR |
| 115 | + } |
| 116 | +} |
0 commit comments