API 2.0 - Filling the gaps with Extensions - DesktopApplication, DesktopWindow & FolderItem Revised 10/28/2022

In my earlier article on API 2.0 - What and Why?, I discuss gaps and inconsistencies in API 2.0's implementation and that these can be largely filled with class extensions.  In my next article I flesh-out an example of doing this for DesktopPopupMenu (and DesktopComboBox by extension).  In this article I'm going to attack DesktopApplication and FolderItem.  In one case this will be filling-in the missing API 2.0 constructs, in the other I will also add a couple very commonly useful utility methods with extensions. 

To recap, this is a basic listing of the type of methods & properties API 2.0 implies for working with lists of items consistently.


As of Xojo 2022 Release 3, DesktopApplication has the following for Window:

Now many of the typical operations we might imagine that we want to add, don't really apply here, because we don't explicitly 'Add', or 'Remove' windows to this list, like other objects, and the list of windows is managed automatically by the framework.  So let's add the following extension to DesktopApplication: 


Public Function LastWindowIndex(extends da as DesktopApplication) As integer
 return da.WindowCount-1
End Function


Now what about DesktopWindow?  DesktopWindow also is somewhat unusual as 'Adding' controls and is not the same as adding items to an array or a menu.  Also 'Removing' controls is a different animal alltogether.  In the future I may look more deeply at a more complete implementation for managing controls, but the purpose doesn't seem clear to me at this time.  So I'll focus on the most relevant consistency-related addition(s).  As of Xojo 2022 Release 3, DesktopWindow has the following items of interest:

So here's what we will be adding:


Public Function LastControlIndex(extends dw as DesktopWindow) As integer
 return dw.ControlCount-1
End Function


Public Function ControlAt(extends dw as DesktopWindow, index as integer) As Object
 if(index<0 or index>=dw.ControlCount) then
  var e as new OutOfBoundsException
  e.message="No control exists for index "+index.toString
  raise e
 return dw.control(index)
End Function


Now what about FolderItem?  FolderItem also is also somewhat unusual, as 'Adding' and 'Removing' files are more carefully managed activities, and 'Adding' and 'Removing' drives is totally different.  As of Xojo 2022 Release 3, FolderItem has the following items of interest:

So here's what we will be adding:

Additionally, there are two very basic 'properties' I use alot but which are missing from FolderItem.


Public Function LastChildIndex(extends fi as FolderItem) As integer
 return fi.count-1
End Function


Public Function Extension(extends fi as FolderItem) As string
 if(fi.IsFolder) then return ""
 var dotCount as integer = fi.name.CountFields(".")
 return fi.name.NthField(".",dotCount)
End Function


Public Function ShortName(extends fi as FolderItem) As string
 if(fi.IsFolder) then return ""
 var fullName as string = fi.name
 var nameLength as integer = fullName.Length
 var dotCount as integer = fi.name.CountFields(".")
 var extLen as integer=1+fullName.NthField(".",dotCount).length
 if(dotCount<=1) then extLen = 0
 return fullname.left(nameLength-extLen)
End Function

That's where I'm ending this installment, with DesktopApplication and FolderItem extensions.