https://kotlinlang.org logo
#doodle
Title
# doodle
m

M.C. Escher

03/29/2024, 2:49 PM
is it possible to disable right-click in the browser on certain elements? for example if i want to show a context-menu when someone right-clicks a button i don't want the browser context menu to show up but my own
n

Nick

03/29/2024, 2:56 PM
this should happen if you consume the pointer pressed event on your button:
Copy code
button.pointerChanged += pressed {
    // ...
    if (Button2 in it.buttons) {
        // consume should prevent the context menu
        it.consume()
    }
}
m

M.C. Escher

03/29/2024, 2:57 PM
whoa that was easy. thanks. i'm really enjoying working with your framework
❤️ 1
Copy code
if (Button2 in it.buttons) {
  it.consume()
  val popupLocation = it.location
  launch(uiDispatcher) {
    modal.invoke {
      val popup = menu(...)
      pointerOutsideModalChanged += PointerListener.clicked { completed(Unit) }
      ModalManager.Modal(popup) { modal ->
        (<http://modal.top|modal.top> eq popupLocation.y)..Strong
        (modal.left eq popupLocation.x)..Strong
      }
    }
  }
}

I'd like to show the popup at the mouse cursor's location. This code works when I use the JVM runner, however in the browser the popup is not shown. Any idea what I'm doing wrong?
it appears in the browser the size of the popup is zero
n

Nick

03/29/2024, 3:03 PM
how are you setting the popup size? i’m surprised you see a different result for desktop.
m

M.C. Escher

03/29/2024, 3:03 PM
i'm not setting the popup size at all
n

Nick

03/29/2024, 3:06 PM
make sure you do. not clear why it has a size for desktop. the modal alignment you have only positions the top/left. you could have it size the popup as well. but it might make more sense for the popup to attempt to expand to its contents.
also consider adding more constraints for the case you popup might go off the screen. you can see an example here. this can’t be totally prevented of course, but you could have it float to different places based on how close it is to the display edges. you could also have it show relative to the clicked button so it follows the pointer location and the button. just some random thoughts 😅 .
m

M.C. Escher

03/29/2024, 3:14 PM
it's not clear to me how to set the size.. calling
popup.size = Size(w,h)
doesn't seem to have an effect
n

Nick

03/29/2024, 3:18 PM
oh, sorry; i didn’t realize you were using the built in menu. do you have the right modules applied for web? you need to have a
MenuBehavior
installed and be using a
Theme
. maybe you’re doing this for desktop but not web?
m

M.C. Escher

03/29/2024, 3:19 PM
ah my bad sorry.. i forgot to add the menu-behaviour module to the JS runner's list of app-modules
👍 1
one last question for today (i hope :D)... when i set
allowPointerThrough = true
and right click a second time on my element, then it pops up a second menu, the first menu does not go away. any idea how to fix this?
n

Nick

03/29/2024, 3:25 PM
i think that’s b/c the
pointerOutsideChanged
is using a click which won’t happen b/c the pressed is consumed (press happens before click, which happens after press then release). so add a pressed listener to
pointerOutsideChanged
as well that will dismiss the modal.
m

M.C. Escher

03/29/2024, 3:26 PM
ah right. makes sense. thank you very much
👍 1