use super::View;
use crate::views;
use crate::Color;
pub trait ShapeStyleArg {
fn foreground_style(self, view: View) -> View;
}
impl ShapeStyleArg for Color {
fn foreground_style(self, view: View) -> View {
view.foreground_style_color(self.r, self.g, self.b, self.a)
}
}
impl ShapeStyleArg for views::SystemColor {
fn foreground_style(self, view: View) -> View {
view.foreground_style_value(views::system_color_value(self))
}
}
impl ShapeStyleArg for views::Hierarchical {
fn foreground_style(self, view: View) -> View {
view.foreground_style_hierarchical(self)
}
}
impl View {
pub fn foreground_style<S: ShapeStyleArg>(self, style: S) -> Self {
style.foreground_style(self)
}
}
pub trait BackgroundStyleArg {
fn background_style(self, view: View) -> View;
}
impl BackgroundStyleArg for Color {
fn background_style(self, view: View) -> View {
View::new(views::background_style_color(
&view.handle,
self.r,
self.g,
self.b,
self.a,
))
}
}
impl BackgroundStyleArg for views::SystemColor {
fn background_style(self, view: View) -> View {
View::new(views::background_style_value(
&view.handle,
views::system_color_value(self),
))
}
}
impl BackgroundStyleArg for views::Hierarchical {
fn background_style(self, view: View) -> View {
View::new(views::background_style_hierarchical(&view.handle, self))
}
}
impl View {
pub fn background_style<S: BackgroundStyleArg>(self, style: S) -> Self {
style.background_style(self)
}
}
pub trait EnvironmentArg {
fn environment(self, view: View) -> View;
}
impl EnvironmentArg for views::SymbolVariants {
fn environment(self, view: View) -> View {
View::new(views::environment_symbol_variants(&view.handle, self))
}
}
impl EnvironmentArg for views::ImageScale {
fn environment(self, view: View) -> View {
View::new(views::environment_image_scale(&view.handle, self))
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum NavigationViewStyle {
Automatic,
Columns,
DoubleColumn,
#[cfg(not(target_os = "macos"))]
Stack,
}
#[derive(Clone, Copy, Debug)]
pub enum Accessibility<'a> {
Label(&'a str),
Hint(&'a str),
Value(&'a str),
Hidden(bool),
Identifier(&'a str),
SortPriority(f64),
ActivationPoint(f64, f64),
InputLabels(&'a [&'a str]),
SelectionIdentifier(views::HashableValue<'a>),
}
#[derive(Clone, Copy, Debug)]
pub enum Draggable<'a> {
Text(&'a str),
ContainerItem(views::HashableValue<'a>),
}
pub enum ScrollTarget<'a> {
Edge(views::Edge),
Id {
state: &'a views::BoundState<String>,
anchor: Option<(f64, f64)>,
},
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum AppStorageStore<'a> {
Standard,
Suite(&'a str),
}
impl View {
pub fn environment<V: EnvironmentArg>(self, value: V) -> Self {
value.environment(self)
}
pub fn navigation_view_style(self, style: NavigationViewStyle) -> Self {
Self::new(match style {
NavigationViewStyle::Automatic => {
views::navigation_view_style_automatic(&self.handle)
}
NavigationViewStyle::Columns => views::navigation_view_style_columns(&self.handle),
NavigationViewStyle::DoubleColumn => {
views::navigation_view_style_double_column(&self.handle)
}
#[cfg(not(target_os = "macos"))]
NavigationViewStyle::Stack => views::navigation_view_style_stack(&self.handle),
})
}
pub unsafe fn modifier(
self,
modifier_meta: *const core::ffi::c_void,
bytes: &[u8],
) -> Self {
Self::new(unsafe { views::apply_modifier(&self.handle, modifier_meta, bytes) })
}
pub fn accessibility(self, what: Accessibility<'_>) -> Self {
Self::new(match what {
Accessibility::Label(s) => views::accessibility_label(&self.handle, s),
Accessibility::Hint(s) => views::accessibility_hint(&self.handle, s),
Accessibility::Value(s) => views::accessibility_value(&self.handle, s, true),
Accessibility::Hidden(b) => views::accessibility_hidden(&self.handle, b),
Accessibility::Identifier(s) => views::accessibility_identifier(&self.handle, s),
Accessibility::SortPriority(p) => {
views::accessibility_sort_priority(&self.handle, p)
}
Accessibility::ActivationPoint(x, y) => {
views::accessibility_activation_point(&self.handle, x, y)
}
Accessibility::InputLabels(l) => {
views::accessibility_input_labels(&self.handle, l, true)
}
Accessibility::SelectionIdentifier(id) => {
views::accessibility_selection_identifier(&self.handle, id)
}
})
}
pub fn draggable(self, payload: Draggable<'_>) -> Self {
Self::new(match payload {
Draggable::Text(t) => views::draggable_text(&self.handle, t),
Draggable::ContainerItem(id) => views::draggable_container_item(&self.handle, id),
})
}
pub fn scroll_position(self, target: ScrollTarget<'_>) -> Self {
Self::new(match target {
ScrollTarget::Edge(e) => views::scroll_position_edge(&self.handle, e),
ScrollTarget::Id { state, anchor } => {
views::scroll_position_id(&self.handle, state, anchor)
}
})
}
pub fn transform_environment<K: crate::state::EnvKey>(
self,
action: impl Fn(K::Value) -> K::Value + 'static,
) -> Self {
Self::new(views::transform_environment::<K>(&self.handle, action))
}
pub fn default_app_storage(self, store: AppStorageStore<'_>) -> Self {
Self::new(match store {
AppStorageStore::Standard => views::default_app_storage_standard(&self.handle),
AppStorageStore::Suite(name) => {
views::default_app_storage_suite(&self.handle, name)
.unwrap_or_else(|| panic!("UserDefaults refused the suite {name:?}"))
}
})
}
}
impl View {
pub fn rotation_effect(self, degrees: f64) -> Self {
self.rotation(degrees, 0.5, 0.5)
}
pub fn rotation_3d_effect(self, degrees: f64, ax: f64, ay: f64, az: f64) -> Self {
self.rotation_3d(degrees, ax, ay, az)
}
pub fn scale_effect(self, sx: f64, sy: f64) -> Self {
self.scale(sx, sy)
}
pub fn multiline_text_alignment(self, align: crate::views::TextAlign) -> Self {
self.multiline_alignment(align)
}
}
pub trait ClipShapeArg {
fn clip_shape(self, view: View) -> View;
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum ClipShape {
Circle,
Capsule,
Ellipse,
RoundedRect(f64),
CircularRoundedRect(f64),
}
impl ClipShapeArg for ClipShape {
fn clip_shape(self, view: View) -> View {
match self {
Self::Circle => view.clip_circle(),
Self::Capsule => view.clip_capsule(),
Self::Ellipse => view.clip_ellipse(),
Self::RoundedRect(r) => view.clip_rounded_rect(r),
Self::CircularRoundedRect(r) => view.clip_rounded_rect_styled(r, false),
}
}
}
impl View {
pub fn clip_shape<S: ClipShapeArg>(self, shape: S) -> Self {
shape.clip_shape(self)
}
}