restructured files
This commit is contained in:
1
app/app.go
Normal file
1
app/app.go
Normal file
@@ -0,0 +1 @@
|
||||
package app
|
||||
88
app/battery.go
Normal file
88
app/battery.go
Normal file
@@ -0,0 +1,88 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/mattn/go-gtk/glib"
|
||||
)
|
||||
|
||||
func initBattery(a *App) {
|
||||
base := "/sys/class/power_supply"
|
||||
entries, err := os.ReadDir(base)
|
||||
if err != nil {
|
||||
fmt.Println("battery init:", err)
|
||||
return
|
||||
}
|
||||
|
||||
var folder string
|
||||
for _, e := range entries {
|
||||
if strings.Contains(strings.ToLower(e.Name()), "bat") {
|
||||
folder = e.Name()
|
||||
break
|
||||
}
|
||||
}
|
||||
if folder == "" {
|
||||
fmt.Println("no battery folder found")
|
||||
return
|
||||
}
|
||||
rootPath := base + "/" + folder
|
||||
|
||||
info, err := readBattery(rootPath)
|
||||
if err != nil {
|
||||
fmt.Println("read battery:", err)
|
||||
return
|
||||
}
|
||||
a.battery = info
|
||||
a.updateBatteryLabel(info)
|
||||
|
||||
go func() {
|
||||
last := info
|
||||
for {
|
||||
cur, err := readBattery(rootPath)
|
||||
if err == nil &&
|
||||
(cur.Percent != last.Percent || cur.Charging != last.Charging) {
|
||||
last = cur
|
||||
a.battery = cur
|
||||
glib.IdleAdd(func() {
|
||||
a.updateBatteryLabel(cur)
|
||||
})
|
||||
}
|
||||
time.Sleep(30 * time.Second)
|
||||
}
|
||||
}()
|
||||
|
||||
}
|
||||
|
||||
func readBattery(rootPath string) (BatteryInfo, error) {
|
||||
val, err := os.ReadFile(rootPath + "/capacity")
|
||||
if err != nil {
|
||||
return BatteryInfo{}, err
|
||||
}
|
||||
status, err := os.ReadFile(rootPath + "/status")
|
||||
if err != nil {
|
||||
return BatteryInfo{}, err
|
||||
}
|
||||
|
||||
p, err := strconv.Atoi(strings.TrimSpace(string(val)))
|
||||
if err != nil {
|
||||
return BatteryInfo{}, err
|
||||
}
|
||||
|
||||
s := strings.ToLower(strings.TrimSpace(string(status)))
|
||||
charging := !strings.Contains(s, "dis")
|
||||
|
||||
return BatteryInfo{Percent: p, Charging: charging, Path: rootPath}, nil
|
||||
}
|
||||
|
||||
func (a *App) updateBatteryLabel(b BatteryInfo) {
|
||||
txt := fmt.Sprintf("%d%%", b.Percent)
|
||||
fmt.Println("changed battery label")
|
||||
if b.Charging {
|
||||
txt += " (charging)"
|
||||
}
|
||||
a.batteryLabel.SetText(txt)
|
||||
}
|
||||
26
app/clock.go
Normal file
26
app/clock.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/mattn/go-gtk/glib"
|
||||
)
|
||||
|
||||
func startMinuteClock(onTick func(now time.Time)) (stop func()) {
|
||||
quit := make(chan struct{})
|
||||
go func() {
|
||||
for {
|
||||
now := time.Now()
|
||||
next := now.Truncate(time.Minute).Add(time.Minute)
|
||||
wait := time.Until(next)
|
||||
select {
|
||||
case <-time.After(wait):
|
||||
n := time.Now()
|
||||
glib.IdleAdd(func() { onTick(n) })
|
||||
case <-quit:
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
return func() { close(quit) }
|
||||
}
|
||||
14
app/types.go
Normal file
14
app/types.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package app
|
||||
|
||||
import "github.com/mattn/go-gtk/gtk"
|
||||
|
||||
type App struct {
|
||||
battery BatteryInfo
|
||||
batteryLabel *gtk.Label
|
||||
}
|
||||
|
||||
type BatteryInfo struct {
|
||||
Percent int
|
||||
Charging bool
|
||||
Path string
|
||||
}
|
||||
101
app/ui.go
Normal file
101
app/ui.go
Normal file
@@ -0,0 +1,101 @@
|
||||
package app
|
||||
|
||||
// #cgo pkg-config: gtk+-2.0
|
||||
|
||||
import "C"
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"runtime"
|
||||
"time"
|
||||
|
||||
"github.com/mattn/go-gtk/gdk"
|
||||
"github.com/mattn/go-gtk/gtk"
|
||||
)
|
||||
|
||||
func isKindle() bool {
|
||||
return runtime.GOARCH == "arm" || runtime.GOARCH == "arm64"
|
||||
}
|
||||
|
||||
func Run() {
|
||||
runtime.LockOSThread()
|
||||
gtk.Init(nil)
|
||||
|
||||
app := App{}
|
||||
w, h := screenSizeWithFallback()
|
||||
fmt.Printf("screen size %dx%d\n", w, h)
|
||||
|
||||
win := gtk.NewWindow(gtk.WINDOW_TOPLEVEL)
|
||||
win.SetTitle("L:A_N:application_PC:TS_ID:com.lab126.store")
|
||||
win.SetDefaultSize(w, h)
|
||||
win.Connect("destroy", gtk.MainQuit)
|
||||
|
||||
bg := gtk.NewEventBox()
|
||||
win.Add(bg)
|
||||
|
||||
root := gtk.NewVBox(false, 0)
|
||||
bg.Add(root)
|
||||
|
||||
// top bar
|
||||
topBar := gtk.NewHBox(false, 8)
|
||||
topBar.SetBorderWidth(6)
|
||||
root.PackStart(topBar, false, false, 0)
|
||||
|
||||
leftStatus := gtk.NewLabel("random info here")
|
||||
topBar.PackStart(leftStatus, false, false, 8)
|
||||
|
||||
app.batteryLabel = gtk.NewLabel("Batt --%")
|
||||
netLabel := gtk.NewLabel("Wi-Fi")
|
||||
timeLabel := gtk.NewLabel(time.Now().Format("15:04"))
|
||||
|
||||
topBar.PackEnd(timeLabel, false, false, 8)
|
||||
topBar.PackEnd(netLabel, false, false, 8)
|
||||
topBar.PackEnd(app.batteryLabel, false, false, 8)
|
||||
|
||||
scroll := gtk.NewScrolledWindow(nil, nil)
|
||||
scroll.SetShadowType(gtk.SHADOW_NONE)
|
||||
scroll.SetPolicy(gtk.POLICY_NEVER, gtk.POLICY_NEVER)
|
||||
root.PackStart(scroll, true, true, 0)
|
||||
|
||||
vp := gtk.NewViewport(nil, nil)
|
||||
scroll.Add(vp)
|
||||
|
||||
content := gtk.NewVBox(false, 14)
|
||||
|
||||
vp.Add(content)
|
||||
|
||||
if isKindle() {
|
||||
bg.ModifyBG(gtk.STATE_NORMAL, gdk.NewColor("#ffffff"))
|
||||
scroll.ModifyBG(gtk.STATE_NORMAL, gdk.NewColor("#ffffff"))
|
||||
vp.ModifyBG(gtk.STATE_NORMAL, gdk.NewColor("#ffffff"))
|
||||
content.ModifyBG(gtk.STATE_NORMAL, gdk.NewColor("#ffffff"))
|
||||
}
|
||||
|
||||
title := gtk.NewLabel("")
|
||||
title.SetMarkup("<span size='20000' weight='bold'>some cool text</span>")
|
||||
content.PackStart(title, false, false, 0)
|
||||
|
||||
body := gtk.NewLabel("aaaa")
|
||||
body.SetLineWrap(true)
|
||||
content.PackStart(body, false, false, 0)
|
||||
|
||||
_ = startMinuteClock(func(now time.Time) {
|
||||
timeLabel.SetText(now.Format("15:04"))
|
||||
})
|
||||
initBattery(&app)
|
||||
|
||||
win.ShowAll()
|
||||
gtk.Main()
|
||||
|
||||
}
|
||||
|
||||
func screenSizeWithFallback() (int, int) {
|
||||
if !isKindle() {
|
||||
return int(math.Round(1072 * 0.8)), int(math.Round(1448 * 0.8))
|
||||
}
|
||||
w, h := gdk.ScreenWidth(), gdk.ScreenHeight()
|
||||
if w <= 0 || h <= 0 {
|
||||
return 1072, 1448
|
||||
}
|
||||
return w, h
|
||||
}
|
||||
Reference in New Issue
Block a user