Platforms to show: All Mac Windows Linux Cross-Platform

/MacBase/NSURL async icon loading


Required plugins for this example: MBS MacBase Plugin, MBS Main Plugin

You find this example project in your Plugins Download as a Xojo project file within the examples folder: /MacBase/NSURL async icon loading

This example is the version from Mon, 22th Nov 2015.

Project "NSURL async icon loading.xojo_binary_project"
Class App Inherits Application
Const kEditClear = "&Löschen"
Const kFileQuit = "Beenden"
Const kFileQuitShortcut = ""
End Class
Class MainWindow Inherits Window
Control List Inherits Listbox
ControlInstance List Inherits Listbox
EventHandler Sub Change() if me.ListIndex < 0 then image.Backdrop = nil else dim n as NSImageMBS = List.CellTag(me.ListIndex, 0) if n = nil then image.Backdrop = nil else n.setSize image.Width, image.Height image.Backdrop = n.CopyPictureWithMask end if end if End EventHandler
End Control
Control Image Inherits Canvas
ControlInstance Image Inherits Canvas
End Control
EventHandler Sub Open() dim appFolder as FolderItem = SpecialFolder.Applications dim appURL as new NSURLMBS(appFolder) dim e as NSErrorMBS dim items() as NSURLMBS = appURL.Items(e, true) if e<>nil then dim m as string = e.LocalizedDescription Break end if // let the plugin cache icons, so you can draw them quicker const PreCacheIcons = true dim keys() as string = array( NSURLMBS.NSURLEffectiveIconKey ) #if true then // ask for each on it's own for each item as NSURLMBS in items List.AddRow "", item.Name Counter = Counter + 1 dim tag as Variant = counter List.RowTag(List.LastIndex) = tag #if RBVersion >= 2013 then // requires delegates item.resourceValuesForKeys(keys, AddressOf GotIcon, tag, PreCacheIcons) #else Break #endif next #else // ask for all of them in one run for each item as NSURLMBS in items List.AddRow "", item.Name Counter = Counter + 1 dim tag as Variant = counter List.RowTag(List.LastIndex) = tag next NSURLMBS.URLsResourceValuesForKeys(items, keys, AddressOf GotIcons, nil, PreCacheIcons) #endif End EventHandler
Sub GotIcon(URL as NSURLMBS, keys() as String, Values as Dictionary, Error as NSErrorMBS, tag as variant) if ControlCount = 0 then // window close already! Return end if dim u as integer = List.ListCount-1 for i as integer = 0 to u if List.RowTag(i) = tag then // found dim n as NSImageMBS = values.Value(NSURLMBS.NSURLEffectiveIconKey) // keep icon List.CellTag(i, 0) = n n.setSize(16,16) List.RowPicture(i) = n.CopyPictureWithMask 'n.setSize image.Width, image.Height 'image.Backdrop = n.CopyPictureWithMask Return end if next End Sub
Sub GotIcons(URLs() as NSURLMBS, keys() as String, Values() as Dictionary, Errors() as NSErrorMBS, tag as variant) if ControlCount = 0 then // window close already! Return end if dim u as integer = values.Ubound for i as integer = 0 to u dim value as Dictionary = values(i) dim n as NSImageMBS = value.Value(NSURLMBS.NSURLEffectiveIconKey) // keep icon List.CellTag(i, 0) = n n.setSize(16,16) List.RowPicture(i) = n.CopyPictureWithMask next End Sub
Property Counter As Integer
End Class
MenuBar MenuBar1
MenuItem FileMenu = "&Ablage"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "&Bearbeiten"
MenuItem EditUndo = "&Rückgängig"
MenuItem UntitledMenu1 = "-"
MenuItem EditCut = "&Ausschneiden"
MenuItem EditCopy = "&Kopieren"
MenuItem EditPaste = "&Einfügen"
MenuItem EditClear = "#App.kEditClear"
MenuItem UntitledMenu0 = "-"
MenuItem EditSelectAll = "&Alles auswählen"
End MenuBar
End Project

See also:

The items on this page are in the following plugins: MBS MacBase Plugin.


The biggest plugin in space...