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

Replace boolean parameter to Layout::align with AlignmentOptions struct #278

Merged
merged 1 commit into from
Feb 18, 2025

Conversation

nicoburns
Copy link
Contributor

A small but nice improvement to the public API.

@nicoburns nicoburns added the enhancement New feature or request label Feb 17, 2025
@nicoburns nicoburns removed the enhancement New feature or request label Feb 17, 2025
@nicoburns nicoburns added this to the 0.3 Release milestone Feb 17, 2025
@nicoburns nicoburns force-pushed the alignment-options-struct branch from 2119050 to e4ecf67 Compare February 17, 2025 22:00
Copy link
Member

@tomcur tomcur left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

Comment on lines +13 to +16
/// If set to `true`, "end" and "center" alignment will apply even if the line contents are
/// wider than the alignment width. If it is set to `false`, all overflowing lines will be
/// [`Alignment::Start`] aligned.
pub align_when_overflowing: bool,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A nit: it may read more naturally by flipping the meaning of the bool and calling the field start_alignment_on_overflow.

Copy link
Member

@DJMcNab DJMcNab left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Want to get discussion on the signature of align.

Comment on lines 187 to +189
container_width: Option<f32>,
alignment: Alignment,
align_when_overflowing: bool,
options: AlignmentOptions,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not convinced by this signature. Having a pile of arguments, and then an options struct doesn't quite seem right to me:

fn align(&mut self, alignment: Alignment, options: AlignmentOptions)

seems more natural, given that container_width is already optional.

Or even:

fn align(&mut self, options: AlignmentOptions)

Copy link
Member

@tomcur tomcur Feb 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One issue with moving container_width into the options is that it cannot account for floated boxes yet, and I don't think we know what that's going to look like yet. (Though I guess floated boxes just subtract from the space provided by container_width.)

I certainly see the logic of moving alignment into AlignmentOptions.

Copy link
Contributor Author

@nicoburns nicoburns Feb 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm... I suppose you could put container_width in the options object. I think alignment ought to stay out. It would also be possible to have separate align and align_with_options functions where the former uses the default options.

Copy link
Member

@tomcur tomcur Feb 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would also be possible to have separate align and align_with_options functions where the former uses the default options.

That also makes sense.

Just another consideration: perhaps container_width shouldn't be optional. Aligning to the layout's calculated width is not necessarily a sensible default, as that width changes depending on how exactly lines were broken given the container width (max_advance) used for line breaking.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just another consideration: perhaps container_width shouldn't be optional. Aligning to the layout's calculated width is not necessarily a sensible default, as that width changes depending on how exactly lines were broken given the container width (max_advance) used for line breaking.

This could also make sense.

impl Default for AlignmentOptions {
fn default() -> Self {
Self {
align_when_overflowing: false,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible that true is a more sensible default?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have particularly strong feelings about the default.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe the current default follows what the web does (i.e., when a line overflows, you always see the start of it, in both LTR and RTL text).

@nicoburns nicoburns force-pushed the alignment-options-struct branch from c5994de to 3458d7d Compare February 18, 2025 09:42
Signed-off-by: Nico Burns <nico@nicoburns.com>
@nicoburns nicoburns force-pushed the alignment-options-struct branch from 3458d7d to c2b2d56 Compare February 18, 2025 09:50
@nicoburns
Copy link
Contributor Author

I think I'm going to merge this as-is, as there seems to be agreement that this is an improvement over what we have. Further API changes and/or default changes can happen in followups.

@nicoburns nicoburns added this pull request to the merge queue Feb 18, 2025
Merged via the queue into linebender:main with commit 77d1bba Feb 18, 2025
21 checks passed
@nicoburns nicoburns deleted the alignment-options-struct branch February 18, 2025 10:18
tomcur added a commit that referenced this pull request Feb 18, 2025
Some prior discussion here:
#278 (comment).

> Aligning to the layout's calculated width is not necessarily a
sensible default, as that width changes depending on how exactly lines
were broken given the container width (max_advance) used for line
breaking.
tomcur added a commit that referenced this pull request Feb 18, 2025
Some prior discussion here:
#278 (comment).

> Aligning to the layout's calculated width is not necessarily a
sensible default, as that width changes depending on how exactly lines
were broken given the container width (max_advance) used for line
breaking.
@waywardmonkeys
Copy link
Contributor

I don't know why there was a rush to land this rather than just get it right.

I won't include this in my update to masonry since apparently it has to change again (although now that this landed, who will drive actually fixing it?).

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.

Replace boolean option in Layout::align with AlignmentOptions Struct
5 participants