-
-
Notifications
You must be signed in to change notification settings - Fork 377
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add Constraint helpers (e.g. from_lengths) #641
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #641 +/- ##
======================================
Coverage 90.9% 90.9%
======================================
Files 42 42
Lines 12677 12795 +118
======================================
+ Hits 11525 11643 +118
Misses 1152 1152 ☔ View full report in Codecov by Sentry. |
Needs to be rebased before merging |
Except for the point I mentioned above, I have successfully rebased this branch. I may not solve every conflict as expected, so I prefer not to share my version here. @joshka but I believe most of them can transfer. |
I'm going to split this into a second PR - the first just a pure refactor (because the underlying changes in layout.rs make it difficult to rebase it and ensure that no code is lost). --- layout-main-sorted.txt 2023-12-09 12:18:47
+++ layout-sorted.txt 2023-12-09 12:20:09
@@ -1,10 +1,10 @@
fn alignment_from_str() {
fn alignment_to_string() {
-fn as_ref(&self) -> &Constraint {
fn constraint_to_string() {
fn corner_from_str() {
fn corner_to_string() {
fn custom_cache_size() {
+fn default() -> Layout {
fn default() -> Self {
fn default_cache_size() {
fn direction_from_str() {
@@ -15,7 +15,6 @@
fn from((width, height): (u16, u16)) -> Self {
fn get_x_width_with_segment_size(
fn layout_constraints() {
-fn layout_default() {
fn layout_direction() {
fn layout_margins() {
fn layout_new() {
@@ -40,31 +39,29 @@
fn test_split_equally_in_underspecified_case() {
fn try_split(area: Rect, layout: &Layout) -> Result<Rc<[Rect]>, AddConstraintError> {
fn vertical_split_by_height() {
-impl AsRef<Constraint> for Constraint {
impl Constraint {
impl Default for Constraint {
+impl Default for Layout {
+impl Display for Constraint {
+impl Display for Margin {
impl Element {
impl From<(u16, u16)> for Size {
impl Layout {
impl Margin {
-impl fmt::Display for Constraint {
-impl fmt::Display for Margin {
pub const DEFAULT_CACHE_SIZE: usize = 16;
pub const fn direction(mut self, direction: Direction) -> Layout {
pub const fn horizontal_margin(mut self, horizontal: u16) -> Layout {
pub const fn margin(mut self, margin: u16) -> Layout {
pub const fn new(horizontal: u16, vertical: u16) -> Margin {
-pub const fn segment_size(mut self, segment_size: SegmentSize) -> Layout {
pub const fn vertical_margin(mut self, vertical: u16) -> Layout {
pub enum Alignment {
pub enum Constraint {
pub enum Corner {
pub enum Direction {
-pub enum SegmentSize {
pub fn apply(&self, length: u16) -> u16 {
-pub fn constraints<I>(mut self, constraints: I) -> Layout
+pub fn constraints<C: AsRef<[Constraint]>>(mut self, constraints: C) -> Layout {
pub fn init_cache(cache_size: usize) -> bool {
-pub fn new<I>(direction: Direction, constraints: I) -> Layout
+pub fn new<C: AsRef<[Constraint]>>(direction: Direction, constraints: C) -> Layout {
pub fn split(&self, area: Rect) -> Rc<[Rect]> {
pub height: u16,
pub horizontal: u16,
@@ -74,4 +71,6 @@
pub use rect::*;
pub vertical: u16,
pub width: u16,
+pub(crate) const fn segment_size(mut self, segment_size: SegmentSize) -> Layout {
+pub(crate) enum SegmentSize {
struct Element { |
Adds helper methods that convert from iterators of u16 values to the specific Constraint type. This makes it easy to create constraints like: ```rust // a fixed layout let constraints = Constraint::from_lengths([10, 20, 10]); // a centered layout let constraints = Constraint::from_ratios([(1, 4), (1, 2), (1, 4)]); let constraints = Constraint::from_percentages([25, 50, 25]); // a centered layout with a minimum size let constraints = Constraint::from_mins([0, 100, 0]); // a sidebar / main layout with maximum sizes let constraints = Constraint::from_maxes([30, 200]); ```
a3ffa96
to
c523f64
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Other than the typo looks good
Adds helper methods that convert from iterators of u16 values to the
specific Constraint type. This makes it easy to create constraints like: