Skip to content

Commit

Permalink
feat: consistent tool switching
Browse files Browse the repository at this point in the history
  • Loading branch information
3akev committed Sep 22, 2024
1 parent 9f1f9ac commit c3d2ac2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
13 changes: 11 additions & 2 deletions lib/components/canvas/canvas_gesture_detector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class CanvasGestureDetector extends StatefulWidget {
required this.onDrawUpdate,
required this.onDrawEnd,
required this.onPressureChanged,
required this.onHovering,
required this.onHoveringEnd,
required this.onStylusButtonChanged,
required this.undo,
required this.redo,
Expand All @@ -49,6 +51,8 @@ class CanvasGestureDetector extends StatefulWidget {
/// Called when the pressure of the stylus changes,
/// pressure is negative if stylus button is pressed
final ValueChanged<double?> onPressureChanged;
final VoidCallback onHovering;
final VoidCallback onHoveringEnd;
final ValueChanged<bool> onStylusButtonChanged;

final VoidCallback undo;
Expand Down Expand Up @@ -407,9 +411,14 @@ class CanvasGestureDetectorState extends State<CanvasGestureDetector> {
bool stylusButtonWasPressed = false;

void _listenerPointerHoverEvent(PointerEvent event) {
if (event.kind != PointerDeviceKind.stylus) return;

// Apparently flutter synthesizes a hover event on pointer down,
// so these need to be excluded here.
if (event.kind == PointerDeviceKind.stylus && !event.synthesized) {
// so these are used to detect when hovering ends
if (event.synthesized) {
widget.onHoveringEnd();
} else {
widget.onHovering();
if (stylusButtonWasPressed != (event.buttons == kPrimaryStylusButton)) {
stylusButtonWasPressed = event.buttons == kPrimaryStylusButton;
widget.onStylusButtonChanged(stylusButtonWasPressed);
Expand Down
20 changes: 13 additions & 7 deletions lib/pages/editor/editor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ class EditorState extends State<Editor> {
/// Used to record a move in the history.
Offset moveOffset = Offset.zero;

bool currentGestureIsDrawing = false;
bool isHovering = true;
int? dragPageIndex;
double? currentPressure;
bool isDrawGesture(ScaleStartDetails details) {
Expand Down Expand Up @@ -551,7 +551,6 @@ class EditorState extends State<Editor> {
}

void onDrawStart(ScaleStartDetails details) {
currentGestureIsDrawing = true;
final page = coreInfo.pages[dragPageIndex!];
final position = page.renderBox!.globalToLocal(details.focalPoint);
history.canRedo = false;
Expand Down Expand Up @@ -641,7 +640,6 @@ class EditorState extends State<Editor> {
}

void onDrawEnd(ScaleEndDetails details) {
currentGestureIsDrawing = false;
final page = coreInfo.pages[dragPageIndex!];
bool shouldSave = true;
if (PencilSound.isPlaying) PencilSound.pause();
Expand Down Expand Up @@ -731,15 +729,21 @@ class EditorState extends State<Editor> {
currentPressure = pressure == 0 ? null : pressure;
}

void onHovering() {
isHovering = true;
}

void onHoveringEnd() {
isHovering = false;
}

void onStylusButtonChanged(bool buttonPressed) {
// whether the stylus button is or was pressed
stylusButtonPressed = stylusButtonPressed || buttonPressed;

if (currentGestureIsDrawing) return;

if (!buttonPressed) {
if (isHovering && !buttonPressed) {
if (tmpTool != null) {
// was pressed while hovering, but no longer is
// while hovering, button was pressed, but no longer is => restore tool
currentTool = tmpTool!;
tmpTool = null;
setState(() {});
Expand Down Expand Up @@ -1374,6 +1378,8 @@ class EditorState extends State<Editor> {
onDrawStart: onDrawStart,
onDrawUpdate: onDrawUpdate,
onDrawEnd: onDrawEnd,
onHovering: onHovering,
onHoveringEnd: onHoveringEnd,
onStylusButtonChanged: onStylusButtonChanged,
onPressureChanged: onPressureChanged,
undo: undo,
Expand Down

0 comments on commit c3d2ac2

Please sign in to comment.