Skip to content

What each Swift file does

Dav edited this page Aug 28, 2020 · 3 revisions

PlaygroundPage.swift

The root SwiftUI view is defined here. It calls another view which is inside a user module. Keeping all the Swift files in a user module has the benefit that all the code is available amongst these files without having to modify file access levels. The view you call from root has to be marked as Public as well as its initialiser and body.

Code placed in a user module

These files do most of the work for you. Define some buttons in an array of type Buttons. Call CreateButtonsFromArray in a HStack or VStack for example and pass in this array. The buttons will be created for you. When you press them they will send the commands defined in the array.

Connection To Server.swift

This defines the serverIPAddress - make sure it matches the IP address of the computer hosting the Python server DirectInputServer. The class TCPClient creates a connection to the server so the Playground can communicate to the server. Every time a message is sent it is sent with sharedTCPClient of which only one instance is created.

Data Model CustomButton.swift

It's easier to define data in one place and use separate code to define presentation. Arrays of buttons are used and their properties are defined here.

Data NumPad Buttons.swift

Array of buttons that will be used with CreateButtonsFromArray to create a keyboard number pad.

Data for Regular Buttons.swift

Arrays of buttons that will be used with CreateButtonsFromArray to create stacks of buttons.

Enum Scancodes.swift

Defines every keystroke that can be sent

View ButtonView.swift

This view defines a SwiftUI button and its properties that can be modified.

View Connection to Server.swift

A row that displays the Server IP address and a button to gracefully stop the connection.

View CreateButtonsFromArray.swift

Pass an array of type buttons to this struct and for each array item a SwiftUI button will be created.

View FirstTabView.swift

This view provides the presentation for the first tab.

View FooterView.swift

This row shows messages sent to DirectInputServer.

View NumPad.swift

Call NumPad() in a containing view and a number pad on a keyboard will be created.

View SecondTabView.swift

This view provides the presentation for the second tab.

View TabViews.swift

This is called from the PlaygroundPage and creates a TabView. Here you can define how many tabs, their icons and tab names. For each tab you can define its content.