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: extend gbuilder by aliases and constructor #92

Merged
merged 3 commits into from
Dec 25, 2024
Merged

Conversation

JarKz
Copy link
Contributor

@JarKz JarKz commented Dec 24, 2024

Motivation

The syntax Spacing(top = 10, bottom = 10, left = 5, right = 5) is annoying to declare because or repeating and it's also applicable for Spacing(top = 5, bottom = 5, left = 5, right = 5). Therefore, need to something that should help write less information but achieve the same result.

Changes

  • The Value enum type is now extended by availability to clone. It's optional but urgent for assignment the same value for different fields. Of course, for single field the clone characteristic will not be used, so don't worry about it.
  • Introduced the alias of field. Some fields can have the same aliases and it means that for this alias value will assigned for these fields.
  • Introduced the constructor syntax. If the struct is homogeneous (have the same types of all fields), it can possibility to have a constructor which consumes single value and assigns to all fields.

New syntax

The old syntax remains as is. That's why the PR is extension.

Currently only Spacing and Alignment have simplifications:

  • Aliases:
    • For Spacing introduced new horizontal, vertical and all aliases. The horizontal is alias of top and bottom both, vertical - left and right both, all - top, bottom, left and right together (why not?).
    • For Alignment introduced new diagonal alias of horizontal and vertial both.
  • Constructors:
    • For Spacing there is a constructor that consumes UInt value and assigns to all directions (behaves like all alias).
    • For Alignment there is a constructor that consumer Literal value and assigns to all fields (behaves like diagonal alias).

Example of usage:

alias Center = Alignment(diagonal = center)
// or
alias Center = Alignment(center)
// It's the same value

alias OnlyVerticalSpacing = Spacing(vertical = 10) // like Spacing(top = 10, bottom = 10)
alias OnlyHorizontalSpacing = Spacing(horizontal = 20) // like Spacing(left = 20, right = 20)

alias AllDirectionalSpacing = Spacing(all = 15)
// or
alias AllDirectionalSpacing = Spacing(15)
// It's the same value

What about other types?

Currently there's not so much helpful types to configure layout for which is need to shorthand. But in future might be a type that is homogeneous or some fields are homogeneous and have same semantic, and for these types this shorthand will be applied.

@JarKz JarKz merged commit 63a1740 into main Dec 25, 2024
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants