2019-03-13 23:57:03 +00:00
package main
import (
2023-08-29 12:33:05 +00:00
"bytes"
2023-08-28 14:33:01 +00:00
"encoding/json"
2023-08-29 12:33:05 +00:00
"image"
"image/color"
"image/png"
2023-08-28 14:33:01 +00:00
"os"
"path/filepath"
2019-03-13 23:57:03 +00:00
"github.com/getlantern/systray"
2023-08-28 14:33:01 +00:00
"github.com/go-vgo/robotgo"
"github.com/kirsle/configdir"
2019-03-20 19:44:51 +00:00
"github.com/prashantgupta24/automatic-mouse-mover/pkg/mousemover"
2023-08-28 14:33:01 +00:00
log "github.com/sirupsen/logrus"
2019-03-13 23:57:03 +00:00
)
2023-08-28 14:33:01 +00:00
type AppSettings struct {
Icon string ` json:"icon" `
}
var configPath = configdir . LocalConfig ( "amm" )
var configFile = filepath . Join ( configPath , "settings.json" )
2019-03-13 23:57:03 +00:00
func main ( ) {
systray . Run ( onReady , onExit )
}
2023-08-29 12:33:05 +00:00
func getIcon ( iconName string , active bool ) [ ] byte {
if iconName != "mouse" && iconName != "cloud" && iconName != "geometric" && iconName != "man" {
iconName = "mouse"
2023-08-28 14:33:01 +00:00
}
2023-08-29 13:34:14 +00:00
var b [ ] byte
var err error
ex , _ := os . Executable ( )
exPath := filepath . Dir ( ex )
if _ , err := os . Stat ( exPath + "/../Resources/assets/icon" ) ; os . IsNotExist ( err ) {
b , err = os . ReadFile ( exPath + "/../assets/icon/" + iconName + ".png" )
} else {
b , err = os . ReadFile ( exPath + "/../Resources/assets/icon/" + iconName + ".png" )
}
2023-08-29 12:33:05 +00:00
if err != nil {
panic ( err )
}
if active {
img , err := png . Decode ( bytes . NewReader ( b ) )
if err != nil {
log . Fatalln ( err )
}
var dimg * image . RGBA = image . NewRGBA ( img . Bounds ( ) )
for y := img . Bounds ( ) . Min . Y ; y < img . Bounds ( ) . Max . Y ; y ++ {
for x := img . Bounds ( ) . Min . X ; x < img . Bounds ( ) . Max . X ; x ++ {
r , g , b , a := img . At ( x , y ) . RGBA ( )
if a != 0 {
dimg . Set ( x , y , color . RGBA { 30 , 144 , 255 , 255 } )
} else {
dimg . Set ( x , y , color . RGBA { uint8 ( r ) , uint8 ( g ) , uint8 ( b ) , uint8 ( a ) } )
}
}
}
var c bytes . Buffer
png . Encode ( & c , dimg )
return c . Bytes ( )
}
return b
}
func setIcon ( iconName string , configFile string , settings * AppSettings , active ... bool ) {
systray . SetIcon ( getIcon ( iconName , len ( active ) != 0 && active [ 0 ] ) )
2023-08-28 14:33:01 +00:00
if configFile != "" {
2023-08-29 12:33:05 +00:00
* settings = AppSettings { iconName }
2023-08-28 14:33:01 +00:00
fh , _ := os . Create ( configFile )
defer fh . Close ( )
encoder := json . NewEncoder ( fh )
encoder . Encode ( settings )
}
}
2019-03-13 23:57:03 +00:00
func onReady ( ) {
go func ( ) {
2023-08-28 14:33:01 +00:00
err := configdir . MakePath ( configPath )
if err != nil {
panic ( err )
}
var settings AppSettings
settings = AppSettings { "mouse" }
if _ , err = os . Stat ( configFile ) ; os . IsNotExist ( err ) {
fh , err := os . Create ( configFile )
if err != nil {
panic ( err )
}
defer fh . Close ( )
encoder := json . NewEncoder ( fh )
encoder . Encode ( settings )
} else {
fh , err := os . Open ( configFile )
if err != nil {
panic ( err )
}
defer fh . Close ( )
decoder := json . NewDecoder ( fh )
decoder . Decode ( & settings )
}
2021-08-19 19:01:27 +00:00
about := systray . AddMenuItem ( "About AMM" , "Information about the app" )
systray . AddSeparator ( )
2019-03-13 23:57:03 +00:00
ammStart := systray . AddMenuItem ( "Start" , "start the app" )
2019-03-20 19:44:51 +00:00
ammStop := systray . AddMenuItem ( "Stop" , "stop the app" )
2023-08-28 14:33:01 +00:00
icons := systray . AddMenuItem ( "Icons" , "icon of the app" )
mouse := icons . AddSubMenuItem ( "Mouse" , "Mouse icon" )
2023-08-29 12:33:05 +00:00
mouse . SetIcon ( getIcon ( "mouse" , false ) )
2023-08-28 14:33:01 +00:00
cloud := icons . AddSubMenuItem ( "Cloud" , "Cloud icon" )
2023-08-29 12:33:05 +00:00
cloud . SetIcon ( getIcon ( "cloud" , false ) )
2023-08-28 14:33:01 +00:00
man := icons . AddSubMenuItem ( "Man" , "Man icon" )
2023-08-29 12:33:05 +00:00
man . SetIcon ( getIcon ( "man" , false ) )
2023-08-28 14:33:01 +00:00
geometric := icons . AddSubMenuItem ( "Geometric" , "Geometric" )
2023-08-29 12:33:05 +00:00
geometric . SetIcon ( getIcon ( "geometric" , false ) )
2023-08-28 14:33:01 +00:00
2019-03-28 19:52:25 +00:00
ammStop . Disable ( )
2023-08-29 12:33:05 +00:00
setIcon ( settings . Icon , "" , & settings , true )
2019-03-13 23:57:03 +00:00
systray . AddSeparator ( )
mQuit := systray . AddMenuItem ( "Quit" , "Quit the whole app" )
// Sets the icon of a menu item. Only available on Mac.
//mQuit.SetIcon(icon.Data)
2019-03-20 19:44:51 +00:00
mouseMover := mousemover . GetInstance ( )
2021-08-19 19:01:27 +00:00
mouseMover . Start ( )
ammStart . Disable ( )
ammStop . Enable ( )
2023-08-29 12:33:05 +00:00
2019-03-13 23:57:03 +00:00
for {
select {
case <- ammStart . ClickedCh :
2019-03-28 19:52:25 +00:00
log . Infof ( "starting the app" )
2019-03-20 19:44:51 +00:00
mouseMover . Start ( )
2019-03-28 19:52:25 +00:00
ammStart . Disable ( )
ammStop . Enable ( )
2019-03-13 23:57:03 +00:00
//notify.SendMessage("starting the app")
2023-08-29 12:33:05 +00:00
setIcon ( settings . Icon , configFile , & settings , true )
2019-03-13 23:57:03 +00:00
2019-03-20 19:44:51 +00:00
case <- ammStop . ClickedCh :
2019-03-28 19:52:25 +00:00
log . Infof ( "stopping the app" )
ammStart . Enable ( )
ammStop . Disable ( )
2019-03-20 19:44:51 +00:00
mouseMover . Quit ( )
2023-08-29 12:33:05 +00:00
setIcon ( settings . Icon , configFile , & settings , false )
2019-03-13 23:57:03 +00:00
case <- mQuit . ClickedCh :
2019-03-28 19:52:25 +00:00
log . Infof ( "Requesting quit" )
2019-03-20 19:44:51 +00:00
mouseMover . Quit ( )
2019-03-13 23:57:03 +00:00
systray . Quit ( )
return
2023-08-28 14:33:01 +00:00
case <- mouse . ClickedCh :
2023-08-29 12:33:05 +00:00
setIcon ( "mouse" , configFile , & settings , ammStart . Disabled ( ) )
2023-08-28 14:33:01 +00:00
case <- cloud . ClickedCh :
2023-08-29 12:33:05 +00:00
setIcon ( "cloud" , configFile , & settings , ammStart . Disabled ( ) )
2023-08-28 14:33:01 +00:00
case <- man . ClickedCh :
2023-08-29 12:33:05 +00:00
setIcon ( "man" , configFile , & settings , ammStart . Disabled ( ) )
2023-08-28 14:33:01 +00:00
case <- geometric . ClickedCh :
2023-08-29 12:33:05 +00:00
setIcon ( "geometric" , configFile , & settings , ammStart . Disabled ( ) )
2021-08-19 19:01:27 +00:00
case <- about . ClickedCh :
log . Infof ( "Requesting about" )
2023-08-28 14:33:01 +00:00
robotgo . Alert ( "Automatic-mouse-mover app v1.2.0" , "Developed by Prashant Gupta. \n\nMore info at: https://github.com/prashantgupta24/automatic-mouse-mover" , "OK" , "" )
2019-03-13 23:57:03 +00:00
}
}
} ( )
}
func onExit ( ) {
// clean up here
2019-03-28 19:52:25 +00:00
log . Infof ( "Finished quitting" )
2019-03-13 23:57:03 +00:00
}