Skip to content

Commit

Permalink
Fullscreen apps can block idle detection
Browse files Browse the repository at this point in the history
  • Loading branch information
xanderfrangos committed Jun 1, 2024
1 parent 66b28df commit 322defe
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/components/SettingsWindow.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -1142,6 +1142,7 @@ export default class SettingsWindow extends PureComponent {
</div>
} />
</SettingsOption>
<SettingsOption title={"Fullscreen apps are never idle"} description={"Fullscreen apps block idle detection. This only applies to the focused window."} input={this.renderToggle("detectIdleCheckFullscreen")} />
</div>
</SettingsPage>

Expand Down
15 changes: 14 additions & 1 deletion src/electron.js
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,7 @@ const defaultSettings = {
detectIdleTimeEnabled: false,
detectIdleTimeSeconds: 0,
detectIdleTimeMinutes: 5,
detectIdleCheckFullscreen: true,
idleRestoreSeconds: 0,
wakeRestoreSeconds: 0,
hardwareRestoreSeconds: 0,
Expand Down Expand Up @@ -3702,11 +3703,23 @@ async function startIdleCheckShort() {
notIdleMonitor = setInterval(idleCheckShort, 1000)
}

function isFocusedWindowFullscreen() {
try {
if(!settings.detectIdleCheckFullscreen) return false;
const focusedHwnd = WindowUtils.getForegroundWindow()
const isFullscreen = WindowUtils.getWindowFullscreen(focusedHwnd)
return isFullscreen
} catch(e) {
console.log(e)
return false
}
}

function idleCheckShort() {
try {
const idleTime = powerMonitor.getSystemIdleTime()

if (!userIdleDimmed && settings.detectIdleTimeEnabled && !settings.disableAutoApply && idleTime >= getIdleSettingValue()) {
if (!userIdleDimmed && settings.detectIdleTimeEnabled && !settings.disableAutoApply && idleTime >= getIdleSettingValue() && !isFocusedWindowFullscreen()) {
console.log(`\x1b[36mUser idle. Dimming displays.\x1b[0m`)
userIdleDimmed = true
idleMonitorBlock?.release?.()
Expand Down
7 changes: 6 additions & 1 deletion src/modules/setwindowpos/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
const addon = require("bindings")("setwindowpos");
module.exports = {
setWindowPos: addon.setWindowPos,
getWindowPos: addon.getWindowPos,
getClientPos: addon.getClientPos,
getClientPos: addon.getClientPos,
setForegroundWindow: addon.setForegroundWindow,
getForegroundWindow: addon.getForegroundWindow
getForegroundWindow: addon.getForegroundWindow,
getWindowLong: addon.getWindowLong,
getWindowFullscreen: addon.getWindowFullscreen
}
58 changes: 58 additions & 0 deletions src/modules/setwindowpos/setwindowpos.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,50 @@ Napi::Boolean SetWindowPosition(const Napi::CallbackInfo& info) {
return Napi::Boolean::New(info.Env(), result);
}

Napi::Object RectToObj(Napi::Env env, RECT rect) {
Napi::Object pos = Napi::Object::New(env);
pos.Set(Napi::String::New(env, "top"), Napi::Number::New(env, rect.top));
pos.Set(Napi::String::New(env, "right"), Napi::Number::New(env, rect.right));
pos.Set(Napi::String::New(env, "bottom"), Napi::Number::New(env, rect.bottom));
pos.Set(Napi::String::New(env, "left"), Napi::Number::New(env, rect.left));
pos.Set(Napi::String::New(env, "width"), Napi::Number::New(env, rect.right - rect.left));
pos.Set(Napi::String::New(env, "height"), Napi::Number::New(env, rect.bottom - rect.top));
return pos;
}

Napi::Object GetWindowPosition(const Napi::CallbackInfo& info) {
Napi::Number hwnd = info[0].As<Napi::Number>();
RECT rect;
GetWindowRect((HWND) hwnd.Int32Value(), &rect);
return RectToObj(info.Env(), rect);
}

Napi::Object GetClientPosition(const Napi::CallbackInfo& info) {
Napi::Number hwnd = info[0].As<Napi::Number>();
RECT rect;
GetClientRect((HWND) hwnd.Int32Value(), &rect);
return RectToObj(info.Env(), rect);
}

Napi::Boolean GetWindowFullscreen(const Napi::CallbackInfo& info)
{
HWND hwnd = (HWND) info[0].As<Napi::Number>().Int32Value();

MONITORINFO monitorInfo = { 0 };
monitorInfo.cbSize = sizeof(MONITORINFO);
GetMonitorInfo(MonitorFromWindow(hwnd, MONITOR_DEFAULTTOPRIMARY), &monitorInfo);

RECT rect;
GetWindowRect(hwnd, &rect);

bool fullscreen = rect.left == monitorInfo.rcMonitor.left
&& rect.right == monitorInfo.rcMonitor.right
&& rect.top == monitorInfo.rcMonitor.top
&& rect.bottom == monitorInfo.rcMonitor.bottom;

return Napi::Boolean::New(info.Env(), fullscreen);
}

Napi::Number GetForegroundWin(const Napi::CallbackInfo& info) {
HWND result = GetForegroundWindow();

Expand All @@ -29,10 +73,24 @@ Napi::Boolean SetForegroundWin(const Napi::CallbackInfo& info) {
return Napi::Boolean::New(info.Env(), result);
}

Napi::Number GetWinLong(const Napi::CallbackInfo& info) {
Napi::Number hwnd = info[0].As<Napi::Number>();
Napi::Number index = info[1].As<Napi::Number>();

LONG_PTR result = GetWindowLongPtr((HWND) hwnd.Int32Value(), (int) index.Int32Value());

return Napi::Number::New(info.Env(), result);
}


Napi::Object Init(Napi::Env env, Napi::Object exports) {
exports.Set(Napi::String::New(env, "setWindowPos"), Napi::Function::New(env, SetWindowPosition));
exports.Set(Napi::String::New(env, "getWindowPos"), Napi::Function::New(env, GetWindowPosition));
exports.Set(Napi::String::New(env, "getClientPos"), Napi::Function::New(env, GetClientPosition));
exports.Set(Napi::String::New(env, "getForegroundWindow"), Napi::Function::New(env, GetForegroundWin));
exports.Set(Napi::String::New(env, "setForegroundWindow"), Napi::Function::New(env, SetForegroundWin));
exports.Set(Napi::String::New(env, "getWindowLong"), Napi::Function::New(env, GetWinLong));
exports.Set(Napi::String::New(env, "getWindowFullscreen"), Napi::Function::New(env, GetWindowFullscreen));
return exports;
}

Expand Down

0 comments on commit 322defe

Please sign in to comment.