Skip to content

Commit

Permalink
fix: reader back arrow icon button was crashing in some cases (bad x/…
Browse files Browse the repository at this point in the history
…y screen oordinates) (Fixes #1061 Supersedes PR #1063 )
  • Loading branch information
danielweck committed May 26, 2020
1 parent 3ae023d commit c1d32cb
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 14 deletions.
18 changes: 18 additions & 0 deletions src/common/rectangle/window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,24 @@ export const defaultRectangle = (): Rectangle => (
x: Math.round(screen.getPrimaryDisplay().workAreaSize.width / 3),
y: Math.round(screen.getPrimaryDisplay().workAreaSize.height / 3),
});
export const normalizeRectangle = (winBound: Rectangle) => {
const rect = defaultRectangle();

// note: 0 and NaN are falsy (as well as null and undefined),
// positive and negative numbers are truthy.
if (!winBound.x) {
winBound.x = rect.x;
}
if (!winBound.y) {
winBound.y = rect.y;
}
if (!winBound.width) {
winBound.width = rect.width;
}
if (!winBound.height) {
winBound.height = rect.height;
}
};

// export type t_savedWindowsRectangle = typeof savedWindowsRectangle;
// export const savedWindowsRectangle = async (rectangle: Rectangle) => {
Expand Down
20 changes: 12 additions & 8 deletions src/main/redux/sagas/reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,20 +122,19 @@ function* readerFullscreenRequest(action: readerActions.fullScreenRequest.TActio
function* readerDetachRequest(action: readerActions.detachModeRequest.TAction) {

const libWin = yield* callTyped(() => getLibraryWindowFromDi());

if (libWin) {

// try-catch to do not trigger an error message when the winbound is not handle by the os
let libBound: Electron.Rectangle;
try {
// get an bound with offset
libBound = yield* callTyped(getWinBound, undefined);
debug("getWinBound(undefined)", libBound);
if (libBound) {
libWin.setBounds(libBound);
}
} catch (e) {

debug("cannot set libBound", libBound, e);
debug("error libWin.setBounds(libBound)", e);
}

// this should never occur, but let's do it for certainty
Expand Down Expand Up @@ -163,7 +162,7 @@ function* readerDetachRequest(action: readerActions.detachModeRequest.TAction) {
yield put(readerActions.detachModeSuccess.build());
}

function* getWinBound(publicationIdentifier: string) {
function* getWinBound(publicationIdentifier: string | undefined) {

const readers = yield* selectTyped((state: RootState) => state.win.session.reader);
const library = yield* selectTyped((state: RootState) => state.win.session.library);
Expand All @@ -173,9 +172,9 @@ function* getWinBound(publicationIdentifier: string) {
return library.windowBound;
}

let winBound = yield* selectTyped(
let winBound = (yield* selectTyped(
(state: RootState) => state.win.registry.reader[publicationIdentifier]?.windowBound,
);
)) as Electron.Rectangle | undefined;

const winBoundArray = [];
winBoundArray.push(library.windowBound);
Expand Down Expand Up @@ -299,13 +298,18 @@ function* readerCLoseRequestFromIdentifier(action: readerActions.closeRequest.TA

yield call(readerCloseRequest, action.sender.identifier);

const libWin = getLibraryWindowFromDi();
const libWin = yield* callTyped(() => getLibraryWindowFromDi());
if (libWin) {

const winBound = yield* selectTyped(
(state: RootState) => state.win.session.library.windowBound,
);
libWin.setBounds(winBound);
debug("state.win.session.library.windowBound", winBound);
try {
libWin.setBounds(winBound);
} catch (e) {
debug("error libWin.setBounds(winBound)", e);
}

if (libWin.isMinimized()) {
libWin.restore();
Expand Down
17 changes: 11 additions & 6 deletions src/main/redux/sagas/win/reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import * as debug_ from "debug";
import { readerIpc } from "readium-desktop/common/ipc";
import { ReaderMode } from "readium-desktop/common/models/reader";
import { normalizeRectangle } from "readium-desktop/common/rectangle/window";
import { readerActions } from "readium-desktop/common/redux/actions";
import { takeSpawnEvery } from "readium-desktop/common/redux/sagas/takeSpawnEvery";
import { callTyped, selectTyped } from "readium-desktop/common/redux/sagas/typed-saga";
Expand Down Expand Up @@ -130,12 +131,16 @@ function* winClose(action: winActions.reader.closed.TAction) {
yield put(readerActions.attachModeRequest.build());

} else {
try {
const readerWin = getReaderWindowFromDi(identifier);
libraryWindow.setBounds(readerWin.getBounds());

} catch (_err) {
debug("can't load readerWin from di :", identifier);
const readerWin = yield* callTyped(() => getReaderWindowFromDi(identifier));
if (readerWin) {
try {
const winBound = readerWin.getBounds();
debug("_______3 readerWin.getBounds()", winBound);
normalizeRectangle(winBound);
libraryWindow.setBounds(winBound);
} catch (e) {
debug("error libraryWindow.setBounds(readerWin.getBounds())", e);
}
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/redux/sagas/win/session/library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// ==LICENSE-END==

import * as debug_ from "debug";
import { normalizeRectangle } from "readium-desktop/common/rectangle/window";
import { takeSpawnLeading } from "readium-desktop/common/redux/sagas/takeSpawnLeading";
import { error } from "readium-desktop/main/error";
import { winActions } from "readium-desktop/main/redux/actions";
Expand Down Expand Up @@ -73,6 +74,8 @@ function* libraryMoveOrResizeObserver(action: winActions.session.registerLibrary

try {
const winBound = library.getBounds();
debug("_______2 library.getBounds()", winBound);
normalizeRectangle(winBound);
yield put(winActions.session.setBound.build(id, winBound));
} catch (e) {
debug("set library bound error", e);
Expand Down
3 changes: 3 additions & 0 deletions src/main/redux/sagas/win/session/reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// ==LICENSE-END==

import * as debug_ from "debug";
import { normalizeRectangle } from "readium-desktop/common/rectangle/window";
import { takeSpawnEvery } from "readium-desktop/common/redux/sagas/takeSpawnEvery";
import { error } from "readium-desktop/main/error";
import { winActions } from "readium-desktop/main/redux/actions";
Expand Down Expand Up @@ -69,6 +70,8 @@ function* readerMoveOrResizeObserver(action: winActions.session.registerReader.T

try {
const winBound = reader.getBounds();
debug("_______1 reader.getBounds()", winBound);
normalizeRectangle(winBound);
yield put(winActions.session.setBound.build(id, winBound));
} catch (e) {
debug("set reader bound error", id, e);
Expand Down

0 comments on commit c1d32cb

Please sign in to comment.