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

Add the possibility to change the orientation of Menu #9

Closed
wants to merge 6 commits into from

Conversation

micielski
Copy link
Contributor

@micielski micielski commented Aug 2, 2024

image
It also closes #4
It's weird to have .set_orientation(...) on MenuState instead of just Menu, but since it affects MenuState::right(), MenuState::left() I think it has to be this way?

I'm also wondering whether MenuState::set_orientation(...) should take mut self or a &mut self, the second option would allow the user to adjust the orientation more easily when the program is running for example when there is not enough space on one side but there is on the other

@codecov-commenter
Copy link

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

Attention: Patch coverage is 0% with 70 lines in your changes missing coverage. Please review.

Project coverage is 0.00%. Comparing base (bc7c477) to head (f383b0e).

Files Patch % Lines
src/lib.rs 0.00% 70 Missing ⚠️

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@          Coverage Diff          @@
##            main      #9   +/-   ##
=====================================
  Coverage   0.00%   0.00%           
=====================================
  Files          1       1           
  Lines        313     357   +44     
=====================================
- Misses       313     357   +44     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@joshka
Copy link
Collaborator

joshka commented Aug 3, 2024

I fixed a bunch of the clippy lints in #11, so ignore them, and the release-plz fail in #10

Copy link
Collaborator

@joshka joshka left a comment

Choose a reason for hiding this comment

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

The changes in the methods add a small amount of complexity, and there's currently no tests. I wonder if you'd consider adding some to help show how this change works with simple examples. There's two parts of that

  • how does the state work
  • how does the menu render.

src/lib.rs Outdated Show resolved Hide resolved
///
/// This is a fluent setter method which must be chained or used as it consumes self
#[must_use = "method moves the value of self and returns the modified value"]
pub const fn set_orientation(mut self, orientation: MenuOrientation) -> Self {
Copy link
Collaborator

Choose a reason for hiding this comment

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

For consitency with the methods on Menu, this should probably be just orientation, not set_orientation. This pattern is the same one we use in Ratatui, and is called the builder lite pattern.

Suggested change
pub const fn set_orientation(mut self, orientation: MenuOrientation) -> Self {
pub const fn orientation(mut self, orientation: MenuOrientation) -> Self {

If this is something that you need to change at runtime and not construction time, then it probably would be called set_ and take &mut self.

if state.orientation.is_right() {
x_pos
} else {
let name_len = u16::try_from(item.name.len()).expect("name should be short");
Copy link
Collaborator

Choose a reason for hiding this comment

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

It might be difficult to actually work out what the problem is from this error message. Perhaps give more details. This is something a user will likely see, and the dev needs some info to repro.

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 not sure what I should do here, other than change "name" to "item name". This line just calculates the length of the name of root group, e.g. File, Edit, About in the example. I could show the problematic name in the .expect(...) but I'm not sure if this is a good idea since the length of it would be bigger than u16::MAX. I could truncate it though if that's what you want

Copy link
Collaborator

Choose a reason for hiding this comment

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

It seems reasonable to truncate this instead of erroring. I'm not sure I'd want to use a UI with menus that are 65536 chars long ;)

src/lib.rs Outdated
x_pos
} else {
let name_len = u16::try_from(item.name.len()).expect("name should be short");
x_pos - self.drop_down_width + name_len
Copy link
Owner

Choose a reason for hiding this comment

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

x_pos is 0u16 for the first item, this substraction will panic.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OMG, thanks for noticing this.
Unfortunately it would panic anyway since in this case the drop-down is being rendered to the left and so since there's no place for it anyway, the Clear widget will panic with this:
Trying to access position outside the buffer
I'll fix this one in a sec

src/lib.rs Outdated
let child_x = if orientation.is_right() {
x + self.drop_down_width
} else {
x - self.drop_down_width
Copy link
Owner

@shuoli84 shuoli84 Aug 3, 2024

Choose a reason for hiding this comment

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

check if x less than self.drop_down_width

@shuoli84
Copy link
Owner

shuoli84 commented Aug 3, 2024

After tried the demo, seems the "orientation" behavior should be calculated dynamically based on current layout.

@micielski
Copy link
Contributor Author

After tried the demo, seems the "orientation" behavior should be calculated dynamically based on current layout.

Could you elaborate?

@shuoli84
Copy link
Owner

shuoli84 commented Aug 3, 2024

Sure. I don't have a clear design yet, just rough idea.
Properly render menu should be the default behavior, and don't need user to set orientation manually. E.g, if the right space is not enough, it should automatically render on left.
Is this what you wanted?

@shuoli84
Copy link
Owner

shuoli84 commented Aug 3, 2024

I opened pr #14 which fixes out-of-area display and #4. Take a look whether it fixes the issue?

@micielski micielski marked this pull request as draft August 3, 2024 18:55
@micielski micielski closed this Aug 5, 2024
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.

Indent menu when first menu item is not rendered all the way on the left side.
4 participants