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) } } 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("some cool text") 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) }