mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 03:57:43 +00:00
LibGUI+SoundPlayer: Add Slider option to jump to cursor
When the cursor is clicked outside of the slider knob, the current behavior is that it will step up or down by the Slider page step amount. This commit adds an option to jump the slider knob directly to the where the mouse cursor is on mouse down events. This behavior is disabled by default. It must be enabled with `Slider::set_jump_to_cursor()`. Jump to cursor is enabled in SoundPlayer since most music players have this behavior.
This commit is contained in:
parent
6612e026ba
commit
c3a60a5dcd
3 changed files with 22 additions and 2 deletions
|
@ -87,10 +87,26 @@ void Slider::mousedown_event(MouseEvent& event)
|
|||
m_drag_origin = event.position();
|
||||
m_drag_origin_value = value();
|
||||
return;
|
||||
}
|
||||
|
||||
const auto mouse_offset = event.position().primary_offset_for_orientation(orientation());
|
||||
|
||||
if (jump_to_cursor()) {
|
||||
float normalized_mouse_offset = 0.0f;
|
||||
if (orientation() == Orientation::Vertical) {
|
||||
normalized_mouse_offset = static_cast<float>(mouse_offset) / static_cast<float>(height());
|
||||
} else {
|
||||
normalized_mouse_offset = static_cast<float>(mouse_offset) / static_cast<float>(width());
|
||||
}
|
||||
|
||||
int new_value = static_cast<int>(min() + ((max() - min()) * normalized_mouse_offset));
|
||||
set_value(new_value);
|
||||
} else {
|
||||
if (event.position().primary_offset_for_orientation(orientation()) > knob_rect().last_edge_for_orientation(orientation()))
|
||||
auto knob_first_edge = knob_rect().first_edge_for_orientation(orientation());
|
||||
auto knob_last_edge = knob_rect().last_edge_for_orientation(orientation());
|
||||
if (mouse_offset > knob_last_edge)
|
||||
set_value(value() + page_step());
|
||||
else if (event.position().primary_offset_for_orientation(orientation()) < knob_rect().first_edge_for_orientation(orientation()))
|
||||
else if (mouse_offset < knob_first_edge)
|
||||
set_value(value() - page_step());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue