WinUIpad: Word Count, Text Cursor Position, App Shutdown
I was hoping to keep this to weekly updates, but two weekends came and went with me struggling to figure out why WinUIpad was crashing on app exit in certain conditions. So another few weeks with the Windows App SDK and WinUI 3 was another few weeks of frustration, as always. But that’s the job, and this new update includes basic implementations of the Word count and Line and column position displays, plus one of the dumbest and most difficult tasks to do properly with this framework, app shutdown.
Here’s what’s new.
Word count
Looking over my WinUIpad to-do list, I decided to tackle a few of what I felt would be the easier tasks. But as is so often the case with the Windows App SDK, I had mixed results.
The simplest, perhaps, is the Word count display in the status bar: As the user types, I just need to update the count. This is a feature I added to the first, Windows Forms-based versions of .NETpad dating back to perhaps 2019. And it’s been in each version ever since.
For this version of what’s now called WinUIpad, I decided to look at the code I had written or, in this case, obtained previously. And I found a March 2020 update to .NETpad that uses a regular expression to output the word count. Which is how I know I didn’t write it.
You’ve heard the phrase that the best phone is the one you have with you. In a similar vein, the best code is often the code you don’t need to write. And though I’m sure I will revisit this later as part of an app-wide refactoring, the goal for now it not get stuck on details and to just get it done. So I went back to the well.
As with previous versions of the app, a lot of the action takes place in the TextChanging() event handler for the main TextBox. Three things now happen when this event handler fires: It changes the Document object instance’s TextChanging property to True, changes the Document object instance’s Contents property to the TextBox’s Text contents, and, now, it also runs a new helper method called UpdateCount().
As for the new UpdateCount() method, this is based on the aforementioned 5+ year-old code.
That TextBox has two event handlers (the other is SelectionChanged() and is discussed below), but UpdateCount() only needs to be called from here. So that’s that. For now.
Line and column position
This I haven’t completely solved, though I’ve left the code I have so far in there for now. The issue is that the Windows App SDK/WinUI 3 TextBox control doesn’t support WPF’s GetLineIndexFromCharacterIndex(), CaretIndex(), and GetCharacterIndexFromLineIndex() methods, and so you have to do all that work yourself. I know, crazy.
Since WPF is open source, it’s possible to look at the source code for those three methods, but for now I am trying to just figure it out using GitHub Copilot, which is an endless circle jerk of errors when it comes to Windows App SDK/WinUI 3, and my own limited capabilities.
Anyway, as per the Upda...
The post WinUIpad: Word Count, Text Cursor Position, App Shutdown appeared first on Thurrott.com.