Replace audio devices with audio toggle

This commit is contained in:
NeilClack
2026-04-23 17:29:14 -05:00
parent 4f33296f24
commit 105d8900d5
2 changed files with 51 additions and 30 deletions
+18
View File
@@ -0,0 +1,18 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch file",
"type": "go",
"request": "launch",
"mode": "debug",
"program": "rhemawave.go",
"env": {
"DEBUGINFOD_URLS": ""
}
},
]
}
+33 -30
View File
@@ -20,9 +20,8 @@ type AudioDevices struct {
title StringItem title StringItem
prompt StringItem prompt StringItem
AudioStatusToggle StringItem AudioStatusToggle StringItem
highlightIndex int
selectedIndex int
verticalMargin int32 verticalMargin int32
ItemScale float32
CfgPtr *db.AppConfig CfgPtr *db.AppConfig
} }
@@ -59,37 +58,26 @@ func (p *AudioDevices) Init(ctx *PageCtx) error {
// Calculate scales // Calculate scales
titleScale := float32(64) / float32(baseFontSize) titleScale := float32(64) / float32(baseFontSize)
itemScale := float32(32) / float32(baseFontSize) p.ItemScale = float32(32) / float32(baseFontSize)
// Create title // Create title
p.title, err = NewStringItem("Select an Audio Output Device", p.font, colors.White) p.title, err = NewStringItem("Toggle Audio", p.font, colors.White)
if err != nil { if err != nil {
return fmt.Errorf("creating title: %w", err) return fmt.Errorf("creating title: %w", err)
} }
p.title.Scale(titleScale) p.title.Scale(titleScale)
// Create prompt // Create prompt
p.prompt, err = NewStringItem("Use Arrow Keys to select, Enter to confirm", p.font, colors.White) p.prompt, err = NewStringItem("Switch On/Off with Enter. Setting saves immediately.", p.font, colors.White)
if err != nil { if err != nil {
return fmt.Errorf("creating prompt: %w", err) return fmt.Errorf("creating prompt: %w", err)
} }
p.prompt.Scale(itemScale) p.prompt.Scale(p.ItemScale)
on := p.Context.CfgPtr.AudioEnabled if err = p.CreateAudioStatusItem(); err != nil {
logger.Error("failed to create AudioStatusItem in AudioDevices.Init()")
audioStatus := "Turn Audio On"
if on {
audioStatus = "Turn Audio Off"
} }
audioStatusStringItem, err := NewStringItem(audioStatus, p.font, colors.White)
if err != nil {
return fmt.Errorf("failed to create Turn Audio Off string item: %w", err)
}
// Scale the new option
audioStatusStringItem.Scale(itemScale)
p.layout() p.layout()
return nil return nil
} }
@@ -106,20 +94,40 @@ func (p *AudioDevices) CreateAudioStatusItem() error {
if err != nil { if err != nil {
return fmt.Errorf("failed to create '%s' string item: %v", statusString, err) return fmt.Errorf("failed to create '%s' string item: %v", statusString, err)
} }
fmt.Printf("AudioStatusToggle (%d)", audioStatusStringItem.ID)
audioStatusStringItem.Scale(p.ItemScale)
p.AudioStatusToggle = audioStatusStringItem p.AudioStatusToggle = audioStatusStringItem
// Position the audio toggle
p.AudioStatusToggle.X = p.ScreenCenterX - p.AudioStatusToggle.W/2 // Horizontal Position
p.AudioStatusToggle.Y = p.ScreenCenterY + (p.AudioStatusToggle.H / 2) // Vertical Position
return nil return nil
} }
func (p *AudioDevices) layout() { func (p *AudioDevices) layout() {
// Position title at bottom // Position title at the top
p.title.X = p.ScreenCenterX - p.title.W/2 p.title.X = p.ScreenCenterX - p.title.W/2
p.title.Y = p.ScreenHeight - p.title.H - p.verticalMargin*2 p.title.Y = p.ScreenHeight - p.title.H - p.verticalMargin*2
// Position prompt above title // Position prompt below the title
p.prompt.X = p.ScreenCenterX - p.prompt.W/2 p.prompt.X = p.ScreenCenterX - p.prompt.W/2
p.prompt.Y = p.title.Y - p.verticalMargin*2 - p.prompt.H p.prompt.Y = p.title.Y - p.verticalMargin*2 - p.prompt.H
} }
func (p *AudioDevices) ToggleAudio() {
// Toggle current setting
p.Context.CfgPtr.AudioEnabled = !p.Context.CfgPtr.AudioEnabled
// Update the database
p.Context.DbPtr.Save(p.Context.CfgPtr)
// Update the configuration
p.Context.RefreshConfig()
// Update AudioEnabled UI element
p.CreateAudioStatusItem()
}
func (p *AudioDevices) HandleEvent(event *sdl.Event) error { func (p *AudioDevices) HandleEvent(event *sdl.Event) error {
e, ok := (*event).(*sdl.KeyboardEvent) e, ok := (*event).(*sdl.KeyboardEvent)
if !ok || e.Type != sdl.KEYDOWN { if !ok || e.Type != sdl.KEYDOWN {
@@ -129,14 +137,7 @@ func (p *AudioDevices) HandleEvent(event *sdl.Event) error {
switch e.Keysym.Sym { switch e.Keysym.Sym {
case sdl.K_RETURN: case sdl.K_RETURN:
// Toggle AudioEnabled p.ToggleAudio()
p.Context.CfgPtr.AudioEnabled = !p.Context.CfgPtr.AudioEnabled
// Save the config to the database
p.Context.DbPtr.Save(p.Context.CfgPtr)
// Refresh the config throughout the application
p.Context.RefreshConfig()
// Update AudioEnabled UI element
case sdl.K_BACKSPACE: case sdl.K_BACKSPACE:
p.Context.NavigateTo(SETTINGS) p.Context.NavigateTo(SETTINGS)
} }
@@ -145,19 +146,21 @@ func (p *AudioDevices) HandleEvent(event *sdl.Event) error {
} }
func (p *AudioDevices) Render() error { func (p *AudioDevices) Render() error {
// Always render title and prompt
if err := p.RenderTexture(p.textShader, p.title.ID, p.title.Position(), p.title.RawDimensions(), p.title.Color); err != nil { if err := p.RenderTexture(p.textShader, p.title.ID, p.title.Position(), p.title.RawDimensions(), p.title.Color); err != nil {
logger.Error("unable to render title texture: %v", err) logger.Error("unable to render title texture: %v", err)
return fmt.Errorf("unable to render title texture: %w", err) return fmt.Errorf("unable to render title texture: %w", err)
} }
if err := p.RenderTexture(p.textShader, p.prompt.ID, p.prompt.Position(), p.prompt.RawDimensions(), p.prompt.Color); err != nil { if err := p.RenderTexture(p.textShader, p.prompt.ID, p.prompt.Position(), p.prompt.RawDimensions(), p.prompt.Color); err != nil {
logger.Error("unable to render prompt texture: %v", err) logger.Error("unable to render prompt texture: %v", err)
return fmt.Errorf("unable to render prompt texture: %w", err) return fmt.Errorf("unable to render prompt texture: %w", err)
} }
if err := p.RenderTexture(p.textShader, p.AudioStatusToggle.ID, p.AudioStatusToggle.Position(), p.AudioStatusToggle.RawDimensions(), p.AudioStatusToggle.Color); err != nil { if err := p.RenderTexture(p.textShader, p.AudioStatusToggle.ID, p.AudioStatusToggle.Position(), p.AudioStatusToggle.RawDimensions(), p.AudioStatusToggle.Color); err != nil {
logger.Error("unable to render audioStatusToggle texture: %v", err) logger.Error("unable to render audioStatusToggle texture: %v", err)
return fmt.Errorf("unable to render audioStatusToggle texture: %w", err) return fmt.Errorf("unable to render audioStatusToggle texture: %w", err)
} }
return nil return nil
} }