fixed some ui state issues

This commit is contained in:
2026-01-29 07:11:39 -06:00
parent 038bc3e88c
commit a44f433bba
2 changed files with 31 additions and 17 deletions
+11 -8
View File
@@ -12,7 +12,7 @@ import (
"github.com/rivo/tview" "github.com/rivo/tview"
) )
func MonitorDevice(ctx context.Context, app *tview.Application, frame **tview.Frame, device *Device) { func MonitorDevice(ctx context.Context, app *tview.Application, frame **tview.Frame, state *UIState, device *Device) {
ticker := time.NewTicker(100 * time.Millisecond) // Check every 100ms ticker := time.NewTicker(100 * time.Millisecond) // Check every 100ms
defer ticker.Stop() defer ticker.Stop()
for { for {
@@ -24,19 +24,19 @@ func MonitorDevice(ctx context.Context, app *tview.Application, frame **tview.Fr
if err != nil { if err != nil {
if device.Connected { if device.Connected {
device.Close() device.Close()
UpdateDeviceStatus(app, *frame, "Cardputer disconnected", tcell.ColorRed) UpdateDeviceStatus(app, *frame, state, "Cardputer disconnected", tcell.ColorRed)
} }
} else { } else {
if !device.Connected { if !device.Connected {
UpdateDeviceStatus(app, *frame, "Cardputer found: "+port, tcell.ColorWhite) UpdateDeviceStatus(app, *frame, state, "Cardputer found: "+port, tcell.ColorWhite)
err = device.Connect() err = device.Connect()
if err != nil { if err != nil {
UpdateDeviceStatus(app, *frame, "Error connecting to cardputer: "+err.Error(), tcell.ColorRed) UpdateDeviceStatus(app, *frame, state, "Error connecting to cardputer: "+err.Error(), tcell.ColorRed)
} else { } else {
UpdateDeviceStatus(app, *frame, "Connected to cardputer: "+port, tcell.ColorGreen) UpdateDeviceStatus(app, *frame, state, "Connected to cardputer: "+port, tcell.ColorGreen)
} }
} else { } else {
UpdateDeviceStatus(app, *frame, "Connected to cardputer: "+port, tcell.ColorGreen) UpdateDeviceStatus(app, *frame, state, "Connected to cardputer: "+port, tcell.ColorGreen)
} }
} }
} }
@@ -49,7 +49,10 @@ func main() {
app := tview.NewApplication() app := tview.NewApplication()
var currentFrame *tview.Frame var currentFrame *tview.Frame
frame := AppUI(app, &currentFrame) state := &UIState{
Page: "home",
}
frame := AppUI(app, &currentFrame, state)
currentFrame = frame currentFrame = frame
app.SetRoot(frame, true) app.SetRoot(frame, true)
@@ -76,7 +79,7 @@ func main() {
app.Stop() app.Stop()
}() }()
go MonitorDevice(ctx, app, &currentFrame, device) go MonitorDevice(ctx, app, &currentFrame, state, device)
if err := app.Run(); err != nil { if err := app.Run(); err != nil {
log.Fatal("Error running program:", err) log.Fatal("Error running program:", err)
+20 -9
View File
@@ -5,42 +5,53 @@ import (
"github.com/rivo/tview" "github.com/rivo/tview"
) )
func AppUI(app *tview.Application, currentFrame **tview.Frame) *tview.Frame { type UIState struct {
Page string
}
func AppUI(app *tview.Application, currentFrame **tview.Frame, state *UIState) *tview.Frame {
list := tview.NewList() list := tview.NewList()
list.AddItem("CPkg Tools", "Create and unpack cpkg files for distribution", 'c', func() { list.AddItem("CPkg Tools", "Create and unpack cpkg files for distribution", 'c', func() {
newFrame := PackageUI(app, currentFrame) newFrame := PackageUI(app, currentFrame, state)
*currentFrame = newFrame *currentFrame = newFrame
state.Page = "package"
app.SetRoot(newFrame, true) app.SetRoot(newFrame, true)
}) })
list.AddItem("Upload to Cardputer", "Creates a package and uploads it to the connected Cardputer", 'u', nil) list.AddItem("Upload to Cardputer", "Creates a package and uploads it to the connected Cardputer", 'u', nil)
frame := tview.NewFrame(list) frame := tview.NewFrame(list)
frame.SetBorders(0, 0, 0, 0, 0, 0) frame.SetBorders(0, 0, 0, 0, 0, 0)
frame.AddText("Parchment - v0.0.1", true, tview.AlignLeft, tcell.ColorWhite) frame.AddText("Parchment - v0.0.1", true, tview.AlignLeft, tcell.ColorWhite)
frame.AddText("Searching for device...", false, tview.AlignLeft, tcell.ColorWhite) frame.AddText("Waiting for cardputer...", false, tview.AlignLeft, tcell.ColorWhite)
return frame return frame
} }
func PackageUI(app *tview.Application, currentFrame **tview.Frame) *tview.Frame { func PackageUI(app *tview.Application, currentFrame **tview.Frame, state *UIState) *tview.Frame {
list := tview.NewList() list := tview.NewList()
list.AddItem("Create Package", "Creates a .cpkg file in the running directory", 'c', nil) list.AddItem("Create Package", "Creates a .cpkg file in the running directory", 'c', nil)
list.AddItem("Unpack Package", "Unpacks a chosen .cpkg file into a subdirectory", 'u', nil) list.AddItem("Unpack Package", "Unpacks a chosen .cpkg file into a subdirectory", 'u', nil)
list.AddItem("Back", "Go back to the home page", 'b', func() { list.AddItem("Back", "Go back to the home page", 'b', func() {
newFrame := AppUI(app, currentFrame) newFrame := AppUI(app, currentFrame, state)
*currentFrame = newFrame *currentFrame = newFrame
state.Page = "home"
app.SetRoot(newFrame, true) app.SetRoot(newFrame, true)
}) })
frame := tview.NewFrame(list) frame := tview.NewFrame(list)
frame.SetBorders(0, 0, 0, 0, 0, 0) frame.SetBorders(0, 0, 0, 0, 0, 0)
frame.AddText("Package Mode", true, tview.AlignLeft, tcell.ColorWhite) frame.AddText("Package Tools", true, tview.AlignLeft, tcell.ColorWhite)
frame.AddText("Waiting for cardputer...", false, tview.AlignLeft, tcell.ColorWhite)
return frame return frame
} }
func UpdateDeviceStatus(app *tview.Application, frame *tview.Frame, status string, color tcell.Color) { func UpdateDeviceStatus(app *tview.Application, frame *tview.Frame, state *UIState, status string, color tcell.Color) {
app.QueueUpdateDraw(func() { app.QueueUpdateDraw(func() {
frame.Clear() frame.Clear()
frame.AddText("Parchment - v0.0.1", true, tview.AlignLeft, tcell.ColorWhite) switch state.Page {
case "home":
frame.AddText("Parchment - v0.0.1", true, tview.AlignLeft, tcell.ColorWhite)
case "package":
frame.AddText("Package Tools", true, tview.AlignLeft, tcell.ColorWhite)
}
frame.AddText(status, false, tview.AlignLeft, color) frame.AddText(status, false, tview.AlignLeft, color)
}) })
} }