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(button): add button implementation #29

Merged
merged 7 commits into from
Mar 12, 2021
Merged

feat(button): add button implementation #29

merged 7 commits into from
Mar 12, 2021

Conversation

swsoyee
Copy link
Owner

@swsoyee swsoyee commented Mar 11, 2021

Close #23 .

A draft implementation of add button to the view.

r3dmol() %>%
  m_add_model(data = pdb_1j72, format = "pdb") %>%
  m_zoom_to() %>%
  m_button(
    name = "cartoon",
    label = "Cartoon",
    func = "
      function(){
        viewer.setStyle({cartoon:{}});
        viewer.render();
      }
    "
  )

Initiation

截屏2021-03-11 22 34 25

After Click the Cartoon Button

截屏2021-03-11 22 34 35

There is only one problem about this feature.

r3dmol() %>%
  m_add_model(data = pdb_1j72, format = "pdb") %>%
  m_zoom_to() %>%
  m_button(
    name = "cartoon",
    label = "Cartoon",
    func = "
      function(){
        viewer.setStyle({cartoon:{}});
        viewer.render();
      }
    "
  ) %>%
  m_button(
    name = "stick",
    label = "Stick",
    func = "
      function(){
        viewer.setStyle({stick:{}});
        viewer.render();
      }
    "
  )

截屏2021-03-11 22 38 44

If you add multiple buttons in the view, the css style of the button position needs to be automatically adjusted according to the number of button, or expose the position argments to the user.

button.setAttribute('style', 'position:absolute;top:10px;left:10px;z-index:1;');

@BradyAJohnston Could you mind helping to finish this implement?

@swsoyee swsoyee added the enhancement New feature or request label Mar 11, 2021
@BradyAJohnston
Copy link
Collaborator

Okay this fix works and seems almost too-simple. Let me know if there is a reason why it shouldn't be done this way.
I'm just using display:inline-block; which then handles all of the button layout automatically.

I tested it with up to 10 buttons and the layout works. It also adjusts with the re-sizing of the window.

What I couldn't figure out was how to have the buttons anywhere other than the top-left. It's not that important, now that the buttons are working. (thanks so much for getting the buttons working, this is awesome!).

button.setAttribute('style', 'display:inline-block;');
r3dmol() %>%
  m_add_model(data = pdb_1j72, format = "pdb") %>%
  m_zoom_to() %>%
  m_button(
    name = "cartoon",
    label = "Cartoon",
    func = "
      function(){
        viewer.setStyle({cartoon:{}});
        viewer.render();
      }
    "
  ) %>%
  m_button(
    name = "stick",
    label = "Stick",
    func = "
      function(){
        viewer.setStyle({stick:{}});
        viewer.render();
      }
    "
  )

viewer with buttons

I also don't know why the checks are failing - the devtools::check() is running fine on my machine.

@swsoyee
Copy link
Owner Author

swsoyee commented Mar 12, 2021

Thanks! Using inline-block it's a good idea for automatic layout.

What I couldn't figure out was how to have the buttons anywhere other than the top-left.

Maybe it could be done by adding display:flex to parent element, and do more css trick. I will have a try tonight.

I also don't know why the checks are failing - the devtools::check() is running fine on my machine.

In my experience, some CRAN dependencies may not be immediately available in Github Action if they have been recently updated. Or something cause it failed. You could re-run all action job by click the re-run button in action tab.

@swsoyee
Copy link
Owner Author

swsoyee commented Mar 12, 2021

Not good enough, but now the buttons are able to align in top, center, bottom on the left. I want to use justify-content for horizontal alignment but it seems not working. I can't figure out why in a short time.

You could edit this PR for more friendly for user.

r3dmol() %>%
  m_add_model(data = pdb_1j72, format = "pdb") %>%
  m_zoom_to() %>%
  m_button(
    name = "cartoon",
    label = "Cartoon",
    style = "flex-end",
    func = "
      function(){
        viewer.setStyle({cartoon:{}});
        viewer.render();
      }
    "
  ) %>%
  m_button(
    name = "stick",
    label = "Stick",
    func = "
      function(){
        viewer.setStyle({stick:{}});
        viewer.render();
      }
    "
  )

Some useful references:
https://css-tricks.com/snippets/css/a-guide-to-flexbox/
https://yoksel.github.io/flex-cheatsheet/#section-justify-content

@swsoyee
Copy link
Owner Author

swsoyee commented Mar 12, 2021

@BradyAJohnston I figure out how to fix the layout problem, please wait a moment.

@swsoyee swsoyee marked this pull request as draft March 12, 2021 14:41
@swsoyee
Copy link
Owner Author

swsoyee commented Mar 12, 2021

Now your are able to layout all the button by css style. Ready for review.

r3dmol() %>%
  m_add_model(data = pdb_1j72, format = "pdb") %>%
  m_zoom_to() %>%
  m_button(
    name = "cartoon",
    label = "Cartoon",
    align_items = "flex-end",
    justify_content = "center",
    func = "
      function(){
        viewer.setStyle({cartoon:{}});
        viewer.render();
      }
    "
  ) %>%
  m_button(
    name = "stick",
    label = "Stick",
    func = "
      function(){
        viewer.setStyle({stick:{}});
        viewer.render();
      }
    "
  )

Rplot

@swsoyee swsoyee marked this pull request as ready for review March 12, 2021 15:20
Copy link
Collaborator

@BradyAJohnston BradyAJohnston left a comment

Choose a reason for hiding this comment

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

All looks really good! Thanks for putting so much effort into this and getting it working so well.

I've got some ideas about default buttons etc that I will play around with and might put together a new PR soon.

@BradyAJohnston BradyAJohnston merged commit 04d38e5 into master Mar 12, 2021
@BradyAJohnston BradyAJohnston deleted the button branch March 12, 2021 22:28
@swsoyee swsoyee added this to the v0.1.2 milestone Mar 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Addition of Interactive Buttons
2 participants