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.
- AddXXX(...)
- AddXXXAt(index,...)
- RemoveXXXAt(index)
- FirstXXXIndex This is not in my implementation list, API 2.0 almost universally uses 0-based indexing, making 'FirstXXXIndex' somewhat redundant
- LastXXXIndex
- XXXCount
- XXXAt(index)
- IndexOfXXX(value, [starting index])
- XXXs - iterable list Optional
- XXX(index) Optional
DesktopApplication
As of Xojo 2022 Release 3, DesktopApplication has the following for Window:
- WindowAt(index)
- WindowCount
- Windows(index)
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:
- LastWindowIndex
LastWindowIndex
Public Function LastWindowIndex(extends da as DesktopApplication) As integer
return da.WindowCount-1
End Function
DesktopWindow
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:
- ControlCount - the count of controls in the window
- Control(index)
- Controls [Iterable]
So here's what we will be adding:
- LastControlIndex
- ControlAt(index)
LastControlIndex
Public Function LastControlIndex(extends dw as DesktopWindow) As integer
return dw.ControlCount-1
End Function
ControlAt(index)
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
end
return dw.control(index)
End Function
FolderItem
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:
- DriveCount Shared
- DriveAt(index) Shared
- LastDriveIndex Shared
- Count - the count of child items in the FolderItem
- ChildAt(index)
- Children [Iterable]
So here's what we will be adding:
- LastChildIndex
Additionally, there are two very basic 'properties' I use alot but which are missing from FolderItem.
- Extension - the file extension (if it is a file, for non-files, this is blank)
- ShortName - the filename without the extension (if it is a file, for non-files, this is the Name)
LastChildIndex
Public Function LastChildIndex(extends fi as FolderItem) As integer
return fi.count-1
End Function
Extension
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
ShortName
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.