Fix the back/forward button on topbar (#3719)

* Check only webcontent type === 'window' instead of using webContents.getFocusedWebContents(), as other webview can capture focus

Signed-off-by: Hung-Han (Henry) Chen <chenhungh@gmail.com>

* Update test

Signed-off-by: Hung-Han (Henry) Chen <chenhungh@gmail.com>
This commit is contained in:
chh authored and GitHub committed 2021-09-02 14:21:17 +03:00
1 parent fd1d9c6d35
commit 40a2d9e05c
3 files changed
+26 -7

No files matched your search

@@ -50,11 +50,12 @@ const goForward = jest.fn();
jest.mock("@electron/remote", () => {
return {
webContents: {
getFocusedWebContents: () => {
return {
getAllWebContents: () => {
return [{
getType: () => "window",
goBack,
goForward
};
}];
}
}
};
+2 -2
View File
@@ -69,11 +69,11 @@ export const TopBar = observer(({ children, ...rest }: Props) => {
};
const goBack = () => {
webContents.getFocusedWebContents()?.goBack();
webContents.getAllWebContents().find((webContent) => webContent.getType() === "window")?.goBack();
};
const goForward = () => {
webContents.getFocusedWebContents()?.goForward();
webContents.getAllWebContents().find((webContent) => webContent.getType() === "window")?.goForward();
};
useEffect(() => {
+20 -2
View File
@@ -26,7 +26,25 @@ import { navigation } from "../navigation";
export function watchHistoryState() {
return reaction(() => navigation.location, () => {
broadcastMessage("history:can-go-back", webContents.getFocusedWebContents()?.canGoBack());
broadcastMessage("history:can-go-forward", webContents.getFocusedWebContents()?.canGoForward());
const getAllWebContents = webContents.getAllWebContents();
const canGoBack = getAllWebContents.some((webContent) => {
if (webContent.getType() === "window") {
return webContent.canGoBack();
}
return false;
});
const canGoForward = getAllWebContents.some((webContent) => {
if (webContent.getType() === "window") {
return webContent.canGoForward();
}
return false;
});
broadcastMessage("history:can-go-back", canGoBack);
broadcastMessage("history:can-go-forward", canGoForward);
});
}