Repository: nvaccess/nvda

Issue:
NVDA gets stuck on line in Windows Mail if you open an NVDA dialog
### Steps to reproduce:
1. Find a long email using Windows Mail
2. Go down to around line 60
3. Press a command to open a dialog in NVDA, EG (`NVDA+Ctrl+S` ) for the synthesizer dialog, 
4. Press escape to close the synthesizer dialog
4. Try arrowing around the email.

### Actual behavior:
NVDA seemingly gets stuck at this point. Closing the message fixes this, restarting NVDA does not.

### Expected behavior:
NVDA should continue letting you arrow up and down as normal. Additionally, when this behavior occurs, you can't use NVDA+space to toggle browse mode.

### System configuration
#### NVDA installed/portable/running from source:
Installed
#### NVDA version:
2021.3 beta 2
#### Windows version:
Windows 11 Pro Insider (x64) build 22494.1000
#### Name and version of other software in use when reproducing the issue:
Mail microsoft.windowscommunicationsapps version 16005.14326.20544.0

#### Other information about your system:

### Other questions
#### Does the issue still occur after restarting your computer?
Yes
#### Have you tried any other versions of NVDA? If so, please report their behaviors.
The behavior does not exist in 2021.2. In that version, it works as expected.
#### If add-ons are disabled, is your problem still occurring?
Yes
#### Does the issue still occur after you run the COM Registration Fixing Tool in NVDA's tools menu?
Yes

<details>
<summary>Log snippet</summary>
<pre><code>
Using eSpeak NG version 1.51-dev
INFO - synthDriverHandler.setSynth (19:42:38.179) - MainThread (15676):
Loaded synthDriver espeak
DEBUGWARNING - NVDAObjects.IAccessible.IAccessible._get_IAccessibleRole (19:42:38.217) - MainThread (15676):
accRole failed: (-2147024809, 'The parameter is incorrect.', (None, None, None, 0, None))
DEBUGWARNING - characterProcessing._getSpeechSymbolsForLocale (19:42:40.761) - MainThread (15676):
No CLDR data for locale en_GB
DEBUGWARNING - IAccessibleHandler.internalWinEventHandler._shouldGetEvents (19:42:43.700) - MainThread (15676):
Foreground took too long to change. Foreground still 527122 (CabinetWClass). Should be 198384 (WorkerW)
DEBUGWARNING - NVDAObjects.IAccessible.IAccessible._get_IAccessibleRole (19:42:44.955) - MainThread (15676):
accRole failed: (-2147024809, 'The parameter is incorrect.', (None, None, None, 0, None))
DEBUGWARNING - IAccessibleHandler.internalWinEventHandler._shouldGetEvents (19:42:47.570) - MainThread (15676):
Foreground took too long to change. Foreground still 460462 (ApplicationFrameWindow). Should be 132058 (XamlExplorerHostIslandWindow)
DEBUGWARNING - RPC process 14260 (HxOutlook.exe) (19:42:47.675) - Dummy-3 (22048):
Thread 332, build\x86_64\remote\ia2Support.cpp, IA2Support_inProcess_initialize, 121:
disabling IA2 support

DEBUGWARNING - NVDAObjects.NVDAObject._reportErrorInPreviousWord (19:42:49.316) - MainThread (15676):
Error fetching last character of previous word
Traceback (most recent call last):
  File "NVDAObjects\__init__.pyc", line 1050, in _reportErrorInPreviousWord
  File "documentBase.pyc", line 22, in makeTextInfo
  File "textInfos\offsets.pyc", line 470, in __init__
  File "textInfos\offsets.pyc", line 249, in _getCaretOffset
NotImplementedError
DEBUGWARNING - NVDAObjects.IAccessible.IAccessible._get_IAccessibleRole (19:42:52.233) - MainThread (15676):
accRole failed: (-2147024809, 'The parameter is incorrect.', (None, None, None, 0, None))
DEBUGWARNING - synthDriverHandler.getSynthList (19:42:54.571) - MainThread (15676):
Synthesizer 'mssp' doesn't pass the check, excluding from list
DEBUGWARNING - NVDAObjects.IAccessible.IAccessible._get_IAccessibleRole (19:42:55.277) - MainThread (15676):
accRole failed: (-2147024809, 'The parameter is incorrect.', (None, None, None, 0, None))
INFO - core.main (19:43:01.541) - MainThread (15676):
Exiting
INFO - config.ConfigManager.save (19:43:01.549) - MainThread (15676):
Base configuration saved
INFO - __main__ (19:43:03.112) - MainThread (15676):
NVDA exit
</code></pre>
</details>

If you require an example eml file, I can provide one.


Discussion:
cc @michaelDCurran  @feerrenrut  Given that this reportedly does not occur in 2021.2 it would be nice to triage this issue before 2021.3 is released.
I can't reproduce this from the instructions given on Windows 10. 
I can reproduce this with 2021.3beta2 and Windows 11. I don't know the technical NVDA term here, but it visually appears as if the focus/browsing mode has changed when returning focus to the mail window.
I could also reproduce this with 2021.2 and Windows 11
I can also reproduce this. In fact, it does not have to be an NVDA dialog, but simply focusing anything else in the OS and coming back to Mail is enough. However, you need to be scrolled enough down the email so that the message headers disappear. 
Because this only affect those on Windows 11 and 2021.3 is close to release, we will target 2022.1 for a fix. There is a work around for those who have already updated to Windows 11: Close and reopen the email message by pressing escape.

Resolving patch:
diff --git a/source/appModules/hxmail.py b/source/appModules/hxmail.py
--- a/source/appModules/hxmail.py
+++ b/source/appModules/hxmail.py
@@ -1,10 +1,11 @@
-#A part of NonVisual Desktop Access (NVDA)
-#Copyright (C) 2016-2019 NV Access Limited, Joseph Lee
-#This file is covered by the GNU General Public License.
-#See the file COPYING for more details.
+# A part of NonVisual Desktop Access (NVDA)
+# Copyright (C) 2016-2022 NV Access Limited, Joseph Lee
+# This file is covered by the GNU General Public License.
+# See the file COPYING for more details.
 
-"""An appModule for the Windows 10 Mail app"""
+"""An appModule for the Windows Mail app, for Windows 10 and 11."""
 
+import controlTypes
 import appModuleHandler
 from comtypes import COMError
 import UIAHandler
@@ -19,6 +20,13 @@ class MailWordDocument(WordDocument):
 
 	treeInterceptorClass=MailWordDocumentTreeInterceptor
 	def _get_shouldCreateTreeInterceptor(self):
+		# Newer versions of Mail (Windows 11+) correctly set the readonly state for emails
+		if controlTypes.State.READONLY in self.states:
+			return True
+
+		# For older versions of Mail (Windows 10), determine the readonly state
+		# based on being in an email with set headers, rather than draft headers.
+
 		# Locate the Reading pane in the ancestors
 		condition=UIAHandler.handler.clientObject.createPropertyCondition(UIAHandler.UIA_ClassNamePropertyId,"ReadingPaneModern")
 		walker=UIAHandler.handler.clientObject.createTreeWalker(condition)


Test patch: