| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- package main
- import (
- "fmt"
- "strings"
- )
- // BrightnessConfig 显示器亮度配置:记住各显示器亮度(按显示器名称),供启动时恢复
- type BrightnessConfig struct {
- RestoreOnStart bool `json:"restoreOnStart"` // 启动应用时恢复上次亮度
- Levels map[string]int `json:"levels"` // 显示器名称 -> 亮度百分比(0-100)
- }
- // MonitorBrightnessInfo 单个显示器的亮度状态(返回给前端渲染滑块)
- type MonitorBrightnessInfo struct {
- Index int `json:"index"` // 显示器序号(与 SetBrightness 的 index 一致)
- Name string `json:"name"` // 显示器名称(物理监视器描述,取不到时用设备名)
- Percent int `json:"percent"` // 当前亮度百分比 0-100
- Hardware bool `json:"hardware"` // true=DDC/CI 硬件调光;false=Gamma 软件调光
- }
- // BrightnessService 显示器亮度控制:Windows 下优先 DDC/CI 硬件调光,
- // 显示器不支持时回退 Gamma 软件调光;其他平台不提供(Available 返回 false)。
- type BrightnessService struct{}
- // NewBrightnessService 创建亮度服务实例
- func NewBrightnessService() *BrightnessService { return &BrightnessService{} }
- // Available 当前平台是否支持亮度调节
- func (s *BrightnessService) Available() bool { return brightnessAvailable() }
- // GetMonitors 列出所有显示器及当前亮度;软件调光的显示器取设置里保存的亮度(默认 100)
- func (s *BrightnessService) GetMonitors() []MonitorBrightnessInfo {
- list := listMonitorBrightness()
- levels := loadSettings().Brightness.Levels
- for i := range list {
- if !list[i].Hardware {
- if v, ok := levels[strings.TrimSpace(list[i].Name)]; ok {
- list[i].Percent = v
- }
- }
- if list[i].Percent < 0 {
- list[i].Percent = 0
- } else if list[i].Percent > 100 {
- list[i].Percent = 100
- }
- }
- return list
- }
- // SetBrightness 设置指定显示器亮度(0-100),硬件不可用时自动回退 Gamma
- func (s *BrightnessService) SetBrightness(index, percent int) error {
- if percent < 0 {
- percent = 0
- } else if percent > 100 {
- percent = 100
- }
- return setMonitorBrightness(index, percent)
- }
- // TurnOff 立即关闭所有显示器(息屏),移动鼠标或按键即恢复
- func (s *BrightnessService) TurnOff() error {
- return turnOffDisplay()
- }
- // SaveLevel 记住某个显示器当前亮度(按名称保存,供启动时恢复)
- func (s *BrightnessService) SaveLevel(name string, percent int) error {
- name = strings.TrimSpace(name)
- if name == "" {
- return fmt.Errorf("显示器名称不能为空")
- }
- if percent < 0 {
- percent = 0
- } else if percent > 100 {
- percent = 100
- }
- settings := loadSettings()
- if settings.Brightness.Levels == nil {
- settings.Brightness.Levels = map[string]int{}
- }
- settings.Brightness.Levels[name] = percent
- return saveSettings(settings)
- }
|