Quellcode durchsuchen

refactor(ui): add NewConfiguredBox func

ayn2op vor 1 Jahr
Ursprung
Commit
c9eb6b54f6
4 geänderte Dateien mit 38 neuen und 55 gelöschten Zeilen
  1. 6 19
      cmd/guilds_tree.go
  2. 5 18
      cmd/message_input.go
  3. 4 17
      cmd/messages_text.go
  4. 23 1
      internal/ui/util.go

+ 6 - 19
cmd/guilds_tree.go

@@ -8,6 +8,7 @@ import (
 
 	"github.com/atotto/clipboard"
 	"github.com/ayn2op/discordo/internal/config"
+	"github.com/ayn2op/discordo/internal/ui"
 	"github.com/diamondburned/arikawa/v3/discord"
 	"github.com/diamondburned/arikawa/v3/gateway"
 	"github.com/gdamore/tcell/v2"
@@ -28,29 +29,15 @@ func newGuildsTree(app *tview.Application, cfg *config.Config) *GuildsTree {
 		app:      app,
 	}
 
-	t := cfg.Theme
+	gt.Box = ui.NewConfiguredBox(gt.Box, &cfg.Theme)
+
 	gt.
 		SetRoot(tview.NewTreeNode("")).
 		SetTopLevel(1).
-		SetGraphics(t.GuildsTree.Graphics).
-		SetSelectedFunc(gt.onSelected)
-
-	b := t.Border
-	p := b.Padding
-	gt.
-		SetInputCapture(gt.onInputCapture).
+		SetGraphics(cfg.Theme.GuildsTree.Graphics).
+		SetSelectedFunc(gt.onSelected).
 		SetTitle("Guilds").
-		SetTitleAlign(tview.AlignLeft).
-		SetBorder(b.Enabled).
-		SetBorderPadding(p[0], p[1], p[2], p[3]).
-		SetFocusFunc(func() {
-			gt.SetBorderColor(tcell.GetColor(b.ActiveColor))
-			gt.SetTitleColor(tcell.GetColor(t.ActiveTitleColor))
-		}).
-		SetBlurFunc(func() {
-			gt.SetBorderColor(tcell.GetColor(b.Color))
-			gt.SetTitleColor(tcell.GetColor(t.TitleColor))
-		})
+		SetInputCapture(gt.onInputCapture)
 
 	return gt
 }

+ 5 - 18
cmd/message_input.go

@@ -9,6 +9,7 @@ import (
 	"github.com/atotto/clipboard"
 	"github.com/ayn2op/discordo/internal/config"
 	"github.com/ayn2op/discordo/internal/consts"
+	"github.com/ayn2op/discordo/internal/ui"
 	"github.com/diamondburned/arikawa/v3/api"
 	"github.com/diamondburned/arikawa/v3/discord"
 	"github.com/diamondburned/arikawa/v3/utils/json/option"
@@ -32,31 +33,17 @@ func newMessageInput(app *tview.Application, cfg *config.Config) *MessageInput {
 		app:      app,
 	}
 
-	t := cfg.Theme
+	mi.Box = ui.NewConfiguredBox(mi.Box, &cfg.Theme)
+
 	mi.
-		SetTextStyle(tcell.StyleDefault.Background(tcell.GetColor(t.BackgroundColor))).
+		SetTextStyle(tcell.StyleDefault.Background(tcell.GetColor(cfg.Theme.BackgroundColor))).
 		SetClipboard(func(s string) {
 			_ = clipboard.WriteAll(s)
 		}, func() string {
 			text, _ := clipboard.ReadAll()
 			return text
-		})
-
-	b := t.Border
-	p := b.Padding
-	mi.
-		SetInputCapture(mi.onInputCapture).
-		SetTitleAlign(tview.AlignLeft).
-		SetBorder(b.Enabled).
-		SetBorderPadding(p[0], p[1], p[2], p[3]).
-		SetFocusFunc(func() {
-			mi.SetBorderColor(tcell.GetColor(b.ActiveColor))
-			mi.SetTitleColor(tcell.GetColor(t.ActiveTitleColor))
 		}).
-		SetBlurFunc(func() {
-			mi.SetBorderColor(tcell.GetColor(b.Color))
-			mi.SetTitleColor(tcell.GetColor(t.TitleColor))
-		})
+		SetInputCapture(mi.onInputCapture)
 
 	return mi
 }

+ 4 - 17
cmd/messages_text.go

@@ -38,6 +38,8 @@ func newMessagesText(app *tview.Application, cfg *config.Config) *MessagesText {
 		app:      app,
 	}
 
+	mt.Box = ui.NewConfiguredBox(mt.Box, &cfg.Theme)
+
 	t := cfg.Theme
 	mt.
 		SetDynamicColors(true).
@@ -48,24 +50,9 @@ func newMessagesText(app *tview.Application, cfg *config.Config) *MessagesText {
 		SetHighlightedFunc(mt.onHighlighted).
 		SetChangedFunc(func() {
 			app.Draw()
-		})
-
-	b := t.Border
-	p := b.Padding
-	mt.
-		SetInputCapture(mt.onInputCapture).
-		SetTitle("Messages").
-		SetTitleAlign(tview.AlignLeft).
-		SetBorder(b.Enabled).
-		SetBorderPadding(p[0], p[1], p[2], p[3]).
-		SetFocusFunc(func() {
-			mt.SetBorderColor(tcell.GetColor(b.ActiveColor))
-			mt.SetTitleColor(tcell.GetColor(t.ActiveTitleColor))
 		}).
-		SetBlurFunc(func() {
-			mt.SetBorderColor(tcell.GetColor(b.Color))
-			mt.SetTitleColor(tcell.GetColor(t.TitleColor))
-		})
+		SetTitle("Messages").
+		SetInputCapture(mt.onInputCapture)
 
 	markdown.DefaultRenderer.AddOptions(
 		renderer.WithOption("emojiColor", t.MessagesText.EmojiColor),

+ 23 - 1
internal/ui/util.go

@@ -1,6 +1,28 @@
 package ui
 
-import "github.com/rivo/tview"
+import (
+	"github.com/ayn2op/discordo/internal/config"
+	"github.com/gdamore/tcell/v2"
+	"github.com/rivo/tview"
+)
+
+func NewConfiguredBox(box *tview.Box, cfg *config.Theme) *tview.Box {
+	b := cfg.Border
+	p := b.Padding
+	box.
+		SetBorder(cfg.Border.Enabled).
+		SetBorderPadding(p[0], p[1], p[2], p[3]).
+		SetTitleAlign(tview.AlignLeft).
+		SetFocusFunc(func() {
+			box.SetBorderColor(tcell.GetColor(b.ActiveColor))
+			box.SetTitleColor(tcell.GetColor(cfg.ActiveTitleColor))
+		}).
+		SetBlurFunc(func() {
+			box.SetBorderColor(tcell.GetColor(b.Color))
+			box.SetTitleColor(tcell.GetColor(cfg.TitleColor))
+		})
+	return box
+}
 
 func Centered(p tview.Primitive, width, height int) tview.Primitive {
 	return tview.NewGrid().