tageditor_windows.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. //go:build windows
  2. // ============================================================================
  3. // tageditor_windows.go — 标签编辑输入窗口(难点2)
  4. //
  5. // 右键菜单点「标签(&T)」后弹出的小窗口:输入「标签」和「备注」两栏,
  6. // 确定时写入标签数据库(SaveTag),取消/关闭则不写。
  7. //
  8. // 实现:仿 mouseAlertLoop 的窗口线程模式(runtime.LockOSThread + RegisterClassExW
  9. // + CreateWindowExW + GetMessage 消息循环 + 自有 WndProc)。不用 Wails 前端模态
  10. // 是因为右键场景前台是 explorer,切到 Wails 窗口体验割裂且需新增前端 UI+事件通道。
  11. // ============================================================================
  12. package main
  13. import (
  14. "fmt"
  15. "os"
  16. "runtime"
  17. "sync"
  18. "syscall"
  19. "unsafe"
  20. )
  21. // 标签编辑窗口控件 ID
  22. const (
  23. tagEditID = 1001 // 标签 EDIT
  24. noteEditID = 1002 // 备注 EDIT(多行)
  25. btnOKID = 1003 // 确定
  26. btnCancelID = 1004 // 取消
  27. tagEditorWidth = 400 // 逻辑宽度(px @96DPI),创建时按 DPI 缩放
  28. tagEditorHeight = 260 // 逻辑高度(px @96DPI),创建时按 DPI 缩放
  29. )
  30. // 标准控件样式
  31. const (
  32. wsChild = 0x40000000
  33. wsVisible = 0x10000000
  34. wsTabStop = 0x00010000
  35. wsGroup = 0x00020000
  36. wsBorder = 0x00800000
  37. wsCaption = 0x00C00000
  38. wsSysMenu = 0x00080000
  39. wsThickFrame = 0x00040000
  40. wsOverlapped = 0x00000000
  41. wsExDlgModal = 0x00000001
  42. wsExTopMost = 0x00000008
  43. wsExAppWindow = 0x00040000
  44. esMultiLine = 0x0004
  45. esAutoHScroll = 0x0080
  46. esAutoVScroll = 0x0040
  47. bsDefPushButton = 0x0001
  48. )
  49. // 本文件用到的 user32 过程(procCreateWindowEx/procGetMessage/procRegisterClassEx/
  50. // procDefWindowProc/procDestroyWindow/procShowWindow/procSetForegroundWindow/
  51. // procGetSystemMetrics/procGetModuleHandleW 等已在其它文件定义,此处不复用)。
  52. var (
  53. procGetWindowTextW = user32.NewProc("GetWindowTextW")
  54. procSetWindowTextW = user32.NewProc("SetWindowTextW")
  55. procGetDlgItem = user32.NewProc("GetDlgItem")
  56. procGetWindowTextLengthW = user32.NewProc("GetWindowTextLengthW")
  57. procSetFocus = user32.NewProc("SetFocus")
  58. procPostQuitMessage = user32.NewProc("PostQuitMessage")
  59. procGetClientRect = user32.NewProc("GetClientRect")
  60. )
  61. // tagEditor 标签编辑窗口状态(单实例,包级)。
  62. var tagEditor struct {
  63. mu sync.Mutex
  64. hwnd uintptr
  65. path string
  66. cb uintptr // WndProc 回调地址(保持引用防 GC)
  67. classOK bool
  68. started bool
  69. }
  70. // openTagEditor 弹出标签编辑窗口(由右键菜单回调异步调用)。
  71. // 会启动一个 LockOSThread 的窗口线程。path 为规范化前的完整路径(显示用原始,
  72. // 写入库时由 SaveTag 再规范化)。
  73. func openTagEditor(path string) {
  74. if path == "" {
  75. return
  76. }
  77. tagEditor.mu.Lock()
  78. if tagEditor.started {
  79. // 已有一个编辑窗口:把新路径切过去(复用窗口,避免多实例)。
  80. hwnd := tagEditor.hwnd
  81. tagEditor.path = path
  82. tagEditor.mu.Unlock()
  83. if hwnd != 0 {
  84. procSetForegroundWindow.Call(hwnd)
  85. }
  86. return
  87. }
  88. tagEditor.started = true
  89. tagEditor.path = path
  90. tagEditor.mu.Unlock()
  91. go tagEditorLoop()
  92. }
  93. // tagEditorLoop 标签编辑窗口线程。
  94. func tagEditorLoop() {
  95. runtime.LockOSThread()
  96. defer runtime.UnlockOSThread()
  97. hInst, _, _ := procGetModuleHandleW.Call(0)
  98. className, _ := syscall.UTF16PtrFromString("TengTuTagEditor")
  99. // 注册窗口类(WndProc 是 Go 回调,须保持地址)。
  100. tagEditor.mu.Lock()
  101. tagEditor.cb = syscall.NewCallback(tagEditorWndProc)
  102. wc := wndclassex{
  103. CbSize: uint32(unsafe.Sizeof(wndclassex{})),
  104. LpfnWndProc: tagEditor.cb,
  105. HInstance: hInst,
  106. LpszClassName: className,
  107. HCursor: 0,
  108. }
  109. atom, _, _ := procRegisterClassEx.Call(uintptr(unsafe.Pointer(&wc)))
  110. if atom == 0 {
  111. tagEditor.classOK = false
  112. tagEditor.mu.Unlock()
  113. fmt.Fprintf(os.Stderr, "[tageditor] RegisterClassEx failed\n")
  114. return
  115. }
  116. tagEditor.classOK = true
  117. tagEditor.mu.Unlock()
  118. // 按 DPI 缩放窗口尺寸:manifest permonitorv2 下窗口按物理像素创建,
  119. // 高 DPI 屏幕(125%/150%/200%)上 380x220 物理像素显得过小(用户反馈
  120. // 「窗口小,看不到按钮/输入框」)。这里把逻辑尺寸乘上缩放因子。
  121. scale := dpiScale()
  122. winW := scaleInt(tagEditorWidth, scale)
  123. winH := scaleInt(tagEditorHeight, scale)
  124. // 屏幕中心定位
  125. sw, _, _ := procGetSystemMetrics.Call(0) // SM_CXSCREEN
  126. sh, _, _ := procGetSystemMetrics.Call(1) // SM_CYSCREEN
  127. x := int(sw)/2 - winW/2
  128. y := int(sh)/2 - winH/2
  129. // 创建主窗口(带标题栏、系统菜单;模态于 explorer 之上)
  130. hwnd, _, _ := procCreateWindowEx.Call(
  131. uintptr(wsExTopMost|wsExAppWindow), // 置顶 + 应用窗口
  132. uintptr(unsafe.Pointer(className)),
  133. strPtr("添加标签 / 备注"),
  134. uintptr(wsOverlapped|wsCaption|wsSysMenu|wsThickFrame),
  135. uintptr(x), uintptr(y),
  136. uintptr(winW), uintptr(winH),
  137. 0, 0, hInst, 0,
  138. )
  139. if hwnd == 0 {
  140. fmt.Fprintf(os.Stderr, "[tageditor] CreateWindowEx failed\n")
  141. tagEditor.mu.Lock()
  142. tagEditor.started = false
  143. tagEditor.mu.Unlock()
  144. return
  145. }
  146. tagEditor.mu.Lock()
  147. tagEditor.hwnd = hwnd
  148. tagEditor.mu.Unlock()
  149. // 创建控件:标签标签+输入框、备注标签+多行输入框、确定/取消按钮
  150. createTagEditorControls(hwnd, hInst)
  151. procShowWindow.Call(hwnd, 1) // SW_SHOWNORMAL
  152. procSetForegroundWindow.Call(hwnd)
  153. // procGetDlgItem.Call 返回 (hwnd, r2, err);Go 不允许对多返回值直接下标,
  154. // 先取第一值(控件句柄)再 SetFocus。
  155. tagEditCtrl, _, _ := procGetDlgItem.Call(hwnd, tagEditID)
  156. procSetFocus.Call(tagEditCtrl)
  157. // 消息循环(直到 WM_QUIT)
  158. var m msg
  159. for {
  160. ret, _, _ := procGetMessage.Call(uintptr(unsafe.Pointer(&m)), 0, 0, 0)
  161. if ret == 0 {
  162. break
  163. }
  164. procTranslateMessage.Call(uintptr(unsafe.Pointer(&m)))
  165. procDispatchMessage.Call(uintptr(unsafe.Pointer(&m)))
  166. }
  167. // 清理
  168. if hwnd != 0 {
  169. procDestroyWindow.Call(hwnd)
  170. }
  171. tagEditor.mu.Lock()
  172. tagEditor.hwnd = 0
  173. tagEditor.started = false
  174. tagEditor.mu.Unlock()
  175. }
  176. // createTagEditorControls 创建标签编辑窗口内的控件。
  177. func createTagEditorControls(hwnd, hInst uintptr) {
  178. // mk 创建静态标签文本控件(class="STATIC",text 为显示文字)。
  179. // procCreateWindowEx.Call 返回 (hwnd, r2, err),此处只关心句柄,故取 [0]。
  180. // 坐标/尺寸用 int(DPI 缩放后计算),内部转 uintptr。
  181. mk := func(text string, x, y, w, h int) {
  182. ts, _ := syscall.UTF16PtrFromString(text)
  183. procCreateWindowEx.Call(0,
  184. uintptr(unsafe.Pointer(utf16("STATIC"))),
  185. uintptr(unsafe.Pointer(ts)),
  186. uintptr(wsChild|wsVisible|wsGroup),
  187. uintptr(x), uintptr(y), uintptr(w), uintptr(h), hwnd, 0, hInst, 0)
  188. }
  189. // 按 DPI 缩放控件尺寸与坐标(与窗口同比例),否则高 DPI 屏上输入框/按钮
  190. // 仍按物理像素创建而显得过小、且放不进被放大的窗口。
  191. scale := dpiScale()
  192. pad := scaleInt(12, scale)
  193. labelH := scaleInt(18, scale)
  194. inputH := scaleInt(26, scale)
  195. btnW := scaleInt(80, scale)
  196. btnH := scaleInt(30, scale)
  197. gap := scaleInt(12, scale) // 行间距
  198. labelX := pad
  199. labelW := scaleInt(64, scale)
  200. inputX := pad + labelW
  201. noteH := scaleInt(80, scale) // 备注输入框高度(多行)
  202. // 客户区实际尺寸:外层窗口含标题栏+边框,CreateWindowEx 传入的是外层宽高,
  203. // 若按外层高度布局按钮会超出客户区被裁切(用户反馈「按钮/输入框显示不全」)。
  204. // 这里用 GetClientRect 取真实客户区,按钮贴客户区底边、备注框贴按钮上方。
  205. var cr rect
  206. procGetClientRect.Call(hwnd, uintptr(unsafe.Pointer(&cr)))
  207. clientW := int(cr.Right - cr.Left)
  208. clientH := int(cr.Bottom - cr.Top)
  209. if clientW <= 0 || clientH <= 0 { // 兜底:取外层尺寸(几乎不会发生)
  210. clientW = scaleInt(tagEditorWidth, scale)
  211. clientH = scaleInt(tagEditorHeight, scale)
  212. }
  213. btnY := clientH - pad - btnH // 按钮底边贴客户区底边(留 pad)
  214. editW := clientW - inputX - pad // 输入框右缘贴客户区右边(留 pad)
  215. noteY := pad + inputH + gap // 备注框在标签输入框下方 gap
  216. // 备注框高度:按钮上方到备注框顶部之间全部利用(若剩余空间不足则用 noteH)
  217. if btnY-noteY-gap < noteH {
  218. noteH = btnY - noteY - gap
  219. if noteH < scaleInt(40, scale) {
  220. noteH = scaleInt(40, scale) // 保底 40px,避免极端情况挤压
  221. }
  222. }
  223. // 标签
  224. mk("标签:", labelX, pad, labelW, labelH)
  225. procCreateWindowEx.Call(0,
  226. uintptr(unsafe.Pointer(utf16("EDIT"))),
  227. 0,
  228. uintptr(wsChild|wsVisible|wsTabStop|wsBorder),
  229. uintptr(inputX), uintptr(pad-scaleInt(1, scale)), uintptr(editW), uintptr(inputH), hwnd,
  230. tagEditID, hInst, 0)
  231. // 备注
  232. mk("备注:", labelX, noteY, labelW, labelH)
  233. procCreateWindowEx.Call(0,
  234. uintptr(unsafe.Pointer(utf16("EDIT"))),
  235. 0,
  236. uintptr(wsChild|wsVisible|wsTabStop|wsBorder|esMultiLine|esAutoVScroll|esAutoHScroll),
  237. uintptr(inputX), uintptr(noteY-scaleInt(1, scale)), uintptr(editW), uintptr(noteH), hwnd,
  238. noteEditID, hInst, 0)
  239. // 按钮:取消(左)、确定(右),底边贴客户区底边
  240. procCreateWindowEx.Call(0,
  241. uintptr(unsafe.Pointer(utf16("BUTTON"))),
  242. uintptr(unsafe.Pointer(utf16("取消"))),
  243. uintptr(wsChild|wsVisible|wsTabStop),
  244. uintptr(clientW-btnW*2-pad*2), uintptr(btnY), uintptr(btnW), uintptr(btnH), hwnd,
  245. btnCancelID, hInst, 0)
  246. procCreateWindowEx.Call(0,
  247. uintptr(unsafe.Pointer(utf16("BUTTON"))),
  248. uintptr(unsafe.Pointer(utf16("确定"))),
  249. uintptr(wsChild|wsVisible|wsTabStop|bsDefPushButton),
  250. uintptr(clientW-btnW-pad), uintptr(btnY), uintptr(btnW), uintptr(btnH), hwnd,
  251. btnOKID, hInst, 0)
  252. }
  253. // utf16 快捷转 UTF16 指针(临时,须立即使用)。
  254. func utf16(s string) *uint16 {
  255. p, _ := syscall.UTF16PtrFromString(s)
  256. return p
  257. }
  258. // tagEditorWndProc 标签编辑窗口消息处理。
  259. func tagEditorWndProc(hwnd uintptr, msgID uint32, wParam uintptr, lParam uintptr) uintptr {
  260. switch msgID {
  261. case 0x0111: // WM_COMMAND:控件通知
  262. id := uint32(wParam & 0xffff)
  263. switch id {
  264. case btnOKID:
  265. // 读控件文本 → 写库 → 关闭
  266. tagEditor.mu.Lock()
  267. path := tagEditor.path
  268. tagEditor.mu.Unlock()
  269. tagText := getControlText(hwnd, tagEditID)
  270. noteText := getControlText(hwnd, noteEditID)
  271. if err := getTagSvc().SaveTag(path, tagText, noteText); err != nil {
  272. logTagErr("SaveTag", err)
  273. // 写库失败:提示并保持窗口打开(先取确定按钮句柄)
  274. okBtn, _, _ := procGetDlgItem.Call(hwnd, btnOKID)
  275. procSetWindowTextW.Call(okBtn, uintptr(unsafe.Pointer(utf16("确定(失败)"))))
  276. } else {
  277. procPostQuitMessage.Call(0)
  278. }
  279. case btnCancelID:
  280. procPostQuitMessage.Call(0)
  281. }
  282. case 0x0010: // WM_CLOSE:用户点标题栏关闭 → 视为取消
  283. procPostQuitMessage.Call(0)
  284. case 0x0002: // WM_DESTROY
  285. procPostQuitMessage.Call(0)
  286. }
  287. // msgID 是 uint32,转 uintptr 传入 DefWindowProc;
  288. // procDefWindowProc.Call 返回 (ret, r2, err),只需取第一值。
  289. ret, _, _ := procDefWindowProc.Call(hwnd, uintptr(msgID), wParam, lParam)
  290. return ret
  291. }
  292. // getControlText 读取 EDIT 控件文本。
  293. func getControlText(parent, id uintptr) string {
  294. edit, _, _ := procGetDlgItem.Call(parent, id)
  295. if edit == 0 {
  296. return ""
  297. }
  298. // 先查长度,再读
  299. n, _, _ := procGetWindowTextLengthW.Call(edit)
  300. if n == 0 {
  301. return ""
  302. }
  303. buf := make([]uint16, n+1)
  304. procGetWindowTextW.Call(edit, uintptr(unsafe.Pointer(&buf[0])), n+1)
  305. return syscall.UTF16ToString(buf)
  306. }