Skip to main content

ComposedView

Struct ComposedView 

Source
pub struct ComposedView {
    pub metadata: *const c_void,
    pub witness_table: *const c_void,
    pub id: i64,
    /* private fields */
}
Expand description

A runtime-built Swift view type with any mix of property wrappers.

use swiftui_native::state::{ComposedBuilder, ColorSchemeKey};
use swiftui_native::views;

let mut b = ComposedBuilder::new();
let count = b.state(0_i64);
let dark = b.app_storage::<bool>("dark", false);
let scheme = b.environment::<ColorSchemeKey>();
let v = b.build(move |p| {
    views::text(&format!("{} {} {:?}", p.state(count), p.stored(dark), p.env(scheme)))
});
v.set_state(count, 1);

Reading a property as the wrong kind, or at the wrong type, does not compile — which is what the handles buy over addressing properties by number:

ⓘ
use swiftui_native::state::ComposedBuilder;
use swiftui_native::views;

let mut b = ComposedBuilder::new();
let count = b.state(0_i64);
let v = b.build(|_| views::text("x"));
let _: bool = v.stored(count); // a `@State` is not a `StoredProp`

Each of StatefulView, StoredView and EnvironmentView is the one-property case of this; they are kept because they are simpler to use when one is all that is wanted.

Fields§

§metadata: *const c_void§witness_table: *const c_void§id: i64

Implementations§

Source§

impl ComposedView

Source

pub fn set_state<T: StateValue>(&self, prop: StateProp<T>, value: T) -> bool

Write the @State at index i, re-running the body.

Returns false before the first body evaluation, when there is no binding to write through yet — the same contract as StatefulView::set.

Source

pub fn stored<T: StoredValue>(&self, prop: StoredProp<T>) -> T

Read an @AppStorage from outside the body.

Source

pub fn set_stored<T: StoredValue>(&self, prop: StoredProp<T>, value: T)

Write an @AppStorage, which persists and re-runs the body.

Source

pub fn set_focus(&self, prop: FocusProp, value: bool) -> bool

Move focus to — or away from — the @FocusState at index i.

Writing true focuses the field the property is attached to, which is what @FocusState var focused: Bool; focused = true does in Swift. Returns false before the first body evaluation, when there is no installed property to write to yet.

Source

pub fn focus(&self, prop: FocusProp) -> bool

Whether the @FocusState at index i has focus, read from outside the body.

Source

pub fn runs(&self) -> usize

How many times SwiftUI has evaluated the body.

Source

pub fn as_view(&self) -> ViewHandle

Wrap the instance so it can be hosted like any other view.

Trait Implementations§

Source§

impl Drop for ComposedView

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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 = !

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

fn try_from(value: U) -> Result<T, !>

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.