<commit_before>package main

import (
	"errors"
	"fmt"
	"github.com/nvsoft/cef"
	"github.com/nvsoft/goapp/config"
	"github.com/nvsoft/win"
	"os"
	"syscall"
	"unsafe"
)

const (
	ICON_MAIN       = 100
	nguiWindowClass = `\o/ NGui_Window_Class \o/`
)

var (
	hInstance win.HINSTANCE
	manifest  config.Manifest
)

var wndProc = syscall.NewCallback(WndProc)
var browserSettings = cef.BrowserSettings{}

func init() {
	hInstance := win.GetModuleHandle(nil)
	if hInstance == 0 {
		panic("GetModuleHandle")
	}

	MustRegisterWindowClass(nguiWindowClass)

	manifest.Load()
}

func main() {
	cef.ExecuteProcess(unsafe.Pointer(hInstance))

	settings := cef.Settings{}
	//settings.SingleProcess = 1                     // 单进程模式
	settings.CachePath = manifest.CachePath()      // Set to empty to disable
	settings.LogSeverity = cef.LOGSEVERITY_DEFAULT // LOGSEVERITY_VERBOSE
	//settings.LocalesDirPath = releasePath + "/locales"
	settings.Locale = manifest.Locale() //"zh-CN"
	settings.BrowserSubprocessPath = manifest.BrowserSubprocessPath()
	//settings.RemoteDebuggingPort = 7000
	cef.Initialize(settings)

    renderWindow := createMainWindow()
    //renderWindow1 := createMainWindow()

	go func() {
		createBrowser(renderWindow, manifest.FirstPage()) // http://www.baidu.com/
        //createBrowser(renderWindow1, "http://www.sohu.com/") // http://www.baidu.com/
	}()

	//go func() {
	//	working()
	//}()

	cef.RunMessageLoop()
	cef.Shutdown()
	os.Exit(0)
}

func createMainWindow() win.HWND {
	var dwExStyle, dwStyle uint32 = 0, 0

	dwStyle = win.WS_OVERLAPPEDWINDOW

	if !manifest.FormFixed() {
		dwStyle |= win.WS_SIZEBOX
	}

	// 获取屏幕宽度和高度
	var x, y int32
	var width, height int32

	width = manifest.Width()
	height = manifest.Height()
	x = (win.GetSystemMetrics(win.SM_CXSCREEN) - width) / 2
	y = (win.GetSystemMetrics(win.SM_CYSCREEN)-height)/2 - 2

	renderWindow := win.CreateWindowEx(
		dwExStyle,
		syscall.StringToUTF16Ptr(nguiWindowClass),
		nil,
		dwStyle, //|win.WS_CLIPSIBLINGS,
		x,       //win.CW_USEDEFAULT,
		y,       //win.CW_USEDEFAULT,
		width,   //win.CW_USEDEFAULT,
		height,  //win.CW_USEDEFAULT,
		0,       //hwndParent
		0,
		0, //hInstance
		nil)
	if renderWindow == 0 {
		//err := errors.New("CreateWindowEx")
		return win.HWND(0)
	}

	win.MoveWindow(renderWindow, x, y, width, height, false)

	fmt.Printf("CreateWindow x=%v, y=%v, width=%v, height=%v\n", x, y, width, height)

	return renderWindow
}

func createBrowser(renderWindow win.HWND, url string) {
	//browser := cef.CreateBrowser(unsafe.Pointer(hwnd), &browserSettings, url, false)
	//browserSettings := cef.BrowserSettings{}
	cef.CreateBrowser(unsafe.Pointer(renderWindow), &browserSettings, url, false)

	//m_dwStyle = WS_CHILD | WS_CLIPCHILDREN | WS_CLIPSIBLINGS | WS_TABSTOP |
	//		WS_VISIBLE;
	cef.WindowResized(unsafe.Pointer(renderWindow))

	win.ShowWindow(renderWindow, win.SW_SHOW) //win.SW_SHOW
	win.UpdateWindow(renderWindow)

	//cef.WindowResized(unsafe.Pointer(renderWindow))
	// It should be enough to call WindowResized after 10ms,
	// though to be sure let's extend it to 100ms.
	//time.AfterFunc(time.Millisecond*100, func() {
	//	cef.WindowResized(unsafe.Pointer(renderWindow))
	//})
}

func WndProc(hwnd win.HWND, msg uint32, wParam, lParam uintptr) (result uintptr) {
	switch msg {
	case win.WM_CREATE:
		result = win.DefWindowProc(hwnd, msg, wParam, lParam)
	case win.WM_SIZE:
		// 最小化时不能调整Cef窗体，否则恢复时界面一片空白
		if wParam == win.SIZE_RESTORED || wParam == win.SIZE_MAXIMIZED {
			cef.WindowResized(unsafe.Pointer(hwnd))
		}<commit_msg>修改创建主窗口方式<commit_after>package main

import (
	"errors"
	"fmt"
	"github.com/nvsoft/cef"
	"github.com/nvsoft/goapp/config"
	"github.com/nvsoft/win"
	"os"
	"syscall"
	"unsafe"
    //. "time"
    "runtime"
)

const (
	ICON_MAIN       = 100
	nguiWindowClass = `\o/ Goapp_Window_Class \o/`
)

var (
	hInstance win.HINSTANCE
	manifest  config.Manifest
)

var wndProc = syscall.NewCallback(WndProc)
var browserSettings = cef.BrowserSettings{}
var winHandlers map[unsafe.Pointer]win.HWND

func init() {
	hInstance := win.GetModuleHandle(nil)
	if hInstance == 0 {
		panic("GetModuleHandle")
	}

	MustRegisterWindowClass(nguiWindowClass)

	manifest.Load()
}

func main() {
    runtime.GOMAXPROCS(4)
    cef.ExecuteProcess(unsafe.Pointer(hInstance))

    cef.SetupCreateRootWindowCallback(createRootWindow)

	settings := cef.Settings{}
	//settings.SingleProcess = 1                     // 单进程模式
	settings.CachePath = manifest.CachePath()      // Set to empty to disable
	settings.LogSeverity = cef.LOGSEVERITY_DEFAULT // LOGSEVERITY_VERBOSE
	//settings.LocalesDirPath = releasePath + "/locales"
	settings.Locale = manifest.Locale() //"zh-CN"
	settings.BrowserSubprocessPath = manifest.BrowserSubprocessPath()
	//settings.RemoteDebuggingPort = 7000
	cef.Initialize(settings)

    //renderWindow := createMainWindow()
    //renderWindow1 := createMainWindow()
    //renderWindow1 := createMainWindow()

	go func() {
        //createMainBrowser()
        //renderWindow := createMainWindow()
        //createBrowser(renderWindow, manifest.FirstPage())
        //createBrowser(renderWindow1, "http://www.sohu.com/") // http://www.baidu.com/
	}()

    /*go func() {
        time.Sleep(5 * time.Second)
        go func() {
            createBrowser(renderWindow1, "http://www.sohu.com/")
        }()
    }()*/

	//go func() {
	//	working()
	//}()

	cef.RunMessageLoop()
	cef.Shutdown()
	os.Exit(0)
}

func createRootWindow() {
    renderWindow := createMainWindow()
    createBrowser(renderWindow, manifest.FirstPage()) // http://www.baidu.com/
}

func createMainWindow() win.HWND {
	var dwExStyle, dwStyle uint32 = 0, 0

	dwStyle = win.WS_OVERLAPPEDWINDOW

	if !manifest.FormFixed() {
		dwStyle |= win.WS_SIZEBOX
	}

	// 获取屏幕宽度和高度
	var x, y int32
	var width, height int32

	width = manifest.Width()
	height = manifest.Height()
	x = (win.GetSystemMetrics(win.SM_CXSCREEN) - width) / 2
	y = (win.GetSystemMetrics(win.SM_CYSCREEN)-height)/2 - 2

	renderWindow := win.CreateWindowEx(
		dwExStyle,
		syscall.StringToUTF16Ptr(nguiWindowClass),
		nil,
		dwStyle, //|win.WS_CLIPSIBLINGS,
		x,       //win.CW_USEDEFAULT,
		y,       //win.CW_USEDEFAULT,
		width,   //win.CW_USEDEFAULT,
		height,  //win.CW_USEDEFAULT,
		0,       //hwndParent
		0,
		0, //hInstance
		nil)
	if renderWindow == 0 {
		//err := errors.New("CreateWindowEx")
		return win.HWND(0)
	}

	win.MoveWindow(renderWindow, x, y, width, height, false)

	fmt.Printf("CreateWindow x=%v, y=%v, width=%v, height=%v, renderWindow=%v\n", x, y, width, height, renderWindow)

    //winHandlers[unsafe.Pointer(renderWindow)] = renderWindow

	return renderWindow
}

func createBrowser(renderWindow win.HWND, url string) {
    //winHandlers[unsafe.Pointer(renderWindow)] = renderWindow
	//browser := cef.CreateBrowser(unsafe.Pointer(hwnd), &browserSettings, url, false)
	//browserSettings := cef.BrowserSettings{}
	cef.CreateBrowser(unsafe.Pointer(renderWindow), &browserSettings, url, false)

	//m_dwStyle = WS_CHILD | WS_CLIPCHILDREN | WS_CLIPSIBLINGS | WS_TABSTOP |
	//		WS_VISIBLE;
	cef.WindowResized(unsafe.Pointer(renderWindow))

	win.ShowWindow(renderWindow, win.SW_SHOW) //win.SW_SHOW
	win.UpdateWindow(renderWindow)

	//cef.WindowResized(unsafe.Pointer(renderWindow))
	// It should be enough to call WindowResized after 10ms,
	// though to be sure let's extend it to 100ms.
	//time.AfterFunc(time.Millisecond*100, func() {
	//	cef.WindowResized(unsafe.Pointer(renderWindow))
	//})
}

func WndProc(hwnd win.HWND, msg uint32, wParam, lParam uintptr) (result uintptr) {
	switch msg {
	case win.WM_CREATE:
		result = win.DefWindowProc(hwnd, msg, wParam, lParam)
	case win.WM_SIZE:
		// 最小化时不能调整Cef窗体，否则恢复时界面一片空白
		if wParam == win.SIZE_RESTORED || wParam == win.SIZE_MAXIMIZED {
			cef.WindowResized(unsafe.Pointer(hwnd))
		}
{'fraction_non_alphanumeric': 0.13948415244450146, 'fraction_numerical': 0.010778904144745284, 'mean_word_length': 3.811111111111111, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 7, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3043680', 'n_tokens_mistral': 3044, 'n_tokens_neox': 2901, 'n_words': 518}