forked from Elara6331/itd
Rewrite itgui and add new screenshots
This commit is contained in:
@@ -2,109 +2,61 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"image/color"
|
||||
"strconv"
|
||||
"fmt"
|
||||
|
||||
"fyne.io/fyne/v2"
|
||||
"fyne.io/fyne/v2/canvas"
|
||||
"fyne.io/fyne/v2/container"
|
||||
"fyne.io/fyne/v2/theme"
|
||||
"fyne.io/fyne/v2/widget"
|
||||
"go.arsenm.dev/itd/api"
|
||||
)
|
||||
|
||||
func motionTab(parent fyne.Window, client *api.Client) *fyne.Container {
|
||||
// Create label for heart rate
|
||||
xCoordLbl := newText("0", 24)
|
||||
// Creae container to store heart rate section
|
||||
xCoordSect := container.NewVBox(
|
||||
newText("X Coordinate", 12),
|
||||
xCoordLbl,
|
||||
canvas.NewLine(theme.ShadowColor()),
|
||||
)
|
||||
func motionTab(ctx context.Context, client *api.Client, w fyne.Window) fyne.CanvasObject {
|
||||
// Create titledText for each coordinate
|
||||
xText := newTitledText("X Coordinate", "0")
|
||||
yText := newTitledText("Y Coordinate", "0")
|
||||
zText := newTitledText("Z Coordinate", "0")
|
||||
|
||||
// Create label for heart rate
|
||||
yCoordLbl := newText("0", 24)
|
||||
// Creae container to store heart rate section
|
||||
yCoordSect := container.NewVBox(
|
||||
newText("Y Coordinate", 12),
|
||||
yCoordLbl,
|
||||
canvas.NewLine(theme.ShadowColor()),
|
||||
)
|
||||
// Create label for heart rate
|
||||
zCoordLbl := newText("0", 24)
|
||||
// Creae container to store heart rate section
|
||||
zCoordSect := container.NewVBox(
|
||||
newText("Z Coordinate", 12),
|
||||
zCoordLbl,
|
||||
canvas.NewLine(theme.ShadowColor()),
|
||||
)
|
||||
var ctxCancel func()
|
||||
|
||||
// Create variable to keep track of whether motion started
|
||||
started := false
|
||||
|
||||
// Create button to stop motion
|
||||
stopBtn := widget.NewButton("Stop", nil)
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
onClose = append(onClose, cancel)
|
||||
|
||||
// Create button to start motion
|
||||
startBtn := widget.NewButton("Start", func() {
|
||||
// if motion is started
|
||||
if started {
|
||||
// Do nothing
|
||||
return
|
||||
}
|
||||
// Set motion started
|
||||
started = true
|
||||
// Watch motion values
|
||||
motionCh, err := client.WatchMotion(ctx)
|
||||
if err != nil {
|
||||
guiErr(err, "Error getting heart rate channel", true, parent)
|
||||
}
|
||||
// Create done channel
|
||||
done := make(chan struct{}, 1)
|
||||
go func() {
|
||||
for {
|
||||
select {
|
||||
case <-done:
|
||||
return
|
||||
case motion := <-motionCh:
|
||||
// Set labels to new values
|
||||
xCoordLbl.Text = strconv.Itoa(int(motion.X))
|
||||
yCoordLbl.Text = strconv.Itoa(int(motion.Y))
|
||||
zCoordLbl.Text = strconv.Itoa(int(motion.Z))
|
||||
// Refresh labels to display new values
|
||||
xCoordLbl.Refresh()
|
||||
yCoordLbl.Refresh()
|
||||
zCoordLbl.Refresh()
|
||||
}
|
||||
// Create start button
|
||||
toggleBtn := widget.NewButton("Start", nil)
|
||||
// Set button's on tapped callback
|
||||
toggleBtn.OnTapped = func() {
|
||||
switch toggleBtn.Text {
|
||||
case "Start":
|
||||
// Create new context for motion
|
||||
motionCtx, cancel := context.WithCancel(ctx)
|
||||
// Set ctxCancel to function so that stop button can run it
|
||||
ctxCancel = cancel
|
||||
// Watch motion
|
||||
motionCh, err := client.WatchMotion(motionCtx)
|
||||
if err != nil {
|
||||
guiErr(err, "Error watching motion", false, w)
|
||||
return
|
||||
}
|
||||
}()
|
||||
// Create stop function
|
||||
stopBtn.OnTapped = func() {
|
||||
done <- struct{}{}
|
||||
started = false
|
||||
cancel()
|
||||
go func() {
|
||||
// For every motion event
|
||||
for motion := range motionCh {
|
||||
// Set coordinates
|
||||
xText.SetBody(fmt.Sprint(motion.X))
|
||||
yText.SetBody(fmt.Sprint(motion.Y))
|
||||
zText.SetBody(fmt.Sprint(motion.Z))
|
||||
}
|
||||
}()
|
||||
// Set button text to "Stop"
|
||||
toggleBtn.SetText("Stop")
|
||||
case "Stop":
|
||||
// Cancel motion context
|
||||
ctxCancel()
|
||||
// Set button text to "Start"
|
||||
toggleBtn.SetText("Start")
|
||||
}
|
||||
}
|
||||
|
||||
})
|
||||
// Run stop button function on close if possible
|
||||
onClose = append(onClose, func() {
|
||||
if stopBtn.OnTapped != nil {
|
||||
stopBtn.OnTapped()
|
||||
}
|
||||
})
|
||||
|
||||
// Return new container containing all elements
|
||||
return container.NewVBox(
|
||||
// Add rectangle for a bit of padding
|
||||
canvas.NewRectangle(color.Transparent),
|
||||
startBtn,
|
||||
stopBtn,
|
||||
xCoordSect,
|
||||
yCoordSect,
|
||||
zCoordSect,
|
||||
)
|
||||
return container.NewVScroll(container.NewVBox(
|
||||
toggleBtn,
|
||||
xText,
|
||||
yText,
|
||||
zText,
|
||||
))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user