types-focus-handler.go 2.7 KB
Newer Older
yanghye's avatar
yanghye 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
//----------------------------------------
//
// Copyright © yanghy. All Rights Reserved.
//
// Licensed under Apache License Version 2.0, January 2004
//
// https://www.apache.org/licenses/LICENSE-2.0
//
//----------------------------------------

package cef

import (
yanghye's avatar
yanghye 已提交
14 15
	"github.com/energye/energy/v2/common/imports"
	"github.com/energye/energy/v2/consts"
yanghye's avatar
yanghye 已提交
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
	"github.com/energye/golcl/lcl"
	"github.com/energye/golcl/lcl/api"
	"unsafe"
)

// ************************** creates ************************** //

// FocusHandlerRef -> ICefFocusHandler
var FocusHandlerRef focusHandler

type focusHandler uintptr

func (*focusHandler) New() *ICefFocusHandler {
	var result uintptr
	imports.Proc(internale_CefFocusHandlerRef_Create).Call(uintptr(unsafe.Pointer(&result)))
	if result != 0 {
		return &ICefFocusHandler{instance: unsafe.Pointer(result)}
	}
	return nil
}

// ************************** impl ************************** //

// Instance 实例
func (m *ICefFocusHandler) Instance() uintptr {
	if m == nil {
		return 0
	}
	return uintptr(m.instance)
}

func (m *ICefFocusHandler) Free() {
	if m.instance != nil {
		m.base.Free(m.Instance())
		m.instance = nil
	}
}

func (m *ICefFocusHandler) IsValid() bool {
	if m == nil || m.instance == nil {
		return false
	}
	return m.instance != nil
}

func (m *ICefFocusHandler) SetOnTakeFocus(fn onTakeFocus) {
	if !m.IsValid() {
		return
	}
	imports.Proc(internale_CefFocusHandler_OnTakeFocus).Call(m.Instance(), api.MakeEventDataPtr(fn))
}

func (m *ICefFocusHandler) SetOnSetFocus(fn onSetFocus) {
	if !m.IsValid() {
		return
	}
	imports.Proc(internale_CefFocusHandler_OnSetFocus).Call(m.Instance(), api.MakeEventDataPtr(fn))
}

func (m *ICefFocusHandler) SetOnGotFocus(fn onGotFocus) {
	if !m.IsValid() {
		return
	}
	imports.Proc(internale_CefFocusHandler_OnGotFocus).Call(m.Instance(), api.MakeEventDataPtr(fn))
}

// ************************** events ************************** //

type onTakeFocus func(browser *ICefBrowser, next bool)
type onSetFocus func(browser *ICefBrowser, source consts.TCefFocusSource) bool
type onGotFocus func(browser *ICefBrowser)

func init() {
	lcl.RegisterExtEventCallback(func(fn interface{}, getVal func(idx int) uintptr) bool {
		getPtr := func(i int) unsafe.Pointer {
			return unsafe.Pointer(getVal(i))
		}
		switch fn.(type) {
		case onTakeFocus:
			browse := &ICefBrowser{instance: getPtr(0)}
			next := api.GoBool(getVal(1))
			fn.(onTakeFocus)(browse, next)
		case onSetFocus:
			browse := &ICefBrowser{instance: getPtr(0)}
			source := consts.TCefFocusSource(getVal(1))
			result := (*bool)(getPtr(2))
			*result = fn.(onSetFocus)(browse, source)
		case onGotFocus:
			browse := &ICefBrowser{instance: getPtr(0)}
			fn.(onGotFocus)(browse)
		default:
			return false
		}
		return true
	})
}