energy/cef/chromium-callback.go

331 lines
12 KiB
Go
Raw Normal View History

2022-10-04 13:21:05 +08:00
//----------------------------------------
//
// Copyright © yanghy. All Rights Reserved.
//
// Licensed under Apache License Version 2.0, January 2004
//
// https://www.apache.org/licenses/LICENSE-2.0
2022-10-04 13:21:05 +08:00
//
//----------------------------------------
2023-03-01 18:01:34 +08:00
// chromium event 默认事件实现
2023-05-31 17:41:14 +08:00
2022-10-04 13:21:05 +08:00
package cef
import (
2023-05-31 18:00:34 +08:00
"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"
2022-10-04 13:21:05 +08:00
"github.com/energye/golcl/lcl"
"github.com/energye/golcl/lcl/rtl"
2023-05-31 17:41:14 +08:00
"github.com/energye/golcl/lcl/types"
"github.com/energye/golcl/lcl/types/messages"
2022-10-04 13:21:05 +08:00
)
2023-02-20 14:42:17 +08:00
// chromiumOnAfterCreate 事件处理函数返回true将不继续执行
2022-10-04 13:21:05 +08:00
func chromiumOnAfterCreate(browser *ICefBrowser) bool {
2023-03-03 12:52:00 +08:00
if common.IsWindows() {
rtl.SendMessage(browser.HostWindowHandle(), messages.WM_SETICON, 1, lcl.Application.Icon().Handle())
}
2023-05-31 17:41:14 +08:00
// 浏览器创建完之后
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()
}
}
2022-10-04 13:21:05 +08:00
return false
}
2023-02-20 14:42:17 +08:00
// chromiumOnBeforeBrowser
2022-10-04 13:21:05 +08:00
func chromiumOnBeforeBrowser(browser *ICefBrowser, frame *ICefFrame) {
2023-05-31 17:41:14 +08:00
if window.CurrentBrowseWindowCache != nil {
bw := window.CurrentBrowseWindowCache.(IBrowserWindow)
if bw.Id() != browser.Identifier() {
return
}
window.CurrentBrowseWindowCache = nil
// 辅助工具不具有浏览器窗口特性
2023-05-31 17:41:14 +08:00
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 {
2022-10-04 13:21:05 +08:00
return
}
2023-05-31 17:41:14 +08:00
BrowserWindow.putWindowInfo(browser.BrowserId(), bw)
// 只LCL窗口使用自定义的窗口拖拽
if bw.IsLCL() {
dragExtensionJS(frame, bw.WindowProperty().EnableWebkitAppRegion) // drag extension
}
2022-10-04 13:21:05 +08:00
}
// 当前应用是LCL窗口预先创建下一个window
if !application.IsMessageLoop() {
QueueAsyncCall(func(id int) {
2023-05-31 17:41:14 +08:00
// lcl
BrowserWindow.createNextLCLPopupWindow()
})
}
2022-10-04 13:21:05 +08:00
}
2023-03-01 18:01:34 +08:00
// chromiumOnBeforeClose - chromium 关闭之前
2022-10-04 13:21:05 +08:00
func chromiumOnBeforeClose(browser *ICefBrowser) {
2023-05-31 17:41:14 +08:00
BrowserWindow.removeWindowInfo(browser.Identifier())
2022-10-04 13:21:05 +08:00
}
var (
2023-03-03 12:52:00 +08:00
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
2022-10-04 13:21:05 +08:00
)
2023-02-20 14:42:17 +08:00
// chromiumOnBeforeContextMenu 右键菜单 - 默认实现
func chromiumOnBeforeContextMenu(window IBrowserWindow, browser *ICefBrowser, frame *ICefFrame, params *ICefContextMenuParams, model *ICefMenuModel) {
if !window.Chromium().Config().EnableMenu() {
2022-10-04 13:21:05 +08:00
model.Clear()
return
}
//开发者工具和显示源代码不展示自定义默认菜单
if window.WindowType() == consts.WT_DEV_TOOLS || window.WindowType() == consts.WT_VIEW_SOURCE {
return
2022-10-04 13:21:05 +08:00
}
2023-03-03 12:52:00 +08:00
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)
2022-10-04 13:21:05 +08:00
model.Clear()
if undoVisible {
2023-03-03 12:52:00 +08:00
undoId = consts.MENU_ID_UNDO //model.CefMis.NextCommandId()
2022-10-04 13:21:05 +08:00
model.AddMenuItem(&MenuItem{
CommandId: undoId,
2023-05-31 17:41:14 +08:00
Text: i18n.Resource("undo"),
2022-10-04 13:21:05 +08:00
Accelerator: "ctrl+z",
2023-03-03 12:52:00 +08:00
Callback: func(browser *ICefBrowser, commandId consts.MenuId, params *ICefContextMenuParams, menuType consts.TCefContextMenuType, eventFlags uint32, result *bool) {
2022-10-19 18:19:17 +08:00
browser.GetFocusedFrame().Undo()
2022-10-04 13:21:05 +08:00
},
})
model.SetEnabled(undoId, undoEnabled)
}
if redoVisible {
2023-03-03 12:52:00 +08:00
redoId = consts.MENU_ID_REDO //model.CefMis.NextCommandId()
2022-10-04 13:21:05 +08:00
model.AddMenuItem(&MenuItem{
CommandId: redoId,
2023-05-31 17:41:14 +08:00
Text: i18n.Resource("redo"),
2022-10-04 13:21:05 +08:00
Accelerator: "ctrl+shift+z",
2023-03-03 12:52:00 +08:00
Callback: func(browser *ICefBrowser, commandId consts.MenuId, params *ICefContextMenuParams, menuType consts.TCefContextMenuType, eventFlags uint32, result *bool) {
2022-10-19 18:19:17 +08:00
browser.GetFocusedFrame().Redo()
2022-10-04 13:21:05 +08:00
},
})
model.SetEnabled(redoId, redoEnabled)
}
if undoVisible && redoVisible {
model.AddSeparator()
}
if cutVisible {
2023-03-03 12:52:00 +08:00
cutId = consts.MENU_ID_CUT //model.CefMis.NextCommandId()
2022-10-04 13:21:05 +08:00
model.AddMenuItem(&MenuItem{
CommandId: cutId,
2023-05-31 17:41:14 +08:00
Text: i18n.Resource("cut"),
2022-10-04 13:21:05 +08:00
Accelerator: "ctrl+x",
2023-03-03 12:52:00 +08:00
Callback: func(browser *ICefBrowser, commandId consts.MenuId, params *ICefContextMenuParams, menuType consts.TCefContextMenuType, eventFlags uint32, result *bool) {
2022-10-19 18:19:17 +08:00
browser.GetFocusedFrame().Cut()
2022-10-04 13:21:05 +08:00
},
})
model.SetEnabled(cutId, cutEnabled)
}
if copyVisible {
2023-03-03 12:52:00 +08:00
copyId = consts.MENU_ID_COPY //model.CefMis.NextCommandId()
2022-10-04 13:21:05 +08:00
model.AddMenuItem(&MenuItem{
CommandId: copyId,
2023-05-31 17:41:14 +08:00
Text: i18n.Resource("copy"),
2022-10-04 13:21:05 +08:00
Accelerator: "ctrl+c",
2023-03-03 12:52:00 +08:00
Callback: func(browser *ICefBrowser, commandId consts.MenuId, params *ICefContextMenuParams, menuType consts.TCefContextMenuType, eventFlags uint32, result *bool) {
2022-10-19 18:19:17 +08:00
browser.GetFocusedFrame().Copy()
2022-10-04 13:21:05 +08:00
},
})
model.SetEnabled(copyId, copyEnabled)
}
if pasteVisible {
2023-03-03 12:52:00 +08:00
pasteId = consts.MENU_ID_PASTE //model.CefMis.NextCommandId()
2022-10-04 13:21:05 +08:00
model.AddMenuItem(&MenuItem{
CommandId: pasteId,
2023-05-31 17:41:14 +08:00
Text: i18n.Resource("paste"),
2022-10-04 13:21:05 +08:00
Accelerator: "ctrl+v",
2023-03-03 12:52:00 +08:00
Callback: func(browser *ICefBrowser, commandId consts.MenuId, params *ICefContextMenuParams, menuType consts.TCefContextMenuType, eventFlags uint32, result *bool) {
2022-10-19 18:19:17 +08:00
browser.GetFocusedFrame().Paste()
2022-10-04 13:21:05 +08:00
},
})
model.SetEnabled(pasteId, pasteEnabled)
}
if selectAllVisible {
2023-03-03 12:52:00 +08:00
selectAllId = consts.MENU_ID_SELECT_ALL //model.CefMis.NextCommandId()
2022-10-04 13:21:05 +08:00
model.AddMenuItem(&MenuItem{
CommandId: selectAllId,
2023-05-31 17:41:14 +08:00
Text: i18n.Resource("selectAll"),
2022-10-04 13:21:05 +08:00
Accelerator: "ctrl+a",
2023-03-03 12:52:00 +08:00
Callback: func(browser *ICefBrowser, commandId consts.MenuId, params *ICefContextMenuParams, menuType consts.TCefContextMenuType, eventFlags uint32, result *bool) {
2022-10-19 18:19:17 +08:00
browser.GetFocusedFrame().SelectAll()
2022-10-04 13:21:05 +08:00
},
})
model.SetEnabled(pasteId, selectAllEnabled)
}
if cutVisible && copyVisible && pasteVisible && selectAllVisible {
model.AddSeparator()
}
//A标签和图片 链接
2023-05-31 17:41:14 +08:00
if params.TypeFlags() == 5 && params.MediaType() == consts.CM_MEDIATYPE_NONE { //a=5
2022-10-04 13:21:05 +08:00
aUrlId = model.CefMis.NextCommandId()
2023-05-31 17:41:14 +08:00
model.AddItem(aUrlId, i18n.Resource("copyLink"))
2022-10-04 13:21:05 +08:00
}
2023-05-31 17:41:14 +08:00
if params.TypeFlags() == 9 && params.MediaType() == consts.CM_MEDIATYPE_IMAGE { // image=9
2022-10-04 13:21:05 +08:00
//copyImageId = model.CefMis.NextCommandId()
//model.AddItem(copyImageId, "复制图片")
imageUrlId = model.CefMis.NextCommandId()
2023-05-31 17:41:14 +08:00
model.AddItem(imageUrlId, i18n.Resource("copyImageLink"))
2022-10-04 13:21:05 +08:00
imageSaveId = model.CefMis.NextCommandId()
2023-05-31 17:41:14 +08:00
model.AddItem(imageSaveId, i18n.Resource("imageSaveAs"))
2022-10-04 13:21:05 +08:00
}
2023-05-31 17:41:14 +08:00
if (params.TypeFlags() == 5 && params.MediaType() == consts.CM_MEDIATYPE_NONE) || params.TypeFlags() == 9 && params.MediaType() == consts.CM_MEDIATYPE_IMAGE {
2022-10-04 13:21:05 +08:00
model.AddSeparator()
}
backId = model.CefMis.NextCommandId()
model.AddMenuItem(&MenuItem{
CommandId: backId,
2023-05-31 17:41:14 +08:00
Text: i18n.Resource("back"),
2022-10-04 13:21:05 +08:00
Accelerator: "alt+" + string(rune(37)),
2023-03-03 12:52:00 +08:00
Callback: func(browser *ICefBrowser, commandId consts.MenuId, params *ICefContextMenuParams, menuType consts.TCefContextMenuType, eventFlags uint32, result *bool) {
2022-10-04 13:21:05 +08:00
if browser.CanGoBack() {
2023-02-26 19:14:49 +08:00
browser.GoBack()
2022-10-04 13:21:05 +08:00
}
},
})
forwardId = model.CefMis.NextCommandId()
model.AddMenuItem(&MenuItem{
CommandId: forwardId,
2023-05-31 17:41:14 +08:00
Text: i18n.Resource("forward"),
2022-10-04 13:21:05 +08:00
Accelerator: "alt+" + string(rune(39)),
2023-03-03 12:52:00 +08:00
Callback: func(browser *ICefBrowser, commandId consts.MenuId, params *ICefContextMenuParams, menuType consts.TCefContextMenuType, eventFlags uint32, result *bool) {
2022-10-04 13:21:05 +08:00
if browser.CanGoForward() {
2023-02-26 19:14:49 +08:00
browser.GoForward()
2022-10-04 13:21:05 +08:00
}
},
})
model.AddSeparator()
printId = model.CefMis.NextCommandId()
model.AddMenuItem(&MenuItem{
CommandId: printId,
2023-05-31 17:41:14 +08:00
Text: i18n.Resource("print"),
2022-10-04 13:21:05 +08:00
Accelerator: "ctrl+p",
2023-03-03 12:52:00 +08:00
Callback: func(browser *ICefBrowser, commandId consts.MenuId, params *ICefContextMenuParams, menuType consts.TCefContextMenuType, eventFlags uint32, result *bool) {
2022-10-04 13:21:05 +08:00
browser.Print()
},
})
model.AddSeparator()
closeBrowserId = model.CefMis.NextCommandId()
2023-05-31 17:41:14 +08:00
model.AddItem(closeBrowserId, i18n.Resource("closeBrowser"))
2022-10-04 13:21:05 +08:00
model.AddSeparator()
refreshId = model.CefMis.NextCommandId()
model.AddMenuItem(&MenuItem{
CommandId: refreshId,
2023-05-31 17:41:14 +08:00
Text: i18n.Resource("refresh"),
2022-10-04 13:21:05 +08:00
Accelerator: "ctrl+r",
2023-03-03 12:52:00 +08:00
Callback: func(browser *ICefBrowser, commandId consts.MenuId, params *ICefContextMenuParams, menuType consts.TCefContextMenuType, eventFlags uint32, result *bool) {
2022-10-04 13:21:05 +08:00
browser.Reload()
},
})
forcedRefreshId = model.CefMis.NextCommandId()
model.AddMenuItem(&MenuItem{
CommandId: forcedRefreshId,
2023-05-31 17:41:14 +08:00
Text: i18n.Resource("forcedRefresh"),
2022-10-04 13:21:05 +08:00
Accelerator: "shift+ctrl+r",
2023-03-03 12:52:00 +08:00
Callback: func(browser *ICefBrowser, commandId consts.MenuId, params *ICefContextMenuParams, menuType consts.TCefContextMenuType, eventFlags uint32, result *bool) {
2022-10-04 13:21:05 +08:00
browser.ReloadIgnoreCache()
},
})
model.AddSeparator()
if window.Chromium().Config().EnableViewSource() {
2022-10-04 13:21:05 +08:00
viewSourceId = model.CefMis.NextCommandId()
model.AddMenuItem(&MenuItem{
CommandId: viewSourceId,
2023-05-31 17:41:14 +08:00
Text: i18n.Resource("viewPageSource"),
2022-10-04 13:21:05 +08:00
Accelerator: "ctrl+u",
2023-03-03 12:52:00 +08:00
Callback: func(browser *ICefBrowser, commandId consts.MenuId, params *ICefContextMenuParams, menuType consts.TCefContextMenuType, eventFlags uint32, result *bool) {
2023-02-26 19:14:49 +08:00
browser.ViewSource()
2022-10-04 13:21:05 +08:00
},
})
}
if window.Chromium().Config().EnableDevTools() {
2022-10-04 13:21:05 +08:00
devToolsId = model.CefMis.NextCommandId()
2023-05-31 17:41:14 +08:00
model.AddItem(devToolsId, i18n.Resource("devTools"))
2022-10-04 13:21:05 +08:00
}
if browser.CanGoBack() {
model.SetEnabled(backId, true)
} else {
model.SetEnabled(backId, false)
}
if browser.CanGoForward() {
model.SetEnabled(forwardId, true)
} else {
model.SetEnabled(forwardId, false)
}
}
2023-02-20 14:42:17 +08:00
// chromiumOnContextMenuCommand 右键菜单 - 默认实现
func chromiumOnContextMenuCommand(window IBrowserWindow, browser *ICefBrowser, frame *ICefFrame, params *ICefContextMenuParams, commandId consts.MenuId, eventFlags uint32, result *bool) {
2023-05-31 17:41:14 +08:00
browserId := browser.Identifier()
defer func() {
if err := recover(); err != nil {
2023-05-31 17:41:14 +08:00
logger.Error("OnContextMenuCommand Error:", err, "browserId:", browserId)
}
}()
2022-10-04 13:21:05 +08:00
*result = true
if commandId == backId {
browser.GoBack()
} else if commandId == forwardId {
browser.GoForward()
} else if commandId == printId {
browser.Print()
} else if commandId == closeBrowserId {
2023-05-31 17:41:14 +08:00
winInfo := BrowserWindow.GetWindowInfo(browserId)
winInfo.CloseBrowserWindow()
2022-10-04 13:21:05 +08:00
} else if commandId == refreshId {
browser.Reload()
} else if commandId == forcedRefreshId {
browser.ReloadIgnoreCache()
} else if commandId == viewSourceId {
if window.Chromium().Config().EnableViewSource() {
2023-02-26 19:14:49 +08:00
browser.ViewSource()
2022-10-04 13:21:05 +08:00
}
} else if commandId == devToolsId {
if window.Chromium().Config().EnableDevTools() {
2022-10-04 13:21:05 +08:00
browser.ShowDevTools()
}
} else if commandId == aUrlId {
2023-05-31 17:41:14 +08:00
lcl.Clipboard.SetAsText(params.LinkUrl())
2022-10-04 13:21:05 +08:00
} else if commandId == copyImageId {
frame.Copy()
} else if commandId == imageUrlId {
2023-05-31 17:41:14 +08:00
lcl.Clipboard.SetAsText(params.SourceUrl())
2022-10-04 13:21:05 +08:00
} else if commandId == imageSaveId {
2023-05-31 17:41:14 +08:00
browser.StartDownload(params.SourceUrl())
2022-10-04 13:21:05 +08:00
}
*result = true
}
2023-05-31 17:41:14 +08:00
// chromiumOnBeforePopup 弹出窗口
func chromiumOnBeforePopup(sender lcl.IObject, browser *ICefBrowser, frame *ICefFrame, beforePopupInfo *BeforePopupInfo, client *ICefClient, settings *TCefBrowserSettings, extraInfo *ICefDictionaryValue, noJavascriptAccess *bool, result *bool) {
}