Skip to content

Commit

Permalink
Add mouseMoveEvent, enterEvent and leaveEvent (#248)
Browse files Browse the repository at this point in the history
* impl mouseMoveEvent, enterEvent and leaveEvent

* update example

* mention new mouse events in the docs
  • Loading branch information
roman01la authored Mar 16, 2020
1 parent 0f24862 commit a135cd6
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 6 deletions.
2 changes: 1 addition & 1 deletion docs/components.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ desktop. More detailed information can be seen [here](components/Window.md)
| TouchableHighlight | activeOpacity, underlayColor, style, onPress, onLongPress |
| TouchableOpacity | activeOpacity, style, onPress, onLongPress |
| TouchableWithoutFeedback | onPress, onLongPress |
| View | style |
| View | style, onMouseMove, onMouseEnter, onMouseLeave |
22 changes: 22 additions & 0 deletions src/backends/qt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,34 @@ export function desktopSize(): Size {

export abstract class BaseElement {
element: any;
hasMouseTracking() {
return this.element.hasMouseTracking();
}
setMouseTracking(v: boolean) {
this.element.setMouseTracking(v);
}
mousePressEvent(func: () => void) {
this.element.mousePressEvent(func);
}
mouseReleaseEvent(func: () => void) {
this.element.mouseReleaseEvent(func);
}
mouseMoveEvent(func: (x: number, y: number) => void) {
// TODO: Mouse tracking should be turned of when the event handler
// is being removed (not changed to a new function)
// This probably needs teardown logic in propsUpdater function
if (!this.hasMouseTracking()) {
this.setMouseTracking(true);
}

this.element.mouseMoveEvent(func);
}
enterEvent(func: () => void) {
this.element.enterEvent(func);
}
leaveEvent(func: () => void) {
this.element.leaveEvent(func);
}
setStyleSheet(obj: object) {
this.element.setStyleSheet(convertStyleSheet(obj));
}
Expand Down
53 changes: 49 additions & 4 deletions src/components/View.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,24 @@ import convertStyleSheet from "../utils/convertStyleSheet";
import { YogaComponent } from "./YogaComponent";
import { getBackend } from "../backends/index";

interface Point {
x: number;
y: number;
}

interface MouseMoveEvent {
point: Point;
}

interface Props {
style: React.CSSProperties;
onResponderGrant: () => void;
onResponderRelease: () => void;
onMouseMove: (event: MouseMoveEvent) => void;
onMouseEnter: () => void;
onMouseLeave: () => void;
}

declare global {
namespace JSX {
interface IntrinsicElements {
Expand All @@ -30,12 +48,18 @@ export default (p: Props) => {
const propTypes = {
style: PropTypes.object,
onResponderGrant: PropTypes.func,
onResponderRelease: PropTypes.func
onResponderRelease: PropTypes.func,
onMouseMove: PropTypes.func,
onMouseEnter: PropTypes.func,
onMouseLeave: PropTypes.func
};
const defaultProps = {
style: {},
onResponderGrant: () => {},
onResponderRelease: () => {}
onResponderRelease: () => {},
onMouseMove: (event: MouseMoveEvent) => {},
onMouseEnter: () => {},
onMouseLeave: () => {}
};

const ViewElement = getBackend()["ViewElement"];
Expand All @@ -48,7 +72,10 @@ export default (p: Props) => {

const handlers = {
onResponderGrant: props.onResponderGrant,
onResponderRelease: props.onResponderRelease
onResponderRelease: props.onResponderRelease,
onMouseMove: props.onMouseMove,
onMouseEnter: props.onMouseEnter,
onMouseLeave: props.onMouseLeave
};

element.mousePressEvent(() => {
Expand All @@ -63,6 +90,18 @@ export default (p: Props) => {
}
});

element.mouseMoveEvent((x: number, y: number) => {
handlers.onMouseMove({ point: { x, y } });
});

element.enterEvent(() => {
handlers.onMouseEnter();
});

element.leaveEvent(() => {
handlers.onMouseLeave();
});

const containerProps = Container(
child => {
child.element.setParent(element);
Expand All @@ -85,7 +124,13 @@ export default (p: Props) => {
);

const updateProps = propsUpdater(
[handlers, "onResponderGrant", "onResponderRelease"],
[
handlers,
"onResponderGrant",
"onResponderRelease",
"onMouseEnter",
"onMouseLeave"
],
{
style: (style: React.CSSProperties) => {
element.setStyleSheet(style);
Expand Down
18 changes: 17 additions & 1 deletion testprog.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ class Example extends Component {
state = {
test: 'dsdasdsa',
a: true,
mouseState: 'idle',
x: 50,
y: 50,
};
componentDidMount() {
//setTimeout(() => this.setState({ test: "dsawewwww" }), 3000);
Expand All @@ -35,14 +38,27 @@ class Example extends Component {
style={{ height: '25%', width: '25%', backgroundColor: 'blue' }}
>
<View
onMouseEnter={() => this.setState({ mouseState: 'MOUSE ENTERED' })}
onMouseLeave={() => this.setState({ mouseState: 'MOUSE LEFT' })}
onMouseMove={event => this.setState(event.point)}
style={{ height: '50%', width: '100%', backgroundColor: 'green' }}
>
<Button
style={{ backgroundColor: 'white' }}
onPress={() => console.log('PRessed')}
title="My button"
/>
<Text>Hello</Text>
<Text>{`Mouse state: ${this.state.mouseState}`}</Text>
<View
style={{
width: 40,
height: 40,
backgroundColor: 'black',
position: 'absolute',
top: this.state.y,
left: this.state.x,
}}
></View>
</View>
</Window>
</App>
Expand Down

0 comments on commit a135cd6

Please sign in to comment.