Compare commits

...
33 Commits
Author SHA1 Message Date
haraldandClaude Fable 5.2 544e004106 chore(release): bump version to 0.45.2
Build & Publish Release / Android APK (push) Successful in 7m14s
Build & Publish Release / Build Packages & Publish to Gitea (push) Successful in 13m13s
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-14 16:51:10 +01:00
haraldandClaude Fable 5.2 36bb77c299 fix(storage): keep a page changed by another device when saving over it
A change from another device could reach the page file while a stroke was
being drawn; the check that takes it over waits until everything is saved,
so the next autosave overwrote it. save_page now notices that the file is
not what this process last read or wrote: their version is kept as a
conflict copy (which becomes a page of its own), or simply stays if there
is nothing new to save here.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-14 16:46:06 +01:00
haraldandClaude Fable 5.2 41323950dc chore(release): bump version to 0.45.1
Build & Publish Release / Android APK (push) Successful in 7m25s
Build & Publish Release / Build Packages & Publish to Gitea (push) Successful in 14m9s
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-14 16:22:47 +01:00
haraldandClaude Fable 5.2 e95b03e316 fix: smaller fixes from the project review
- settings: one invalid value in ui.json no longer resets all settings
  (including the storage location), only that value
- sync: a worker whose pass outlives a stop/start ends afterwards
- updater: only install packages whose signed file name carries a newer
  version than the running one
- canvas: grow the stroke vertex buffer at most up to the device limit

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-14 15:49:11 +01:00
haraldandClaude Fable 5.2 a22f081115 fix: robustness fixes from the project review
- autosave: a manifest that failed to save is retried with backoff instead
  of waiting for the next edit
- import: NaN and infinite numbers from broken files no longer reach the
  page file (they were stored as null and made the page unreadable)
- import: SVG images cannot load local files, Rnote page count is capped
- pdf: a panic in the PDF library fails one page or import instead of
  ending the render thread or the app
- storage: remove the temporary file when writing it fails

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-14 15:44:51 +01:00
haraldandClaude Fable 5.2 c2652ea05b fix: data-safety fixes from the project review
- autosave: pages a failed flush could not write no longer end up in the
  notebook that is opened next; they go to the recovery folder
- app: finish an open text edit and save before switching notebooks, and
  stay in the notebook when saving fails
- trash: purge by the deletion time in the entry name, not by the folder's
  modification time, which survives the move into .trash
- notebooks are identified by their folder name, so a copied folder is a
  notebook of its own instead of an alias of the original
- manifest: fall back to manifest.json.bak only for a damaged file, not for
  a newer format version or an I/O error
- strokes keep their width after scaling and a restart
- PDF export draws shapes with their stroke width instead of a fixed 1.5 pt
- a first page that fails to load shows the warning; a first page that was
  never saved is an empty page; make-space refuses read-only pages

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-14 15:39:54 +01:00
haraldandClaude Opus 5.5 7c69edab12 chore(release): bump version to 0.45.0
Build & Publish Release / Android APK (push) Successful in 9m7s
Build & Publish Release / Build Packages & Publish to Gitea (push) Successful in 23m24s
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 11:02:47 +01:00
haraldandClaude Opus 5.5 6f52d4801f docs: describe saved colors (Issue #56)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 10:52:22 +01:00
haraldandClaude Opus 5.5 bb9bb58790 feat(colors): save own colors under the presets of the color popovers (Issue #56)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 10:52:16 +01:00
haraldandClaude Opus 5.5 9dbe625eef fix(text): copy and paste in text fields through the system clipboard (Issue #57)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 10:52:16 +01:00
haraldandClaude Opus 5.5 e7e161e70c style: cargo fmt
Build & Publish Release / Android APK (push) Successful in 6m8s
Build & Publish Release / Build Packages & Publish to Gitea (push) Successful in 11m37s
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-09 22:49:30 +01:00
haraldandClaude Opus 5.5 043f487e9c chore(release): bump version to 0.44.0
Build & Publish Release / Android APK (push) Successful in 5m55s
Build & Publish Release / Build Packages & Publish to Gitea (push) Failing after 1m10s
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-09 22:37:36 +01:00
haraldandClaude Opus 5.5 3b0b4f51fb feat(pens): seven pens in the toolbar (Issue #56)
Pens 5-7 are stored in a new field, so versions before 0.44 still read
the first four instead of resetting all settings.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-09 22:34:35 +01:00
haraldandClaude Opus 5.5 14abadbd03 fix(ui): fit dialogs beside the toolbar on narrow screens (Issue #50)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-09 22:34:35 +01:00
haraldandClaude Opus 5.5 f279790e00 fix(ruler): stop a finger drag of the ruler when the pen comes near (Issue #55)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-09 22:34:35 +01:00
haraldandClaude Opus 5.5 7bd3d9e82f fix(touch): hand on a touch lift without waiting for the next frame (Issues #28, #55)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-09 22:34:27 +01:00
haraldandClaude Opus 5.5 ab0f49199b chore(release): bump version to 0.43.0
Build & Publish Release / Android APK (push) Successful in 7m2s
Build & Publish Release / Build Packages & Publish to Gitea (push) Successful in 12m9s
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-09 09:14:25 +01:00
haraldandClaude Opus 5.5 38b860bc32 fix(pen): wake the app for pen events on Wayland (Issue #53)
octotablet reads pen events from its own queue on winit's Wayland
connection and only dispatches what is already there. winit read them
from the socket but, finding none of its own events, went back to sleep
without AboutToWait, so pen strokes waited until something else woke the
loop: they showed up late or only after touching the screen. Until
0.39.3 the app redrew continuously, which hid this; the battery fix
there exposed it. Every read from the socket now wakes the app. Idle CPU
under a headless Wayland compositor stays at 0.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-09 09:10:42 +01:00
haraldandClaude Opus 5.5 055e39c24e feat(eraser): erase text boxes as a whole (Issue #54)
The eraser skipped text boxes along with images, so a typed text or an
inserted math result could only be removed with the selection tool. The
whole-stroke and partial erasers now remove a text box once they touch
it; the highlighter eraser leaves it alone and images are still never
erased.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-09 09:10:37 +01:00
haraldandClaude Opus 5.5 1b22ffa526 chore(release): bump version to 0.42.2
Build & Publish Release / Android APK (push) Successful in 6m1s
Build & Publish Release / Build Packages & Publish to Gitea (push) Successful in 12m7s
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-05 13:00:36 +01:00
haraldandClaude Opus 5.5 0d9e9aa2ea fix(input): remember "draw with the finger", on by default on Android (Issue #50)
The setting was never saved and always started off, so one finger only
moved the page after every start. On a phone without a pen nothing could
be written until it was switched on again, and an update restarts the
app. It is now saved with the other UI settings and starts on on
Android, where phones usually have no pen.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-05 12:20:52 +01:00
haraldandClaude Opus 5.5 298932cb32 chore(release): bump version to 0.42.1
Build & Publish Release / Android APK (push) Successful in 10m47s
Build & Publish Release / Build Packages & Publish to Gitea (push) Successful in 31m46s
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-02 09:09:05 +01:00
haraldandClaude Opus 5.5 127bc8d730 fix(input): keep waking while a finger is down so the lift arrives on time (Issue #28)
On Wayland the touch-up event could already sit in winit's queue, read
from the socket by the graphics driver, without waking the event loop.
It then only arrived together with the next touch, which stopped the
glide right away: Pia's log showed every lift handled 200-1000 ms late
while the compositor timestamps were 10 ms apart, and her screencast
shows the page stop dead after each swipe. The loop now wakes every
8 ms while a finger is on the screen.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-30 09:42:34 +01:00
haraldandClaude Opus 5.5 19fc8dd3ca ci: fetch the APK after restoring the Rust cache
Build & Publish Release / Android APK (push) Successful in 7m18s
Build & Publish Release / Build Packages & Publish to Gitea (push) Successful in 13m41s
A cache restored without an exact match (Cargo.lock changed with the
rustls update) cleans target/, which removed the downloaded APK, so the
v0.42.0 release job stopped at INK_NOTES_REQUIRE_APK.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-28 20:59:44 +01:00
haraldandClaude Opus 5.5 17d0f07b9a build(deps): bump rustls from 0.23.45 to 0.23.46 (Issue #51)
Build & Publish Release / Android APK (push) Successful in 8m14s
Build & Publish Release / Build Packages & Publish to Gitea (push) Failing after 25m48s
Fixes RUSTSEC key material in the Debug output of client session values;
ink-notes doesn't use a custom session store, so it wasn't affected.
Flatpak cargo sources updated alongside.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-28 20:23:33 +01:00
haraldandClaude Opus 5.5 200f76e32f chore(release): bump version to 0.42.0
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-28 20:12:58 +01:00
haraldandClaude Opus 5.5 b8748c56d4 feat(sync): built-in Nextcloud sync (Issue #49, stage 2)
Sign in to any Nextcloud server (also private ones in a subfolder, with
their own port or a self-signed certificate) through the browser with
Login Flow v2; the app gets an app password of its own, which
"Disconnect" revokes again.

A background thread syncs the storage folder with the folder ink-notes
on the server every 30 seconds and shortly after saving, three-way
against the state after the last pass: one-sided changes are copied,
manifests and library.json are merged, a page changed on both sides is
kept twice (the server's version as conflict copy, which disk_watch
turns into a page), empty pages give way, deletions go into the trash
on both sides and lose against changes. Changes to the open notebook
wait while it has unsaved changes. Folder ETags spare listing unchanged
notebooks; the pass stops instead of deleting when the storage folder
or the server folder is missing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-28 19:24:49 +01:00
haraldandClaude Opus 5.5 8c0ccdd394 chore(release): bump version to 0.41.1
Build & Publish Release / Android APK (push) Successful in 6m33s
Build & Publish Release / Build Packages & Publish to Gitea (push) Successful in 12m52s
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-28 16:48:55 +01:00
haraldandClaude Opus 5.5 3d6dbf09da fix(android): keep the toolbar out of the status and navigation bars (Issue #50)
NativeActivity draws the whole window, also below the system bars, so the
top toolbar button lay under the status bar and taps there went to
Android (no other notebook could be opened). InkActivity now reports the
system bar and cutout insets; the app shrinks egui's screen rect to the
free area and adds the side insets to the page centering. The page itself
still scrolls below the bars.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-28 15:32:43 +01:00
haraldandClaude Opus 5.5 2a9a67d399 chore(release): bump version to 0.41.0
Build & Publish Release / Android APK (push) Successful in 6m23s
Build & Publish Release / Build Packages & Publish to Gitea (push) Successful in 13m57s
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-28 14:48:54 +01:00
haraldandClaude Opus 5.5 307c803246 feat(math): offer the result next to a handwritten = (Issue #46, stage 3)
New module math_assist.rs: when the last two strokes form an = and the pen
rests for 500 ms, the strokes left of it (in a band around the =, up to a
wide gap) are recognized and computed. The suggestion appears next to
the = and shows what was read, e.g. "2⁴ = 16"; tapping it with the pen
or clicking inserts the result as a text box in the ink color and as high
as the handwriting, as one undo step. The chip is not interactive for
egui, so fingers and palms pass through. Any new stroke drops it. In a
chain like x = 2 · 3 = the part after the previous = is computed.

Off by default: ☰ → Input → "Compute formulas after =", saved in
ui.json. math::display writes the recognized expression neatly.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-28 14:45:00 +01:00
haraldandClaude Opus 5.5 d16cd2db72 feat(math): recognize handwritten expressions (Issue #46, stage 2)
New module math_ink.rs turns the strokes of an expression into text for
math::solve. Consecutive overlapping strokes are grouped into symbols,
each symbol is classified with the $P point-cloud recognizer (plus the
turning angle and a small stroke-count penalty) against built-in
templates for digits, + ( ) / x y a b, and the structure comes from the
positions: stacked lines are =, stacked dots :, lines with symbols above
and below are fraction bars, low dots between digits decimal commas and
raised symbols exponents. Own templates can be added for stage 4.

Tested with synthetic handwriting (slanted, rotated, warped); not used by
the UI yet.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-28 14:27:25 +01:00
haraldandClaude Opus 5.5 cd490dd9a7 feat(math): exact evaluation of handwritten expressions (Issue #46, stage 1)
New module math.rs: solve() reads a recognised expression such as 2^4,
2,5 · 3 or (x + 3)² and returns its result as text (16, 7,5,
x² + 6x + 9). Own parser with exact fractions and polynomial expansion
instead of evalexpr, as implicit multiplication and expanding need it
anyway. Not used by the UI yet.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-28 12:34:32 +01:00
38 changed files with 7510 additions and 265 deletions

No files matched your search

+8 -6
View File
@@ -19,12 +19,6 @@ jobs:
- name: Checkout Repository
uses: actions/checkout@v4.2.2
- name: Fetch Android APK
uses: actions/download-artifact@v4.3.0
with:
name: ink-notes-android-apk
path: target
- name: Install System Packaging Tools
run: |
export DEBIAN_FRONTEND=noninteractive
@@ -71,6 +65,14 @@ jobs:
cargo clippy --all-targets -- -D warnings
cargo test
# Only after the Rust cache step: restoring a cache that doesn't match exactly (e.g.
# after Cargo.lock changed) cleans target/ and would remove the APK again
- name: Fetch Android APK
uses: actions/download-artifact@v4.3.0
with:
name: ink-notes-android-apk
path: target
- name: Build Release Artifacts (.deb, .AppImage, .exe, .apk, SHA256SUMS.txt + .minisig)
run: |
chmod +x scripts/*.sh
Generated
+5 -3
View File
@@ -1540,7 +1540,7 @@ dependencies = [
[[package]]
name = "ink-notes"
version = "0.40.0"
version = "0.45.2"
dependencies = [
"ab_glyph",
"android_logger",
@@ -1565,6 +1565,7 @@ dependencies = [
"pollster",
"quick-xml",
"resvg",
"rustls",
"semver",
"serde",
"serde_json",
@@ -1576,6 +1577,7 @@ dependencies = [
"wayland-client",
"wayland-protocols",
"webbrowser",
"webpki-roots 0.26.11",
"wgpu",
"winit",
]
@@ -2578,9 +2580,9 @@ dependencies = [
[[package]]
name = "rustls"
version = "0.23.45"
version = "0.23.46"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0d41d731c7d2f962d1ccc364cec258de3c0e93b38c2fb3ba97ac74513048d634"
checksum = "48e13bd8c0e9365c43cfa5c9e8f9ad49d3c8444926c9aac819e0e4dc503c8fdf"
dependencies = [
"log",
"once_cell",
+5 -1
View File
@@ -1,6 +1,6 @@
[package]
name = "ink-notes"
version = "0.40.0"
version = "0.45.2"
edition = "2021"
description = "Notizbuch-App für Stift, Touch und Maus unter Linux"
repository = "https://gitea.pansi.eu/harald/ink-notes"
@@ -38,6 +38,10 @@ semver = "1.0"
sha2 = "0.10"
ureq = { version = "2.10", default-features = false, features = ["tls", "gzip", "json"] }
url = "2.5"
# Nextcloud sync (Issue #49): pinning a self-signed certificate needs our own verifier;
# same versions and features as ureq uses
rustls = { version = "0.23", default-features = false, features = ["logging", "ring", "std", "tls12"] }
webpki-roots = "0.26"
# Bilder einfügen: Dekodieren (PNG/JPEG/GIF/WebP/BMP) und Systemzwischenablage
image = { version = "0.25", default-features = false, features = ["png", "jpeg", "gif", "webp", "bmp"] }
+15 -4
View File
@@ -22,12 +22,12 @@ Die Oberfläche gibt es auf Deutsch und Englisch; ink-notes folgt der Systemspra
## Funktionen
### Schreiben und Zeichnen
- **Vier Stifte** in der Werkzeugleiste, jeder mit eigener Farbe und Stärke (0,15–5 mm, mit Druckstärke). Ändert man Farbe oder Stärke, merkt sich der gewählte Stift das – auch über einen Neustart hinweg. **Textmarker** (halbtransparent, 4–8 mm) in vier Farben; innerhalb eines Strichs wird er nicht dunkler, erst ein neuer Strich deckt stärker.
- **Eigene Farben:** Jede Farbe in der Werkzeugleiste (Stifte, Textmarker, Text) lässt sich anpassen – gewählte Farbe erneut antippen, eine Vorgabe oder mit 🎨 einen freien Farbton wählen. Die Leiste merkt sich die Farben. Am Bildschirm erscheinen sie genauso wie im PDF- und Bildexport.
- **Sieben Stifte** in der Werkzeugleiste, jeder mit eigener Farbe und Stärke (0,15–5 mm, mit Druckstärke). Ändert man Farbe oder Stärke, merkt sich der gewählte Stift das – auch über einen Neustart hinweg. **Textmarker** (halbtransparent, 4–8 mm) in vier Farben; innerhalb eines Strichs wird er nicht dunkler, erst ein neuer Strich deckt stärker.
- **Eigene Farben:** Jede Farbe in der Werkzeugleiste (Stifte, Textmarker, Text) lässt sich anpassen – gewählte Farbe erneut antippen, eine Vorgabe oder mit 🎨 einen freien Farbton wählen. Die Leiste merkt sich die Farben. Unter den Vorgaben legt „➕ Farbe merken“ den aktuellen Farbton in „Meine Farben“ ab (bis zu 12, gemeinsam für Stifte, Textmarker und Text; 🗑 entfernt die gewählte). Am Bildschirm erscheinen sie genauso wie im PDF- und Bildexport.
- **Glättung in zwei Stufen:** Während des Schreibens glättet ein 1€-Filter das Zittern, die Linie erscheint sofort. Beim Absetzen wird der Strich nachbearbeitet: Ecken (Knicke über 65°) bleiben erhalten und werden rund verbunden, der Druckverlauf wird geglättet, überflüssige Punkte entfallen, die Enden werden rund.
- **Druckkurve** im ☰-Menü an den eigenen Stift anpassen (weicher/härter, Schwelle gegen Grundrauschen).
- **Tasten am Stift** im ☰-Menü belegen: Radieren (Standard), Auswählen, Textmarker, Stift oder nichts – gilt, solange die Taste gedrückt ist. Jede Taste erscheint dort nach dem ersten Drücken.
- **Radierer** in drei Modi: ganze Striche, nur den überstrichenen Teil (Teilstrich) oder nur Textmarker; die Größe stellt man in einem Menü wie bei den Stiften ein (wird gemerkt). Die Stiftrückseite radiert ebenfalls.
- **Radierer** in drei Modi: ganze Striche, nur den überstrichenen Teil (Teilstrich) oder nur Textmarker; berührte Textfelder verschwinden ganz, Bilder bleiben; die Größe stellt man in einem Menü wie bei den Stiften ein (wird gemerkt). Die Stiftrückseite radiert ebenfalls.
- **Virtuelles Lineal** (`L`): verschieben, am Griff drehen (rastet bei 15° ein); Striche entlang einer Kante werden gerade.
- **Nur ansehen** (👁 in der Werkzeugleiste): Stift, Finger und Maus scrollen und zoomen nur; die Werkzeuge verschwinden, nichts lässt sich versehentlich ändern. Erneut 👁 tippen, um zu schreiben.
- **Platz schaffen** (↕ in der Werkzeugleiste): aufs Blatt tippen und nach unten ziehen – alles darunter rutscht mit, auch auf den folgenden Seiten. Was über ein Seitenende rutschen würde, beginnt auf der nächsten Seite; fehlende Seiten werden angehängt. Nach oben ziehen schließt leeren Platz. Ein Schritt für Rückgängig.
@@ -37,6 +37,11 @@ Die Oberfläche gibt es auf Deutsch und Englisch; ink-notes folgt der Systemspra
- Erkannt werden Linien, Pfeile, Kreise, Ellipsen, Rechtecke, Dreiecke, Rauten, Vielecke, Koordinatensysteme (L-Form), Achsenkreuze (ein „+“ in einem Zug oder zwei gekreuzte Geraden, Pfeilspitzen an allen vier Enden) und Oktaeder (Skizze als 3D-Körper).
- Die Erkennung arbeitet rein geometrisch (Bogenlänge, Douglas-Peucker, Kreis-Fit), ohne Machine Learning. Nicht alles wird erkannt – was nicht passt, bleibt Handschrift.
### Formeln ausrechnen (experimentell)
- Einschalten im ☰-Menü unter Eingabe („Formeln nach „=“ ausrechnen“). Nach einem handgeschriebenen `=` und einer kurzen Pause erscheint daneben das Ergebnis samt dem Gelesenen, z. B. `2⁴ = 16` oder `(x + 3)² = x² + 6x + 9`; Antippen mit dem Stift oder Klicken fügt es als Textfeld in der Stiftfarbe und in der Höhe der Handschrift ein (Rückgängig entfernt es). Finger und Handballen lösen nichts aus.
- Gelesen werden Ziffern, `+ − · : /`, Klammern, Hochzahlen (hochgestellt geschrieben), Bruchstriche, Dezimalkommas und die Variablen `x`, `y`, `a`, `b`. Zahlen werden exakt als Brüche gerechnet, Ausdrücke mit Variablen ausmultipliziert. Vor der Formel etwas Abstand lassen, damit weiter links Geschriebenes nicht mitgelesen wird; in einer Kette wie `x = 2 · 3 =` wird der Teil nach dem vorigen `=` gerechnet.
- Die Zeichen werden mit Punktwolken-Vorlagen ($P) erkannt, ohne Machine Learning und offline. Zeigt der Vorschlag ein falsches Zeichen, ihn einfach ignorieren.
### Auswählen und Bearbeiten
- **Auswahlwerkzeug** (`V`): antippen oder Rahmen aufziehen, dann verschieben, an den Ecken skalieren, am Griff drehen oder mit `R` um 90° drehen. Funktioniert mit Maus, Finger und Stift.
- Auswahl über die Palette **umfärben**, **kopieren/einfügen** (`Strg + C/V`), **löschen** (`Entf`).
@@ -68,7 +73,8 @@ Die Oberfläche gibt es auf Deutsch und Englisch; ink-notes folgt der Systemspra
- Jede Datei wird erst vollständig in eine temporäre Datei geschrieben und dann ersetzt – ein Absturz beim Speichern hinterlässt keine halbe Datei.
- **Beim Schließen** (Fenster oder `Strg + Q`) und beim Wechsel des Notizbuchs wird alles Ausstehende geschrieben, bevor es weitergeht. Schlägt das fehl, bleibt die App offen und bietet Wiederholen oder eine Notfall-Sicherung an.
- `Strg + S` speichert sofort. Der Punkt am ☰-Knopf zeigt den Stand: grün = gespeichert, orange = Änderungen warten, gelb = speichert gerade, rot = Fehler.
- **Speicherort** (☰-Menü, „Speicherort“): Liegen die Notizbücher in einem Ordner, den **Nextcloud, Google Drive, Syncthing oder Dropbox** abgleichen, sind sie auf allen Geräten verfügbar. Die Notizbücher liegen im Unterordner `ink-notes`; ist er noch leer, werden die vorhandenen Notizbücher dorthin kopiert (die alten bleiben erhalten). Änderungen von einem anderen Gerät erscheinen nach wenigen Sekunden ohne neu zu öffnen – aber nur, solange lokal alles gespeichert ist, damit nichts überschrieben wird. Legt der Sync-Client eine Konfliktkopie einer Seite an, wird sie als eigene Seite („Konfliktkopie: …“) übernommen statt verloren zu gehen. Ist ein Notizbuch gerade auf einem anderen Gerät geöffnet, warnt die App beim Öffnen.
- **Speicherort** (☰-Menü, „Speicherort“): Liegen die Notizbücher in einem Ordner, den **Nextcloud, Google Drive, Syncthing oder Dropbox** abgleichen, sind sie auf allen Geräten verfügbar. Die Notizbücher liegen im Unterordner `ink-notes`; ist er noch leer, werden die vorhandenen Notizbücher dorthin kopiert (die alten bleiben erhalten). Änderungen von einem anderen Gerät erscheinen nach wenigen Sekunden ohne neu zu öffnen – aber nur, solange lokal alles gespeichert ist, damit nichts überschrieben wird. Legt der Sync-Client eine Konfliktkopie einer Seite an, wird sie als eigene Seite („Konfliktkopie: …“) übernommen statt verloren zu gehen. Dasselbe passiert, wenn eine Änderung genau dann eintrifft, während du auf dieser Seite schreibst: Deine Fassung wird gespeichert, die andere bleibt als Konfliktkopie erhalten. Ist ein Notizbuch gerade auf einem anderen Gerät geöffnet, warnt die App beim Öffnen.
- **Nextcloud eingebaut** (☰-Menü, „Nextcloud“; auch unter Android und ohne Sync-Client): Adresse der eigenen Nextcloud eingeben – auch ein privater Server, in einem Unterordner (`example.org/nextcloud`) oder mit eigenem Port. Die Anmeldung läuft im Browser (auch mit Zwei-Faktor-Anmeldung); die App bekommt ein eigenes App-Passwort, das sich in Nextcloud unter Einstellungen → Sicherheit jederzeit widerrufen lässt, und sieht das echte Passwort nie. Die Notizbücher werden alle 30 Sekunden und kurz nach dem Speichern mit dem Ordner `ink-notes` in den Nextcloud-Dateien abgeglichen; offline arbeitet die App normal weiter. Wurde eine Seite auf zwei Geräten geändert, bleiben beide Fassungen erhalten (die andere als eigene Seite); gelöschte Notizbücher und Seiten landen im Papierkorb (lokal `.trash`, auf dem Server dessen Papierkorb). Einem selbst signierten Zertifikat vertraut die App, nachdem du seinen Fingerabdruck bestätigt hast; Adressen mit `http://` gehen nur mit Warnung, weil das App-Passwort dann im Netz lesbar wäre. „Trennen“ beendet den Abgleich und widerruft das App-Passwort; die Notizbücher bleiben erhalten. Bitte entweder den eingebauten Abgleich oder den Ordner eines Sync-Clients als Speicherort verwenden, nicht beides für dieselben Notizbücher.
### Export
- **PDF** (`Strg + E`) nach `~/Documents/<Titel>.pdf`: Striche und Formen als Vektoren, Textfelder als echter, durchsuchbarer Text, importierte PDF-Seiten als Vektor-Unterlage, Seitentitel als Lesezeichen.
@@ -190,7 +196,12 @@ src/
├── history.rs # Rückgängig/Wiederholen
├── storage.rs # Laden/Speichern (atomar), Notizbücher, Papierkorb, Speicherort
├── disk_watch.rs # Änderungen anderer Geräte übernehmen (Sync-Clients)
├── nextcloud.rs # Nextcloud: Anmeldung im Browser, WebDAV, gemerkte Zertifikate
├── cloud_sync.rs # Eingebauter Abgleich mit Nextcloud (Vergleich, Zusammenführen, Hintergrund)
├── library.rs # Ordner der Notizbücher (library.json)
├── math.rs # Exaktes Ausrechnen handgeschriebener Formeln (Issue #46)
├── math_ink.rs # Erkennung handgeschriebener Formeln ($P-Punktwolken)
├── math_assist.rs # Ergebnis neben einem handgeschriebenen „=“ (Auslöser, Vorschlag)
├── autosave.rs # Speichern im Hintergrund
├── export.rs # PDF-Export
├── page_export.rs # Seite als PNG/SVG, Auswahl als Bild
+15 -4
View File
@@ -22,12 +22,12 @@ The interface is available in English and German; ink-notes follows the system l
## Features
### Writing and drawing
- **Four pens** in the toolbar, each with its own color and width (0.15–5 mm, pressure-sensitive). When you change a pen's color or width, it remembers that – also across restarts. **Highlighter** (semi-transparent, 4–8 mm) in four colors; it does not get darker within one stroke, only a new stroke covers more strongly.
- **Custom colors:** Every color in the toolbar (pens, highlighter, text) can be changed – tap the selected color again and pick a preset or any shade with 🎨. The toolbar remembers the colors. They look the same on screen as in PDF and image exports.
- **Seven pens** in the toolbar, each with its own color and width (0.15–5 mm, pressure-sensitive). When you change a pen's color or width, it remembers that – also across restarts. **Highlighter** (semi-transparent, 4–8 mm) in four colors; it does not get darker within one stroke, only a new stroke covers more strongly.
- **Custom colors:** Every color in the toolbar (pens, highlighter, text) can be changed – tap the selected color again and pick a preset or any shade with 🎨. The toolbar remembers the colors. Under the presets, “➕ Save color” keeps the current shade in “My colors” (up to 12, shared by pens, highlighter and text; 🗑 removes the selected one). They look the same on screen as in PDF and image exports.
- **Two-stage smoothing:** While you write, a 1€ filter removes jitter and the line appears immediately. When you lift the pen, the stroke is refined: corners (bends over 65°) are kept and drawn round, the pressure curve is smoothed, redundant points are dropped and the ends become round.
- **Pressure curve** adjustable to your pen in the ☰ menu (softer/harder, threshold against base noise).
- **Pen buttons** can be assigned in the ☰ menu: erase (default), select, highlighter, pen or nothing – active while the button is held. Each button appears there after it is pressed for the first time.
- **Eraser** with three modes: whole strokes, only the part you wipe over (partial stroke), or highlighter only; its size is set in a menu like the pens (remembered). The back end of the pen erases too.
- **Eraser** with three modes: whole strokes, only the part you wipe over (partial stroke), or highlighter only; text boxes it touches go as a whole, images stay; its size is set in a menu like the pens (remembered). The back end of the pen erases too.
- **Virtual ruler** (`L`): move it, rotate it by its handle (snaps to 15°); strokes along an edge become straight.
- **View only** (👁 in the toolbar): pen, finger and mouse only scroll and zoom; the tools disappear and nothing can be changed by accident. Tap 👁 again to write.
- **Make space** (↕ in the toolbar): tap the page and drag down – everything below moves along, also on the following pages. Whatever would slide past the end of a page continues on the next one; missing pages are appended. Dragging up closes empty space. Undo reverts it in one step.
@@ -37,6 +37,11 @@ The interface is available in English and German; ink-notes follows the system l
- Recognized shapes: lines, arrows, circles, ellipses, rectangles, triangles, rhombuses, polygons, coordinate systems (L shape), axis crosses (a "+" in one stroke or two crossing straight lines, arrowheads on all four ends) and octahedra (sketch as a 3D solid).
- Recognition is purely geometric (arc length, Douglas-Peucker, circle fitting), without machine learning. Not everything is recognized – whatever does not fit stays handwriting.
### Computing formulas (experimental)
- Turn on in the ☰ menu under input ("Compute formulas after “=”"). After a handwritten `=` and a short pause, the result appears next to it together with what was read, e.g. `2⁴ = 16` or `(x + 3)² = x² + 6x + 9`; tapping it with the pen or clicking inserts it as a text box in the ink color, as high as the handwriting (undo removes it). Fingers and palms do not trigger it.
- Read are digits, `+ − · : /`, parentheses, exponents (written raised), fraction bars, decimal commas and the variables `x`, `y`, `a`, `b`. Numbers are computed exactly as fractions, expressions with variables are multiplied out. Leave some space before the formula, so that writing further to the left is not read with it; in a chain such as `x = 2 · 3 =` the part after the previous `=` is computed.
- The symbols are recognized with point-cloud templates ($P), without machine learning and offline. If the suggestion shows a wrong symbol, just ignore it.
### Selecting and editing
- **Selection tool** (`V`): tap or drag a frame, then move, scale by the corners, rotate by the handle, or rotate by 90° with `R`. Works with mouse, finger and pen.
- **Recolor** the selection with the palette, **copy/paste** (`Ctrl + C/V`), **delete** (`Del`).
@@ -68,7 +73,8 @@ The interface is available in English and German; ink-notes follows the system l
- Every file is first written completely to a temporary file and then replaced – a crash while saving never leaves a half-written file.
- **When closing** (window or `Ctrl + Q`) and when switching notebooks, everything pending is written before continuing. If that fails, the app stays open and offers to retry or to make an emergency backup.
- `Ctrl + S` saves immediately. The dot on the ☰ button shows the state: green = saved, orange = changes pending, yellow = saving, red = error.
- **Storage location** (☰ menu, "Storage location"): put the notebooks into a folder that **Nextcloud, Google Drive, Syncthing or Dropbox** keep in sync, and they are available on all your devices. The notebooks are kept in the subfolder `ink-notes`; if it is still empty, the existing notebooks are copied there (the old ones stay). Changes arriving from another device show up within a few seconds without reopening – but only while everything local is saved, so nothing gets overwritten. If the sync client makes a conflict copy of a page, it is added as a page of its own („Conflict copy: …“) instead of getting lost. Opening a notebook that is open on another device shows a warning.
- **Storage location** (☰ menu, "Storage location"): put the notebooks into a folder that **Nextcloud, Google Drive, Syncthing or Dropbox** keep in sync, and they are available on all your devices. The notebooks are kept in the subfolder `ink-notes`; if it is still empty, the existing notebooks are copied there (the old ones stay). Changes arriving from another device show up within a few seconds without reopening – but only while everything local is saved, so nothing gets overwritten. If the sync client makes a conflict copy of a page, it is added as a page of its own („Conflict copy: …“) instead of getting lost. The same happens when a change arrives just while you are writing on that page: your version is saved and theirs is kept as a conflict copy. Opening a notebook that is open on another device shows a warning.
- **Nextcloud built in** (☰ menu, "Nextcloud"; also on Android and without a sync client): enter the address of your Nextcloud – also a private server, in a subfolder (`example.org/nextcloud`) or with its own port. Signing in happens in the browser (also with two-factor sign-in); the app gets an app password of its own, which you can revoke in Nextcloud under Settings → Security at any time, and never sees your real password. The notebooks are synced with the folder `ink-notes` in your Nextcloud files every 30 seconds and shortly after saving; the app keeps working offline. If a page was changed on two devices, both versions are kept (the other one as a page of its own); deleted notebooks and pages go into the trash (locally `.trash`, on the server its trash bin). A self-signed certificate is trusted once you have confirmed its fingerprint; addresses with `http://` only work with a warning, since the app password would be readable on the network. "Disconnect" stops syncing and revokes the app password; the notebooks stay. Use either the built-in sync or a sync client's folder as storage location, not both for the same notebooks.
### Export
- **PDF** (`Ctrl + E`) to `~/Documents/<title>.pdf`: strokes and shapes as vectors, text boxes as real, searchable text, imported PDF pages as vector background, page titles as bookmarks.
@@ -192,7 +198,12 @@ src/
├── history.rs # Undo/redo
├── storage.rs # Loading/saving (atomic), notebooks, trash, storage location
├── disk_watch.rs # Taking over changes from other devices (sync clients)
├── nextcloud.rs # Nextcloud: sign-in in the browser, WebDAV, pinned certificates
├── cloud_sync.rs # Built-in sync with Nextcloud (comparison, merging, background thread)
├── library.rs # Notebook folders (library.json)
├── math.rs # Exact evaluation of handwritten math (Issue #46)
├── math_ink.rs # Recognition of handwritten math ($P point clouds)
├── math_assist.rs # Result next to a handwritten "=" (trigger, suggestion)
├── autosave.rs # Saving in the background
├── export.rs # PDF export
├── page_export.rs # Page as PNG/SVG, selection as image
+1
View File
@@ -177,6 +177,7 @@ Liegen die Notizbücher in einem Ordner, den Nextcloud, Google Drive, Syncthing
- `.opened_by.json` (`{"device": "<Rechnername>", "since": <Unix-Zeit>}`) wird beim Öffnen eines Notizbuchs geschrieben und beim Schließen entfernt. Eine Markierung eines anderen Geräts führt zu einer Warnung; die Sperrdatei `.lock` schützt nur vor einer zweiten Instanz auf demselben Rechner.
- Solange alle lokalen Änderungen gespeichert sind, prüft die App den Ordner alle 3 Sekunden (und wenn das Fenster den Fokus bekommt). Geänderte Seitendateien werden neu geladen, ein geändertes `manifest.json` bringt neue, entfernte oder umsortierte Seiten und Titel mit.
- Konfliktkopien der Sync-Clients (`page_<id> (….json`, `page_<id>_….json`, `page_<id>.sync-conflict-….json`) werden als neue Seiten hinter dem Original eingefügt und in `<name>.imported` umbenannt.
- Bevor die App eine Seitendatei überschreibt, die sich seit ihrem letzten Lesen oder Schreiben geändert hat, sichert sie den vorgefundenen Inhalt als `page_<id> (conflict <Unix-Zeit>).json` – außer sie hat selbst nichts geändert (dann bleibt die Datei, wie sie ist).
### Neuere Versionen
Dateien mit einer höheren `format_version` als der unterstützten werden nicht geöffnet – so kann eine ältere App-Version keine Inhalte verlieren, die sie nicht kennt.
+1
View File
@@ -177,6 +177,7 @@ If the notebooks are kept in a folder synced by Nextcloud, Google Drive, Syncthi
- `.opened_by.json` (`{"device": "<host name>", "since": <unix time>}`) is written when a notebook is opened and removed when it is closed. A marker from another device leads to a warning; the lock file `.lock` only protects against a second instance on the same computer.
- While all local changes are saved, the app checks the folder every 3 seconds (and when the window gets the focus). Changed page files are reloaded, a changed `manifest.json` brings in new, removed or reordered pages and titles.
- Conflict copies of sync clients (`page_<id> (….json`, `page_<id>_….json`, `page_<id>.sync-conflict-….json`) are added as new pages after the original and renamed to `<name>.imported`.
- Before the app overwrites a page file that changed since it last read or wrote it, it keeps the content found there as `page_<id> (conflict <unix time>).json`, unless it has no changes of its own (then the file is left as it is).
### Newer versions
Files with a higher `format_version` than supported are not opened – this way an older app version cannot lose content it does not know.
@@ -5,11 +5,13 @@ import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.OpenableColumns;
import android.text.InputType;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowInsets;
import android.view.inputmethod.BaseInputConnection;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputConnection;
@@ -29,6 +31,9 @@ import java.io.OutputStream;
* An invisible view with its own input connection passes committed text to Rust.
* - Picking files: the system file dialog (ACTION_OPEN_DOCUMENT); the chosen file is copied to
* the cache folder, as Rust can only open files by path.
* - System bars: NativeActivity draws the whole window, also below the status and navigation
* bars and display cutouts. Their sizes go to Rust, which keeps its buttons out of them
* (Issue #50).
*
* Rust calls showKeyboard and pickFile through JNI and receives the native* calls
* (src/android_bridge.rs).
@@ -49,6 +54,9 @@ public class InkActivity extends NativeActivity {
/** Path of the copied file, or null if nothing was chosen or copying failed. */
static native void nativeFilePicked(String path);
/** Window edges covered by system bars or cutouts, in pixels. */
static native void nativeSetInsets(int left, int top, int right, int bottom);
private TextSink textSink;
@Override
@@ -56,6 +64,26 @@ public class InkActivity extends NativeActivity {
super.onCreate(savedInstanceState);
textSink = new TextSink(this);
addContentView(textSink, new ViewGroup.LayoutParams(1, 1));
View decor = getWindow().getDecorView();
decor.setOnApplyWindowInsetsListener((view, insets) -> {
reportInsets(insets);
return view.onApplyWindowInsets(insets);
});
decor.requestApplyInsets();
}
@SuppressWarnings("deprecation")
private static void reportInsets(WindowInsets insets) {
if (Build.VERSION.SDK_INT >= 30) {
android.graphics.Insets bars = insets.getInsets(
WindowInsets.Type.systemBars() | WindowInsets.Type.displayCutout());
nativeSetInsets(bars.left, bars.top, bars.right, bars.bottom);
} else {
// Before Android 11 the system window insets include the bars (and since
// Android 9 the cutout)
nativeSetInsets(insets.getSystemWindowInsetLeft(), insets.getSystemWindowInsetTop(),
insets.getSystemWindowInsetRight(), insets.getSystemWindowInsetBottom());
}
}
/** Shows or hides the soft keyboard; called from Rust on any thread. */
+50
View File
@@ -84,6 +84,56 @@
</branding>
<content_rating type="oars-1.1" />
<releases>
<release version="0.45.2" date="2026-11-14">
<description>
<p>A change from another device that arrives while you are writing on the same page is no longer overwritten; it is kept as a conflict copy.</p>
</description>
</release>
<release version="0.45.1" date="2026-11-14">
<description>
<p>Fixes from a review of the whole project, mainly for the safety of your notes: switching notebooks, the trash, copied notebook folders, scaled strokes and imports of broken files.</p>
</description>
</release>
<release version="0.45.0" date="2026-11-14">
<description>
<p>Own colors can be saved under the presets of the color menus, and copy and paste in text fields works with other apps.</p>
</description>
</release>
<release version="0.44.0" date="2026-11-09">
<description>
<p>Seven pens in the toolbar, dialogs that fit on phones, and touch lifts that arrive right away on GNOME, so the page glides after a swipe and the ruler no longer follows the hovering pen.</p>
</description>
</release>
<release version="0.43.0" date="2026-11-09">
<description>
<p>The pen draws without delay again on Wayland, and the eraser removes text boxes such as an inserted math result.</p>
</description>
</release>
<release version="0.42.2" date="2026-11-05">
<description>
<p>"Draw with finger" is remembered now and on by default on Android, so phones without a pen can write again after a restart or an update.</p>
</description>
</release>
<release version="0.42.1" date="2026-11-02">
<description>
<p>Swiping with a finger glides on again on Wayland: lifting the finger no longer arrives late and stops the page right away.</p>
</description>
</release>
<release version="0.42.0" date="2026-10-28">
<description>
<p>Built-in Nextcloud sync: sign in through the browser and the notebooks are synced with your Nextcloud, also private servers and without a sync client.</p>
</description>
</release>
<release version="0.41.1" date="2026-10-28">
<description>
<p>Android: the toolbar no longer hides behind the status bar and the navigation bar.</p>
</description>
</release>
<release version="0.41.0" date="2026-10-28">
<description>
<p>Experimental: computes handwritten formulas after an equals sign and offers the result next to it (turn on in the menu under input).</p>
</description>
</release>
<release version="0.40.0" date="2026-10-28">
<description>
<p>New Android version: a signed APK for 64-bit ARM devices with pen, touch, soft keyboard, file import and exports to Downloads.</p>
+5 -5
View File
@@ -3655,14 +3655,14 @@
{
"type": "archive",
"archive-type": "tar-gzip",
"url": "https://static.crates.io/crates/rustls/rustls-0.23.45.crate",
"sha256": "0d41d731c7d2f962d1ccc364cec258de3c0e93b38c2fb3ba97ac74513048d634",
"dest": "cargo/vendor/rustls-0.23.45"
"url": "https://static.crates.io/crates/rustls/rustls-0.23.46.crate",
"sha256": "48e13bd8c0e9365c43cfa5c9e8f9ad49d3c8444926c9aac819e0e4dc503c8fdf",
"dest": "cargo/vendor/rustls-0.23.46"
},
{
"type": "inline",
"contents": "{\"package\": \"0d41d731c7d2f962d1ccc364cec258de3c0e93b38c2fb3ba97ac74513048d634\", \"files\": {}}",
"dest": "cargo/vendor/rustls-0.23.45",
"contents": "{\"package\": \"48e13bd8c0e9365c43cfa5c9e8f9ad49d3c8444926c9aac819e0e4dc503c8fdf\", \"files\": {}}",
"dest": "cargo/vendor/rustls-0.23.46",
"dest-filename": ".cargo-checksum.json"
},
{
+50
View File
@@ -84,6 +84,56 @@
</branding>
<content_rating type="oars-1.1" />
<releases>
<release version="0.45.2" date="2026-11-14">
<description>
<p>A change from another device that arrives while you are writing on the same page is no longer overwritten; it is kept as a conflict copy.</p>
</description>
</release>
<release version="0.45.1" date="2026-11-14">
<description>
<p>Fixes from a review of the whole project, mainly for the safety of your notes: switching notebooks, the trash, copied notebook folders, scaled strokes and imports of broken files.</p>
</description>
</release>
<release version="0.45.0" date="2026-11-14">
<description>
<p>Own colors can be saved under the presets of the color menus, and copy and paste in text fields works with other apps.</p>
</description>
</release>
<release version="0.44.0" date="2026-11-09">
<description>
<p>Seven pens in the toolbar, dialogs that fit on phones, and touch lifts that arrive right away on GNOME, so the page glides after a swipe and the ruler no longer follows the hovering pen.</p>
</description>
</release>
<release version="0.43.0" date="2026-11-09">
<description>
<p>The pen draws without delay again on Wayland, and the eraser removes text boxes such as an inserted math result.</p>
</description>
</release>
<release version="0.42.2" date="2026-11-05">
<description>
<p>"Draw with finger" is remembered now and on by default on Android, so phones without a pen can write again after a restart or an update.</p>
</description>
</release>
<release version="0.42.1" date="2026-11-02">
<description>
<p>Swiping with a finger glides on again on Wayland: lifting the finger no longer arrives late and stops the page right away.</p>
</description>
</release>
<release version="0.42.0" date="2026-10-28">
<description>
<p>Built-in Nextcloud sync: sign in through the browser and the notebooks are synced with your Nextcloud, also private servers and without a sync client.</p>
</description>
</release>
<release version="0.41.1" date="2026-10-28">
<description>
<p>Android: the toolbar no longer hides behind the status bar and the navigation bar.</p>
</description>
</release>
<release version="0.41.0" date="2026-10-28">
<description>
<p>Experimental: computes handwritten formulas after an equals sign and offers the result next to it (turn on in the menu under input).</p>
</description>
</release>
<release version="0.40.0" date="2026-10-28">
<description>
<p>New Android version: a signed APK for 64-bit ARM devices with pen, touch, soft keyboard, file import and exports to Downloads.</p>
+16
View File
@@ -3,6 +3,22 @@
Notebook app for pen, touch and mouse on Linux (tested on Zorin OS 18 / Ubuntu 24.04).
### Highlights:
- **Sync while writing**: If a change from another device reached a page just while you were writing on it, saving your stroke overwrote that change. Now your version is saved and theirs is kept as a page of its own ("Conflict copy: …"), as for other sync conflicts; if you changed nothing on the page, their version is simply taken over
- **Safer notes**: A review of the whole project fixed a number of rare errors. Notebooks deleted after not being edited for 30 days were removed from the trash at the next start instead of 30 days later. If saving failed while switching notebooks, pages could end up in the other notebook, and an open text field moved along; the app now saves first and stays in the notebook if that fails. A copied notebook folder is a notebook of its own instead of opening the original. Strokes keep their width after scaling and a restart, shapes are exported to PDF with their line width, and a page that could not be loaded always shows its warning. Importing broken Rnote, Saber, SVG or PDF files can no longer make a page unreadable or stop the PDF display. One invalid value in the settings file no longer resets all settings, and updates are only installed if the signed package is newer than the running version
- **My colors**: Under the presets of the color menus (pen, highlighter, text), "➕ Save color" keeps the current shade in a row of your own colors – up to 12, shared by all three tools and kept across restarts; 🗑 removes the selected one (Issue #56)
- **Copy and paste in text fields**: Ctrl+C, Ctrl+X and Ctrl+V in a text field now use the system clipboard, so text moves between ink-notes and other apps. Before, they only worked within the app (Issue #57)
- **Seven pens**: The toolbar now has seven pens instead of four (new: violet, orange, gray). Each remembers its color and width; tap the selected pen again to change them. Older versions keep reading the first four pens (Issue #56)
- **Momentum scrolling on GNOME, second try**: GNOME could deliver the lift of a swipe only together with the next touch, because the library under winit held it back until the next touch frame. Lifts are now handed on right away, so the page glides on after a quick swipe (Issue #28)
- **Ruler stays put**: After moving the ruler with a finger, it could follow the hovering pen (same cause as above). In addition, a finger drag of the ruler now ends as soon as the pen comes near (Issue #55)
- **Dialogs on phones**: The notebook manager and the other dialogs were wider than a phone screen, partly under the toolbar and cut off at the right edge. They now fit beside the toolbar (Issue #50)
- **Pen without lag on Wayland**: Pen events reached the app only when something else woke it, so strokes trailed behind the pen or appeared only later. Every event from the compositor now wakes the app; it still uses no CPU while idle (Issue #53)
- **Eraser removes text**: The whole-stroke and partial erasers now remove a text box – typed text or an inserted math result – as soon as they touch it. The highlighter eraser leaves text alone, images are never erased (Issue #54)
- **Writing with the finger on Android**: "Draw with finger" (☰ → Input) is now remembered and starts switched on on Android, where phones usually have no pen. Before, it was off after every start, so after an update one finger only moved the page (Issue #50)
- **Momentum scrolling with the finger fixed**: On Wayland, lifting the finger after a swipe could reach the app only together with the next touch, so the page stopped dead instead of gliding on. The app now keeps listening while a finger is on the screen, so the page glides after a quick swipe and slows down gradually (Issue #28)
- **Nextcloud built in**: ☰ → Storage location → Nextcloud syncs the notebooks directly with your Nextcloud – also on Android and without a sync client. Enter the server address (private servers in a subfolder, with their own port or a self-signed certificate work too), sign in in the browser; the app gets an app password of its own that "Disconnect" revokes again. Syncing runs every 30 seconds and shortly after saving and keeps working offline; a page changed on two devices is kept twice, deleted notebooks and pages go into the trash on both sides, changes arriving for the open notebook wait until it is saved (Issue #49)
- **Security update**: rustls 0.23.46 (TLS library for update checks and the Nextcloud sync) fixes a way for key material to end up in logs (Issue #51); ink-notes itself was not affected, as it doesn't keep TLS sessions
- **Android toolbar fix**: The toolbar and menus stay clear of the status bar, the navigation bar and the camera cutout, so the topmost button (📚 notebooks) can be tapped again; in landscape the page is centered between the side bars. Nothing changes on Linux and Windows (Issue #50)
- **Computing formulas (experimental)**: Turn on in ☰ → Input ("Compute formulas after “=”"). After a handwritten `=` and a short pause, the result appears next to it together with what was read, e.g. `2⁴ = 16` or `(x + 3)² = x² + 6x + 9`; tapping it with the pen or clicking inserts it as text in the ink color and as high as the handwriting. Digits, `+ − · : /`, parentheses, exponents, fraction bars, decimal commas and `x`, `y`, `a`, `b` are read; numbers are computed exactly, expressions with variables multiplied out. Recognition runs offline without machine learning – feedback with real handwriting welcome (Issue #46)
- **Android**: `ink-notes_<version>_arm64.apk` brings ink-notes to Android 8 and newer (64-bit ARM) – download it on the device and open it to install. The pen is told apart from fingers (pressure, pen buttons, eraser end, palm rejection), text fields open the soft keyboard, import and inserting images use the system file dialog, and exports are saved to `Download/ink-notes` (Android 10 and newer). Notebooks live in the app's own storage and are removed when uninstalling; updates are installed by opening the new APK. Nothing changes on Linux and Windows
- **Battery drain fixed**: ink-notes redrew its window at full frame rate even when nothing happened, which kept the app and the desktop (e.g. gnome-shell) busy and drained the battery. It now only draws when something changes; idle CPU load drops to practically zero. Also applies to Windows. Internally, the Android version now follows the app lifecycle and can be packed into an APK (not part of the release yet)
- **PDF export on current lopdf**: The PDF export uses its own small PDF writer on top of lopdf 0.46 instead of the outdated printpdf 0.7 (which pinned lopdf 0.31). Exported PDFs look the same – layers, searchable text, images, highlighters and bookmarks stay as before
+40 -8
View File
@@ -6,6 +6,7 @@
//! below, possibly on another thread, so they only queue messages and wake the event loop.
use std::path::PathBuf;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Mutex;
use jni::objects::{JObject, JString, JValue};
@@ -24,15 +25,23 @@ enum Message {
static MESSAGES: Mutex<Vec<Message>> = Mutex::new(Vec::new());
/// What the open file dialog is for
static PICKING: Mutex<Option<PickerKind>> = Mutex::new(None);
/// Window edges covered by system bars or cutouts (left, top, right, bottom; pixels)
static INSETS: Mutex<[f32; 4]> = Mutex::new([0.0; 4]);
/// The insets changed since the last frame
static INSETS_CHANGED: AtomicBool = AtomicBool::new(false);
fn wake() {
if let Some(app) = crate::platform::android_app() {
app.create_waker().wake();
}
}
fn push(message: Message) {
MESSAGES
.lock()
.unwrap_or_else(|e| e.into_inner())
.push(message);
if let Some(app) = crate::platform::android_app() {
app.create_waker().wake();
}
wake();
}
#[no_mangle]
@@ -75,16 +84,39 @@ pub extern "system" fn Java_eu_pansi_inknotes_InkActivity_nativeFilePicked(
push(Message::Picked(path));
}
/// Is a message waiting (then the app draws a new frame to take it)?
#[no_mangle]
pub extern "system" fn Java_eu_pansi_inknotes_InkActivity_nativeSetInsets(
_env: JNIEnv,
_class: JObject,
left: jint,
top: jint,
right: jint,
bottom: jint,
) {
let insets = [left, top, right, bottom].map(|v| v.max(0) as f32);
*INSETS.lock().unwrap_or_else(|e| e.into_inner()) = insets;
INSETS_CHANGED.store(true, Ordering::Relaxed);
wake();
}
/// Window edges covered by the status bar, the navigation bar or display cutouts (left, top,
/// right, bottom; physical pixels)
pub fn insets() -> [f32; 4] {
*INSETS.lock().unwrap_or_else(|e| e.into_inner())
}
/// Is a message waiting or did the insets change (then the app draws a new frame)?
pub fn has_messages() -> bool {
!MESSAGES
.lock()
.unwrap_or_else(|e| e.into_inner())
.is_empty()
INSETS_CHANGED.load(Ordering::Relaxed)
|| !MESSAGES
.lock()
.unwrap_or_else(|e| e.into_inner())
.is_empty()
}
/// Typed text and deletions as egui input, and the action for a picked file.
pub fn take_messages() -> (Vec<egui::Event>, Vec<UiAction>) {
INSETS_CHANGED.store(false, Ordering::Relaxed);
let messages = std::mem::take(&mut *MESSAGES.lock().unwrap_or_else(|e| e.into_inner()));
let mut events = Vec::new();
let mut actions = Vec::new();
+424 -122
View File
@@ -125,9 +125,16 @@ fn space_release(
return;
};
if let Some(run) = notebook.space_run(drag.page_index) {
for index in run {
for index in run.clone() {
let _ = storage::ensure_page_loaded(storage_dir, notebook, index);
}
if notebook.pages[run].iter().any(|p| p.is_readonly()) {
ui_state.set_export_message(
t!("Platz schaffen nicht möglich: Eine der Seiten konnte nicht geladen werden")
.to_string(),
);
return;
}
}
let dy = drag.clamped_dy();
if let Some(changed) = notebook.make_space(drag.page_index, drag.y, dy) {
@@ -335,7 +342,7 @@ fn commit_stroke_to_notebook(
notebook: &mut Notebook,
manifest: &mut NotebookManifest,
autosave: &mut AutosaveController,
ui_state: &UiState,
ui_state: &mut UiState,
window: &Window,
snapped_shape: Option<RecognizedShape>,
) {
@@ -357,6 +364,7 @@ fn commit_stroke_to_notebook(
let page = notebook.current_page_mut();
page.replace_stroke(previous_id, stroke);
canvas.sync_page(page);
ui_state.math.stroke_committed(page, Instant::now());
autosave.mark_dirty(notebook, manifest);
window.request_redraw();
return;
@@ -383,6 +391,8 @@ fn commit_stroke_to_notebook(
let page = notebook.current_page_mut();
page.add_stroke(stroke);
canvas.sync_page(page);
// An `=` may have been completed (Issue #46)
ui_state.math.stroke_committed(page, Instant::now());
manifest.next_stroke_id = notebook.next_stroke_id;
autosave.mark_dirty(notebook, manifest);
// Durchgehende Ansicht: unten auf der letzten Seite geht es auf einer neuen weiter
@@ -570,6 +580,19 @@ impl ClipboardBridge {
(!text.trim().is_empty()).then_some(text)
}
/// Text copied or cut in a text field goes to the system clipboard, so it can be pasted
/// into other apps (Issue #57).
fn set_system_text(&mut self, text: String) {
if self.system.is_none() {
self.system = platform::clipboard::Clipboard::new().ok();
}
if let Some(system) = self.system.as_mut() {
if let Err(err) = system.set_text(text) {
eprintln!("[Zwischenablage] Text nicht gesetzt: {err}");
}
}
}
/// Merkt sich beim internen Kopieren das aktuelle Systembild, den Text und das
/// Quell-Notizbuch.
fn note_internal_copy(&mut self, storage_dir: &Path) {
@@ -915,6 +938,54 @@ fn insert_text_on_page(
ui_state.set_export_message(t!("Text eingefügt – mit dem Text-Werkzeug (T) änderbar"));
}
/// Inserts the result of a math suggestion as a text box next to its `=`, in the ink color
/// and as high as the handwriting, as one undo step (Issue #46)
fn insert_math_result(
suggestion: &crate::math_assist::Suggestion,
canvas: &mut CanvasState,
notebook: &mut Notebook,
manifest: &mut NotebookManifest,
autosave: &mut AutosaveController,
) {
let page = notebook.current_page();
if page.id != suggestion.page_id || page.is_readonly() {
return;
}
let font = crate::text::TextFont::Sans;
let ascent_per_em = crate::text::TextBox::new(String::new(), 1.0, font, 1.0)
.layout()
.ascent;
let (origin, size) = suggestion.text_placement(ascent_per_em);
let mut edit = crate::text::TextEditing::new_at(
page.id,
origin,
page.text_right_edge(origin.x),
size,
font,
suggestion.color,
);
edit.origin = origin;
edit.text.content = suggestion.result.clone();
// As wide as the result, so that it does not wrap near the edge of the page
edit.text.wrap_width = edit
.text
.layout()
.lines
.iter()
.map(|line| line.width)
.fold(0.0, f32::max)
+ size;
let id = notebook.next_stroke_id();
let Some(stroke) = edit.to_stroke(id) else {
return;
};
let page = notebook.current_page_mut();
page.add_stroke(stroke);
canvas.sync_page(page);
manifest.next_stroke_id = notebook.next_stroke_id;
autosave.mark_dirty(notebook, manifest);
}
/// Beendet eine laufende Textbearbeitung: übernimmt das Feld (neu, geändert oder – wenn
/// geleert – entfernt) als einen Undo-Schritt auf seiner Seite.
fn finish_text_edit(
@@ -953,6 +1024,32 @@ fn finish_text_edit(
canvas.sync_page(notebook.current_page_mut());
}
/// Before another notebook takes the place of the open one: finishes a running text edit
/// (it belongs to this notebook, page ids repeat in every notebook) and saves everything.
/// Returns `false` and tells the user when saving failed; the notebook then has to stay open,
/// otherwise the unsaved changes would be lost.
fn save_before_switch(
canvas: &mut CanvasState,
notebook: &mut Notebook,
manifest: &mut NotebookManifest,
autosave: &mut AutosaveController,
ui_state: &mut UiState,
) -> bool {
finish_text_edit(canvas, notebook, manifest, autosave, ui_state);
match autosave.flush() {
Ok(()) => true,
Err(err) => {
let msg = err.messages.join(", ");
eprintln!("[Autosave] Notebook not switched, saving failed: {msg}");
ui_state.set_export_message(t!(
"Nicht gewechselt, Speichern fehlgeschlagen: {err}",
err = msg
));
false
}
}
}
/// Text-Werkzeug: Tippen auf das Blatt beendet eine laufende Bearbeitung und öffnet das
/// Textfeld unter `page_pt` zum Bearbeiten – oder ein neues an dieser Stelle.
fn text_tool_click(
@@ -1416,9 +1513,12 @@ pub fn run_with(
ui_state.pattern_color = ui_settings.pattern_color;
ui_state.pressure_curve = ui_settings.pressure_curve;
ui_state.continuous_scroll = ui_settings.continuous_scroll;
ui_state.pen_presets = ui_settings.pen_presets;
ui_state.pen_presets = ui_settings.pens();
ui_state.stylus_buttons = ui_settings.stylus_buttons.clone();
ui_state.set_eraser_radius_mm(ui_settings.eraser_radius_mm);
ui_state.math.set_enabled(ui_settings.math_assist);
ui_state.touch_draw_enabled = ui_settings.touch_draw;
ui_state.saved_colors = ui_settings.saved_colors.clone();
ui_state.select_pen(ui_settings.active_pen);
ui_state.highlighter_colors = ui_settings.highlighter_colors;
ui_state.select_highlighter_color(ui_settings.active_highlighter);
@@ -1431,6 +1531,8 @@ pub fn run_with(
ui_state.library = Library::load(&storage_base_dir);
ui_state.storage_dir = storage_base_dir.clone();
ui_state.storage_dir_custom = ui_settings.storage_dir.is_some();
// Built-in Nextcloud sync of the storage folder (Issue #49, stage 2)
let mut cloud = crate::cloud_sync::CloudSync::new(storage_base_dir.clone());
if let Some(warning) = startup_warning {
refresh_notebooks(&storage_base_dir, &mut ui_state);
ui_state.set_export_message(warning);
@@ -1680,6 +1782,21 @@ pub fn run_with(
);
}
// Built-in sync: changes from the server to the open notebook wait while it
// has unsaved changes; a pass soon after saving
let unsaved = new_status != AutosaveStatus::Saved
|| is_drawing
|| is_stylus_drawing
|| ui_state.text_edit.is_some();
if cloud.update(&manifest.id, unsaved) {
disk_watch.check_soon(Instant::now());
refresh_notebooks(&storage_base_dir, &mut ui_state);
window.request_redraw();
}
if prev_status != AutosaveStatus::Saved && new_status == AutosaveStatus::Saved {
cloud.sync_soon();
}
// Take over changes from other devices (Issue #49), but only while everything
// local is saved and nothing is being drawn or typed
if disk_watch.dir != storage_dir {
@@ -1831,6 +1948,10 @@ pub fn run_with(
PenEvent::In => {
is_stylus_in_proximity = true;
stylus_tool_id = Some(tool);
// A finger that moved the ruler is done once the pen comes near:
// if its lift has not arrived yet, the ruler would follow the
// hovering pen (Issue #55). The pen grabs the ruler only on touch.
ui_state.ruler_drag = None;
}
PenEvent::Out => {
if is_stylus_in_proximity {
@@ -1864,7 +1985,7 @@ pub fn run_with(
&mut notebook,
&mut manifest,
&mut autosave,
&ui_state,
&mut ui_state,
&window,
stroke_snapped_shape.take(),
);
@@ -2048,6 +2169,26 @@ pub fn run_with(
stylus_tool_id = Some(tool);
let (sx, sy) = stylus_screen_pos;
// Tapping a math suggestion inserts its result (Issue #46)
if let Some(suggestion) = (!ui_state.view_mode)
.then(|| {
ui_state.math.take_hit(egui::pos2(sx / scale, sy / scale))
})
.flatten()
{
insert_math_result(
&suggestion,
&mut canvas,
&mut notebook,
&mut manifest,
&mut autosave,
);
// The rest of this tap goes nowhere
stylus_ui_capture = true;
window.request_redraw();
continue;
}
if ui::is_screen_point_in_ui(&egui_ctx, &ui_state, (sx, sy), scale)
{
// In die Eingabe-Warteschlange des NÄCHSTEN egui-Frames
@@ -2275,7 +2416,7 @@ pub fn run_with(
&mut notebook,
&mut manifest,
&mut autosave,
&ui_state,
&mut ui_state,
&window,
snapped_shape,
);
@@ -2351,6 +2492,16 @@ pub fn run_with(
window.request_redraw();
}
// Pause after an `=` is over: compute the expression (Issue #46)
if ui_state.math.update(
notebook.current_page(),
Instant::now(),
is_drawing || is_stylus_drawing,
crate::i18n::language() == crate::i18n::Language::German,
) {
window.request_redraw();
}
// Nachgleiten nach dem Wischen
if let Some(delta) = fling.step(Instant::now()) {
canvas.pan(delta.x, delta.y);
@@ -2372,12 +2523,19 @@ pub fn run_with(
|| is_stylus_drawing
|| is_stylus_in_proximity
|| fling.is_active()
// While a finger is down: on Wayland, the lift can already sit in winit's
// queue (read from the socket by the graphics driver) without waking the
// loop, and then only arrives with the next touch, which stops the glide
// right away (Issue #28)
|| !active_touches.is_empty()
{
target.set_control_flow(ControlFlow::wait_duration(Duration::from_millis(8)));
} else if let Some(at) = [
egui_repaint_at,
long_press.deadline(),
ui_state.math.deadline(),
Some(disk_watch.deadline()),
cloud.deadline(),
// The updater thread does not wake the loop: look for its results now and then
matches!(
updater.status(),
@@ -2420,6 +2578,28 @@ pub fn run_with(
consumed: false,
}
} else {
// Ctrl+V in a text field: egui-winit is built without its own clipboard
// access and would only paste what was copied inside the app, so it gets the
// text of the system clipboard first (Issue #57)
if let WindowEvent::KeyboardInput {
event:
KeyEvent {
logical_key: Key::Character(c),
state: ElementState::Pressed,
..
},
..
} = &event
{
if c.eq_ignore_ascii_case("v")
&& egui_winit_state.egui_input().modifiers.command
&& egui_ctx.wants_keyboard_input()
{
if let Some(text) = clipboard.system_text() {
egui_winit_state.set_clipboard_text(text);
}
}
}
egui_winit_state.on_window_event(&window, &event)
};
// egui-winit asks for a repaint after every RedrawRequested as well; following
@@ -2495,6 +2675,7 @@ pub fn run_with(
WindowEvent::Focused(true) => {
// Coming back from another app: look for changes right away
disk_watch.check_soon(Instant::now());
cloud.sync_soon();
window.request_redraw();
}
@@ -3176,6 +3357,25 @@ pub fn run_with(
return;
}
// Clicking a math suggestion inserts its result (Issue #46)
if button == MouseButton::Left && !ui_state.view_mode {
let (sx, sy) = cursor_screen_pos;
let scale = window.scale_factor() as f32;
if let Some(suggestion) =
ui_state.math.take_hit(egui::pos2(sx / scale, sy / scale))
{
insert_math_result(
&suggestion,
&mut canvas,
&mut notebook,
&mut manifest,
&mut autosave,
);
window.request_redraw();
return;
}
}
fling.stop();
// 1. Pan via mittlere Maustaste oder Leertaste + Linksklick
// In view-only mode the left and right buttons move the page too (Issue #35)
@@ -3380,7 +3580,7 @@ pub fn run_with(
&mut notebook,
&mut manifest,
&mut autosave,
&ui_state,
&mut ui_state,
&window,
snapped_shape,
);
@@ -3589,6 +3789,9 @@ pub fn run_with(
match touch.phase {
TouchPhase::Started => {
// Jede neue Berührung hält ein gleitendes Blatt an
if debug_fling && fling.is_active() {
eprintln!("[Nachgleiten] Neuer Finger hält das Gleiten an");
}
fling.stop();
// Event-Isolation für Touch-Eingaben
let is_touch_in_ui = ui::is_screen_point_in_ui(
@@ -3958,7 +4161,7 @@ pub fn run_with(
&mut notebook,
&mut manifest,
&mut autosave,
&ui_state,
&mut ui_state,
&window,
snapped_shape,
);
@@ -4048,6 +4251,17 @@ pub fn run_with(
raw_input.events.extend(events);
ui_state.queued_actions.extend(actions);
}
// Keep the UI out of system bars and cutouts (Android, Issue #50)
let safe_area = platform::safe_area_insets();
if let Some(rect) = raw_input.screen_rect.filter(|_| safe_area != [0.0; 4])
{
let ppp = egui_ctx.pixels_per_point();
let [left, top, right, bottom] = safe_area.map(|v| v / ppp);
raw_input.screen_rect = Some(egui::Rect::from_min_max(
rect.min + egui::vec2(left, top),
rect.max - egui::vec2(right, bottom),
));
}
egui_ctx.begin_frame(raw_input);
// 2. Auswahl-Overlay rendern (Bounding Box, Griffe, Lasso)
@@ -4067,6 +4281,7 @@ pub fn run_with(
.as_ref()
.filter(|(id, _, _)| *id == manifest.id)
.map(|(_, index, _)| index + 1);
ui_state.cloud = cloud.view();
let actions = ui::render_ui(
&egui_ctx,
&mut ui_state,
@@ -4080,8 +4295,8 @@ pub fn run_with(
let reserved =
ui_state.sidebar_reserved_width() * window.scale_factor() as f32;
let (inset_left, inset_right) = match ui_state.sidebar_side {
SidebarSide::Left => (reserved, 0.0),
SidebarSide::Right => (0.0, reserved),
SidebarSide::Left => (reserved + safe_area[0], safe_area[2]),
SidebarSide::Right => (safe_area[0], reserved + safe_area[2]),
};
if canvas.set_ui_insets(inset_left, inset_right) {
window.request_redraw();
@@ -4671,11 +4886,18 @@ pub fn run_with(
if id == manifest.id {
ui_state.is_document_menu_open = false;
window.request_redraw();
} else if !save_before_switch(
&mut canvas,
&mut notebook,
&mut manifest,
&mut autosave,
&mut ui_state,
) {
window.request_redraw();
} else {
let new_notebook_dir = storage_base_dir.join(&id);
match storage::load_notebook(&new_notebook_dir) {
Ok((loaded_nb, loaded_manifest)) => {
let _ = autosave.flush();
match storage::NotebookLock::acquire(
&new_notebook_dir,
) {
@@ -4738,71 +4960,78 @@ pub fn run_with(
}
}
UiAction::ImportFile(path) => {
// Neben das gerade geöffnete Notizbuch
let folder =
ui_state.library.folder_of(&manifest.id).to_string();
let title = path
.file_stem()
.map(|s| s.to_string_lossy().into_owned())
.unwrap_or_else(|| "Import".to_string());
match crate::import::import_file(
&storage_base_dir,
&title,
&path,
if save_before_switch(
&mut canvas,
&mut notebook,
&mut manifest,
&mut autosave,
&mut ui_state,
) {
Ok((new_nb, new_manifest, new_dir, report)) => {
let id = new_manifest.id.clone();
change_library(
&storage_base_dir,
&mut ui_state,
|library| library.move_notebook(&id, &folder),
);
let _ = autosave.flush();
_notebook_lock =
storage::NotebookLock::acquire(&new_dir).ok();
autosave.set_read_only(_notebook_lock.is_none());
autosave.switch_notebook_dir(new_dir.clone());
notebook = new_nb;
manifest = new_manifest;
storage_dir = new_dir;
selection_state.clear();
ui_state.current_notebook_id = manifest.id.clone();
ui_state.document_title = manifest.title.clone();
refresh_notebooks(&storage_base_dir, &mut ui_state);
canvas.reset_view();
canvas.sync_page(notebook.current_page_mut());
update_window_title(
&window,
&notebook,
&manifest,
ui_state.active_tool,
autosave.status(),
);
let mut message = t!(
"'{title}' importiert ({count} Seiten)",
title = manifest.title,
count = notebook.pages.len()
);
if let Some(pdf) = &report.missing_pdf {
message.push_str(&t!(
", PDF-Hintergrund nicht gefunden: {file}",
file = pdf
// Neben das gerade geöffnete Notizbuch
let folder =
ui_state.library.folder_of(&manifest.id).to_string();
let title = path
.file_stem()
.map(|s| s.to_string_lossy().into_owned())
.unwrap_or_else(|| "Import".to_string());
match crate::import::import_file(
&storage_base_dir,
&title,
&path,
) {
Ok((new_nb, new_manifest, new_dir, report)) => {
let id = new_manifest.id.clone();
change_library(
&storage_base_dir,
&mut ui_state,
|library| library.move_notebook(&id, &folder),
);
_notebook_lock =
storage::NotebookLock::acquire(&new_dir).ok();
autosave.set_read_only(_notebook_lock.is_none());
autosave.switch_notebook_dir(new_dir.clone());
notebook = new_nb;
manifest = new_manifest;
storage_dir = new_dir;
selection_state.clear();
ui_state.current_notebook_id = manifest.id.clone();
ui_state.document_title = manifest.title.clone();
refresh_notebooks(&storage_base_dir, &mut ui_state);
canvas.reset_view();
canvas.sync_page(notebook.current_page_mut());
update_window_title(
&window,
&notebook,
&manifest,
ui_state.active_tool,
autosave.status(),
);
let mut message = t!(
"'{title}' importiert ({count} Seiten)",
title = manifest.title,
count = notebook.pages.len()
);
if let Some(pdf) = &report.missing_pdf {
message.push_str(&t!(
", PDF-Hintergrund nicht gefunden: {file}",
file = pdf
));
}
if report.skipped > 0 {
message.push_str(&t!(
", {count} Elemente übersprungen",
count = report.skipped
));
}
ui_state.set_export_message(message);
}
Err(err) => {
eprintln!("[Import] '{}': {err}", path.display());
ui_state.set_export_message(t!(
"Nicht importiert: {err}",
err = err
));
}
if report.skipped > 0 {
message.push_str(&t!(
", {count} Elemente übersprungen",
count = report.skipped
));
}
ui_state.set_export_message(message);
}
Err(err) => {
eprintln!("[Import] '{}': {err}", path.display());
ui_state.set_export_message(t!(
"Nicht importiert: {err}",
err = err
));
}
}
window.request_redraw();
@@ -4812,54 +5041,66 @@ pub fn run_with(
folder,
layout,
} => {
let _ = autosave.flush();
match storage::create_new_notebook(&storage_base_dir, &title) {
Ok((mut new_nb, mut new_manifest, new_dir)) => {
let id = new_manifest.id.clone();
new_manifest.layout = Some(layout);
if layout == NotebookLayout::Endless {
new_nb.make_endless();
if save_before_switch(
&mut canvas,
&mut notebook,
&mut manifest,
&mut autosave,
&mut ui_state,
) {
match storage::create_new_notebook(
&storage_base_dir,
&title,
) {
Ok((mut new_nb, mut new_manifest, new_dir)) => {
let id = new_manifest.id.clone();
new_manifest.layout = Some(layout);
if layout == NotebookLayout::Endless {
new_nb.make_endless();
}
change_library(
&storage_base_dir,
&mut ui_state,
|library| library.move_notebook(&id, &folder),
);
_notebook_lock =
storage::NotebookLock::acquire(&new_dir).ok();
autosave.set_read_only(_notebook_lock.is_none());
autosave.switch_notebook_dir(new_dir.clone());
notebook = new_nb;
manifest = new_manifest;
storage_dir = new_dir;
autosave.mark_dirty(&notebook, &mut manifest);
canvas.reset_view();
selection_state.clear();
ui_state.current_notebook_id = manifest.id.clone();
ui_state.document_title = manifest.title.clone();
refresh_notebooks(&storage_base_dir, &mut ui_state);
ui_state.is_document_menu_open = false;
canvas.sync_page(notebook.current_page_mut());
update_window_title(
&window,
&notebook,
&manifest,
ui_state.active_tool,
autosave.status(),
);
ui_state.set_export_message(t!(
"Dokument '{title}' erstellt",
title = manifest.title
));
window.request_redraw();
}
Err(err) => {
eprintln!("[Dokument-Fehler] Konnte Dokument '{title}' nicht erstellen: {err}");
ui_state.set_export_message(t!(
"Fehler beim Erstellen: {err}",
err = err
));
}
change_library(
&storage_base_dir,
&mut ui_state,
|library| library.move_notebook(&id, &folder),
);
_notebook_lock =
storage::NotebookLock::acquire(&new_dir).ok();
autosave.set_read_only(_notebook_lock.is_none());
autosave.switch_notebook_dir(new_dir.clone());
notebook = new_nb;
manifest = new_manifest;
storage_dir = new_dir;
autosave.mark_dirty(&notebook, &mut manifest);
canvas.reset_view();
selection_state.clear();
ui_state.current_notebook_id = manifest.id.clone();
ui_state.document_title = manifest.title.clone();
refresh_notebooks(&storage_base_dir, &mut ui_state);
ui_state.is_document_menu_open = false;
canvas.sync_page(notebook.current_page_mut());
update_window_title(
&window,
&notebook,
&manifest,
ui_state.active_tool,
autosave.status(),
);
ui_state.set_export_message(t!(
"Dokument '{title}' erstellt",
title = manifest.title
));
window.request_redraw();
}
Err(err) => {
eprintln!("[Dokument-Fehler] Konnte Dokument '{title}' nicht erstellen: {err}");
ui_state.set_export_message(t!(
"Fehler beim Erstellen: {err}",
err = err
));
}
} else {
window.request_redraw();
}
}
UiAction::RenameNotebook { id, new_title } => {
@@ -4929,7 +5170,21 @@ pub fn run_with(
.unwrap_or_else(|| id.clone());
if is_current {
let _ = autosave.flush();
finish_text_edit(
&mut canvas,
&mut notebook,
&mut manifest,
&mut autosave,
&mut ui_state,
);
if let Err(err) = autosave.flush() {
eprintln!(
"[Autosave] Not saved before deleting: {}",
err.messages.join(", ")
);
}
// Nothing may be written into the folder once it is in the trash
autosave.abandon_pending();
drop(_notebook_lock.take());
}
@@ -5128,6 +5383,26 @@ pub fn run_with(
}
window.request_redraw();
}
UiAction::CloudConnect { server, trusted } => {
cloud.connect(&server, trusted);
window.request_redraw();
}
UiAction::CloudCancel => {
cloud.cancel_login();
window.request_redraw();
}
UiAction::CloudSyncNow => {
let _ = autosave.flush();
cloud.sync_now();
window.request_redraw();
}
UiAction::CloudDisconnect => {
cloud.disconnect();
ui_state.set_export_message(
t!("Nextcloud getrennt – die Notizbücher bleiben auf diesem Gerät").to_string(),
);
window.request_redraw();
}
UiAction::OpenUrlInBrowser(url) => {
let _ = webbrowser::open(&url);
}
@@ -5187,6 +5462,22 @@ pub fn run_with(
}
}
}
UiAction::SetMathAssist(on) => {
ui_settings.math_assist = on;
save_ui_settings(
&ui_settings,
ui_settings_path.as_deref(),
&mut ui_state,
);
}
UiAction::SetTouchDraw(on) => {
ui_settings.touch_draw = on;
save_ui_settings(
&ui_settings,
ui_settings_path.as_deref(),
&mut ui_state,
);
}
UiAction::SetSidebarSide(side) => {
ui_settings.sidebar_side = side;
save_ui_settings(
@@ -5205,8 +5496,13 @@ pub fn run_with(
ui_state.set_export_message(
t!("Die Notizbücher liegen bereits dort").to_string(),
);
} else {
let _ = autosave.flush();
} else if save_before_switch(
&mut canvas,
&mut notebook,
&mut manifest,
&mut autosave,
&mut ui_state,
) {
match storage::copy_storage(&storage_base_dir, &new_base) {
Ok(copied) => {
// Release lock and open marker in the old location
@@ -5222,6 +5518,7 @@ pub fn run_with(
ui_state.storage_dir = storage_base_dir.clone();
ui_state.storage_dir_custom =
ui_settings.storage_dir.is_some();
cloud.set_local_dir(storage_base_dir.clone());
refresh_notebooks(&storage_base_dir, &mut ui_state);
// Same notebook if it exists there, otherwise the first one
let open_id = ui_state
@@ -5411,13 +5708,14 @@ pub fn run_with(
ui_state.sync_tool_colors();
if !egui_ctx.is_using_pointer() {
let mut updated = ui_settings.clone();
updated.pen_presets = ui_state.pen_presets;
updated.set_pens(ui_state.pen_presets);
updated.active_pen = ui_state.active_pen;
updated.highlighter_colors = ui_state.highlighter_colors;
updated.active_highlighter = ui_state.active_highlighter;
updated.text_colors = ui_state.text_colors;
updated.active_text_color = ui_state.active_text_color;
updated.eraser_radius_mm = ui_state.eraser_radius_mm;
updated.saved_colors.clone_from(&ui_state.saved_colors);
if updated != ui_settings {
ui_settings = updated;
save_ui_settings(
@@ -5455,6 +5753,10 @@ pub fn run_with(
// 4. egui Frame beenden & Primitives tessellieren
let full_output = egui_ctx.end_frame();
let ime_active = full_output.platform_output.ime.is_some();
if !full_output.platform_output.copied_text.is_empty() {
clipboard
.set_system_text(full_output.platform_output.copied_text.clone());
}
egui_winit_state
.handle_platform_output(&window, full_output.platform_output);
if let Some(requester) = keyboard_requester.as_mut() {
+137 -6
View File
@@ -228,6 +228,8 @@ pub struct AutosaveController {
pending_pages: HashMap<u64, PageData>,
pending_manifest: Option<NotebookManifest>,
retries: HashMap<u64, PageRetry>,
/// Backoff of the manifest after a failed write
manifest_retry: Option<PageRetry>,
last_errors: Vec<String>,
status: AutosaveStatus,
in_flight_count: usize,
@@ -281,6 +283,7 @@ impl AutosaveController {
pending_pages: HashMap::new(),
pending_manifest: None,
retries: HashMap::new(),
manifest_retry: None,
last_errors: Vec::new(),
status: AutosaveStatus::Saved,
in_flight_count: 0,
@@ -428,16 +431,25 @@ impl AutosaveController {
retry.backoff = (retry.backoff * 2).min(Duration::from_secs(30));
}
},
AutosaveResponse::ManifestSaved { result } => {
if let Err((err_msg, failed_manifest)) = result {
AutosaveResponse::ManifestSaved { result } => match result {
Ok(()) => self.manifest_retry = None,
Err((err_msg, failed_manifest)) => {
eprintln!("[Autosave-Fehler] Manifest fehlgeschlagen: {err_msg}");
self.status = AutosaveStatus::Error;
self.last_errors.push(t!("Manifest: {msg}", msg = err_msg));
if self.pending_manifest.is_none() {
self.pending_manifest = Some(*failed_manifest);
}
// Dispatching reset the debounce timers, so without a retry time of
// its own the manifest would wait for the next edit
let retry = self.manifest_retry.get_or_insert(PageRetry {
next_retry: now,
backoff: Duration::from_secs(1),
});
retry.next_retry = now + retry.backoff;
retry.backoff = (retry.backoff * 2).min(Duration::from_secs(30));
}
}
},
}
if self.in_flight_count == 0
@@ -468,7 +480,12 @@ impl AutosaveController {
.get(id)
.map(|r| now >= r.next_retry)
.unwrap_or(false)
});
})
|| (self.pending_manifest.is_some()
&& self
.manifest_retry
.as_ref()
.is_some_and(|r| now >= r.next_retry));
if max_wait_hit || debounce_hit || retry_ready {
self.dispatch_pending();
@@ -515,7 +532,11 @@ impl AutosaveController {
}
}
if let Some(manifest) = self.pending_manifest.take() {
let manifest_ready = self
.manifest_retry
.as_ref()
.is_none_or(|r| now >= r.next_retry);
if let Some(manifest) = self.pending_manifest.take_if(|_| manifest_ready) {
let cmd = AutosaveCommand::SaveManifest {
notebook_dir: self.notebook_dir.clone(),
manifest: manifest.clone(),
@@ -546,6 +567,7 @@ impl AutosaveController {
}
// Backoff für den Flush ignorieren, damit sofort geschrieben wird
self.retries.clear();
self.manifest_retry = None;
if !self.pending_pages.is_empty() || self.pending_manifest.is_some() {
self.dispatch_pending();
@@ -618,11 +640,45 @@ impl AutosaveController {
}
/// Wechselt das Zielverzeichnis für zukünftige Speicheroperationen (z. B. beim Dokumentenwechsel).
///
/// Whatever the flush could not write belongs to the old notebook and must never be
/// written into the new folder, see `abandon_pending`.
pub fn switch_notebook_dir(&mut self, new_notebook_dir: PathBuf) {
let _ = self.flush();
if let Err(err) = self.flush() {
eprintln!(
"[Autosave] Unsaved changes of '{}' dropped on notebook switch: {}",
self.notebook_dir.display(),
err.messages.join(", ")
);
}
self.abandon_pending();
self.notebook_dir = new_notebook_dir;
}
/// Gives up on everything still waiting to be written: it goes into the recovery folder
/// and leaves the queue. For the moment the notebook folder stops being the target
/// (switch to another notebook, notebook moved to the trash).
pub fn abandon_pending(&mut self) {
if !self.pending_pages.is_empty() {
match self.export_recovery(None) {
Ok(files) => {
for file in files {
eprintln!("[Autosave] Recovery copy: {}", file.display());
}
}
Err(e) => eprintln!("[Autosave] Recovery copy failed: {e}"),
}
}
self.pending_pages.clear();
self.pending_manifest = None;
self.retries.clear();
self.manifest_retry = None;
self.last_errors.clear();
self.first_change_time = None;
self.last_change_time = None;
self.status = AutosaveStatus::Saved;
}
/// Beendet den Hintergrund-Worker ordnungsgemäß nach einem Flush.
pub fn shutdown(&mut self) -> Result<(), AutosaveError> {
let res = self.flush();
@@ -739,6 +795,7 @@ pub mod tests {
/// Fake-PageStore zum Simulieren von I/O-Fehlern und Panics.
pub struct FakePageStore {
pub fail_page_writes: Mutex<usize>,
pub fail_manifest_writes: Mutex<usize>,
pub panic_on_page: Mutex<bool>,
pub page_writes: Arc<AtomicUsize>,
pub manifest_writes: Arc<AtomicUsize>,
@@ -748,6 +805,7 @@ pub mod tests {
pub fn new() -> Self {
Self {
fail_page_writes: Mutex::new(0),
fail_manifest_writes: Mutex::new(0),
panic_on_page: Mutex::new(false),
page_writes: Arc::new(AtomicUsize::new(0)),
manifest_writes: Arc::new(AtomicUsize::new(0)),
@@ -782,11 +840,84 @@ pub mod tests {
_dir: &Path,
_manifest: &NotebookManifest,
) -> Result<(), StorageError> {
let mut fails = self.fail_manifest_writes.lock().unwrap();
if *fails > 0 {
*fails -= 1;
return Err(StorageError::InvalidManifest(
"Simulierter Store-Fehler".to_string(),
));
}
self.manifest_writes.fetch_add(1, Ordering::SeqCst);
Ok(())
}
}
#[test]
fn test_switch_drops_pages_the_flush_could_not_write() {
let clock = Arc::new(MockClock::new(Instant::now()));
let store = Arc::new(FakePageStore::new());
*store.fail_page_writes.lock().unwrap() = 1;
let recovery = tempfile::tempdir().unwrap();
std::env::set_var("XDG_DATA_HOME", recovery.path());
let mut controller = AutosaveController::with_options(
PathBuf::from("/tmp/old_nb"),
Duration::from_millis(100),
Duration::from_secs(10),
Arc::clone(&store) as Arc<dyn PageStore>,
Arc::clone(&clock) as Arc<dyn Clock>,
);
let page = Page::new(7, PageBackground::Grid5mm);
let manifest = NotebookManifest::new("old_nb", "Old");
controller.mark_page_dirty(&page, &manifest);
controller.switch_notebook_dir(PathBuf::from("/tmp/new_nb"));
let manifests_before = store.manifest_writes.load(Ordering::SeqCst);
// Nothing of the old notebook may reach the new folder
assert!(controller.flush().is_ok());
assert_eq!(store.page_writes.load(Ordering::SeqCst), 0);
assert_eq!(
store.manifest_writes.load(Ordering::SeqCst),
manifests_before
);
assert_eq!(controller.status(), AutosaveStatus::Saved);
}
#[test]
fn test_failed_manifest_is_retried_without_another_edit() {
let clock = Arc::new(MockClock::new(Instant::now()));
let store = Arc::new(FakePageStore::new());
*store.fail_manifest_writes.lock().unwrap() = 1;
let mut controller = AutosaveController::with_options(
PathBuf::from("/tmp/manifest_retry"),
Duration::from_millis(100),
Duration::from_secs(10),
Arc::clone(&store) as Arc<dyn PageStore>,
Arc::clone(&clock) as Arc<dyn Clock>,
);
let manifest = NotebookManifest::new("nb", "Retry");
controller.mark_manifest_dirty(&manifest);
clock.advance(Duration::from_millis(200));
controller.update();
controller.sync_worker();
assert_eq!(controller.update(), AutosaveStatus::Error);
assert_eq!(store.manifest_writes.load(Ordering::SeqCst), 0);
// Not before the backoff has passed
controller.update();
controller.sync_worker();
controller.update();
assert_eq!(store.manifest_writes.load(Ordering::SeqCst), 0);
clock.advance(Duration::from_secs(2));
controller.update();
controller.sync_worker();
assert_eq!(controller.update(), AutosaveStatus::Saved);
assert_eq!(store.manifest_writes.load(Ordering::SeqCst), 1);
}
#[test]
fn test_debounce_bundles_changes() {
let clock = Arc::new(MockClock::new(Instant::now()));
+29 -2
View File
@@ -1021,8 +1021,13 @@ impl CanvasState {
}
if page.vertex_cache.len() > self.committed_vertex_capacity {
self.committed_vertex_capacity =
(page.vertex_cache.len() * 2).max(Self::INITIAL_COMMITTED_CAPACITY);
let vertex_size = std::mem::size_of::<InkVertex>() as u64;
let max_vertices = (self.device.limits().max_buffer_size / vertex_size) as usize;
self.committed_vertex_capacity = grown_vertex_capacity(
page.vertex_cache.len(),
Self::INITIAL_COMMITTED_CAPACITY,
max_vertices,
);
self.committed_vertex_buffer = self.device.create_buffer(&wgpu::BufferDescriptor {
label: Some("Committed Strokes Vertex Buffer (Reallocated)"),
size: (self.committed_vertex_capacity * std::mem::size_of::<InkVertex>()) as u64,
@@ -1735,10 +1740,32 @@ fn text_resolution(screen_px_per_unit: f32) -> f32 {
2.0_f32.powf(steps / 4.0).clamp(0.125, 8.0)
}
/// Capacity for a vertex buffer that has to hold `needed` vertices: twice as much, so that
/// it is not reallocated with every stroke, but no more than the device allows. Doubling
/// alone asked for a buffer beyond the limit as soon as a very full page used half of it,
/// which ends in a validation panic.
fn grown_vertex_capacity(needed: usize, initial: usize, max_vertices: usize) -> usize {
needed
.saturating_mul(2)
.max(initial)
.min(max_vertices)
.max(needed)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_vertex_capacity_doubles_up_to_the_device_limit() {
assert_eq!(grown_vertex_capacity(10, 1000, 1_000_000), 1000);
assert_eq!(grown_vertex_capacity(5000, 1000, 1_000_000), 10_000);
// More than half the limit: the limit, not twice the need
assert_eq!(grown_vertex_capacity(600_000, 1000, 1_000_000), 1_000_000);
// Never less than what is needed
assert_eq!(grown_vertex_capacity(2_000_000, 1000, 1_000_000), 2_000_000);
}
#[test]
fn test_text_resolution_is_quantized_and_never_below_screen() {
for px in [0.2_f32, 0.27, 0.5, 1.3, 3.9] {
+1981
View File
File diff suppressed because it is too large. Load diff
+97 -1
View File
@@ -87,7 +87,7 @@ fn scan(dir: &Path) -> HashMap<String, Fingerprint> {
}
/// Page id of a regular page file (`page_<id>.json`).
fn page_file_id(name: &str) -> Option<u64> {
pub(crate) fn page_file_id(name: &str) -> Option<u64> {
name.strip_prefix("page_")?
.strip_suffix(".json")?
.parse()
@@ -461,6 +461,102 @@ mod tests {
assert!(outcome.is_empty(), "{outcome:?}");
}
/// Writes the page file the way a sync client does: behind the back of `storage`.
fn write_from_another_device(dir: &Path, page: &Page) {
let bytes = serde_json::to_vec(&PageData::from(page)).unwrap();
fs::write(dir.join(format!("page_{}.json", page.id)), bytes).unwrap();
}
fn conflict_copies(dir: &Path) -> Vec<String> {
fs::read_dir(dir)
.unwrap()
.flatten()
.map(|e| e.file_name().to_string_lossy().into_owned())
.filter(|n| conflict_copy_of(n).is_some())
.collect()
}
#[test]
fn test_saving_over_a_change_from_another_device_keeps_both() {
let dir = tempfile::tempdir().unwrap();
let (mut notebook, mut manifest) = saved_notebook(dir.path());
let now = Instant::now();
let mut watch = DiskWatch::new(dir.path().to_path_buf(), now);
// Their stroke arrives while a stroke is being drawn here, so the check has to wait
let mut theirs = notebook.current_page().clone();
theirs.add_stroke(stroke(5, 300.0));
write_from_another_device(dir.path(), &theirs);
// The stroke is finished and saved before the check ran
notebook.current_page_mut().add_stroke(stroke(2, 200.0));
notebook.next_stroke_id = 3;
storage::save_page(dir.path(), &PageData::from(notebook.current_page())).unwrap();
// Ours is in the page file, theirs next to it
let saved = storage::load_page(dir.path(), 1).unwrap();
let ids: Vec<u64> = saved.strokes.iter().map(|s| s.id).collect();
assert_eq!(ids, vec![1, 2]);
assert_eq!(conflict_copies(dir.path()).len(), 1);
// The check turns their version into a page of its own
let outcome = watch.check(&mut notebook, &mut manifest, now);
assert_eq!(outcome.conflict_pages.len(), 1);
assert_eq!(notebook.pages.len(), 2);
assert_eq!(notebook.pages[0].strokes.len(), 2);
assert_eq!(notebook.pages[1].strokes.len(), 2);
assert!(conflict_copies(dir.path()).is_empty());
// Saving again is an ordinary save
notebook.pages[0].add_stroke(stroke(9, 500.0));
storage::save_page(dir.path(), &PageData::from(&notebook.pages[0])).unwrap();
assert!(conflict_copies(dir.path()).is_empty());
}
#[test]
fn test_saving_an_unchanged_page_keeps_the_change_from_another_device() {
let dir = tempfile::tempdir().unwrap();
let (mut notebook, mut manifest) = saved_notebook(dir.path());
let now = Instant::now();
let mut watch = DiskWatch::new(dir.path().to_path_buf(), now);
let mut theirs = notebook.current_page().clone();
theirs.add_stroke(stroke(5, 300.0));
write_from_another_device(dir.path(), &theirs);
// E.g. turning pages marks the page for saving although nothing changed on it
storage::save_page(dir.path(), &PageData::from(notebook.current_page())).unwrap();
assert!(conflict_copies(dir.path()).is_empty());
let outcome = watch.check(&mut notebook, &mut manifest, now);
assert_eq!(outcome.reloaded_pages, 1);
assert_eq!(notebook.pages.len(), 1);
assert_eq!(notebook.pages[0].strokes.len(), 2);
}
#[test]
fn test_saving_a_new_page_over_one_from_another_device_keeps_both() {
let dir = tempfile::tempdir().unwrap();
let (mut notebook, _) = saved_notebook(dir.path());
// Both devices add a second page; it gets the same id on both
let index = notebook.add_page(PageBackground::Blank);
let mut theirs = notebook.pages[index].clone();
theirs.add_stroke(stroke(7, 300.0));
write_from_another_device(dir.path(), &theirs);
notebook.pages[index].add_stroke(stroke(8, 400.0));
storage::save_page(dir.path(), &PageData::from(&notebook.pages[index])).unwrap();
assert_eq!(conflict_copies(dir.path()).len(), 1);
// A file never read here but with the content to be saved is no conflict
let index = notebook.add_page(PageBackground::Blank);
notebook.pages[index].add_stroke(stroke(9, 300.0));
write_from_another_device(dir.path(), &notebook.pages[index]);
storage::save_page(dir.path(), &PageData::from(&notebook.pages[index])).unwrap();
assert_eq!(conflict_copies(dir.path()).len(), 1);
}
#[test]
fn test_page_still_being_synced_is_tried_again() {
let dir = tempfile::tempdir().unwrap();
+103 -7
View File
@@ -275,7 +275,7 @@ pub enum EraserMode {
/// Nur Textmarker – die Handschrift darunter bleibt erhalten
HighlighterOnly,
/// Nur den überstrichenen Teil eines Strichs entfernen; der Rest bleibt in Stücken
/// erhalten. Formen werden als Ganzes radiert.
/// erhalten. Formen und Textfelder werden als Ganzes radiert.
Partial,
}
@@ -578,6 +578,12 @@ impl Stroke {
min_dist
}
/// A text box goes as a whole once the eraser touches it, like a typed or inserted math
/// result (Issue #54). Images are never erased.
fn erased_as_text(&self, eraser_pos: glam::Vec2, eraser_radius: f32) -> bool {
self.is_text() && self.distance_to_point(eraser_pos) <= eraser_radius
}
/// Prüft, ob dieser Strich von einem Radierer an Position `eraser_pos` mit Radius `eraser_radius` getroffen wird.
pub fn intersects_eraser(
&self,
@@ -685,6 +691,15 @@ impl Stroke {
if let Some(ref mut shape) = self.shape {
shape.scale(origin, factors);
}
// The line width grows with the stroke. It is stored and the mesh is rebuilt from it
// on load; without this a scaled stroke would be back at its old width after a restart.
// Images and text fields have no line width.
if !self.is_object() {
let factor = (factors.x * factors.y).abs().sqrt();
if factor.is_finite() && factor > 0.0 {
self.width *= factor;
}
}
self.recompute_aabb();
}
@@ -942,10 +957,15 @@ impl Page {
let mut hit_indices = Vec::new();
for (i, stroke) in self.strokes.iter().enumerate() {
if !stroke.is_object()
&& mode.erases(stroke.tool)
&& stroke.intersects_eraser(eraser_pos, eraser_radius, max_stroke_radius)
{
let hit = if stroke.is_text() {
mode != EraserMode::HighlighterOnly
&& stroke.erased_as_text(eraser_pos, eraser_radius)
} else {
!stroke.is_object()
&& mode.erases(stroke.tool)
&& stroke.intersects_eraser(eraser_pos, eraser_radius, max_stroke_radius)
};
if hit {
hit_indices.push(i);
}
}
@@ -970,7 +990,7 @@ impl Page {
/// Teilradierer: entfernt den Teil aller Striche im Kreis um `eraser_pos`. Getroffene
/// Striche werden durch ihre übrig gebliebenen Stücke ersetzt (neue IDs aus `next_id`),
/// Formen als Ganzes entfernt, Bilder und Textfelder nie. Aufeinanderfolgende Schnitte
/// Formen und Textfelder als Ganzes entfernt, Bilder nie. Aufeinanderfolgende Schnitte
/// eines Radierzugs bilden einen gemeinsamen Undo-Schritt. Gibt `true` zurück, wenn sich etwas geändert hat.
pub fn erase_partial_at(
&mut self,
@@ -982,6 +1002,14 @@ impl Page {
let mut added = Vec::new();
let mut result = Vec::with_capacity(self.strokes.len());
for stroke in std::mem::take(&mut self.strokes) {
if stroke.is_text() {
if stroke.erased_as_text(eraser_pos, eraser_radius) {
removed.push(stroke);
} else {
result.push(stroke);
}
continue;
}
let reach = eraser_radius + stroke.width / 2.0;
if stroke.is_object() || !stroke.aabb.expanded(reach).contains_point(eraser_pos) {
result.push(stroke);
@@ -1555,7 +1583,12 @@ impl Notebook {
/// der Seite und gibt die Kennungen der geänderten Seiten zurück (`None`: nichts zu tun).
pub fn make_space(&mut self, page_index: usize, y: f32, dy: f32) -> Option<Vec<u64>> {
let run = self.space_run(page_index)?;
if self.pages[run.clone()].iter().any(|p| !p.is_loaded) {
// A page that could not be loaded is never saved: strokes pushed onto it would be gone
// after a restart, and its own content is unknown
if self.pages[run.clone()]
.iter()
.any(|p| !p.is_loaded || p.is_readonly())
{
return None;
}
let dy = if dy < 0.0 {
@@ -2177,6 +2210,57 @@ mod tests {
assert!(page.strokes.is_empty());
}
#[test]
fn test_eraser_removes_text_boxes_but_not_images() {
let corners = |x: f32| {
[
glam::Vec2::new(x, 100.0),
glam::Vec2::new(x + 100.0, 100.0),
glam::Vec2::new(x + 100.0, 150.0),
glam::Vec2::new(x, 150.0),
]
};
let text = || {
Stroke::new_text(
1,
crate::text::TextBox::new("10".into(), 40.0, crate::text::TextFont::Sans, 100.0),
corners(100.0),
[0.0, 0.0, 0.0, 1.0],
)
};
for mode in [EraserMode::All, EraserMode::Partial] {
let mut page = Page::new(1, PageBackground::Grid5mm);
page.add_stroke(text());
page.add_stroke(Stroke::new_image(2, "a".into(), corners(300.0)));
let mut next_id = 10;
let mut erase = |page: &mut Page, at: glam::Vec2| match mode {
EraserMode::Partial => page.erase_partial_at(at, 10.0, &mut next_id),
_ => !page.erase_at(at, 10.0, 2.0, mode).is_empty(),
};
// Next to the box: nothing; touching its edge or inside: the whole box goes
assert!(!erase(&mut page, glam::Vec2::new(85.0, 120.0)));
assert!(!erase(&mut page, glam::Vec2::new(350.0, 120.0)));
assert!(erase(&mut page, glam::Vec2::new(95.0, 120.0)));
assert_eq!(
page.strokes.iter().map(|s| s.id).collect::<Vec<_>>(),
vec![2]
);
assert!(page.undo());
assert_eq!(page.strokes.len(), 2);
}
// The highlighter eraser leaves text alone
let mut page = Page::new(1, PageBackground::Grid5mm);
page.add_stroke(text());
assert!(page
.erase_at(
glam::Vec2::new(150.0, 120.0),
10.0,
2.0,
EraserMode::HighlighterOnly
)
.is_empty());
}
#[test]
fn test_pdf_page_size_follows_orientation() {
// Hochformat (A4, Letter) bleibt A4 hoch
@@ -2610,6 +2694,18 @@ mod tests {
);
}
#[test]
fn test_make_space_refuses_pages_that_failed_to_load() {
let mut nb = Notebook::new();
nb.add_page(PageBackground::Blank);
nb.pages[0].strokes = vec![block(1, 1000.0, 100.0)];
nb.pages[1].persist = PersistState::ReadOnlyAfterLoadError;
assert!(nb.make_space(0, 900.0, 300.0).is_none());
assert_eq!(top_of(&nb, 0, 1), Some(1000.0));
nb.pages[1].persist = PersistState::Normal;
assert!(nb.make_space(0, 900.0, 300.0).is_some());
}
#[test]
fn test_remove_and_restore_current_page() {
let mut nb = Notebook::new();
+27 -4
View File
@@ -553,7 +553,7 @@ fn render_ink_layer(
));
}
if let Some(ref shape) = stroke.shape {
render_recognized_shape(layer, shape, stroke.color);
render_recognized_shape(layer, shape, stroke.color, stroke.width);
} else {
match strategy {
ExportStrategy::FilledPolygon => {
@@ -831,20 +831,31 @@ fn render_pdf_raster(
draw_image_xobject(layer, xobject, &pdf.corners(page.size));
}
/// Line width of a shape in PDF points, as wide as its mesh on screen
/// (`geometry::shape_to_mesh`: the stroke width in page units, at least 2). Shapes without a
/// stored width get the former fixed 1.5 pt (about 0.5 mm).
fn shape_outline_pt(width_units: f32) -> f32 {
if width_units <= 0.0 || !width_units.is_finite() {
return 1.5;
}
width_units.max(2.0) * (A4_WIDTH_MM / PAGE_WIDTH) * (72.0 / 25.4)
}
/// Rendert eine mathematisch bereinigte Form (Gerade, Kreis, Rechteck, Koordinatensystem)
/// als verlustfreien Vektorpfad im PDF.
fn render_recognized_shape(
layer: &crate::pdf_writer::PdfLayerReference,
shape: &crate::geometry::RecognizedShape,
color: [f32; 4],
width_units: f32,
) {
use crate::geometry::RecognizedShape;
let stroke_color = Color::Rgb(Rgb::new(color[0], color[1], color[2], None));
layer.set_outline_color(stroke_color);
layer.set_outline_thickness(1.5); // ca. 0.5 mm Standard-Liniendicke
// Wie auf dem Bildschirm: spitze Ecken und stumpfe Enden (der Grafikzustand kann noch
// runde Verbindungen von einem vorherigen Freihandstrich enthalten)
layer.set_outline_thickness(shape_outline_pt(width_units));
// Wie auf dem Bildschirm: spitze Ecken und stumpfe Enden (der Grafikzustand kann noch
// runde Verbindungen von einem vorherigen Freihandstrich enthalten)
layer.set_line_cap_style(crate::pdf_writer::LineCapStyle::Butt);
layer.set_line_join_style(crate::pdf_writer::LineJoinStyle::Miter);
@@ -1588,6 +1599,18 @@ mod tests {
assert!(bytes.starts_with(b"%PDF-1."));
}
#[test]
fn test_shape_outline_follows_stroke_width() {
let mm = |pt: f32| pt * 25.4 / 72.0;
// 5 page units are 0.5 mm, a 100 unit highlighter line is 10 mm wide
assert!((mm(shape_outline_pt(5.0)) - 0.5).abs() < 1e-3);
assert!((mm(shape_outline_pt(100.0)) - 10.0).abs() < 1e-3);
// The mesh is never thinner than 2 units
assert!((mm(shape_outline_pt(0.5)) - 0.2).abs() < 1e-3);
assert_eq!(shape_outline_pt(0.0), 1.5);
assert_eq!(shape_outline_pt(f32::NAN), 1.5);
}
#[test]
fn test_image_matrix_maps_unit_square_to_corners() {
// Bild über die volle Seitenbreite, oberes Viertel der Seite
+2
View File
@@ -172,6 +172,8 @@ mod tests {
("rnote.rs", include_str!("rnote.rs")),
("saber.rs", include_str!("saber.rs")),
("xopp.rs", include_str!("xopp.rs")),
("nextcloud.rs", include_str!("nextcloud.rs")),
("cloud_sync.rs", include_str!("cloud_sync.rs")),
];
/// Alle Texte in `t!("…")` und `tn!("…")` eines Quelltexts, auch über mehrere Zeilen
+61
View File
@@ -13,6 +13,7 @@ pub const EN: &[(&str, &str)] = &[
("1 Objekt | [R] Drehen [Entf] Löschen", "1 object | [R] Rotate [Del] Delete"),
("Abbrechen", "Cancel"),
("Abfrage fehlgeschlagen:", "Request failed:"),
("Abgleich beenden und das App-Passwort widerrufen; die Notizbücher bleiben auf diesem Gerät und auf dem Server", "Stop syncing and revoke the app password; the notebooks stay on this device and on the server"),
("Achsenkreuz", "Axis cross"),
("Achteck (Oktagon)", "Octagon"),
("Aktuelle Version:", "Current version:"),
@@ -20,6 +21,9 @@ pub const EN: &[(&str, &str)] = &[
("Alle", "All"),
("Alles gespeichert", "All saved"),
("Als leere Seite bearbeiten (Schreibschutz aufheben)", "Edit as blank page (remove write protection)"),
("Anmeldung abgelaufen – bitte erneut verbinden", "Sign-in expired – please connect again"),
("Anmeldung abgelehnt – bitte neu verbinden", "Sign-in rejected – please connect again"),
("Anmeldung im Browser; die App bekommt ein eigenes App-Passwort, das sich in Nextcloud jederzeit widerrufen lässt", "Sign in in the browser; the app gets an app password of its own, which can be revoked in Nextcloud at any time"),
("AppImage (Portabel)", "AppImage (portable)"),
("Asset-Name darf nicht leer sein.", "Asset name must not be empty."),
("Auch per Strg+V aus der Zwischenablage oder durch Ziehen einer Bilddatei ins Fenster", "Also with Ctrl+V from the clipboard or by dragging an image file into the window"),
@@ -44,6 +48,8 @@ pub const EN: &[(&str, &str)] = &[
("Bild mit transparentem Hintergrund, nur Inhalt", "Image with transparent background, content only"),
("Bild nicht eingefügt: {err}", "Image not inserted: {err}"),
("Bild, Text oder kopierte Objekte aus der Zwischenablage hier einfügen", "Paste an image, text or copied objects from the clipboard here"),
("Bitte die Adresse des Nextcloud-Servers eingeben", "Please enter the address of the Nextcloud server"),
("Bitte im Browser anmelden und den Zugriff erlauben …", "Please sign in in the browser and grant access …"),
("Blanko", "Blank"),
("Blau", "Blue"),
("Code", "Code"),
@@ -56,15 +62,34 @@ pub const EN: &[(&str, &str)] = &[
("Den Ordner gibt es nicht mehr", "The folder no longer exists"),
("Den Ordner „{name}“ gibt es hier schon", "A folder “{name}” already exists here"),
("Den Zielordner gibt es nicht mehr", "The target folder no longer exists"),
("Der Ordner „ink-notes“ auf dem Server fehlt. Zur Sicherheit wird nichts gelöscht – zum erneuten Hochladen die Verbindung trennen und neu verbinden.", "The folder “ink-notes” is missing on the server. To be safe, nothing is deleted – to upload again, disconnect and connect again."),
("Der Ordnername ist leer", "The folder name is empty"),
("Der Ordnername ist zu lang (höchstens {max} Zeichen)", "The folder name is too long (at most {max} characters)"),
("Der Server leitet nach {to} weiter – bitte diese Adresse verwenden", "The server redirects to {to} – please use that address"),
("Der Speicherort {dir} fehlt. Zur Sicherheit wird nichts gelöscht.", "The storage location {dir} is missing. To be safe, nothing is deleted."),
("Der übergeordnete Ordner existiert nicht mehr", "The parent folder no longer exists"),
("DICKEN-PRESETS", "WIDTH PRESETS"),
("Die Adresse muss mit https:// beginnen", "The address must start with https://"),
("Die beschädigte Originaldatei wurde in Quarantäne gesichert:", "The damaged original file was saved to quarantine:"),
("Die Datei ist kein lesbares PDF", "The file is not a readable PDF"),
("Die einzige Seite eines Notizbuchs kann nicht gelöscht werden", "The only page of a notebook cannot be deleted"),
("Die Flatpak-Version wird über die Software-Verwaltung aktualisiert.", "The Flatpak version is updated through the software manager."),
("Die heruntergeladene Datei existiert nicht mehr.", "The downloaded file no longer exists."),
("Inzwischen geändert", "Changed meanwhile"),
("Kein Datenordner", "No data folder"),
("NEXTCLOUD", "NEXTCLOUD"),
("Nextcloud getrennt – die Notizbücher bleiben auf diesem Gerät", "Nextcloud disconnected – the notebooks stay on this device"),
("Nicht gefunden", "Not found"),
("Server nicht erreichbar: {msg}", "Server not reachable: {msg}"),
("Server-Fehler {code}", "Server error {code}"),
("Sofort mit dem Server abgleichen", "Sync with the server right away"),
("Trennen", "Disconnect"),
("Unerwartete Antwort des Servers", "Unexpected answer from the server"),
("Ungültige Adresse: {e}", "Invalid address: {e}"),
("Unter dieser Adresse antwortet kein Nextcloud-Server", "No Nextcloud server answers at this address"),
("Vertrauen", "Trust"),
("Zertifikat nicht vertrauenswürdig (SHA-256 {fingerprint})", "Certificate not trusted (SHA-256 {fingerprint})"),
("Zugang nicht gespeichert: {err}", "Sign-in not saved: {err}"),
("die oberste Ebene", "the top level"),
("Die Zwischenablage ist leer", "The clipboard is empty"),
("Diese Version wird über die Software-Verwaltung aktualisiert (z. B. GNOME Software oder „flatpak update“).", "This version is updated through the software manager (e.g. GNOME Software or “flatpak update”)."),
@@ -83,6 +108,10 @@ pub const EN: &[(&str, &str)] = &[
("Dunkelgrau", "Dark gray"),
("Dunkle Stiftfarben aufhellen", "Lighten dark pen colors"),
("Eigene Farbe wählen", "Choose a custom color"),
("Meine Farbe", "My color"),
("➕ Farbe merken", "➕ Save color"),
("Diese Farbe aus „Meine Farben“ entfernen", "Remove this color from “My colors”"),
("Die aktuelle Farbe zu „Meine Farben“ hinzufügen", "Add the current color to “My colors”"),
("Ein Ordner kann nicht in sich selbst verschoben werden", "A folder cannot be moved into itself"),
("Ein-/Ausgabefehler: {e}", "I/O error: {e}"),
("Eine Fläche ohne Rand: in alle Richtungen schreiben, verschieben und zoomen. Wechsel nur bei einer einzigen Seite ohne PDF bzw. zurück, solange alles im Startausschnitt liegt.", "One surface without edges: write, pan and zoom in every direction. Switching only works for a single page without a PDF, and back only while everything lies in the starting area."),
@@ -104,9 +133,14 @@ pub const EN: &[(&str, &str)] = &[
("Fehler beim Schreiben auf die Festplatte: {e}", "Error writing to disk: {e}"),
("Fehler beim Umbenennen: {err}", "Error while renaming: {err}"),
("Fehler beim Wiederherstellen: {err}", "Error while restoring: {err}"),
(
"Das PDF kann nicht verarbeitet werden (interner Fehler)",
"The PDF cannot be processed (internal error)",
),
("Fehler beim Öffnen: {err}", "Error while opening: {err}"),
("Fehler: {err}", "Error: {err}"),
("Fehlermeldung: {message}", "Error message: {message}"),
("Formeln nach „=“ ausrechnen", "Compute formulas after “=”"),
("Formen (Shape)", "Shapes"),
("Formen: Striche werden zu Linien, Rechtecken, Kreisen … (S/F)", "Shapes: strokes become lines, rectangles, circles … (S/F)"),
("Fünfeck (Pentagon)", "Pentagon"),
@@ -123,6 +157,7 @@ pub const EN: &[(&str, &str)] = &[
("Graphit-Grau", "Graphite gray"),
("Größere Schrift (jetzt {size} pt)", "Larger font (now {size} pt)"),
("Grün", "Green"),
("gerade eben", "just now"),
("hart", "hard"),
("Hell", "Light"),
("Hellgrün", "Light green"),
@@ -144,6 +179,10 @@ pub const EN: &[(&str, &str)] = &[
("Kariert", "Grid"),
("KAROS, LINIEN UND PUNKTE", "GRID, LINES AND DOTS"),
("kein Asset-Ordner", "no asset folder"),
(
"'{asset_name}' ist keine neuere Version und wird nicht installiert.",
"'{asset_name}' is not a newer version and will not be installed.",
),
("Kein Eintrag für '{asset_name}' in SHA256SUMS.txt gefunden. Heruntergeladene Datei wurde gelöscht.", "No entry for '{asset_name}' found in SHA256SUMS.txt. The downloaded file was deleted."),
("Kein Release ausgewählt. Download abgelehnt.", "No release selected. Download refused."),
("Keine manifest.json vorhanden", "No manifest.json present"),
@@ -186,6 +225,7 @@ pub const EN: &[(&str, &str)] = &[
("Mindestdruck", "Minimum pressure"),
("Mit dem Finger zeichnen", "Draw with finger"),
("Mittel", "Medium"),
("Nach einem handgeschriebenen „=“ erscheint das Ergebnis daneben, z. B. 2⁴ = 16. Antippen mit dem Stift oder Klicken fügt es als Text ein.\nGelesen werden Ziffern, + − · : /, Klammern, Hochzahlen, Brüche und x, y, a, b.", "After a handwritten “=” the result appears next to it, e.g. 2⁴ = 16. Tapping it with the pen or clicking inserts it as text.\nDigits, + − · : /, parentheses, exponents, fractions and x, y, a, b are read."),
("Name …", "Name …"),
("Neon-Gelb", "Neon yellow"),
("Netzwerk-Transportfehler: {t}", "Network transport error: {t}"),
@@ -364,6 +404,8 @@ pub const EN: &[(&str, &str)] = &[
("Verzeichnis '{path}' enthält kein manifest.json", "Folder '{path}' contains no manifest.json"),
("Verzeichnis '{path}' existiert nicht", "Folder '{path}' does not exist"),
("Vorherige Seite (Bild auf)", "Previous page (Page Up)"),
("vor {hours} h", "{hours} h ago"),
("vor {minutes} min", "{minutes} min ago"),
("weich", "soft"),
("Weich: schon leichter Druck ergibt kräftige Linien.\nHart: erst fester Druck ergibt kräftige Linien.", "Soft: even light pressure gives bold lines.\nHard: only firm pressure gives bold lines."),
("Weitere Funktionen – Update verfügbar", "More – update available"),
@@ -383,6 +425,7 @@ pub const EN: &[(&str, &str)] = &[
("Zugriffsfehler (I/O). Die Datei existiert, kann aber nicht gelesen werden.\nDie Seite bleibt schreibgeschützt, um ein Überschreiben zu verhindern.", "Access error (I/O). The file exists but cannot be read.\nThe page stays read-only to prevent overwriting it."),
("Zurück zu Seiten geht nur, solange alles im Startausschnitt liegt", "Back to pages only works while everything lies in the starting area"),
("Zurücksetzen (Strg+0)", "Reset (Ctrl+0)"),
("z. B. cloud.example.org", "e.g. cloud.example.org"),
("{count} Objekt(e) eingefügt", "{count} object(s) pasted"),
("{count} Objekt(e) gelöscht", "{count} object(s) deleted"),
("{count} Objekt(e) kopiert – auch als Bild für andere Programme", "{count} object(s) copied – also as an image for other apps"),
@@ -403,21 +446,28 @@ pub const EN: &[(&str, &str)] = &[
("„{title}“ verschieben nach:", "Move “{title}” to:"),
("↩ Rückgängig", "↩ Undo"),
("⏳ Update lädt … {percent} %", "⏳ Downloading update … {percent} %"),
("⏳ Verbinde …", "⏳ Connecting …"),
("⏷ {mm} mm Platz", "⏷ {mm} mm space"),
("♾ Endlos", "♾ Endless"),
("⚙ Erweiterte Server-Einstellungen", "⚙ Advanced server settings"),
("⚠ Beschädigt", "⚠ Damaged"),
("⚠ Das Zertifikat von {server} ist von keiner bekannten Stelle signiert. Nur vertrauen, wenn dieser Fingerabdruck mit dem des Servers übereinstimmt:", "⚠ The certificate of {server} isn't signed by a known authority. Only trust it if this fingerprint matches the server's:"),
("⚠ Datenintegritäts-Warnung", "⚠ Data integrity warning"),
("⚠ In anderer Instanz geöffnet (Schreibschutz)", "⚠ Opened in another instance (read-only)"),
("⚠ Ohne https ist das App-Passwort im Netz lesbar – nur im eigenen Heimnetz verwenden", "⚠ Without https the app password can be read on the network – only use this in your own home network"),
("⚠ Ungespeicherte Änderungen", "⚠ Unsaved changes"),
("⚠ Unverschlüsselt (http)", "⚠ Unencrypted (http)"),
("⚠\u{fe0f} {name} (Beschädigt - kein Manifest)", "⚠\u{fe0f} {name} (damaged - no manifest)"),
("⚠\u{fe0f} {name} (Beschädigt)", "⚠\u{fe0f} {name} (damaged)"),
("⚪ Offline – neuer Versuch in Kürze ({msg})", "⚪ Offline – trying again shortly ({msg})"),
("⚫ Aktiv", "⚫ Active"),
("✔ Synchronisiert {when}", "✔ Synced {when}"),
("✔ Update bereit", "✔ Update ready"),
("➕ Neue Seite einfügen", "➕ Insert new page"),
("➕ Notizbuch", "➕ Notebook"),
("⟲ Gelöschte Seite {number} wiederherstellen", "⟲ Restore deleted page {number}"),
("⬇ Downloads", "⬇ Downloads"),
("🌐 Browser öffnen", "🌐 Open browser"),
("🌐 Release auf Gitea öffnen", "🌐 Open release on Gitea"),
("🏠 Persönlicher Ordner", "🏠 Home folder"),
("💾 Einstellungen speichern", "💾 Save settings"),
@@ -433,10 +483,13 @@ pub const EN: &[(&str, &str)] = &[
("📚 Oberste Ebene", "📚 Top level"),
("📜 Durchgehend", "📜 Continuous"),
("📦 Paket mit Software-Center öffnen / installieren", "📦 Open / install package with the software center"),
("🔄 Jetzt", "🔄 Now"),
("🔄 Suche nach Updates …", "🔄 Checking for updates …"),
("🔄 Synchronisiere …", "🔄 Syncing …"),
("🔄 Updates & Versionen", "🔄 Updates & versions"),
("🔄 Backup", "🔄 Backup"),
("🔄 Nach Updates suchen", "🔄 Check for updates"),
("🔗 Verbinden …", "🔗 Connect …"),
("🖥 Schreibtisch", "🖥 Desktop"),
("🖼 Bild aus Datei …", "🖼 Image from file …"),
("🖼 Bild einfügen … (Strg+I)", "🖼 Insert image … (Ctrl+I)"),
@@ -449,6 +502,14 @@ pub const EN: &[(&str, &str)] = &[
("🚀 Updates & Versionen", "🚀 Updates & versions"),
(", PDF-Hintergrund nicht gefunden: {file}", ", PDF background not found: {file}"),
(", {count} Elemente übersprungen", ", {count} elements skipped"),
(
"Platz schaffen nicht möglich: Eine der Seiten konnte nicht geladen werden",
"Cannot make space: one of the pages could not be loaded",
),
(
"Nicht gewechselt, Speichern fehlgeschlagen: {err}",
"Not switched, saving failed: {err}",
),
("Nicht importiert: {err}", "Not imported: {err}"),
("Importieren", "Import"),
("📄 Importieren", "📄 Import"),
+74 -4
View File
@@ -125,11 +125,24 @@ pub fn page(
/// we draw a diameter of `width · (0.5 + p)`, so the widest point gets p = 1 and thinner
/// ones less (down to a third of the widest).
pub fn fit_widths(widths: &[f32]) -> (f32, Vec<f32>) {
let widest = widths.iter().copied().fold(0.0, f32::max).max(0.01);
let widest = widths
.iter()
.copied()
.filter(|w| w.is_finite())
.fold(0.0, f32::max)
.max(0.01);
let width = widest / 1.5;
let pressures = widths
.iter()
.map(|w| (w / width - 0.5).clamp(0.0, 1.0))
.map(|w| {
let p = w / width - 0.5;
// NaN survives `clamp`, is stored as `null` and makes the page unreadable
if p.is_finite() {
p.clamp(0.0, 1.0)
} else {
0.5
}
})
.collect();
(width, pressures)
}
@@ -153,17 +166,29 @@ pub fn ink_stroke(
color[3] = 1.0;
StrokeTool::Pen
};
// Broken files may carry NaN or infinite numbers. They would be written as `null` and
// the page could not be read again, so such points are left out.
let samples = points
.iter()
.zip(pressures)
.filter(|(p, _)| p.is_finite())
.enumerate()
.map(|(i, (p, &pressure))| SampleData {
x: p.x,
y: p.y,
p: pressure,
p: if pressure.is_finite() {
pressure.clamp(0.0, 1.0)
} else {
0.5
},
t: i as f64 * 0.005,
})
.collect();
let width = if width.is_finite() && width > 0.0 {
width
} else {
5.0
};
StrokeData {
id,
color,
@@ -246,7 +271,12 @@ pub fn unpremultiply(pixels: &mut [u8]) {
/// Rasterizes an SVG for a box of `size` page units (about 250 dpi).
pub fn rasterize_svg(svg: &str, size: glam::Vec2) -> Option<(u32, u32, Vec<u8>)> {
use resvg::{tiny_skia, usvg};
let tree = usvg::Tree::from_str(svg, &usvg::Options::default()).ok()?;
// By default usvg loads `<image href="/some/local/file">` from disk. An imported note
// must not be able to pull files of this device into the notebook; embedded data URLs
// still work.
let mut options = usvg::Options::default();
options.image_href_resolver.resolve_string = Box::new(|_, _| None);
let tree = usvg::Tree::from_str(svg, &options).ok()?;
let max = crate::assets::MAX_IMPORT_DIMENSION as f32;
let px_per_unit = 250.0 / 254.0;
let width = (size.x * px_per_unit).round().clamp(1.0, max) as u32;
@@ -288,4 +318,44 @@ mod tests {
// Thinner than a third of the widest point is not possible
assert!((drawn[3] - 10.0).abs() < 1e-3);
}
#[test]
fn test_broken_numbers_never_reach_the_page_file() {
let (width, pressures) = fit_widths(&[f32::NAN, 10.0, f32::INFINITY]);
assert!(width.is_finite() && pressures.iter().all(|p| p.is_finite()));
let points = [
glam::Vec2::new(10.0, 10.0),
glam::Vec2::new(f32::NAN, 20.0),
glam::Vec2::new(30.0, 30.0),
];
let stroke = ink_stroke(
1,
[0.0, 0.0, 0.0, 1.0],
false,
&points,
f32::NAN,
&[f32::NAN, 0.5, 2.0],
);
assert_eq!(stroke.samples.len(), 2);
assert!(stroke.width.is_finite());
let json = serde_json::to_string(&stroke).unwrap();
assert!(!json.contains("null"), "{json}");
assert!(serde_json::from_str::<StrokeData>(&json).is_ok());
}
#[test]
fn test_svg_cannot_embed_local_files() {
let dir = tempfile::tempdir().unwrap();
let secret = dir.path().join("secret.png");
image::RgbaImage::from_pixel(8, 8, image::Rgba([255, 0, 0, 255]))
.save(&secret)
.unwrap();
let svg = format!(
r#"<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="8" height="8"><image width="8" height="8" xlink:href="{}"/></svg>"#,
secret.display()
);
let (_, _, pixels) = rasterize_svg(&svg, glam::Vec2::new(80.0, 80.0)).unwrap();
assert!(pixels.iter().all(|&b| b == 0), "the local image was drawn");
}
}
+5
View File
@@ -7,6 +7,7 @@ pub mod assets;
pub mod autosave;
pub mod background;
pub mod canvas;
pub mod cloud_sync;
pub mod disk_watch;
pub mod document;
pub mod export;
@@ -18,6 +19,10 @@ pub mod i18n;
pub mod image_layer;
pub mod import;
pub mod library;
pub mod math;
pub mod math_assist;
pub mod math_ink;
pub mod nextcloud;
pub mod osk;
pub mod page_export;
pub mod paths;
+757
View File
@@ -0,0 +1,757 @@
//! Exact evaluation of handwritten math (Issue #46, stage 1).
//!
//! Takes the text of a recognised expression such as `2^4`, `2,5 · 3` or `(x + 3)²`
//! (optionally ending with `=`) and returns its result as text: `16`, `7,5` and
//! `x² + 6x + 9`. Numbers are exact fractions, so `1 : 3 · 3` gives `1`, and expressions
//! with variables are expanded into polynomials.
//!
//! The input is lenient towards how math is written by hand: decimal comma or point,
//! `·`, `×`, `*` and `:`, `÷`, `/`, superscript exponents, Unicode minus signs and implicit
//! multiplication (`2x`, `3(x + 1)`, `(a + b)(a − b)`).
use std::cmp::Ordering;
use std::collections::BTreeMap;
use std::fmt::Write as _;
/// Largest exponent for a polynomial that is not a plain number
const MAX_POLYNOMIAL_EXPONENT: u32 = 32;
/// Largest exponent for a plain number
const MAX_NUMBER_EXPONENT: u32 = 1000;
/// Results with more terms are not shown (they would not fit next to the `=` anyway)
const MAX_TERMS: usize = 64;
/// Terminating decimals with more digits after the separator are shown as fractions
const MAX_DECIMALS: u32 = 12;
/// Computes the result of `expression`, written for display after its `=` sign.
///
/// `decimal_comma` selects the decimal separator of the result. Returns `None` if the
/// expression cannot be read or computed (e.g. division by zero, a variable in a
/// denominator or overflow), and also if there is nothing to compute, i.e. the result would
/// just repeat the input (`5`, `x`, `2x`).
pub fn solve(expression: &str, decimal_comma: bool) -> Option<String> {
let tokens = tokenize(expression)?;
let ast = Parser::new(&tokens).parse()?;
if matches!(ast, Expr::Number(_) | Expr::Variable(_)) {
return None;
}
let result = ast.eval()?;
if result.terms.len() > MAX_TERMS {
return None;
}
let text = result.format(decimal_comma);
let compact = |s: &str| -> String {
s.chars()
.filter(|c| !c.is_whitespace())
.map(|c| if c == '−' { '-' } else { c })
.collect()
};
if compact(&text) == compact(expression.trim().trim_end_matches('=')) {
return None;
}
Some(text)
}
/// `expression` written neatly for showing it, e.g. `2⁴` for `2^(4)`, `(x + 1)/2` for
/// `((x+1)/(2))` or `2,5 · 3` for `2,5*3`. `None` if it cannot be read.
pub fn display(expression: &str, decimal_comma: bool) -> Option<String> {
let tokens = tokenize(expression)?;
let ast = Parser::new(&tokens).parse()?;
Some(ast.display(decimal_comma))
}
// --- Rational numbers ---------------------------------------------------------------------
/// Exact fraction `num / den` with `den > 0`, always reduced
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct Rational {
num: i128,
den: i128,
}
fn gcd(mut a: i128, mut b: i128) -> i128 {
a = a.abs();
b = b.abs();
while b != 0 {
(a, b) = (b, a % b);
}
a
}
impl Rational {
const ZERO: Rational = Rational { num: 0, den: 1 };
const ONE: Rational = Rational { num: 1, den: 1 };
fn new(num: i128, den: i128) -> Option<Rational> {
// i128::MIN has no positive counterpart, which `gcd` and `neg` need
if den == 0 || num == i128::MIN || den == i128::MIN {
return None;
}
let g = gcd(num, den).max(1);
let (mut num, mut den) = (num / g, den / g);
if den < 0 {
num = num.checked_neg()?;
den = den.checked_neg()?;
}
Some(Rational { num, den })
}
fn integer(n: i128) -> Rational {
Rational { num: n, den: 1 }
}
fn is_zero(self) -> bool {
self.num == 0
}
fn add(self, other: Rational) -> Option<Rational> {
let g = gcd(self.den, other.den);
let den = (self.den / g).checked_mul(other.den)?;
let num = self
.num
.checked_mul(other.den / g)?
.checked_add(other.num.checked_mul(self.den / g)?)?;
Rational::new(num, den)
}
fn neg(self) -> Option<Rational> {
Some(Rational {
num: self.num.checked_neg()?,
den: self.den,
})
}
fn mul(self, other: Rational) -> Option<Rational> {
// Cross-reducing first keeps the intermediate products small
let g1 = gcd(self.num, other.den).max(1);
let g2 = gcd(other.num, self.den).max(1);
let num = (self.num / g1).checked_mul(other.num / g2)?;
let den = (self.den / g2).checked_mul(other.den / g1)?;
Rational::new(num, den)
}
fn recip(self) -> Option<Rational> {
Rational::new(self.den, self.num)
}
fn pow(self, exponent: i128) -> Option<Rational> {
if exponent.unsigned_abs() > MAX_NUMBER_EXPONENT as u128 {
return None;
}
let base = if exponent < 0 { self.recip()? } else { self };
let mut result = Rational::ONE;
for _ in 0..exponent.unsigned_abs() {
result = result.mul(base)?;
}
Some(result)
}
fn as_integer(self) -> Option<i128> {
(self.den == 1).then_some(self.num)
}
/// The number without its sign: as a terminating decimal if it has one with at most
/// [`MAX_DECIMALS`] digits, otherwise as a fraction `p/q`.
fn format_abs(self, decimal_comma: bool) -> String {
let num = self.num.unsigned_abs();
let den = self.den.unsigned_abs();
let whole = num / den;
let mut rest = num % den;
if rest == 0 {
return whole.to_string();
}
let mut reduced = den;
let mut decimals = 0;
for factor in [2, 5] {
while reduced.is_multiple_of(factor) {
reduced /= factor;
}
}
if reduced == 1 {
let mut digits = String::new();
while rest != 0 && decimals <= MAX_DECIMALS {
let Some(shifted) = rest.checked_mul(10) else {
break;
};
rest = shifted;
digits.push(char::from(b'0' + (rest / den) as u8));
rest %= den;
decimals += 1;
}
if rest == 0 {
let separator = if decimal_comma { ',' } else { '.' };
return format!("{whole}{separator}{digits}");
}
}
format!("{num}/{den}")
}
}
// --- Polynomials --------------------------------------------------------------------------
/// Product of variables with their exponents, e.g. `a²b` = `{a: 2, b: 1}`
type Monomial = BTreeMap<char, u32>;
/// Sum of monomials with exact coefficients; a plain number is the empty monomial
#[derive(Debug, Clone, PartialEq, Eq)]
struct Polynomial {
terms: BTreeMap<Monomial, Rational>,
}
impl Polynomial {
fn constant(value: Rational) -> Polynomial {
let mut terms = BTreeMap::new();
if !value.is_zero() {
terms.insert(Monomial::new(), value);
}
Polynomial { terms }
}
fn variable(name: char) -> Polynomial {
let mut terms = BTreeMap::new();
terms.insert(Monomial::from([(name, 1)]), Rational::ONE);
Polynomial { terms }
}
/// The value if this is a plain number
fn as_constant(&self) -> Option<Rational> {
match self.terms.len() {
0 => Some(Rational::ZERO),
1 => self.terms.get(&Monomial::new()).copied(),
_ => None,
}
}
fn add_term(&mut self, monomial: Monomial, coefficient: Rational) -> Option<()> {
let sum = match self.terms.get(&monomial) {
Some(existing) => existing.add(coefficient)?,
None => coefficient,
};
if sum.is_zero() {
self.terms.remove(&monomial);
} else {
self.terms.insert(monomial, sum);
}
Some(())
}
fn add(mut self, other: &Polynomial) -> Option<Polynomial> {
for (monomial, coefficient) in &other.terms {
self.add_term(monomial.clone(), *coefficient)?;
}
Some(self)
}
fn neg(&self) -> Option<Polynomial> {
let mut terms = BTreeMap::new();
for (monomial, coefficient) in &self.terms {
terms.insert(monomial.clone(), coefficient.neg()?);
}
Some(Polynomial { terms })
}
fn mul(&self, other: &Polynomial) -> Option<Polynomial> {
let mut product = Polynomial::constant(Rational::ZERO);
for (m1, c1) in &self.terms {
for (m2, c2) in &other.terms {
let mut monomial = m1.clone();
for (&name, &exponent) in m2 {
let e = monomial.entry(name).or_insert(0);
*e = e.checked_add(exponent)?;
}
product.add_term(monomial, c1.mul(*c2)?)?;
}
if product.terms.len() > MAX_TERMS * 4 {
return None;
}
}
Some(product)
}
fn div(&self, divisor: &Polynomial) -> Option<Polynomial> {
let factor = divisor.as_constant()?.recip()?;
self.mul(&Polynomial::constant(factor))
}
fn pow(&self, exponent: &Polynomial) -> Option<Polynomial> {
let exponent = exponent.as_constant()?.as_integer()?;
if let Some(base) = self.as_constant() {
return Some(Polynomial::constant(base.pow(exponent)?));
}
if !(0..=MAX_POLYNOMIAL_EXPONENT as i128).contains(&exponent) {
return None;
}
let mut result = Polynomial::constant(Rational::ONE);
for _ in 0..exponent {
result = result.mul(self)?;
}
Some(result)
}
/// E.g. `x² + 6x + 9`, `a² − b²`, `7,5`: highest degree first, then in alphabetical
/// order of the variables (`a² + 2ab + b²`).
fn format(&self, decimal_comma: bool) -> String {
if self.terms.is_empty() {
return "0".to_owned();
}
let degree = |m: &Monomial| m.values().sum::<u32>();
let mut terms: Vec<_> = self.terms.iter().collect();
terms.sort_by(|(a, _), (b, _)| degree(b).cmp(&degree(a)).then_with(|| graded_order(a, b)));
let mut out = String::new();
for (index, (monomial, coefficient)) in terms.into_iter().enumerate() {
let negative = coefficient.num < 0;
match (index, negative) {
(0, true) => out.push('−'),
(0, false) => {}
(_, true) => out.push_str(" − "),
(_, false) => out.push_str(" + "),
}
let magnitude = coefficient.format_abs(decimal_comma);
if monomial.is_empty() {
out.push_str(&magnitude);
continue;
}
if magnitude.contains('/') {
let _ = write!(out, "({magnitude})");
} else if magnitude != "1" {
out.push_str(&magnitude);
}
for (&name, &exponent) in monomial {
out.push(name);
if exponent > 1 {
out.push_str(&superscript(exponent));
}
}
}
out
}
}
/// Order within the same degree: the monomial with the higher power of the alphabetically
/// first variable comes first (`a²`, `ab`, `b²`).
fn graded_order(a: &Monomial, b: &Monomial) -> Ordering {
let names: std::collections::BTreeSet<char> = a.keys().chain(b.keys()).copied().collect();
for name in names {
let ea = a.get(&name).copied().unwrap_or(0);
let eb = b.get(&name).copied().unwrap_or(0);
match eb.cmp(&ea) {
Ordering::Equal => continue,
other => return other,
}
}
Ordering::Equal
}
const SUPERSCRIPT_DIGITS: [char; 10] = ['⁰', '¹', '²', '³', '⁴', '⁵', '⁶', '⁷', '⁸', '⁹'];
fn superscript(n: u32) -> String {
n.to_string()
.chars()
.map(|d| SUPERSCRIPT_DIGITS[d.to_digit(10).unwrap_or(0) as usize])
.collect()
}
// --- Reading the expression ---------------------------------------------------------------
#[derive(Debug, Clone, PartialEq)]
enum Token {
Number(Rational),
Variable(char),
Plus,
Minus,
Times,
/// `/` for a fraction, `:` for a division sign (`:` and `÷`)
Divide(char),
Power,
Open,
Close,
}
fn tokenize(input: &str) -> Option<Vec<Token>> {
let input = input.trim().trim_end_matches('=').trim_end();
let chars: Vec<char> = input.chars().collect();
let mut tokens = Vec::new();
let mut i = 0;
while i < chars.len() {
let c = chars[i];
if c.is_ascii_digit() {
let (value, next) = read_number(&chars, i)?;
tokens.push(Token::Number(value));
i = next;
continue;
}
if superscript_value(c).is_some() || c == '⁻' {
// A run of superscript characters is the exponent: x²³ = x^23, 2⁻¹ = 2^(-1)
let negative = c == '⁻';
let start = if negative { i + 1 } else { i };
let mut end = start;
let mut value: i128 = 0;
while let Some(digit) = chars.get(end).and_then(|&d| superscript_value(d)) {
value = value.checked_mul(10)?.checked_add(digit)?;
end += 1;
}
if end == start {
return None;
}
tokens.push(Token::Power);
if negative {
tokens.push(Token::Minus);
}
tokens.push(Token::Number(Rational::integer(value)));
i = end;
continue;
}
let token = match c {
_ if c.is_whitespace() => None,
'+' => Some(Token::Plus),
'-' | '−' | '–' => Some(Token::Minus),
'*' | '·' | '⋅' | '∙' | '×' => Some(Token::Times),
'/' => Some(Token::Divide('/')),
':' | '÷' => Some(Token::Divide(':')),
'^' => Some(Token::Power),
'(' | '[' | '{' => Some(Token::Open),
')' | ']' | '}' => Some(Token::Close),
_ if c.is_ascii_alphabetic() => Some(Token::Variable(c)),
_ => return None,
};
tokens.extend(token);
i += 1;
}
Some(tokens)
}
fn superscript_value(c: char) -> Option<i128> {
SUPERSCRIPT_DIGITS
.iter()
.position(|&d| d == c)
.map(|p| p as i128)
}
/// Reads `12`, `2,5` or `0.125` from `start`; returns the value and the index after it
fn read_number(chars: &[char], start: usize) -> Option<(Rational, usize)> {
let mut i = start;
let mut num: i128 = 0;
let mut den: i128 = 1;
let mut in_fraction = false;
while i < chars.len() {
let c = chars[i];
if let Some(digit) = c.to_digit(10) {
num = num.checked_mul(10)?.checked_add(digit as i128)?;
if in_fraction {
den = den.checked_mul(10)?;
}
} else if (c == ',' || c == '.')
&& !in_fraction
&& chars.get(i + 1).is_some_and(|d| d.is_ascii_digit())
{
in_fraction = true;
} else {
break;
}
i += 1;
}
Some((Rational::new(num, den)?, i))
}
#[derive(Debug, Clone, PartialEq)]
enum Expr {
Number(Rational),
Variable(char),
Neg(Box<Expr>),
Add(Box<Expr>, Box<Expr>),
Sub(Box<Expr>, Box<Expr>),
Mul(Box<Expr>, Box<Expr>),
/// With the sign it was written with, for [`display`]
Div(Box<Expr>, Box<Expr>, char),
Pow(Box<Expr>, Box<Expr>),
}
impl Expr {
fn eval(&self) -> Option<Polynomial> {
Some(match self {
Expr::Number(value) => Polynomial::constant(*value),
Expr::Variable(name) => Polynomial::variable(*name),
Expr::Neg(e) => e.eval()?.neg()?,
Expr::Add(a, b) => a.eval()?.add(&b.eval()?)?,
Expr::Sub(a, b) => a.eval()?.add(&b.eval()?.neg()?)?,
Expr::Mul(a, b) => a.eval()?.mul(&b.eval()?)?,
Expr::Div(a, b, _) => a.eval()?.div(&b.eval()?)?,
Expr::Pow(a, b) => a.eval()?.pow(&b.eval()?)?,
})
}
}
impl Expr {
/// Binding strength for deciding on parentheses: sums < products < signs < powers < atoms
fn precedence(&self) -> u8 {
match self {
Expr::Add(..) | Expr::Sub(..) => 1,
Expr::Mul(..) | Expr::Div(..) => 2,
Expr::Neg(_) => 3,
Expr::Pow(..) => 4,
Expr::Number(_) | Expr::Variable(_) => 5,
}
}
/// The text of this expression, in parentheses if it binds weaker than `min`
fn display_at(&self, min: u8, decimal_comma: bool) -> String {
let text = self.display(decimal_comma);
if self.precedence() < min {
format!("({text})")
} else {
text
}
}
fn display(&self, decimal_comma: bool) -> String {
let dc = decimal_comma;
match self {
Expr::Number(value) => value.format_abs(dc),
Expr::Variable(name) => name.to_string(),
Expr::Neg(e) => format!("−{}", e.display_at(3, dc)),
Expr::Add(a, b) => format!("{} + {}", a.display_at(1, dc), b.display_at(1, dc)),
Expr::Sub(a, b) => format!("{} − {}", a.display_at(1, dc), b.display_at(2, dc)),
Expr::Mul(a, b) => {
let left = a.display_at(2, dc);
let right = b.display_at(3, dc);
// 2x, 3(x + 1), x², but 2 · 3 and x · 2
let implicit = !matches!(**a, Expr::Neg(_))
&& (matches!(**b, Expr::Variable(_))
|| matches!(&**b, Expr::Pow(base, _) if matches!(**base, Expr::Variable(_)))
|| right.starts_with('('));
if implicit {
format!("{left}{right}")
} else {
format!("{left} · {right}")
}
}
Expr::Div(a, b, sign) => match sign {
'/' => format!("{}/{}", a.display_at(4, dc), b.display_at(4, dc)),
_ => format!("{} : {}", a.display_at(2, dc), b.display_at(3, dc)),
},
Expr::Pow(base, exponent) => {
let base = base.display_at(5, dc);
let integer = |e: &Expr| match e {
Expr::Number(v) => v.as_integer().map(|n| superscript(n as u32)),
_ => None,
};
let small = match &**exponent {
Expr::Neg(e) => integer(e).map(|s| format!("⁻{s}")),
e => integer(e),
};
match small {
Some(small) => format!("{base}{small}"),
None => format!("{base}^({})", exponent.display(dc)),
}
}
}
}
}
/// Recursive descent with the usual precedence: `+ −` < `· :` and implicit
/// multiplication < unary minus < `^` (right-associative, so `−2² = −4` and `2^3^2 = 2^9`).
struct Parser<'a> {
tokens: &'a [Token],
pos: usize,
}
impl<'a> Parser<'a> {
fn new(tokens: &'a [Token]) -> Self {
Parser { tokens, pos: 0 }
}
fn parse(mut self) -> Option<Expr> {
let expr = self.sum()?;
(self.pos == self.tokens.len()).then_some(expr)
}
fn peek(&self) -> Option<&Token> {
self.tokens.get(self.pos)
}
fn eat(&mut self, token: &Token) -> bool {
let found = self.peek() == Some(token);
if found {
self.pos += 1;
}
found
}
fn sum(&mut self) -> Option<Expr> {
let mut left = self.product()?;
loop {
if self.eat(&Token::Plus) {
left = Expr::Add(Box::new(left), Box::new(self.product()?));
} else if self.eat(&Token::Minus) {
left = Expr::Sub(Box::new(left), Box::new(self.product()?));
} else {
return Some(left);
}
}
}
fn product(&mut self) -> Option<Expr> {
let mut left = self.unary()?;
loop {
if self.eat(&Token::Times) {
left = Expr::Mul(Box::new(left), Box::new(self.unary()?));
} else if let Some(&Token::Divide(sign)) = self.peek() {
self.pos += 1;
left = Expr::Div(Box::new(left), Box::new(self.unary()?), sign);
} else if matches!(self.peek(), Some(Token::Variable(_) | Token::Open)) {
// Implicit multiplication: 2x, 3(x + 1), (a + b)(a − b). Two numbers in a
// row are not multiplied, they are more likely a misread.
left = Expr::Mul(Box::new(left), Box::new(self.power()?));
} else {
return Some(left);
}
}
}
fn unary(&mut self) -> Option<Expr> {
if self.eat(&Token::Minus) {
return Some(Expr::Neg(Box::new(self.unary()?)));
}
if self.eat(&Token::Plus) {
return self.unary();
}
self.power()
}
fn power(&mut self) -> Option<Expr> {
let base = self.atom()?;
if self.eat(&Token::Power) {
return Some(Expr::Pow(Box::new(base), Box::new(self.unary()?)));
}
Some(base)
}
fn atom(&mut self) -> Option<Expr> {
let token = self.peek()?.clone();
self.pos += 1;
match token {
Token::Number(value) => Some(Expr::Number(value)),
Token::Variable(name) => Some(Expr::Variable(name)),
Token::Open => {
let inner = self.sum()?;
self.eat(&Token::Close).then_some(inner)
}
_ => None,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn de(expression: &str) -> Option<String> {
solve(expression, true)
}
#[test]
fn arithmetic() {
assert_eq!(de("2^4 ="), Some("16".into()));
assert_eq!(de("2⁴="), Some("16".into()));
assert_eq!(de("1 + 2 · 3"), Some("7".into()));
assert_eq!(de("(1 + 2) × 3"), Some("9".into()));
assert_eq!(de("7 − 10"), Some("−3".into()));
assert_eq!(de("−2^2"), Some("−4".into()));
assert_eq!(de("(−2)²"), Some("4".into()));
assert_eq!(de("2^3^2"), Some("512".into()));
assert_eq!(de("10 : 4"), Some("2,5".into()));
assert_eq!(de("10 ÷ 4"), Some("2,5".into()));
assert_eq!(de("3 * [2 + 1]"), Some("9".into()));
}
#[test]
fn decimals_and_fractions() {
assert_eq!(de("2,5 · 3"), Some("7,5".into()));
assert_eq!(solve("2.5 * 3", false), Some("7.5".into()));
assert_eq!(de("0,1 + 0,2"), Some("0,3".into()));
assert_eq!(de("1 : 3"), Some("1/3".into()));
assert_eq!(de("1 : 3 · 3"), Some("1".into()));
assert_eq!(de("2^-2"), Some("0,25".into()));
assert_eq!(de("2⁻¹"), Some("0,5".into()));
assert_eq!(de("1 : 8"), Some("0,125".into()));
}
#[test]
fn polynomials() {
assert_eq!(de("(x + 3)^2 ="), Some("x² + 6x + 9".into()));
assert_eq!(de("(x+3)²"), Some("x² + 6x + 9".into()));
assert_eq!(de("(a + b)²"), Some("a² + 2ab + b²".into()));
assert_eq!(de("(a + b)(a − b)"), Some("a² − b²".into()));
assert_eq!(de("(a − b)²"), Some("a² − 2ab + b²".into()));
assert_eq!(de("2x + 3x"), Some("5x".into()));
assert_eq!(de("x · x"), Some("x²".into()));
assert_eq!(de("3(x + 1)"), Some("3x + 3".into()));
assert_eq!(de("(x + 1)^3"), Some("x³ + 3x² + 3x + 1".into()));
assert_eq!(de("(2x − 1)²"), Some("4x² − 4x + 1".into()));
assert_eq!(de("x − x"), Some("0".into()));
assert_eq!(de("(x + 1) : 2"), Some("0,5x + 0,5".into()));
assert_eq!(de("x : 3"), Some("(1/3)x".into()));
assert_eq!(de("−(x + 1)"), Some("−x − 1".into()));
assert_eq!(
de("(x+1)^10").map(|s| s.starts_with("x¹⁰ + 10x⁹")),
Some(true)
);
}
#[test]
fn nothing_to_compute() {
assert_eq!(de("5"), None);
assert_eq!(de("x ="), None);
assert_eq!(de("2x"), None);
assert_eq!(de("x² + 1"), None);
assert_eq!(de("2,5"), None);
}
#[test]
fn unreadable_or_not_computable() {
assert_eq!(de(""), None);
assert_eq!(de("="), None);
assert_eq!(de("1 +"), None);
assert_eq!(de("(1 + 2"), None);
assert_eq!(de("1 + 2)"), None);
assert_eq!(de("2 3"), None);
assert_eq!(de("1 : 0"), None);
assert_eq!(de("0^-1"), None);
assert_eq!(de("1 : x"), None);
assert_eq!(de("x^y"), None);
assert_eq!(de("x^0,5"), None);
assert_eq!(de("2 ? 3"), None);
assert_eq!(de("1,2,3"), None);
}
#[test]
fn displays_expressions() {
let show = |e: &str| display(e, true).unwrap();
assert_eq!(show("2^(4)"), "2⁴");
assert_eq!(show("(x+3)^(2)"), "(x + 3)²");
assert_eq!(show("2,5·3"), "2,5 · 3");
assert_eq!(show("9:3"), "9 : 3");
assert_eq!(show("((x+1)/(2))"), "(x + 1)/2");
assert_eq!(show("((1)/(2))+((1)/(2·3))"), "1/2 + 1/(2 · 3)");
assert_eq!(show("2^(-1)"), "2⁻¹");
assert_eq!(show("2^(x+1)"), "2^(x + 1)");
assert_eq!(show("3(x+1)"), "3(x + 1)");
assert_eq!(show("2x^(2)"), "2x²");
assert_eq!(show("x·2"), "x · 2");
assert_eq!(show("(a+b)(a-b)"), "(a + b)(a − b)");
assert_eq!(show("7-(5-2)"), "7 − (5 − 2)");
assert_eq!(show("-2^(2)"), "−2²");
assert_eq!(show("(-2)^(2)"), "(−2)²");
assert_eq!(show("0.5"), "0,5");
assert!(display("1 +", true).is_none());
}
#[test]
fn overflow_and_limits() {
assert_eq!(de("10^100"), None);
assert_eq!(de("2^100000"), None);
assert_eq!(de("(x + 1)^33"), None);
assert_eq!(de("(a+b+c+d+e)^8"), None);
assert_eq!(de("99999999999999999999999999999999999999999999 + 1"), None);
assert_eq!(de("2^64"), Some("18446744073709551616".into()));
}
}
+465
View File
@@ -0,0 +1,465 @@
//! Computes handwritten math after an `=` (Issue #46, stage 3).
//!
//! When the last two strokes on a page form an `=` and the pen then rests for
//! [`TRIGGER_DELAY`], the strokes left of it are recognized ([`crate::math_ink`]) and computed
//! ([`crate::math::solve`]). The result is offered next to the `=` as a suggestion that also
//! shows what was read (`2⁴ = 16`), so that a misread symbol is noticed. Tapping it with the
//! pen or clicking it inserts the result as a text box; fingers and palms are ignored. Any new
//! stroke drops the suggestion. Off by default (☰ → Input).
use std::sync::Arc;
use std::time::{Duration, Instant};
use glam::Vec2;
use crate::document::{Page, Stroke, StrokeTool};
use crate::math_ink::Recognizer;
/// Pause after the `=` before its expression is computed; a new stroke cancels it
pub const TRIGGER_DELAY: Duration = Duration::from_millis(500);
/// Estimated symbol height relative to the width of the `=`
const SYMBOL_PER_EQUALS: f32 = 1.3;
/// Strokes left of the `=` belong to the expression up to a gap of this many symbol heights
const MAX_GAP: f32 = 2.0;
/// At most this many strokes are read as the expression
const MAX_STROKES: usize = 80;
/// Cap height of the text font relative to its size (Ubuntu: 0.69 em)
const CAP_HEIGHT_PER_EM: f32 = 0.7;
/// A result offered next to an `=`
#[derive(Debug, Clone, PartialEq)]
pub struct Suggestion {
pub page_id: u64,
/// The two strokes of the `=`
pub equals: [u64; 2],
/// What was read, written neatly (`2⁴`)
pub expression: String,
/// The result (`16`)
pub result: String,
/// Where the result goes: left end of the text on the vertical middle of the `=`
pub anchor: Vec2,
/// Height of the handwritten digits in page units
pub symbol_height: f32,
/// Ink color of the `=`
pub color: [f32; 4],
}
impl Suggestion {
/// Top left corner and size (em) of a text box that puts the result next to the `=`, as
/// high as the handwriting
pub fn text_placement(&self, ascent_per_em: f32) -> (Vec2, f32) {
let size = self.symbol_height / CAP_HEIGHT_PER_EM;
// Baseline at the bottom of digits centered on the `=`
let baseline = self.anchor.y + self.symbol_height / 2.0;
(
Vec2::new(self.anchor.x, baseline - ascent_per_em * size),
size,
)
}
}
#[derive(Debug, Clone)]
struct Pending {
page_id: u64,
equals: [u64; 2],
due: Instant,
}
/// State of the math assistant: waiting after an `=`, and the suggestion shown
#[derive(Default, Clone)]
pub struct MathAssist {
pub enabled: bool,
/// Built on first use
recognizer: Option<Arc<Recognizer>>,
pending: Option<Pending>,
suggestion: Option<Suggestion>,
/// Where the UI drew the suggestion (egui points), for [`Self::take_hit`]
pub chip_rect: Option<egui::Rect>,
}
impl std::fmt::Debug for MathAssist {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("MathAssist")
.field("enabled", &self.enabled)
.field("pending", &self.pending)
.field("suggestion", &self.suggestion)
.finish_non_exhaustive()
}
}
impl MathAssist {
/// After a stroke was added to `page`: drops the suggestion and waits if the stroke
/// completed an `=`
pub fn stroke_committed(&mut self, page: &Page, now: Instant) {
self.suggestion = None;
self.chip_rect = None;
self.pending = None;
if !self.enabled {
return;
}
if let Some((a, b)) = equals_at_end(&page.strokes) {
self.pending = Some(Pending {
page_id: page.id,
equals: [page.strokes[a].id, page.strokes[b].id],
due: now + TRIGGER_DELAY,
});
}
}
/// When [`Self::update`] has something to do
pub fn deadline(&self) -> Option<Instant> {
self.pending.as_ref().map(|p| p.due)
}
/// Computes the expression once the pause after the `=` is over (not while a stroke is
/// being drawn). True if a suggestion appeared.
pub fn update(
&mut self,
page: &Page,
now: Instant,
drawing: bool,
decimal_comma: bool,
) -> bool {
let Some(pending) = self.pending.as_ref() else {
return false;
};
if drawing || now < pending.due {
return false;
}
let pending = self.pending.take().expect("checked above");
if pending.page_id != page.id || !self.enabled {
return false;
}
let recognizer = self
.recognizer
.get_or_insert_with(|| Arc::new(Recognizer::new()));
self.suggestion = suggest(recognizer, page, pending.equals, decimal_comma);
self.suggestion.is_some()
}
/// The suggestion if it belongs to `page` and its `=` is still there
pub fn suggestion_for(&self, page: &Page) -> Option<&Suggestion> {
self.suggestion.as_ref().filter(|s| {
s.page_id == page.id
&& s.equals
.iter()
.all(|id| page.strokes.iter().any(|stroke| stroke.id == *id))
})
}
/// The suggestion if `pos` (egui points) is on it; it is then taken away
pub fn take_hit(&mut self, pos: egui::Pos2) -> Option<Suggestion> {
if !self.chip_rect.is_some_and(|r| r.contains(pos)) {
return None;
}
self.chip_rect = None;
self.suggestion.take()
}
pub fn set_enabled(&mut self, enabled: bool) {
self.enabled = enabled;
if !enabled {
self.pending = None;
self.suggestion = None;
self.chip_rect = None;
}
}
}
/// Handwriting that can be read: pen strokes (also snapped lines), no highlighter, text or
/// images
fn is_ink(stroke: &Stroke) -> bool {
!stroke.is_object() && stroke.tool != StrokeTool::Highlighter && !stroke.points.is_empty()
}
fn positions(stroke: &Stroke) -> Vec<Vec2> {
stroke.points.iter().map(|s| s.position).collect()
}
/// A straight, roughly horizontal stroke: one bar of an `=`
fn is_bar(stroke: &Stroke) -> bool {
let size = stroke.aabb.max - stroke.aabb.min;
let length: f32 = positions(stroke)
.windows(2)
.map(|w| w[0].distance(w[1]))
.sum();
size.x > 0.0 && size.x >= 2.5 * size.y && length <= 1.3 * size.x
}
/// Indices of the last two ink strokes if they form an `=`: two bars of similar length, one
/// above the other
fn equals_at_end(strokes: &[Stroke]) -> Option<(usize, usize)> {
let mut ink = strokes.iter().enumerate().rev().filter(|(_, s)| is_ink(s));
let (b, second) = ink.next()?;
let (a, first) = ink.next()?;
if !is_bar(first) || !is_bar(second) {
return None;
}
let (w1, w2) = (
first.aabb.max.x - first.aabb.min.x,
second.aabb.max.x - second.aabb.min.x,
);
let (short, long) = (w1.min(w2), w1.max(w2));
let overlap = first.aabb.max.x.min(second.aabb.max.x) - first.aabb.min.x.max(second.aabb.min.x);
let dy = ((first.aabb.min.y + first.aabb.max.y) - (second.aabb.min.y + second.aabb.max.y))
.abs()
/ 2.0;
(overlap >= 0.5 * short && long <= 2.0 * short && dy >= 0.1 * long && dy <= long)
.then_some((a, b))
}
/// Indices of the strokes left of the `=` that make up its expression, in the order they were
/// written: ink in a band around the `=`, going left until a wide gap
fn expression_strokes(strokes: &[Stroke], equals: (usize, usize)) -> Vec<usize> {
let (e1, e2) = (&strokes[equals.0], &strokes[equals.1]);
let min = e1.aabb.min.min(e2.aabb.min);
let max = e1.aabb.max.max(e2.aabb.max);
let height = (max.x - min.x) * SYMBOL_PER_EQUALS;
let middle = (min.y + max.y) / 2.0;
let mut candidates: Vec<usize> = (0..strokes.len())
.filter(|&i| i != equals.0 && i != equals.1 && is_ink(&strokes[i]))
.filter(|&i| {
let s = &strokes[i].aabb;
let center = (s.min.y + s.max.y) / 2.0;
(center - middle).abs() <= 2.0 * height
&& s.max.y - s.min.y <= 3.0 * height
&& s.max.x <= min.x + 0.2 * height
})
.collect();
candidates.sort_by(|&a, &b| strokes[b].aabb.max.x.total_cmp(&strokes[a].aabb.max.x));
let mut left = min.x;
let mut chosen = Vec::new();
for i in candidates {
let s = &strokes[i].aabb;
if s.max.x < left - MAX_GAP * height || chosen.len() >= MAX_STROKES {
break;
}
left = left.min(s.min.x);
chosen.push(i);
}
chosen.sort_unstable();
chosen
}
/// Reads and computes the expression in front of the `=` made of the strokes `equals`
fn suggest(
recognizer: &Recognizer,
page: &Page,
equals: [u64; 2],
decimal_comma: bool,
) -> Option<Suggestion> {
let strokes = &page.strokes;
let index = |id: u64| strokes.iter().position(|s| s.id == id);
let pair = (index(equals[0])?, index(equals[1])?);
let mut chosen = expression_strokes(strokes, pair);
if chosen.is_empty() {
return None;
}
chosen.extend([pair.0, pair.1]);
let points: Vec<Vec<Vec2>> = chosen.iter().map(|&i| positions(&strokes[i])).collect();
let recognition = recognizer.recognize(&points)?;
// The `=` must be read as the last symbol, from exactly its two strokes
let last = recognition.symbols.last()?;
let equals_strokes = [points.len() - 2, points.len() - 1];
let mut last_strokes = last.strokes.clone();
last_strokes.sort_unstable();
if last.ch != '=' || last_strokes != equals_strokes {
return None;
}
// In a chain like `x = 2 + 3 =` only the part after the previous `=` is computed
let text = recognition.text.strip_suffix('=')?;
let expression = text.rsplit('=').next()?;
// A dot or comma that was written decides the separator of the result
let decimal_comma = if expression.contains(',') {
true
} else if expression.contains('.') {
false
} else {
decimal_comma
};
let result = crate::math::solve(expression, decimal_comma)?;
let shown = crate::math::display(expression, decimal_comma)?;
let (e1, e2) = (&strokes[pair.0], &strokes[pair.1]);
let right = e1.aabb.max.x.max(e2.aabb.max.x);
let middle = (e1.aabb.min.y.min(e2.aabb.min.y) + e1.aabb.max.y.max(e2.aabb.max.y)) / 2.0;
let mut heights: Vec<f32> = recognition
.symbols
.iter()
.filter(|s| s.ch.is_ascii_digit() || s.ch.is_ascii_alphabetic())
.map(|s| s.max.y - s.min.y)
.collect();
heights.sort_by(f32::total_cmp);
let width = right - e1.aabb.min.x.min(e2.aabb.min.x);
let symbol_height = heights
.last()
.copied()
.unwrap_or(width * SYMBOL_PER_EQUALS)
.max(1.0);
Some(Suggestion {
page_id: page.id,
equals,
expression: shown,
result,
anchor: Vec2::new(right + 0.4 * symbol_height, middle),
symbol_height,
color: e2.color,
})
}
#[cfg(test)]
mod tests {
use super::*;
use crate::document::StylusSample;
use crate::math_ink::tests::{write_line, Jitter};
const BLACK: [f32; 4] = [0.0, 0.0, 0.0, 1.0];
fn stroke(id: u64, points: &[Vec2]) -> Stroke {
let samples = points
.iter()
.enumerate()
.map(|(i, &position)| StylusSample {
position,
pressure: 0.5,
timestamp: i as f64 * 0.01,
})
.collect();
Stroke::new(id, samples, Vec::new(), BLACK)
}
/// A page with `text` written at `origin`, scaled by `scale`
fn page_with(lines: &[(&str, Vec2)], scale: f32) -> Page {
let mut page = Page::new(7, crate::document::PageBackground::Blank);
let mut id = 1;
for (seed, (text, origin)) in lines.iter().enumerate() {
for points in write_line(text, &mut Jitter(seed as u64 + 3)) {
let points: Vec<Vec2> = points.iter().map(|p| *origin + *p * scale).collect();
page.add_stroke(stroke(id, &points));
id += 1;
}
}
page
}
fn run(page: &Page) -> Option<Suggestion> {
let mut assist = MathAssist {
enabled: true,
..Default::default()
};
let start = Instant::now();
assist.stroke_committed(page, start);
assert!(!assist.update(page, start, false, true), "too early");
assist.update(page, start + TRIGGER_DELAY, false, true);
assist.suggestion_for(page).cloned()
}
#[test]
fn suggests_result_after_equals() {
let page = page_with(&[("(x+3)^2=", Vec2::new(100.0, 200.0))], 3.0);
let suggestion = run(&page).unwrap();
assert_eq!(suggestion.expression, "(x + 3)²");
assert_eq!(suggestion.result, "x² + 6x + 9");
let equals_right = page
.strokes
.iter()
.rev()
.take(2)
.map(|s| s.aabb.max.x)
.fold(f32::MIN, f32::max);
assert!(suggestion.anchor.x > equals_right);
// The handwriting is 14 units high, scaled by 3
assert!(
(suggestion.symbol_height - 42.0).abs() < 6.0,
"{suggestion:?}"
);
}
#[test]
fn ignores_writing_far_to_the_left_and_on_other_lines() {
let page = page_with(
&[
("7", Vec2::new(0.0, 200.0)),
("5·5", Vec2::new(0.0, 300.0)),
("2^4=", Vec2::new(150.0, 200.0)),
],
2.0,
);
let suggestion = run(&page).unwrap();
assert_eq!(suggestion.expression, "2⁴");
assert_eq!(suggestion.result, "16");
}
#[test]
fn computes_the_last_part_of_a_chain() {
let page = page_with(&[("x=2·3=", Vec2::new(0.0, 0.0))], 2.0);
assert_eq!(run(&page).unwrap().result, "6");
}
#[test]
fn nothing_without_equals_or_when_disabled() {
let page = page_with(&[("2+3", Vec2::ZERO)], 2.0);
assert!(run(&page).is_none());
let page = page_with(&[("2+3=", Vec2::ZERO)], 2.0);
let mut assist = MathAssist::default();
assist.stroke_committed(&page, Instant::now());
assert!(assist.deadline().is_none());
}
#[test]
fn new_stroke_or_removed_equals_drops_the_suggestion() {
let mut page = page_with(&[("1+1=", Vec2::ZERO)], 2.0);
let mut assist = MathAssist {
enabled: true,
..Default::default()
};
let start = Instant::now();
assist.stroke_committed(&page, start);
// A stroke still being drawn when the time is up: nothing yet
assert!(!assist.update(&page, start + TRIGGER_DELAY, true, true));
assert!(assist.update(&page, start + TRIGGER_DELAY, false, true));
assert_eq!(assist.suggestion_for(&page).unwrap().result, "2");
let last = page.strokes.last().unwrap().id;
page.remove_strokes_by_ids(&[last]);
assert!(assist.suggestion_for(&page).is_none());
assist.stroke_committed(&page, start);
assert!(assist.suggestion.is_none());
}
#[test]
fn hit_takes_the_suggestion() {
let page = page_with(&[("1+1=", Vec2::ZERO)], 2.0);
let mut assist = MathAssist {
enabled: true,
..Default::default()
};
let start = Instant::now();
assist.stroke_committed(&page, start);
assist.update(&page, start + TRIGGER_DELAY, false, true);
assist.chip_rect = Some(egui::Rect::from_min_size(
egui::pos2(10.0, 10.0),
egui::vec2(50.0, 20.0),
));
assert!(assist.take_hit(egui::pos2(5.0, 5.0)).is_none());
assert_eq!(assist.take_hit(egui::pos2(20.0, 20.0)).unwrap().result, "2");
assert!(assist.take_hit(egui::pos2(20.0, 20.0)).is_none());
}
#[test]
fn result_text_sits_on_the_baseline() {
let suggestion = Suggestion {
page_id: 1,
equals: [1, 2],
expression: "1 + 1".into(),
result: "2".into(),
anchor: Vec2::new(100.0, 50.0),
symbol_height: 14.0,
color: BLACK,
};
let (top_left, size) = suggestion.text_placement(0.9);
assert!((size - 20.0).abs() < 0.01);
// Baseline (top + ascent) at the bottom of digits centered on the `=`
assert!((top_left.y + 0.9 * size - 57.0).abs() < 0.01);
assert_eq!(top_left.x, 100.0);
}
}
+1259
View File
File diff suppressed because it is too large. Load diff
+853
View File
@@ -0,0 +1,853 @@
//! Talking to a Nextcloud server (Issue #49, stage 2): finding the server, signing in through
//! the browser (Login Flow v2, the app gets its own app password), and WebDAV access to the
//! folder `ink-notes` in the user's files. The sync itself is in `cloud_sync.rs`.
//!
//! Private servers often use a self-signed certificate. Such a certificate is trusted once the
//! user has confirmed its SHA-256 fingerprint; it is then pinned for this server.
use std::io::Read;
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex};
use std::time::Duration;
use base64::Engine;
use rustls::client::danger::{HandshakeSignatureValid, ServerCertVerified, ServerCertVerifier};
use rustls::pki_types::{CertificateDer, ServerName, UnixTime};
use rustls::{DigitallySignedStruct, SignatureScheme};
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use crate::t;
/// Name of the synced folder in the user's files.
pub const REMOTE_FOLDER: &str = "ink-notes";
/// Largest file the sync downloads (bigger ones are surely not ours).
const MAX_DOWNLOAD: u64 = 512 * 1024 * 1024;
/// A connected Nextcloud account, stored in `nextcloud.json` next to the settings.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Account {
/// Server address including a subfolder, without trailing slash
pub server: String,
/// Login name for the app password
pub login_name: String,
/// User id in WebDAV paths (differs from the login name e.g. with e-mail logins)
pub user_id: String,
pub app_password: String,
/// SHA-256 fingerprint of a certificate the user trusts although it isn't signed by a
/// known authority
#[serde(default, skip_serializing_if = "Option::is_none")]
pub pinned_cert: Option<String>,
}
impl Account {
pub fn file_path() -> Option<PathBuf> {
crate::paths::config_dir().map(|d| d.join("nextcloud.json"))
}
pub fn load_from(path: &Path) -> Option<Self> {
serde_json::from_slice(&std::fs::read(path).ok()?).ok()
}
/// Stores the account; the file is only readable by the user.
pub fn save_to(&self, path: &Path) -> Result<(), String> {
if let Some(dir) = path.parent() {
crate::storage::create_secure_dir_all(dir).map_err(|e| e.to_string())?;
}
crate::storage::atomic_write_json(path, self).map_err(|e| e.to_string())?;
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
let _ = std::fs::set_permissions(path, std::fs::Permissions::from_mode(0o600));
}
Ok(())
}
/// `user@server` for the interface.
pub fn label(&self) -> String {
let host = url::Url::parse(&self.server)
.ok()
.and_then(|u| {
let port = u.port().map(|p| format!(":{p}")).unwrap_or_default();
u.host_str()
.map(|h| format!("{h}{port}{}", u.path().trim_end_matches('/')))
})
.unwrap_or_else(|| self.server.clone());
format!("{}@{host}", self.login_name)
}
pub fn is_insecure(&self) -> bool {
self.server.starts_with("http://")
}
}
/// Why talking to the server failed.
#[derive(Debug, Clone, PartialEq)]
pub enum Error {
/// The certificate isn't signed by a known authority (or doesn't match the pinned one)
Untrusted {
fingerprint: String,
},
/// Server not reachable (offline, wrong address)
Offline(String),
/// App password revoked or wrong
Unauthorized,
NotFound,
/// The file changed on the server since we last saw it
PreconditionFailed,
Other(String),
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Error::Untrusted { fingerprint } => write!(
f,
"{}",
t!(
"Zertifikat nicht vertrauenswürdig (SHA-256 {fingerprint})",
fingerprint = fingerprint
)
),
Error::Offline(msg) => write!(f, "{}", t!("Server nicht erreichbar: {msg}", msg = msg)),
Error::Unauthorized => write!(f, "{}", t!("Anmeldung abgelehnt – bitte neu verbinden")),
Error::NotFound => write!(f, "{}", t!("Nicht gefunden")),
Error::PreconditionFailed => write!(f, "{}", t!("Inzwischen geändert")),
Error::Other(msg) => write!(f, "{msg}"),
}
}
}
/// Server address from what the user typed: `https://` is added when missing, a pasted link
/// into the web interface is cut back to the server (a subfolder such as `/nextcloud` stays).
pub fn normalize_server(input: &str) -> Result<String, String> {
let input = input.trim();
if input.is_empty() {
return Err(t!("Bitte die Adresse des Nextcloud-Servers eingeben").to_string());
}
let with_scheme = if input.contains("://") {
input.to_string()
} else {
format!("https://{input}")
};
let mut url = url::Url::parse(&with_scheme).map_err(|e| t!("Ungültige Adresse: {e}", e = e))?;
if !matches!(url.scheme(), "https" | "http") || url.host_str().is_none() {
return Err(t!("Die Adresse muss mit https:// beginnen").to_string());
}
url.set_query(None);
url.set_fragment(None);
let _ = url.set_username("");
let _ = url.set_password(None);
let mut path = url.path().to_string();
for marker in [
"/index.php",
"/apps/",
"/login",
"/remote.php",
"/status.php",
"/s/",
] {
if let Some(pos) = path.find(marker) {
path.truncate(pos);
}
}
url.set_path(path.trim_end_matches('/'));
Ok(url.as_str().trim_end_matches('/').to_string())
}
/// SHA-256 fingerprint as colon-separated hex pairs, as browsers show it.
pub fn fingerprint(cert: &[u8]) -> String {
Sha256::digest(cert)
.iter()
.map(|b| format!("{b:02X}"))
.collect::<Vec<_>>()
.join(":")
}
/// Accepts what the usual authorities sign and, besides, the one pinned certificate. Remembers
/// the fingerprint of a rejected certificate so the user can be asked about it.
#[derive(Debug)]
struct Verifier {
webpki: Arc<rustls::client::WebPkiServerVerifier>,
provider: Arc<rustls::crypto::CryptoProvider>,
pinned: Option<String>,
rejected: Arc<Mutex<Option<String>>>,
}
impl ServerCertVerifier for Verifier {
fn verify_server_cert(
&self,
end_entity: &CertificateDer<'_>,
intermediates: &[CertificateDer<'_>],
server_name: &ServerName<'_>,
ocsp_response: &[u8],
now: UnixTime,
) -> Result<ServerCertVerified, rustls::Error> {
match self.webpki.verify_server_cert(
end_entity,
intermediates,
server_name,
ocsp_response,
now,
) {
Ok(ok) => Ok(ok),
Err(err) => {
let print = fingerprint(end_entity);
if self.pinned.as_deref() == Some(print.as_str()) {
return Ok(ServerCertVerified::assertion());
}
if let Ok(mut slot) = self.rejected.lock() {
*slot = Some(print);
}
Err(err)
}
}
}
fn verify_tls12_signature(
&self,
message: &[u8],
cert: &CertificateDer<'_>,
dss: &DigitallySignedStruct,
) -> Result<HandshakeSignatureValid, rustls::Error> {
rustls::crypto::verify_tls12_signature(
message,
cert,
dss,
&self.provider.signature_verification_algorithms,
)
}
fn verify_tls13_signature(
&self,
message: &[u8],
cert: &CertificateDer<'_>,
dss: &DigitallySignedStruct,
) -> Result<HandshakeSignatureValid, rustls::Error> {
rustls::crypto::verify_tls13_signature(
message,
cert,
dss,
&self.provider.signature_verification_algorithms,
)
}
fn supported_verify_schemes(&self) -> Vec<SignatureScheme> {
self.provider
.signature_verification_algorithms
.supported_schemes()
}
}
/// HTTP client for one server, with the pinned certificate if any.
pub struct Client {
agent: ureq::Agent,
rejected: Arc<Mutex<Option<String>>>,
user_agent: String,
}
impl Client {
pub fn new(pinned: Option<String>) -> Self {
let provider = Arc::new(rustls::crypto::ring::default_provider());
let mut roots = rustls::RootCertStore::empty();
roots.extend(webpki_roots::TLS_SERVER_ROOTS.iter().cloned());
let rejected = Arc::new(Mutex::new(None));
let mut agent = ureq::AgentBuilder::new()
.timeout_connect(Duration::from_secs(10))
.timeout_read(Duration::from_secs(120))
.timeout_write(Duration::from_secs(120))
.redirects(0);
let webpki = rustls::client::WebPkiServerVerifier::builder_with_provider(
Arc::new(roots),
provider.clone(),
)
.build();
let config = webpki.map_err(|e| e.to_string()).and_then(|webpki| {
let verifier = Verifier {
webpki,
provider: provider.clone(),
pinned,
rejected: rejected.clone(),
};
rustls::ClientConfig::builder_with_provider(provider)
.with_safe_default_protocol_versions()
.map(|b| {
b.dangerous()
.with_custom_certificate_verifier(Arc::new(verifier))
.with_no_client_auth()
})
.map_err(|e| e.to_string())
});
match config {
Ok(config) => agent = agent.tls_config(Arc::new(config)),
Err(err) => eprintln!("[Nextcloud] TLS-Einrichtung fehlgeschlagen: {err}"),
}
Self {
agent: agent.build(),
rejected,
user_agent: format!("ink-notes ({})", crate::storage::device_name()),
}
}
fn request(&self, method: &str, url: &str) -> ureq::Request {
if let Ok(mut slot) = self.rejected.lock() {
*slot = None;
}
self.agent
.request(method, url)
.set("User-Agent", &self.user_agent)
}
/// Turns a failed request into an [`Error`].
fn error(&self, err: ureq::Error) -> Error {
match err {
ureq::Error::Status(401, _) => Error::Unauthorized,
ureq::Error::Status(404, _) => Error::NotFound,
ureq::Error::Status(412, _) => Error::PreconditionFailed,
ureq::Error::Status(code, resp) => {
let location = resp.header("Location").map(str::to_string);
match location {
Some(to) if (300..400).contains(&code) => Error::Other(t!(
"Der Server leitet nach {to} weiter – bitte diese Adresse verwenden",
to = to
)),
_ => Error::Other(t!("Server-Fehler {code}", code = code)),
}
}
ureq::Error::Transport(transport) => {
if let Some(print) = self.rejected.lock().ok().and_then(|s| s.clone()) {
return Error::Untrusted { fingerprint: print };
}
Error::Offline(transport.to_string())
}
}
}
}
/// Checks that a Nextcloud server answers at `server`.
pub fn check_server(client: &Client, server: &str) -> Result<(), Error> {
let resp = client
.request("GET", &format!("{server}/status.php"))
.call()
.map_err(|e| client.error(e))?;
let status: serde_json::Value = resp.into_json().map_err(|_| not_nextcloud())?;
if status.get("installed").and_then(|v| v.as_bool()) == Some(true) {
Ok(())
} else {
Err(not_nextcloud())
}
}
fn not_nextcloud() -> Error {
Error::Other(t!("Unter dieser Adresse antwortet kein Nextcloud-Server").to_string())
}
/// A started sign-in: the user approves it in the browser, the app polls for the result.
#[derive(Debug, Clone, PartialEq)]
pub struct LoginFlow {
pub login_url: String,
poll_url: String,
token: String,
}
/// Puts a link the server built with its own idea of its address (wrong behind a proxy) onto
/// the address the user gave.
fn onto_server(link: &str, server: &str, marker: &str) -> String {
let same_origin = match (url::Url::parse(link), url::Url::parse(server)) {
(Ok(a), Ok(b)) => a.origin() == b.origin(),
_ => false,
};
match link.find(marker) {
Some(pos) if !same_origin => format!("{server}/index.php{}", &link[pos..]),
_ => link.to_string(),
}
}
/// Starts Login Flow v2; the app password gets the device name as its name.
pub fn start_login(client: &Client, server: &str) -> Result<LoginFlow, Error> {
#[derive(Deserialize)]
struct Poll {
token: String,
endpoint: String,
}
#[derive(Deserialize)]
struct Start {
poll: Poll,
login: String,
}
let resp = client
.request("POST", &format!("{server}/index.php/login/v2"))
.call()
.map_err(|e| client.error(e))?;
let start: Start = resp.into_json().map_err(|_| not_nextcloud())?;
Ok(LoginFlow {
login_url: onto_server(&start.login, server, "/login/v2"),
poll_url: onto_server(&start.poll.endpoint, server, "/login/v2"),
token: start.poll.token,
})
}
/// Login name and app password once the user has approved the sign-in.
pub fn poll_login(client: &Client, flow: &LoginFlow) -> Result<Option<(String, String)>, Error> {
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct Done {
login_name: String,
app_password: String,
}
match client
.request("POST", &flow.poll_url)
.send_form(&[("token", flow.token.as_str())])
{
Ok(resp) => {
let done: Done = resp.into_json().map_err(|e| Error::Other(e.to_string()))?;
Ok(Some((done.login_name, done.app_password)))
}
// Not approved yet
Err(ureq::Error::Status(404, _)) => Ok(None),
Err(err) => Err(client.error(err)),
}
}
fn basic_auth(login_name: &str, password: &str) -> String {
let raw = format!("{login_name}:{password}");
format!(
"Basic {}",
base64::engine::general_purpose::STANDARD.encode(raw)
)
}
/// User id of the signed-in user (the name in WebDAV paths).
pub fn user_id(
client: &Client,
server: &str,
login_name: &str,
password: &str,
) -> Result<String, Error> {
let resp = client
.request(
"GET",
&format!("{server}/ocs/v2.php/cloud/user?format=json"),
)
.set("OCS-APIRequest", "true")
.set("Authorization", &basic_auth(login_name, password))
.call()
.map_err(|e| client.error(e))?;
let value: serde_json::Value = resp.into_json().map_err(|e| Error::Other(e.to_string()))?;
value["ocs"]["data"]["id"]
.as_str()
.map(str::to_string)
.ok_or_else(|| Error::Other(t!("Unerwartete Antwort des Servers").to_string()))
}
/// Revokes the app password on the server (when disconnecting).
pub fn revoke(account: &Account) -> Result<(), Error> {
let client = Client::new(account.pinned_cert.clone());
client
.request(
"DELETE",
&format!("{}/ocs/v2.php/core/apppassword", account.server),
)
.set("OCS-APIRequest", "true")
.set(
"Authorization",
&basic_auth(&account.login_name, &account.app_password),
)
.call()
.map(|_| ())
.map_err(|e| client.error(e))
}
/// One entry of a folder listing on the server.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Entry {
/// Path relative to the synced folder, `/`-separated
pub path: String,
pub is_dir: bool,
pub etag: String,
}
/// How an upload must find the file on the server.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Expect<'a> {
/// Must not exist yet
New,
/// Must still have this ETag
Etag(&'a str),
}
/// The folder `ink-notes` on the server, as the sync sees it.
pub trait Remote {
/// Entries directly in `dir` (`""` is the synced folder); `None` if it doesn't exist.
fn list(&mut self, dir: &str) -> Result<Option<Vec<Entry>>, Error>;
/// Content and ETag of a file.
fn get(&mut self, path: &str) -> Result<(Vec<u8>, String), Error>;
/// Uploads a file and returns its new ETag.
fn put(&mut self, path: &str, data: &[u8], expect: Expect<'_>) -> Result<String, Error>;
/// Creates a folder (fine if it exists).
fn mkdir(&mut self, path: &str) -> Result<(), Error>;
/// Deletes a file or folder (into the server's trash bin); with an ETag only if unchanged.
fn delete(&mut self, path: &str, etag: Option<&str>) -> Result<(), Error>;
}
/// [`Remote`] over WebDAV.
pub struct WebDav {
client: Client,
/// `…/remote.php/dav/files/<user>/ink-notes/`
root: url::Url,
auth: String,
}
impl WebDav {
pub fn new(account: &Account) -> Result<Self, Error> {
Self::with_folder(account, REMOTE_FOLDER)
}
/// Like [`Self::new`], with another folder (for tests against a real server).
pub fn with_folder(account: &Account, folder: &str) -> Result<Self, Error> {
let mut root = url::Url::parse(&format!("{}/", account.server))
.map_err(|e| Error::Other(e.to_string()))?;
root.path_segments_mut()
.map_err(|_| Error::Other(account.server.clone()))?
.pop_if_empty()
.extend(["remote.php", "dav", "files", &account.user_id, folder, ""]);
Ok(Self {
client: Client::new(account.pinned_cert.clone()),
root,
auth: basic_auth(&account.login_name, &account.app_password),
})
}
fn url(&self, path: &str, dir: bool) -> String {
let mut url = self.root.clone();
if let Ok(mut segments) = url.path_segments_mut() {
segments.pop_if_empty();
if !path.is_empty() {
segments.extend(path.split('/'));
}
if dir {
segments.push("");
}
}
url.to_string()
}
fn request(&self, method: &str, url: &str) -> ureq::Request {
self.client
.request(method, url)
.set("Authorization", &self.auth)
}
}
/// Decodes `%xx` escapes of a URL path.
fn percent_decode(s: &str) -> String {
let bytes = s.as_bytes();
let mut out = Vec::with_capacity(bytes.len());
let mut i = 0;
while i < bytes.len() {
if bytes[i] == b'%' && i + 2 < bytes.len() {
let hex = std::str::from_utf8(&bytes[i + 1..i + 3]).unwrap_or("");
if let Ok(b) = u8::from_str_radix(hex, 16) {
out.push(b);
i += 3;
continue;
}
}
out.push(bytes[i]);
i += 1;
}
String::from_utf8_lossy(&out).into_owned()
}
/// Entries of a PROPFIND answer: (decoded href path, is folder, ETag).
pub fn parse_multistatus(xml: &str) -> Result<Vec<(String, bool, String)>, String> {
use quick_xml::events::Event;
let mut reader = quick_xml::Reader::from_str(xml);
let mut entries = Vec::new();
let mut href = String::new();
let mut etag = String::new();
let mut is_dir = false;
let mut ok = false;
let mut in_tag: Option<&'static str> = None;
loop {
match reader.read_event().map_err(|e| e.to_string())? {
Event::Start(e) => {
let name = e.local_name();
match name.as_ref() {
b"response" => {
href.clear();
etag.clear();
is_dir = false;
ok = false;
}
b"href" => in_tag = Some("href"),
b"getetag" => in_tag = Some("etag"),
b"status" => in_tag = Some("status"),
b"collection" => is_dir = true,
_ => {}
}
}
Event::Empty(e) => {
if e.local_name().as_ref() == b"collection" {
is_dir = true;
}
}
event @ (Event::Text(_) | Event::GeneralRef(_)) => {
let text = match event {
Event::Text(t) => t.decode().map_err(|e| e.to_string())?.into_owned(),
Event::GeneralRef(r) => match r.resolve_char_ref() {
Ok(Some(c)) => c.to_string(),
_ => r
.decode()
.ok()
.and_then(|name| quick_xml::escape::resolve_predefined_entity(&name))
.unwrap_or_default()
.to_string(),
},
_ => String::new(),
};
match in_tag {
Some("href") => href.push_str(&text),
Some("etag") => etag.push_str(&text),
Some("status") => ok |= text.contains(" 200 "),
_ => {}
}
}
Event::End(e) => {
let name = e.local_name();
match name.as_ref() {
b"href" | b"getetag" | b"status" => in_tag = None,
b"response" if ok => {
entries.push((percent_decode(href.trim()), is_dir, etag.trim().to_string()))
}
_ => {}
}
}
Event::Eof => break,
_ => {}
}
}
Ok(entries)
}
/// ETag of a file in an answer, as PROPFIND lists it. Nextcloud's own `OC-ETag` is left alone
/// by the web server; a plain `ETag` may carry a mark of the compression.
fn etag_of(resp: &ureq::Response) -> Option<String> {
if let Some(etag) = resp.header("OC-ETag") {
return Some(etag.to_string());
}
let etag = resp.header("ETag")?;
let plain = etag.strip_suffix('"').and_then(|unquoted| {
["-gzip", "-br", "-zstd", "-df"]
.iter()
.find_map(|mark| unquoted.strip_suffix(mark))
});
Some(plain.map_or(etag.to_string(), |tag| format!("{tag}\"")))
}
const PROPFIND_BODY: &str = r#"<?xml version="1.0"?><d:propfind xmlns:d="DAV:"><d:prop><d:getetag/><d:resourcetype/></d:prop></d:propfind>"#;
impl Remote for WebDav {
fn list(&mut self, dir: &str) -> Result<Option<Vec<Entry>>, Error> {
let url = self.url(dir, true);
let resp = match self
.request("PROPFIND", &url)
.set("Depth", "1")
.set("Content-Type", "application/xml; charset=utf-8")
.send_string(PROPFIND_BODY)
{
Ok(resp) => resp,
Err(ureq::Error::Status(404, _)) => return Ok(None),
Err(err) => return Err(self.client.error(err)),
};
let body = resp
.into_string()
.map_err(|e| Error::Offline(e.to_string()))?;
let root_path = percent_decode(self.root.path());
let entries = parse_multistatus(&body).map_err(Error::Other)?;
Ok(Some(
entries
.into_iter()
.filter_map(|(href, is_dir, etag)| {
// Hrefs are paths, sometimes full URLs
let path = match href.find("/remote.php/") {
Some(pos) if href.contains("://") => href[pos..].to_string(),
_ => href,
};
let path = path.strip_prefix(&root_path)?.trim_end_matches('/');
(path != dir && !path.is_empty()).then(|| Entry {
path: path.to_string(),
is_dir,
etag,
})
})
.collect(),
))
}
fn get(&mut self, path: &str) -> Result<(Vec<u8>, String), Error> {
// Uncompressed: Apache marks the ETag of a compressed answer (`"…-gzip"`)
let resp = self
.request("GET", &self.url(path, false))
.set("Accept-Encoding", "identity")
.call()
.map_err(|e| self.client.error(e))?;
let etag = etag_of(&resp).unwrap_or_default();
let mut data = Vec::new();
resp.into_reader()
.take(MAX_DOWNLOAD)
.read_to_end(&mut data)
.map_err(|e| Error::Offline(e.to_string()))?;
Ok((data, etag))
}
fn put(&mut self, path: &str, data: &[u8], expect: Expect<'_>) -> Result<String, Error> {
let mut req = self.request("PUT", &self.url(path, false));
req = match expect {
Expect::New => req.set("If-None-Match", "*"),
Expect::Etag(etag) => req.set("If-Match", etag),
};
let resp = req.send_bytes(data).map_err(|e| self.client.error(e))?;
match etag_of(&resp) {
Some(etag) => Ok(etag),
None => {
// Look it up when the server didn't say
let parent = path.rsplit_once('/').map_or("", |(p, _)| p);
self.list(parent)?
.unwrap_or_default()
.into_iter()
.find(|e| e.path == path)
.map(|e| e.etag)
.ok_or(Error::NotFound)
}
}
}
fn mkdir(&mut self, path: &str) -> Result<(), Error> {
match self.request("MKCOL", &self.url(path, true)).call() {
Ok(_) | Err(ureq::Error::Status(405, _)) => Ok(()),
Err(err) => Err(self.client.error(err)),
}
}
fn delete(&mut self, path: &str, etag: Option<&str>) -> Result<(), Error> {
let mut req = self.request("DELETE", &self.url(path, false));
if let Some(etag) = etag {
req = req.set("If-Match", etag);
}
match req.call() {
Ok(_) | Err(ureq::Error::Status(404, _)) => Ok(()),
Err(err) => Err(self.client.error(err)),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn normalizes_server_addresses() {
assert_eq!(
normalize_server("cloud.example.org").unwrap(),
"https://cloud.example.org"
);
assert_eq!(
normalize_server(" https://example.org/nextcloud/ ").unwrap(),
"https://example.org/nextcloud"
);
assert_eq!(
normalize_server("https://example.org/nextcloud/index.php/apps/files/?dir=/").unwrap(),
"https://example.org/nextcloud"
);
assert_eq!(
normalize_server("http://192.168.1.5:8080/apps/dashboard/").unwrap(),
"http://192.168.1.5:8080"
);
assert!(normalize_server("").is_err());
assert!(normalize_server("ftp://example.org").is_err());
}
#[test]
fn moves_links_onto_the_given_server() {
let server = "https://cloud.example.org/nc";
assert_eq!(
onto_server("http://localhost/login/v2/poll", server, "/login/v2"),
"https://cloud.example.org/nc/index.php/login/v2/poll"
);
let same = "https://cloud.example.org/nc/login/v2/flow/abc";
assert_eq!(onto_server(same, server, "/login/v2"), same);
}
#[test]
fn parses_propfind_answers() {
let xml = r#"<?xml version="1.0"?>
<d:multistatus xmlns:d="DAV:"><d:response><d:href>/remote.php/dav/files/pia/ink-notes/</d:href><d:propstat><d:prop><d:getetag>&quot;abc&quot;</d:getetag><d:resourcetype><d:collection/></d:resourcetype></d:prop><d:status>HTTP/1.1 200 OK</d:status></d:propstat></d:response>
<d:response><d:href>/remote.php/dav/files/pia/ink-notes/library.json</d:href><d:propstat><d:prop><d:getetag>&quot;e1&quot;</d:getetag><d:resourcetype/></d:prop><d:status>HTTP/1.1 200 OK</d:status></d:propstat></d:response>
<d:response><d:href>/remote.php/dav/files/pia/ink-notes/Mathe%20%C3%BC/</d:href><d:propstat><d:prop><d:getetag>"e2"</d:getetag><d:resourcetype><d:collection/></d:resourcetype></d:prop><d:status>HTTP/1.1 200 OK</d:status></d:propstat></d:response></d:multistatus>"#;
let entries = parse_multistatus(xml).unwrap();
assert_eq!(
entries,
vec![
(
"/remote.php/dav/files/pia/ink-notes/".to_string(),
true,
"\"abc\"".to_string()
),
(
"/remote.php/dav/files/pia/ink-notes/library.json".to_string(),
false,
"\"e1\"".to_string()
),
(
"/remote.php/dav/files/pia/ink-notes/Mathe ü/".to_string(),
true,
"\"e2\"".to_string()
),
]
);
}
#[test]
fn builds_webdav_urls() {
let account = Account {
server: "https://example.org/nc".into(),
login_name: "pia@example.org".into(),
user_id: "pia".into(),
app_password: "x".into(),
pinned_cert: None,
};
let dav = WebDav::new(&account).unwrap();
assert_eq!(
dav.url("nb 1/page_1.json", false),
"https://example.org/nc/remote.php/dav/files/pia/ink-notes/nb%201/page_1.json"
);
assert_eq!(
dav.url("", true),
"https://example.org/nc/remote.php/dav/files/pia/ink-notes/"
);
assert_eq!(account.label(), "pia@example.org@example.org/nc");
}
#[test]
fn account_round_trip() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("cfg").join("nextcloud.json");
let account = Account {
server: "https://example.org".into(),
login_name: "pia".into(),
user_id: "pia".into(),
app_password: "secret".into(),
pinned_cert: Some("AA:BB".into()),
};
account.save_to(&path).unwrap();
assert_eq!(Account::load_from(&path), Some(account));
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
let mode = std::fs::metadata(&path).unwrap().permissions().mode();
assert_eq!(mode & 0o777, 0o600);
}
}
}
+38 -10
View File
@@ -40,8 +40,12 @@ pub const MAX_IMPORT_PAGES: usize = 2000;
/// Öffnet ein PDF und liefert die angezeigte Größe jeder Seite in Punkt (Drehung
/// berücksichtigt).
pub fn page_sizes(bytes: Vec<u8>) -> Result<Vec<(f32, f32)>, String> {
let pdf = open(bytes)?;
let sizes: Vec<(f32, f32)> = pdf.pages().iter().map(|p| p.render_dimensions()).collect();
// Runs on the main thread during import: a panic in the PDF library must not take the
// app down
let sizes: Vec<(f32, f32)> = guarded(|| {
let pdf = open(bytes)?;
Ok(pdf.pages().iter().map(|p| p.render_dimensions()).collect())
})?;
if sizes.is_empty() {
return Err(t!("Das PDF enthält keine Seiten").into());
}
@@ -55,6 +59,13 @@ pub fn page_sizes(bytes: Vec<u8>) -> Result<Vec<(f32, f32)>, String> {
Ok(sizes)
}
/// Runs PDF code and turns a panic of the PDF library (malformed files) into an error.
fn guarded<T>(work: impl FnOnce() -> Result<T, String>) -> Result<T, String> {
std::panic::catch_unwind(std::panic::AssertUnwindSafe(work)).unwrap_or_else(|_| {
Err(t!("Das PDF kann nicht verarbeitet werden (interner Fehler)").to_string())
})
}
fn open(bytes: Vec<u8>) -> Result<Pdf, String> {
Pdf::new(bytes).map_err(|e| match e {
LoadPdfError::Decryption(_) => {
@@ -209,21 +220,31 @@ impl PdfRenderService {
queue = ready.wait(queue).unwrap_or_else(|e| e.into_inner());
}
};
// A panic here would end the thread and with it every PDF page until
// the next start, so it only fails the page at hand
if open_pdf.as_ref().map(|(p, _)| p) != Some(&request.path) {
open_pdf = std::fs::read(&request.path)
.map_err(|e| e.to_string())
.and_then(open)
.map_err(|e| eprintln!("[PDF] '{}': {e}", request.path.display()))
.ok()
.map(|pdf| (request.path.clone(), pdf));
open_pdf = guarded(|| {
std::fs::read(&request.path)
.map_err(|e| e.to_string())
.and_then(open)
})
.map_err(|e| eprintln!("[PDF] '{}': {e}", request.path.display()))
.ok()
.map(|pdf| (request.path.clone(), pdf));
}
let result = match &open_pdf {
// Mipmaps hier statt im Haupt-Thread: sonst stockt das Scrollen bei
// jeder neu gerenderten Seite (Issue #28)
Some((_, pdf)) => render_page(pdf, request.page, request.max_pixels)
.map(crate::image_layer::mip_chain),
Some((_, pdf)) => guarded(|| {
render_page(pdf, request.page, request.max_pixels)
.map(crate::image_layer::mip_chain)
}),
None => Err(t!("PDF kann nicht geöffnet werden").into()),
};
if result.is_err() {
// The document may be in a broken state after a panic
open_pdf = None;
}
if res_tx.send((request.key, result)).is_err() {
return;
}
@@ -356,6 +377,13 @@ impl Drop for PdfRenderService {
pub(crate) mod tests {
use super::*;
#[test]
fn test_panic_in_pdf_code_becomes_an_error() {
let result: Result<(), String> = guarded(|| panic!("simulated panic in the PDF library"));
assert!(result.is_err());
assert_eq!(guarded(|| Ok(7)), Ok(7));
}
/// Minimales gültiges PDF mit Seiten der angegebenen Größen (Punkt); jede Seite zeigt ein
/// rotes Rechteck in der linken oberen Ecke. Optional mit `/Rotate`.
pub(crate) fn sample_pdf(pages: &[(f32, f32, i32)]) -> Vec<u8> {
+18
View File
@@ -143,6 +143,20 @@ pub fn android_app() -> Option<winit::platform::android::activity::AndroidApp> {
ANDROID_APP.get().cloned()
}
/// Window edges (left, top, right, bottom; physical pixels) covered by system bars or display
/// cutouts. Android: the app draws below the status and navigation bars, so its buttons must
/// keep out of them (Issue #50). Desktop: none.
pub fn safe_area_insets() -> [f32; 4] {
#[cfg(target_os = "android")]
{
crate::android_bridge::insets()
}
#[cfg(not(target_os = "android"))]
{
[0.0; 4]
}
}
/// Makes an exported file available to the user. Desktop: it already is (the path is
/// returned). Android: copied to `Download/ink-notes` through the MediaStore (Android 10 and
/// newer), as other apps can't see the app's own folder; older versions keep it there.
@@ -386,6 +400,10 @@ pub mod clipboard {
pub fn set_image(&mut self, _image: ImageData) -> Result<(), &'static str> {
Err("not available on Android")
}
pub fn set_text(&mut self, _text: String) -> Result<(), &'static str> {
Err("not available on Android")
}
}
}
}
+18 -1
View File
@@ -561,7 +561,8 @@ fn convert(doc: &RnoteDocument, notebook_dir: &Path) -> Imported {
let scale = scale_pt * (72.0 / doc.dpi) as f32;
// Rnote grows documents page by page; tolerate rounding in the stored size
let count = |length: f64, page: f64| ((length / page - 0.01).ceil() as usize).max(1);
let columns = count(doc.size.x, doc.format.x);
// A broken document size must not ask for millions of pages
let columns = count(doc.size.x, doc.format.x).min(MAX_PAGES);
let rows = count(doc.size.y, doc.format.y);
let rows = rows.min((MAX_PAGES / columns).max(1));
let targets = (0..rows * columns)
@@ -889,6 +890,22 @@ mod tests {
assert!(second.strokes[1].is_image());
}
#[test]
fn test_broken_document_size_is_capped() {
let dir = tempfile::tempdir().unwrap();
let doc = RnoteDocument {
format: DVec2::new(800.0, 1100.0),
dpi: 96.0,
origin: DVec2::ZERO,
size: DVec2::new(1e18, 1e18),
endless: false,
background: PageBackground::Blank,
items: Vec::new(),
};
let imported = convert(&doc, dir.path());
assert!(imported.pages.len() <= MAX_PAGES);
}
#[test]
fn test_infinite_documents_become_endless() {
let temp_dir = tempfile::tempdir().unwrap();
+148 -13
View File
@@ -100,10 +100,16 @@ impl PenPreset {
}
}
/// Anzahl der Stifte in der Werkzeugleiste.
pub const PEN_PRESET_COUNT: usize = 4;
/// Anzahl der Stifte in der Werkzeugleiste (Issue #56: seven).
pub const PEN_PRESET_COUNT: usize = 7;
/// Standardstifte: Schwarz, Blau und Rot fein, Grün etwas kräftiger.
/// Pens stored in `pen_presets`. Versions before 0.44 expect exactly four there and would
/// reset all settings for any other length, so the further pens go to `more_pen_presets` and a
/// rollback keeps the first four.
pub const STORED_PEN_PRESETS: usize = 4;
/// Standardstifte: Schwarz, Blau und Rot fein, Grün etwas kräftiger, dazu Violett, Orange und
/// Grau.
pub const DEFAULT_PEN_PRESETS: [PenPreset; PEN_PRESET_COUNT] = [
PenPreset {
color: [0.12, 0.12, 0.14, 1.0],
@@ -121,6 +127,18 @@ pub const DEFAULT_PEN_PRESETS: [PenPreset; PEN_PRESET_COUNT] = [
color: [0.18, 0.65, 0.35, 1.0],
width_mm: 0.8,
},
PenPreset {
color: [0.55, 0.25, 0.75, 1.0],
width_mm: 0.5,
},
PenPreset {
color: [0.95, 0.55, 0.10, 1.0],
width_mm: 0.5,
},
PenPreset {
color: [0.45, 0.45, 0.48, 1.0],
width_mm: 0.5,
},
];
/// Farben des Textmarkers in der Werkzeugleiste (halbtransparent).
@@ -199,6 +217,9 @@ pub struct StylusButton {
/// Höchstens so viele Stifttasten werden gemerkt.
pub const MAX_STYLUS_BUTTONS: usize = 4;
/// How many own colors fit under the presets of the color popovers (two rows)
pub const MAX_SAVED_COLORS: usize = 12;
/// Benutzereinstellungen der Oberfläche.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(default)]
@@ -215,8 +236,10 @@ pub struct UiSettings {
/// Frühere globale Einstellung „Seiten durchgehend scrollen“: gilt nur noch für Notizbücher
/// ohne eigene Anordnung im Manifest (vor 0.28 angelegt), wird nicht mehr geändert
pub continuous_scroll: bool,
/// Stifte der Werkzeugleiste und der gewählte davon
pub pen_presets: [PenPreset; PEN_PRESET_COUNT],
/// Stifte der Werkzeugleiste und der gewählte davon: the first four here, the others in
/// `more_pen_presets` (see [`STORED_PEN_PRESETS`]); use [`Self::pens`]
pub pen_presets: [PenPreset; STORED_PEN_PRESETS],
pub more_pen_presets: Vec<PenPreset>,
pub active_pen: usize,
/// Eigene Farben von Textmarker und Text-Werkzeug und die gewählte davon
pub highlighter_colors: [[f32; 4]; 4],
@@ -232,6 +255,13 @@ pub struct UiSettings {
/// Folder of the notebooks, e.g. inside a Nextcloud folder; default location if unset
/// (Issue #49)
pub storage_dir: Option<PathBuf>,
/// Compute handwritten math after `=` (Issue #46)
pub math_assist: bool,
/// One finger draws instead of moving the page. On by default on Android, where phones
/// usually have no pen (Issue #50)
pub touch_draw: bool,
/// Own colors under the presets of the color popovers (Issue #56)
pub saved_colors: Vec<[f32; 3]>,
}
impl Default for UiSettings {
@@ -243,7 +273,13 @@ impl Default for UiSettings {
pattern_color: PatternColor::default(),
pressure_curve: PressureCurve::default(),
continuous_scroll: true,
pen_presets: DEFAULT_PEN_PRESETS,
pen_presets: [
DEFAULT_PEN_PRESETS[0],
DEFAULT_PEN_PRESETS[1],
DEFAULT_PEN_PRESETS[2],
DEFAULT_PEN_PRESETS[3],
],
more_pen_presets: DEFAULT_PEN_PRESETS[STORED_PEN_PRESETS..].to_vec(),
active_pen: 0,
highlighter_colors: DEFAULT_HIGHLIGHTER_COLORS,
active_highlighter: 0,
@@ -253,6 +289,9 @@ impl Default for UiSettings {
language: None,
eraser_radius_mm: 3.0,
storage_dir: None,
math_assist: false,
touch_draw: cfg!(target_os = "android"),
saved_colors: Vec::new(),
}
}
}
@@ -263,22 +302,45 @@ impl UiSettings {
crate::paths::config_dir().map(|d| d.join("ui.json"))
}
/// Lädt die Einstellungen; bei fehlender oder beschädigter Datei gelten Standardwerte.
/// Keeps every setting of a partly invalid file that can still be read; only the
/// unreadable ones get their default. One bad value must not reset everything, least of
/// all the storage location.
fn salvage(content: &str) -> Self {
let Ok(serde_json::Value::Object(file)) = serde_json::from_str(content) else {
return Self::default();
};
let mut good = serde_json::Map::new();
for (key, value) in file {
good.insert(key.clone(), value);
let candidate = serde_json::Value::Object(good.clone());
if serde_json::from_value::<Self>(candidate).is_err() {
eprintln!("[Einstellungen] '{key}' ist ungültig, verwende Standardwert.");
good.remove(&key);
}
}
serde_json::from_value(serde_json::Value::Object(good)).unwrap_or_default()
}
/// Lädt die Einstellungen; bei fehlender Datei gelten Standardwerte, bei einer teilweise
/// ungültigen Datei nur für die unlesbaren Werte.
pub fn load_from(path: &Path) -> Self {
let mut settings: Self = match std::fs::read_to_string(path) {
Ok(content) => serde_json::from_str(&content).unwrap_or_else(|e| {
eprintln!(
"[Einstellungen] '{}' ist ungültig ({e}), verwende Standardwerte.",
"[Einstellungen] '{}' ist ungültig ({e}), unlesbare Werte werden ersetzt.",
path.display()
);
Self::default()
Self::salvage(&content)
}),
Err(_) => Self::default(),
};
settings.pressure_curve = settings.pressure_curve.sanitized();
for (preset, fallback) in settings.pen_presets.iter_mut().zip(DEFAULT_PEN_PRESETS) {
*preset = preset.sanitized(fallback);
let pens = settings.pens();
let mut sanitized = DEFAULT_PEN_PRESETS;
for ((preset, pen), fallback) in sanitized.iter_mut().zip(pens).zip(DEFAULT_PEN_PRESETS) {
*preset = pen.sanitized(fallback);
}
settings.set_pens(sanitized);
settings.active_pen = settings.active_pen.min(PEN_PRESET_COUNT - 1);
for (color, fallback) in settings
.highlighter_colors
@@ -297,9 +359,31 @@ impl UiSettings {
.active_text_color
.min(settings.text_colors.len() - 1);
settings.stylus_buttons.truncate(MAX_STYLUS_BUTTONS);
settings.saved_colors.truncate(MAX_SAVED_COLORS);
for rgb in &mut settings.saved_colors {
let [r, g, b, _] = sanitized_color([rgb[0], rgb[1], rgb[2], 1.0], [0.0, 0.0, 0.0, 1.0]);
*rgb = [r, g, b];
}
settings
}
/// All pens of the toolbar; pens missing in the file (older versions had four) are the
/// default pens.
pub fn pens(&self) -> [PenPreset; PEN_PRESET_COUNT] {
let mut pens = DEFAULT_PEN_PRESETS;
let stored = self.pen_presets.iter().chain(&self.more_pen_presets);
for (pen, stored) in pens.iter_mut().zip(stored) {
*pen = *stored;
}
pens
}
pub fn set_pens(&mut self, pens: [PenPreset; PEN_PRESET_COUNT]) {
self.pen_presets
.copy_from_slice(&pens[..STORED_PEN_PRESETS]);
self.more_pen_presets = pens[STORED_PEN_PRESETS..].to_vec();
}
/// Speichert die Einstellungen atomar.
pub fn save_to(&self, path: &Path) -> Result<(), StorageError> {
storage::atomic_write_json(path, self)
@@ -331,7 +415,14 @@ mod tests {
pen_presets: [PenPreset {
color: [0.5, 0.2, 0.7, 1.0],
width_mm: 1.5,
}; PEN_PRESET_COUNT],
}; STORED_PEN_PRESETS],
more_pen_presets: vec![
PenPreset {
color: [0.1, 0.6, 0.7, 1.0],
width_mm: 2.0,
};
PEN_PRESET_COUNT - STORED_PEN_PRESETS
],
active_pen: 2,
highlighter_colors: [[0.9, 0.5, 0.1, 0.35]; 4],
active_highlighter: 3,
@@ -350,6 +441,9 @@ mod tests {
language: Some(Language::English),
eraser_radius_mm: 6.0,
storage_dir: Some(PathBuf::from("/home/pia/Nextcloud/ink-notes")),
math_assist: true,
touch_draw: true,
saved_colors: vec![[0.2, 0.4, 0.6], [1.0, 0.5, 0.0]],
};
settings.save_to(&path).unwrap();
assert_eq!(UiSettings::load_from(&path), settings);
@@ -363,6 +457,45 @@ mod tests {
assert_eq!(UiSettings::load_from(&path), UiSettings::default());
}
#[test]
fn test_one_invalid_value_does_not_reset_the_other_settings() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("ui.json");
std::fs::write(
&path,
br#"{"storage_dir": "/home/pia/Nextcloud/ink-notes", "eraser_radius_mm": "big", "math_assist": true}"#,
)
.unwrap();
let loaded = UiSettings::load_from(&path);
assert_eq!(
loaded.storage_dir,
Some(PathBuf::from("/home/pia/Nextcloud/ink-notes"))
);
assert!(loaded.math_assist);
assert_eq!(
loaded.eraser_radius_mm,
UiSettings::default().eraser_radius_mm
);
}
#[test]
fn test_seven_pens_stay_readable_for_older_versions() {
// Versions before 0.44 read `pen_presets` as exactly four pens (Issue #56)
#[derive(Deserialize)]
struct OldSettings {
pen_presets: [PenPreset; 4],
}
let mut settings = UiSettings::default();
let mut pens = DEFAULT_PEN_PRESETS;
pens[6].color = [0.3, 0.3, 0.9, 1.0];
settings.set_pens(pens);
let json = serde_json::to_string(&settings).unwrap();
let old: OldSettings = serde_json::from_str(&json).unwrap();
assert_eq!(old.pen_presets[..], DEFAULT_PEN_PRESETS[..4]);
let back: UiSettings = serde_json::from_str(&json).unwrap();
assert_eq!(back.pens(), pens);
}
#[test]
fn test_old_settings_file_keeps_new_defaults() {
// ui.json aus v0.3.x kennt nur die Seite der Werkzeugleiste
@@ -388,7 +521,7 @@ mod tests {
assert_eq!(curve.threshold, 0.0);
// Ohne Stifte: die Standardstifte; unsinnige Stifte werden begrenzt
assert_eq!(settings.pen_presets, DEFAULT_PEN_PRESETS);
assert_eq!(settings.pens(), DEFAULT_PEN_PRESETS);
assert_eq!(settings.active_pen, 0);
std::fs::write(
&path,
@@ -405,6 +538,8 @@ mod tests {
assert_eq!(settings.pen_presets[0].color, [1.0, 0.0, 0.5, 1.0]);
assert_eq!(settings.pen_presets[0].width_mm, 5.0);
assert_eq!(settings.pen_presets[1].width_mm, 0.15);
// Files from before 0.44 have four pens: the others are the default pens
assert_eq!(settings.pens()[4..], DEFAULT_PEN_PRESETS[4..]);
// Eigene Farben: Textmarker bleibt halbtransparent, Text deckend
std::fs::write(
+313 -28
View File
@@ -555,8 +555,12 @@ pub fn atomic_write_file(path: &Path, content: &[u8]) -> Result<(), io::Error> {
use std::os::unix::fs::PermissionsExt;
let _ = fs::set_permissions(&temp_path, fs::Permissions::from_mode(0o600));
}
file.write_all(content)?;
file.sync_all()?;
// On a full disk every retry would otherwise leave another partial file behind
if let Err(e) = file.write_all(content).and_then(|_| file.sync_all()) {
drop(file);
let _ = fs::remove_file(&temp_path);
return Err(e);
}
}
// Atomares Rename
@@ -630,8 +634,29 @@ fn load_manifest_from_file(path: &Path) -> Result<NotebookManifest, StorageError
}
/// Lädt das Manifest aus `manifest.json`.
/// Fällt bei Fehlern/Beschädigung automatisch auf `manifest.json.bak` zurück.
/// Fällt bei Beschädigung automatisch auf `manifest.json.bak` zurück.
///
/// The folder name is the identity of a notebook: everything opens, renames and deletes
/// `<base>/<id>`. A copied or renamed folder still carries the id of the original in its
/// manifest, so the id is always taken from the folder.
pub fn load_manifest(notebook_dir: &Path) -> Result<NotebookManifest, StorageError> {
let mut manifest = load_manifest_or_backup(notebook_dir)?;
// Canonical, so that a path like `.` still yields the folder name
let dir = notebook_dir.canonicalize();
let name = dir
.as_deref()
.unwrap_or(notebook_dir)
.file_name()
.and_then(|n| n.to_str());
if let Some(name) = name {
if manifest.id != name {
manifest.id = name.to_string();
}
}
Ok(manifest)
}
fn load_manifest_or_backup(notebook_dir: &Path) -> Result<NotebookManifest, StorageError> {
let path = notebook_dir.join("manifest.json");
if !path.exists() {
let bak_path = notebook_dir.join("manifest.json.bak");
@@ -647,6 +672,13 @@ pub fn load_manifest(notebook_dir: &Path) -> Result<NotebookManifest, StorageErr
match load_manifest_from_file(&path) {
Ok(manifest) => Ok(manifest),
// The backup only stands in for a damaged file. A manifest of a newer app version or
// one that cannot be read right now is intact: opening the older backup instead would
// let the next save overwrite it with stale content.
Err(e @ StorageError::UnsupportedVersion { .. }) => Err(e),
Err(StorageError::Io(e)) if e.kind() != io::ErrorKind::InvalidData => {
Err(StorageError::Io(e))
}
Err(e) => {
let bak_path = notebook_dir.join("manifest.json.bak");
if bak_path.exists() {
@@ -689,21 +721,96 @@ pub fn quarantine_corrupted_page(notebook_dir: &Path, page_id: u64) -> Result<Pa
}
/// Speichert eine Seite atomar in `page_<id>.json`.
///
/// The file may have been changed behind our back since this process last read or wrote it:
/// by the built-in sync or a sync client that brought a change from another device, before
/// `disk_watch` got to take it over (it waits while something is being drawn or is unsaved).
/// Overwriting it would silently drop that change. So, as the sync does for a page changed on
/// both sides, their version is kept as a conflict copy next to the page (`disk_watch` turns
/// it into a page of its own); if we have nothing new ourselves, their version simply stays.
pub fn save_page(notebook_dir: &Path, page: &PageData) -> Result<(), StorageError> {
let path = notebook_dir.join(format!("page_{}.json", page.id));
if path.exists() {
let bak_path = notebook_dir.join(format!("page_{}.json.v0.bak", page.id));
if !bak_path.exists() {
if let Ok(content) = fs::read_to_string(&path) {
if let Ok(old_p) = serde_json::from_str::<PageData>(&content) {
if old_p.format_version == 0 {
let _ = fs::copy(&path, &bak_path);
}
}
let bytes = serde_json::to_vec(page)?;
let ours = file_hash(&bytes);
let key = seen_key(&path);
if let Ok(theirs) = fs::read(&path) {
let seen = seen_files().get(&key).copied();
let their_page = serde_json::from_slice::<PageData>(&theirs).ok();
if seen != Some(file_hash(&theirs)) && theirs != bytes {
if seen == Some(ours) {
// Nothing changed here since we last read or wrote the page
return Ok(());
}
let differs = their_page.as_ref().is_some_and(|their_page| {
let mut their_page = their_page.clone();
their_page.format_version = page.format_version;
their_page != *page
});
if differs {
// Fails rather than overwrites: the caller tries again
write_conflict_copy(notebook_dir, page.id, &theirs)?;
}
}
let bak_path = notebook_dir.join(format!("page_{}.json.v0.bak", page.id));
if !bak_path.exists() && their_page.is_some_and(|p| p.format_version == 0) {
let _ = fs::copy(&path, &bak_path);
}
}
atomic_write_json(&path, page)
atomic_write_file(&path, &bytes)?;
seen_files().insert(key, ours);
Ok(())
}
type FileHash = [u8; 32];
fn file_hash(bytes: &[u8]) -> FileHash {
use sha2::Digest;
sha2::Sha256::digest(bytes).into()
}
/// Content of the page files as this process last read or wrote them (see [`save_page`]).
fn seen_files() -> std::sync::MutexGuard<'static, std::collections::HashMap<PathBuf, FileHash>> {
static SEEN: std::sync::OnceLock<
std::sync::Mutex<std::collections::HashMap<PathBuf, FileHash>>,
> = std::sync::OnceLock::new();
SEEN.get_or_init(Default::default)
.lock()
.unwrap_or_else(|e| e.into_inner())
}
/// The same file must get the same key however its folder is spelled.
fn seen_key(path: &Path) -> PathBuf {
match (path.parent().map(Path::canonicalize), path.file_name()) {
(Some(Ok(dir)), Some(name)) => dir.join(name),
_ => path.to_path_buf(),
}
}
/// Keeps `content` as `page_<id> (conflict <unix time>).json`, the name the sync uses too.
fn write_conflict_copy(notebook_dir: &Path, page_id: u64, content: &[u8]) -> io::Result<PathBuf> {
let ts = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_secs();
let mut n = 0;
let path = loop {
let name = match n {
0 => format!("page_{page_id} (conflict {ts}).json"),
n => format!("page_{page_id} (conflict {ts}-{n}).json"),
};
let path = notebook_dir.join(name);
if !path.exists() {
break path;
}
n += 1;
};
atomic_write_file(&path, content)?;
eprintln!(
"[Storage] Seite {page_id} wurde außerhalb geändert, Konfliktkopie: {}",
path.display()
);
Ok(path)
}
/// Lädt eine Seite aus `page_<id>.json`.
@@ -735,6 +842,7 @@ pub fn load_page(notebook_dir: &Path, page_id: u64) -> Result<PageData, StorageE
});
}
seen_files().insert(seen_key(&path), file_hash(json_str.as_bytes()));
Ok(page_data)
}
@@ -817,8 +925,22 @@ pub fn load_notebook(notebook_dir: &Path) -> Result<(Notebook, NotebookManifest)
page.persist = PersistState::ReadOnlyAfterLoadError;
page
}
// Listed but never saved (still empty): an empty page, as with lazy loading. Nothing
// can be overwritten, there is no file.
Err(StorageError::PageNotFound(_)) => {
Page::new(first_id, first_bg).with_size(manifest.size_for_page(first_id))
}
Err(e) => {
eprintln!("[Storage] Konnte erste Seite {first_id} nicht laden: {e}. Erstelle leere Ersatzseite.");
// Without the warning the page looks writable while nothing on it is saved
load_warnings.insert(
first_id,
PageLoadWarning {
page_id: first_id,
message: e.to_string(),
quarantine_path: None,
},
);
let mut page =
Page::new(first_id, first_bg).with_size(manifest.size_for_page(first_id));
page.persist = PersistState::ReadOnlyAfterLoadError;
@@ -931,6 +1053,14 @@ pub fn ensure_page_loaded(
}
Err(e) => {
eprintln!("[Storage] Fehler beim Lazy Loading von Seite {page_id}: {e}");
notebook.load_warnings.insert(
page_id,
PageLoadWarning {
page_id,
message: e.to_string(),
quarantine_path: None,
},
);
notebook.pages[page_idx].is_loaded = true;
notebook.pages[page_idx].persist = PersistState::ReadOnlyAfterLoadError;
notebook.pages[page_idx].rebuild_cache_if_dirty();
@@ -1502,7 +1632,23 @@ pub fn restore_manifest_from_backup(notebook_dir: &Path) -> Result<(), StorageEr
}
}
/// Time an entry was moved into `.trash`, read from the `_<unix seconds>` suffix of its name
/// (`<id>_<ts>`, `sync_<ts>`).
fn trashed_at(name: &str) -> Option<std::time::SystemTime> {
let (_, ts) = name.rsplit_once('_')?;
if ts.is_empty() || !ts.bytes().all(|b| b.is_ascii_digit()) {
return None;
}
let secs = ts.parse::<u64>().ok()?;
std::time::UNIX_EPOCH.checked_add(std::time::Duration::from_secs(secs))
}
/// Entfernt Einträge aus `.trash`, die älter als `max_age` sind.
///
/// The age counts from the moment of deletion, which is the timestamp in the entry name.
/// The modification time of a folder survives the move into `.trash`, so it would purge a
/// notebook that had not been edited for a while right after it was deleted. It is only the
/// fallback for entries without a timestamp in the name.
pub fn cleanup_trash(base_dir: &Path, max_age: std::time::Duration) -> io::Result<usize> {
let trash_dir = base_dir.join(".trash");
if !trash_dir.exists() {
@@ -1513,20 +1659,25 @@ pub fn cleanup_trash(base_dir: &Path, max_age: std::time::Duration) -> io::Resul
if let Ok(entries) = fs::read_dir(&trash_dir) {
for entry in entries.flatten() {
let path = entry.path();
if let Ok(metadata) = entry.metadata() {
if let Ok(modified) = metadata.modified() {
if let Ok(age) = now.duration_since(modified) {
if age >= max_age {
if path.is_dir() {
if fs::remove_dir_all(&path).is_ok() {
removed += 1;
}
} else if fs::remove_file(&path).is_ok() {
removed += 1;
}
}
}
}
let since = entry
.file_name()
.to_str()
.and_then(trashed_at)
.or_else(|| entry.metadata().ok()?.modified().ok());
let expired = since
.and_then(|t| now.duration_since(t).ok())
.is_some_and(|age| age >= max_age);
if !expired {
continue;
}
let is_dir = entry.file_type().map(|t| t.is_dir()).unwrap_or(false);
let gone = if is_dir {
fs::remove_dir_all(&path)
} else {
fs::remove_file(&path)
};
if gone.is_ok() {
removed += 1;
}
}
}
@@ -1566,6 +1717,77 @@ mod tests {
use std::sync::Arc;
use std::time::{Duration, Instant};
#[test]
fn test_scaled_stroke_keeps_its_width_after_reload() {
let temp_dir = tempfile::tempdir().unwrap();
let color = [0.0, 0.0, 0.0, 1.0];
let samples: Vec<StylusSample> = (0..20)
.map(|i| StylusSample {
position: glam::Vec2::new(200.0 + 10.0 * i as f32, 500.0),
pressure: 0.5,
timestamp: i as f64 * 0.01,
})
.collect();
let tool = crate::document::StrokeTool::default();
let vertices = reconstruct_stroke_vertices(&samples, color, 5.0, tool, true);
let mut stroke = Stroke::new(1, samples, vertices, color);
stroke.width = 5.0;
stroke.finalized = true;
let thickness = |s: &Stroke| {
let ys = s.vertices.iter().map(|v| v.position[1]);
ys.clone().fold(f32::MIN, f32::max) - ys.fold(f32::MAX, f32::min)
};
let before = thickness(&stroke);
stroke.scale(glam::Vec2::new(200.0, 500.0), glam::Vec2::splat(3.0));
assert!((stroke.width - 15.0).abs() < 1e-4);
let scaled = thickness(&stroke);
assert!((scaled - 3.0 * before).abs() < 0.01);
let mut page = Page::new(1, PageBackground::Blank);
page.add_stroke(stroke);
save_page(temp_dir.path(), &PageData::from(&page)).unwrap();
let loaded = load_page(temp_dir.path(), 1).unwrap().to_page();
assert!((loaded.strokes[0].width - 15.0).abs() < 1e-4);
let reloaded = thickness(&loaded.strokes[0]);
assert!(
(reloaded - scaled).abs() < 0.05 * scaled,
"scaled {scaled}, after reload {reloaded}"
);
}
#[test]
fn test_cleanup_trash_counts_from_deletion_not_from_last_edit() {
let base = tempfile::tempdir().unwrap();
let trash = base.path().join(".trash");
let now = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_secs();
let day = 24 * 3600;
let long_ago = std::time::SystemTime::now() - Duration::from_secs(90 * day);
// Deleted just now, last edited 90 days ago: stays
let fresh = trash.join(format!("nb_a_{now}"));
// Deleted 40 days ago: goes
let expired = trash.join(format!("nb_b_{}", now - 40 * day));
// No timestamp in the name: the modification time decides
let unnamed = trash.join("stray");
for dir in [&fresh, &expired, &unnamed] {
fs::create_dir_all(dir).unwrap();
fs::write(dir.join("manifest.json"), "{}").unwrap();
}
for dir in [&fresh, &unnamed] {
fs::File::open(dir).unwrap().set_modified(long_ago).unwrap();
}
let removed = cleanup_trash(base.path(), Duration::from_secs(30 * day)).unwrap();
assert_eq!(removed, 2);
assert!(fresh.exists());
assert!(!expired.exists());
assert!(!unnamed.exists());
}
#[test]
fn test_atomic_save_and_reload() {
let temp_dir = tempfile::tempdir().expect("Konnte temporäres Verzeichnis nicht anlegen");
@@ -2111,10 +2333,73 @@ mod tests {
fs::write(&broken_manifest_path, b"{ broken json ... ").unwrap();
let loaded = load_manifest(nb_dir).expect("Muss aus .bak geladen werden");
assert_eq!(loaded.id, "nb_test");
assert_eq!(loaded.title, "Backup Test");
}
#[test]
fn test_first_page_that_fails_to_load_warns_and_a_missing_one_is_empty() {
let base = tempfile::tempdir().unwrap();
let (_, _, dir) = create_new_notebook(base.path(), "First page").unwrap();
let first = load_manifest(&dir).unwrap().page_ids()[0];
let file = dir.join(format!("page_{first}.json"));
// Never saved: an ordinary empty page
let _ = fs::remove_file(&file);
let (nb, _) = load_notebook(&dir).unwrap();
assert!(!nb.pages[0].is_readonly());
assert!(nb.load_warnings.is_empty());
// Written by a newer version: read-only, and the user is told
let mut data = PageData::from(&Page::new(first, PageBackground::Blank));
data.format_version = CURRENT_FORMAT_VERSION + 1;
fs::write(&file, serde_json::to_vec(&data).unwrap()).unwrap();
let (nb, _) = load_notebook(&dir).unwrap();
assert!(nb.pages[0].is_readonly());
assert!(nb.load_warnings.contains_key(&first));
}
#[test]
fn test_newer_manifest_is_not_replaced_by_its_backup() {
let temp_dir = tempfile::tempdir().unwrap();
let nb_dir = temp_dir.path();
let old = NotebookManifest::new("nb", "Old state");
atomic_write_json(&nb_dir.join("manifest.json.bak"), &old).unwrap();
let mut newer = NotebookManifest::new("nb", "New state");
newer.format_version = CURRENT_FORMAT_VERSION + 1;
atomic_write_json(&nb_dir.join("manifest.json"), &newer).unwrap();
assert!(matches!(
load_manifest(nb_dir),
Err(StorageError::UnsupportedVersion { .. })
));
}
#[test]
fn test_copied_notebook_folder_is_a_notebook_of_its_own() {
let base = tempfile::tempdir().unwrap();
let (_, manifest, dir) = create_new_notebook(base.path(), "Original").unwrap();
let copy = base.path().join("copy_of_it");
fs::create_dir(&copy).unwrap();
fs::copy(dir.join("manifest.json"), copy.join("manifest.json")).unwrap();
let mut ids: Vec<String> = list_notebooks(base.path())
.into_iter()
.map(|s| s.id)
.collect();
ids.sort();
let mut expected = vec!["copy_of_it".to_string(), manifest.id.clone()];
expected.sort();
assert_eq!(ids, expected);
// Opened by its folder name, and the id stays that way once saved
let (_, mut loaded) = load_notebook(&copy).unwrap();
assert_eq!(loaded.id, "copy_of_it");
loaded.title = "Copy".to_string();
save_manifest(&copy, &loaded).unwrap();
assert_eq!(load_manifest(&dir).unwrap().title, "Original");
assert_eq!(load_manifest(&copy).unwrap().title, "Copy");
}
#[test]
fn test_create_notebook_collision_generates_new_id() {
let temp_dir = tempfile::tempdir().unwrap();
+373 -29
View File
@@ -20,7 +20,7 @@ use crate::file_picker::{FilePicker, PickerKind};
use crate::ruler::{Ruler, RulerHit};
use crate::settings::{
PenPreset, PressureCurve, SidebarSide, DEFAULT_HIGHLIGHTER_COLORS, DEFAULT_PEN_PRESETS,
DEFAULT_TEXT_COLORS, PEN_PRESET_COUNT,
DEFAULT_TEXT_COLORS, MAX_SAVED_COLORS, PEN_PRESET_COUNT,
};
use crate::storage::{NotebookLayout, NotebookSummary};
use crate::text::{TextEditing, TextFont, TEXT_SIZES_PT};
@@ -317,6 +317,10 @@ pub enum UiAction {
UnlockCorruptedPage(u64),
/// Werkzeugleiste an die andere Fensterseite verlegen (wird gespeichert)
SetSidebarSide(SidebarSide),
/// Compute handwritten math after `=` on or off (Issue #46)
SetMathAssist(bool),
/// One finger draws or moves the page (saved, Issue #50)
SetTouchDraw(bool),
/// Sprache der Oberfläche; `None` = Sprache des Systems
SetLanguage(Option<crate::i18n::Language>),
/// Papierfarbe der Anzeige und Aufhellen dunkler Tinte (wird gespeichert)
@@ -341,6 +345,15 @@ pub enum UiAction {
ImportFile(std::path::PathBuf),
/// Store the notebooks in this folder, `None` for the default location (Issue #49)
SetStorageDir(Option<std::path::PathBuf>),
/// Sign in to a Nextcloud server through the browser; with the fingerprint of a
/// certificate the user trusts (Issue #49, stage 2)
CloudConnect {
server: String,
trusted: Option<String>,
},
CloudCancel,
CloudSyncNow,
CloudDisconnect,
/// Aktuelle Seite als Bild oder SVG exportieren
ExportPage(PageExportKind),
/// Titel einer Seite setzen (leer = entfernen)
@@ -413,6 +426,8 @@ pub struct UiState {
pub update_modal_rect: Option<egui::Rect>,
/// Steuert, ob Touch-Eingaben mit einem Finger zeichnen oder das Dokument verschieben (Default: false = reine Gesten-Navigation & Zoom)
pub touch_draw_enabled: bool,
/// Computes handwritten math after an `=` (Issue #46)
pub math: crate::math_assist::MathAssist,
/// Zuletzt gelöschtes Notizbuch für Rückgängig während der Sitzung (Papierkorb-Pfad, Titel)
pub last_deleted_notebook: Option<(std::path::PathBuf, String)>,
/// Stifte der Werkzeugleiste (Farbe und Stärke) und der gewählte davon; `pen_color` und
@@ -431,6 +446,8 @@ pub struct UiState {
pub is_text_color_menu_open: bool,
/// Farbwähler in den Farb-Popovern aufgeklappt
pub show_color_picker: bool,
/// „Meine Farben“ in the color popovers (Issue #56)
pub saved_colors: Vec<[f32; 3]>,
/// Kontextmenü nach langem Drücken (Einfügen an dieser Stelle)
pub context_menu: Option<ContextMenu>,
/// Laufendes Platzschaffen (Vorschau)
@@ -493,8 +510,9 @@ pub struct UiState {
pub scrollbar_grab: Option<f32>,
/// Aktuelle Abbildung Seite ↔ Bildschirm (für Lineal und Treffertests)
pub page_view: Option<PageView>,
/// Fenstergröße in logischen Punkten (vom letzten Bild)
pub screen_size: egui::Vec2,
/// Usable part of the window in logical points (from the last frame; on Android without the
/// system bars)
pub screen_rect: egui::Rect,
/// Eingabefeld „Titel dieser Seite“ in der Seitenübersicht und die Seite, zu der es gehört
pub page_title_input: String,
pub page_title_for: Option<u64>,
@@ -508,6 +526,9 @@ pub struct UiState {
/// Folder holding the notebooks and whether the user picked it (Issue #49)
pub storage_dir: std::path::PathBuf,
pub storage_dir_custom: bool,
/// Nextcloud sync as the app last saw it, and the server address being typed
pub cloud: crate::cloud_sync::View,
pub cloud_server_input: String,
}
impl Default for UiState {
@@ -546,6 +567,7 @@ impl Default for UiState {
show_updater_config: false,
update_modal_rect: None,
touch_draw_enabled: false,
math: crate::math_assist::MathAssist::default(),
last_deleted_notebook: None,
pen_presets: DEFAULT_PEN_PRESETS,
active_pen: 0,
@@ -555,6 +577,7 @@ impl Default for UiState {
active_text_color: 0,
is_text_color_menu_open: false,
show_color_picker: false,
saved_colors: Vec::new(),
context_menu: None,
space_drag: None,
stylus_buttons: Vec::new(),
@@ -588,13 +611,15 @@ impl Default for UiState {
ruler_drag: None,
scrollbar_grab: None,
page_view: None,
screen_size: egui::Vec2::ZERO,
screen_rect: egui::Rect::ZERO,
page_title_input: String::new(),
page_title_for: None,
page_picker_list: false,
last_picker_dir: None,
storage_dir: std::path::PathBuf::new(),
storage_dir_custom: false,
cloud: crate::cloud_sync::View::default(),
cloud_server_input: String::new(),
}
}
}
@@ -661,7 +686,7 @@ impl UiState {
/// Mitte des Bereichs neben der Werkzeugleiste (logische Punkte).
fn visible_canvas_center(&self) -> egui::Pos2 {
(self.screen_size / 2.0).to_pos2()
self.screen_rect.center()
+ egui::vec2(
match self.sidebar_side {
SidebarSide::Left => self.sidebar_reserved_width() / 2.0,
@@ -699,7 +724,7 @@ impl UiState {
}
/// Länge der längsten Farbpalette; so viel Platz hält die Leiste immer frei.
pub const MAX_PALETTE_LEN: usize = 5;
pub const MAX_PALETTE_LEN: usize = PEN_PRESET_COUNT;
/// Vertical gap between toolbar buttons and the height of a divider line.
pub const SIDEBAR_SPACING: f32 = 2.0;
pub const SIDEBAR_DIVIDER: f32 = 3.0;
@@ -1282,17 +1307,21 @@ fn autosave_status_style(status: AutosaveStatus) -> (egui::Color32, &'static str
fn color_editor(
ui: &mut egui::Ui,
presets: &[ColorPreset],
saved: &mut Vec<[f32; 3]>,
color: &mut [f32; 4],
picker_open: &mut bool,
) -> bool {
let same_rgb = |a: &[f32], b: &[f32]| {
a[..3]
.iter()
.zip(&b[..3])
.all(|(a, b)| (a - b).abs() < 0.01)
};
let mut changed = false;
ui.horizontal(|ui| {
ui.spacing_mut().item_spacing = egui::vec2(8.0, 0.0);
for &(preset, label) in presets {
let is_sel = preset[..3]
.iter()
.zip(&color[..3])
.all(|(a, b)| (a - b).abs() < 0.01);
let is_sel = same_rgb(&preset, color);
if color_circle_swatch(ui, preset, is_sel, crate::i18n::tr(label), 30.0).clicked() {
*color = [preset[0], preset[1], preset[2], color[3]];
changed = true;
@@ -1309,6 +1338,46 @@ fn color_editor(
*picker_open = !*picker_open;
}
});
// My colors (Issue #56): kept for pen, highlighter and text alike
ui.add_space(6.0);
ui.horizontal_wrapped(|ui| {
ui.spacing_mut().item_spacing = egui::vec2(8.0, 6.0);
let mut remove = None;
for (i, rgb) in saved.iter().enumerate() {
let swatch = [rgb[0], rgb[1], rgb[2], 1.0];
let is_sel = same_rgb(rgb, color);
if color_circle_swatch(ui, swatch, is_sel, t!("Meine Farbe"), 30.0).clicked() {
*color = [rgb[0], rgb[1], rgb[2], color[3]];
changed = true;
}
if is_sel {
remove = Some(i);
}
}
if let Some(i) = remove {
if ui
.button(egui::RichText::new("🗑").size(16.0))
.on_hover_text(t!("Diese Farbe aus „Meine Farben“ entfernen"))
.clicked()
{
saved.remove(i);
}
} else if saved.len() < MAX_SAVED_COLORS {
let label = if saved.is_empty() {
t!("➕ Farbe merken")
} else {
"➕"
};
if ui
.button(label)
.on_hover_text(t!("Die aktuelle Farbe zu „Meine Farben“ hinzufügen"))
.clicked()
{
saved.push([color[0], color[1], color[2]]);
}
}
});
if *picker_open {
ui.add_space(6.0);
let [r, g, b] = [0, 1, 2].map(|i| (color[i].clamp(0.0, 1.0) * 255.0).round() as u8);
@@ -1652,6 +1721,57 @@ fn text_style_buttons(ui: &mut egui::Ui, btn: f32, state: &mut UiState) {
/// Bearbeitung eines Textfelds: egui-Textfeld an der Stelle und in der Größe des Felds auf
/// dem Blatt (ungedreht), mit derselben Schrift. Esc beendet die Bearbeitung.
/// The result offered next to a handwritten `=` (Issue #46): what was read and the result,
/// e.g. `2⁴ = 16`. Not interactive for egui, so that fingers and palms pass through to the
/// page; the pen and the mouse take it via [`crate::math_assist::MathAssist::take_hit`].
fn math_suggestion(ctx: &egui::Context, state: &mut UiState, notebook: &Notebook) {
state.math.chip_rect = None;
if state.view_mode {
return;
}
let (Some(view), Some(suggestion)) = (
state.page_view,
state.math.suggestion_for(notebook.current_page()).cloned(),
) else {
return;
};
let size = (suggestion.symbol_height * view.scale * 0.8).clamp(14.0, 48.0);
let response = egui::Area::new(egui::Id::new("math_suggestion"))
.fixed_pos(view.to_screen(suggestion.anchor))
.pivot(egui::Align2::LEFT_CENTER)
.order(egui::Order::Foreground)
.interactable(false)
.show(ctx, |ui| {
egui::Frame::none()
.fill(egui::Color32::from_rgba_unmultiplied(30, 34, 44, 235))
.stroke(egui::Stroke::new(
1.5_f32,
egui::Color32::from_rgb(70, 130, 230),
))
.rounding(size * 0.4)
.inner_margin(egui::vec2(size * 0.45, size * 0.2))
.show(ui, |ui| {
ui.horizontal(|ui| {
ui.spacing_mut().item_spacing.x = size * 0.25;
ui.label(
egui::RichText::new(format!("{} =", suggestion.expression))
.size(size * 0.7)
.color(egui::Color32::from_gray(170)),
);
ui.label(
egui::RichText::new(&suggestion.result)
.size(size)
.strong()
.color(egui::Color32::WHITE),
);
});
});
})
.response;
// A little larger than drawn, as the pen does not hit exactly
state.math.chip_rect = Some(response.rect.expand(6.0));
}
fn text_edit_overlay(ctx: &egui::Context, state: &mut UiState) {
let adapt = state.adapt_ink_colors && state.paper_theme.is_dark();
let (Some(view), Some(edit)) = (state.page_view, state.text_edit.as_mut()) else {
@@ -2869,8 +2989,9 @@ fn file_picker_dialog(ctx: &egui::Context, state: &mut UiState, actions: &mut Ve
),
),
};
let width = dialog_width(ctx, state, 460.0);
dialog_window(title, state, false).show(ctx, |ui| {
ui.set_width(460.0);
ui.set_width(width);
touch_friendly_spacing(ui);
if dialog_header(ui, header, true) {
close = true;
@@ -3041,6 +3162,17 @@ fn dialog_window(title: &'static str, state: &UiState, danger: bool) -> egui::Wi
.frame(dialog_frame(danger))
}
/// Content width of a dialog: `wanted`, but at most what fits beside the toolbar, so that on a
/// phone the dialog is neither hidden under the toolbar nor cut off at the edge (Issue #50).
fn dialog_width(ctx: &egui::Context, state: &UiState, wanted: f32) -> f32 {
// Frame margins (20 each side) and a gap to the toolbar and the screen edge
let free = ctx.screen_rect().width()
- state.sidebar_reserved_width()
- 2.0 * 20.0
- 2.0 * UiState::SIDEBAR_MARGIN;
wanted.min(free).max(200.0)
}
/// Kopfzeile eines Dialogs mit Titel und optionalem Schließen-Knopf.
/// Gibt `true` zurück, wenn „Schließen“ getippt wurde.
fn dialog_header(ui: &mut egui::Ui, title: &str, closable: bool) -> bool {
@@ -3133,6 +3265,171 @@ pub fn key_allowed_in_view_mode(key: &winit::keyboard::Key, ctrl: bool) -> bool
}
}
/// "How long ago" for the last sync.
fn elapsed_label(at: std::time::SystemTime) -> String {
let secs = at.elapsed().map_or(0, |d| d.as_secs());
if secs < 60 {
t!("gerade eben").to_string()
} else if secs < 3600 {
t!("vor {minutes} min", minutes = secs / 60)
} else {
t!("vor {hours} h", hours = secs / 3600)
}
}
/// Nextcloud sync in the menu (Issue #49, stage 2).
fn cloud_section(ui: &mut egui::Ui, state: &mut UiState, actions: &mut Vec<UiAction>) {
use crate::cloud_sync::Status;
let small = |text: String, color: egui::Color32| {
egui::Label::new(egui::RichText::new(text).size(11.5).color(color)).wrap(true)
};
let grey = egui::Color32::from_gray(190);
let warn = egui::Color32::from_rgb(240, 180, 80);
let button = |text: String| egui::Button::new(text).min_size(egui::vec2(115.0, 28.0));
ui.add_space(4.0);
ui.label(
egui::RichText::new(t!("NEXTCLOUD"))
.size(10.5)
.strong()
.color(egui::Color32::from_gray(160)),
);
let status = state.cloud.status.clone();
if matches!(
status,
Status::Connecting | Status::WaitingForLogin { .. } | Status::Syncing
) {
ui.ctx()
.request_repaint_after(std::time::Duration::from_millis(500));
}
match status {
Status::Off | Status::Failed(_) if state.cloud.account.is_none() => {
if let Status::Failed(msg) = &status {
ui.add(small(format!("⚠ {msg}"), warn));
}
let field = ui.add(
egui::TextEdit::singleline(&mut state.cloud_server_input)
.hint_text(t!("z. B. cloud.example.org"))
.margin(egui::vec2(8.0, 6.0))
.desired_width(f32::INFINITY),
);
if state.cloud_server_input.trim().starts_with("http://") {
ui.add(small(
t!("⚠ Ohne https ist das App-Passwort im Netz lesbar – nur im eigenen Heimnetz verwenden").to_string(),
warn,
));
}
let enter = field.lost_focus() && ui.input(|i| i.key_pressed(egui::Key::Enter));
if ui
.add_enabled(
!state.cloud_server_input.trim().is_empty(),
button(t!("🔗 Verbinden …").to_string()),
)
.on_hover_text(t!("Anmeldung im Browser; die App bekommt ein eigenes App-Passwort, das sich in Nextcloud jederzeit widerrufen lässt"))
.clicked()
|| enter && !state.cloud_server_input.trim().is_empty()
{
actions.push(UiAction::CloudConnect {
server: state.cloud_server_input.clone(),
trusted: None,
});
}
}
Status::Off | Status::Connecting => {
ui.add(small(t!("⏳ Verbinde …").to_string(), grey));
if ui.add(button(t!("Abbrechen").to_string())).clicked() {
actions.push(UiAction::CloudCancel);
}
}
Status::Untrusted {
server,
fingerprint,
} => {
ui.add(small(
t!("⚠ Das Zertifikat von {server} ist von keiner bekannten Stelle signiert. Nur vertrauen, wenn dieser Fingerabdruck mit dem des Servers übereinstimmt:", server = server),
warn,
));
ui.add(
egui::Label::new(
egui::RichText::new(&fingerprint)
.monospace()
.size(10.0)
.color(grey),
)
.wrap(true),
);
ui.horizontal(|ui| {
if ui.add(button(t!("Vertrauen").to_string())).clicked() {
actions.push(UiAction::CloudConnect {
server: server.clone(),
trusted: Some(fingerprint.clone()),
});
}
if ui.add(button(t!("Abbrechen").to_string())).clicked() {
actions.push(UiAction::CloudCancel);
}
});
}
Status::WaitingForLogin { url } => {
ui.add(small(
t!("Bitte im Browser anmelden und den Zugriff erlauben …").to_string(),
grey,
));
ui.horizontal(|ui| {
if ui
.add(button(t!("🌐 Browser öffnen").to_string()))
.clicked()
{
actions.push(UiAction::OpenUrlInBrowser(url.clone()));
}
if ui.add(button(t!("Abbrechen").to_string())).clicked() {
actions.push(UiAction::CloudCancel);
}
});
}
Status::Idle { .. } | Status::Syncing | Status::Offline(_) | Status::Failed(_) => {
if let Some(account) = &state.cloud.account {
ui.add(small(format!("👤 {account}"), grey).truncate(true))
.on_hover_text(account);
}
let (text, color) = match &status {
Status::Idle { last: Some(at) } => (
t!("✔ Synchronisiert {when}", when = elapsed_label(*at)),
egui::Color32::from_rgb(120, 200, 120),
),
Status::Offline(msg) => (
t!("⚪ Offline – neuer Versuch in Kürze ({msg})", msg = msg),
grey,
),
Status::Failed(msg) => (format!("⚠ {msg}"), warn),
_ => (t!("🔄 Synchronisiere …").to_string(), grey),
};
ui.add(small(text, color));
if state.cloud.insecure {
ui.add(small(t!("⚠ Unverschlüsselt (http)").to_string(), warn));
}
ui.horizontal(|ui| {
if ui
.add_enabled(
!matches!(status, Status::Syncing),
button(t!("🔄 Jetzt").to_string()),
)
.on_hover_text(t!("Sofort mit dem Server abgleichen"))
.clicked()
{
actions.push(UiAction::CloudSyncNow);
}
if ui
.add(button(t!("Trennen").to_string()))
.on_hover_text(t!("Abgleich beenden und das App-Passwort widerrufen; die Notizbücher bleiben auf diesem Gerät und auf dem Server"))
.clicked()
{
actions.push(UiAction::CloudDisconnect);
}
});
}
}
}
/// Rendert die schwebende Toolbar und alle zugehörigen UI-Komponenten mit `egui`.
///
/// Gibt eine Liste von `UiAction`-Events zurück, die vom Hauptprogramm verarbeitet werden.
@@ -3144,12 +3441,13 @@ pub fn render_ui(
updater: &crate::updater::UpdaterController,
) -> Vec<UiAction> {
let mut actions = std::mem::take(&mut state.queued_actions);
state.screen_size = ctx.screen_rect().size();
state.screen_rect = ctx.screen_rect();
page_separators(ctx, state, notebook);
page_scrollbar(ctx, state, notebook, &mut actions);
space_preview(ctx, state, notebook);
ruler_overlay(ctx, state);
text_edit_overlay(ctx, state);
math_suggestion(ctx, state, notebook);
context_menu(ctx, state, &mut actions);
let mut pen_btn_rect = None;
@@ -3842,6 +4140,7 @@ pub fn render_ui(
if color_editor(
ui,
&PEN_COLOR_PRESETS,
&mut state.saved_colors,
&mut state.pen_color,
&mut state.show_color_picker,
) {
@@ -4023,6 +4322,7 @@ pub fn render_ui(
if color_editor(
ui,
&HIGHLIGHTER_COLOR_PRESETS,
&mut state.saved_colors,
&mut state.highlighter_color,
&mut state.show_color_picker,
) {
@@ -4129,6 +4429,7 @@ pub fn render_ui(
if color_editor(
ui,
&PEN_COLOR_PRESETS,
&mut state.saved_colors,
&mut color,
&mut state.show_color_picker,
) {
@@ -4376,13 +4677,27 @@ pub fn render_ui(
});
section(ui, t!("EINGABE"));
ui.checkbox(
&mut state.touch_draw_enabled,
t!("Mit dem Finger zeichnen"),
)
.on_hover_text(
t!("Aus: ein Finger verschiebt, zwei Finger zoomen (empfohlen mit Stift).\nAn: ein Finger zeichnet."),
);
if ui
.checkbox(
&mut state.touch_draw_enabled,
t!("Mit dem Finger zeichnen"),
)
.on_hover_text(
t!("Aus: ein Finger verschiebt, zwei Finger zoomen (empfohlen mit Stift).\nAn: ein Finger zeichnet."),
)
.changed()
{
actions.push(UiAction::SetTouchDraw(state.touch_draw_enabled));
}
let mut math = state.math.enabled;
if ui
.checkbox(&mut math, t!("Formeln nach „=“ ausrechnen"))
.on_hover_text(t!("Nach einem handgeschriebenen „=“ erscheint das Ergebnis daneben, z. B. 2⁴ = 16. Antippen mit dem Stift oder Klicken fügt es als Text ein.\nGelesen werden Ziffern, + − · : /, Klammern, Hochzahlen, Brüche und x, y, a, b."))
.changed()
{
state.math.set_enabled(math);
actions.push(UiAction::SetMathAssist(math));
}
if let Some(curve) = pressure_curve_editor(ui, state) {
actions.push(UiAction::SavePressureCurve(curve));
}
@@ -4468,6 +4783,7 @@ pub fn render_ui(
actions.push(UiAction::SetStorageDir(None));
}
});
cloud_section(ui, state, &mut actions);
section(ui, t!("STATUS"));
let (dot_color, status_text) = autosave_status_style(autosave_status);
@@ -4683,9 +4999,10 @@ pub fn render_ui(
// 3b. Dokumenten-Manager (Notizbücher wechseln, anlegen, umbenennen, löschen)
if state.is_document_menu_open {
let width = dialog_width(ctx, state, 420.0);
let doc_win = dialog_window(t!("Dokumenten-Manager"), state, false)
.show(ctx, |ui| {
ui.set_width(420.0);
ui.set_width(width);
touch_friendly_spacing(ui);
if dialog_header(ui, t!("📚 Notizbücher"), true) {
state.is_document_menu_open = false;
@@ -4701,13 +5018,13 @@ pub fn render_ui(
folder_breadcrumbs(ui, state, &folder);
ui.add_space(6.0);
// Neues Notizbuch oder neuer Ordner hier
ui.horizontal(|ui| {
// Neues Notizbuch oder neuer Ordner hier (narrow screens: buttons below)
ui.horizontal_wrapped(|ui| {
let text_edit = ui.add(
egui::TextEdit::singleline(&mut state.new_notebook_title_input)
.hint_text(t!("Name …"))
.margin(egui::vec2(8.0, 8.0))
.desired_width(190.0),
.desired_width(190.0_f32.min(ui.available_width())),
);
let create_btn = ui
.button(egui::RichText::new(t!("➕ Notizbuch")).strong())
@@ -4785,7 +5102,7 @@ pub fn render_ui(
let edit = ui.add(
egui::TextEdit::singleline(&mut state.renaming_title_input)
.margin(egui::vec2(8.0, 8.0))
.desired_width(260.0),
.desired_width(260.0_f32.min(ui.available_width() - 80.0)),
);
if ui.button("✔").clicked()
|| (edit.lost_focus() && ui.input(|i| i.key_pressed(egui::Key::Enter)))
@@ -4950,9 +5267,10 @@ pub fn render_ui(
// 3c. Release- & Update-Center (Gitea Releases)
if state.is_update_dialog_open {
let width = dialog_width(ctx, state, 460.0);
let win = dialog_window(t!("Release- & Update-Center"), state, false)
.show(ctx, |ui| {
ui.set_width(460.0);
ui.set_width(width);
touch_friendly_spacing(ui);
if dialog_header(ui, t!("🚀 Updates & Versionen"), true) {
state.is_update_dialog_open = false;
@@ -5174,11 +5492,11 @@ pub fn render_ui(
.show(ui, |ui| {
ui.add_space(4.0);
ui.label(egui::RichText::new(t!("Gitea Server-URL:")).size(11.0));
ui.add(egui::TextEdit::singleline(&mut state.updater_server_url_input).desired_width(380.0));
ui.add(egui::TextEdit::singleline(&mut state.updater_server_url_input).desired_width(380.0_f32.min(ui.available_width())));
ui.add_space(4.0);
ui.label(egui::RichText::new(t!("Personal Access Token (optional für private Repos):")).size(11.0));
ui.add(egui::TextEdit::singleline(&mut state.updater_token_input).password(true).desired_width(380.0));
ui.add(egui::TextEdit::singleline(&mut state.updater_token_input).password(true).desired_width(380.0_f32.min(ui.available_width())));
ui.add_space(6.0);
if ui.button(egui::RichText::new(t!("💾 Einstellungen speichern")).size(11.5)).clicked() {
@@ -5250,9 +5568,10 @@ pub fn render_ui(
// 5. Nicht-wegklickbare Warnung bei Ladefehlern / Quarantäne (Block 1)
if let Some(warning) = notebook.load_warnings.get(&notebook.current_page().id) {
let width = dialog_width(ctx, state, 420.0);
dialog_window(t!("⚠ Datenintegritäts-Warnung"), state, true)
.show(ctx, |ui| {
ui.set_width(420.0);
ui.set_width(width);
touch_friendly_spacing(ui);
ui.horizontal(|ui| {
ui.colored_label(egui::Color32::from_rgb(255, 80, 80), "⚠");
@@ -5321,8 +5640,9 @@ pub fn render_ui(
// Notfall-/Sicherheits-Dialog bei Fehlschlag des Speicherns vor dem Beenden
if let Some(err) = &state.shutdown_error {
let mut dismiss = false;
let width = dialog_width(ctx, state, 460.0);
dialog_window(t!("⚠ Ungespeicherte Änderungen"), state, true).show(ctx, |ui| {
ui.set_width(460.0);
ui.set_width(width);
touch_friendly_spacing(ui);
ui.horizontal(|ui| {
ui.colored_label(egui::Color32::from_rgb(255, 80, 80), "⚠");
@@ -5504,6 +5824,30 @@ mod tests {
assert_eq!(PEN_WIDTH_PRESETS[3].0, 1.2);
}
#[test]
fn test_notebook_dialog_fits_beside_the_toolbar_on_a_phone() {
// Phone in portrait (Issue #50): the dialog was wider than the screen
let mut state = UiState::new();
state.is_document_menu_open = true;
state.available_notebooks = vec![NotebookSummary {
id: "nb-1".to_string(),
title: "Default notebook".to_string(),
page_count: 1,
modified_at: 100,
status: crate::storage::NotebookStatus::Normal,
error: None,
}];
state.current_notebook_id = "nb-1".to_string();
let screen = egui::vec2(412.0, 900.0);
let ctx = render_frames_sized(&mut state, &Notebook::new(), 8, screen);
let dialog = ctx
.memory(|m| m.area_rect(egui::Id::new(t!("Dokumenten-Manager"))))
.unwrap();
let toolbar = state.toolbar_rect.unwrap();
assert!(dialog.left() >= toolbar.right(), "{dialog:?} {toolbar:?}");
assert!(dialog.right() <= screen.x, "{dialog:?}");
}
#[test]
fn test_document_manager_ui_actions() {
let mut state = UiState::new();
+41
View File
@@ -272,6 +272,22 @@ pub fn is_newer_version(remote_tag: &str, current_tag: &str, include_prereleases
}
}
/// Version in a release file name (`ink-notes_<version>_<arch>.<ext>`).
pub fn asset_version(asset_name: &str) -> Option<&str> {
let rest = asset_name.strip_prefix("ink-notes_")?;
let (version, _) = rest.split_once('_')?;
parse_semver(version).map(|_| version)
}
/// Is the package named `asset_name` newer than the running version `current`?
///
/// The signature only covers file names and hashes, not the release they are offered in. A
/// server could therefore present an old, correctly signed package under a new version
/// number. The version in the signed file name closes that gap.
pub fn asset_is_upgrade(asset_name: &str, current: &str) -> bool {
asset_version(asset_name).is_some_and(|version| is_newer_version(version, current, true))
}
/// Berechnet den hex-kodierten SHA-256 Hash einer Datei.
pub fn compute_sha256(path: &Path) -> Result<String, String> {
let mut file = std::fs::File::open(path).map_err(|e| {
@@ -712,6 +728,14 @@ impl UpdaterController {
}
};
if !asset_is_upgrade(&safe_asset_name, env!("CARGO_PKG_VERSION")) {
self.status = UpdaterStatus::Error(t!(
"'{asset_name}' ist keine neuere Version und wird nicht installiert.",
asset_name = safe_asset_name
));
return;
}
self.status = UpdaterStatus::Downloading {
asset_name: safe_asset_name.clone(),
percent: 0.0,
@@ -1036,6 +1060,23 @@ mod tests {
assert!(is_newer_version("1.0.0-rc1", "0.9.0", true));
}
#[test]
fn test_only_packages_named_as_newer_are_upgrades() {
assert_eq!(asset_version("ink-notes_0.46.0_amd64.deb"), Some("0.46.0"));
assert_eq!(
asset_version("ink-notes_0.46.0_x86_64.AppImage"),
Some("0.46.0")
);
assert_eq!(asset_version("SHA256SUMS.txt"), None);
assert_eq!(asset_version("ink-notes_latest_amd64.deb"), None);
assert!(asset_is_upgrade("ink-notes_0.46.0_arm64.apk", "0.45.0"));
// An old release offered again under a new tag
assert!(!asset_is_upgrade("ink-notes_0.30.0_amd64.deb", "0.45.0"));
assert!(!asset_is_upgrade("ink-notes_0.45.0_x86_64.exe", "0.45.0"));
assert!(!asset_is_upgrade("something_else.deb", "0.45.0"));
}
#[test]
fn test_sanitize_asset_name() {
assert_eq!(
+8
View File
@@ -18,6 +18,14 @@ docs of the original crate were removed.
timestamp of each touch event (Started, Moved, Ended), so ink-notes measures the swipe speed
for gliding with the real event times and not with the time it got around to handling them
(Issue #28).
- `src/platform_impl/linux/wayland/event_loop/mod.rs`: every read from the Wayland socket wakes
the app (`AboutToWait`), also when the events belong to another queue on the same connection.
octotablet's pen events live on such a queue and only `dispatch_pending` it, so before, pen
strokes waited until something else woke the loop (Issue #53).
- `src/platform_impl/linux/wayland/seat/touch/mod.rs`: a touch lift (`wl_touch.up`) is handed
on right away instead of waiting for the next `wl_touch.frame`. On a GNOME laptop the lift
of a swipe only arrived with the next touch, which fits a frame sent before the lift: the page did not glide after a
swipe (Issue #28), and a ruler moved with the finger followed the hovering pen (Issue #55).
- `Cargo.toml`: `[lints.rust] warnings = "allow"`, because a path dependency is not built with
capped lints like a crates.io dependency.
@@ -105,10 +105,11 @@ impl<T: 'static> EventLoop<T> {
let wayland_dispatcher =
calloop::Dispatcher::new(wayland_source, |_, queue, winit_state: &mut WinitState| {
let result = queue.dispatch_pending(winit_state);
if result.is_ok()
&& (!winit_state.events_sink.is_empty()
|| !winit_state.window_compositor_updates.is_empty())
{
// ink-notes patch: wake the app for every read from the socket, not only for
// winit's own events. Events for other queues on the same connection (pen
// input from octotablet's queue) would otherwise wait until something else
// wakes the loop, so pen strokes appeared late or not at all (Issue #53).
if result.is_ok() {
winit_state.dispatched_events = true;
}
result
@@ -2,14 +2,15 @@
use sctk::reexports::client::protocol::wl_seat::WlSeat;
use sctk::reexports::client::protocol::wl_surface::WlSurface;
use sctk::reexports::client::protocol::wl_touch::WlTouch;
use sctk::reexports::client::{Connection, Proxy, QueueHandle};
use sctk::reexports::client::protocol::wl_touch::{self, WlTouch};
use sctk::reexports::client::{Connection, Dispatch, Proxy, QueueHandle};
use sctk::reexports::csd_frame::FrameClick;
use std::time::Duration;
use sctk::compositor::SurfaceData;
use sctk::seat::touch::{TouchData, TouchHandler};
use sctk::seat::SeatState;
use crate::dpi::LogicalPosition;
use crate::event::{Touch, TouchPhase, WindowEvent};
@@ -286,4 +287,35 @@ impl TouchDataExt for WlTouch {
}
}
sctk::delegate_touch!(WinitState);
// ink-notes patch: sctk holds every touch event back until the compositor sends the
// `wl_touch.frame` that closes its group. On a GNOME laptop the lift of a swipe only arrived
// together with the next touch, which fits a frame sent before the matching `up`: the page could
// not glide (Issue #28) and a ruler moved with the finger followed the hovering pen (Issue #55).
// A lift is now handed on right away, after the events that came before it.
impl Dispatch<WlTouch, TouchData> for WinitState {
fn event(
state: &mut Self,
touch: &WlTouch,
event: wl_touch::Event,
data: &TouchData,
conn: &Connection,
qh: &QueueHandle<Self>,
) {
if let wl_touch::Event::Up { serial, time, id } = event {
// Hand on what sctk still holds, then the lift itself
<SeatState as Dispatch<WlTouch, TouchData, Self>>::event(
state,
touch,
wl_touch::Event::Frame,
data,
conn,
qh,
);
state.up(conn, qh, touch, serial, time, id);
} else {
<SeatState as Dispatch<WlTouch, TouchData, Self>>::event(
state, touch, event, data, conn, qh,
);
}
}
}