Home Installation Release notes Language Reference Support Downloads Tutorials FutureBasic 5 Links

Asset Catalog - Image

This tutorial requires a couple of image files (sun.png & moon.png) which can be downloaded from here: Images

1. Launch Xcode and choose File > New > File… (⌘N)


 

2. In the template sheet...
a) click "macOS"
b) scroll down to the 'Resource' section and choose "Asset Catalog"
c) click the "Next" button


 

3. Click the "Create" button and save the file as "Assets" to your FB app folder

 

4. An empty asset catalog will be displayed. Click the '+' button at the bottom of the outline view and choose New Image Set from the menu


 

5. Give the image set a name

 

6. Show the Attributes Inspector (⌥⌘4). Uncheck 'Universal' and check 'Mac'

 

7. Change Appearance to 'Any, Dark'


 

8. We want the moon image to be displayed when we're in light mode so drag the moon image file to the '1x Any Appearance' box

 

9. Option-copy the image from '1x Any Appearance' to '2x Any Appearance'

 

10. Drag the sun image file to the '1x Dark' box and option-copy it to the '2x Dark' box

 

11. We're done with the Assets file so close it
 

12. In FutureBasic, create a new file and paste the following code

include resources "Assets.xcassets"

#plist NSAppleEventsUsageDescription @"Allow AppleEvents."

void local fn ToggleDarkMode
CFStringRef source = @"tell application \"System Events\"\ntell appearance preferences\nset dark mode to not dark mode\nend tell\nend tell\n"
AppleScriptRef script = fn AppleScriptWithSource( source )
fn AppleScriptExecute( script, NULL )
end fn

void local fn BuildWindow
window 1, @"Asset Catalog - Image", (0,0,480,270)
WindowSetContentMinSize( 1, fn CGSizeMake(100,100) )

button 1,,, @"", (215,110,50,50),, NSBezelStyleTexturedSquare
ButtonSetBordered( 1, NO )
ButtonSetImage( 1, fn ImageNamed( @"SunMoon" ) )
ViewSetAutoresizingMask( 1, NSViewMinXMargin + NSViewMaxXMargin + NSViewMinYMargin + NSViewMaxYMargin )
end fn

void local fn DoDialog( ev as long, tag as long )
select ( ev )
case _btnClick
select ( tag )
case 1 : fn ToggleDarkMode
end select
end select
end fn

fn BuildWindow

on dialog fn DoDialog

HandleEvents

 

11. Save the FB file to the same folder as the asset catalog and run it