1
0

ticker.go 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // ============================================================================
  2. //go:build !service
  3. // ticker.go — 滚动条窗口
  4. //
  5. // 股票条 / 新闻条:无边框、透明、置顶、可拖动的屏幕顶部一行窗口;
  6. // 位置拖动后延迟保存,重启恢复。
  7. // ============================================================================
  8. package main
  9. import (
  10. "github.com/wailsapp/wails/v3/pkg/application"
  11. )
  12. // 滚动条窗口(股票条 / 新闻条):无边框、透明、置顶、可拖动的一行窗口
  13. var (
  14. stockTickerWin *application.WebviewWindow // 股票滚动条窗口
  15. newsTickerWin *application.WebviewWindow // 新闻滚动条窗口
  16. mobileMode bool // 移动端(窄屏)模式:禁止创建滚动条窗口
  17. )
  18. // SetMobileMode 由前端在窗口尺寸变化时调用:移动端隐藏主机 Tab 并禁止滚动条
  19. func SetMobileMode(mobile bool) {
  20. mobileMode = mobile
  21. syncTickerWindows(loadSettings()) // 立即按新模式刷新滚动条
  22. }
  23. // syncTickerWindows 按配置创建 / 关闭 / 刷新两个滚动条窗口
  24. func syncTickerWindows(settings AppSettings) {
  25. stockTickerWin = syncTickerWindow("stock", settings.StockTicker, stockTickerWin)
  26. newsTickerWin = syncTickerWindow("news", settings.NewsTicker, newsTickerWin)
  27. }
  28. // syncTickerWindow 同步单个滚动条窗口状态:
  29. // 关闭 → 已开启则创建;配置变化 → 重载页面(ticker.js 启动时读取最新配置);已开启 → 关闭
  30. func syncTickerWindow(kind string, cfg TickerConfig, win *application.WebviewWindow) *application.WebviewWindow {
  31. if mobileMode { // 移动端禁止滚动文本:已开则关闭,不创建
  32. if win != nil {
  33. win.Close()
  34. }
  35. return nil
  36. }
  37. if !cfg.Enabled {
  38. if win != nil {
  39. win.Close() // 配置关闭时销毁窗口
  40. }
  41. return nil
  42. }
  43. if win != nil {
  44. width := cfg.Width
  45. if width < 200 {
  46. width = 1400 // 未配置时用默认宽度
  47. }
  48. win.SetSize(width, cfg.FontSize+18) // 宽度 / 字号变化时同步窗口尺寸
  49. win.Reload() // 让页面重新读取颜色 / 速度等配置
  50. return win
  51. }
  52. x, y := cfg.X, cfg.Y
  53. if x < 0 || y < 0 {
  54. x, y = 100, 0 // 首次显示默认放在屏幕顶部
  55. }
  56. width := cfg.Width
  57. if width < 200 {
  58. width = 1400 // 未配置时用默认宽度
  59. }
  60. return app.Window.NewWithOptions(application.WebviewWindowOptions{
  61. Name: "ticker-" + kind,
  62. Title: "腾图滚动条",
  63. Width: width,
  64. Height: cfg.FontSize + 18, // 一行文字高度
  65. X: x,
  66. Y: y,
  67. AlwaysOnTop: true, // 置顶显示
  68. DisableResize: true, // 固定尺寸
  69. Frameless: true, // 无边框
  70. BackgroundType: application.BackgroundTypeTransparent, // 透明背景
  71. URL: "/ticker.html?kind=" + kind,
  72. DevToolsEnabled: false,
  73. DefaultContextMenuDisabled: true,
  74. Windows: application.WindowsWindow{
  75. HiddenOnTaskbar: true, // 不占任务栏
  76. },
  77. })
  78. }