//---------------------------------------- // // Copyright © yanghy. All Rights Reserved. // // Licensed under Apache License Version 2.0, January 2004 // // https://www.apache.org/licenses/LICENSE-2.0 // //---------------------------------------- // chromium event 默认事件实现 package cef import ( "github.com/energye/energy/v2/cef/i18n" "github.com/energye/energy/v2/cef/internal/window" "github.com/energye/energy/v2/common" "github.com/energye/energy/v2/consts" "github.com/energye/energy/v2/logger" "github.com/energye/golcl/lcl" "github.com/energye/golcl/lcl/rtl" "github.com/energye/golcl/lcl/types" "github.com/energye/golcl/lcl/types/messages" ) // chromiumOnAfterCreate 事件处理函数返回true将不继续执行 func chromiumOnAfterCreate(browser *ICefBrowser) bool { if common.IsWindows() { rtl.SendMessage(browser.HostWindowHandle(), messages.WM_SETICON, 1, lcl.Application.Icon().Handle()) } // 浏览器创建完之后 var isMainWindow = browser.Identifier() == BrowserWindow.MainWindow().Id() if isMainWindow { // 主窗口 if BrowserWindow.MainWindow().IsLCL() { //更新windowParent位置和大小 wp := BrowserWindow.MainWindow().AsLCLBrowserWindow().WindowParent() if wp.Align() != types.AlClient { rect := wp.BoundsRect() rect.Left, rect.Top = wp.point() rect.SetSize(wp.size()) wp.SetBoundsRect(rect) } wp.UpdateSize() } } return false } // chromiumOnBeforeBrowser func chromiumOnBeforeBrowser(browser *ICefBrowser, frame *ICefFrame) { if window.CurrentBrowseWindowCache != nil { bw := window.CurrentBrowseWindowCache.(IBrowserWindow) if bw.Id() != browser.Identifier() { return } window.CurrentBrowseWindowCache = nil if bw.WindowType() == consts.WT_DEV_TOOLS || bw.WindowType() == consts.WT_VIEW_SOURCE || bw.WindowProperty().WindowType == consts.WT_DEV_TOOLS || bw.WindowProperty().WindowType == consts.WT_VIEW_SOURCE { return } BrowserWindow.putWindowInfo(browser.BrowserId(), bw) } if !consts.IsMessageLoop { QueueAsyncCall(func(id int) { // lcl BrowserWindow.createNextLCLPopupWindow() }) } } // chromiumOnBeforeClose - chromium 关闭之前 func chromiumOnBeforeClose(browser *ICefBrowser) { BrowserWindow.removeWindowInfo(browser.Identifier()) } // chromiumOnFrameDetached func chromiumOnFrameDetached(browser *ICefBrowser, frame *ICefFrame) { } var ( refreshId, forcedRefreshId consts.MenuId devToolsId, viewSourceId, closeBrowserId consts.MenuId backId, forwardId, printId consts.MenuId undoId, redoId, cutId, copyId, pasteId, delId, selectAllId consts.MenuId imageUrlId, copyImageId, imageSaveId, aUrlId consts.MenuId ) // chromiumOnBeforeContextMenu 右键菜单 - 默认实现 func chromiumOnBeforeContextMenu(sender lcl.IObject, browser *ICefBrowser, frame *ICefFrame, params *ICefContextMenuParams, model *ICefMenuModel) { if !BrowserWindow.Config.ChromiumConfig().EnableMenu() { model.Clear() return } if winInfo := BrowserWindow.GetWindowInfo(browser.Identifier()); winInfo != nil { //开发者工具和显示源代码不展示自定义默认菜单 if winInfo.WindowType() == consts.WT_DEV_TOOLS || winInfo.WindowType() == consts.WT_VIEW_SOURCE { return } } undoVisible, undoEnabled := model.IsVisible(consts.MENU_ID_UNDO), model.IsEnabled(consts.MENU_ID_UNDO) redoVisible, redoEnabled := model.IsVisible(consts.MENU_ID_REDO), model.IsEnabled(consts.MENU_ID_REDO) cutVisible, cutEnabled := model.IsVisible(consts.MENU_ID_CUT), model.IsEnabled(consts.MENU_ID_CUT) copyVisible, copyEnabled := model.IsVisible(consts.MENU_ID_COPY), model.IsEnabled(consts.MENU_ID_COPY) pasteVisible, pasteEnabled := model.IsVisible(consts.MENU_ID_PASTE), model.IsEnabled(consts.MENU_ID_PASTE) selectAllVisible, selectAllEnabled := model.IsVisible(consts.MENU_ID_SELECT_ALL), model.IsEnabled(consts.MENU_ID_SELECT_ALL) model.Clear() if undoVisible { undoId = consts.MENU_ID_UNDO //model.CefMis.NextCommandId() model.AddMenuItem(&MenuItem{ CommandId: undoId, Text: i18n.Resource("undo"), Accelerator: "ctrl+z", Callback: func(browser *ICefBrowser, commandId consts.MenuId, params *ICefContextMenuParams, menuType consts.TCefContextMenuType, eventFlags uint32, result *bool) { browser.GetFocusedFrame().Undo() }, }) model.SetEnabled(undoId, undoEnabled) } if redoVisible { redoId = consts.MENU_ID_REDO //model.CefMis.NextCommandId() model.AddMenuItem(&MenuItem{ CommandId: redoId, Text: i18n.Resource("redo"), Accelerator: "ctrl+shift+z", Callback: func(browser *ICefBrowser, commandId consts.MenuId, params *ICefContextMenuParams, menuType consts.TCefContextMenuType, eventFlags uint32, result *bool) { browser.GetFocusedFrame().Redo() }, }) model.SetEnabled(redoId, redoEnabled) } if undoVisible && redoVisible { model.AddSeparator() } if cutVisible { cutId = consts.MENU_ID_CUT //model.CefMis.NextCommandId() model.AddMenuItem(&MenuItem{ CommandId: cutId, Text: i18n.Resource("cut"), Accelerator: "ctrl+x", Callback: func(browser *ICefBrowser, commandId consts.MenuId, params *ICefContextMenuParams, menuType consts.TCefContextMenuType, eventFlags uint32, result *bool) { browser.GetFocusedFrame().Cut() }, }) model.SetEnabled(cutId, cutEnabled) } if copyVisible { copyId = consts.MENU_ID_COPY //model.CefMis.NextCommandId() model.AddMenuItem(&MenuItem{ CommandId: copyId, Text: i18n.Resource("copy"), Accelerator: "ctrl+c", Callback: func(browser *ICefBrowser, commandId consts.MenuId, params *ICefContextMenuParams, menuType consts.TCefContextMenuType, eventFlags uint32, result *bool) { browser.GetFocusedFrame().Copy() }, }) model.SetEnabled(copyId, copyEnabled) } if pasteVisible { pasteId = consts.MENU_ID_PASTE //model.CefMis.NextCommandId() model.AddMenuItem(&MenuItem{ CommandId: pasteId, Text: i18n.Resource("paste"), Accelerator: "ctrl+v", Callback: func(browser *ICefBrowser, commandId consts.MenuId, params *ICefContextMenuParams, menuType consts.TCefContextMenuType, eventFlags uint32, result *bool) { browser.GetFocusedFrame().Paste() }, }) model.SetEnabled(pasteId, pasteEnabled) } if selectAllVisible { selectAllId = consts.MENU_ID_SELECT_ALL //model.CefMis.NextCommandId() model.AddMenuItem(&MenuItem{ CommandId: selectAllId, Text: i18n.Resource("selectAll"), Accelerator: "ctrl+a", Callback: func(browser *ICefBrowser, commandId consts.MenuId, params *ICefContextMenuParams, menuType consts.TCefContextMenuType, eventFlags uint32, result *bool) { browser.GetFocusedFrame().SelectAll() }, }) model.SetEnabled(pasteId, selectAllEnabled) } if cutVisible && copyVisible && pasteVisible && selectAllVisible { model.AddSeparator() } //A标签和图片 链接 if params.TypeFlags() == 5 && params.MediaType() == consts.CM_MEDIATYPE_NONE { //a=5 aUrlId = model.CefMis.NextCommandId() model.AddItem(aUrlId, i18n.Resource("copyLink")) } if params.TypeFlags() == 9 && params.MediaType() == consts.CM_MEDIATYPE_IMAGE { // image=9 //copyImageId = model.CefMis.NextCommandId() //model.AddItem(copyImageId, "复制图片") imageUrlId = model.CefMis.NextCommandId() model.AddItem(imageUrlId, i18n.Resource("copyImageLink")) imageSaveId = model.CefMis.NextCommandId() model.AddItem(imageSaveId, i18n.Resource("imageSaveAs")) } if (params.TypeFlags() == 5 && params.MediaType() == consts.CM_MEDIATYPE_NONE) || params.TypeFlags() == 9 && params.MediaType() == consts.CM_MEDIATYPE_IMAGE { model.AddSeparator() } backId = model.CefMis.NextCommandId() model.AddMenuItem(&MenuItem{ CommandId: backId, Text: i18n.Resource("back"), Accelerator: "alt+" + string(rune(37)), Callback: func(browser *ICefBrowser, commandId consts.MenuId, params *ICefContextMenuParams, menuType consts.TCefContextMenuType, eventFlags uint32, result *bool) { if browser.CanGoBack() { browser.GoBack() } }, }) forwardId = model.CefMis.NextCommandId() model.AddMenuItem(&MenuItem{ CommandId: forwardId, Text: i18n.Resource("forward"), Accelerator: "alt+" + string(rune(39)), Callback: func(browser *ICefBrowser, commandId consts.MenuId, params *ICefContextMenuParams, menuType consts.TCefContextMenuType, eventFlags uint32, result *bool) { if browser.CanGoForward() { browser.GoForward() } }, }) model.AddSeparator() printId = model.CefMis.NextCommandId() model.AddMenuItem(&MenuItem{ CommandId: printId, Text: i18n.Resource("print"), Accelerator: "ctrl+p", Callback: func(browser *ICefBrowser, commandId consts.MenuId, params *ICefContextMenuParams, menuType consts.TCefContextMenuType, eventFlags uint32, result *bool) { browser.Print() }, }) model.AddSeparator() closeBrowserId = model.CefMis.NextCommandId() model.AddItem(closeBrowserId, i18n.Resource("closeBrowser")) model.AddSeparator() refreshId = model.CefMis.NextCommandId() model.AddMenuItem(&MenuItem{ CommandId: refreshId, Text: i18n.Resource("refresh"), Accelerator: "ctrl+r", Callback: func(browser *ICefBrowser, commandId consts.MenuId, params *ICefContextMenuParams, menuType consts.TCefContextMenuType, eventFlags uint32, result *bool) { browser.Reload() }, }) forcedRefreshId = model.CefMis.NextCommandId() model.AddMenuItem(&MenuItem{ CommandId: forcedRefreshId, Text: i18n.Resource("forcedRefresh"), Accelerator: "shift+ctrl+r", Callback: func(browser *ICefBrowser, commandId consts.MenuId, params *ICefContextMenuParams, menuType consts.TCefContextMenuType, eventFlags uint32, result *bool) { browser.ReloadIgnoreCache() }, }) model.AddSeparator() if BrowserWindow.Config.ChromiumConfig().EnableViewSource() { viewSourceId = model.CefMis.NextCommandId() model.AddMenuItem(&MenuItem{ CommandId: viewSourceId, Text: i18n.Resource("viewPageSource"), Accelerator: "ctrl+u", Callback: func(browser *ICefBrowser, commandId consts.MenuId, params *ICefContextMenuParams, menuType consts.TCefContextMenuType, eventFlags uint32, result *bool) { browser.ViewSource() }, }) } if BrowserWindow.Config.ChromiumConfig().EnableDevTools() { devToolsId = model.CefMis.NextCommandId() model.AddItem(devToolsId, i18n.Resource("devTools")) } if browser.CanGoBack() { model.SetEnabled(backId, true) } else { model.SetEnabled(backId, false) } if browser.CanGoForward() { model.SetEnabled(forwardId, true) } else { model.SetEnabled(forwardId, false) } } // chromiumOnContextMenuCommand 右键菜单 - 默认实现 func chromiumOnContextMenuCommand(sender lcl.IObject, browser *ICefBrowser, frame *ICefFrame, params *ICefContextMenuParams, commandId consts.MenuId, eventFlags uint32, result *bool) { browserId := browser.Identifier() defer func() { if err := recover(); err != nil { logger.Error("OnContextMenuCommand Error:", err, "browserId:", browserId) } }() *result = true if commandId == backId { browser.GoBack() } else if commandId == forwardId { browser.GoForward() } else if commandId == printId { browser.Print() } else if commandId == closeBrowserId { winInfo := BrowserWindow.GetWindowInfo(browserId) winInfo.CloseBrowserWindow() } else if commandId == refreshId { browser.Reload() } else if commandId == forcedRefreshId { browser.ReloadIgnoreCache() } else if commandId == viewSourceId { if BrowserWindow.Config.ChromiumConfig().EnableViewSource() { browser.ViewSource() } } else if commandId == devToolsId { if BrowserWindow.Config.ChromiumConfig().EnableDevTools() { browser.ShowDevTools() } } else if commandId == aUrlId { lcl.Clipboard.SetAsText(params.LinkUrl()) } else if commandId == copyImageId { frame.Copy() } else if commandId == imageUrlId { lcl.Clipboard.SetAsText(params.SourceUrl()) } else if commandId == imageSaveId { browser.StartDownload(params.SourceUrl()) } *result = true } // chromiumOnBeforePopup 弹出窗口 func chromiumOnBeforePopup(sender lcl.IObject, browser *ICefBrowser, frame *ICefFrame, beforePopupInfo *BeforePopupInfo, client *ICefClient, settings *TCefBrowserSettings, extraInfo *ICefDictionaryValue, noJavascriptAccess *bool, result *bool) { }