Skip to content

Commit f98f272

Browse files
committed
Added Image view example
1 parent e390835 commit f98f272

5 files changed

Lines changed: 173 additions & 9 deletions

File tree

examples/image-view/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Image viewer app
2+
3+
This example showcases how to build a basic image viewer clone.
4+
5+
**Screenshot:**
6+
7+
<img alt="demo_mac" src="https://github.com/master-atul/react-desktop/raw/master/examples/image-view/image_view_mac.png" height="400" />
8+
<img alt="demo_linux" src="https://github.com/master-atul/react-desktop/raw/master/examples/image-view/image_view_linux.png" height="400" />
9+
10+
To run the demo:
11+
12+
1. `yarn build`
13+
14+
2. `yarn qode dist/examples/image-view/index.js`

examples/image-view/index.tsx

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import {
2+
Renderer,
3+
View,
4+
Button,
5+
Window,
6+
Image,
7+
LineEdit
8+
} from "../../src/index";
9+
import React, { useEffect, useRef, useMemo, useState } from "react";
10+
import {
11+
AspectRatioMode,
12+
QMainWindow,
13+
QLineEditEvents,
14+
QPushButtonEvents
15+
} from "@nodegui/nodegui";
16+
17+
const App = () => {
18+
const winRef = useRef<QMainWindow>(null);
19+
const [fileUrl, setFileUrl] = useState();
20+
const [imageSrc, setImageSrc] = useState();
21+
useEffect(() => {
22+
if (winRef.current) {
23+
winRef.current.resize(800, 450);
24+
}
25+
}, []);
26+
const lineEditHandler = useMemo(
27+
() => ({
28+
[QLineEditEvents.textChanged]: (text: string) => {
29+
setFileUrl(text);
30+
}
31+
}),
32+
[]
33+
);
34+
35+
const loadButtonHandler = useMemo(
36+
() => ({
37+
[QPushButtonEvents.clicked]: () => {
38+
setImageSrc(fileUrl);
39+
}
40+
}),
41+
[fileUrl]
42+
);
43+
44+
return (
45+
<>
46+
<Window ref={winRef} styleSheet={styleSheet}>
47+
<View id="container">
48+
<View id="controls">
49+
<LineEdit
50+
on={lineEditHandler}
51+
id="textField"
52+
text={fileUrl}
53+
placeholderText="Absolute path to an image"
54+
/>
55+
<Button text="Load Image" on={loadButtonHandler} />
56+
</View>
57+
<Image
58+
id="img"
59+
aspectRatioMode={AspectRatioMode.KeepAspectRatio}
60+
src={imageSrc}
61+
></Image>
62+
</View>
63+
</Window>
64+
</>
65+
);
66+
};
67+
68+
const styleSheet = `
69+
#container {
70+
qproperty-flex: 1;
71+
qproperty-minHeight: '100%';
72+
}
73+
#controls {
74+
qproperty-flexDirection: 'row';
75+
qproperty-justifyContent: 'space-around';
76+
qproperty-alignItems: 'center';
77+
qproperty-paddingHorizontal: 20;
78+
qproperty-paddingVertical: 10;
79+
}
80+
#img {
81+
qproperty-flex: 1;
82+
qproperty-alignment: 'AlignCenter';
83+
}
84+
#textField {
85+
qproperty-flex: 1;
86+
}
87+
`;
88+
89+
Renderer.render(<App />, {
90+
onRender: () => {
91+
console.log("Yo");
92+
},
93+
enableDevtools: true
94+
});

src/components/Image/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ const setProps = (
1616
set src(imageUrl: string) {
1717
const pixMap = new QPixmap(imageUrl);
1818
widget.setPixmap(pixMap);
19+
const size = widget.size();
20+
widget.scalePixmap(size.width, size.height);
1921
},
2022
set aspectRatioMode(mode: AspectRatioMode) {
2123
widget.setAspectRatioMode(mode);

src/components/LineEdit/index.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { ViewProps, setProps as setViewProps } from "../View";
55
interface LineEditProps extends ViewProps {
66
children?: string;
77
text?: string;
8+
placeholderText?: string;
89
}
910

1011
const setProps = (
@@ -13,9 +14,12 @@ const setProps = (
1314
oldProps: LineEditProps
1415
) => {
1516
const setter: LineEditProps = {
16-
// set text(checkboxText: string) {
17-
// widget.setText(checkboxText);
18-
// }
17+
set text(text: string) {
18+
widget.setText(text);
19+
},
20+
set placeholderText(text: string) {
21+
widget.setPlaceholderText(text);
22+
}
1923
};
2024
Object.assign(setter, newProps);
2125
setViewProps(widget, newProps, oldProps);

src/demo.tsx

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,56 @@
1-
import { Renderer, View, Window, Image } from "./index";
2-
import React from "react";
3-
import { AspectRatioMode } from "@nodegui/nodegui";
1+
import { Renderer, View, Button, Window, Image, LineEdit } from "./index";
2+
import React, { useEffect, useRef, useMemo, useState } from "react";
3+
import {
4+
AspectRatioMode,
5+
QMainWindow,
6+
QLineEditEvents,
7+
QPushButtonEvents
8+
} from "@nodegui/nodegui";
49

510
const App = () => {
11+
const winRef = useRef<QMainWindow>(null);
12+
const [fileUrl, setFileUrl] = useState();
13+
const [imageSrc, setImageSrc] = useState();
14+
useEffect(() => {
15+
if (winRef.current) {
16+
winRef.current.resize(800, 450);
17+
}
18+
}, []);
19+
const lineEditHandler = useMemo(
20+
() => ({
21+
[QLineEditEvents.textChanged]: (text: string) => {
22+
setFileUrl(text);
23+
}
24+
}),
25+
[]
26+
);
27+
28+
const loadButtonHandler = useMemo(
29+
() => ({
30+
[QPushButtonEvents.clicked]: () => {
31+
setImageSrc(fileUrl);
32+
}
33+
}),
34+
[fileUrl]
35+
);
36+
637
return (
738
<>
8-
<Window styleSheet={styleSheet}>
39+
<Window ref={winRef} styleSheet={styleSheet}>
940
<View id="container">
41+
<View id="controls">
42+
<LineEdit
43+
on={lineEditHandler}
44+
id="textField"
45+
text={fileUrl}
46+
placeholderText="Absolute path to an image"
47+
/>
48+
<Button text="Load Image" on={loadButtonHandler} />
49+
</View>
1050
<Image
1151
id="img"
1252
aspectRatioMode={AspectRatioMode.KeepAspectRatio}
13-
src="/Users/atulr/Downloads/demo.png"
53+
src={imageSrc}
1454
></Image>
1555
</View>
1656
</Window>
@@ -21,11 +61,21 @@ const App = () => {
2161
const styleSheet = `
2262
#container {
2363
qproperty-flex: 1;
24-
qproperty-flexDirection: column;
2564
qproperty-minHeight: '100%';
2665
}
66+
#controls {
67+
qproperty-flexDirection: 'row';
68+
qproperty-justifyContent: 'space-around';
69+
qproperty-alignItems: 'center';
70+
qproperty-paddingHorizontal: 20;
71+
qproperty-paddingVertical: 10;
72+
}
2773
#img {
2874
qproperty-flex: 1;
75+
qproperty-alignment: 'AlignCenter';
76+
}
77+
#textField {
78+
qproperty-flex: 1;
2979
}
3080
`;
3181

0 commit comments

Comments
 (0)