@@ -11,8 +11,26 @@ private let SwiftViewProc: SUBCLASSPROC = { (hWnd, uMsg, wParam, lParam, uIdSubc
11
11
let view : View ? = unsafeBitCast ( dwRefData, to: AnyObject . self) as? View
12
12
switch uMsg {
13
13
case UINT ( WM_CONTEXTMENU) :
14
- // TODO handle popup menu events
14
+ guard let view = view,
15
+ let menuInteraction = view. interactions. first ( where: { $0 is ContextMenuInteraction } )
16
+ as? ContextMenuInteraction else { break }
17
+ let x = Int16 ( truncatingIfNeeded: lParam)
18
+ let y = Int16 ( truncatingIfNeeded: lParam >> 16 )
19
+ let point = Point ( x: Int ( x) , y: Int ( y) )
20
+ let menuConfiguration = menuInteraction. delegate? . contextMenuInteraction ( menuInteraction,
21
+ configurationForMenuAtLocation: point)
22
+ if let menu = menuConfiguration? . provideActions ( ) {
23
+ view. win32ContextMenu = Win32Menu ( MenuHandle ( owning: CreatePopupMenu ( ) ) , children: menu. children)
24
+ } else {
25
+ view. win32ContextMenu = nil
26
+ }
27
+ let hMenu = view. win32ContextMenu? . hMenu. value
28
+ TrackPopupMenu ( hMenu, UINT ( TPM_RIGHTBUTTON) ,
29
+ Int32 ( x) , Int32 ( y) , 0 , view. hWnd, nil )
15
30
return 0
31
+ case UINT ( WM_COMMAND) :
32
+ // TODO handle menu actions
33
+ break
16
34
default :
17
35
break
18
36
}
@@ -202,6 +220,16 @@ public class View: Responder {
202
220
set { _ = EnableWindow ( self . hWnd, newValue) }
203
221
}
204
222
223
+ public private( set) var interactions : [ Interaction ] = [ ]
224
+ internal var win32ContextMenu : Win32Menu ? = nil
225
+
226
+ public func addInteraction( _ interaction: Interaction ) {
227
+ interaction. willMove ( to: self )
228
+ interaction. view? . interactions. removeAll ( where: { $0 === interaction } )
229
+ interactions. append ( interaction)
230
+ interaction. didMove ( to: self )
231
+ }
232
+
205
233
// MARK - Managing the View Hierarchy
206
234
207
235
public private( set) var superview : View ?
0 commit comments