Struct Platform

Source
pub struct Platform;
Expand description

This class provides some platform related code that runs cross-platform. You might be able to do many of these things with rust or C#, but you might not be able to do them in as a portable manner as these methods do! https://stereokit.net/Pages/StereoKit/Platform.html

§Examples

use stereokit_rust::{util::{Platform}, system::TextContext};

Platform::keyboard_show(true, TextContext::Text);

let mut path = std::env::current_dir().expect("Current directory should be readable");
path.push("assets/textures/");
assert!(path.is_dir());
path.push("readme.md");
assert!(path.is_file());

let file_content = Platform::read_file_text(&path)
                                 .expect("File should be readable");
assert!(file_content.starts_with("# Images "));

assert!(Platform::write_file_text(path, file_content).is_ok());

Implementations§

Source§

impl Platform

Source

pub fn force_fallback_keyboard(force_fallback: bool)

Force the use of StereoKit’s built-in fallback keyboard instead of the system keyboard. This may be great for testing or look and feel matching, but the system keyboard should generally be preferred for accessibility reasons. https://stereokit.net/Pages/StereoKit/Platform/ForceFallbackKeyboard.html

see also platform_keyboard_set_force_fallback

§Examples
use stereokit_rust::util::Platform;

assert_eq!(Platform::get_force_fallback_keyboard(), false);

Platform::force_fallback_keyboard(true);
assert_eq!(Platform::get_force_fallback_keyboard(), true);

Platform::force_fallback_keyboard(false);
assert_eq!(Platform::get_force_fallback_keyboard(), false);
Source

pub fn file_picker<FS: FnMut(&str), FC: FnMut()>( mode: PickerMode, on_select_file: FS, on_cancel: FC, filters: &[impl AsRef<str>], )

TODO: UNSTABLE: Starts a file picker window! This will create a native file picker window if one is available in the current setup, and if it is not, it’ll create a fallback filepicker build using StereoKit’s UI.

Flatscreen apps will show traditional file pickers, and UWP has an OS provided file picker that works in MR. All others currently use the fallback system.

A note for UWP apps, UWP generally does not have permission to access random files, unless the user has chosen them with the picker! This picker properly handles permissions for individual files on UWP, but may have issues with files that reference other files, such as .gltf files with external textures. See Platform::write_file and [Platform.read_file] for manually reading and writing files in a cross-platfom manner. https://stereokit.net/Pages/StereoKit/Platform/FilePicker.html

  • mode - Are we trying to Open a file, or Save a file? This changes the appearance and behavior of the picker to support the specified action.
  • on_select_file - This Action will be called with the proper filename when the picker has successfully completed! On a cancel or close event, this Action is not called.
  • on_cancel - If the user cancels the file picker, or the picker is closed via FilePickerClose, this Action is called.
  • filters - A list of file extensions that the picker should filter for. This is in the format of “.glb” and is case insensitive.

see also platform_file_picker

Source

pub fn file_picker_sz<F: FnMut(bool, &str)>( mode: PickerMode, on_complete: F, filters: &[impl AsRef<str>], )

TODO: UNSTABLE: Starts a file picker window! This will create a native file picker window if one is available in the current setup, and if it is not, it’ll create a fallback filepicker build using StereoKit’s UI.

Flatscreen apps will show traditional file pickers, and UWP has an OS provided file picker that works in MR. All others currently use the fallback system. Some pickers will block the system and return right away, but others will stick around and let users continue to interact with the app.

A note for UWP apps, UWP generally does not have permission to access random files, unless the user has chosen them with the picker! This picker properly handles permissions for individual files on UWP, but may have issues with files that reference other files, such as .gltf files with external textures. See Platform::write_file and [Platform.read_file] for manually reading and writing files in a cross-platfom manner. https://stereokit.net/Pages/StereoKit/Platform/FilePicker.html

  • mode - Are we trying to Open a file, or Save a file? This changes the appearance and behavior of the picker to support the specified action.
  • on_complete - This action will be called when the file picker has finished, either via a cancel event, or from a confirm event. First parameter is a bool, where true indicates the presence of a valid filename, and false indicates a failure or cancel event.
  • filters - A list of file extensions that the picker should filter for. This is in the format of “.glb” and is case insensitive.

see also platform_file_picker_sz

Source

pub fn file_picker_close()

TODO: UNSTABLE: If the picker is visible, this will close it and immediately trigger a cancel event for the active picker. https://stereokit.net/Pages/StereoKit/Platform/FilePickerClose.html

see also platform_file_picker_close

Source

pub fn keyboard_show(show: bool, input_type: TextContext)

Request or hide a soft keyboard for the user to type on. StereoKit will surface OS provided soft keyboards where available, and use a fallback keyboard when not. On systems with physical keyboards, soft keyboards generally will not be shown if the user has interacted with their physical keyboard recently. https://stereokit.net/Pages/StereoKit/Platform/KeyboardShow.html

  • show - Tells whether or not to open or close the soft keyboard.
  • input_type - Soft keyboards can change layout to optimize for the type of text that’s required. StereoKit will request the soft keyboard layout that most closely represents the TextContext provided.

see also platform_keyboard_show

§Examples
use stereokit_rust::{util::Platform, system::TextContext};

// On desktop, this will not show the keyboard, as it is assumed that the user has a physical keyboard.
Platform::keyboard_show(true, TextContext::Text);
assert_eq!(Platform::is_keyboard_visible(), false);

Platform::keyboard_show(false, TextContext::Text);
assert_eq!(Platform::is_keyboard_visible(), false);
Source

pub fn keyboard_set_layout( keyboard_type: TextContext, keyboard_layouts: &Vec<&str>, ) -> bool

Replace the default keyboard type with a custom layout. https://stereokit.net/Pages/StereoKit/Platform/KeyboardSetLayout.html

  • keyboard_type - The type of keyboard to replace.
  • keyboard_layouts - Custom keyboard layout to replace the defualt layout.

Returns true if keyboard type was swapped with the provided layout. see also platform_keyboard_set_layout

§Examples
use stereokit_rust::{util::Platform, system::TextContext};

pub const FR_KEY_TEXT: &str = r#"²|&|é|"|'|(|\-|è|_|ç|à|)|=|{|}|spr:sk/ui/backspace-\b-8-3|spr:sk/ui/close----close
Tab-\t-9-3|a|z|e|r|t|y|u|i|o|p|^|$|[|]|\|
Entrée-\n-13-4|q|s|d|f|g|h|j|k|l|m|ù|*|#|Entrée-\n-13-3
spr:sk/ui/shift--16-3-go_1|<|w|x|c|v|b|n|,|;|:|!|`|@|spr:sk/ui/shift--16-2-go_1|spr:sk/ui/arrow_up--38
Ctrl--17-4-mod|Cmd--91-3|Alt--18-3-go_2| - -32-13|Alt--18-3-go_2|Ctrl--17-3-mod|spr:sk/ui/arrow_left--37|spr:sk/ui/arrow_down--40|spr:sk/ui/arrow_right--39|"#;

pub const FR_KEY_TEXT_SHIFT: &str = r#"@|1|2|3|4|5|6|7|8|9|0|°|+|Æ|Œ|spr:sk/ui/backspace-\b-8-3|spr:sk/ui/close----close
Tab-\t-9-3|A|Z|E|R|T|Y|U|I|O|P|¨|£|Ê|É|È
Entrée-\n-13-4|Q|S|D|F|G|H|J|K|L|M|%|µ|Ç|Entrée-\n-13-3
spr:sk/ui/shift--16-3-go_0|>|W|X|C|V|B|N|?|.|/|§|À|Ô|spr:sk/ui/shift--16-2-go_0|spr:sk/ui/arrow_up--38
Ctrl--17-4-mod|Cmd--91-3|Alt--18-3-go_2| - -32-13|Alt--18-3-go_2|Ctrl--17-3-mod|spr:sk/ui/arrow_left--37|spr:sk/ui/arrow_down--40|spr:sk/ui/arrow_right--39|"#;

pub const FR_KEY_TEXT_ALT: &str = r#"*|/|~|#|{|[|\||`|\\|^|@|]|}|æ|œ|spr:sk/ui/backspace-\b-8-3|spr:sk/ui/close----close
Tab-\t-9-3|à|â|ä|ç|é|è|ê|ë|î|ï|ô|ö|«|»|¤
Entrée-\n-13-4|ù|û|ü|ÿ|À|Â|Ä|Ç|É|È|Ê|Ë|%|Entrée-\n-13-3
spr:sk/ui/shift--16-3-go_1|Î|Ï|Ô|Ö|Ù|Û|Ü|Ÿ|$|£|€|¥|✋|spr:sk/ui/shift--16-2-go_1|spr:sk/ui/arrow_up--38
Ctrl--17-4-mod|Cmd--91-3|Alt--18-3-go_0| - -32-13|Alt--18-3-go_0|Ctrl--17-3-mod|spr:sk/ui/arrow_left--37|spr:sk/ui/arrow_down--40|spr:sk/ui/arrow_right--39|"#;

let keyboard_layouts = vec![FR_KEY_TEXT, FR_KEY_TEXT_SHIFT, FR_KEY_TEXT_ALT];

assert_eq!(Platform::keyboard_set_layout(TextContext::Text, &keyboard_layouts), true);
Source

pub fn read_file_text<'a>( filename: impl AsRef<Path>, ) -> Result<&'a str, StereoKitError>

Reads the entire contents of the file as a UTF-8 string, taking advantage of any permissions that may have been granted by Platform::file_picker(_sz?). Returns Err on failure. https://stereokit.net/Pages/StereoKit/Platform/ReadFileText.html

  • filename - The path to the file to read.

see also platform_read_file

§Examples
use stereokit_rust::{util::{Platform}, system::TextContext};

let mut path = std::env::current_dir().expect("Current directory should be readable");
path.push("config.toml");
assert!(path.is_file());

let file_content = Platform::read_file_text(&path)
                                 .expect("File should be readable");
assert!(file_content.starts_with("[env]"));
Source

pub fn read_file<'a>( filename: impl AsRef<Path>, ) -> Result<&'a [u8], StereoKitError>

Reads the entire contents of the file as a byte array, taking advantage of any permissions that may have been granted by Platform.FilePicker. https://stereokit.net/Pages/StereoKit/Platform/ReadFile.html

  • filename - The path to the file to read.

see also platform_read_file

§Examples
use stereokit_rust::{util::{Platform}, system::TextContext};

let mut path = std::env::current_dir().expect("Current directory should be readable");
path.push("assets/textures/");
assert!(path.is_dir());
path.push("readme.md");
assert!(path.is_file());

let file_content = Platform::read_file(&path)
                                 .expect("File should be readable");
assert!(file_content.starts_with(b"# Images "));
Source

pub fn write_file_text<S: AsRef<str>>( filename: impl AsRef<Path>, text: S, ) -> Result<bool, StereoKitError>

Writes a UTF-8 text file to the filesystem, taking advantage of any permissions that may have been granted by Platform::file_picker. https://stereokit.net/Pages/StereoKit/Platform/WriteFile.html

  • filename - The path to the file to write.
  • text - A string to write to the file. This gets converted to a UTF-8 encoding.

see also platform_write_file_text

§Examples
use stereokit_rust::{util::{Platform}, system::TextContext};

let mut path = std::env::current_dir().expect("Current directory should be readable");
path.push("assets/icons/");
assert!(path.is_dir());
path.push("readme.md");
assert!(path.is_file());

let file_content = Platform::read_file_text(&path)
                                 .expect("File should be readable");
assert!(file_content.starts_with("# Images "));

assert!(Platform::write_file_text(path, file_content).is_ok());
Source

pub fn write_file( filename: impl AsRef<Path>, data: &[u8], ) -> Result<bool, StereoKitError>

Writes an array of bytes to the filesystem, taking advantage of any permissions that may have been granted by Platform::file_picker. https://stereokit.net/Pages/StereoKit/Platform/WriteFile.html

  • filename - The path to the file to write.
  • data - The data to write to the file.

see also platform_write_file

§Examples
use stereokit_rust::{util::{Platform}, system::TextContext};

let mut path = std::env::current_dir().expect("Current directory should be readable");
path.push("assets/icons/");
assert!(path.is_dir());
path.push("readme.md");
assert!(path.is_file());

let file_content = Platform::read_file(&path)
                                 .expect("File should be readable");
assert!(file_content.starts_with(b"# Images "));

assert!(Platform::write_file(path, file_content).is_ok());
Source

pub fn get_file_picker_visible() -> bool

TODO: UNSTABLE: This will check if the file picker interface is currently visible. Some pickers will never show this, as they block the application until the picker has completed. https://stereokit.net/Pages/StereoKit/Platform/FilePickerVisible.html

see also platform_file_picker_visible

§Examples
use stereokit_rust::util::Platform;

assert_eq!(Platform::get_file_picker_visible(), false);
Source

pub fn get_force_fallback_keyboard() -> bool

Force the use of StereoKit’s built-in fallback keyboard instead of the system keyboard. This may be great for testing or look and feel matching, but the system keyboard should generally be preferred for accessibility reasons. https://stereokit.net/Pages/StereoKit/Platform/ForceFallbackKeyboard.html

see also platform_keyboard_get_force_fallback see example Platform::force_fallback_keyboard

Source

pub fn is_keyboard_visible() -> bool

Check if a soft keyboard is currently visible. This may be an OS provided keyboard or StereoKit’s fallback keyboard, but will not indicate the presence of a physical keyboard. https://stereokit.net/Pages/StereoKit/Platform/KeyboardVisible.html

see also platform_keyboard_visible see example Platform::keyboard_show

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Sync + Send>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more