feature.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package main
  2. import (
  3. "context"
  4. "github.com/getlantern/systray"
  5. )
  6. // Feature is a self-contained tray capability. It owns its menu items,
  7. // background loops, and mutable state. Adding a feature means implementing
  8. // this interface and registering it in app.go's feature list — the app's
  9. // event loop itself never changes.
  10. type Feature interface {
  11. // Name identifies the feature (ordering and logging).
  12. Name() string
  13. // Install creates this feature's tray menu items. It is called exactly
  14. // once, from the systray onReady goroutine, before Run.
  15. Install(m *Menu)
  16. // Run starts the feature's background work. It returns when the feature
  17. // shuts down: either Stop is called, or ctx is cancelled at app exit.
  18. Run(ctx context.Context)
  19. // Stop requests shutdown and releases resources (goroutines, child
  20. // processes). It must be safe to call multiple times and before Run.
  21. Stop()
  22. }
  23. // Menu is a thin wrapper over the systray package so features never import
  24. // systray directly. It centralizes menu construction behind one seam.
  25. type Menu struct{}
  26. // Item is a handle to one tray menu entry.
  27. type Item struct {
  28. mi *systray.MenuItem
  29. Clicked chan struct{}
  30. }
  31. // AddItem creates a clickable menu entry.
  32. func (Menu) AddItem(title, tooltip string) *Item {
  33. mi := systray.AddMenuItem(title, tooltip)
  34. return &Item{mi: mi, Clicked: mi.ClickedCh}
  35. }
  36. // AddSeparator inserts a menu divider.
  37. func (Menu) AddSeparator() { systray.AddSeparator() }
  38. // SetTitle updates the item's visible label.
  39. func (it *Item) SetTitle(t string) { it.mi.SetTitle(t) }
  40. // SetTooltip updates the item's hover text.
  41. func (it *Item) SetTooltip(t string) { it.mi.SetTooltip(t) }
  42. // Disable greys the item out.
  43. func (it *Item) Disable() { it.mi.Disable() }
  44. // Enable makes a disabled item clickable again.
  45. func (it *Item) Enable() { it.mi.Enable() }
  46. // setTrayTitle / setTrayTooltip / quitTray are package-level systray shims so
  47. // features can update the tray icon state without importing systray directly.
  48. func setTrayTitle(t string) { systray.SetTitle(t) }
  49. func setTrayTooltip(t string) { systray.SetTooltip(t) }
  50. func quitTray() { systray.Quit() }