Skip to content

Commit aeee567

Browse files
committed
updated all guides
1 parent 9c6b7f7 commit aeee567

File tree

8 files changed

+371
-75
lines changed

8 files changed

+371
-75
lines changed

src/demo.tsx

Lines changed: 42 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,51 @@
1-
import React, {useState} from "react";
2-
import {
3-
Renderer,
4-
Button,
5-
Window,
6-
TabItem,
7-
View,
8-
AnimatedImage,
9-
ComboBox,
10-
Text,
11-
Tabs
12-
} from "./index";
13-
import { QIcon, QVariant, QPushButtonSignals, QSize } from "@nodegui/nodegui";
14-
import { useEventHandler } from "./hooks";
15-
16-
const items = [
17-
{
18-
text: "hello",
19-
icon: new QIcon(
20-
"/Users/atulr/Project/nodegui/nodegui/src/lib/QtGui/__tests__/assets/nodegui.png"
21-
),
22-
userData: new QVariant(12346)
23-
},
24-
{ text: "world" }
25-
];
1+
import React from "react";
2+
import { Renderer, Text, ScrollArea, Window } from "./index";
263

274
const App = () => {
28-
const [titleCount, setTitleCount] = useState(0);
29-
const handler = useEventHandler<QPushButtonSignals>(
30-
{
31-
clicked: () => {
32-
console.log(titleCount);
33-
setTitleCount(titleCount + 1);
34-
}
35-
},
36-
[titleCount]
37-
);
38-
// TODO: need to figure out a way to add tab title and icon
395
return (
406
<Window>
41-
<Tabs tabPosition={0}>
42-
<TabItem
43-
title={`title-${titleCount}`}
44-
icon={
45-
new QIcon(
46-
"/Users/atulr/Project/nodegui/nodegui/src/lib/QtGui/__tests__/assets/nodegui.png"
47-
)
48-
}
49-
>
50-
<View>
51-
<Button on={handler} style={buttonStyle} text={"change title"} />
52-
</View>
53-
</TabItem>
54-
</Tabs>
7+
<ScrollArea>
8+
<Text>
9+
{`
10+
Contrary to popular belief,
11+
Lorem Ipsum is not simply random text.
12+
It has roots in a piece of classical Latin literature from 45 BC,
13+
making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia,
14+
looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage,
15+
and going through the cites of the word in classical literature,
16+
discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32
17+
and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC.
18+
This book is a treatise on the theory of ethics, very popular during the Renaissance.
19+
The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.
20+
21+
The standard chunk of Lorem Ipsum used since the 1500s
22+
is reproduced below for those interested.
23+
Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also
24+
reproduced in their exact original form, accompanied
25+
by English versions from the 1914 translation by H. Rackham.
26+
27+
28+
Why do we use it?
29+
30+
It is a long established
31+
fact that a reader will be distracted by
32+
the readable content of a page when looking at its layout.
33+
The point of using Lorem Ipsum is that it has
34+
a more-or-less normal distribution of letters,
35+
as opposed to using 'Content here, content here',
36+
making it look like readable English.
37+
Many desktop publishing packages and web page
38+
editors now use Lorem Ipsum as their default model text,
39+
and a search for 'lorem ipsum' will uncover many web
40+
sites still in their infancy. Various versions
41+
have evolved over the years, sometimes by accident,
42+
sometimes on purpose (injected humour and the like).
43+
44+
`}
45+
</Text>
46+
</ScrollArea>
5547
</Window>
5648
);
5749
};
5850

59-
const containerStyle = `
60-
flex: 1;
61-
justify-content:'center';
62-
border: 1px solid blue;
63-
padding: 10;
64-
`;
65-
const buttonStyle = `
66-
color: white;
67-
`;
68-
6951
Renderer.render(<App />);
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
2-
sidebar_label: Custom NodeGui Plugin
3-
title: Custom NodeGui Plugin
2+
sidebar_label: Custom React NodeGui Plugin
3+
title: Custom React NodeGui Plugin
44
---
55

66
WIP

website/docs/guides/handle-events.md

Lines changed: 182 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,185 @@ sidebar_label: Handle Events
33
title: Handle Events
44
---
55

6-
WIP
6+
React NodeGui allows you to listen to various events that might originate from the underlying Qt widgets. These events can either be a simple button click or a text change on a lineedit or even something like window being hidden and shown.
7+
8+
In order to do this we need to attach an event listener to the respective widget.
9+
10+
Technically, the event listener is a NodeJs [EventEmitter](https://nodejs.org/api/events.html#events_class_eventemitter) instance that listens to events from the underlying Qt widget. The native Qt widget would send all the events to the event emitter in React NodeGui world and the user can essentially subscribe to it.
11+
12+
Lets see an example to see how this looks in practice.
13+
14+
## Event handling
15+
16+
The following example demonstrates how to add a clicked event listener to a button widget.
17+
18+
<img src="https://github.com/nodegui/react-nodegui/releases/download/assets/events-react.gif" alt="event example" style={{width: '100%', maxWidth: 400}}/>
19+
20+
```javascript
21+
const React = require("react");
22+
const { Renderer, Button, Window } = require("@nodegui/react-nodegui");
23+
24+
const App = () => {
25+
const buttonHandler = {
26+
clicked: () => {
27+
console.log("the button was clicked");
28+
}
29+
};
30+
31+
return (
32+
<Window>
33+
<Button text={"Click me"} on={buttonHandler} />
34+
</Window>
35+
);
36+
};
37+
38+
Renderer.render(<App />);
39+
```
40+
41+
The `on` prop accepts a simple object map with event type as key and a handler function as callback for the event. You can register multiple event handlers by passing multiple events as keys and their handlers as values.
42+
43+
Internally, Qt widgets in nodegui has two types of events.:
44+
45+
- Signals: In short these are basically different for different widgets. So a button maybe have `clicked`, `pressed` signal, while a linedit may have `textChanged` signal.
46+
- QEvents: These are common set of events for all the widgets/qobjects in NodeGui world. These are also helpful at times but typically you would end up using signals more than these common events.
47+
48+
In React NodeGui you can listen to both Signals and QEvents using the same `on` prop.
49+
50+
### useEventHandler hook and typescript support
51+
52+
Although you can pass in an object with event handlers to the `on` prop, its not the most efficient way. This is because everytime the render is called the `on` prop will get a new object meaning the widget will re-render every time. To solve for this we have `useEventHandler` hook.
53+
54+
```ts
55+
import React from "react";
56+
import {
57+
Renderer,
58+
Button,
59+
Window,
60+
useEventHandler
61+
} from "@nodegui/react-nodegui";
62+
import { QPushButtonSignals } from "@nodegui/nodegui";
63+
64+
const App = () => {
65+
const buttonHandler = useEventHandler<QPushButtonSignals>(
66+
{
67+
clicked: () => {
68+
console.log("the button was clicked");
69+
},
70+
pressed: () => {
71+
console.log("button was pressed");
72+
},
73+
objectNameChanged: objectName => {
74+
console.log("new object name", objectName);
75+
}
76+
},
77+
[]
78+
);
79+
80+
return (
81+
<Window>
82+
<Button text={"Click me"} on={buttonHandler} />
83+
</Window>
84+
);
85+
};
86+
87+
Renderer.render(<App />);
88+
```
89+
90+
In a nutshell, the above code uses the `useEventHandler` hook which is a wrapper over `useMemo`.
91+
This means, the buttonHandler remains same on every render call and hence the `on` prop to Button doesnt change.
92+
93+
Here `objectNameChanged` is a QEvent while `clicked` and `pressed` are signals. As an app developer it really doesnt mean much but internally they are both two different things in Qt and React NodeGui allows you to use both of them using a single familar `on` prop.
94+
95+
Also, another point you see in this typescript code is the QPushButtonSignals. The QPushButtonSignals is a type that allows autocompletion of event handlers as you type them.
96+
97+
### How do I know which events are supported ?
98+
99+
In order to find all the supported events for a widget you can take a look at
100+
101+
#### All Signals for the widgets:
102+
103+
- [https://docs.nodegui.org/docs/api/generated/globals/#interfaces](https://docs.nodegui.org/docs/api/generated/globals/#interfaces)
104+
- [https://docs.nodegui.org/docs/api/generated/globals/#type-aliases](https://docs.nodegui.org/docs/api/generated/globals/#type-aliases)
105+
106+
You can subscribe to a signal like so:
107+
108+
```ts
109+
import React from "react";
110+
import {
111+
Renderer,
112+
Button,
113+
Window,
114+
useEventHandler
115+
} from "@nodegui/react-nodegui";
116+
import { QPushButtonSignals } from "@nodegui/nodegui";
117+
118+
const App = () => {
119+
const buttonHandler = useEventHandler<QPushButtonSignals>(
120+
{
121+
clicked: () => {
122+
console.log("the button was clicked");
123+
}
124+
},
125+
[]
126+
);
127+
128+
return (
129+
<Window>
130+
<Button text={"Click me"} on={buttonHandler} />
131+
</Window>
132+
);
133+
};
134+
135+
Renderer.render(<App />);
136+
```
137+
138+
The value you receive in the callback depends on the signal. Refer to respective signal docs for more details. All the handlers are also typed. So if you are using typescript you should get correct autocomplete for it.
139+
140+
#### All common QEvents for the widgets
141+
142+
In nodegui all these common QEvents are represented under an enum type: [WidgetEventTypes](https://docs.nodegui.org/docs/api/generated/enums/widgeteventtypes)
143+
144+
You can subscribe to a QEvent like so:
145+
146+
```typescript
147+
import React from "react";
148+
import {
149+
Renderer,
150+
Text,
151+
Window,
152+
useEventHandler
153+
} from "@nodegui/react-nodegui";
154+
import { QLabelSignals, QMouseEvent, WidgetEventTypes } from "@nodegui/nodegui";
155+
156+
const App = () => {
157+
const textHandler = useEventHandler<QLabelSignals>(
158+
{
159+
MouseMove: (nativeEvt: any) => {
160+
const mouseEvt = new QMouseEvent(nativeEvt);
161+
console.log("mouseMoved at: ", { x: mouseEvt.x(), y: mouseEvt.y() });
162+
},
163+
[WidgetEventTypes.MouseButtonPress]: () => {
164+
console.log("mouse button was pressed");
165+
}
166+
},
167+
[]
168+
);
169+
170+
return (
171+
<Window>
172+
<Text mouseTracking={true} on={textHandler}>
173+
Move your mouse here
174+
</Text>
175+
</Window>
176+
);
177+
};
178+
179+
Renderer.render(<App />);
180+
```
181+
182+
<img src="https://github.com/nodegui/react-nodegui/releases/download/assets/qevents.gif" alt="qevent example" style={{width: '100%', maxWidth: 400}}/>
183+
184+
Note here that every QEvent handler gives a reference to native QEvent in the handler callback.
185+
Not all native QEvent wrappers are implemented yet and we might need your help regarding those. Feel free to jump in and contribute to the nodegui core.
186+
187+
Also you can specify the QEvent type as a regular `MouseMove` string or use it directly from the enum `WidgetEventTypes.MouseMove`. They both achieve same things.

website/docs/guides/images.md

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,55 @@ sidebar_label: Images
33
title: Images
44
---
55

6-
WIP
6+
Images are very important for making your app more interesting.
7+
8+
In React NodeGui, `<Image>` is used to display an image.
9+
10+
Internally Image is a QLabel. QLabel is typically used for displaying text, but it can also display an image.
11+
What this means is that you can pass all the props you pass to a `<Text>` to `<Image>` also.
12+
13+
A very minimal example would look like this:
14+
15+
```js
16+
import React from "react";
17+
import { Renderer, Image, Window } from "@nodegui/react-nodegui";
18+
19+
const App = () => {
20+
return (
21+
<Window>
22+
<Image src="https://docs.nodegui.org/img/logo-circle.png" />
23+
</Window>
24+
);
25+
};
26+
27+
Renderer.render(<App />);
28+
```
29+
30+
Here,
31+
32+
- Image is a native QLabel component that sets the image as its pixmap.
33+
34+
The result would look like this:
35+
36+
<img src="https://github.com/nodegui/react-nodegui/releases/download/assets/images.png" alt="image example" style={{width: '100%', maxWidth: 400}}/>
37+
38+
## Some tips
39+
40+
### Showing large images
41+
42+
The above examples wont allow you to show a huge image without either cutting it off or making the widget huge.
43+
44+
In order to do that:
45+
46+
- You can create the image instance using `<Image>`
47+
- Set the image instance as a child to a `<ScrollArea>`
48+
49+
### Animated images
50+
51+
In order to use animated images, instead of `<Image>` use `<AnimatedImage>`
52+
53+
### Loading an image from a buffer
54+
55+
Lets say we want to load an image from a buffer. In this case we can't use the `src` prop since its only reserved for local file system path or urls.
56+
57+
In this case use the `buffer` prop.

website/docs/guides/networking.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,29 @@ sidebar_label: Networking
33
title: Networking
44
---
55

6-
WIP
6+
Many apps need to load resources from a remote URL. You may want to make a POST request to a REST API, or you may need to fetch a chunk of static content from another server.
7+
8+
Remember that NodeGui apps do not run in a browser and hence do not have access to browser apis. NodeGui app is essentially a Node.js app.
9+
10+
And in a typical Node.js application you would use a third party library like [axios](https://github.com/axios/axios), [node-fetch](https://github.com/node-fetch/node-fetch) or [frisbee](https://github.com/niftylettuce/frisbee) for achieving this functionality.
11+
12+
## Using Node Fetch
13+
14+
[Node Fetch](https://github.com/node-fetch/node-fetch) is a light-weight module that brings window.fetch to Node.js.
15+
16+
An example usage would look like this:
17+
18+
```js
19+
const fetch = require("node-fetch");
20+
async function getData() {
21+
try {
22+
let response = await fetch("https://somewebsite.com/some.json");
23+
let responseJson = await response.json();
24+
return responseJson.somecontent;
25+
} catch (error) {
26+
console.error(error);
27+
}
28+
}
29+
```
30+
31+
Take a look at the [Node Fetch docs](https://github.com/node-fetch/node-fetch) for a full list of properties.

0 commit comments

Comments
 (0)