v2.3.57 Code authorization description

This commit is contained in:
杨红岩 2023-01-25 14:59:28 +08:00
parent 1f16023645
commit 5958a26ce3
7 changed files with 330 additions and 85 deletions

View File

@ -8,57 +8,182 @@
package cef
import "energye/systray"
type MenuItemClick func()
type SysMenu struct {
Label string
Items []*SysMenuItem
label string
items []*SysMenuItem
}
type SysMenuItem struct {
ChildMenu *SysMenu
Label string
Action MenuItemClick
IsSeparator bool
Disabled bool
Checked bool
Icon []byte
Shortcut string
childMenu *SysMenu
menuItem *systray.MenuItem
label string
click MenuItemClick
isSeparator bool
disabled bool
checked bool
icon []byte
shortcut string
}
func (m *SysMenu) AddMenuItem(label string, action MenuItemClick) *SysMenuItem {
item := &SysMenuItem{
Label: label,
Action: action,
label: label,
click: action,
}
m.Items = append(m.Items, item)
m.items = append(m.items, item)
return item
}
func (m *SysMenu) Add(menuItem *SysMenuItem) {
m.Items = append(m.Items, menuItem)
m.items = append(m.items, menuItem)
}
func (m *SysMenu) AddMenuItemSeparator() {
m.Items = append(m.Items, &SysMenuItem{IsSeparator: true})
m.items = append(m.items, &SysMenuItem{isSeparator: true})
}
func (m *SysMenuItem) Add(label string, items ...*SysMenuItem) *SysMenu {
m.ChildMenu = &SysMenu{Label: label, Items: items}
return m.ChildMenu
func (m *SysMenuItem) AddSubMenu(label string, action MenuItemClick) *SysMenuItem {
if m.childMenu == nil {
m.childMenu = &SysMenu{
items: make([]*SysMenuItem, 0, 0),
}
}
return m.childMenu.AddMenuItem(label, action)
}
// NewMenu 创建一个新菜单,给定指定的标签和要显示的项目列表
func NewMenu(label string, items ...*SysMenuItem) *SysMenu {
return &SysMenu{Label: label, Items: items}
func (m *SysMenuItem) SetIcon(v []byte) {
m.icon = v
if m.menuItem != nil {
m.menuItem.SetIcon(v)
}
}
// NewMenuItem 根据传递的标签和操作参数创建一个新菜单项
func NewMenuItem(label string, action MenuItemClick) *SysMenuItem {
return &SysMenuItem{Label: label, Action: action}
func (m *SysMenuItem) Icon() []byte {
return m.icon
}
// NewMenuItemSeparator 创建将用作分隔符的菜单项
func NewMenuItemSeparator() *SysMenuItem {
return &SysMenuItem{IsSeparator: true}
func (m *SysMenuItem) SetChecked(v bool) {
m.checked = v
if m.menuItem != nil {
if v {
m.menuItem.Checked()
} else {
m.menuItem.Uncheck()
}
}
}
func (m *SysMenuItem) Checked() bool {
if m.menuItem != nil {
m.checked = m.menuItem.Checked()
}
return m.checked
}
func (m *SysMenuItem) Check() {
if m.menuItem != nil {
m.menuItem.Check()
}
}
func (m *SysMenuItem) Uncheck() {
if m.menuItem != nil {
m.menuItem.Uncheck()
}
}
func (m *SysMenuItem) SetDisable(v bool) {
m.disabled = v
if m.menuItem != nil {
m.menuItem.Disable()
}
}
func (m *SysMenuItem) Disable() {
if m.menuItem != nil {
m.menuItem.Disable()
}
}
func (m *SysMenuItem) Enable() {
if m.menuItem != nil {
m.menuItem.Enable()
}
}
func (m *SysMenuItem) Disabled() bool {
if m.menuItem != nil {
m.disabled = m.menuItem.Disabled()
}
return m.disabled
}
func (m *SysMenuItem) Show() {
if m.menuItem != nil {
m.menuItem.Show()
}
}
func (m *SysMenuItem) Hide() {
if m.menuItem != nil {
m.menuItem.Hide()
}
}
func (m *SysMenuItem) SetSeparator(v bool) {
m.isSeparator = v
}
func (m *SysMenuItem) IsSeparator() bool {
return m.isSeparator
}
func (m *SysMenuItem) SetLabel(v string) {
m.label = v
if m.menuItem != nil {
m.menuItem.SetTitle(v)
}
}
func (m *SysMenuItem) SetTooltip(v string) {
if m.menuItem != nil {
m.menuItem.SetTooltip(v)
}
}
func (m *SysMenuItem) Label() string {
return m.label
}
func itemForMenuItem(item *SysMenuItem, parent *systray.MenuItem) *systray.MenuItem {
if item == nil || item.isSeparator {
systray.AddSeparator()
return nil
}
var mItem *systray.MenuItem
if item.checked {
if parent != nil {
mItem = parent.AddSubMenuItemCheckbox(item.label, item.label, true)
} else {
mItem = systray.AddMenuItemCheckbox(item.label, item.label, true)
}
} else {
if parent != nil {
mItem = parent.AddSubMenuItem(item.label, item.label)
} else {
mItem = systray.AddMenuItem(item.label, item.label)
}
}
if item.disabled {
mItem.Disable()
}
if item.icon != nil {
mItem.SetIcon(item.icon)
}
item.menuItem = mItem
return mItem
}

View File

@ -9,6 +9,7 @@
package cef
import (
"energye/systray"
"github.com/energye/golcl/lcl"
"github.com/energye/golcl/lcl/types"
)
@ -81,6 +82,11 @@ type CEFTray struct {
//SysTray 系统原生
type SysTray struct {
menu *SysMenu
trayStart, trayStop func()
menu *SysMenu
icon []byte
title, tooltip string
click TrayICONClick
dClick TrayICONClick
rClick func(menu systray.IMenu)
start, stop func()
}

View File

@ -17,11 +17,56 @@ import (
//创建系统托盘
func newSysTray() *SysTray {
return &SysTray{
tray := &SysTray{
menu: &SysMenu{
Items: make([]*SysMenuItem, 0, 0),
items: make([]*SysMenuItem, 0, 0),
},
}
return tray
}
func (m *SysTray) onReady() {
if len(m.icon) > 0 {
systray.SetIcon(m.icon)
}
if m.click != nil {
systray.SetOnClick(m.click)
}
if m.dClick != nil {
systray.SetOnDClick(m.dClick)
}
if m.rClick != nil {
systray.SetOnRClick(m.rClick)
}
if m.title != "" {
systray.SetTitle(m.title)
}
if m.tooltip != "" {
systray.SetTooltip(m.tooltip)
}
systray.ResetMenu()
m.refreshSystray(m.menu.items, nil)
go func() {
for {
select {}
}
}()
}
func (m *SysTray) refreshSystray(items []*SysMenuItem, parent *systray.MenuItem) {
for _, item := range items {
mItem := itemForMenuItem(item, parent)
if mItem == nil {
continue
}
if item.childMenu != nil && len(item.childMenu.items) > 0 {
m.refreshSystray(item.childMenu.items, mItem)
}
}
}
func (m *SysTray) onExit() {
systray.Quit()
}
func (m *SysTray) AsSysTray() *SysTray {
@ -41,36 +86,119 @@ func (m *SysTray) AsLCLTray() *LCLTray {
}
func (m *SysTray) Show() {
if m.start == nil {
m.start, m.stop = systray.RunWithExternalLoop(m.onReady, m.onExit)
m.start()
}
}
func (m *SysTray) Hide() {
}
func (m *SysTray) close() {
m.onExit()
}
// CreateMenu 创建托盘菜单, 如果托盘菜单是空, 把菜单项添加到托盘
// 该方法主动调用后 如果托盘菜单已创建则添加进去, 之后鼠标事件失效
//
// 仅MacOSX平台
func (m *SysTray) CreateMenu() {
if m.start == nil {
systray.CreateMenu()
}
}
// SetMenuNil 托盘菜单设置为nil, 如果托盘菜单不是空, 把菜单项设置为nil
// 该方法主动调用后 将移除托盘菜单, 之后鼠标事件生效
//
// 仅MacOSX平台
func (m *SysTray) SetMenuNil() {
if m.start == nil {
systray.SetMenuNil()
}
}
//ResetMenu 重置托盘菜单
func (m *SysTray) ResetMenu() {
if m.start == nil {
systray.ResetMenu()
}
}
//Add 添加一个菜单项
func (m *SysTray) Add(menuItem *SysMenuItem) {
m.menu.Add(menuItem)
}
//AddMenuItem 添加一个菜单项
func (m *SysTray) AddMenuItem(label string, onClick MenuItemClick) *SysMenuItem {
return m.menu.AddMenuItem(label, onClick)
}
//AddMenuItemSeparator 添加一个分隔线
func (m *SysTray) AddMenuItemSeparator() {
m.menu.AddMenuItemSeparator()
}
//NewMenuItem 创建一个新菜单项
func (m *SysTray) NewMenuItem(label string, onClick MenuItemClick) *SysMenuItem {
return NewMenuItem(label, onClick)
return &SysMenuItem{label: label, click: onClick}
}
//SetOnDblClick 鼠标双击事件
func (m *SysTray) SetOnDblClick(fn TrayICONClick) {
systray.SetOnDClick(fn)
m.dClick = fn
}
//SetOnClick 鼠标单击事件
func (m *SysTray) SetOnClick(fn TrayICONClick) {
systray.SetOnClick(fn)
m.click = fn
}
//SetOnRClick 鼠标右键
func (m *SysTray) SetOnRClick(fn func(menu systray.IMenu)) {
systray.SetOnRClick(fn)
m.rClick = fn
}
func (m *SysTray) SetHint(value string) {
m.tooltip = value
if m.start != nil {
systray.SetTooltip(value)
}
}
func (m *SysTray) SetTitle(title string) {
m.title = title
if m.start != nil {
systray.SetTitle(title)
}
}
//SetIconFS 设置托盘图标
func (m *SysTray) SetIconFS(iconResourcePath string) {
if emfs.IsExist(iconResourcePath) {
data, err := emfs.GetResources(iconResourcePath)
if err == nil {
m.icon = data
if m.start != nil {
systray.SetIcon(m.icon)
}
}
}
}
//SetIcon 设置托盘图标
func (m *SysTray) SetIcon(iconResourcePath string) {
if tools.IsExist(iconResourcePath) {
data, err := ioutil.ReadFile(iconResourcePath)
if err == nil {
m.icon = data
if m.start != nil {
systray.SetIcon(m.icon)
}
}
}
}
func (m *SysTray) Visible() bool {
@ -80,14 +208,6 @@ func (m *SysTray) Visible() bool {
func (m *SysTray) SetVisible(v bool) {
}
func (m *SysTray) SetHint(value string) {
systray.SetTooltip(value)
}
func (m *SysTray) SetTitle(title string) {
systray.SetTitle(title)
}
//设置托盘气泡
//title 气泡标题
//content 气泡内容
@ -99,23 +219,3 @@ func (m *SysTray) SetBalloon(title, content string, timeout int32) ITray {
//显示托盘气泡
func (m *SysTray) ShowBalloon() {
}
//设置托盘图标
func (m *SysTray) SetIconFS(iconResourcePath string) {
if emfs.IsExist(iconResourcePath) {
file, err := emfs.GetResources(iconResourcePath)
if err == nil {
systray.SetTemplateIcon(file, file)
}
}
}
//设置托盘图标
func (m *SysTray) SetIcon(iconResourcePath string) {
if tools.IsExist(iconResourcePath) {
file, err := ioutil.ReadFile(iconResourcePath)
if err == nil {
systray.SetTemplateIcon(file, file)
}
}
}

View File

@ -14,7 +14,6 @@ import (
"github.com/energye/energy/cef"
"github.com/energye/energy/common"
"github.com/energye/energy/consts"
sys_tray "github.com/energye/energy/example/dev-test/sys-tray"
"github.com/energye/energy/ipc"
"github.com/energye/golcl/lcl"
"github.com/energye/golcl/lcl/types"
@ -404,10 +403,6 @@ func AppBrowserInit() {
})
//添加子窗口初始化
cef.BrowserWindow.SetBrowserInitAfter(func(browserWindow cef.IBrowserWindow) {
sysTray(browserWindow)
//tray(browserWindow)
sys_tray.TrayMain()
return
//在这里创建 一些子窗口 子组件 等
//托盘
if common.IsWindows() {
@ -418,11 +413,6 @@ func AppBrowserInit() {
})
}
func sysTray(browserWindow cef.IBrowserWindow) {
sysTray := browserWindow.NewSysTray()
fmt.Printf("NewSysTray %+v\n: ", sysTray)
}
// 托盘 只适用 windows 的系统托盘, 基于html 和 ipc 实现功能
func cefTray(browserWindow cef.IBrowserWindow) {
window := browserWindow.AsLCLBrowserWindow().BrowserWindow()

View File

@ -14,6 +14,7 @@ import (
"github.com/energye/energy/cef"
"github.com/energye/energy/common/assetserve"
"github.com/energye/golcl/lcl"
"time"
)
//go:embed resources
@ -70,19 +71,10 @@ func main() {
//}()
})
cef.BrowserWindow.SetBrowserInitAfter(func(window cef.IBrowserWindow) {
sysTray(window)
//tray(browserWindow)
//sys_tray.TrayMain()
//browserWindow := window.AsLCLBrowserWindow()
//parent := browserWindow.WindowParent()
//parent.SetAlign(types.AlNone)
//parent.SetAnchors(types.NewSet(types.AkTop, types.AkLeft, types.AkBottom, types.AkRight))
//parent.SetTop(100)
//parent.SetLeft(100)
//parent.SetWidth(600)
//parent.SetHeight(600)
//browserWindow.BrowserWindow().SetTransparentColor()
//dwWinStyle := win.GetWindowLong(window.AsLCLBrowserWindow().Handle(), win.GWL_STYLE)
//dwWinStyle |= win.WS_THICKFRAME
//win.SetWindowLong(window.AsLCLBrowserWindow().Handle(), win.GWL_STYLE, uintptr(dwWinStyle))
return
})
cef.SetBrowserProcessStartAfterCallback(func(b bool) {
fmt.Println("主进程启动 创建一个内置http服务")
@ -96,3 +88,35 @@ func main() {
//运行应用
cef.Run(cefApp)
}
func sysTray(browserWindow cef.IBrowserWindow) {
sysTray := browserWindow.NewSysTray()
sysTray.SetIconFS("resources/icon.ico")
sysTray.SetHint("中文hint\n换行中文")
sysTray.SetOnClick(func() {
fmt.Println("SetOnClick")
})
tray := sysTray.AsSysTray()
tray.AddMenuItem("1级菜单1", nil)
tray.AddMenuItemSeparator()
item := tray.AddMenuItem("1级菜单2", nil)
item.AddSubMenu("2级子菜单1", nil)
sub2Menu := item.AddSubMenu("2级子菜单2", nil)
sub2Menu.AddSubMenu("3级子菜单1", nil)
sysTray.Show()
go func() {
var b bool
for {
time.Sleep(time.Second)
b = !b
if b {
sysTray.SetHint(fmt.Sprintf("%d\n%v", time.Now().Second(), b))
sysTray.SetIconFS("resources/icon_1.ico")
} else {
sysTray.SetHint(fmt.Sprintf("%d\n%v", time.Now().Second(), b))
sysTray.SetIconFS("resources/icon.ico")
}
}
}()
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 132 KiB