Skip to content

Commit

Permalink
Add trait to get layout anchors for a view, implement it on common co…
Browse files Browse the repository at this point in the history
…mponents and add some functions that make use of it to simplify creating layout constraints
  • Loading branch information
Isaac-Leonard committed Sep 15, 2023
1 parent 0f64a84 commit 876ff3f
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 0 deletions.
116 changes: 116 additions & 0 deletions src/layout/has_layout_impl.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
use super::traits::HasLayout;
use crate::{
button::Button,

Check failure on line 3 in src/layout/has_layout_impl.rs

View workflow job for this annotation

GitHub Actions / Check that examples build

unresolved imports `crate::button`, `crate::listview`
input::TextField,
layout::{LayoutAnchorX, LayoutAnchorY, LayoutConstraint, SafeAreaLayoutGuide},
listview::ListView,
text::Label,
view::View,
};

/// Takes a list of views, a parent view that contains them and returns layout constraints that will position them from top to bottom separated by the specified padding.
/// The padding is also applied to the sides of each view.
pub fn top_to_bottom(views: Vec<&dyn HasLayout>, parent: &impl HasLayout, padding: f32) -> Vec<LayoutConstraint> {
let (top, bottom) = if let (Some(first), Some(last)) = (views.first(), views.last()) {
(
first.get_top().constraint_equal_to(parent.get_top()).offset(padding),
last.get_bottom().constraint_equal_to(parent.get_bottom()).offset(padding),
)
} else {
// No views were passed
return Vec::new();
};
let adjoining_constraints = views
.windows(2)
.map(|views| views[0].get_bottom().constraint_equal_to(views[1].get_top()));
let side_constraints = views
.iter()
.map(|view| [view.get_leading().constraint_equal_to(parent.get_leading()).offset(padding)])
.flatten();
vec![top, bottom]
.into_iter()
.chain(adjoining_constraints)
.chain(side_constraints)
.collect()
}

/// Returns a list of layout constraints that makes the given view fill the given safe area
pub fn fill_safe_area(view: &impl HasLayout, safe_area: &SafeAreaLayoutGuide) -> Vec<LayoutConstraint> {
vec![
view.get_top().constraint_equal_to(&safe_area.top),
view.get_bottom().constraint_equal_to(&safe_area.bottom),
view.get_leading().constraint_equal_to(&safe_area.leading),
view.get_trailing().constraint_equal_to(&safe_area.trailing),
]
}

impl HasLayout for Label {
fn get_top(&self) -> &LayoutAnchorY {
&self.top
}
fn get_bottom(&self) -> &LayoutAnchorY {
&self.bottom
}
fn get_leading(&self) -> &LayoutAnchorX {
&self.leading
}
fn get_trailing(&self) -> &LayoutAnchorX {
&self.trailing
}
}
impl HasLayout for TextField {
fn get_top(&self) -> &LayoutAnchorY {
&self.top
}
fn get_bottom(&self) -> &LayoutAnchorY {
&self.bottom
}
fn get_leading(&self) -> &LayoutAnchorX {
&self.leading
}
fn get_trailing(&self) -> &LayoutAnchorX {
&self.trailing
}
}
impl HasLayout for Button {
fn get_top(&self) -> &LayoutAnchorY {
&self.top
}
fn get_bottom(&self) -> &LayoutAnchorY {
&self.bottom
}
fn get_leading(&self) -> &LayoutAnchorX {
&self.leading
}
fn get_trailing(&self) -> &LayoutAnchorX {
&self.trailing
}
}
impl<T> HasLayout for View<T> {
fn get_top(&self) -> &LayoutAnchorY {
&self.top
}
fn get_bottom(&self) -> &LayoutAnchorY {
&self.bottom
}
fn get_leading(&self) -> &LayoutAnchorX {
&self.leading
}
fn get_trailing(&self) -> &LayoutAnchorX {
&self.trailing
}
}
impl<T> HasLayout for ListView<T> {
fn get_top(&self) -> &LayoutAnchorY {
&self.top
}
fn get_bottom(&self) -> &LayoutAnchorY {
&self.bottom
}
fn get_leading(&self) -> &LayoutAnchorX {
&self.leading
}
fn get_trailing(&self) -> &LayoutAnchorX {
&self.trailing
}
}
2 changes: 2 additions & 0 deletions src/layout/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,5 @@ mod safe_guide;

#[cfg(feature = "autolayout")]
pub use safe_guide::SafeAreaLayoutGuide;
#[cfg(feature = "autolayout")]
mod has_layout_impl;
10 changes: 10 additions & 0 deletions src/layout/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ use crate::objc_access::ObjcAccess;
#[cfg(feature = "appkit")]
use crate::pasteboard::PasteboardType;

use super::{LayoutAnchorX, LayoutAnchorY};

/// A trait that view wrappers must conform to. Enables managing the subview tree.
#[allow(unused_variables)]
pub trait Layout: ObjcAccess {
Expand Down Expand Up @@ -173,3 +175,11 @@ pub trait Layout: ObjcAccess {
});
}
}

/// A trait to access a views layout anchors
pub trait HasLayout {
fn get_top(&self) -> &LayoutAnchorY;
fn get_bottom(&self) -> &LayoutAnchorY;
fn get_leading(&self) -> &LayoutAnchorX;
fn get_trailing(&self) -> &LayoutAnchorX;
}

0 comments on commit 876ff3f

Please sign in to comment.