Skip to content

Commit

Permalink
editor: remember last-used tool
Browse files Browse the repository at this point in the history
Resolves #370
  • Loading branch information
adil192 committed Feb 22, 2023
1 parent 735f43b commit 11a1304
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 7 deletions.
29 changes: 26 additions & 3 deletions lib/components/canvas/tools/_tool.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,33 @@

import 'package:flutter/material.dart';
import 'package:saber/data/prefs.dart';

class Tool {
abstract class Tool {
@protected
@visibleForTesting
const Tool();

static const Tool textEditing = Tool();
/// An identifier for the tool,
/// used to save the last-used tool in [Prefs.lastTool].
ToolId get toolId;

static const Tool textEditing = _TextEditingTool();
}

class _TextEditingTool extends Tool {
const _TextEditingTool();

@override
ToolId get toolId => ToolId.textEditing;
}

enum ToolId {
fountainPen('fountainPen'),
ballpointPen('ballpointPen'),
highlighter('Highlighter'),
eraser('Eraser'),
select('Select'),
textEditing('TextEditingTool');

final String id;
const ToolId(this.id);
}
3 changes: 3 additions & 0 deletions lib/components/canvas/tools/eraser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ class Eraser extends Tool {
this.size = 10
});

@override
ToolId get toolId => ToolId.eraser;

/// Returns the indices of any [strokes] that are close to the given [eraserPos].
List<int> checkForOverlappingStrokes(Offset eraserPos, List<Stroke> strokes) {
final List<int> indices = [];
Expand Down
3 changes: 2 additions & 1 deletion lib/components/canvas/tools/highlighter.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:saber/components/canvas/tools/_tool.dart';
import 'package:saber/components/canvas/tools/pen.dart';
import 'package:saber/data/prefs.dart';
import 'package:saber/i18n/strings.g.dart';
Expand All @@ -12,6 +12,7 @@ class Highlighter extends Pen {
sizeMax: 100,
sizeStep: 10,
icon: highlighterIcon,
toolId: ToolId.highlighter,
) {
strokeProperties = Prefs.lastHighlighterProperties.value;
}
Expand Down
11 changes: 9 additions & 2 deletions lib/components/canvas/tools/pen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class Pen extends Tool {
required this.sizeMax,
required this.sizeStep,
required this.icon,
required this.toolId,
});

Pen.fountainPen() :
Expand All @@ -26,19 +27,25 @@ class Pen extends Tool {
sizeMax = 25,
sizeStep = 1,
icon = fountainPenIcon,
strokeProperties = Prefs.lastFountainPenProperties.value;
strokeProperties = Prefs.lastFountainPenProperties.value,
toolId = ToolId.fountainPen;

Pen.ballpointPen() :
name = t.editor.pens.ballpointPen,
sizeMin = 1,
sizeMax = 25,
sizeStep = 1,
icon = ballpointPenIcon,
strokeProperties = Prefs.lastBallpointPenProperties.value;
strokeProperties = Prefs.lastBallpointPenProperties.value,
toolId = ToolId.ballpointPen;

final String name;
final double sizeMin, sizeMax, sizeStep;
final IconData icon;

@override
final ToolId toolId;

static const IconData fountainPenIcon = FontAwesomeIcons.penFancy;
static const IconData ballpointPenIcon = FontAwesomeIcons.pen;

Expand Down
3 changes: 3 additions & 0 deletions lib/components/canvas/tools/select.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ class Select extends Tool {
SelectResult selectResult = SelectResult(-1, const [], Path());
bool doneSelecting = false;

@override
ToolId get toolId => ToolId.select;

void unselect() {
doneSelecting = false;
selectResult.pageIndex = -1;
Expand Down
12 changes: 12 additions & 0 deletions lib/data/prefs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:flutter/material.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:nextcloud/nextcloud.dart' show NextcloudProvisioningApiUserDetails_Quota;
import 'package:saber/components/canvas/_canvas_background_painter.dart';
import 'package:saber/components/canvas/tools/_tool.dart';
import 'package:saber/components/canvas/tools/stroke_properties.dart';
import 'package:saber/data/flavor_config.dart';
import 'package:saber/data/nextcloud/nextcloud_client_extension.dart';
Expand Down Expand Up @@ -67,6 +68,7 @@ abstract class Prefs {
static late final PlainPref<List<String>> recentColorsChronological;
static late final PlainPref<List<String>> recentColorsPositioned;

static late final PlainPref<ToolId> lastTool;
static late final PlainPref<StrokeProperties>
lastFountainPenProperties,
lastBallpointPenProperties,
Expand Down Expand Up @@ -127,6 +129,7 @@ abstract class Prefs {
recentColorsChronological = PlainPref('recentColorsChronological', []);
recentColorsPositioned = PlainPref('recentColorsPositioned', [], historicalKeys: ['recentColors']);

lastTool = PlainPref('lastTool', ToolId.fountainPen);
lastFountainPenProperties = PlainPref('lastFountainPenProperties', StrokeProperties.fountainPen, deprecatedKeys: ['lastPenColor']);
lastBallpointPenProperties = PlainPref('lastBallpointPenProperties', StrokeProperties.ballpointPen);
lastHighlighterProperties = PlainPref('lastHighlighterProperties', StrokeProperties.highlighter, deprecatedKeys: ['lastHighlighterColor']);
Expand Down Expand Up @@ -257,6 +260,7 @@ class PlainPref<T> extends IPref<T> {
|| T == typeOf<Queue<String>>()
|| T == StrokeProperties || T == typeOf<Quota?>()
|| T == AxisDirection || T == ThemeMode || T == TargetPlatform
|| T == ToolId
);
}

Expand Down Expand Up @@ -322,6 +326,8 @@ class PlainPref<T> extends IPref<T> {
return await _prefs!.setInt(key, (value as ThemeMode).index);
} else if (T == TargetPlatform) {
return await _prefs!.setInt(key, (value as TargetPlatform).index);
} else if (T == ToolId) {
return await _prefs!.setString(key, (value as ToolId).id);
} else {
return await _prefs!.setString(key, value as String);
}
Expand Down Expand Up @@ -367,6 +373,12 @@ class PlainPref<T> extends IPref<T> {
if (index == null) return null;
if (index == -1) return defaultTargetPlatform as T?;
return TargetPlatform.values[index] as T?;
} else if (T == ToolId) {
String id = _prefs!.getString(key)!;
return ToolId.values
.cast<ToolId?>()
.firstWhere((toolId) => toolId?.id == id, orElse: () => null)
as T?;
} else {
return _prefs!.get(key) as T?;
}
Expand Down
28 changes: 27 additions & 1 deletion lib/pages/editor/editor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,33 @@ class EditorState extends State<Editor> {

late bool needsNaming = widget.needsNaming && Prefs.editorPromptRename.value;

Tool currentTool = Pen.currentPen;
late Tool _currentTool = () {
switch (Prefs.lastTool.value) {
case ToolId.fountainPen:
if (Pen.currentPen.toolId != Prefs.lastTool.value) {
Pen.currentPen = Pen.fountainPen();
}
return Pen.currentPen;
case ToolId.ballpointPen:
if (Pen.currentPen.toolId != Prefs.lastTool.value) {
Pen.currentPen = Pen.ballpointPen();
}
return Pen.currentPen;
case ToolId.highlighter:
return Highlighter.currentHighlighter;
case ToolId.eraser:
return Eraser();
case ToolId.select:
return Select.currentSelect;
case ToolId.textEditing:
return Tool.textEditing;
}
}();
Tool get currentTool => _currentTool;
set currentTool(Tool tool) {
_currentTool = tool;
Prefs.lastTool.value = tool.toolId;
}

/// Whether the note has changed since it was last saved
bool _hasEdited = false;
Expand Down

0 comments on commit 11a1304

Please sign in to comment.