Skip to content

Commit 70ad858

Browse files
committed
feat(forest): support the gap property
1 parent c5e5a98 commit 70ad858

File tree

7 files changed

+162
-1
lines changed

7 files changed

+162
-1
lines changed

float-pigment-forest/src/ffi.rs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,71 @@ pub unsafe extern "C" fn NodeStyleSetOrder(node: NodePtr, value: i32) {
742742
node.set_order(value);
743743
}
744744

745+
/// # Safety
746+
///
747+
#[no_mangle]
748+
pub unsafe extern "C" fn NodeStyleSetRowGap(node: NodePtr, value: f32) {
749+
let node = &*(node.ptr as *mut Node);
750+
node.set_row_gap(DefLength::Points(Len::from_f32(value)));
751+
}
752+
753+
/// # Safety
754+
///
755+
#[no_mangle]
756+
pub unsafe extern "C" fn NodeStyleSetRowGapNormal(node: NodePtr) {
757+
let node = &*(node.ptr as *mut Node);
758+
node.set_row_gap(DefLength::Undefined);
759+
}
760+
761+
/// # Safety
762+
///
763+
#[no_mangle]
764+
pub unsafe extern "C" fn NodeStyleSetRowGapPercentage(node: NodePtr, value: f32) {
765+
let node = &*(node.ptr as *mut Node);
766+
node.set_row_gap(DefLength::Percent(value));
767+
}
768+
769+
/// # Safety
770+
///
771+
#[no_mangle]
772+
pub unsafe extern "C" fn NodeStyleSetRowGapCalcHandle(node: NodePtr, calc_handle: i32) {
773+
let node = &*(node.ptr as *mut Node);
774+
node.set_row_gap(DefLength::Custom(calc_handle));
775+
}
776+
777+
/// # Safety
778+
///
779+
#[no_mangle]
780+
781+
pub unsafe extern "C" fn NodeStyleSetColumnGap(node: NodePtr, value: f32) {
782+
let node = &*(node.ptr as *mut Node);
783+
node.set_column_gap(DefLength::Points(Len::from_f32(value)));
784+
}
785+
786+
/// # Safety
787+
///
788+
#[no_mangle]
789+
pub unsafe extern "C" fn NodeStyleSetColumnGapNormal(node: NodePtr) {
790+
let node = &*(node.ptr as *mut Node);
791+
node.set_column_gap(DefLength::Undefined);
792+
}
793+
794+
/// # Safety
795+
///
796+
#[no_mangle]
797+
pub unsafe extern "C" fn NodeStyleSetColumnGapPercentage(node: NodePtr, value: f32) {
798+
let node = &*(node.ptr as *mut Node);
799+
node.set_column_gap(DefLength::Percent(value));
800+
}
801+
802+
/// # Safety
803+
///
804+
#[no_mangle]
805+
pub unsafe extern "C" fn NodeStyleSetColumnGapCalcHandle(node: NodePtr, calc_handle: i32) {
806+
let node = &*(node.ptr as *mut Node);
807+
node.set_column_gap(DefLength::Custom(calc_handle));
808+
}
809+
745810
/// # Safety
746811
///
747812
#[no_mangle]

float-pigment-forest/src/layout/layout_impl.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,4 +685,14 @@ impl LayoutStyle<Len> for Node {
685685
fn text_align(&self) -> TextAlign {
686686
self.style_manager().text_align()
687687
}
688+
689+
#[inline]
690+
fn row_gap(&self) -> Length {
691+
self.style_manager().row_gap()
692+
}
693+
694+
#[inline]
695+
fn column_gap(&self) -> Length {
696+
self.style_manager().column_gap()
697+
}
688698
}

float-pigment-forest/src/node.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,8 @@ pub trait StyleSetter {
618618
unsafe fn set_aspect_ratio(&self, value: Option<f32>);
619619
unsafe fn set_order(&self, value: i32);
620620
unsafe fn set_text_align(&self, value: TextAlign);
621+
unsafe fn set_row_gap(&self, value: Length);
622+
unsafe fn set_column_gap(&self, value: Length);
621623
}
622624

623625
impl StyleSetter for Node {
@@ -857,6 +859,16 @@ impl StyleSetter for Node {
857859
self.mark_dirty_propagate();
858860
}
859861
}
862+
unsafe fn set_row_gap(&self, value: Length) {
863+
if self.style_manager_mut().set_row_gap(value) {
864+
self.mark_dirty_propagate();
865+
}
866+
}
867+
unsafe fn set_column_gap(&self, value: Length) {
868+
if self.style_manager_mut().set_column_gap(value) {
869+
self.mark_dirty_propagate();
870+
}
871+
}
860872
}
861873

862874
#[cfg(test)]

float-pigment-forest/src/style.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ struct OtherStyle {
7676
pub overflow_y: Overflow,
7777
pub text_align: TextAlign,
7878
pub aspect_ratio: Option<f32>,
79+
pub row_gap: Length,
80+
pub column_gap: Length,
7981
}
8082

8183
impl Default for OtherStyle {
@@ -87,6 +89,8 @@ impl Default for OtherStyle {
8789
overflow_y: Overflow::Visible,
8890
text_align: TextAlign::Start,
8991
aspect_ratio: None,
92+
row_gap: Length::Undefined,
93+
column_gap: Length::Undefined,
9094
}
9195
}
9296
}
@@ -945,4 +949,30 @@ impl StyleManager {
945949
self.flex_style().flex_basis = value;
946950
true
947951
}
952+
953+
pub(crate) fn row_gap(&self) -> Length {
954+
self.other_style().row_gap.clone()
955+
}
956+
957+
pub(crate) fn set_row_gap(&self, value: Length) -> bool {
958+
if self.other_style().row_gap == value {
959+
return false;
960+
}
961+
self.clone_style(StyleBit::Other);
962+
self.other_style().row_gap = value;
963+
true
964+
}
965+
966+
pub(crate) fn column_gap(&self) -> Length {
967+
self.other_style().column_gap.clone()
968+
}
969+
970+
pub(crate) fn set_column_gap(&self, value: Length) -> bool {
971+
if self.other_style().column_gap == value {
972+
return false;
973+
}
974+
self.clone_style(StyleBit::Other);
975+
self.other_style().column_gap = value;
976+
true
977+
}
948978
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
use crate::*;
2+
3+
#[test]
4+
fn flex_gap() {
5+
assert_xml!(
6+
r#"
7+
<div style="display: flex; width: 100px; gap: 10px;">
8+
<div style="height: 10px; flex: 1" expect_width="45"></div>
9+
<div style="height: 10px; flex: 1" expect_width="45" expect_left="55"></div>
10+
</div>
11+
"#
12+
)
13+
}

float-pigment-forest/tests/custom/css_flexbox/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ mod order;
99
mod align_content;
1010

1111
mod flex_item_margin;
12+
mod gap;

float-pigment-forest/tests/mod.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ pub use float_pigment_css::length_num::LengthNum;
77
use float_pigment_css::{
88
parser::parse_inline_style,
99
property::NodeProperties,
10-
typing::{AspectRatio, Display},
10+
typing::{AspectRatio, Display, Gap},
1111
};
1212
pub use float_pigment_forest::Len;
1313
use float_pigment_forest::{layout::LayoutPosition, node::Length, *};
@@ -389,6 +389,36 @@ impl TestCtx {
389389
},
390390
"order" => node.set_order(node_props.order().to_i32()),
391391
"text-align" => node.set_text_align(node_props.text_align()),
392+
"gap" => {
393+
node.set_row_gap({
394+
match node_props.row_gap() {
395+
Gap::Length(l) => def_length(l),
396+
Gap::Normal => Length::Undefined,
397+
}
398+
});
399+
node.set_column_gap({
400+
match node_props.column_gap() {
401+
Gap::Length(l) => def_length(l),
402+
Gap::Normal => Length::Undefined,
403+
}
404+
});
405+
}
406+
"column-gap" => {
407+
node.set_column_gap({
408+
match node_props.column_gap() {
409+
Gap::Length(l) => def_length(l),
410+
Gap::Normal => Length::Undefined,
411+
}
412+
});
413+
}
414+
"row-gap" => {
415+
node.set_row_gap({
416+
match node_props.row_gap() {
417+
Gap::Length(l) => def_length(l),
418+
Gap::Normal => Length::Undefined,
419+
}
420+
});
421+
}
392422
_ => {}
393423
}
394424
});

0 commit comments

Comments
 (0)