Skip to content

Commit 3a30ced

Browse files
committed
feat: annotate images from the full-screen viewer (#46)
An Annotate button in the image viewer opens an editor with pen, highlighter, arrow, rectangle, text, and eraser tools, colors, sizes, undo/redo, and two-finger zoom. The annotated copy is uploaded as a new media image and either added after the original or replaces it in the note; it can also be copied. The tapped image is matched to its inline Markdown through the renderer's parse, and the note is only edited if that source is unchanged. refs #46
1 parent 9f2956d commit 3a30ced

27 files changed

Lines changed: 2429 additions & 32 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,13 @@ zero-based increasing release counter within the month. See [the release guide](
1717
local-folder, and remote images open from the copy already shown in the note, without loading
1818
them again or handing private addresses to another app.
1919
- **Copy image** in the full-screen image viewer puts the image on the clipboard, so it can be
20-
pasted into other apps.
20+
pasted into other apps ([#46](https://github.com/qownnotes/qownnotes-android/issues/46)).
21+
- **Annotate** in the full-screen image viewer lets you draw on an image with a pen, highlighter,
22+
arrows, rectangles, and text, in eight colors and three sizes, with an eraser and undo/redo. Use
23+
two fingers to zoom in for detail. When done, add the annotated image to the note after the
24+
original, replace the original in the note, or copy it. The original file is never overwritten,
25+
and the annotated copy is uploaded to the media folder like any other image
26+
([#46](https://github.com/qownnotes/qownnotes-android/issues/46)).
2127
- **Copy block** in the checkbox block actions of the note preview copies a task with its nested
2228
items and continuation text as Markdown, keeping the checkbox states
2329
([#53](https://github.com/qownnotes/qownnotes-android/issues/53)).

‎README.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ local, Nextcloud, and remote-image data are handled.
4444
- Insert web links from a clipboard-prefilled dialog with automatic page titles and editable labels.
4545
- Find text inside an open note, select and copy rendered text, and adjust the note text size.
4646
- Tap an image in a note to view it full screen with pinch and double-tap zoom, panning, and
47-
swipe-to-close, and copy it to the clipboard.
47+
swipe-to-close, copy it to the clipboard, or annotate it with pen, highlighter, arrows,
48+
rectangles, and text, then add the annotated copy to the note or replace the original with it.
4849
- Reorder checkbox blocks by dragging their handles, or long-press a block to delete it together
4950
with nested items and continuation text after confirmation.
5051
- Detect read-only and QOwnNotes-encrypted notes and fail closed for unsafe HTML, links, images, and
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package org.qownnotes.mobile
2+
3+
import android.graphics.Bitmap
4+
import android.graphics.BitmapFactory
5+
import android.graphics.Color
6+
import androidx.test.ext.junit.runners.AndroidJUnit4
7+
import androidx.test.platform.app.InstrumentationRegistry
8+
import kotlinx.coroutines.runBlocking
9+
import org.junit.After
10+
import org.junit.Assert.assertEquals
11+
import org.junit.Assert.assertThrows
12+
import org.junit.Assert.assertTrue
13+
import org.junit.Before
14+
import org.junit.Test
15+
import org.junit.runner.RunWith
16+
import org.qownnotes.mobile.core.Account
17+
18+
@RunWith(AndroidJUnit4::class)
19+
class AnnotatedImageSaveTest {
20+
private lateinit var application: TestQOwnNotesApplication
21+
private lateinit var localId: String
22+
23+
@Before
24+
fun setUp() = runBlocking<Unit> {
25+
application = InstrumentationRegistry.getInstrumentation().targetContext
26+
.applicationContext as TestQOwnNotesApplication
27+
application.reset()
28+
application.fakeSyncScheduler.pause()
29+
application.component.accountRepository.save(
30+
Account("image-account", "Images", "https://example.com", "user", "user", "1.3")
31+
)
32+
localId = application.component.createNote("image-account").localId
33+
application.component.replaceNoteContent(localId, CONTENT)
34+
}
35+
36+
@After
37+
fun tearDown() = runBlocking<Unit> { application.reset() }
38+
39+
@Test
40+
fun addsTheAnnotatedImageAfterTheOriginal() = runBlocking<Unit> {
41+
application.component.saveAnnotatedImage(localId, CONTENT, 1, annotated(), replace = false)
42+
43+
val (name, bytes) = application.fakeBackend.uploadedImages.single()
44+
assertTrue(name.endsWith(".jpg"))
45+
assertEquals("image/jpeg", application.fakeBackend.uploadedMediaTypes.single())
46+
val decoded = BitmapFactory.decodeByteArray(bytes, 0, bytes.size)
47+
assertEquals(120, decoded.width)
48+
assertTrue(Color.red(decoded.getPixel(60, 40)) > 200)
49+
assertEquals(
50+
"# Shots\n\n`![code](media/c.png)`\n\n![Whiteboard](media/board.jpg)\n\n" +
51+
"![Whiteboard](media/$name)\n\nAfter\n",
52+
content()
53+
)
54+
}
55+
56+
@Test
57+
fun replacesTheImageLinkAndKeepsTheRestOfTheNote() = runBlocking<Unit> {
58+
application.component.saveAnnotatedImage(localId, CONTENT, 1, annotated(), replace = true)
59+
60+
val name = application.fakeBackend.uploadedImages.single().first
61+
assertEquals(CONTENT.replace("media/board.jpg", "media/$name"), content())
62+
}
63+
64+
@Test
65+
fun leavesANoteAloneWhoseImageChangedSinceItWasShown() = runBlocking<Unit> {
66+
application.component.replaceNoteContent(localId, CONTENT.replace("board", "other"))
67+
68+
assertThrows(IllegalStateException::class.java) {
69+
runBlocking {
70+
application.component.saveAnnotatedImage(
71+
localId,
72+
CONTENT,
73+
1,
74+
annotated(),
75+
replace = true
76+
)
77+
}
78+
}
79+
assertEquals(CONTENT.replace("board", "other"), content())
80+
}
81+
82+
private fun annotated() = Bitmap.createBitmap(120, 80, Bitmap.Config.ARGB_8888).apply {
83+
eraseColor(Color.RED)
84+
setHasAlpha(false)
85+
}
86+
87+
private suspend fun content() = application.component.noteRepository.get(localId)!!.content
88+
89+
private companion object {
90+
const val CONTENT =
91+
"# Shots\n\n`![code](media/c.png)`\n\n![Whiteboard](media/board.jpg)\n\nAfter\n"
92+
}
93+
}

‎app/src/androidTest/kotlin/org/qownnotes/mobile/NoteImageViewerTest.kt‎

Lines changed: 97 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,21 @@ import androidx.compose.material3.MaterialTheme
88
import androidx.compose.ui.geometry.Offset
99
import androidx.compose.ui.semantics.SemanticsProperties
1010
import androidx.compose.ui.test.assertIsDisplayed
11+
import androidx.compose.ui.test.assertIsEnabled
12+
import androidx.compose.ui.test.assertIsNotEnabled
13+
import androidx.compose.ui.test.assertIsOn
1114
import androidx.compose.ui.test.assertTextEquals
15+
import androidx.compose.ui.test.click
1216
import androidx.compose.ui.test.doubleClick
1317
import androidx.compose.ui.test.junit4.createComposeRule
1418
import androidx.compose.ui.test.onAllNodesWithTag
1519
import androidx.compose.ui.test.onNodeWithContentDescription
1620
import androidx.compose.ui.test.onNodeWithTag
1721
import androidx.compose.ui.test.performClick
22+
import androidx.compose.ui.test.performTextInput
1823
import androidx.compose.ui.test.performTouchInput
1924
import androidx.compose.ui.test.pinch
25+
import androidx.compose.ui.test.swipe
2026
import androidx.compose.ui.test.swipeDown
2127
import androidx.test.ext.junit.runners.AndroidJUnit4
2228
import androidx.test.platform.app.InstrumentationRegistry
@@ -35,14 +41,19 @@ class NoteImageViewerTest {
3541

3642
private var dismissed = false
3743

38-
private fun showViewer(description: String = "Whiteboard") {
44+
private fun showViewer(
45+
description: String = "Whiteboard",
46+
onSaveAnnotation: (suspend (Bitmap, Boolean) -> Unit)? = null
47+
) {
3948
val bitmap = Bitmap.createBitmap(300, 200, Bitmap.Config.ARGB_8888).apply {
4049
eraseColor(Color.GREEN)
4150
}
4251
composeRule.setContent {
4352
MaterialTheme {
4453
if (!dismissed) {
45-
NoteImageViewer(NoteImage(bitmap, description)) { dismissed = true }
54+
NoteImageViewer(NoteImage(bitmap, description), onSaveAnnotation) {
55+
dismissed = true
56+
}
4657
}
4758
}
4859
}
@@ -117,6 +128,90 @@ class NoteImageViewerTest {
117128
assertEquals(1, File(context.cacheDir, "copied-images").listFiles()!!.size)
118129
}
119130

131+
@Test
132+
fun annotatingDrawsUndoesRedoesAndSavesAsANewImage() {
133+
var saved: Pair<Bitmap, Boolean>? = null
134+
showViewer { annotated, replace -> saved = annotated to replace }
135+
composeRule.onNodeWithTag("image-viewer-annotate").performClick()
136+
composeRule.onNodeWithTag("image-annotation-done").assertIsNotEnabled()
137+
composeRule.onNodeWithTag("image-annotation-tool-pen").assertIsOn()
138+
// Two fingers zoom rather than draw; zooming back out keeps the screen-to-image mapping.
139+
composeRule.onNodeWithTag("image-annotation-canvas").performTouchInput {
140+
pinch(
141+
center - Offset(20f, 0f),
142+
center - Offset(250f, 0f),
143+
center + Offset(20f, 0f),
144+
center + Offset(250f, 0f)
145+
)
146+
}
147+
composeRule.onNodeWithTag("image-annotation-done").assertIsNotEnabled()
148+
composeRule.onNodeWithTag("image-annotation-canvas").performTouchInput {
149+
pinch(
150+
center - Offset(250f, 0f),
151+
center - Offset(20f, 0f),
152+
center + Offset(250f, 0f),
153+
center + Offset(20f, 0f)
154+
)
155+
}
156+
composeRule.onNodeWithTag("image-annotation-done").assertIsNotEnabled()
157+
158+
// The image fills the screen width, so its middle row runs across the screen's middle.
159+
drawAcrossTheMiddle()
160+
composeRule.onNodeWithTag("image-annotation-done").assertIsEnabled()
161+
composeRule.onNodeWithTag("image-annotation-undo").performClick()
162+
composeRule.onNodeWithTag("image-annotation-done").assertIsNotEnabled()
163+
composeRule.onNodeWithTag("image-annotation-redo").performClick()
164+
165+
composeRule.onNodeWithTag("image-annotation-done").performClick()
166+
composeRule.onNodeWithTag("image-annotation-add").performClick()
167+
composeRule.waitUntil(5_000) { dismissed }
168+
169+
val (annotated, replace) = saved!!
170+
assertFalse(replace)
171+
assertEquals(300, annotated.width)
172+
val marked = annotated.getPixel(150, 100)
173+
assertTrue(Color.red(marked) > 200 && Color.green(marked) < 100)
174+
// Away from the stroke the image is unchanged.
175+
assertEquals(Color.GREEN, annotated.getPixel(150, 20))
176+
}
177+
178+
@Test
179+
fun annotationsCanBeErasedAndClosingAsksBeforeDiscardingThem() {
180+
showViewer()
181+
composeRule.onNodeWithTag("image-viewer-annotate").performClick()
182+
drawAcrossTheMiddle()
183+
composeRule.onNodeWithTag("image-annotation-tool-eraser").performClick()
184+
composeRule.onNodeWithTag("image-annotation-canvas").performTouchInput { click(center) }
185+
composeRule.onNodeWithTag("image-annotation-done").assertIsNotEnabled()
186+
composeRule.onNodeWithTag("image-annotation-undo").performClick()
187+
188+
composeRule.onNodeWithTag("image-annotation-close").performClick()
189+
composeRule.onNodeWithTag("image-annotation-discard").performClick()
190+
waitUntilGone("image-annotation-editor")
191+
composeRule.onNodeWithTag("image-viewer-image").assertIsDisplayed()
192+
assertFalse(dismissed)
193+
}
194+
195+
@Test
196+
fun textToolAddsALabelAndImagesOutsideEditableNotesCanOnlyBeCopied() {
197+
showViewer()
198+
composeRule.onNodeWithTag("image-viewer-annotate").performClick()
199+
composeRule.onNodeWithTag("image-annotation-tool-text").performClick()
200+
composeRule.onNodeWithTag("image-annotation-canvas").performTouchInput { click(center) }
201+
composeRule.onNodeWithTag("image-annotation-text-field").performTextInput("Look here")
202+
composeRule.onNodeWithTag("image-annotation-text-done").performClick()
203+
composeRule.onNodeWithTag("image-annotation-done").assertIsEnabled().performClick()
204+
205+
composeRule.onNodeWithTag("image-annotation-add").assertIsNotEnabled()
206+
composeRule.onNodeWithTag("image-annotation-replace").assertIsNotEnabled()
207+
composeRule.onNodeWithTag("image-annotation-copy").assertIsEnabled()
208+
}
209+
210+
private fun drawAcrossTheMiddle() =
211+
composeRule.onNodeWithTag("image-annotation-canvas").performTouchInput {
212+
swipe(Offset(width * 0.2f, centerY), Offset(width * 0.8f, centerY), 300)
213+
}
214+
120215
private fun zoomText(): String? = composeRule.onAllNodesWithTag("image-viewer-zoom")
121216
.fetchSemanticsNodes().firstOrNull()?.config
122217
?.getOrElseNullable(SemanticsProperties.Text) { null }

0 commit comments

Comments
 (0)