1
0
Fork 0

Remove tooltip position when tooltip width changes

This commit is contained in:
Robbert van der Helm 2022-11-15 17:52:45 +01:00
parent 5a9786fd53
commit 4b81191225

View file

@ -18,7 +18,6 @@ use nih_plug::prelude::Param;
use nih_plug_vizia::vizia::prelude::*; use nih_plug_vizia::vizia::prelude::*;
use nih_plug_vizia::widgets::param_base::ParamWidgetBase; use nih_plug_vizia::widgets::param_base::ParamWidgetBase;
use nih_plug_vizia::widgets::util::{self, ModifiersExt}; use nih_plug_vizia::widgets::util::{self, ModifiersExt};
use nih_plug_vizia::widgets::RawParamEvent;
/// When shift+dragging the X-Y pad, one pixel dragged corresponds to this much change in the /// When shift+dragging the X-Y pad, one pixel dragged corresponds to this much change in the
/// normalized parameter. /// normalized parameter.
@ -68,6 +67,11 @@ pub struct GranularDragStatus {
pub y_starting_value: f32, pub y_starting_value: f32,
} }
enum XyPadEvent {
/// The tooltip's size has changed. This causes us to recompute the tooltip position.
TooltipWidthChanged,
}
impl XyPad { impl XyPad {
/// Creates a new [`XyPad`] for the given parameter. See /// Creates a new [`XyPad`] for the given parameter. See
/// [`ParamSlider`][nih_plug_vizia::widgets::ParamSlider] for more information on this /// [`ParamSlider`][nih_plug_vizia::widgets::ParamSlider] for more information on this
@ -162,6 +166,14 @@ impl XyPad {
.left(XyPad::tooltip_pos_x) .left(XyPad::tooltip_pos_x)
.top(XyPad::tooltip_pos_y) .top(XyPad::tooltip_pos_y)
.position_type(PositionType::SelfDirected) .position_type(PositionType::SelfDirected)
.on_geo_changed(|cx, change_flags| {
// When a new parameter value causes the width of the tooltip to
// change, we must recompute its position so it stays anchored to
// the mouse cursor
if change_flags.intersects(GeometryChanged::WIDTH_CHANGED) {
cx.emit(XyPadEvent::TooltipWidthChanged);
}
})
.hoverable(false); .hoverable(false);
}, },
); );
@ -271,16 +283,16 @@ impl View for XyPad {
fn event(&mut self, cx: &mut EventContext, event: &mut Event) { fn event(&mut self, cx: &mut EventContext, event: &mut Event) {
event.map(|window_event, _meta| { event.map(|window_event, _meta| {
if let RawParamEvent::ParametersChanged = window_event { // With an `if let` clippy complains about the irrefutable match, but in case we add
// The tooltip tracks the mouse position, but it also needs to be recomputed when // more events it's a good idea to prevent this from acting as a wildcard.
// the parameter changes while the tooltip is still visible. Without this the let XyPadEvent::TooltipWidthChanged = window_event;
// position maya be off when the parameter is automated, or because of the samll
// delay between interacting with a parameter and the parameter changing. // The tooltip tracks the mouse position, but it also needs to be recomputed when
// FIXME: This _may_ improve the positioning but it most likely doesn't. Relayouting // the parameter changes while the tooltip is still visible. Without this the
// happens _after_ this event. // position maya be off when the parameter is automated, or because of the samll
if cx.hovered() == cx.current() { // delay between interacting with a parameter and the parameter changing.
self.update_tooltip_pos(cx); if cx.hovered() == cx.current() {
} self.update_tooltip_pos(cx);
} }
}); });
@ -344,7 +356,8 @@ impl View for XyPad {
} }
WindowEvent::MouseMove(x, y) => { WindowEvent::MouseMove(x, y) => {
// The tooltip should track the mouse position. This is also recomputed whenever // The tooltip should track the mouse position. This is also recomputed whenever
// parameter values change so it stays in the correct position. // parameter values change (and thus the tooltip's width changes) so it stays in the
// correct position.
self.update_tooltip_pos(cx); self.update_tooltip_pos(cx);
if self.drag_active { if self.drag_active {