Switch the theme live
Widgets copy their colors when they are created, so they now get role tokens (one unique RGB value per role) instead of colors, and a wrapper around the tcell screen replaces the tokens with the current theme's colors in SetContent. Changing the theme in the Config view swaps that table; the next draw recolors everything. Poster colors that equal a token are nudged off it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019reK4DUGYqDUjniWYJvwUW
This commit is contained in:
+26
-1
@@ -30,6 +30,8 @@ type view struct {
|
||||
// App is the top-level arr-tui application.
|
||||
type App struct {
|
||||
app *tview.Application
|
||||
theme theme // the colors in use
|
||||
themed *themedScreen // draws with them; nil until Run
|
||||
tabs *tview.TextView
|
||||
pages *tview.Pages
|
||||
status *tview.TextView
|
||||
@@ -103,7 +105,7 @@ func New(cfg *config.Config) *App {
|
||||
// Ask the terminal before the screen takes it over, and set the colors
|
||||
// before any widget copies them.
|
||||
th, themeNote := chooseTheme(cfg.UI.Theme, terminalAnswers())
|
||||
th.apply()
|
||||
useTokens()
|
||||
a := &App{
|
||||
app: tview.NewApplication(),
|
||||
tabs: tview.NewTextView().SetDynamicColors(true),
|
||||
@@ -140,6 +142,7 @@ func New(cfg *config.Config) *App {
|
||||
if a.covers {
|
||||
a.logs.add(levelInfo, coverNote)
|
||||
}
|
||||
a.theme = th
|
||||
a.logs.add(levelInfo, themeNote)
|
||||
sabClient := sabnzbd.NewClient(cfg.SABnzbd.URL, cfg.SABnzbd.APIKey)
|
||||
a.sab = sabClient
|
||||
@@ -394,12 +397,34 @@ func (a *App) Run() error {
|
||||
a.gfx.deleteAll()
|
||||
}
|
||||
}()
|
||||
// The screen draws through the theme (see themedScreen).
|
||||
s, err := tcell.NewScreen()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
a.themed = newThemedScreen(s, a.theme)
|
||||
a.app.SetScreen(a.themed) // initializes it
|
||||
if a.themed.initErr != nil {
|
||||
return a.themed.initErr
|
||||
}
|
||||
a.themed.DisableMouse()
|
||||
a.themed.DisablePaste()
|
||||
|
||||
go a.watchServices(stop)
|
||||
a.setNotifyDownloads(a.notifyDone)
|
||||
defer a.setNotifyDownloads(false)
|
||||
return a.app.Run()
|
||||
}
|
||||
|
||||
// setTheme switches the colors while the program runs.
|
||||
func (a *App) setTheme(t theme, note string) {
|
||||
a.theme = t
|
||||
if a.themed != nil {
|
||||
a.themed.setTheme(t)
|
||||
}
|
||||
a.logs.add(levelInfo, note)
|
||||
}
|
||||
|
||||
// setNotifyDownloads starts or stops the background watch for finished downloads.
|
||||
func (a *App) setNotifyDownloads(on bool) {
|
||||
a.notifyDone = on
|
||||
|
||||
@@ -35,6 +35,9 @@ func (a *App) applyConfig(c *config.Config) {
|
||||
a.downloads.setInterval(time.Duration(c.UI.RefreshSeconds) * time.Second)
|
||||
}
|
||||
a.bell = c.UI.Bell
|
||||
if old.UI.Theme != c.UI.Theme {
|
||||
a.setTheme(chooseTheme(c.UI.Theme, terminalAnswers()))
|
||||
}
|
||||
if c.UI.ShouldNotifyDownloads() != a.notifyDone {
|
||||
a.setNotifyDownloads(c.UI.ShouldNotifyDownloads())
|
||||
}
|
||||
|
||||
@@ -458,8 +458,8 @@ func (c *coverView) build(cols, rows int) []styledCell {
|
||||
}
|
||||
ch, fg, bg := st.bestCell(px)
|
||||
cells = append(cells, styledCell{ch,
|
||||
tcell.NewRGBColor(int32(fg.R), int32(fg.G), int32(fg.B)),
|
||||
tcell.NewRGBColor(int32(bg.R), int32(bg.G), int32(bg.B))})
|
||||
notToken(tcell.NewRGBColor(int32(fg.R), int32(fg.G), int32(fg.B))),
|
||||
notToken(tcell.NewRGBColor(int32(bg.R), int32(bg.G), int32(bg.B)))})
|
||||
}
|
||||
}
|
||||
return cells
|
||||
|
||||
+1
-1
@@ -71,7 +71,7 @@ var helpSections = []helpSection{
|
||||
{"Tab", "jump between the fields and Save / Revert"},
|
||||
{"Space", "toggle a checkbox; press a button"},
|
||||
{"Test", "(button below the SABnzbd settings) checks the three services with the values typed in"},
|
||||
{"Save, Revert", "(at the bottom) Save writes the file and applies everything at once (the theme at the next start); Revert discards edits"},
|
||||
{"Save, Revert", "(at the bottom) Save writes the file and applies everything at once; Revert discards edits"},
|
||||
}},
|
||||
{title: "Log (F7)", note: "Popups fade away by themselves; everything they said is kept here.", items: []helpItem{
|
||||
{"↑↓, PgUp, PgDn", "scroll"},
|
||||
|
||||
@@ -144,7 +144,7 @@ func (r *toastRoot) Draw(screen tcell.Screen) {
|
||||
if t.expires.Sub(now) < toastFade {
|
||||
fg = named("gray") // fade out
|
||||
}
|
||||
style := tcell.StyleDefault.Background(current.toast).Foreground(fg)
|
||||
style := tcell.StyleDefault.Background(tokens.toast).Foreground(fg)
|
||||
for cx := x; cx < x+width; cx++ {
|
||||
screen.SetContent(cx, y, ' ', nil, style)
|
||||
}
|
||||
|
||||
@@ -108,8 +108,8 @@ func boolSetting(old *bool, value, def bool) *bool {
|
||||
return &value
|
||||
}
|
||||
|
||||
// settingsView edits the configuration file's settings. Everything except
|
||||
// the theme applies right after saving; the theme at the next start.
|
||||
// settingsView edits the configuration file's settings. Everything applies
|
||||
// right after saving.
|
||||
type settingsView struct {
|
||||
app *App
|
||||
root *tview.Flex
|
||||
@@ -184,7 +184,7 @@ func newSettingsView(app *App) *settingsView {
|
||||
f.AddFormItem(v.covers)
|
||||
v.style = tview.NewDropDown().SetLabel("Poster style").SetOptions(coverStyles, func(string, int) { v.edited() })
|
||||
f.AddFormItem(v.style)
|
||||
v.theme = tview.NewDropDown().SetLabel("Theme (at the next start)").SetOptions(themes, func(string, int) { v.edited() })
|
||||
v.theme = tview.NewDropDown().SetLabel("Theme").SetOptions(themes, func(string, int) { v.edited() })
|
||||
f.AddFormItem(v.theme)
|
||||
v.notify = tview.NewCheckbox().SetLabel("Notify about finished downloads").SetChangedFunc(func(bool) { v.edited() })
|
||||
f.AddFormItem(v.notify)
|
||||
@@ -347,7 +347,7 @@ func (v *settingsView) describe() {
|
||||
} else {
|
||||
b.WriteString("Config file: " + tview.Escape(v.app.cfgPath) + "\n")
|
||||
}
|
||||
b.WriteString("Changes take effect when you save; only the theme waits for the next start.\n")
|
||||
b.WriteString("Changes take effect when you save; nothing needs a restart.\n")
|
||||
if len(v.overridden) > 0 {
|
||||
var names []string
|
||||
for _, env := range v.overridden {
|
||||
@@ -437,13 +437,9 @@ func (v *settingsView) save() {
|
||||
v.report("[red]Saved, but not applied: " + tview.Escape(err.Error()) + "[-]")
|
||||
return
|
||||
}
|
||||
themeChanged := effective.UI.Theme != v.app.cfg.UI.Theme
|
||||
v.app.cfgFile = &raw
|
||||
v.app.applyConfig(effective)
|
||||
v.dirty = false
|
||||
v.report("[green]Saved to " + tview.Escape(v.app.cfgPath) + " and applied.[-]")
|
||||
if themeChanged {
|
||||
v.report("The new theme is used from the next start.")
|
||||
}
|
||||
v.app.Notify(levelSuccess, "Settings saved")
|
||||
}
|
||||
|
||||
@@ -51,29 +51,65 @@ func TestChooseTheme(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestThemeApply(t *testing.T) {
|
||||
oldStyles, oldCurrent := tview.Styles, current
|
||||
func TestUseTokens(t *testing.T) {
|
||||
oldStyles := tview.Styles
|
||||
oldNames := map[string]tcell.Color{}
|
||||
for k, v := range tcell.ColorNames {
|
||||
oldNames[k] = v
|
||||
}
|
||||
defer func() {
|
||||
tview.Styles, current = oldStyles, oldCurrent
|
||||
tview.Styles = oldStyles
|
||||
for k, v := range oldNames {
|
||||
tcell.ColorNames[k] = v
|
||||
}
|
||||
}()
|
||||
|
||||
lightTheme.apply()
|
||||
if tview.Styles.PrimitiveBackgroundColor != lightTheme.bg || tview.Styles.PrimaryTextColor != lightTheme.text {
|
||||
useTokens()
|
||||
if tview.Styles.PrimitiveBackgroundColor != tokens.bg || tview.Styles.PrimaryTextColor != tokens.text {
|
||||
t.Error("tview styles not set")
|
||||
}
|
||||
// Color names in texts mean the theme's colors.
|
||||
if named("yellow") != lightTheme.yellow || tcell.GetColor("black") != lightTheme.bg || tcell.GetColor("white") != lightTheme.text {
|
||||
// Color names in texts mean the roles.
|
||||
if named("yellow") != tokens.yellow || tcell.GetColor("black") != tokens.bg || tcell.GetColor("white") != tokens.text {
|
||||
t.Error("color names not set")
|
||||
}
|
||||
if current.name != "light" {
|
||||
t.Error("current theme not set")
|
||||
}
|
||||
|
||||
func TestThemedScreenSwitchesLive(t *testing.T) {
|
||||
sim := tcell.NewSimulationScreen("")
|
||||
if err := sim.Init(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer sim.Fini()
|
||||
sim.SetSize(4, 1)
|
||||
s := newThemedScreen(sim, darkTheme)
|
||||
poster := tcell.NewRGBColor(10, 20, 30)
|
||||
|
||||
draw := func() {
|
||||
s.SetContent(0, 0, 'a', nil, tcell.StyleDefault.Foreground(tokens.yellow).Background(tokens.bg))
|
||||
s.SetContent(1, 0, 'b', nil, tcell.StyleDefault.Foreground(poster).Background(poster))
|
||||
}
|
||||
check := func(th theme) {
|
||||
_, _, st, _ := sim.GetContent(0, 0)
|
||||
if fg, bg, _ := st.Decompose(); fg != th.yellow || bg != th.bg {
|
||||
t.Errorf("%s: role colors drawn as %v on %v", th.name, fg, bg)
|
||||
}
|
||||
_, _, st, _ = sim.GetContent(1, 0)
|
||||
if fg, bg, _ := st.Decompose(); fg != poster || bg != poster {
|
||||
t.Errorf("%s: other colors changed to %v on %v", th.name, fg, bg)
|
||||
}
|
||||
}
|
||||
draw()
|
||||
check(darkTheme)
|
||||
s.setTheme(lightTheme)
|
||||
draw()
|
||||
check(lightTheme)
|
||||
|
||||
// A poster color that equals a role color is nudged off it.
|
||||
if c := notToken(tokens.red); c == tokens.red {
|
||||
t.Error("role color in a picture not nudged")
|
||||
}
|
||||
if notToken(poster) != poster {
|
||||
t.Error("ordinary color changed")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+82
-11
@@ -46,9 +46,6 @@ var (
|
||||
}
|
||||
)
|
||||
|
||||
// current is the theme in use.
|
||||
var current = darkTheme
|
||||
|
||||
// chooseTheme resolves the ui.theme setting: "dark", "light", or "auto" (the
|
||||
// default), which follows the background color the terminal reported and is
|
||||
// dark when it did not say. The second value explains the choice for the log.
|
||||
@@ -69,13 +66,34 @@ func chooseTheme(setting string, info terminalInfo) (theme, string) {
|
||||
return darkTheme, "Theme: dark (auto: the terminal's background " + hex + " is dark)"
|
||||
}
|
||||
|
||||
// apply makes t the colors of everything created afterwards: tview's default
|
||||
// styles (widgets copy them when they are created, so this must run before any
|
||||
// is) and the color names used in the texts, which tview looks up in tcell's
|
||||
// table: "[yellow]" means this theme's yellow, "white" its text color and
|
||||
// "black" its background (the active tab is "[black:white]").
|
||||
func (t theme) apply() {
|
||||
current = t
|
||||
// colors lists the theme's colors in a fixed order of roles.
|
||||
func (t theme) colors() []tcell.Color {
|
||||
return []tcell.Color{t.bg, t.text, t.border, t.field, t.fieldMuted, t.list, t.pressed, t.toast,
|
||||
t.gray, t.yellow, t.green, t.red, t.blue, t.purple}
|
||||
}
|
||||
|
||||
// tokens is the theme the widgets are built with: every role has a color of
|
||||
// its own that no theme uses, and themedScreen replaces it with the color of
|
||||
// the current theme while drawing. That is how the theme changes while the
|
||||
// program runs: widgets copy their colors when they are created, but what they
|
||||
// copy is a role, not a color.
|
||||
var tokens = func() theme {
|
||||
var c [14]tcell.Color
|
||||
for i := range c {
|
||||
c[i] = tcell.NewRGBColor(0xa5, 0x17, int32(i))
|
||||
}
|
||||
return theme{name: "tokens", bg: c[0], text: c[1], border: c[2], field: c[3], fieldMuted: c[4], list: c[5], pressed: c[6], toast: c[7],
|
||||
gray: c[8], yellow: c[9], green: c[10], red: c[11], blue: c[12], purple: c[13]}
|
||||
}()
|
||||
|
||||
// useTokens makes the role colors the defaults of everything created
|
||||
// afterwards: tview's default styles (widgets copy them when they are created,
|
||||
// so this must run before any is) and the color names used in the texts,
|
||||
// which tview looks up in tcell's table: "[yellow]" is the theme's yellow,
|
||||
// "white" its text color and "black" its background (the active tab is
|
||||
// "[black:white]").
|
||||
func useTokens() {
|
||||
t := tokens
|
||||
st := &tview.Styles
|
||||
st.PrimitiveBackgroundColor = t.bg
|
||||
st.ContrastBackgroundColor = t.field
|
||||
@@ -97,7 +115,60 @@ func (t theme) apply() {
|
||||
}
|
||||
}
|
||||
|
||||
// named returns the theme's color for a name used in the texts.
|
||||
// themedScreen draws with the current theme: it replaces the role colors of
|
||||
// everything set on the screen by the theme's colors. Other colors (posters,
|
||||
// the image ids of real-image posters) pass unchanged.
|
||||
type themedScreen struct {
|
||||
tcell.Screen
|
||||
theme theme
|
||||
colors map[tcell.Color]tcell.Color
|
||||
initErr error
|
||||
}
|
||||
|
||||
func newThemedScreen(s tcell.Screen, t theme) *themedScreen {
|
||||
ts := &themedScreen{Screen: s}
|
||||
ts.setTheme(t)
|
||||
return ts
|
||||
}
|
||||
|
||||
// setTheme switches the colors; the next draw shows them.
|
||||
func (s *themedScreen) setTheme(t theme) {
|
||||
s.theme = t
|
||||
s.colors = map[tcell.Color]tcell.Color{}
|
||||
actual := t.colors()
|
||||
for i, tok := range tokens.colors() {
|
||||
s.colors[tok] = actual[i]
|
||||
}
|
||||
}
|
||||
|
||||
func (s *themedScreen) SetContent(x, y int, primary rune, combining []rune, style tcell.Style) {
|
||||
fg, bg, _ := style.Decompose()
|
||||
if c, ok := s.colors[fg]; ok {
|
||||
style = style.Foreground(c)
|
||||
}
|
||||
if c, ok := s.colors[bg]; ok {
|
||||
style = style.Background(c)
|
||||
}
|
||||
s.Screen.SetContent(x, y, primary, combining, style)
|
||||
}
|
||||
|
||||
// Init remembers the error, which tview's SetScreen drops.
|
||||
func (s *themedScreen) Init() error {
|
||||
s.initErr = s.Screen.Init()
|
||||
return s.initErr
|
||||
}
|
||||
|
||||
// notToken nudges a picture's color that happens to equal a role color, so
|
||||
// themedScreen does not replace it.
|
||||
func notToken(c tcell.Color) tcell.Color {
|
||||
r, g, b := c.RGB()
|
||||
if r == 0xa5 && g == 0x17 && b < int32(len(tokens.colors())) {
|
||||
return tcell.NewRGBColor(r, g+1, b)
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
// named returns the role color for a name used in the texts.
|
||||
func named(name string) tcell.Color { return tcell.GetColor(name) }
|
||||
|
||||
func hexColor(c color.RGBA) string {
|
||||
|
||||
Reference in New Issue
Block a user