electron 默认弹窗会导致输入框焦点失去的问题解决方案

  • Post author:
  • Post category:其他


问题  :在Windows上出现,系统自带的弹窗会导致主窗口失去焦点。

解决方案 :

1 使用自己写的弹窗,不想写那就用方案二,注意修改相关变量

var userAgent = navigator.userAgent.toLowerCase();

if (userAgent.indexOf(‘ electron/’) > -1){


const { dialog } = require(‘electron’).remote;//修改默认对话框,修复electron弹出默认对话              框后页面失去焦点的bug

alert = function(str){


var options = {


type: ‘warning’,

buttons: [“确定”],

defaultId: 0,

cancelId:0,

detail:str,

message: ”

}

dialog.showMessageBoxSync(null,options)

}

confirm = function(str){


var options = {


type: ‘warning’,

buttons: [“确认”,”取消”],

defaultId: 0,

cancelId:1,

detail:”,

message: str

}

var flag = dialog.showMessageBoxSync(null,options);

if(flag==0){


return true;

}else{


return false;

}

}

}

2.以下代码放在main.js 创建主窗口后面的,—注意修改相关变量

const isWindows = process.platform === ‘win32’;

let needsFocusFix = false;

let triggeringProgrammaticBlur = false;

win.on(‘blur’, (event) => {


if(!triggeringProgrammaticBlur) {


needsFocusFix = true;

}

})

win.on(‘focus’, (event) => {


if(isWindows && needsFocusFix) {


needsFocusFix = false;

triggeringProgrammaticBlur = true;

setTimeout(function () {


win.blur();

win.focus();

setTimeout(function () {


triggeringProgrammaticBlur = false;

}, 100);

}, 100);

}

})

3.重新打包

4.相关git

https://github.com/electron/electron/issues/19977

https://github.com/electron/electron/issues/20400

学习借鉴 勇往直前!



版权声明:本文为modernk原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。