-
Notifications
You must be signed in to change notification settings - Fork 16
Enable cursor support #225
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -7,6 +7,7 @@ export default class Canvas extends Component{ | |||||||||||||
|
|
||||||||||||||
| this.canvasRef = React.createRef(); | ||||||||||||||
| this.boundHandleKeyPress = this.handleKeyPress.bind(this) | ||||||||||||||
| this.boundOnCursorChange = this.onCursorChange.bind(this) | ||||||||||||||
| this.state = { contextMenu: null }; | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
|
|
@@ -15,6 +16,9 @@ export default class Canvas extends Component{ | |||||||||||||
| // set the paint callback | ||||||||||||||
| this.props.core.canvas.setExternalPaintCallbackFunction(this.paint.bind(this)) | ||||||||||||||
|
|
||||||||||||||
| // set the cursor callback | ||||||||||||||
| this.props.core.canvas.setCursorCallbackFunction(this.boundOnCursorChange) | ||||||||||||||
|
|
||||||||||||||
| // add keydown eventlistener | ||||||||||||||
| document.addEventListener("keydown", this.boundHandleKeyPress) | ||||||||||||||
|
|
||||||||||||||
|
|
@@ -28,14 +32,28 @@ export default class Canvas extends Component{ | |||||||||||||
|
|
||||||||||||||
| componentDidUpdate(prevProps) { | ||||||||||||||
| if (prevProps.core !== this.props.core) { | ||||||||||||||
|
||||||||||||||
| if (prevProps.core !== this.props.core) { | |
| if (prevProps.core !== this.props.core) { | |
| prevProps.core.canvas.setCursorCallbackFunction(undefined); |
Copilot
AI
Apr 5, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
onCursorChange assumes this.canvasRef.current is always available. If a cursor update arrives after unmount (or while swapping cores), this will throw. Add a quick guard (e.g., return early when the ref is missing) to make the callback resilient.
| this.canvasRef.current.style.cursor = cursors[state] ?? 'crosshair'; | |
| const canvas = this.canvasRef.current; | |
| if (!canvas) { | |
| return; | |
| } | |
| canvas.style.cursor = cursors[state] ?? 'crosshair'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this.onCursorChange.bind(this)creates a new function each timecomponentDidMount/componentDidUpdateruns. Consider binding once (e.g.,this.boundOnCursorChange = this.onCursorChange.bind(this)in the constructor) and reusing that reference when registering/unregistering the callback; this avoids repeated allocations and makes cleanup reliable if the core API expects the same function reference.