Keiwando.NFSO

class

NativeFileSOMacWin

public class NativeFileSOMacWin : INativeFileSO 

Summary

Provides methods for native file open and save functionality which is shared between Windows and macOS.

Examples

The following example demonstrates how to use the NativeFileSOMacWin class in order to allow the user to select multiple paths of files to be opened.

 using Keiwando.NFSO;
 
 public class OpenPathsTest {

 	public static void Main() {

 		// We want the user to select paths to plain text files.
 		SupportedFileType[] supportedFileTypes = {
 			SupportedFileType.PlainText
 		};
 
 		bool multiSelect = true; // Can select multiple paths at once
 		string title = "Custom Title"; // The title of the panel
 		string directory = ""; // Remember and reset to the previously selected directory
 		NativeFileSOMacWin.shared.SelectOpenPaths(supportedFileTypes, 
 		  mutltiSelect, title, directory,
 		  delegate(bool pathsWereSelected, string[] paths) {
 			if (pathsWereSelected) {
 				// Process the information within the paths array.
 			} else {
 				// The file selection was cancelled.
 			}
 		});
		}
 }
See also NativeFileSO for examples of how to use the more general API that also applies to mobile platforms.

Remarks

Compared to the NativeFileSO class, the NativeFileSOMacWin class provides additional methods which cannot be implemented in the same way on mobile platforms due to the different available native APIs.

For example, on iOS and Android, the path to a selected file to be opened is only temporarily valid due to native security features and access restrictions. Therefore, the entire file has to be copied into memory before its data can be handed over to the caller of the method.

On desktop platforms, however, it is possible to provide methods that simply return the chosen file path for a save or open operation. This then allows for more custom processing of the selected files. For example, the file contents can be loaded and processed in smaller chunks which is more memory efficient and a preferred solution compared to loading the entire file contents into memory, which should only be done if necessary.

This class is currently compatible with Windows and macOS. Attempting Attempting to call the class methods on unsupported platforms will result in a NullReferenceException.

Thread safety is not guaranteed!

Note for macOS: The Open and Save API methods that take a completion callback display the NSOpenPanel modally as a sheet, which makes it anchored to the top of the window and non-draggable. The ..Sync variants of those calls, however, use a floating panel which is detached from the main application window and can be dragged around by the user.

If the visual style of the panel is of importance to you, simply call the respective method.

Properties

public static NativeFileSOMacWin shared

The shared instance through which the API should be accessed.

Methods

public static OpenedFile FileFromPath (string)

Loads the contents of a file at the specified path into an instance of the OpenedFile class.

public static OpenedFile[] FilesFromPaths (string[])

Loads the contents of multiple files at the specified paths into instances of the OpenedFile class.

public static void SaveFileToPath (FileToSave, string)

Copies the specified file to the given path.

public void OpenFile (SupportedFileType[], Action<bool, OpenedFile>)

Presents a native dialog to the user which allows them to select a single file to be opened. The selected file contents are then loaded into memory managed by an instance of the OpenedFile class.

public void OpenFiles (SupportedFileType[], Action<bool, OpenedFile[]>)

Presents a native dialog to the user which allows them to select multiple files to be opened at once. The selected file contents are then loaded into memory managed by instances of the OpenedFile class.

public void SaveFile (FileToSave)

Presents a native dialog to the user which allows them to select a save location for the specified file and copies the file to that location.

public void OpenFiles (SupportedFileType[], bool, string, string, Action<bool, OpenedFile[]>)

Presents a native dialog to the user which allows them to select multiple files to be opened and loads the selected files into instances of the OpenedFile class.

public OpenedFile[] OpenFilesSync (SupportedFileType[], bool, string, string)

Presents a native dialog to the user which allows them to select multiple files to be opened and loads the selected files into instances of the OpenedFile class.

public void SelectOpenPaths (SupportedFileType[], bool, string, string, Action<bool, string[]>)

Presents a native dialog to the user which allows them to select multiple file paths for opening files.

public string[] SelectOpenPathsSync (SupportedFileType[], bool, string, string)

Presents a native dialog to the user which allows them to select multiple file paths for opening files.

public void SaveFile (FileToSave, string, string)

Presents a native dialog to the user which allows them to select a save location for the specified file and copies the file to that location.

public void SelectSavePath (FileToSave, string, string, Action<bool, string>)

Presents a native dialog to the user which allows them to select a save location for the specified file.

public string SelectSavePathSync (FileToSave, string, string)

Presents a native dialog to the user which allows them to select a save location for the specified file.