Skip to content

Commit

Permalink
v3.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
pidgy committed May 30, 2024
1 parent fd89d25 commit 7d1faf4
Show file tree
Hide file tree
Showing 13 changed files with 279 additions and 154 deletions.
2 changes: 1 addition & 1 deletion core/notify/notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func LastNStrings(n int) (s []string) {
if feed.logs[i].count > 1 {
continue
}
s = append([]string{feed.logs[i].msg}, s...)
s = append(s, feed.logs[i].msg)
}
return
}
Expand Down
2 changes: 1 addition & 1 deletion global/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
const (
Title = "UniteHUD"
Version = "v" + VersionNoV
VersionNoV = "3.3.0"
VersionNoV = "3.4.0"
TitleVersion = Title + " " + Version
AssetDirectory = `assets`
)
Expand Down
5 changes: 3 additions & 2 deletions gui/ui/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"github.com/pidgy/unitehud/gui/ux/button"
"github.com/pidgy/unitehud/gui/ux/decorate"
"github.com/pidgy/unitehud/gui/ux/title"
"github.com/pidgy/unitehud/system/process"
)

type footer struct {
Expand Down Expand Up @@ -153,8 +154,8 @@ func (g *GUI) configure() {

decorate.Background(gtx)
decorate.Label(&ui.footer.api, "API: %s", device.APIHumanName(device.API(config.Current.Video.Capture.Device.API)))
decorate.Label(&ui.footer.cpu, g.performance.cpu)
decorate.Label(&ui.footer.ram, g.performance.ram)
decorate.Label(&ui.footer.cpu, process.CPU.String())
decorate.Label(&ui.footer.ram, process.RAM.String())
decorate.Label(&ui.footer.hz, "%s Hz", g.hz)
decorate.Label(&ui.footer.fps, "%.0f FPS", fps)
decorate.LabelColor(&ui.footer.fps, nrgba.Percent(p).Color())
Expand Down
28 changes: 8 additions & 20 deletions gui/ui/gui.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ type GUI struct {
}

performance struct {
cpu, ram, uptime string
eco bool
uptime string
eco bool
}

previous struct {
Expand Down Expand Up @@ -119,14 +119,10 @@ func New() *GUI {
hz: fps.NewHz(),

performance: struct {
cpu,
ram,
uptime string

eco bool
}{
cpu: "0%",
ram: "0MB",
uptime: "00:00",

eco: true,
Expand Down Expand Up @@ -277,25 +273,17 @@ func (g *GUI) proc() {
for ; is.Now != is.Closing; time.Sleep(time.Second) {
g.performance.uptime = process.Uptime()

ram := process.RAM()
if ram > peak.ram+100 {
peak.ram = ram
if process.RAM.Float64() > peak.ram+100 {
peak.ram = process.RAM.Float64()
notify.Replace("[UI] RAM", notify.Warn, "[UI] RAM Usage: %.0fMB", peak.ram)
}
g.performance.ram = fmt.Sprintf("RAM %.0f%s", ram, "MB")
go stats.RAM(ram)
go stats.RAM(process.RAM.Float64())

cpu, err := process.CPU()
if err != nil {
notify.Error("[Process] Failed to monitor CPU/RAM (%v)", err)
continue
}
if cpu > peak.cpu+10 {
peak.cpu = cpu
if process.CPU.Float64() > peak.cpu+10 {
peak.cpu = process.CPU.Float64()
notify.Replace("[UI] CPU Usage", notify.Warn, "[UI] CPU Usage: %.1f%s", peak.cpu, "%")
}
g.performance.cpu = fmt.Sprintf("CPU %.1f%s", cpu, "%")
go stats.CPU(cpu)
go stats.CPU(process.CPU.Float64())
}
}

Expand Down
8 changes: 4 additions & 4 deletions gui/ui/loading.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"github.com/pidgy/unitehud/gui/is"
"github.com/pidgy/unitehud/gui/ux/decorate"
"github.com/pidgy/unitehud/system/tray"
"github.com/pidgy/unitehud/system/wapi"
)

type loading struct {
Expand Down Expand Up @@ -67,9 +66,10 @@ func (g *GUI) loading() {
for is.Now == is.Loading {
switch event := g.window.NextEvent().(type) {
case app.ViewEvent:
g.HWND = event.HWND
tray.SetHWND(g.HWND)
wapi.SetWindowDarkMode(g.HWND)
if event.HWND != 0 {
g.HWND = event.HWND
tray.SetHWND(g.HWND)
}
case system.DestroyEvent:
g.next(is.Closing)
return
Expand Down
5 changes: 3 additions & 2 deletions gui/ui/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import (
"github.com/pidgy/unitehud/system/desktop"
"github.com/pidgy/unitehud/system/desktop/clicked"
"github.com/pidgy/unitehud/system/discord"
"github.com/pidgy/unitehud/system/process"
"github.com/pidgy/unitehud/system/save"
"github.com/pidgy/unitehud/system/tray"
)
Expand Down Expand Up @@ -213,9 +214,9 @@ func (g *GUI) main() {
g.dimensions.size = event.Size

decorate.Background(gtx)
decorate.Label(&ui.labels.cpu, g.performance.cpu)
decorate.Label(&ui.labels.cpu, process.CPU.String())
decorate.Label(&ui.labels.cpuGraph, stats.CPUGraph())
decorate.Label(&ui.labels.ram, g.performance.ram)
decorate.Label(&ui.labels.ram, process.RAM.String())
decorate.Label(&ui.labels.ramGraph, stats.RAMGraph())
decorate.Label(&ui.labels.holding, ui.labels.holding.Text)
decorate.ForegroundAlt(&ui.labels.cpuGraph.Color)
Expand Down
80 changes: 49 additions & 31 deletions system/process/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"golang.org/x/sys/windows"

"github.com/pidgy/unitehud/core/notify"
"github.com/pidgy/unitehud/global"
)

Expand All @@ -23,42 +24,18 @@ type Process struct {
Exe string
}

type Stat struct {
value float64
label string
}

var (
handle syscall.Handle
memory runtime.MemStats

ctime, etime, ktime, utime syscall.Filetime
prev, usage = ctime.Nanoseconds(), ktime.Nanoseconds() + utime.Nanoseconds()
cpus = float64(runtime.NumCPU()) - 2
CPU, RAM = Stat{0, "CPU 0%"}, Stat{0, "RAM 0MB"}
)

func CPU() (float64, error) {
err := syscall.GetProcessTimes(handle, &ctime, &etime, &ktime, &utime)
if err != nil {
return 0.0, err
}

now := time.Now().UnixNano()
diff := now - prev

current := ktime.Nanoseconds() + utime.Nanoseconds()
diff2 := current - usage

prev = now
usage = current

return (100 * float64(diff2) / float64(diff)) / cpus, nil
}

func Memory() runtime.MemStats {
return memory
}

func RAM() float64 {
memory = runtime.MemStats{}
runtime.ReadMemStats(&memory)
return float64(memory.Sys) / 1024 / 1024
}
func init() { go poll() }

func Start() error {
err := replace()
Expand All @@ -74,6 +51,14 @@ func Start() error {
return nil
}

func (s *Stat) String() string {
return s.label
}

func (s *Stat) Float64() float64 {
return s.value
}

func Uptime() string {
u := time.Time{}.Add(time.Since(global.Uptime))
return fmt.Sprintf("%02d:%02d:%02d", u.Hour(), u.Minute(), u.Second())
Expand Down Expand Up @@ -148,6 +133,39 @@ func kill(exe string) error {
return nil
}

func poll() {
cpus := float64(runtime.NumCPU()) - 2
prev, usage := int64(0), int64(0)

t := time.NewTicker(time.Second * 5)
for range t.C {
var ctime, etime, ktime, utime syscall.Filetime
err := syscall.GetProcessTimes(handle, &ctime, &etime, &ktime, &utime)
if err != nil {
notify.Error("[Process] Failed to poll process statistics (%v)", err)
return
}

now := time.Now().UnixNano()
diff := now - prev

current := ktime.Nanoseconds() + utime.Nanoseconds()
diff2 := current - usage

prev = now
usage = current

CPU.value = ((100 * float64(diff2) / float64(diff)) / cpus)
CPU.label = fmt.Sprintf("CPU %.1f%s", CPU.value, "%")

memory := runtime.MemStats{}
runtime.ReadMemStats(&memory)

RAM.value = float64(memory.Sys) / 1024 / 1024
RAM.label = fmt.Sprintf("RAM %.1fMB", RAM.value)
}
}

func replace() error {
for _, exe := range []string{"UniteHUD.exe", "UniteHUD_Debug.exe"} {
err := kill(path.Base(exe))
Expand Down
75 changes: 56 additions & 19 deletions system/tray/tray.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package tray

import (
"os"
"time"

"github.com/rupor-github/win-gpg-agent/systray"
"github.com/skratchdot/open-golang/open"

"github.com/pidgy/unitehud/avi/img"
"github.com/pidgy/unitehud/core/notify"
"github.com/pidgy/unitehud/system/process"
"github.com/pidgy/unitehud/system/wapi"
)

Expand All @@ -23,10 +25,11 @@ var (
var menu = struct {
visible bool

header,
website,
startstop,
quit toggle
header toggle
website toggle
startstop toggle
cpu, ram toggle
quit toggle

startstopq chan bool
errorq chan error
Expand All @@ -51,10 +54,20 @@ func Open(title, version string, exit func()) error {

go systray.Run(func() {
menu.header = header(title, version)
menu.cpu = cpu()
menu.ram = ram()
menu.website = website()
menu.startstop = startstop()

menu.quit = quit()

go func() {
for ; ; time.Sleep(time.Second) {
menu.cpu.event()
menu.ram.event()
}
}()

menu.errorq <- nil

menu.visible = true
Expand Down Expand Up @@ -96,25 +109,16 @@ func StartStopEvent() bool {
}
}

func quit() toggle {
notify.Debug("[Tray] Adding Quit")
func cpu() toggle {
notify.Debug("[Tray] Adding CPU")

systray.AddSeparator()

return toggle{
MenuItem: systray.AddMenuItem("Quit UniteHUD", "Close UniteHUD"),
event: func() { os.Exit(0) },
}
}

func startstop() toggle {
notify.Debug("[Tray] Adding Start/Stop")

systray.AddSeparator()

return toggle{
MenuItem: systray.AddMenuItem("Start", "Start capturing events"),
event: func() { menu.startstopq <- true },
MenuItem: systray.AddMenuItem("CPU 0%", "CPU"),
event: func() {
menu.cpu.SetTitle(process.CPU.String())
},
}
}

Expand All @@ -137,6 +141,39 @@ func header(title, version string) toggle {
}
}

func quit() toggle {
notify.Debug("[Tray] Adding Quit")

systray.AddSeparator()

return toggle{
MenuItem: systray.AddMenuItem("Quit UniteHUD", "Close UniteHUD"),
event: func() { os.Exit(0) },
}
}

func ram() toggle {
notify.Debug("[Tray] Adding RAM")

return toggle{
MenuItem: systray.AddMenuItem("RAM 0MB", "RAM"),
event: func() {
menu.ram.SetTitle(process.RAM.String())
},
}
}

func startstop() toggle {
notify.Debug("[Tray] Adding Start/Stop")

systray.AddSeparator()

return toggle{
MenuItem: systray.AddMenuItem("Start", "Start capturing events"),
event: func() { menu.startstopq <- true },
}
}

func website() toggle {
notify.Debug("[Tray] Adding Website")

Expand Down
9 changes: 8 additions & 1 deletion system/wapi/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,10 @@ func ObjectSelect(hwnd1, hwnd2 uintptr) {
}

func RaiseWindow(hwnd uintptr) {
go BringWindowToTop.Call(hwnd)
ShowWindow.Call(hwnd, ShowWindowFlags.ShowMinimized)
ShowWindow.Call(hwnd, ShowWindowFlags.Restore)

// go BringWindowToTop.Call(hwnd)
}

func SetWindowDarkMode(hwnd uintptr) {
Expand All @@ -153,6 +156,10 @@ func SetWindowPosNoSize(hwnd uintptr, pt image.Point) {
helpSetWindowPos(hwnd, pt, image.Pt(0, 0), SetWindowPosFlags.NoSize)
}

func SetWindowPosNoSizeNoMoveShowWindow(hwnd uintptr) {
helpSetWindowPos(hwnd, image.Pt(0, 0), image.Pt(0, 0), SetWindowPosFlags.NoSize|SetWindowPosFlags.NoMove|SetWindowPosFlags.ShowWindow)
}

func SetWindowPosHide(hwnd uintptr, pt image.Point, size image.Point) {
helpSetWindowPos(hwnd, pt, size, SetWindowPosFlags.Hide)
}
Expand Down
Loading

0 comments on commit 7d1faf4

Please sign in to comment.