mirror of
https://github.com/anotherhadi/settuings.git
synced 2026-10-05 16:18:24 +02:00
@@ -14,22 +14,28 @@ import (
|
||||
bluetoothUI "github.com/anotherhadi/settuings/internal/ui/bluetooth"
|
||||
notificationsUI "github.com/anotherhadi/settuings/internal/ui/components/notifications"
|
||||
networkUI "github.com/anotherhadi/settuings/internal/ui/network"
|
||||
powerUI "github.com/anotherhadi/settuings/internal/ui/power"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
var pageShortcuts = func() map[string]page {
|
||||
m := make(map[string]page, len(pageRegistry))
|
||||
for i, e := range pageRegistry {
|
||||
// buildPageShortcuts numbers the visible pages 1..N in sidebar order, so
|
||||
// hiding a page never leaves a gap in the shortcuts. It must run after
|
||||
// config.Load, hence a function called from New rather than a package var.
|
||||
func buildPageShortcuts() map[string]page {
|
||||
visible := visiblePages()
|
||||
m := make(map[string]page, len(visible))
|
||||
for i, e := range visible {
|
||||
m[strconv.Itoa(i+1)] = e.id
|
||||
}
|
||||
return m
|
||||
}()
|
||||
}
|
||||
|
||||
type Model struct {
|
||||
page page
|
||||
logPath string
|
||||
fatalErr error
|
||||
logFileErr error
|
||||
page page
|
||||
pageShortcuts map[string]page
|
||||
logPath string
|
||||
fatalErr error
|
||||
logFileErr error
|
||||
|
||||
width int
|
||||
height int
|
||||
@@ -38,6 +44,7 @@ type Model struct {
|
||||
network networkUI.Model
|
||||
bluetooth bluetoothUI.Model
|
||||
audio audioUI.Model
|
||||
power powerUI.Model
|
||||
notifications notificationsUI.Model
|
||||
}
|
||||
|
||||
@@ -61,10 +68,12 @@ func New(initialPage string) (Model, error) {
|
||||
|
||||
m := Model{
|
||||
page: startPage,
|
||||
pageShortcuts: buildPageShortcuts(),
|
||||
about: aboutUI.New(),
|
||||
network: networkUI.New(),
|
||||
bluetooth: bluetoothUI.New(),
|
||||
audio: audioUI.New(),
|
||||
power: powerUI.New(),
|
||||
notifications: notificationsUI.New(),
|
||||
sidebarState: sidebarState(cfg.TUI.DefaultSidebarState),
|
||||
logPath: logPath,
|
||||
@@ -83,7 +92,7 @@ func New(initialPage string) (Model, error) {
|
||||
func (m Model) FatalErr() error { return m.fatalErr }
|
||||
|
||||
func (m Model) Init() tea.Cmd {
|
||||
cmds := []tea.Cmd{m.about.Init(), m.network.Init(), m.bluetooth.Init(), m.audio.Init()}
|
||||
cmds := []tea.Cmd{m.about.Init(), m.network.Init(), m.bluetooth.Init(), m.audio.Init(), m.power.Init()}
|
||||
if m.page != pageAbout {
|
||||
cmds = append(cmds, m.activatePage(m.page))
|
||||
}
|
||||
|
||||
@@ -4,11 +4,13 @@ import (
|
||||
"strings"
|
||||
|
||||
tea "charm.land/bubbletea/v2"
|
||||
"github.com/anotherhadi/settuings/internal/config"
|
||||
"github.com/anotherhadi/settuings/internal/icons"
|
||||
aboutUI "github.com/anotherhadi/settuings/internal/ui/about"
|
||||
audioUI "github.com/anotherhadi/settuings/internal/ui/audio"
|
||||
bluetoothUI "github.com/anotherhadi/settuings/internal/ui/bluetooth"
|
||||
networkUI "github.com/anotherhadi/settuings/internal/ui/network"
|
||||
powerUI "github.com/anotherhadi/settuings/internal/ui/power"
|
||||
)
|
||||
|
||||
type page string
|
||||
@@ -18,6 +20,7 @@ const (
|
||||
pageNetwork page = "Network"
|
||||
pageBluetooth page = "Bluetooth"
|
||||
pageAudio page = "Audio"
|
||||
pagePower page = "Power"
|
||||
)
|
||||
|
||||
// pageEntry describes a page and all its integration hooks.
|
||||
@@ -103,6 +106,20 @@ var pageRegistry = []pageEntry{
|
||||
activate: func(m *Model) tea.Cmd { return m.audio.Activate() },
|
||||
deactivate: func(m *Model) { m.audio.Deactivate() },
|
||||
},
|
||||
{
|
||||
id: pagePower,
|
||||
icon: func() string { return icons.I.Power },
|
||||
|
||||
render: func(m *Model) string { return m.power.View().Content },
|
||||
update: func(m *Model, msg tea.Msg) tea.Cmd {
|
||||
updated, cmd := m.power.Update(msg)
|
||||
m.power = updated.(powerUI.Model)
|
||||
return cmd
|
||||
},
|
||||
isEditing: func(m *Model) bool { return m.power.IsEditing() },
|
||||
resize: func(m *Model, w, h int) { m.power.SetSize(w, h) },
|
||||
activate: func(m *Model) tea.Cmd { return m.power.Activate() },
|
||||
},
|
||||
}
|
||||
|
||||
// PageNames returns the valid page identifiers, in sidebar order, usable
|
||||
@@ -125,3 +142,32 @@ func lookupPage(name string) (page, bool) {
|
||||
}
|
||||
return "", false
|
||||
}
|
||||
|
||||
// visiblePages returns pageRegistry entries not listed in the user's
|
||||
// tui.hidden_pages config, preserving registry order. A page hidden this
|
||||
// way is still reachable via --page: hiding only affects the sidebar and
|
||||
// its numbered shortcuts.
|
||||
func visiblePages() []pageEntry {
|
||||
hiddenNames := config.Global.TUI.HiddenPages
|
||||
if len(hiddenNames) == 0 {
|
||||
return pageRegistry
|
||||
}
|
||||
|
||||
hidden := make(map[page]bool, len(hiddenNames))
|
||||
for _, name := range hiddenNames {
|
||||
if p, ok := lookupPage(name); ok {
|
||||
hidden[p] = true
|
||||
}
|
||||
}
|
||||
if len(hidden) == 0 {
|
||||
return pageRegistry
|
||||
}
|
||||
|
||||
visible := make([]pageEntry, 0, len(pageRegistry))
|
||||
for _, e := range pageRegistry {
|
||||
if !hidden[e.id] {
|
||||
visible = append(visible, e)
|
||||
}
|
||||
}
|
||||
return visible
|
||||
}
|
||||
|
||||
@@ -62,7 +62,7 @@ func (m *Model) renderSidebar() string {
|
||||
var items strings.Builder
|
||||
badgeUnread := lipgloss.NewStyle().Foreground(ilovetui.S.Warning).Bold(true)
|
||||
|
||||
for i, entry := range pageRegistry {
|
||||
for i, entry := range visiblePages() {
|
||||
selected := entry.id == m.page
|
||||
badgeStyle, textStyle := badgeNormal, textNormal
|
||||
if selected {
|
||||
|
||||
@@ -47,7 +47,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
m.resizeChildren()
|
||||
|
||||
default:
|
||||
if p, ok := pageShortcuts[msg.String()]; ok {
|
||||
if p, ok := m.pageShortcuts[msg.String()]; ok {
|
||||
prev := m.page
|
||||
m.page = p
|
||||
if prev != p {
|
||||
|
||||
Reference in New Issue
Block a user