Skip to content
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

Merged
merged 2 commits into from
Dec 11, 2023
Merged

Conversation

joshka
Copy link
Member

@joshka joshka commented Nov 22, 2023

Adds helper methods that convert from iterators of u16 values to the
specific Constraint type. This makes it easy to create constraints like:

// 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]);

Copy link

codecov bot commented Nov 22, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (0576a8a) 90.9% compared to head (d3e28fc) 90.9%.
Report is 1 commits behind head on main.

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.
📢 Have feedback on the report? Share it here.

@joshka
Copy link
Member Author

joshka commented Dec 4, 2023

Needs to be rebased before merging

@Valentin271 Valentin271 added this to the v0.25.0 milestone Dec 6, 2023
@TieWay59
Copy link
Contributor

TieWay59 commented Dec 9, 2023

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.

@joshka
Copy link
Member Author

joshka commented Dec 9, 2023

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]);
```
Copy link
Member

@Valentin271 Valentin271 left a 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

src/layout.rs Outdated Show resolved Hide resolved
@joshka joshka merged commit d19b266 into main Dec 11, 2023
33 checks passed
@joshka joshka deleted the constraint-helpers branch December 11, 2023 02:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants