Merge pull request #7 from prashantgupta24/refactor-1

adding notification for permission issue
This commit is contained in:
Prashant Gupta 2019-03-28 15:06:28 -07:00 committed by GitHub
commit d75c5b471d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 27 additions and 19 deletions

View File

@ -7,4 +7,10 @@ Ever felt the need to keep your machine awake without having to resort to the ag
> Introducing the simplest app on the market that has the sole purpose of moving your mouse pointer at regular intervals so that your machine never sleeps! And best of all, it works **ONLY** when you are not working, so be rest assured that the mouse won't start moving on its own without the machine actually being idle. > Introducing the simplest app on the market that has the sole purpose of moving your mouse pointer at regular intervals so that your machine never sleeps! And best of all, it works **ONLY** when you are not working, so be rest assured that the mouse won't start moving on its own without the machine actually being idle.
## Error while moving the app
In case you get an error from the app saying `mouse pointer cannot be moved.`, you need to give the app permission to control your mouse. Don't worry, it's nothing sinister, but Mac doesn't allow apps to gain accessibility to the computer by default (even standard apps like Automator, Firefox etc. who might want to access some features need to go through the same process)
In order to resolve this error you need to:
> Go to Security & Privacy -> Privacy -> Accessibility and allow the `amm` app to gain access.

View File

@ -8,6 +8,7 @@ import (
"github.com/go-vgo/robotgo" "github.com/go-vgo/robotgo"
"github.com/prashantgupta24/activity-tracker/pkg/tracker" "github.com/prashantgupta24/activity-tracker/pkg/tracker"
"github.com/prashantgupta24/automatic-mouse-mover/pkg/notify"
) )
var instance *MouseMover var instance *MouseMover
@ -45,13 +46,17 @@ func (m *MouseMover) Start() {
select { select {
case heartbeat := <-heartbeatCh: case heartbeat := <-heartbeatCh:
if !heartbeat.WasAnyActivity { if !heartbeat.WasAnyActivity {
commCh := make(chan bool) mouseMoveSuccessCh := make(chan bool)
go moveMouse(movePixel, commCh) go moveAndCheck(movePixel, mouseMoveSuccessCh)
select { select {
case wasMouseMoveSuccess := <-commCh: case wasMouseMoveSuccess := <-mouseMoveSuccessCh:
if wasMouseMoveSuccess { if wasMouseMoveSuccess {
log.Infof("moving mouse at : %v\n\n", time.Now()) log.Infof("moved mouse at : %v\n\n", time.Now())
movePixel *= -1 movePixel *= -1
} else {
msg := "Mouse pointer cannot be moved. See README for details."
log.Errorf(msg)
notify.SendMessage(msg)
} }
case <-time.After(timeout * time.Millisecond): case <-time.After(timeout * time.Millisecond):
//timeout, do nothing //timeout, do nothing
@ -80,12 +85,20 @@ func (m *MouseMover) updateRunningStatus(isRunning bool) {
m.runningStatus = isRunning m.runningStatus = isRunning
} }
func moveMouse(movePixel int, commCh chan bool) { func moveAndCheck(movePixel int, mouseMoveSuccessCh chan bool) {
currentX, currentY := robotgo.GetMousePos() currentX, currentY := robotgo.GetMousePos()
moveToX := currentX + movePixel moveToX := currentX + movePixel
moveToY := currentY + movePixel moveToY := currentY + movePixel
robotgo.MoveMouse(moveToX, moveToY) robotgo.MoveMouse(moveToX, moveToY)
commCh <- true
//check if mouse moved. Sometimes mac users need to give
//extra permission for controlling the mouse
movedX, movedY := robotgo.GetMousePos()
if movedX == currentX && movedY == currentY {
mouseMoveSuccessCh <- false
} else {
mouseMoveSuccessCh <- true
}
} }
//Quit the app //Quit the app

View File

@ -1,11 +1,9 @@
package mousemover package mousemover
import ( import (
"math"
"testing" "testing"
"time" "time"
"github.com/go-vgo/robotgo"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite" "github.com/stretchr/testify/suite"
) )
@ -44,12 +42,3 @@ func (suite *TestMover) TestAppStartAndStop() {
time.Sleep(time.Millisecond * 500) //wait for app to stop time.Sleep(time.Millisecond * 500) //wait for app to stop
assert.False(t, mouseMover.isRunning(), "app should have stopped") assert.False(t, mouseMover.isRunning(), "app should have stopped")
} }
func (suite *TestMover) TestMouseMove() {
t := suite.T()
movePixel := 10
currentX, _ := robotgo.GetMousePos()
commCh := make(chan bool, 1)
moveMouse(movePixel, commCh)
movedX, _ := robotgo.GetMousePos()
assert.Equal(t, float64(movePixel), math.Abs(float64(movedX-currentX)))
}

View File

@ -1,7 +1,7 @@
package notify package notify
import ( import (
"fmt" log "github.com/sirupsen/logrus"
gosxnotifier "github.com/deckarep/gosx-notifier" gosxnotifier "github.com/deckarep/gosx-notifier"
) )
@ -11,6 +11,6 @@ func SendMessage(msg string) {
note := gosxnotifier.NewNotification(msg) note := gosxnotifier.NewNotification(msg)
err := note.Push() err := note.Push()
if err != nil { if err != nil {
fmt.Println("could not send notification") log.Error("could not send")
} }
} }