Skip to content

Commit

Permalink
Reset preview on rcc load
Browse files Browse the repository at this point in the history
  • Loading branch information
nekiro committed Sep 28, 2022
1 parent 0639c34 commit 19fa951
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 43 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "rcc-editor",
"productName": "rcc-editor",
"version": "2.0.0",
"version": "2.0.1",
"description": "edit qt .rcc files",
"main": ".webpack/main",
"scripts": {
Expand Down
28 changes: 0 additions & 28 deletions src/renderer/app.tsx
Original file line number Diff line number Diff line change
@@ -1,31 +1,3 @@
/**
* This file will automatically be loaded by webpack and run in the "renderer" context.
* To learn more about the differences between the "main" and the "renderer" context in
* Electron, visit:
*
* https://electronjs.org/docs/latest/tutorial/process-model
*
* By default, Node.js integration in this file is disabled. When enabling Node.js integration
* in a renderer process, please be aware of potential security implications. You can read
* more about security risks here:
*
* https://electronjs.org/docs/tutorial/security
*
* To enable Node.js integration in this file, open up `main.js` and enable the `nodeIntegration`
* flag:
*
* ```
* // Create the browser window.
* mainWindow = new BrowserWindow({
* width: 800,
* height: 600,
* webPreferences: {
* nodeIntegration: true
* }
* });
* ```
*/

import React from 'react';
import ReactDOM from 'react-dom/client';
import MainScreen from './screens/MainScreen';
Expand Down
10 changes: 2 additions & 8 deletions src/renderer/components/Preview/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,7 @@ import React from 'react';
import useSelectedElement from '../../hooks/useSelectedElement';
import styles from './style.css';

type PreviewProps = {
visible: boolean;
};

export default function Preview({
visible,
}: PreviewProps & React.ImgHTMLAttributes<HTMLImageElement>) {
export default function Preview() {
const [selected, setSelected] = useSelectedElement();

const onDropToPreview = async (event: React.DragEvent<HTMLImageElement>) => {
Expand All @@ -34,7 +28,7 @@ export default function Preview({
<img
onDrop={onDropToPreview}
onDragOver={onDragOverToPreview}
className={`${styles.preview} ${visible ? styles.visible : ''}`}
className={`${styles.preview} ${selected.element ? styles.visible : ''}`}
src={
selected.element ? `data:image/png;base64,${selected.image?.data}` : ''
}
Expand Down
4 changes: 2 additions & 2 deletions src/renderer/hooks/useSelectedElement.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Image } from '../../types/Image';

type SelectedElementContext = [
{ element?: HTMLButtonElement; image?: Image },
(element: HTMLButtonElement, image: Image) => void,
(element?: HTMLButtonElement, image?: Image) => void,
];

const context = createContext<SelectedElementContext>([{}, () => true]);
Expand All @@ -19,7 +19,7 @@ export const SelectedElementProvider = ({ children }: PropsWithChildren) => {
image?: Image;
}>({});

const setElement = (element: HTMLButtonElement, image: Image) =>
const setElement = (element?: HTMLButtonElement, image?: Image) =>
setElementState({
element,
image,
Expand Down
11 changes: 7 additions & 4 deletions src/renderer/screens/MainScreen/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@ import SelectableList from '../../components/SelectableList';
import styles from './style.css';
import { Image } from '../../../types/Image';
import Preview from '../../components/Preview';
import useSelectedElement from '../../hooks/useSelectedElement';
import useSelectedElement from '../../../renderer/hooks/useSelectedElement';

export default function MainScreen() {
const [images, setImages] = useState<Image[]>([]);
const [selectedElement] = useSelectedElement();
const [, setElement] = useSelectedElement();

useEffect(() => {
window.api.handlers.listImages.subscribe(
(_: Electron.IpcRendererEvent, images: Image[]) => setImages(images),
(_: Electron.IpcRendererEvent, images: Image[]) => {
setElement();
setImages(images);
},
);
window.api.handlers.listImage.subscribe(
(_: Electron.IpcRendererEvent, index: number, image: Image) =>
Expand All @@ -32,7 +35,7 @@ export default function MainScreen() {
<div
className={`${styles['center-container']} ${styles['miniature-holder']}`}
>
<Preview visible={!!selectedElement} />
<Preview />
</div>
</div>
);
Expand Down

0 comments on commit 19fa951

Please sign in to comment.