Skip to content

Commit

Permalink
Some styluses do not send move events when button is pressed. So Eras…
Browse files Browse the repository at this point in the history
…er do not work.

This change allows switch to eraser by pressing and releasing stylus button (no more holding button), then erasing items work.

Creates new preference, which can change default behavior of pressing button when pen is above screen.

Solves issue saber-notes#1410
  • Loading branch information
QubaB committed Feb 16, 2025
1 parent 7f45b1d commit 1d4267a
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 10 deletions.
2 changes: 2 additions & 0 deletions lib/data/prefs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ abstract class Prefs {
static late final PlainPref<bool> autoClearWhiteboardOnExit;

static late final PlainPref<bool> disableEraserAfterUse;
static late final PlainPref<bool> eraserOnStylusButtonPressAndRelease;
static late final PlainPref<bool> hideFingerDrawingToggle;

static late final PlainPref<List<String>> recentColorsChronological;
Expand Down Expand Up @@ -209,6 +210,7 @@ abstract class Prefs {
autoClearWhiteboardOnExit = PlainPref('autoClearWhiteboardOnExit', false);

disableEraserAfterUse = PlainPref('disableEraserAfterUse', false);
eraserOnStylusButtonPressAndRelease = PlainPref('eraserOnStylusButtonPressAndRelease', false);
hideFingerDrawingToggle = PlainPref('hideFingerDrawingToggle', false);

recentColorsChronological = PlainPref('recentColorsChronological', []);
Expand Down
2 changes: 2 additions & 0 deletions lib/i18n/strings_en.g.dart
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,7 @@ class TranslationsSettingsPrefLabelsEn {
String get maxImageSize => 'Maximum image size';
String get autoClearWhiteboardOnExit => 'Auto-clear the whiteboard';
String get disableEraserAfterUse => 'Auto-disable the eraser';
String get eraserOnStylusButtonPressAndRelease => 'Eraser on stylus button press and release';
String get hideFingerDrawingToggle => 'Hide the finger drawing toggle';
String get editorPromptRename => 'Prompt you to rename new notes';
String get hideHomeBackgrounds => 'Hide backgrounds on the home screen';
Expand All @@ -403,6 +404,7 @@ class TranslationsSettingsPrefDescriptionsEn {
String get preferGreyscale => 'For e-ink displays';
String get autoClearWhiteboardOnExit => 'Clears the whiteboard after you exit the app';
String get disableEraserAfterUse => 'Automatically switches back to the pen after using the eraser';
String get eraserOnStylusButtonPressAndRelease => 'Switch to eraser pressing stylus button and releasing it';
String get maxImageSize => 'Larger images will be compressed';
late final TranslationsSettingsPrefDescriptionsHideFingerDrawingEn hideFingerDrawing = TranslationsSettingsPrefDescriptionsHideFingerDrawingEn.internal(_root);
String get editorPromptRename => 'You can always rename notes later';
Expand Down
49 changes: 39 additions & 10 deletions lib/pages/editor/editor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -891,20 +891,49 @@ class EditorState extends State<Editor> {
// whether the stylus button is or was pressed
stylusButtonPressed = stylusButtonPressed || buttonPressed;

if (isHovering) {
if (buttonPressed) {
if (currentTool is Eraser) return;
tmpTool = currentTool;
currentTool = Eraser();
setState(() {});
} else {
if (tmpTool != null) {
currentTool = tmpTool!;
tmpTool = null;
if (!Prefs.eraserOnStylusButtonPressAndRelease.value) {
// standard behavior of stylus button, while holding is erasing
if (isHovering) {
if (buttonPressed) {
if (currentTool is Eraser) return;
tmpTool = currentTool;
currentTool = Eraser();
setState(() {});
} else {
if (tmpTool != null) {
currentTool = tmpTool!;
tmpTool = null;
setState(() {});
}
}
}
}
else {
// some pens do not send moving events when stylus button is pressed
// so switch to eraser when button is pressed and back on next press
if (isHovering) {
if (buttonPressed) {
// switch to Eraser
if (currentTool is Eraser) {
if (tmpTool != null) {
// change back original tool
currentTool = tmpTool!;
tmpTool = null;
setState(() {});
}
else {
return; // when I am on eraser and previous tool is not set, it means that Eraser is main tool
}
}
else {
tmpTool = currentTool;
currentTool = Eraser();
setState(() {});
}
}
}
}

}

void onMoveImage(EditorImage image, Rect offset) {
Expand Down
7 changes: 7 additions & 0 deletions lib/pages/home/settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,12 @@ class _SettingsPageState extends State<SettingsPage> {
icon: FontAwesomeIcons.eraser,
pref: Prefs.disableEraserAfterUse,
),
SettingsSwitch(
title: t.settings.prefLabels.eraserOnStylusButtonPressAndRelease,
subtitle: t.settings.prefDescriptions.eraserOnStylusButtonPressAndRelease,
icon: FontAwesomeIcons.eraser,
pref: Prefs.eraserOnStylusButtonPressAndRelease,
),
SettingsSwitch(
title: t.settings.prefLabels.hideFingerDrawingToggle,
subtitle: () {
Expand Down Expand Up @@ -626,3 +632,4 @@ class _SettingsPageState extends State<SettingsPage> {
super.dispose();
}
}

0 comments on commit 1d4267a

Please sign in to comment.