restructured files

This commit is contained in:
2025-10-29 17:40:54 +01:00
parent 7d978177f5
commit dc65776a91
8 changed files with 156 additions and 134 deletions

1
app/app.go Normal file
View File

@@ -0,0 +1 @@
package app

View File

@@ -1,4 +1,4 @@
package main
package app
import (
"fmt"
@@ -77,3 +77,12 @@ func readBattery(rootPath string) (BatteryInfo, error) {
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
View 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
View 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
View 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
}

7
go.mod
View File

@@ -1,11 +1,8 @@
module main
module nomi
go 1.25.2
require (
github.com/mattn/go-gtk v0.0.0-20240119050609-48574e312fac
golang.org/x/sys v0.37.0
)
require github.com/mattn/go-gtk v0.0.0-20240119050609-48574e312fac
require (
github.com/mattn/go-pointer v0.0.1 // indirect

2
go.sum
View File

@@ -8,7 +8,5 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
golang.org/x/sys v0.37.0 h1:fdNQudmxPjkdUTPnLn5mdQv7Zwvbvpaxqs831goi9kQ=
golang.org/x/sys v0.37.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

128
main.go
View File

@@ -1,131 +1,7 @@
package main
// #cgo pkg-config: gtk+-2.0
import "C"
import (
"fmt"
"runtime"
"time"
"github.com/mattn/go-gtk/gdk"
"github.com/mattn/go-gtk/glib"
"github.com/mattn/go-gtk/gtk"
)
func screenSizeWithFallback() (int, int) {
w, h := gdk.ScreenWidth(), gdk.ScreenHeight()
if w <= 0 || h <= 0 {
// Kindle Paperwhite 4 portrait as a safe fallback
return 1072, 1448
}
return w, h
}
type App struct {
battery BatteryInfo
batteryLabel *gtk.Label
}
type BatteryInfo struct {
Percent int
Charging bool
Path string
}
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) }
}
import "nomi/app"
func main() {
app := App{}
runtime.LockOSThread()
gtk.Init(nil)
w, h := screenSizeWithFallback()
fmt.Printf("Using 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()
bg.ModifyBG(gtk.STATE_NORMAL, gdk.NewColor("#ffffff"))
win.Add(bg)
// main container
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)
//Scrollable content
scroll := gtk.NewScrolledWindow(nil, nil)
scroll.SetShadowType(gtk.SHADOW_NONE)
scroll.SetPolicy(gtk.POLICY_NEVER, gtk.POLICY_NEVER)
scroll.ModifyBG(gtk.STATE_NORMAL, gdk.NewColor("#ffffff"))
root.PackStart(scroll, true, true, 0)
vp := gtk.NewViewport(nil, nil)
vp.ModifyBG(gtk.STATE_NORMAL, gdk.NewColor("#ffffff"))
scroll.Add(vp)
content := gtk.NewVBox(false, 14)
content.ModifyBG(gtk.STATE_NORMAL, gdk.NewColor("#ffffff"))
vp.Add(content)
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 (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)
app.Run()
}