Skip to content

Commit a5bd06b

Browse files
committed
more methods
1 parent acc28ca commit a5bd06b

2 files changed

Lines changed: 156 additions & 0 deletions

File tree

src/geometry.rs

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,10 @@ impl Region {
348348
}
349349
}
350350

351+
#[classmethod]
352+
#[pyo3(signature=(window_region, region, *, top = false))]
351353
pub fn get_scroll_to_visible(
354+
_cls: &Bound<'_, PyType>,
352355
window_region: &Region,
353356
region: &Region,
354357
top: bool,
@@ -752,4 +755,156 @@ impl Region {
752755
height: y2.max(oy2) - y,
753756
}
754757
}
758+
759+
fn split(&self, mut cut_x: i32, mut cut_y: i32) -> (Region, Region, Region, Region) {
760+
let Region {
761+
x,
762+
y,
763+
width,
764+
height,
765+
} = *self;
766+
767+
if cut_x < 0 {
768+
cut_x = width + cut_x;
769+
}
770+
if cut_y < 0 {
771+
cut_y = height + cut_y;
772+
}
773+
(
774+
Region {
775+
x: x,
776+
y: y,
777+
width: cut_x,
778+
height: cut_y,
779+
},
780+
Region {
781+
x: x + cut_x,
782+
y: y,
783+
width: width - cut_x,
784+
height: cut_y,
785+
},
786+
Region {
787+
x: x,
788+
y: y + cut_y,
789+
width: cut_x,
790+
height: height - cut_y,
791+
},
792+
Region {
793+
x: x + cut_x,
794+
y: y + cut_y,
795+
width: width - cut_x,
796+
height: height - cut_y,
797+
},
798+
)
799+
}
800+
801+
fn split_horizontal(&self, mut cut: i32) -> (Region, Region) {
802+
let Region {
803+
x,
804+
y,
805+
width,
806+
height,
807+
} = *self;
808+
if cut < 0 {
809+
cut = height + cut;
810+
}
811+
(
812+
Region {
813+
x: x,
814+
y: y,
815+
width: width,
816+
height: cut,
817+
},
818+
Region {
819+
x: x,
820+
y: y + cut,
821+
width: width,
822+
height: height - cut,
823+
},
824+
)
825+
}
826+
827+
#[pyo3(signature=(container, x_axis=true, y_axis=true))]
828+
fn translate_inside(&self, container: &Region, x_axis: bool, y_axis: bool) -> Region {
829+
let Region {
830+
x: x1,
831+
y: y1,
832+
width: width1,
833+
height: height1,
834+
} = *container;
835+
let Region {
836+
x: x2,
837+
y: y2,
838+
width: width2,
839+
height: height2,
840+
} = *self;
841+
Region {
842+
x: if x_axis {
843+
x2.min(x1 + width1 - width2).max(x1)
844+
} else {
845+
x2
846+
},
847+
y: if y_axis {
848+
y2.min(y1 + height1 - height2).max(y1)
849+
} else {
850+
y2
851+
},
852+
width: width2,
853+
height: height2,
854+
}
855+
}
856+
857+
#[pyo3(signature = (x_axis=1, y_axis=1, margin=None))]
858+
fn inflect(&self, x_axis: i32, y_axis: i32, margin: Option<Spacing>) -> Region {
859+
let inflect_margin = margin.unwrap_or(Spacing {
860+
top: 0,
861+
right: 0,
862+
bottom: 0,
863+
left: 0,
864+
});
865+
let Region {
866+
mut x,
867+
mut y,
868+
width,
869+
height,
870+
} = *self;
871+
if x_axis != 0 {
872+
x = x + (width + inflect_margin.max_width()) * x_axis;
873+
}
874+
if y_axis != 0 {
875+
y = y + (height + inflect_margin.max_height()) * y_axis;
876+
}
877+
Region {
878+
x,
879+
y,
880+
width,
881+
height,
882+
}
883+
}
884+
}
885+
886+
#[pyclass(frozen)]
887+
#[derive(Debug, Clone)]
888+
pub struct Spacing {
889+
#[pyo3(get)]
890+
pub top: i32,
891+
#[pyo3(get)]
892+
pub right: i32,
893+
#[pyo3(get)]
894+
pub bottom: i32,
895+
#[pyo3(get)]
896+
pub left: i32,
897+
}
898+
899+
#[pymethods]
900+
impl Spacing {
901+
#[getter]
902+
fn max_width(&self) -> i32 {
903+
self.left.max(self.right)
904+
}
905+
906+
#[getter]
907+
fn max_height(&self) -> i32 {
908+
self.top.max(self.bottom)
909+
}
755910
}

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@ fn textual_speedups(m: &Bound<'_, PyModule>) -> PyResult<()> {
1414
m.add_class::<geometry::GeometryOffset>()?;
1515
m.add_class::<geometry::Size>()?;
1616
m.add_class::<geometry::Region>()?;
17+
m.add_class::<geometry::Spacing>()?;
1718
Ok(())
1819
}

0 commit comments

Comments
 (0)