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

xilem_masonry: Add Label widget #226

Merged
merged 3 commits into from
Apr 27, 2024

Conversation

Philipp-M
Copy link
Contributor

Adds the label widget to xilem_masonry with some (but not all) attribute setters, and methods to disable the label in masonry.
Also slightly extends the Flex view, by being able to specify the direction (and a small fix in FlexSplice).

@@ -8,6 +8,7 @@ use masonry::{
BoxConstraints, EventCtx, LayoutCtx, LifeCycle, LifeCycleCtx, PaintCtx, Point, PointerEvent,
Size, StatusChange, TextEvent, Widget, WidgetId, WidgetPod,
};
pub use masonry::{widget::Axis, Color, TextAlignment};
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'm shamelessly reexporting all these attributes to the root of the library (since I think they're mostly unambiguous), not sure what our policy here is.

Copy link
Member

Choose a reason for hiding this comment

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

This is probably #223 again. I think this addition is fine, but we need to reason out a final policy for this, rather than making ad-hoc decisions

@@ -35,7 +44,7 @@ where
let mut scratch = Vec::new();
let mut splice = VecSplice::new(&mut elements, &mut scratch);
let seq_state = self.sequence.build(cx, &mut splice);
let mut view = widget::Flex::column();
let mut view = widget::Flex::for_axis(self.axis);
Copy link
Member

Choose a reason for hiding this comment

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

I didn't expose this intentionally, because Masonry doesn't support changing the axis at runtime.

We do not want to have views which give different final trees in build and rebuild

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, haven't checked that closely yet. That's unfortunate, and I agree, all attributes that are able to set in the view should of course change the widget tree.
In that case maybe stick to something like h_flex or h_stack something a like (open for naming suggestions)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Or, well just implement it in masonry, checking that now...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So I've implemented it in masonry, and checked it via

let axis = if count % 2 == 0 {
    Axis::Horizontal
} else {
    Axis::Vertical
};

...
flex(()).direction(axis)

(but have hardcoded the values in the example)

@@ -121,7 +130,6 @@ impl ElementSplice for FlexSplice<'_> {
}

fn len(&self) -> usize {
// This is not correct because of the spacer items. Is `len` actually needed?
self.element.len() - self.ix
self.ix
Copy link
Member

Choose a reason for hiding this comment

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

Sure! I suspect that we'll probably just remove len, anyway; but this is more correct

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah probably makes sense, doesn't seem to be needed anymore (I'll probably look into this more deeply, whether that is indeed the case).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's used in old xilem::view::List, so I guess we still need this, in case we don't exactly know the size of a ViewSequence in the view logic (e.g. lazy loading of items in a list, not sure currently whether that should happen in the xilem reactive layer?)

Copy link
Member

Choose a reason for hiding this comment

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

I don't think its use in List is necessary, but I haven't remade the List ViewSequence yet, so I'm not certain

pub fn label(label: impl Into<ArcStr>) -> Label {
Label {
label: label.into(),
text_color: Color::default(),
Copy link
Member

Choose a reason for hiding this comment

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

Hmm, Color implementing Default is weird - the default would surely depend on the use case?

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 can instead make it black (which seems to be the default) to be more clear. I agree, looking at it more closely, because default for the alpha value seems to be 0 (which I don't think is a good default). Maybe remove Default for peniko::Color alltogether (or make it explicitely black with opaque alpha)?

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, removing that Default is worth a PR, at worst to get a reason it should stay

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've made it Color::BLACK for now in this PR.

Copy link
Contributor

Choose a reason for hiding this comment

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

if we kept Default, this could just use functional record update syntax,
since label is the only non-default field. Agree about the alpha value.

Label {
  label: label.into(),
  ..Default::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'm not sure I follow. Label: Default would not be a good API, because there's nearly no use for an empty label.

(Plus, I think that would allocate without need, which isn't great.)

Copy link
Contributor

Choose a reason for hiding this comment

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

Of course, I just momentarily forgot functional record update syntax requires the structure itself to be Default not just the fields.

crates/xilem_masonry/src/view/label.rs Show resolved Hide resolved
@@ -180,6 +186,13 @@ impl LabelMut<'_> {
self.widget.alignment = alignment;
self.ctx.request_layout();
}

/// Set disabled
Copy link
Member

Choose a reason for hiding this comment

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

What does it even mean for a label to be disabled?

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 guess it makes more sense in combination with a checkbox. But basically, that it's not clickable in HTML jargon at least

Copy link
Member

Choose a reason for hiding this comment

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

Right, so e.g. the contents can't be clicked and dragged? I guess it also would have consequences for the accessibility metadata

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah I guess that is indeed the case.

@Philipp-M Philipp-M requested a review from DJMcNab April 27, 2024 14:50
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.

I still think we need some way to check the flex direction change works as expected.

Are you willing to add a Masonry level test for it? I've not dug into the test suite myself, so I'm not sure what that would involve.
If not, I think we can just merge this for expedience.

We do also need to add some xilem_masonry level tests. It would be awesome if you could get that started (although feel free to work on whichever features you want, of course)

data.count = -1_000_000;
}),
))
.direction(Axis::Horizontal),
Copy link
Member

Choose a reason for hiding this comment

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

Should we have a dynamically toggling axis in the same widget here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure, it jumps around the layout, which I didn't find satisfying, but I added it for the +n buttons when data.active changes, which is less annoying.

crates/masonry/src/widget/flex.rs Outdated Show resolved Hide resolved
@Philipp-M
Copy link
Contributor Author

We do also need to add some xilem_masonry level tests. It would be awesome if you could get that started (although feel free to work on whichever features you want, of course)

I can look into that some time soon, but I think focus on "bling bling" right now seems more appropriate ?

@DJMcNab
Copy link
Member

DJMcNab commented Apr 27, 2024

but I think focus on "bling bling" right now seems more appropriate ?

Probably 😆. It would be nice to have at least some tests we can point to, but I see your point

As I say, merging-as-is is fine, although I've not run this myself

@Philipp-M
Copy link
Contributor Author

Are you willing to add a Masonry level test for it?

I think I would do that, but for me locally a lot of the tests are failing, because I suspect a different font is chosen for me (new test snapshots are generated, with slightly different looking font), not sure currently how to force one specific font (I haven't looked into it, but I think it makes sense to include a font in the repo, and force it in the tests?)
Tagging @PoignardAzur

Anyway, it should be easy to add a test for the direction, but I'd rather merge this now and do that in different PR.

@Philipp-M Philipp-M added this pull request to the merge queue Apr 27, 2024
@PoignardAzur
Copy link
Contributor

Yeah, we can probably postpone tests until we figure out how to get a reliable font stack.

Overall LGTM.

Merged via the queue into linebender:main with commit 235fbb7 Apr 27, 2024
7 checks passed
@Philipp-M Philipp-M deleted the xilem_masonry-label branch April 27, 2024 16:04
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.

4 participants