swiftui-native 0.0.1

SwiftUI from pure Rust — no Swift bridge, no shim, only swift-runtime calls
Documentation
//! Modifiers taking a plain SwiftUI enum.

use super::*;
use crate::abi;
use crate::resolve;
//
// `Visibility`, `Prominence` and friends have cases rather than static
// properties, so there is no getter to call. The case is selected through the
// enum's own `destructiveInjectEnumTag` witness — writing the index by hand
// happens to work for some and produces a case that does not exist for others.
//
// How it is then *passed* depends on the enum, and there is no way to ask:
// `Visibility` is frozen and travels in a register, while `ColorScheme`,
// `DynamicTypeSize`, `Prominence` and `TextSelectionAffinity` are not and
// travel by pointer. All four are one byte, so size does not tell them apart —
// getting it wrong crashes one way and silently yields `Visibility.()` the
// other, which is why each is spelled out here.

/// `SwiftUI.DynamicTypeSize`, in declaration order.
///
/// An enum with no associated values is laid out as its case index, so the
/// discriminant here *is* the representation.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum DynamicTypeSize {
    XSmall = 0,
    Small = 1,
    Medium = 2,
    Large = 3,
    XLarge = 4,
    XxLarge = 5,
    XxxLarge = 6,
    Accessibility1 = 7,
    Accessibility2 = 8,
    Accessibility3 = 9,
    Accessibility4 = 10,
    Accessibility5 = 11,
}

/// `SwiftUI.LayoutDirection`, in declaration order.
///
/// An enum with no associated values is laid out as its case index, so the
/// discriminant here *is* the representation.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum LayoutDirection {
    LeftToRight = 0,
    RightToLeft = 1,
}

/// `SwiftUI.Prominence`, in declaration order.
///
/// An enum with no associated values is laid out as its case index, so the
/// discriminant here *is* the representation.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Prominence {
    Standard = 0,
    Increased = 1,
}

/// `SwiftUI.TextSelectionAffinity`, in declaration order.
///
/// An enum with no associated values is laid out as its case index, so the
/// discriminant here *is* the representation.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum TextSelectionAffinity {
    Automatic = 0,
    Upstream = 1,
    Downstream = 2,
}

/// `SwiftUI.TouchBarItemPresence`, in declaration order.
///
/// Every case carries the item's identifier, so all three are payload cases —
/// and payload cases are numbered before empty ones, which here means the tags
/// are still the declaration order. That puts `required` first, not `default`.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum TouchBarItemPresence {
    Required = 0,
    Default = 1,
    Optional = 2,
}

/// `SwiftUI.Visibility`, in declaration order.
///
/// An enum with no associated values is laid out as its case index, so the
/// discriminant here *is* the representation.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Visibility {
    Automatic = 0,
    Visible = 1,
    Hidden = 2,
}

/// Apply `.colorScheme(_:)`.
pub fn color_scheme(view: &ViewHandle, value: ColorScheme) -> ViewHandle {
    unsafe {
        let meta = abi::metadata0(resolve::color_scheme_ma());
        let mut arg = vec![0u8; abi::type_bytes(meta)];
        abi::inject_enum_tag(&mut arg, meta, value as u32);
        opaque_modifier(
            resolve::color_scheme_mod_fn(),
            resolve::color_scheme_oret(),
            view,
            &[arg.as_mut_ptr() as u64],
            [0.0; 4],
        )
    }
}

/// Apply `.dynamicTypeSize(_:)`.
pub fn dynamic_type_size(view: &ViewHandle, value: DynamicTypeSize) -> ViewHandle {
    unsafe {
        let meta = abi::metadata0(resolve::dynamic_type_size_ma());
        let mut arg = vec![0u8; abi::type_bytes(meta)];
        abi::inject_enum_tag(&mut arg, meta, value as u32);
        opaque_modifier(
            resolve::dynamic_type_size_fn(),
            resolve::dynamic_type_size_oret(),
            view,
            &[arg.as_mut_ptr() as u64],
            [0.0; 4],
        )
    }
}

/// Apply `.headerProminence(_:)`.
pub fn header_prominence(view: &ViewHandle, value: Prominence) -> ViewHandle {
    unsafe {
        let meta = abi::metadata0(resolve::prominence_ma());
        let mut arg = vec![0u8; abi::type_bytes(meta)];
        abi::inject_enum_tag(&mut arg, meta, value as u32);
        opaque_modifier(
            resolve::header_prominence_fn(),
            resolve::header_prominence_oret(),
            view,
            &[arg.as_mut_ptr() as u64],
            [0.0; 4],
        )
    }
}

/// Apply `.labelsVisibility(_:)`.
pub fn labels_visibility(view: &ViewHandle, value: Visibility) -> ViewHandle {
    unsafe {
        let meta = abi::metadata0(resolve::visibility_ma());
        let arg = abi::enum_argument(meta, value as u32);
        opaque_modifier(
            resolve::labels_visibility_fn(),
            resolve::labels_visibility_oret(),
            view,
            &[arg],
            [0.0; 4],
        )
    }
}

/// Apply `.menuIndicator(_:)`.
pub fn menu_indicator(view: &ViewHandle, value: Visibility) -> ViewHandle {
    unsafe {
        let meta = abi::metadata0(resolve::visibility_ma());
        let arg = abi::enum_argument(meta, value as u32);
        opaque_modifier(
            resolve::menu_indicator_fn(),
            resolve::menu_indicator_oret(),
            view,
            &[arg],
            [0.0; 4],
        )
    }
}

/// Apply `.persistentSystemOverlays(_:)`.
pub fn persistent_system_overlays(view: &ViewHandle, value: Visibility) -> ViewHandle {
    unsafe {
        let meta = abi::metadata0(resolve::visibility_ma());
        let arg = abi::enum_argument(meta, value as u32);
        opaque_modifier(
            resolve::persistent_system_overlays_fn(),
            resolve::persistent_system_overlays_oret(),
            view,
            &[arg],
            [0.0; 4],
        )
    }
}

/// Apply `.pointerVisibility(_:)`.
pub fn pointer_visibility(view: &ViewHandle, value: Visibility) -> ViewHandle {
    unsafe {
        let meta = abi::metadata0(resolve::visibility_ma());
        let arg = abi::enum_argument(meta, value as u32);
        opaque_modifier(
            resolve::pointer_visibility_fn(),
            resolve::pointer_visibility_oret(),
            view,
            &[arg],
            [0.0; 4],
        )
    }
}

/// Apply `.presentationDragIndicator(_:)`.
pub fn presentation_drag_indicator(view: &ViewHandle, value: Visibility) -> ViewHandle {
    unsafe {
        let meta = abi::metadata0(resolve::visibility_ma());
        let arg = abi::enum_argument(meta, value as u32);
        opaque_modifier(
            resolve::presentation_drag_indicator_fn(),
            resolve::presentation_drag_indicator_oret(),
            view,
            &[arg],
            [0.0; 4],
        )
    }
}

/// Apply `.scrollContentBackground(_:)`.
pub fn scroll_content_background(view: &ViewHandle, value: Visibility) -> ViewHandle {
    unsafe {
        let meta = abi::metadata0(resolve::visibility_ma());
        let arg = abi::enum_argument(meta, value as u32);
        opaque_modifier(
            resolve::scroll_content_background_fn(),
            resolve::scroll_content_background_oret(),
            view,
            &[arg],
            [0.0; 4],
        )
    }
}

/// Apply `.sliderThumbVisibility(_:)`.
pub fn slider_thumb_visibility(view: &ViewHandle, value: Visibility) -> ViewHandle {
    unsafe {
        let meta = abi::metadata0(resolve::visibility_ma());
        let arg = abi::enum_argument(meta, value as u32);
        opaque_modifier(
            resolve::slider_thumb_visibility_fn(),
            resolve::slider_thumb_visibility_oret(),
            view,
            &[arg],
            [0.0; 4],
        )
    }
}

/// Apply `.tableColumnHeaders(_:)`.
pub fn table_column_headers(view: &ViewHandle, value: Visibility) -> ViewHandle {
    unsafe {
        let meta = abi::metadata0(resolve::visibility_ma());
        let arg = abi::enum_argument(meta, value as u32);
        opaque_modifier(
            resolve::table_column_headers_fn(),
            resolve::table_column_headers_oret(),
            view,
            &[arg],
            [0.0; 4],
        )
    }
}

/// Apply `.textSelectionAffinity(_:)`.
pub fn text_selection_affinity(view: &ViewHandle, value: TextSelectionAffinity) -> ViewHandle {
    unsafe {
        let meta = abi::metadata0(resolve::text_selection_affinity_ma());
        let mut arg = vec![0u8; abi::type_bytes(meta)];
        abi::inject_enum_tag(&mut arg, meta, value as u32);
        opaque_modifier(
            resolve::text_selection_affinity_fn(),
            resolve::text_selection_affinity_oret(),
            view,
            &[arg.as_mut_ptr() as u64],
            [0.0; 4],
        )
    }
}


/// Apply `.writingToolsAffordanceVisibility(_:)`.
pub fn writing_tools_affordance_visibility(view: &ViewHandle, value: Visibility) -> ViewHandle {
    unsafe {
        let meta = abi::metadata0(resolve::visibility_ma());
        let arg = abi::enum_argument(meta, value as u32);
        opaque_modifier(
            resolve::writing_tools_affordance_visibility_fn(),
            resolve::writing_tools_affordance_visibility_oret(),
            view,
            &[arg],
            [0.0; 4],
        )
    }
}