backive/ui/ui_main.go

172 lines
3.3 KiB
Go
Raw Normal View History

2022-03-07 23:24:02 +01:00
package ui
import (
2022-06-07 21:20:16 +02:00
"encoding/json"
2022-03-12 23:08:57 +01:00
"fmt"
2022-06-07 21:20:16 +02:00
"net"
"strconv"
"time"
2022-03-12 23:08:57 +01:00
2022-03-07 23:24:02 +01:00
"fyne.io/fyne/v2"
2022-03-17 20:56:24 +01:00
"fyne.io/fyne/v2/driver/desktop"
2022-03-17 21:01:26 +01:00
"fyne.io/fyne/v2/theme"
2022-03-07 23:24:02 +01:00
"github.com/qwc/backive"
)
type UISettings struct {
hideUntil time.Time
globalLevel int
}
2022-03-07 23:24:02 +01:00
var (
app fyne.App
window fyne.Window
config backive.Configuration
db backive.Database
doNotShowUntil time.Time = time.Unix(0, 0)
c net.Conn
uisettings UISettings
messageLevel int
2022-03-07 23:24:02 +01:00
)
2022-06-07 21:20:16 +02:00
func Init(a fyne.App, w fyne.Window, conf backive.Configuration, d backive.Database) {
2022-03-07 23:24:02 +01:00
app = a
2022-03-17 20:56:24 +01:00
a.SetIcon(theme.FyneLogo())
makeTray(app)
2022-06-07 21:20:16 +02:00
config = conf
2022-03-07 23:24:02 +01:00
db = d
2022-06-07 21:20:16 +02:00
go PollConnection()
2022-03-07 23:24:02 +01:00
}
2022-06-07 21:20:16 +02:00
func PollConnection() {
var err error
for {
if c == nil {
c, err = net.Dial("unix", config.Settings.UIUnixSocketLocation)
} else {
err = fmt.Errorf("Connection already established")
}
if err != nil {
// ignore
err = nil
// sleep a while and then retry
time.Sleep(10 * time.Second)
}
}
}
func NotificationRun() {
2022-06-07 21:20:16 +02:00
if c != nil {
b := make([]byte, 2048)
i, err := c.Read(b)
if err == nil && i > 0 {
var data map[string]string
err = json.Unmarshal(b, &data)
if err == nil {
ShowNotification(data)
}
// else ignore and try to read again
err = nil
}
// we just try again and discard the error
err = nil
}
}
2022-06-07 21:20:16 +02:00
func ShowNotification(data map[string]string) {
if ShallShow(data) {
app.SendNotification(
fyne.NewNotification(
2022-06-07 21:20:16 +02:00
data["header"],
data["message"],
),
)
}
}
2022-06-07 21:20:16 +02:00
func ShallShow(data map[string]string) bool {
level, err := strconv.ParseUint(data["level"], 10, 64)
if err != nil {
return false
}
if level <= 10 {
return true
}
if int(level) <= uisettings.globalLevel && messageLevel > 0 {
return false
}
if int(level) <= uisettings.globalLevel {
return true
}
return false
}
func SetHideUntil(until time.Time) {
uisettings.hideUntil = until
}
func SetMessageLevel(level int) {
if level <= 10 {
messageLevel = level
} else {
uisettings.globalLevel = level
messageLevel = 0
}
}
func SaveSettings() {
// save internal settings to file in homedir
}
func LoadSettings() {
// load settings
}
2022-03-17 20:56:24 +01:00
func makeTray(app fyne.App) {
if desk, ok := app.(desktop.App); ok {
hideReminders := fyne.NewMenuItem(
"Hide Reminders for",
nil,
)
hideReminders.ChildMenu = fyne.NewMenu(
"",
fyne.NewMenuItem("Hide reminder notifications for today", func() {
doNotShowUntil = time.Now().AddDate(0, 0, 1)
SetHideUntil(doNotShowUntil)
}),
fyne.NewMenuItem("Hide reminder notifications for a hour", func() {
doNotShowUntil = time.Now().Add(time.Hour)
SetHideUntil(doNotShowUntil)
}),
)
levelMenu := fyne.NewMenuItem(
"Set notification level", nil,
)
levelMenu.ChildMenu = fyne.NewMenu(
"",
fyne.NewMenuItem(
"Only problems and tasks finished (resets to default with restart)",
func() { SetMessageLevel(10) },
),
fyne.NewMenuItem(
"+ Reminders (default)",
func() { SetMessageLevel(20) },
),
fyne.NewMenuItem(
"+ Informational messages",
func() { SetMessageLevel(30) },
),
fyne.NewMenuItem(
"+ Verbose/Debug messages",
func() { SetMessageLevel(40) },
),
)
menu := fyne.NewMenu(
"backive",
hideReminders,
levelMenu,
)
2022-03-17 20:56:24 +01:00
desk.SetSystemTrayMenu(menu)
}
}