Skip to content

Commit 3250063

Browse files
committed
Add grid placement APIs
1 parent d1f03d0 commit 3250063

File tree

4 files changed

+111
-6
lines changed

4 files changed

+111
-6
lines changed

src/ffi/error.rs

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
//! Return types for C FFI
22
3-
use super::{StyleValue, StyleValueUnit};
3+
use super::{StyleValue, StyleValueUnit, GridPlacement};
4+
5+
pub (crate) trait TaffyFFIResult {
6+
type Value;
7+
fn from_value(value: Self::Value) -> Self;
8+
fn from_return_code(return_code: ReturnCode) -> Self;
9+
}
410

511
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
612
#[repr(u8)]
@@ -35,14 +41,45 @@ pub enum ReturnCode {
3541
UnexpectedNegative,
3642
}
3743

44+
impl TaffyFFIResult for ReturnCode {
45+
type Value = ReturnCode;
46+
fn from_value(value: Self::Value) -> Self {
47+
value
48+
}
49+
fn from_return_code(return_code: ReturnCode) -> Self {
50+
return_code
51+
}
52+
}
53+
3854
#[repr(C)]
3955
pub struct StyleValueResult {
4056
pub return_code: ReturnCode,
4157
pub value: StyleValue,
4258
}
4359

44-
impl From<ReturnCode> for StyleValueResult {
45-
fn from(return_code: ReturnCode) -> Self {
60+
impl TaffyFFIResult for StyleValueResult {
61+
type Value = StyleValue;
62+
fn from_value(value: Self::Value) -> Self {
63+
Self { return_code: ReturnCode::Ok, value }
64+
}
65+
fn from_return_code(return_code: ReturnCode) -> Self {
4666
Self { return_code, value: StyleValue { unit: StyleValueUnit::None, value: 0.0 } }
4767
}
4868
}
69+
70+
71+
#[repr(C)]
72+
pub struct GridPlacementResult {
73+
pub return_code: ReturnCode,
74+
pub value: GridPlacement,
75+
}
76+
77+
impl TaffyFFIResult for GridPlacementResult {
78+
type Value = GridPlacement;
79+
fn from_value(value: Self::Value) -> Self {
80+
Self { return_code: ReturnCode::Ok, value }
81+
}
82+
fn from_return_code(return_code: ReturnCode) -> Self {
83+
Self { return_code, value: GridPlacement { start: 0, end: 0, span: 0 } }
84+
}
85+
}

src/ffi/style.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::geometry::Rect;
44
use crate::prelude as core;
55
use std::ffi::c_void;
66

7-
use super::{ReturnCode, StyleValue, StyleValueResult, StyleValueUnit};
7+
use super::{GridPlacement, ReturnCode, StyleValue, StyleValueResult, StyleValueUnit, GridPlacementResult, TaffyFFIResult};
88

99
/// A wrapper around [`core::Style`] which allows the individual style properties to be accessed
1010
/// via FFI-friendly getter and setter functions
@@ -17,7 +17,7 @@ pub struct Style {
1717
macro_rules! assert_style_pointer_is_non_null {
1818
($raw_style_ptr:expr) => {{
1919
if ($raw_style_ptr as *const c_void) == std::ptr::null() {
20-
return ReturnCode::NullStylePointer.into();
20+
return TaffyFFIResult::from_return_code(ReturnCode::NullStylePointer);
2121
}
2222
}};
2323
}
@@ -34,7 +34,8 @@ macro_rules! get_style {
3434
let return_value = $block;
3535

3636
Box::leak(style_box);
37-
StyleValueResult { return_code: ReturnCode::Ok, value: return_value.into() }
37+
38+
TaffyFFIResult::from_value(return_value.into())
3839
}};
3940
}
4041

@@ -142,3 +143,15 @@ pub unsafe extern "C" fn Taffy_set_padding_trbl(
142143
};
143144
})
144145
}
146+
147+
/* Grid APIs */
148+
149+
/// Get grid item's column placement
150+
pub fn style_get_grid_column(raw_style: *mut c_void) -> GridPlacementResult {
151+
get_style!(raw_style, style, style.grid_column)
152+
}
153+
154+
/// Set grid item's column placement
155+
pub fn style_set_grid_column(raw_style: *mut c_void, placement: GridPlacement) -> ReturnCode {
156+
with_style_mut!(raw_style, style, style.grid_column = placement.into())
157+
}

src/ffi/value.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,25 @@ impl TryFrom<StyleValue> for core::LengthPercentageAuto {
9696
}
9797
}
9898
}
99+
100+
/// For all fields, zero represents not set
101+
#[derive(Debug, Clone, Copy, PartialEq)]
102+
#[repr(C)]
103+
pub struct GridPlacement {
104+
pub start: i16,
105+
pub end: i16,
106+
pub span: u16,
107+
}
108+
109+
impl From<GridPlacement> for core::Line<core::GridPlacement> {
110+
fn from(placement: GridPlacement) -> Self {
111+
Self::from_raw_parts(placement.start, placement.span, placement.end)
112+
}
113+
}
114+
115+
impl From<core::Line<core::GridPlacement>> for GridPlacement {
116+
fn from(placement: core::Line<core::GridPlacement>) -> Self {
117+
let (start, span, end) = placement.into_raw_parts();
118+
Self { start, span, end }
119+
}
120+
}

src/style/grid.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,39 @@ impl Line<GridPlacement> {
161161
end: self.end.into_origin_zero_placement(explicit_track_count),
162162
}
163163
}
164+
165+
/// Convert raw values of start, span, and end into a [`GridPlacement`].
166+
/// Zero is not a valid value for any of the values and is thus used to indicate unset
167+
/// Only 2 of the 3 values should be set. If all 3 are set then `span_value` is ignored.
168+
pub (crate) fn from_raw_parts(start: i16, span_value: u16, end: i16) -> Self {
169+
match (start, span_value, end) {
170+
(0, 0, 0) => auto(),
171+
(start, 0, 0) => Line { start: line(start), end: auto() },
172+
(0, 0, end) => Line { start: auto(), end: line(end) },
173+
(0, span_value, 0) => span(span_value),
174+
(start, span_value, 0) => Line { start: line(start), end: span(span_value) },
175+
(0, span_value, end) => Line { start: auto(), end: line(end) },
176+
(start, _, end) => Line { start: line(start), end: line(end) },
177+
}
178+
}
179+
180+
/// Convert raw values of start, span, and end into a [`GridPlacement`].
181+
/// Zero is not a valid value for any of the values and is thus used to indicate unset
182+
/// Only 2 of the 3 values should be set. If all 3 are set then `span_value` is ignored.
183+
pub (crate) fn into_raw_parts(self) -> (i16, u16, i16) {
184+
use GenericGridPlacement::*;
185+
match (self.start, self.end) {
186+
(Line(start), Line(end)) => (start.as_i16(), 0, end.as_i16()),
187+
(Line(start), Span(span)) => (start.as_i16(), span, 0),
188+
(Line(start), Auto) => (start.as_i16(), 1, 0),
189+
(Span(span), Line(end)) => (0, span, end.as_i16()),
190+
(Span(span), Span(_)) => (0, span, 0),
191+
(Span(span), Auto) => (0, span, 0),
192+
(Auto, Line(end)) => (0, 1, end.as_i16()),
193+
(Auto, Span(span)) => (0, span, 0),
194+
(Auto, Auto) => (0, 1, 0),
195+
}
196+
}
164197
}
165198

166199
impl Line<OriginZeroGridPlacement> {

0 commit comments

Comments
 (0)