# base/platform (vanilla-engine 0.4.0)

> https://vanillapunch.com/docs/vanilla-engine/0.4.0/api/base/platform/

Platform information. (the original base/platform.js)
- The original picks the platform and browser by looking at navigator.user_agent.
- The Rust version looks at user_agent with the same rules as the original on the web (wasm32), and uses the build target OS on desktop.
  The desktop browser is Chrome. The desktop version is tuned to give the same results as the original running on Chrome (fonts and glyph widths, primary pointer detection, page events,
  safe area, on-screen keyboard), and the original only shows this name in developer tools and statistics text, so browser branches in game code must also take the Chrome branch to get the same results.
  On Linux it is SteamDeck if running on a Steam Deck. It counts as a Steam Deck if the environment variable SteamDeck=1 that Steam sets when launching on a Steam Deck is present
  (Valve's Proton launch script tells the Steam Deck apart by this), or if, like Valve SteamOS's jupiter-hw-support,
  /sys/class/dmi/id/board_vendor is Valve and board_name is Jupiter (LCD) or Galileo (OLED).
- The original's getOrAddCanvas is done by platform/web.rs with the canvas id from the engine settings.
- get_resource_usage: on the web, adds up performance's resource entries as in the original. On desktop nothing is received over the network, so
  it lists the files the engine read as assets (asset byte reads, Engine::load_image, audio files) in the order read. The type is "file", and both the transfer size and the decoded size are the file's byte count.
  (fonts read from the system, such as the default and fallback fonts, are not listed, just like the original's system fonts)
  The old misspelled name get_resouce_usage remains as an alias calling the same function. (same in the original)
- get_safe_area_rect: the original reads the size of the canvas (or the window if none). The Rust version has no document object, so it takes that size. Insets on desktop are 0 like desktop Chrome.
- is_coarse_pointer: CSS pointer: coarse as in the original. On desktop, Chromium's detection was ported. (true on Windows in tablet mode or on a touch device without a mouse, on Linux when there is only a touchscreen
  without a mouse or touchpad, always false on Mac. platform/desktop.rs)
- is_desktop_web: the original reads the platform information anew on every call. The Rust version uses what it read at creation. (user_agent does not change)
  Desktop builds are not mobile and have no touch point count (navigator.maxTouchPoints), so it is always true.

## Platform (struct) (Clone, Debug, Default)

Platform information.

Example: Pick the platform and browser from user_agent

```rust
use vanilla::{BrowserType, Platform, PlatformType};

let mut platform = Platform::new();
platform.apply_user_agent("Mozilla/5.0 (iPhone; CPU iPhone OS 18_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.0 Mobile/15E148 Safari/604.1");
assert_eq!(platform.get_platform_name(), PlatformType::IOs);
assert_eq!(platform.get_platform_name().get_name(), "iOS");
assert_eq!(platform.get_browser_name(), BrowserType::Safari);
assert!(platform.is_mobile());
assert!(!platform.is_desktop_web());
```

Example: Decide whether to show shortcut hints from the engine's platform information

```rust
use vanilla::Engine;

fn is_shortcut_hint_visible(engine: &Engine) -> bool {
	let platform = engine.get_platform();
	platform.is_desktop_web() && !platform.is_coarse_pointer()
}
```

- `pub fn new() -> Platform` (constructor, since 0.3.2): Constructor. (also performs detection. The original calls get_platform_info after construction)
  - returns Platform: Platform information with detection done. (picked from navigator's user_agent on the web, from the build target OS on desktop)
- `pub fn get_platform_info(&mut self) -> (PlatformType, BrowserType)` (method, since 0.3.2): Detects and applies platform information. (platform, browser)
  - returns (PlatformType, BrowserType): The (platform, browser) detected again and stored. (the mobile flag is also stored again)
- `pub fn apply_user_agent(&mut self, user_agent: &str)` (method, since 0.3.2): Picks the platform and browser from user_agent. (same order as the regular expressions of the original getPlatformInfo, case-insensitive)
  - Also stores the mobile flag. (mobile if it contains mobi, android, iphone or ipad)
  - `user_agent` &str: The browser's user_agent string.
- `pub fn get_platform_name(&self) -> PlatformType` (method, since 0.3.2): Platform.
  - returns PlatformType: The detected platform.
- `pub fn get_browser_name(&self) -> BrowserType` (method, since 0.3.2): Browser.
  - returns BrowserType: The detected browser. (Chrome for desktop builds)
- `pub fn is_mobile(&self) -> bool` (method, since 0.3.2): Whether it is mobile.
  - returns bool: true if mobile. (by user_agent on the web; for desktop builds, when targeting Android, iOS or iPadOS)
- `pub fn is_desktop_web(&self) -> bool` (method, since 0.4.0): Whether it is desktop web. (used to pick out things shown only on desktop, such as mouse wheel and keyboard hints)
  - Not if mobile. Safari on iPad reports itself as Mac like a desktop, so a Mac with multiple touch points counts as an iPad.
  - returns bool: true if desktop web. (false if mobile, or a Mac with navigator.maxTouchPoints of 2 or more; always true for desktop builds)
- `pub fn is_coarse_pointer(&self) -> bool` (method, since 0.4.0): Whether the primary pointer is a finger. (CSS pointer: coarse, used to pick out things shown only on mouse screens, such as shortcut hints)
  - returns bool: true if the primary pointer is a finger. (read anew on every call, always false on Mac desktop)
- `pub fn get_resource_usage(&self) -> ResourceUsage` (method, since 0.4.0): Resource usage. (transfer size and decoded size of each file the web received)
  - returns ResourceUsage: List of files received so far and their totals. (gathered anew on every call; on desktop, the files the engine read as assets)
- `pub fn get_resouce_usage(&self) -> ResourceUsage` (method, since 0.4.0): Resource usage. (an alias keeping the old misspelled name, same as get_resource_usage)
  - returns ResourceUsage: Same value as get_resource_usage.
- `pub fn get_safe_area_rect(&self, area_size: Vector2) -> Rect` (method, since 0.4.0): Safe area. (the rectangle of area_size minus env(safe-area-inset-*), insets truncated to integers like the original parseInt)
  - The insets are CSS pixels, so give area_size in CSS pixels too. (like the original, the canvas CSS size clientWidth, clientHeight, not the drawing buffer size) Width and height are 0 or more.
  - `area_size` Vector2: Base size to subtract the insets from. (pixels, in place of the original's canvas size)
  - returns Rect: A rectangle starting at (left inset, top inset), with the left and right insets taken from the width and the top and bottom insets taken from the height.

  Example: On desktop the insets are 0, so it is the given size as is

  ```rust
  use vanilla::{Platform, Rect, Vector2};
  
  let platform = Platform::new();
  let safe_area_rect = platform.get_safe_area_rect(Vector2::create(800.0, 600.0));
  assert_eq!(safe_area_rect, Rect::create(0.0, 0.0, 800.0, 600.0));
  ```

## LoadedFile (struct) (Clone, Debug, PartialEq)

One received file. (the original loadedFiles' { path, name, type, transferSize, decodedSize })

Example: Find the largest file

```rust
use vanilla::base::platform::LoadedFile;
use vanilla::Platform;

let platform = Platform::new();
let resource_usage = platform.get_resource_usage();
let largest_file: Option<&LoadedFile> = resource_usage.loaded_files.iter().max_by(|first, second| first.decoded_size.total_cmp(&second.decoded_size));
if let Some(loaded_file) = largest_file {
	println!("{} {}", loaded_file.path, loaded_file.decoded_size);
}
```

- `pub path: String` (field, since 0.4.0): Path. (on the web, the URL that is the performance entry's name; on desktop, the asset path the engine read)
  - returns String
- `pub name: String` (field, since 0.4.0): File name, the last segment of the path. (split by / on the web, by / and \ on desktop)
  - returns String
- `pub resource_type: String` (field, since 0.4.0): Type. (initiatorType on the web, "file" on desktop)
  - returns String
- `pub transfer_size: f64` (field, since 0.4.0): Transfer size. (bytes, transferSize on the web, the file's byte count on desktop)
  - returns f64
- `pub decoded_size: f64` (field, since 0.4.0): Decoded size. (bytes, decodedBodySize on the web, the file's byte count on desktop)
  - returns f64

## ResourceUsage (struct) (Clone, Debug, Default, PartialEq)

Resource usage. (the original { totalTransferSize, totalDecodedSize, loadedFiles })

Example: The totals are the sums of each file's values

```rust
use vanilla::Platform;

let platform = Platform::new();
let resource_usage = platform.get_resource_usage();
let total_transfer_size: f64 = resource_usage.loaded_files.iter().map(|loaded_file| loaded_file.transfer_size).sum();
assert_eq!(total_transfer_size, resource_usage.total_transfer_size);
println!("{} {}", resource_usage.loaded_files.len(), resource_usage.total_decoded_size);
```

- `pub total_transfer_size: f64` (field, since 0.4.0): Total transfer size. (bytes)
  - returns f64
- `pub total_decoded_size: f64` (field, since 0.4.0): Total decoded size. (bytes)
  - returns f64
- `pub loaded_files: Vec<LoadedFile>` (field, since 0.4.0): List of received files. (in read order on desktop)
  - returns Vec<LoadedFile>

## Functions and constants

- `pub const SYSTEM_FONT_STRING: &str` (field, since 0.3.2): Common system font list. (the original SYSTEM_FONT_STRING, a web CSS font list)
  - returns &str

## PlatformType (enum) (Clone, Copy, Debug, PartialEq, Eq)

Platform type.

- `Windows`: Windows.
- `MacOs`: macOS. (macintosh or mac os x in user_agent on the web)
- `Android`: Android.
- `SteamDeck`: Steam Deck. (valve steam gameoverlay in user_agent on the web; on desktop, Linux identified as a Steam Deck)
- `IPadOs`: iPadOS. (ipad in user_agent on the web)
- `IOs`: iOS. (iphone or ipod in user_agent on the web)
- `Linux`: Linux. (not a Steam Deck)
- `Unknown`: None of the above.

- `pub fn get_name(&self) -> &'static str` (method, since 0.3.2): Name string, same as the original. ("Windows", "macOS" ...)
  - returns &'static str: Name string. ("Windows", "macOS", "Android", "SteamDeck", "iPadOS", "iOS", "Linux", "Unknown")

## BrowserType (enum) (Clone, Copy, Debug, PartialEq, Eq)

Browser type.

- `Chrome`: Chrome. (always this for desktop builds)
- `Edge`: Edge. (edg in user_agent on the web, checked before Chrome)
- `Firefox`: Firefox.
- `InternetExplorer`: Internet Explorer. (msie or trident in user_agent on the web)
- `Safari`: Safari. (on the web, user_agent containing safari without edg and chrome)
- `Unknown`: None of the above.

- `pub fn get_name(&self) -> &'static str` (method, since 0.3.2): Name string, same as the original. ("Chrome", "Edge" ...)
  - returns &'static str: Name string. ("Chrome", "Edge", "Firefox", "Internet Explorer", "Safari", "Unknown")
