Skip to content

Commit 4d7f528

Browse files
committed
View and Controls: remove context menu handling
This was not completed, and more importantly, took over a name that is meant to be used in subclasses. This renames the `menu` item to `hMenu`, and makes it a menu handle. The construction of the menu needs to be plumbed through, but this removes the old handling while the new handling is implemented.
1 parent e3acd01 commit 4d7f528

File tree

2 files changed

+12
-81
lines changed

2 files changed

+12
-81
lines changed

Sources/SwiftWin32/Platform/Win32+Menu.swift

Lines changed: 4 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -6,78 +6,6 @@ import WinSDK
66
@_implementationOnly
77
import OrderedCollections
88

9-
internal struct Win32Menu {
10-
internal let hMenu: MenuHandle
11-
12-
private let items: [Win32MenuElement]
13-
14-
internal init(_ hMenu: MenuHandle, items: [MenuElement]) {
15-
self.hMenu = hMenu
16-
self.items = items.map { Win32MenuElement($0) }
17-
for (index, child) in self.items.enumerated() {
18-
InsertMenuItemW(hMenu.value, UINT(index), true, &child.info)
19-
}
20-
}
21-
}
22-
23-
internal class Win32MenuElement {
24-
private var title: [WCHAR]
25-
private let submenu: Win32Menu?
26-
private let image: BitmapHandle?
27-
28-
internal var info: MENUITEMINFOW
29-
30-
private init(title: String, image: Image?, submenu: Win32Menu?, fType: Int32) {
31-
self.title = title.wide
32-
self.submenu = submenu
33-
34-
let imageHandle: BitmapHandle?
35-
if let bitmap = image?.bitmap {
36-
imageHandle = BitmapHandle(from: bitmap)
37-
} else {
38-
imageHandle = nil
39-
}
40-
self.image = imageHandle
41-
42-
self.info = self.title.withUnsafeMutableBufferPointer {
43-
MENUITEMINFOW(cbSize: UINT(MemoryLayout<MENUITEMINFOW>.size),
44-
fMask: UINT(MIIM_FTYPE | MIIM_STATE | MIIM_ID | MIIM_STRING | MIIM_SUBMENU | MIIM_DATA | MIIM_BITMAP),
45-
fType: UINT(fType),
46-
fState: UINT(MFS_ENABLED),
47-
wID: UInt32.random(in: .min ... .max),
48-
hSubMenu: submenu?.hMenu.value,
49-
hbmpChecked: nil,
50-
hbmpUnchecked: nil,
51-
dwItemData: 0,
52-
dwTypeData: $0.baseAddress,
53-
cch: UINT(title.count),
54-
hbmpItem: imageHandle?.value)
55-
}
56-
}
57-
58-
internal convenience init(_ element: MenuElement) {
59-
if let menu = element as? Menu {
60-
self.init(title: element.title,
61-
image: element.image,
62-
submenu: Win32Menu(MenuHandle(owning: CreatePopupMenu()),
63-
items: menu.children),
64-
fType: MFT_STRING)
65-
} else {
66-
self.init(title: element.title, image: element.image, submenu: nil, fType: MFT_STRING)
67-
}
68-
}
69-
}
70-
71-
extension Win32MenuElement {
72-
internal static var separator: Win32MenuElement {
73-
Win32MenuElement(title: "", image: nil, submenu: nil, fType: MFT_SEPARATOR)
74-
}
75-
76-
internal var isSeparator: Bool {
77-
info.fType == MFT_SEPARATOR
78-
}
79-
}
80-
819
internal final class _MenuBuilder: MenuSystem {
8210
internal private(set) var hMenu: MenuHandle
8311
internal private(set) weak var view: View?
@@ -93,6 +21,10 @@ internal final class _MenuBuilder: MenuSystem {
9321
self.view = view
9422
self.menus = []
9523
}
24+
25+
override func setNeedsRebuild() {
26+
// TODO(compnerd) create the actual menus
27+
}
9628
}
9729

9830
extension _MenuBuilder: MenuBuilder {

Sources/SwiftWin32/Views and Controls/View.swift

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,26 +23,25 @@ private let SwiftViewProc: SUBCLASSPROC = { (hWnd, uMsg, wParam, lParam, uIdSubc
2323
break
2424
}
2525

26-
let x = LOWORD(lParam), y = HIWORD(lParam)
27-
2826
// Clear any existing menu.
29-
view.menu = nil
27+
view.hMenu = nil
3028

29+
let x = LOWORD(lParam), y = HIWORD(lParam)
3130
if let actions = interaction.delegate?
3231
.contextMenuInteraction(interaction,
3332
configurationForMenuAtLocation: Point(x: x, y: y))?
3433
.actionProvider?([]) {
35-
// TODO: handle a possible failure in `CreatePopupMenu`
36-
view.menu = Win32Menu(MenuHandle(owning: CreatePopupMenu()),
37-
items: actions.children)
38-
_ = TrackPopupMenu(view.menu?.hMenu.value, UINT(TPM_RIGHTBUTTON),
39-
Int32(x), Int32(y), 0, view.hWnd, nil)
4034
}
35+
let position: Point = interaction.location(in: view)
36+
_ = TrackPopupMenu(view.hMenu?.value, UINT(TPM_RIGHTBUTTON),
37+
Int32(x), Int32(y), 0, view.hWnd, nil)
4138

4239
return 0
40+
4341
case UINT(WM_COMMAND):
4442
// TODO: handle menu actions
4543
break
44+
4645
default:
4746
break
4847
}
@@ -238,7 +237,7 @@ public class View: Responder {
238237
}
239238
}
240239

241-
internal var menu: Win32Menu? = nil
240+
internal var hMenu: MenuHandle?
242241

243242
// MARK - Creating a View Object
244243

0 commit comments

Comments
 (0)