You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
2.2 KiB
Go

package main
import (
"fmt"
"os/exec"
"strings"
"time"
disabledicon "gitea.ronald1985.uk/ronald1985/Eye-Reminder/icons/disabledicon"
enabledicon "gitea.ronald1985.uk/ronald1985/Eye-Reminder/icons/enabledicon"
"github.com/gen2brain/beeep"
"github.com/getlantern/systray"
ge "github.com/ronaldr1985/graceful-exit"
)
const (
TIME_INBETWEEN_NOTIFICATIONS = 20 * time.Minute
)
func SendNotification(title string, message string) bool {
path, err := exec.LookPath("herbe")
if err == nil {
var args []string
args = append(args, title, message)
cmd := exec.Command(path, args...)
out, err := cmd.CombinedOutput()
if out == nil && err == nil {
return true
}
} else if strings.Contains(err.Error(), "executable file not found in $PATH") {
err := beeep.Notify(title, message, "")
if err != nil {
panic(err)
}
}
return false
}
func systrayOnReady() {
var enabled bool = true
systray.SetTitle("Eye Reminder")
systray.SetTooltip("Eye Reminder")
mQuitOrig := systray.AddMenuItem("Quit", "Quit the whole app")
go func() {
<-mQuitOrig.ClickedCh
fmt.Println("Requesting quit")
systray.Quit()
fmt.Println("Finished quitting")
}()
iconUpdated := false
go func() {
lastTimeNotificationWasSent := time.Now().Add(-20 * time.Minute)
for {
if enabled {
if !iconUpdated {
systray.SetIcon(enabledicon.Data)
iconUpdated = true
}
if time.Since(lastTimeNotificationWasSent) > TIME_INBETWEEN_NOTIFICATIONS {
go func() {
SendNotification("Eye Reminder", "Look away for the screen for atleast 20 seconds")
}()
lastTimeNotificationWasSent = time.Now()
}
} else {
if !iconUpdated {
systray.SetIcon(disabledicon.Data)
iconUpdated = true
}
}
}
}()
go func() {
systray.AddSeparator()
mEnabled := systray.AddMenuItemCheckbox("Enabled", "Check Me", true)
for {
select {
case <-mEnabled.ClickedCh:
if mEnabled.Checked() {
mEnabled.Uncheck()
enabled = false
iconUpdated = false
} else {
mEnabled.Check()
enabled = true
iconUpdated = false
}
}
}
}()
}
func main() {
ge.HandleSignals(false)
systrayOnExit := func() {
}
systray.Run(systrayOnReady, systrayOnExit)
}