Skip to content

Commit

Permalink
Merge pull request #1394 from hovancik/dnd-enhance
Browse files Browse the repository at this point in the history
Improve DND monitoring
  • Loading branch information
hovancik authored Nov 18, 2023
2 parents 0d18310 + 053cd27 commit be1d17e
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 17 deletions.
22 changes: 22 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: Check for file
if: matrix.os == 'ubuntu-latest'
run: cat /home/runner/.dbus/session-bus/$(cat /var/lib/dbus/machine-id)-0
- name: Set Timezone
uses: szenius/[email protected]
with:
Expand All @@ -28,8 +46,12 @@ jobs:
node-version: '18.17.1'
- run: npm install npm@9 -g
- run: npm install
- name: Install missing usocket
if: matrix.os == 'ubuntu-latest'
run: npm install usocket
- 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')
}

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')
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')
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)
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
} 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()
return wqh || (wfa !== -1 && wfa !== 0)
} else if (process.platform === 'darwin') {
return require('macos-notification-state').getDoNotDisturb()
return this.macosNotificationState.getDoNotDisturb()
} else if (process.platform === 'linux') {
return await this._isDndEnabledLinux()
}
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
"postinstall": "electron-builder install-app-deps",
"pack": "electron-builder build --dir",
"dist": "electron-builder build --x64 --ia32",
"test": "mocha test",
"test-single": "mocha $1",
"test": "mocha test --exit",
"test-single": "mocha $1 --exit",
"tdd": "mocha --watch test",
"lint": "./node_modules/.bin/standard",
"prepublishOnly": "pinst --disable",
Expand Down
17 changes: 12 additions & 5 deletions test/dndManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ const DndManager = require('../app/utils/dndManager')
const Store = require('electron-store')

chai.should()
const timeout = process.env.CI ? 30000 : 10000

describe('dndManager', function () {
this.timeout(timeout)
let settings = null
let dndManager = null

Expand All @@ -18,40 +20,45 @@ describe('dndManager', function () {
dndManager = new DndManager(settings)
})

it('should be running with default settings', () => {
it('should be running with default settings', (done) => {
dndManager.isOnDnd.should.be.equal(false)
dndManager.monitorDnd.should.be.equal(true)
done()
})

it('should not be running with monitorDnd: false', () => {
it('should not be running with monitorDnd: false', (done) => {
settings.set('monitorDnd', false)
dndManager.stop()
dndManager = null
dndManager = new DndManager(settings)
dndManager.isOnDnd.should.be.equal(false)
dndManager.monitorDnd.should.be.equal(false)
done()
})

it('should be running with monitorDnd: true', () => {
it('should be running with monitorDnd: true', (done) => {
settings.set('monitorDnd', true)
dndManager.stop()
dndManager = null
dndManager = new DndManager(settings)
dndManager.isOnDnd.should.be.equal(false)
dndManager.monitorDnd.should.be.equal(true)
done()
})

it('should start when start()', () => {
it('should start when start()', (done) => {
dndManager.stop()
dndManager.start()
dndManager.isOnDnd.should.be.equal(false)
dndManager.monitorDnd.should.be.equal(true)
done()
})

it('should stop when stop()', () => {
it('should stop when stop()', (done) => {
dndManager.stop()
dndManager.monitorDnd.should.be.equal(false)
dndManager.isOnDnd.should.be.equal(false)
done()
})

afterEach(() => {
Expand Down

0 comments on commit be1d17e

Please sign in to comment.