Skip to content

Commit 6baadcc

Browse files
committed
fix: hide keyboard when leaving edit mode
1 parent b7a1cb1 commit 6baadcc

5 files changed

Lines changed: 33 additions & 8 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ uses [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
2222

2323
### Fixed
2424

25+
- The onscreen keyboard now closes reliably when leaving Markdown edit mode.
2526
- Local `media/` images in Nextcloud notes now use the versioned Notes attachment endpoint instead
2627
of failing to load as red placeholders.
2728
- Checkboxes in nested task lists can now be toggled in view mode; previously only top-level

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import kotlinx.coroutines.flow.first
4242
import kotlinx.coroutines.runBlocking
4343
import org.hamcrest.Matchers.containsString
4444
import org.junit.Assert.assertEquals
45+
import org.junit.Assert.assertFalse
4546
import org.junit.Assert.assertTrue
4647
import org.junit.Before
4748
import org.junit.Rule
@@ -635,10 +636,18 @@ class AppLaunchTest {
635636
importAccount("alice", "Existing note", "etag-1", 10)
636637
composeRule.onNodeWithText("Existing note").performClick()
637638
composeRule.enterEditMode()
639+
lateinit var editor: TextView
640+
onView(withId(R.id.markdown_editor)).check { view, _ ->
641+
editor = view as TextView
642+
assertTrue(editor.hasFocus())
643+
}
638644

639645
composeRule.onNodeWithTag("cancel-editing").performClick()
640646

641647
composeRule.waitForTag("markdown-view")
648+
composeRule.runOnIdle {
649+
assertFalse("leaving edit mode must release editor focus", editor.hasFocus())
650+
}
642651
composeRule.onNodeWithTag("confirm-discard-changes").assertDoesNotExist()
643652
composeRule.onNodeWithTag("edit-note").assertIsDisplayed()
644653
}

‎app/src/main/kotlin/org/qownnotes/mobile/MainActivity.kt‎

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1190,6 +1190,11 @@ private fun NoteDetailScreen(
11901190
var selectionStart by rememberSaveable(localId) { mutableStateOf(0) }
11911191
var selectionEnd by rememberSaveable(localId) { mutableStateOf(0) }
11921192
var editor by remember { mutableStateOf<MarkdownEditText?>(null) }
1193+
val leaveEditMode = {
1194+
// Hide the IME while the editor still has a valid window token, before Compose removes it.
1195+
editor?.releaseInputFocus()
1196+
editing = false
1197+
}
11931198
val editorScrollState = rememberScrollState()
11941199
var editorViewportHeight by remember { mutableIntStateOf(0) }
11951200
var renderedView by remember { mutableStateOf<AppCompatTextView?>(null) }
@@ -1298,10 +1303,10 @@ private fun NoteDetailScreen(
12981303
val source = draft
12991304
if (source != null) {
13001305
scope.launch {
1301-
if (component.saveDraft(localId, source)) editing = false
1306+
if (component.saveDraft(localId, source)) leaveEditMode()
13021307
}
13031308
} else {
1304-
editing = false
1309+
leaveEditMode()
13051310
}
13061311
}
13071312
BackHandler(enabled = finding && !editing) {
@@ -1354,9 +1359,13 @@ private fun NoteDetailScreen(
13541359
val source = draft
13551360
if (editing && source != null) {
13561361
scope.launch {
1357-
if (component.saveDraft(localId, source)) onBackToList()
1362+
if (component.saveDraft(localId, source)) {
1363+
editor?.releaseInputFocus()
1364+
onBackToList()
1365+
}
13581366
}
13591367
} else {
1368+
editor?.releaseInputFocus()
13601369
onBackToList()
13611370
}
13621371
},
@@ -1535,7 +1544,7 @@ private fun NoteDetailScreen(
15351544
if (draft != contentBeforeEditing) {
15361545
showDiscardConfirmation = true
15371546
} else {
1538-
editing = false
1547+
leaveEditMode()
15391548
}
15401549
}
15411550
)
@@ -1547,7 +1556,7 @@ private fun NoteDetailScreen(
15471556
val source = draft
15481557
if (source != null) {
15491558
scope.launch {
1550-
if (component.saveDraft(localId, source)) editing = false
1559+
if (component.saveDraft(localId, source)) leaveEditMode()
15511560
}
15521561
}
15531562
}
@@ -1877,10 +1886,10 @@ private fun NoteDetailScreen(
18771886
scope.launch {
18781887
component.saveDraft(localId, restored)
18791888
draft = restored
1880-
editing = false
1889+
leaveEditMode()
18811890
}
18821891
} else {
1883-
editing = false
1892+
leaveEditMode()
18841893
}
18851894
},
18861895
modifier = Modifier.testTag("confirm-discard-changes")

‎markdown-android/src/main/kotlin/org/qownnotes/mobile/markdown/MarkdownEditor.kt‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ private val FENCE = Regex("^ {0,3}(`{3,}|~{3,})(?:[^`]*)$")
141141
class MarkdownEditText @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) :
142142
AppCompatEditText(context, attrs) {
143143
var onSelectionChanged: ((Int, Int) -> Unit)? = null
144+
private var inputFocusRequest: Runnable? = null
144145

145146
/**
146147
* Invoked before an edit the writer did not type, such as a formatting action, so that an undo
@@ -171,16 +172,21 @@ class MarkdownEditText @JvmOverloads constructor(context: Context, attrs: Attrib
171172

172173
/** Gives the editor input focus and asks the input method to open. */
173174
fun focusForInput() {
175+
inputFocusRequest?.let(::removeCallbacks)
174176
val request = Runnable {
177+
inputFocusRequest = null
175178
if (isFocused || requestFocus()) {
176179
inputMethodManager()?.showSoftInput(this, InputMethodManager.SHOW_IMPLICIT)
177180
}
178181
}
182+
inputFocusRequest = request
179183
if (isAttachedToWindow && hasWindowFocus()) request.run() else post(request)
180184
}
181185

182186
/** Releases input focus and hides the input method when editing stops. */
183187
fun releaseInputFocus() {
188+
inputFocusRequest?.let(::removeCallbacks)
189+
inputFocusRequest = null
184190
inputMethodManager()?.hideSoftInputFromWindow(windowToken, 0)
185191
clearFocus()
186192
}

‎mobile-app-plan.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -831,7 +831,7 @@ Implemented:
831831
- Fixed the editor focus and IME defect recorded on 2026-09-01. `AppCompatEditText` resolves its default style from the AppCompat `editTextStyle` theme attribute, which only exists in `Theme.AppCompat` descendants. The application theme derived from the framework `Theme.Material.Light.NoActionBar`, so `Widget.AppCompat.EditText` was never applied and the editor was left focusable but not focusable in touch mode, making a cursor and keyboard unreachable by tapping.
832832
- Rebased the application theme on `Theme.AppCompat.DayNight.NoActionBar` so hosted AppCompat widgets get their intended styles and follow the system dark mode like the Compose theme.
833833
- Set the interactive flags on `MarkdownEditText` itself so editing no longer depends on the hosting theme, and removed the inherited widget background because Compose supplies the editor surface.
834-
- Added focus and keyboard activation when edit mode opens, focus restoration after formatting-toolbar actions, keyboard dismissal when the editor is released, and IME insets so the keyboard cannot cover the editor.
834+
- Added focus and keyboard activation when edit mode opens, focus restoration after formatting-toolbar actions, keyboard dismissal before leaving edit mode (with editor-release cleanup as a fallback), and IME insets so the keyboard cannot cover the editor.
835835
- Changed formatting actions to replace only the changed range instead of the whole document, preserving undo history, spans, and in-progress input-method composition.
836836
- Applied the Compose surface text color to the hosted editor and rendered views so note text stays legible in dark mode.
837837
- Added an adjustable note text size, requested during Phase 3 rather than planned. `A-` and `A+` controls on the note screen step through discrete `sp` sizes, apply to both the rendered note and the source editor, persist in `SharedPreferences`, survive process death, and carry accessibility descriptions. Rendered headings and code rescale without re-rendering because Markwon sizes them relative to the view.

0 commit comments

Comments
 (0)