github.com/AlpineAIO/wails/v2@v2.0.0-beta.32.0.20240505041856-1047a8fa5fef/internal/frontend/desktop/windows/winc/app.go (about)

     1  //go:build windows
     2  
     3  /*
     4   * Copyright (C) 2019 The Winc Authors. All Rights Reserved.
     5   * Copyright (C) 2010-2013 Allen Dang. All Rights Reserved.
     6   */
     7  package winc
     8  
     9  import (
    10  	"runtime"
    11  	"time"
    12  	"unsafe"
    13  
    14  	"github.com/AlpineAIO/wails/v2/internal/frontend/desktop/windows/winc/w32"
    15  )
    16  
    17  var (
    18  	// resource compilation tool assigns app.ico ID of 3
    19  	// rsrc -manifest app.manifest -ico app.ico -o rsrc.syso
    20  	AppIconID = 3
    21  )
    22  
    23  func init() {
    24  	runtime.LockOSThread()
    25  
    26  	gAppInstance = w32.GetModuleHandle("")
    27  	if gAppInstance == 0 {
    28  		panic("Error occurred in App.Init")
    29  	}
    30  
    31  	// Initialize the common controls
    32  	var initCtrls w32.INITCOMMONCONTROLSEX
    33  	initCtrls.DwSize = uint32(unsafe.Sizeof(initCtrls))
    34  	initCtrls.DwICC =
    35  		w32.ICC_LISTVIEW_CLASSES | w32.ICC_PROGRESS_CLASS | w32.ICC_TAB_CLASSES |
    36  			w32.ICC_TREEVIEW_CLASSES | w32.ICC_BAR_CLASSES
    37  
    38  	w32.InitCommonControlsEx(&initCtrls)
    39  }
    40  
    41  // SetAppIconID sets recource icon ID for the apps windows.
    42  func SetAppIcon(appIconID int) {
    43  	AppIconID = appIconID
    44  }
    45  
    46  func GetAppInstance() w32.HINSTANCE {
    47  	return gAppInstance
    48  }
    49  
    50  func PreTranslateMessage(msg *w32.MSG) bool {
    51  	// This functions is called by the MessageLoop. It processes the
    52  	// keyboard accelerator keys and calls Controller.PreTranslateMessage for
    53  	// keyboard and mouse events.
    54  
    55  	processed := false
    56  
    57  	if (msg.Message >= w32.WM_KEYFIRST && msg.Message <= w32.WM_KEYLAST) ||
    58  		(msg.Message >= w32.WM_MOUSEFIRST && msg.Message <= w32.WM_MOUSELAST) {
    59  
    60  		if msg.Hwnd != 0 {
    61  			if controller := GetMsgHandler(msg.Hwnd); controller != nil {
    62  				// Search the chain of parents for pretranslated messages.
    63  				for p := controller; p != nil; p = p.Parent() {
    64  
    65  					if processed = p.PreTranslateMessage(msg); processed {
    66  						break
    67  					}
    68  				}
    69  			}
    70  		}
    71  	}
    72  
    73  	return processed
    74  }
    75  
    76  // RunMainLoop processes messages in main application loop.
    77  func RunMainLoop() int {
    78  	m := (*w32.MSG)(unsafe.Pointer(w32.GlobalAlloc(0, uint32(unsafe.Sizeof(w32.MSG{})))))
    79  	defer w32.GlobalFree(w32.HGLOBAL(unsafe.Pointer(m)))
    80  
    81  	for w32.GetMessage(m, 0, 0, 0) != 0 {
    82  
    83  		if !PreTranslateMessage(m) {
    84  			w32.TranslateMessage(m)
    85  			w32.DispatchMessage(m)
    86  		}
    87  
    88  		time.Sleep(15 * time.Millisecond)
    89  	}
    90  
    91  	w32.GdiplusShutdown()
    92  	return int(m.WParam)
    93  }
    94  
    95  // PostMessages processes recent messages. Sometimes helpful for instant window refresh.
    96  func PostMessages() {
    97  	m := (*w32.MSG)(unsafe.Pointer(w32.GlobalAlloc(0, uint32(unsafe.Sizeof(w32.MSG{})))))
    98  	defer w32.GlobalFree(w32.HGLOBAL(unsafe.Pointer(m)))
    99  
   100  	for i := 0; i < 10; i++ {
   101  		if w32.GetMessage(m, 0, 0, 0) != 0 {
   102  			if !PreTranslateMessage(m) {
   103  				w32.TranslateMessage(m)
   104  				w32.DispatchMessage(m)
   105  			}
   106  		}
   107  	}
   108  }
   109  
   110  func Exit() {
   111  	w32.PostQuitMessage(0)
   112  }