Focus automatically when window is shown (#34)

This commit is contained in:
Quang Lam 2019-11-22 17:30:11 -06:00 committed by GitHub
parent 683a084b72
commit 5fa23297fb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 74 additions and 44 deletions

View file

@ -34,7 +34,7 @@
"fs-extra": "8.1.0",
"is-url": "1.2.4",
"jimp": "0.6.8",
"menubar": "7.0.0",
"menubar": "6.0.8",
"node-fetch": "2.6.0",
"request": "2.88.0",
"request-promise-native": "1.0.8",
@ -52,7 +52,7 @@
"classnames": "2.2.6",
"concurrently": "5.0.0",
"cross-env": "6.0.3",
"electron": "6.1.4",
"electron": "6.1.5",
"electron-builder": "21.2.0",
"electron-notarize": "0.1.1",
"eslint": "6.1.0",

View file

@ -42,27 +42,6 @@ const addView = (browserWindow, workspace) => {
},
});
if (workspace.active) {
browserWindow.setBrowserView(view);
const contentSize = browserWindow.getContentSize();
const offsetTitlebar = 0;
const x = 68;
const y = global.showNavigationBar ? 36 + offsetTitlebar : 0 + offsetTitlebar;
view.setBounds({
x,
y,
width: contentSize[0] - x,
height: contentSize[1] - y,
});
view.setAutoResize({
width: true,
height: true,
});
}
view.webContents.on('did-start-loading', () => {
if (getWorkspace(workspace.id).active) {
didFailLoad[workspace.id] = false;
@ -82,6 +61,16 @@ const addView = (browserWindow, workspace) => {
});
});
if (workspace.active) {
const handleFocus = () => {
// focus on webview
// https://github.com/quanglam2807/webcatalog/issues/398
view.webContents.focus();
view.webContents.removeListener('did-stop-loading', handleFocus);
};
view.webContents.on('did-stop-loading', handleFocus);
}
// https://electronjs.org/docs/api/web-contents#event-did-fail-load
view.webContents.on('did-fail-load', (e, errorCode, errorDesc, validateUrl, isMainFrame) => {
if (isMainFrame && errorCode < 0 && errorCode !== -3) {
@ -212,6 +201,27 @@ const addView = (browserWindow, workspace) => {
|| workspace.homeUrl);
views[workspace.id] = view;
if (workspace.active) {
browserWindow.setBrowserView(view);
const contentSize = browserWindow.getContentSize();
const offsetTitlebar = 0;
const x = 68;
const y = global.showNavigationBar ? 36 + offsetTitlebar : 0 + offsetTitlebar;
view.setBounds({
x,
y,
width: contentSize[0] - x,
height: contentSize[1] - y,
});
view.setAutoResize({
width: true,
height: true,
});
}
};
const getView = (id) => views[id];
@ -244,6 +254,10 @@ const setActiveView = (browserWindow, id) => {
height: true,
});
// focus on webview
// https://github.com/quanglam2807/webcatalog/issues/398
view.webContents.focus();
sendToAllWindows('update-is-loading', view.webContents.isLoading());
sendToAllWindows('update-did-fail-load', Boolean(didFailLoad[id]));
};

View file

@ -62,6 +62,15 @@ const createAsync = () => {
return new Promise((resolve, reject) => {
try {
mb.on('after-create-window', () => {
mb.window.on('focus', () => {
const view = mb.window.getBrowserView();
if (view && view.webContents) {
view.webContents.focus();
}
});
});
mb.on('ready', () => {
mb.tray.on('right-click', () => {
mb.tray.popUpContextMenu(contextMenu);
@ -129,6 +138,13 @@ const createAsync = () => {
win = null;
});
win.on('focus', () => {
const view = win.getBrowserView();
if (view && view.webContents) {
view.webContents.focus();
}
});
return Promise.resolve();
};

View file

@ -5201,10 +5201,10 @@ electron-window-state@5.0.3:
jsonfile "^4.0.0"
mkdirp "^0.5.1"
electron@6.1.4:
version "6.1.4"
resolved "https://registry.yarnpkg.com/electron/-/electron-6.1.4.tgz#21cbcf5de74a0e284621dce1b8c0fe092c146c2d"
integrity sha512-QUN59E0dPmkAEYi+OU0jJG6tIAi8VDJpsHLRmdyL62oAuT3+CWJ/gefmNwAWQsIpNj/DieVTn5h+/t/Hke4T+Q==
electron@6.1.5:
version "6.1.5"
resolved "https://registry.yarnpkg.com/electron/-/electron-6.1.5.tgz#1f1bc54042587d8368edd43ffecb0ce7c84cab87"
integrity sha512-PrdJKkAS0IaSJwu4him03VYqvAKK1qyWTE/ieb4LgcbR4F4u90b91/7xna6P1GpD/FXiHqzZQcs0SvK/o08ckQ==
dependencies:
"@types/node" "^10.12.18"
electron-download "^4.1.0"
@ -8639,10 +8639,10 @@ memory-fs@^0.4.0, memory-fs@^0.4.1:
errno "^0.1.3"
readable-stream "^2.0.1"
menubar@7.0.0:
version "7.0.0"
resolved "https://registry.yarnpkg.com/menubar/-/menubar-7.0.0.tgz#7d1a1a0adcd003a7fb3ef81cea91c0865a95f18d"
integrity sha512-83cton9fdguCSpkDvFyeYIZ0wWUlpDq610D2KAcXzlQeYr9ccSHtLlLt85RqCeBAcyLVZ2lu8MSt7KHODlcIOw==
menubar@6.0.8:
version "6.0.8"
resolved "https://registry.yarnpkg.com/menubar/-/menubar-6.0.8.tgz#deff4b4227c73149f6316c481accb1486e7c9e7f"
integrity sha512-S02SLiqec8MAUbV2vsUThh0P9XPKS9mrHVN3c/6T1g+tuHwFX7TRup3xCAjIGCO+Hxs32tWmjqVh4KjBGIXudQ==
dependencies:
electron-positioner "^4.1.0"
@ -10863,15 +10863,15 @@ react-dev-utils@^9.1.0:
strip-ansi "5.2.0"
text-table "0.2.0"
react-dom@16.11.0:
version "16.11.0"
resolved "https://registry.yarnpkg.com/react-dom/-/react-dom-16.11.0.tgz#7e7c4a5a85a569d565c2462f5d345da2dd849af5"
integrity sha512-nrRyIUE1e7j8PaXSPtyRKtz+2y9ubW/ghNgqKFHHAHaeP0fpF5uXR+sq8IMRHC+ZUxw7W9NyCDTBtwWxvkb0iA==
react-dom@16.12.0:
version "16.12.0"
resolved "https://registry.yarnpkg.com/react-dom/-/react-dom-16.12.0.tgz#0da4b714b8d13c2038c9396b54a92baea633fe11"
integrity sha512-LMxFfAGrcS3kETtQaCkTKjMiifahaMySFDn71fZUNpPHZQEzmk/GiAeIT8JSOrHB23fnuCOMruL2a8NYlw+8Gw==
dependencies:
loose-envify "^1.1.0"
object-assign "^4.1.1"
prop-types "^15.6.2"
scheduler "^0.17.0"
scheduler "^0.18.0"
react-error-overlay@^6.0.3:
version "6.0.3"
@ -10994,10 +10994,10 @@ react-transition-group@^2.2.1:
prop-types "^15.6.2"
react-lifecycles-compat "^3.0.4"
react@16.11.0:
version "16.11.0"
resolved "https://registry.yarnpkg.com/react/-/react-16.11.0.tgz#d294545fe62299ccee83363599bf904e4a07fdbb"
integrity sha512-M5Y8yITaLmU0ynd0r1Yvfq98Rmll6q8AxaEe88c8e7LxO8fZ2cNgmFt0aGAS9wzf1Ao32NKXtCl+/tVVtkxq6g==
react@16.12.0:
version "16.12.0"
resolved "https://registry.yarnpkg.com/react/-/react-16.12.0.tgz#0c0a9c6a142429e3614834d5a778e18aa78a0b83"
integrity sha512-fglqy3k5E+81pA8s+7K0/T3DBCF0ZDOher1elBFzF7O6arXJgzyu/FW+COxFvAWXJoJN9KIZbT2LXlukwphYTA==
dependencies:
loose-envify "^1.1.0"
object-assign "^4.1.1"
@ -11659,10 +11659,10 @@ saxes@^3.1.9:
dependencies:
xmlchars "^2.1.1"
scheduler@^0.17.0:
version "0.17.0"
resolved "https://registry.yarnpkg.com/scheduler/-/scheduler-0.17.0.tgz#7c9c673e4ec781fac853927916d1c426b6f3ddfe"
integrity sha512-7rro8Io3tnCPuY4la/NuI5F2yfESpnfZyT6TtkXnSWVkcu0BCDJ+8gk5ozUaFaxpIyNuWAPXrH0yFcSi28fnDA==
scheduler@^0.18.0:
version "0.18.0"
resolved "https://registry.yarnpkg.com/scheduler/-/scheduler-0.18.0.tgz#5901ad6659bc1d8f3fdaf36eb7a67b0d6746b1c4"
integrity sha512-agTSHR1Nbfi6ulI0kYNK0203joW2Y5W4po4l+v03tOoiJKpTBbxpNhWDvqc/4IcOw+KLmSiQLTasZ4cab2/UWQ==
dependencies:
loose-envify "^1.1.0"
object-assign "^4.1.1"