Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Offline-first notes where every line is also a live calculator. Write freely;
any line that looks like arithmetic is worked out as you type and its result
appears in a gutter beside it.
appears at the end of the line.

```
Lisbon trip budget
Expand Down Expand Up @@ -388,10 +388,10 @@ mirrored-div stack a browser forces on you.

Results are aligned by laying the note out a second time with the same width,
style and strut the field uses, and reading each line's offset from it. The
gutter and the field share one `ScrollController`. That combination is what
results and the field share one `ScrollController`. That combination is what
keeps a result pinned to its line through wrapping, scrolling and text scaling
— and it is asserted directly in `test/note_editor_test.dart`, which compares
chip positions against the field's own `RenderEditable` geometry.
result positions against the field's own `RenderEditable` geometry.

Clicking a result copies it at full precision.

Expand Down Expand Up @@ -542,8 +542,8 @@ The spec described an Electron build. These changed for Flutter:
most recently edited note when no launch text was entered. A launch draft
becomes a new note, unless the app was opened from a widget, which carries
it into the note being written instead. Search and the full note
list are built only when the hamburger drawer first opens; the gutter is
fixed-width and the divider is hidden.
list are built only when the hamburger drawer first opens; results keep
a fixed-width column beside the text.
- **The widgets (iOS / Android)** — two of them, offering three actions:
**Write**, **Dictate** and **Capture**. The square widget is one cell and one
action, chosen when it is placed and changed afterwards — iOS through *Edit
Expand Down
17 changes: 17 additions & 0 deletions lib/calc/engine.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ class LineResult {

final DigitGrouping grouping;

/// Whether the line reads the note's running total, `total` or `sum`, or
/// converts it, as `total in eur` does: the figure a list of amounts
/// builds up to.
final bool isTotal;

/// Number-system-aware words and scale context, prepared only if a visible
/// chip needs them. Long notes should not spell every off-screen result on
/// each keystroke.
Expand All @@ -34,6 +39,7 @@ class LineResult {
required this.text,
required this.copyText,
this.grouping = DigitGrouping.international,
this.isTotal = false,
});

ResultKind get kind => value.kind;
Expand Down Expand Up @@ -149,6 +155,16 @@ class CalcEngine {
_ => false,
};

/// The readouts of the running total itself, as against `prev` or `avg`.
static bool _isTotalReadout(Node node, Map<String, CalcValue> variables) =>
switch (node) {
IdentifierNode() =>
!variables.containsKey(node.name) &&
const {'sum', 'total'}.contains(node.name.toLowerCase()),
ConvertNode() => _isTotalReadout(node.value, variables),
_ => false,
};

_EvaluatedLine? _evaluateLine(
String rawLine,
int index,
Expand Down Expand Up @@ -365,6 +381,7 @@ class CalcEngine {
text: ResultFormatter.display(value, grouping: grouping),
copyText: ResultFormatter.copy(value),
grouping: grouping,
isTotal: _isTotalReadout(node, scope.variables),
),
isAggregateReadout: _isAggregateReadout(node, scope.variables),
);
Expand Down
17 changes: 6 additions & 11 deletions lib/core/theme.dart
Original file line number Diff line number Diff line change
Expand Up @@ -832,18 +832,13 @@ class AppTypeScale {
? 13.5
: 12.5;

/// The result chip in the gutter.
/// A calculation's result: its pill at the end of the line, or a figure in
/// a phone's results column.
///
/// Its own step rather than [body], because it is monospace figures inside a
/// fixed column: the width the note gives up to it is a layout decision, so
/// the type has to be chosen against that column rather than against the
/// prose elsewhere. Touch gains what the column can actually seat without
/// pushing the writing area below half the screen.
static double get result => _touch
? 14
: _windows
? 13
: 12.5;
/// Its own step rather than [body], because it is monospace figures set
/// against the writing rather than prose. Touch gains what a phone's column
/// can actually seat without pushing the writing area below half the screen.
static double get result => _touch ? 14 : 13;

/// Control labels: buttons, list rows, fields, menu items.
static double get control => _touch
Expand Down
47 changes: 7 additions & 40 deletions lib/data/layout_prefs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,13 @@ class LayoutPrefs extends ChangeNotifier {
/// and wide enough for the page's toolbar beside the rail.
static const Size minimumWindowSize = Size(520, 520);

static const double minGutterWidth = 72;
static const double maxGutterWidth = 480;
static const double defaultGutterWidth = 190;

static const double minSidebarWidth = 150;
static const double maxSidebarWidth = 420;
static const double defaultSidebarWidth = 336;

static const String _gutterKey = 'gutter.v1';
static const String _resultsVisibleKey = 'resultsVisible.v1';
/// v1 recorded the old results column folded away to make room. Results
/// now take no width, so they start out shown again.
static const String _resultsVisibleKey = 'resultsVisible.v2';
static const String _sidebarKey = 'sidebar.v1';
static const String _hiddenFolderVisibleKey = 'hiddenFolderVisible.v1';
static const String _noteCardsKey = 'noteCards.v1';
Expand Down Expand Up @@ -140,7 +137,6 @@ class LayoutPrefs extends ChangeNotifier {
/// locale", which is what this did before it could ask.
String? _region;

double _gutterWidth = defaultGutterWidth;
bool _resultsVisible = true;
double _sidebarWidth = defaultSidebarWidth;
bool _sidebarVisible = false;
Expand Down Expand Up @@ -182,7 +178,6 @@ class LayoutPrefs extends ChangeNotifier {
}) : _locale = locale ?? (() => PlatformDispatcher.instance.locale),
_readRegion = region ?? SystemRegion.read;

double get gutterWidth => _gutterWidth;
bool get resultsVisible => _resultsVisible;
double get sidebarWidth => _sidebarWidth;
bool get sidebarVisible => _sidebarVisible;
Expand Down Expand Up @@ -331,7 +326,6 @@ class LayoutPrefs extends ChangeNotifier {
ResultFormatter.sample(system.resolve(_locale(), region: _region));

void load() {
_gutterWidth = _clampGutter(_readDouble(_gutterKey) ?? defaultGutterWidth);
_resultsVisible = _store.read<bool>(_resultsVisibleKey) ?? true;
_sidebarWidth = _clampSidebar(
_readDouble(_sidebarKey) ?? defaultSidebarWidth,
Expand Down Expand Up @@ -399,14 +393,6 @@ class LayoutPrefs extends ChangeNotifier {
notifyListeners();
}

set gutterWidth(double value) {
final clamped = _clampGutter(value);
if (clamped == _gutterWidth) return;
_gutterWidth = clamped;
_store.putNow(_gutterKey, clamped);
notifyListeners();
}

set resultsVisible(bool value) {
if (value == _resultsVisible) return;
_resultsVisible = value;
Expand Down Expand Up @@ -735,22 +721,9 @@ class LayoutPrefs extends ChangeNotifier {
return null;
}

void resetGutterWidth() => gutterWidth = defaultGutterWidth;

void resetPanelWidths() {
if (_gutterWidth == defaultGutterWidth &&
_sidebarWidth == defaultSidebarWidth &&
_resultsVisible) {
return;
}
_gutterWidth = defaultGutterWidth;
_sidebarWidth = defaultSidebarWidth;
_resultsVisible = true;
_store.putNow(_gutterKey, _gutterWidth);
_store.putNow(_sidebarKey, _sidebarWidth);
_store.putNow(_resultsVisibleKey, _resultsVisible);
notifyListeners();
}
/// The notes list is the one panel left with a width of its own: results
/// sit at the ends of their lines.
void resetPanelWidths() => sidebarWidth = defaultSidebarWidth;

/// Records that the login-item default has had its one chance, whether or
/// not the OS accepted it. A machine whose policy forbids login items would
Expand All @@ -766,9 +739,7 @@ class LayoutPrefs extends ChangeNotifier {

void toggleAlwaysOnTop() => alwaysOnTop = !_alwaysOnTop;

/// The right-hand column's counterpart to [toggleSidebar]. The divider's
/// own handle already collapses and restores it; this is the same door for
/// anyone whose hands are on the keyboard.
/// Hides the results at the ends of calculating lines, or brings them back.
void toggleResults() => resultsVisible = !_resultsVisible;

set hiddenFolderVisible(bool value) {
Expand Down Expand Up @@ -877,10 +848,6 @@ class LayoutPrefs extends ChangeNotifier {
return clean.isEmpty ? null : clean;
}

static double _clampGutter(double value) => value.isFinite
? value.clamp(minGutterWidth, maxGutterWidth)
: defaultGutterWidth;

static double _clampSidebar(double value) => value.isFinite
? value.clamp(minSidebarWidth, maxSidebarWidth)
: defaultSidebarWidth;
Expand Down
4 changes: 2 additions & 2 deletions lib/data/shortcut_prefs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ extension ShortcutActionCopy on ShortcutAction {
ShortcutAction.focusThirdPane => 'Focus pane 3',
ShortcutAction.toggleSidebar => 'Toggle notes list',
ShortcutAction.toggleHiddenFolder => 'Toggle Hidden Notes folder',
ShortcutAction.toggleResults => 'Toggle results column',
ShortcutAction.toggleResults => 'Toggle results',
ShortcutAction.toggleAlwaysOnTop => 'Keep window on top',
ShortcutAction.deleteNote => 'Archive current note',
ShortcutAction.openSettings => 'Open settings',
Expand Down Expand Up @@ -85,7 +85,7 @@ extension ShortcutActionCopy on ShortcutAction {
ShortcutAction.toggleHiddenFolder =>
'Reveal or hide Hidden Notes in the notes list',
ShortcutAction.toggleResults =>
'Show or hide the results column on the right',
'Show or hide the results at the ends of calculating lines',
ShortcutAction.toggleAlwaysOnTop =>
'Float the window over other apps, or let it fall behind again',
ShortcutAction.deleteNote =>
Expand Down
171 changes: 171 additions & 0 deletions lib/ui/editor/inline_results.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
import 'dart:math' as math;

import 'package:material_ui/material_ui.dart';

import '../../calc/engine.dart';
import '../../core/theme.dart';
import 'line_metrics.dart';
import 'results_gutter.dart';
import 'scroll_passthrough.dart';

/// Each result at the right-hand end of its own line: a pill on the line's
/// last visual row, with a faint leader running to it from where the writing
/// stops.
///
/// Laid over the text field rather than inside it, and placed from the same
/// measurement of the note the field's own layout produces, so nothing here
/// is ever part of the note's text: the caret, the selection, copy and paste
/// and undo never meet it. Only the pills take the pointer. Everything around
/// them, the leaders included, reaches the field underneath.
class InlineResults extends StatelessWidget {
const InlineResults({
super.key,
required this.results,
required this.offsets,
required this.scroll,
required this.padding,
required this.columnWidth,
});

final Map<int, LineResult> results;

/// Must have been measured with every line in [results] in its
/// `lastRowsOf`.
final LineOffsets offsets;

/// The field's own, so the pills move with the text.
final ScrollController scroll;

/// Where the field's text starts inside this layer, and the bands above and
/// below it that the text scrolls under.
final EdgeInsets padding;

/// How wide the text wraps. A pill's right edge sits on it.
final double columnWidth;

/// Between the end of the writing and the leader, and between the leader
/// and the pill.
static const double gap = 12;

@override
Widget build(BuildContext context) {
final leader = context.palette.textTertiary.withValues(alpha: 0.3);
return Padding(
padding: EdgeInsets.only(top: padding.top, bottom: padding.bottom),
// Top and bottom only: a pill scrolls under the padding as the text
// does, but may run on into the margin beside the column.
child: ClipRect(
clipper: const VerticalClipper(),
child: LayoutBuilder(
builder: (context, constraints) => ListenableBuilder(
listenable: scroll,
builder: (context, _) {
final scrolled = scroll.hasClients ? scroll.offset : 0.0;
return Stack(
clipBehavior: Clip.none,
children: [
for (final MapEntry(key: line, value: result)
in results.entries)
if (offsets.lastRows[line] case final row?)
// Only what can be seen; a long note stays cheap.
if (row.bottom - scrolled >= 0 &&
row.top - scrolled <= constraints.maxHeight)
Positioned(
key: ValueKey(line),
left: 0,
right: 0,
top: row.top - scrolled,
height: row.height,
child: CustomMultiChildLayout(
delegate: _ResultLineLayout(
textEnd: padding.left + row.right,
columnRight: padding.left + columnWidth,
),
children: [
LayoutId(
id: _Part.leader,
child: IgnorePointer(
child: CustomPaint(
key: ValueKey('result-leader-$line'),
painter: _LeaderPainter(leader),
),
),
),
LayoutId(
id: _Part.pill,
child: ScrollPassthrough(
controller: scroll,
child: ResultChip(
result: result,
collapsedWidth: columnWidth / 2,
look: ResultChipLook.pill,
),
),
),
],
),
),
],
);
},
),
),
),
);
}
}

enum _Part { leader, pill }

/// Puts a line's pill at the right-hand end of the column and runs the
/// leader up to it.
///
/// The writing always wins the row. When it already reaches past where the
/// pill would go, the pill follows the writing instead, out into the margin
/// as far as the page allows, and only once there is no margin left does it
/// cover the end of the line.
class _ResultLineLayout extends MultiChildLayoutDelegate {
_ResultLineLayout({required this.textEnd, required this.columnRight});

final double textEnd;
final double columnRight;

@override
void performLayout(Size size) {
final pill = layoutChild(_Part.pill, BoxConstraints(maxWidth: size.width));
final start = textEnd + InlineResults.gap;
var left = columnRight - pill.width;
if (left < start) left = math.min(start, size.width - pill.width);
left = math.max(0.0, left);
positionChild(_Part.pill, Offset(left, (size.height - pill.height) / 2));

final length = math.max(0.0, left - InlineResults.gap - start);
layoutChild(_Part.leader, BoxConstraints.tight(Size(length, size.height)));
positionChild(_Part.leader, Offset(start, 0));
}

@override
bool shouldRelayout(_ResultLineLayout oldDelegate) =>
oldDelegate.textEnd != textEnd || oldDelegate.columnRight != columnRight;
}

/// A dotted rule across the middle of the row: square dots a dot apart.
class _LeaderPainter extends CustomPainter {
const _LeaderPainter(this.color);

final Color color;

static const double _dot = 1;

@override
void paint(Canvas canvas, Size size) {
final paint = Paint()..color = color;
final top = (size.height - _dot) / 2;
for (var x = 0.0; x + _dot <= size.width; x += _dot * 2) {
canvas.drawRect(Rect.fromLTWH(x, top, _dot, _dot), paint);
}
}

@override
bool shouldRepaint(_LeaderPainter oldDelegate) => oldDelegate.color != color;
}
Loading
Loading