Skip to content

Commit

Permalink
feat: observe mouse move to update app
Browse files Browse the repository at this point in the history
  • Loading branch information
Innei committed Mar 22, 2024
1 parent ddaf3b3 commit 75de33c
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 3 deletions.
4 changes: 4 additions & 0 deletions ProcessReporter.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
1F6FE2DE2A46F5CA00D646CE /* Reporter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F6FE2DD2A46F5CA00D646CE /* Reporter.swift */; };
1FAAEC482A66702100C086FF /* Application.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1FAAEC472A66702100C086FF /* Application.swift */; };
1FBAFA042A815AA200425224 /* RelativeTimeView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1FBAFA032A815AA200425224 /* RelativeTimeView.swift */; };
1FD1ADC12BAD41B0001904BB /* MouseMove.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1FD1ADC02BAD41B0001904BB /* MouseMove.swift */; };
1FF3ED2B2A8692A900EEF21D /* IntergrationView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1FF3ED2A2A8692A900EEF21D /* IntergrationView.swift */; };
1FF3ED2E2A8732AA00EEF21D /* ApiIntegrationView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1FF3ED2D2A8732AA00EEF21D /* ApiIntegrationView.swift */; };
1FF3ED302A8732C600EEF21D /* SlackIntegrationView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1FF3ED2F2A8732C600EEF21D /* SlackIntegrationView.swift */; };
Expand Down Expand Up @@ -73,6 +74,7 @@
1FAAEC462A666ED800C086FF /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist; path = Info.plist; sourceTree = "<group>"; };
1FAAEC472A66702100C086FF /* Application.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Application.swift; sourceTree = "<group>"; };
1FBAFA032A815AA200425224 /* RelativeTimeView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RelativeTimeView.swift; sourceTree = "<group>"; };
1FD1ADC02BAD41B0001904BB /* MouseMove.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MouseMove.swift; sourceTree = "<group>"; };
1FF3ED2A2A8692A900EEF21D /* IntergrationView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IntergrationView.swift; sourceTree = "<group>"; };
1FF3ED2D2A8732AA00EEF21D /* ApiIntegrationView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ApiIntegrationView.swift; sourceTree = "<group>"; };
1FF3ED2F2A8732C600EEF21D /* SlackIntegrationView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SlackIntegrationView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -110,6 +112,7 @@
1FAAEC472A66702100C086FF /* Application.swift */,
1F1DA6BD2A875EDE0076B77F /* NotificationManager.swift */,
1FFC0FC42A5AE4FE00F67585 /* ActiveApplicationObserver.swift */,
1FD1ADC02BAD41B0001904BB /* MouseMove.swift */,
1F3BE35E2A62F13D0059A818 /* Agent.swift */,
1F3BE36E2A63B3220059A818 /* Request.swift */,
1F6FE2D72A469A8300D646CE /* Store.swift */,
Expand Down Expand Up @@ -354,6 +357,7 @@
1FFC0FC52A5AE4FE00F67585 /* ActiveApplicationObserver.swift in Sources */,
1F3BE35F2A62F13D0059A818 /* Agent.swift in Sources */,
1F3BE3692A62FB220059A818 /* Persisted+Modifier.swift in Sources */,
1FD1ADC12BAD41B0001904BB /* MouseMove.swift in Sources */,
1F04C2262A63CBED002FE62F /* ReportInfoView.swift in Sources */,
1F04C21C2A63C068002FE62F /* Atoms.swift in Sources */,
1F6FE2DA2A469B7500D646CE /* SettingView.swift in Sources */,
Expand Down
2 changes: 2 additions & 0 deletions ProcessReporter/ProcessReporterApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,15 @@ struct swiftui_menu_barApp: App {

if JotaiStore.shared.get(Atoms.isReportingAtom) {
reporter.startReporting()

} else {
Application.openSetting()
}

ActiveApplicationObserver.shared.observe { name in
JotaiStore.shared.set(Atoms.currentFrontAppAtom, value: name)
}
monitorMouseMove()
}

var menuIcon: String {
Expand Down
6 changes: 5 additions & 1 deletion ProcessReporter/Utils/ActiveApplicationObserver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ class ActiveApplicationObserver {
}
}

struct ActiveApplicationInfo {
struct ActiveApplicationInfo: Equatable {
var title: String?

static func ==(lhs: ActiveApplicationInfo, rhs: ActiveApplicationInfo) -> Bool {
return lhs.title == rhs.title
}
}
35 changes: 34 additions & 1 deletion ProcessReporter/Utils/MouseMove.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,37 @@
// Created by Innei on 2024/3/22.
//

import Foundation
import Cocoa
import AppKit
import Accessibility

func monitorMouseMove() {


var lastSelectedText = ""
var info : ActiveApplicationInfo = ActiveApplicationObserver.shared.getActiveApplicationInfo()
NSEvent.addGlobalMonitorForEvents(matching:
[.leftMouseUp, .keyDown]
) { (event) in

switch event.type {
case .leftMouseUp, .keyDown:

var currentInfo = ActiveApplicationObserver.shared.getActiveApplicationInfo()

if currentInfo != info {

DispatchQueue.main.async {
Reporter.shared.report()
}
}
info = currentInfo
break

default:
break
}

}

}
2 changes: 1 addition & 1 deletion ProcessReporter/Utils/Reporter/Reporter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ class Reporter {
timer = nil
}

private func report() {
public func report() {
let shouldReport = JotaiStore.shared.get(Atoms.isReportingAtom)
if !shouldReport {
debugPrint("Report is disabled.")
Expand Down

0 comments on commit 75de33c

Please sign in to comment.