2023-04-06 01:06:11 +00:00
|
|
|
/*
|
|
|
|
* Vencord, a modification for Discord's desktop app
|
|
|
|
* Copyright (c) 2023 Vendicated and contributors
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import { addContextMenuPatch, NavContextMenuPatchCallback, removeContextMenuPatch } from "@api/ContextMenu";
|
2023-05-05 23:36:00 +00:00
|
|
|
import { definePluginSettings } from "@api/Settings";
|
2023-04-11 00:00:42 +00:00
|
|
|
import { disableStyle, enableStyle } from "@api/Styles";
|
2023-04-06 01:06:11 +00:00
|
|
|
import { makeRange } from "@components/PluginSettings/components";
|
|
|
|
import { Devs } from "@utils/constants";
|
|
|
|
import { debounce } from "@utils/debounce";
|
|
|
|
import definePlugin, { OptionType } from "@utils/types";
|
2023-11-23 02:11:17 +00:00
|
|
|
import { ContextMenuApi, Menu, React, ReactDOM } from "@webpack/common";
|
2023-04-06 01:06:11 +00:00
|
|
|
import type { Root } from "react-dom/client";
|
|
|
|
|
|
|
|
import { Magnifier, MagnifierProps } from "./components/Magnifier";
|
|
|
|
import { ELEMENT_ID } from "./constants";
|
2023-04-11 00:00:42 +00:00
|
|
|
import styles from "./styles.css?managed";
|
2023-04-06 01:06:11 +00:00
|
|
|
|
|
|
|
export const settings = definePluginSettings({
|
|
|
|
saveZoomValues: {
|
|
|
|
type: OptionType.BOOLEAN,
|
|
|
|
description: "Whether to save zoom and lens size values",
|
|
|
|
default: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
invertScroll: {
|
|
|
|
type: OptionType.BOOLEAN,
|
|
|
|
description: "Invert scroll",
|
|
|
|
default: true,
|
|
|
|
},
|
|
|
|
|
2023-06-27 20:41:19 +00:00
|
|
|
nearestNeighbour: {
|
|
|
|
type: OptionType.BOOLEAN,
|
|
|
|
description: "Use Nearest Neighbour Interpolation when scaling images",
|
|
|
|
default: false,
|
|
|
|
},
|
|
|
|
|
2023-06-30 13:50:56 +00:00
|
|
|
square: {
|
|
|
|
type: OptionType.BOOLEAN,
|
|
|
|
description: "Make the lens square",
|
|
|
|
default: false,
|
|
|
|
},
|
|
|
|
|
2023-04-06 01:06:11 +00:00
|
|
|
zoom: {
|
|
|
|
description: "Zoom of the lens",
|
|
|
|
type: OptionType.SLIDER,
|
|
|
|
markers: makeRange(1, 50, 4),
|
|
|
|
default: 2,
|
|
|
|
stickToMarkers: false,
|
|
|
|
},
|
|
|
|
size: {
|
|
|
|
description: "Radius / Size of the lens",
|
|
|
|
type: OptionType.SLIDER,
|
|
|
|
markers: makeRange(50, 1000, 50),
|
|
|
|
default: 100,
|
|
|
|
stickToMarkers: false,
|
|
|
|
},
|
|
|
|
|
|
|
|
zoomSpeed: {
|
|
|
|
description: "How fast the zoom / lens size changes",
|
|
|
|
type: OptionType.SLIDER,
|
|
|
|
markers: makeRange(0.1, 5, 0.2),
|
|
|
|
default: 0.5,
|
|
|
|
stickToMarkers: false,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2023-04-15 00:47:03 +00:00
|
|
|
const imageContextMenuPatch: NavContextMenuPatchCallback = children => () => {
|
2023-04-13 02:22:38 +00:00
|
|
|
children.push(
|
|
|
|
<Menu.MenuGroup id="image-zoom">
|
2023-06-30 13:50:56 +00:00
|
|
|
<Menu.MenuCheckboxItem
|
|
|
|
id="vc-square"
|
|
|
|
label="Square Lens"
|
|
|
|
checked={settings.store.square}
|
|
|
|
action={() => {
|
|
|
|
settings.store.square = !settings.store.square;
|
2023-11-23 02:11:17 +00:00
|
|
|
ContextMenuApi.closeContextMenu();
|
2023-06-30 13:50:56 +00:00
|
|
|
}}
|
|
|
|
/>
|
2023-09-12 21:14:17 +00:00
|
|
|
<Menu.MenuCheckboxItem
|
|
|
|
id="vc-nearest-neighbour"
|
2023-10-25 21:37:07 +00:00
|
|
|
label="Nearest Neighbour"
|
2023-09-12 21:14:17 +00:00
|
|
|
checked={settings.store.nearestNeighbour}
|
|
|
|
action={() => {
|
|
|
|
settings.store.nearestNeighbour = !settings.store.nearestNeighbour;
|
2023-11-23 02:11:17 +00:00
|
|
|
ContextMenuApi.closeContextMenu();
|
2023-09-12 21:14:17 +00:00
|
|
|
}}
|
|
|
|
/>
|
2023-04-13 02:22:38 +00:00
|
|
|
<Menu.MenuControlItem
|
2023-06-30 13:50:56 +00:00
|
|
|
id="vc-zoom"
|
2023-04-13 02:22:38 +00:00
|
|
|
label="Zoom"
|
|
|
|
control={(props, ref) => (
|
|
|
|
<Menu.MenuSliderControl
|
|
|
|
ref={ref}
|
|
|
|
{...props}
|
|
|
|
minValue={1}
|
|
|
|
maxValue={50}
|
|
|
|
value={settings.store.zoom}
|
|
|
|
onChange={debounce((value: number) => settings.store.zoom = value, 100)}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
<Menu.MenuControlItem
|
2023-06-30 13:50:56 +00:00
|
|
|
id="vc-size"
|
2023-04-13 02:22:38 +00:00
|
|
|
label="Lens Size"
|
|
|
|
control={(props, ref) => (
|
|
|
|
<Menu.MenuSliderControl
|
|
|
|
ref={ref}
|
|
|
|
{...props}
|
|
|
|
minValue={50}
|
|
|
|
maxValue={1000}
|
|
|
|
value={settings.store.size}
|
|
|
|
onChange={debounce((value: number) => settings.store.size = value, 100)}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
<Menu.MenuControlItem
|
2023-06-30 13:50:56 +00:00
|
|
|
id="vc-zoom-speed"
|
2023-04-13 02:22:38 +00:00
|
|
|
label="Zoom Speed"
|
|
|
|
control={(props, ref) => (
|
|
|
|
<Menu.MenuSliderControl
|
|
|
|
ref={ref}
|
|
|
|
{...props}
|
|
|
|
minValue={0.1}
|
|
|
|
maxValue={5}
|
|
|
|
value={settings.store.zoomSpeed}
|
|
|
|
onChange={debounce((value: number) => settings.store.zoomSpeed = value, 100)}
|
|
|
|
renderValue={(value: number) => `${value.toFixed(3)}x`}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</Menu.MenuGroup>
|
|
|
|
);
|
2023-04-06 01:06:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default definePlugin({
|
|
|
|
name: "ImageZoom",
|
|
|
|
description: "Lets you zoom in to images and gifs. Use scroll wheel to zoom in and shift + scroll wheel to increase lens radius / size",
|
|
|
|
authors: [Devs.Aria],
|
2023-05-12 01:41:00 +00:00
|
|
|
tags: ["ImageUtilities"],
|
|
|
|
|
2023-04-06 01:06:11 +00:00
|
|
|
patches: [
|
|
|
|
{
|
2023-10-25 15:20:11 +00:00
|
|
|
find: "Messages.OPEN_IN_BROWSER",
|
2023-04-06 01:06:11 +00:00
|
|
|
replacement: {
|
2023-10-25 15:20:11 +00:00
|
|
|
// there are 2 image thingies. one for carosuel and one for the single image.
|
|
|
|
// so thats why i added global flag.
|
|
|
|
// also idk if this patch is good, should it be more specific?
|
|
|
|
// https://regex101.com/r/xfvNvV/1
|
|
|
|
match: /return.{1,200}\.wrapper.{1,200}src:\i,/g,
|
|
|
|
replace: `$&id: '${ELEMENT_ID}',`
|
2023-04-06 01:06:11 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
find: "handleImageLoad=",
|
|
|
|
replacement: [
|
|
|
|
{
|
2023-10-25 15:20:11 +00:00
|
|
|
match: /showThumbhashPlaceholder:\i,/,
|
2023-10-13 02:07:21 +00:00
|
|
|
replace: "...$self.makeProps(this),$&"
|
2023-04-06 01:06:11 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
{
|
2023-10-25 15:20:11 +00:00
|
|
|
match: /componentDidMount\(\){/,
|
2023-04-06 01:06:11 +00:00
|
|
|
replace: "$&$self.renderMagnifier(this);",
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
2023-10-25 15:20:11 +00:00
|
|
|
match: /componentWillUnmount\(\){/,
|
2023-04-06 01:06:11 +00:00
|
|
|
replace: "$&$self.unMountMagnifier();"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
2023-11-01 01:19:02 +00:00
|
|
|
{
|
|
|
|
find: ".carouselModal",
|
|
|
|
replacement: {
|
|
|
|
match: /(?<=\.carouselModal.{0,100}onClick:)\i,/,
|
|
|
|
replace: "()=>{},"
|
|
|
|
}
|
|
|
|
}
|
2023-04-06 01:06:11 +00:00
|
|
|
],
|
|
|
|
|
|
|
|
settings,
|
|
|
|
|
|
|
|
// to stop from rendering twice /shrug
|
|
|
|
currentMagnifierElement: null as React.FunctionComponentElement<MagnifierProps & JSX.IntrinsicAttributes> | null,
|
|
|
|
element: null as HTMLDivElement | null,
|
|
|
|
|
|
|
|
Magnifier,
|
|
|
|
root: null as Root | null,
|
|
|
|
makeProps(instance) {
|
|
|
|
return {
|
|
|
|
onMouseOver: () => this.onMouseOver(instance),
|
|
|
|
onMouseOut: () => this.onMouseOut(instance),
|
|
|
|
onMouseDown: (e: React.MouseEvent) => this.onMouseDown(e, instance),
|
|
|
|
onMouseUp: () => this.onMouseUp(instance),
|
|
|
|
id: instance.props.id,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
renderMagnifier(instance) {
|
|
|
|
if (instance.props.id === ELEMENT_ID) {
|
|
|
|
if (!this.currentMagnifierElement) {
|
|
|
|
this.currentMagnifierElement = <Magnifier size={settings.store.size} zoom={settings.store.zoom} instance={instance} />;
|
|
|
|
this.root = ReactDOM.createRoot(this.element!);
|
|
|
|
this.root.render(this.currentMagnifierElement);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
unMountMagnifier() {
|
|
|
|
this.root?.unmount();
|
|
|
|
this.currentMagnifierElement = null;
|
|
|
|
this.root = null;
|
|
|
|
},
|
|
|
|
|
|
|
|
onMouseOver(instance) {
|
|
|
|
instance.setState((state: any) => ({ ...state, mouseOver: true }));
|
|
|
|
},
|
|
|
|
onMouseOut(instance) {
|
|
|
|
instance.setState((state: any) => ({ ...state, mouseOver: false }));
|
|
|
|
},
|
|
|
|
onMouseDown(e: React.MouseEvent, instance) {
|
|
|
|
if (e.button === 0 /* left */)
|
|
|
|
instance.setState((state: any) => ({ ...state, mouseDown: true }));
|
|
|
|
},
|
|
|
|
onMouseUp(instance) {
|
|
|
|
instance.setState((state: any) => ({ ...state, mouseDown: false }));
|
|
|
|
},
|
|
|
|
|
|
|
|
start() {
|
2023-04-11 00:00:42 +00:00
|
|
|
enableStyle(styles);
|
2023-04-06 01:06:11 +00:00
|
|
|
addContextMenuPatch("image-context", imageContextMenuPatch);
|
|
|
|
this.element = document.createElement("div");
|
|
|
|
this.element.classList.add("MagnifierContainer");
|
|
|
|
document.body.appendChild(this.element);
|
|
|
|
},
|
|
|
|
|
|
|
|
stop() {
|
2023-04-11 00:00:42 +00:00
|
|
|
disableStyle(styles);
|
2023-04-06 01:06:11 +00:00
|
|
|
// so componenetWillUnMount gets called if Magnifier component is still alive
|
|
|
|
this.root && this.root.unmount();
|
|
|
|
this.element?.remove();
|
|
|
|
removeContextMenuPatch("image-context", imageContextMenuPatch);
|
|
|
|
}
|
|
|
|
});
|