CefContextMenuHandler.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. namespace Xilium.CefGlue
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.Runtime.InteropServices;
  7. using Xilium.CefGlue.Interop;
  8. /// <summary>
  9. /// Implement this interface to handle context menu events. The methods of this
  10. /// class will be called on the UI thread.
  11. /// </summary>
  12. public abstract unsafe partial class CefContextMenuHandler
  13. {
  14. private void on_before_context_menu(cef_context_menu_handler_t* self, cef_browser_t* browser, cef_frame_t* frame, cef_context_menu_params_t* @params, cef_menu_model_t* model)
  15. {
  16. CheckSelf(self);
  17. var mBrowser = CefBrowser.FromNative(browser);
  18. var mFrame = CefFrame.FromNative(frame);
  19. using (var mState = CefContextMenuParams.FromNative(@params))
  20. using (var mModel = CefMenuModel.FromNative(model))
  21. {
  22. OnBeforeContextMenu(mBrowser, mFrame, mState, mModel);
  23. }
  24. }
  25. /// <summary>
  26. /// Called before a context menu is displayed. |params| provides information
  27. /// about the context menu state. |model| initially contains the default
  28. /// context menu. The |model| can be cleared to show no context menu or
  29. /// modified to show a custom menu. Do not keep references to |params| or
  30. /// |model| outside of this callback.
  31. /// </summary>
  32. protected virtual void OnBeforeContextMenu(CefBrowser browser, CefFrame frame, CefContextMenuParams state, CefMenuModel model)
  33. {
  34. }
  35. private int run_context_menu(cef_context_menu_handler_t* self, cef_browser_t* browser, cef_frame_t* frame, cef_context_menu_params_t* @params, cef_menu_model_t* model, cef_run_context_menu_callback_t* callback)
  36. {
  37. CheckSelf(self);
  38. var mBrowser = CefBrowser.FromNative(browser);
  39. var mFrame = CefFrame.FromNative(frame);
  40. using (var mParameters = CefContextMenuParams.FromNative(@params))
  41. using (var mModel = CefMenuModel.FromNative(model))
  42. {
  43. var mCallback = CefRunContextMenuCallback.FromNative(callback);
  44. var result = RunContextMenu(mBrowser, mFrame, mParameters, mModel, mCallback);
  45. return result ? 1 : 0;
  46. }
  47. }
  48. /// <summary>
  49. /// Called to allow custom display of the context menu. |params| provides
  50. /// information about the context menu state. |model| contains the context menu
  51. /// model resulting from OnBeforeContextMenu. For custom display return true
  52. /// and execute |callback| either synchronously or asynchronously with the
  53. /// selected command ID. For default display return false. Do not keep
  54. /// references to |params| or |model| outside of this callback.
  55. /// </summary>
  56. protected virtual bool RunContextMenu(CefBrowser browser, CefFrame frame, CefContextMenuParams parameters, CefMenuModel model, CefRunContextMenuCallback callback)
  57. {
  58. return false;
  59. }
  60. private int on_context_menu_command(cef_context_menu_handler_t* self, cef_browser_t* browser, cef_frame_t* frame, cef_context_menu_params_t* @params, int command_id, CefEventFlags event_flags)
  61. {
  62. CheckSelf(self);
  63. var mBrowser = CefBrowser.FromNative(browser);
  64. var mFrame = CefFrame.FromNative(frame);
  65. using (var mState = CefContextMenuParams.FromNative(@params))
  66. {
  67. var result = OnContextMenuCommand(mBrowser, mFrame, mState, command_id, event_flags);
  68. return result ? 1 : 0;
  69. }
  70. }
  71. /// <summary>
  72. /// Called to execute a command selected from the context menu. Return true if
  73. /// the command was handled or false for the default implementation. See
  74. /// cef_menu_id_t for the command ids that have default implementations. All
  75. /// user-defined command ids should be between MENU_ID_USER_FIRST and
  76. /// MENU_ID_USER_LAST. |params| will have the same values as what was passed to
  77. /// OnBeforeContextMenu(). Do not keep a reference to |params| outside of this
  78. /// callback.
  79. /// </summary>
  80. protected virtual bool OnContextMenuCommand(CefBrowser browser, CefFrame frame, CefContextMenuParams state, int commandId, CefEventFlags eventFlags)
  81. {
  82. return false;
  83. }
  84. private void on_context_menu_dismissed(cef_context_menu_handler_t* self, cef_browser_t* browser, cef_frame_t* frame)
  85. {
  86. CheckSelf(self);
  87. var mBrowser = CefBrowser.FromNative(browser);
  88. var mFrame = CefFrame.FromNative(frame);
  89. OnContextMenuDismissed(mBrowser, mFrame);
  90. }
  91. /// <summary>
  92. /// Called when the context menu is dismissed irregardless of whether the menu
  93. /// was empty or a command was selected.
  94. /// </summary>
  95. protected virtual void OnContextMenuDismissed(CefBrowser browser, CefFrame frame)
  96. {
  97. }
  98. }
  99. }