Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transparency should be disabled when page fill method is full width and background color is not black and gray #2

Merged
merged 1 commit into from
Feb 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
142 changes: 71 additions & 71 deletions frontend/src/hooks/useSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,94 +4,94 @@ import { tryJSONParse } from "../utils/utils";
const key = "__manga_useSettings_v1"; // update this if settings change

export type SettingsType = {
readingDirection: "LEFT-TO-RIGHT" | "RIGHT-TO-LEFT" | "TOP-TO-BOTTOM";
imageFitMethod: "TO-SCREEN" | "TO-WIDTH" | "TO-HEIGHT";
stripWidthControl: "AUTO" | "MANUAL";
stripWidth: string;
readerBackgroundColor: string;
readerCustomBackgroundColor: string;
readerButtonsAppearance: "SOLID" | "HOLLOW" | "TRANSPARENT";
displayCurrentPageIndicator: "ON-FOCUS" | "ALWAYS" | "NEVER";
currentPageIndicatorLocation: "LEFT" | "CENTER" | "RIGHT";
pagesModifyColors: "NONE" | "DARKEN" | "INVERT" | "WARM";
tapToNavigate: "NO" | "YES";
displayChapterNavigation: "ON-FOCUS" | "ALWAYS";
readerKeyboardNavigation: {
disabled: "NO" | "YES";
wasd: "YES" | "NO";
arrowKeys: "YES" | "NO";
nextPage: "YES" | "NO";
previousPage: "YES" | "NO"; // wrongly named page, please fix
spacebar: "NEXT-PAGE" | "NEXT-CHAPTER" | "DISABLED";
};
readerShowDesktopDrawer: "YES" | "NO";
readerClickNavigationDisabled: "NO" | "YES";
readerClickNavigation: "PREV-MENU-NEXT" | "PREV-NEXT" | "ONLY-NEXT";
desktopSideMenuOpen: "NO" | "YES";
readingDirection: "LEFT-TO-RIGHT" | "RIGHT-TO-LEFT" | "TOP-TO-BOTTOM";
imageFitMethod: "TO-SCREEN" | "TO-WIDTH" | "TO-HEIGHT";
stripWidthControl: "AUTO" | "MANUAL";
stripWidth: string;
readerBackgroundColor: "#000000" | "#111" | "#ffffff" | "custom";
readerCustomBackgroundColor: string;
readerButtonsAppearance: "SOLID" | "HOLLOW" | "TRANSPARENT";
displayCurrentPageIndicator: "ON-FOCUS" | "ALWAYS" | "NEVER";
currentPageIndicatorLocation: "LEFT" | "CENTER" | "RIGHT";
pagesModifyColors: "NONE" | "DARKEN" | "INVERT" | "WARM";
tapToNavigate: "NO" | "YES";
displayChapterNavigation: "ON-FOCUS" | "ALWAYS";
readerKeyboardNavigation: {
disabled: "NO" | "YES";
wasd: "YES" | "NO";
arrowKeys: "YES" | "NO";
nextPage: "YES" | "NO";
previousPage: "YES" | "NO"; // wrongly named page, please fix
spacebar: "NEXT-PAGE" | "NEXT-CHAPTER" | "DISABLED";
};
readerShowDesktopDrawer: "YES" | "NO";
readerClickNavigationDisabled: "NO" | "YES";
readerClickNavigation: "PREV-MENU-NEXT" | "PREV-NEXT" | "ONLY-NEXT";
desktopSideMenuOpen: "NO" | "YES";
};

const defaultSettings: SettingsType = {
readingDirection: "LEFT-TO-RIGHT",
imageFitMethod: "TO-SCREEN",
stripWidthControl: "AUTO",
stripWidth: "100%",
readerBackgroundColor: "#000000",
readerCustomBackgroundColor: "#000000",
readerButtonsAppearance: "SOLID",
displayCurrentPageIndicator: "ON-FOCUS",
currentPageIndicatorLocation: "CENTER",
pagesModifyColors: "NONE",
tapToNavigate: "NO",
displayChapterNavigation: "ON-FOCUS",
readerKeyboardNavigation: {
disabled: "NO",
wasd: "YES",
arrowKeys: "YES",
nextPage: "YES",
previousPage: "YES",
spacebar: "NEXT-PAGE",
},
readerShowDesktopDrawer: "YES",
readerClickNavigationDisabled: "NO",
readerClickNavigation: "PREV-MENU-NEXT",
desktopSideMenuOpen: "NO",
readingDirection: "LEFT-TO-RIGHT",
imageFitMethod: "TO-SCREEN",
stripWidthControl: "AUTO",
stripWidth: "100%",
readerBackgroundColor: "#000000",
readerCustomBackgroundColor: "#000000",
readerButtonsAppearance: "SOLID",
displayCurrentPageIndicator: "ON-FOCUS",
currentPageIndicatorLocation: "CENTER",
pagesModifyColors: "NONE",
tapToNavigate: "NO",
displayChapterNavigation: "ON-FOCUS",
readerKeyboardNavigation: {
disabled: "NO",
wasd: "YES",
arrowKeys: "YES",
nextPage: "YES",
previousPage: "YES",
spacebar: "NEXT-PAGE",
},
readerShowDesktopDrawer: "YES",
readerClickNavigationDisabled: "NO",
readerClickNavigation: "PREV-MENU-NEXT",
desktopSideMenuOpen: "NO",
};

export default function useSettings() {
const [settings, setSettings] = useState<SettingsType>(getCachedSettings());
const [snapshot, update] = useState(0);
const setSetting = useCallback(
(keys: string, value: any) => {
const snapshot = Date.now();
void updateDeep(settings, value, keys); // With reference!
void update(snapshot);
void setSettings((prevState) => ({
...prevState,
...settings,
}));
void cacheSettings(settings);
},
[update, settings, setSettings]
);
const [settings, setSettings] = useState<SettingsType>(getCachedSettings());
const [snapshot, update] = useState(0);
const setSetting = useCallback(
(keys: string, value: any) => {
const snapshot = Date.now();
void updateDeep(settings, value, keys); // With reference!
void update(snapshot);
void setSettings(prevState => ({
...prevState,
...settings,
}));
void cacheSettings(settings);
},
[update, settings, setSettings],
);

return [settings, setSetting] as const;
return [settings, setSetting] as const;
}

function getCachedSettings(): SettingsType {
const cached = tryJSONParse<SettingsType>(localStorage.getItem(key));
const cached = tryJSONParse<SettingsType>(localStorage.getItem(key));

return cached || defaultSettings;
return cached || defaultSettings;
}

function cacheSettings(settings: SettingsType) {
return void localStorage.setItem(key, JSON.stringify(settings));
return void localStorage.setItem(key, JSON.stringify(settings));
}

/** https://stackoverflow.com/a/6842900/13188385 */
function updateDeep(obj: any, value: any, path: any) {
var i;
path = path.split(".");
for (i = 0; i < path.length - 1; i++) obj = obj[path[i]];
var i;
path = path.split(".");
for (i = 0; i < path.length - 1; i++) obj = obj[path[i]];

obj[path[i]] = value;
obj[path[i]] = value;
}