ticker.go 3.1 KB

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