Skip to content

Commit

Permalink
Improve DND monitoring
Browse files Browse the repository at this point in the history
better use machine resources
  • Loading branch information
hovancik committed Nov 18, 2023
1 parent 0d18310 commit 1155a1a
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 10 deletions.
19 changes: 19 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,24 @@ jobs:
fail-fast: false

steps:
- name: Setup DBUS environment
if: matrix.os == 'ubuntu-latest'
run: |
export DISPLAY=:0
sudo apt install dbus-x11
machineId=$(cat /var/lib/dbus/machine-id)
mkdir -p /home/runner/.dbus
mkdir -p /home/runner/.dbus/session-bus/
touch /home/runner/.dbus/session-bus/${machineId}-0
sudo dbus-launch --exit-with-session
sleep 5 # Wait for the DBUS session to start
echo $DBUS_SESSION_BUS_ADDRESS
eval `dbus-launch --sh-syntax`
address=$(echo $DBUS_SESSION_BUS_ADDRESS)
echo "DBUS_SESSION_BUS_ADDRESS=${address}" > /home/runner/.dbus/session-bus/${machineId}-0
- name: 1
if: matrix.os == 'ubuntu-latest'
run: cat /home/runner/.dbus/session-bus/$(cat /var/lib/dbus/machine-id)-0
- name: Set Timezone
uses: szenius/set-timezone@v1.1
with:
Expand All @@ -31,5 +49,6 @@ jobs:
- run: npx nyc --reporter=lcov npm test
env:
CI: true
DISPLAY: ':0'
- name: Codecov
uses: codecov/codecov-action@v3
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
18.17.1
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

### Fixed
- improve DND monitoring memory usage

## [1.15.0] - 2023-11-11
### Added
- new end-of-the-break sound
Expand Down
2 changes: 1 addition & 1 deletion app/preferences.html
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
<input type="checkbox" value="naturalBreaks" id="monitorIdleTime">
<label data-i18next="preferences.settings.monitorIdleTime" for="monitorIdleTime"></label>
</div>
<div class="linux-hidden">
<div>
<input type="checkbox" class="negative" value="monitorDnd" id="monitorDnd">
<label data-i18next="preferences.settings.monitorDnd" for="monitorDnd"></label>
</div>
Expand Down
26 changes: 17 additions & 9 deletions app/utils/dndManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@ class DndManager extends EventEmitter {
this.monitorDnd = settings.get('monitorDnd')
this.timer = null
this.isOnDnd = false

if (process.platform === 'win32') {
this.windowsFocusAssist = require('windows-focus-assist')
this.windowsQuietHours = require('windows-quiet-hours')
} else if (process.platform === 'darwin') {
this.macosNotificationState = require('macos-notification-state')
} else if (process.platform === 'linux') {
this.bus = require('dbus-final').sessionBus()
this.util = require('node:util')

Check warning on line 19 in app/utils/dndManager.js

View check run for this annotation

Codecov / codecov/patch

app/utils/dndManager.js#L17-L19

Added lines #L17 - L19 were not covered by tests
}

if (this.monitorDnd) {
this.start()
}
Expand All @@ -31,10 +42,8 @@ class DndManager extends EventEmitter {
}

async _isDndEnabledLinux () {
const dbus = require('dbus-final')
const bus = dbus.sessionBus()
try {
const obj = await bus.getProxyObject('org.freedesktop.Notifications', '/org/freedesktop/Notifications')
const obj = await this.bus.getProxyObject('org.freedesktop.Notifications', '/org/freedesktop/Notifications')

Check warning on line 46 in app/utils/dndManager.js

View check run for this annotation

Codecov / codecov/patch

app/utils/dndManager.js#L46

Added line #L46 was not covered by tests
const properties = obj.getInterface('org.freedesktop.DBus.Properties')
const dndEnabled = await properties.Get('org.freedesktop.Notifications', 'Inhibited')
if (await dndEnabled.value) {
Expand All @@ -45,7 +54,7 @@ class DndManager extends EventEmitter {
}

try {
const obj = await bus.getProxyObject('org.xfce.Xfconf', '/org/xfce/Xfconf')
const obj = await this.bus.getProxyObject('org.xfce.Xfconf', '/org/xfce/Xfconf')

Check warning on line 57 in app/utils/dndManager.js

View check run for this annotation

Codecov / codecov/patch

app/utils/dndManager.js#L57

Added line #L57 was not covered by tests
const properties = obj.getInterface('org.xfce.Xfconf')
const dndEnabled = await properties.GetProperty('xfce4-notifyd', '/do-not-disturb')
if (await dndEnabled.value) {
Expand All @@ -56,8 +65,7 @@ class DndManager extends EventEmitter {
}

try {
const util = require('node:util')
const exec = util.promisify(require('node:child_process').exec)
const exec = this.util.promisify(require('node:child_process').exec)

Check warning on line 68 in app/utils/dndManager.js

View check run for this annotation

Codecov / codecov/patch

app/utils/dndManager.js#L68

Added line #L68 was not covered by tests
const { stdout } = await exec('gsettings get org.gnome.desktop.notifications show-banners')
if (stdout.replace(/[^0-9a-zA-Z]/g, '') === 'false') {
return true
Expand All @@ -75,12 +83,12 @@ class DndManager extends EventEmitter {
if (process.platform === 'win32') {
let wfa = 0
try {
wfa = require('windows-focus-assist').getFocusAssist().value
wfa = this.windowsFocusAssist.getFocusAssist().value

Check warning on line 86 in app/utils/dndManager.js

View check run for this annotation

Codecov / codecov/patch

app/utils/dndManager.js#L86

Added line #L86 was not covered by tests
} catch (e) { wfa = -1 } // getFocusAssist() throw an error if OS isn't windows
const wqh = require('windows-quiet-hours').getIsQuietHours()
const wqh = this.windowsQuietHours.getIsQuietHours()

Check warning on line 88 in app/utils/dndManager.js

View check run for this annotation

Codecov / codecov/patch

app/utils/dndManager.js#L88

Added line #L88 was not covered by tests
return wqh || (wfa !== -1 && wfa !== 0)
} else if (process.platform === 'darwin') {
return require('macos-notification-state').getDoNotDisturb()
return this.macosNotificationState.getDoNotDisturb()

Check warning on line 91 in app/utils/dndManager.js

View check run for this annotation

Codecov / codecov/patch

app/utils/dndManager.js#L91

Added line #L91 was not covered by tests
} else if (process.platform === 'linux') {
return await this._isDndEnabledLinux()
}
Expand Down

0 comments on commit 1155a1a

Please sign in to comment.