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

Slider event fixes #1270

Merged
merged 5 commits into from
Feb 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ NOTE: [`epaint`](epaint/CHANGELOG.md), [`eframe`](eframe/CHANGELOG.md), [`egui_w
* Calling `Context::set_pixels_per_point` before the first frame will now work.
* Tooltips that don't fit the window don't flicker anymore ([#1240](https://github.com/emilk/egui/pull/1240)).
* Scroll areas now follow text cursor ([1252](https://github.com/emilk/egui/pull/1252)).
* Slider: correctly respond with drag and focus events when interacting with the value directly ([1270](https://github.com/emilk/egui/pull/1270)).

### Contributors 🙏
* [AlexxxRu](https://github.com/alexxxru): [#1108](https://github.com/emilk/egui/pull/1108).
Expand Down
2 changes: 2 additions & 0 deletions egui/src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,8 @@ impl Response {
impl Response {
/// A logical "or" operation.
/// For instance `a.union(b).hovered` means "was either a or b hovered?".
///
/// The resulting [`Self::id`] will come from the first (`self`) argument.
pub fn union(&self, other: Self) -> Self {
assert!(self.ctx == other.ctx);
crate::egui_assert!(
Expand Down
2 changes: 1 addition & 1 deletion egui/src/widgets/drag_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ impl<'a> Widget for DragValue<'a> {
emath::format_with_decimals_in_range(value, auto_decimals..=max_decimals)
};

let kb_edit_id = ui.auto_id_with("edit");
let kb_edit_id = ui.next_auto_id();
let is_kb_editing = ui.memory().has_focus(kb_edit_id);

let mut response = if is_kb_editing {
Expand Down
47 changes: 27 additions & 20 deletions egui/src/widgets/slider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,10 +300,10 @@ impl<'a> Slider<'a> {

impl<'a> Slider<'a> {
/// Just the slider, no text
fn allocate_slider_space(&self, ui: &mut Ui, perpendicular: f32) -> Response {
fn allocate_slider_space(&self, ui: &mut Ui, thickness: f32) -> Response {
let desired_size = match self.orientation {
SliderOrientation::Horizontal => vec2(ui.spacing().slider_width, perpendicular),
SliderOrientation::Vertical => vec2(perpendicular, ui.spacing().slider_width),
SliderOrientation::Horizontal => vec2(ui.spacing().slider_width, thickness),
SliderOrientation::Vertical => vec2(thickness, ui.spacing().slider_width),
};
ui.allocate_response(desired_size, Sense::click_and_drag())
}
Expand Down Expand Up @@ -453,15 +453,7 @@ impl<'a> Slider<'a> {
}
}

fn label_ui(&mut self, ui: &mut Ui) {
if !self.text.is_empty() {
let text_color = self.text_color.unwrap_or_else(|| ui.visuals().text_color());
let text = RichText::new(&self.text).color(text_color);
ui.add(Label::new(text).wrap(false));
}
}

fn value_ui(&mut self, ui: &mut Ui, position_range: RangeInclusive<f32>) {
fn value_ui(&mut self, ui: &mut Ui, position_range: RangeInclusive<f32>) -> Response {
// If `DragValue` is controlled from the keyboard and `step` is defined, set speed to `step`
let change = ui.input().num_presses(Key::ArrowUp) as i32
+ ui.input().num_presses(Key::ArrowRight) as i32
Expand All @@ -472,7 +464,7 @@ impl<'a> Slider<'a> {
_ => self.current_gradient(&position_range),
};
let mut value = self.get_value();
ui.add(
let response = ui.add(
DragValue::new(&mut value)
.speed(speed)
.clamp_range(self.clamp_range())
Expand All @@ -484,6 +476,7 @@ impl<'a> Slider<'a> {
if value != self.get_value() {
self.set_value(value);
}
response
}

/// delta(value) / delta(points)
Expand All @@ -499,21 +492,35 @@ impl<'a> Slider<'a> {
}

fn add_contents(&mut self, ui: &mut Ui) -> Response {
let perpendicular = ui
let thickness = ui
.text_style_height(&TextStyle::Body)
.at_least(ui.spacing().interact_size.y);
let slider_response = self.allocate_slider_space(ui, perpendicular);
self.slider_ui(ui, &slider_response);
let mut response = self.allocate_slider_space(ui, thickness);
self.slider_ui(ui, &response);

if self.show_value {
let position_range = self.position_range(&slider_response.rect);
self.value_ui(ui, position_range);
let position_range = self.position_range(&response.rect);
let value_response = self.value_ui(ui, position_range);
if value_response.gained_focus()
|| value_response.has_focus()
|| value_response.lost_focus()
{
// Use the `DragValue` id as the id of the whole widget,
// so that the focus events work as expected.
response = value_response.union(response);
} else {
// Use the slider id as the id for the whole widget
response = response.union(value_response);
}
}

if !self.text.is_empty() {
self.label_ui(ui);
let text_color = self.text_color.unwrap_or_else(|| ui.visuals().text_color());
let text = RichText::new(&self.text).color(text_color);
ui.add(Label::new(text).wrap(false));
}
slider_response

response
}
}

Expand Down