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

Add option to disable mouse dragging on scroll area. #2163

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
22 changes: 21 additions & 1 deletion crates/egui/src/containers/scroll_area.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ pub struct ScrollArea {
offset_y: Option<f32>,
/// If false, we ignore scroll events.
scrolling_enabled: bool,
/// If false we ignore mouse drag events.
mouse_dragging_enabled: bool,

/// If true for vertical or horizontal the scroll wheel will stick to the
/// end position until user manually changes position. It will become true
Expand Down Expand Up @@ -132,6 +134,7 @@ impl ScrollArea {
offset_x: None,
offset_y: None,
scrolling_enabled: true,
mouse_dragging_enabled: true,
stick_to_end: [false; 2],
}
}
Expand Down Expand Up @@ -251,6 +254,19 @@ impl ScrollArea {
self
}

/// Control the dragging behavior
/// If `true` (default), the scroll area will respond to mouse dragging.
/// If `false`, the scroll area will not respond to mouse dragging
///
/// This can be used, for example for code editors where
/// selecting and scrolling with a mouse is required.
///
/// This controls both scrolling directions.
pub fn enable_mouse_dragging(mut self, enable: bool) -> Self {
self.mouse_dragging_enabled = enable;
self
}

/// For each axis, should the containing area shrink if the content is small?
///
/// If true, egui will add blank space outside the scroll area.
Expand Down Expand Up @@ -305,6 +321,7 @@ struct Prepared {
/// `viewport.min == ZERO` means we scrolled to the top.
viewport: Rect,
scrolling_enabled: bool,
mouse_dragging_enabled: bool,
stick_to_end: [bool; 2],
}

Expand All @@ -320,6 +337,7 @@ impl ScrollArea {
offset_x,
offset_y,
scrolling_enabled,
mouse_dragging_enabled,
stick_to_end,
} = self;

Expand Down Expand Up @@ -417,6 +435,7 @@ impl ScrollArea {
content_ui,
viewport,
scrolling_enabled,
mouse_dragging_enabled,
stick_to_end,
}
}
Expand Down Expand Up @@ -521,6 +540,7 @@ impl Prepared {
content_ui,
viewport: _,
scrolling_enabled,
mouse_dragging_enabled,
stick_to_end,
} = self;

Expand Down Expand Up @@ -608,7 +628,7 @@ impl Prepared {

if content_is_too_large[0] || content_is_too_large[1] {
// Drag contents to scroll (for touch screens mostly):
let sense = if self.scrolling_enabled {
let sense = if self.mouse_dragging_enabled {
Sense::drag()
} else {
Sense::hover()
Expand Down
3 changes: 1 addition & 2 deletions crates/epaint/src/text/text_layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,13 @@ fn layout_section(

paragraph.glyphs.push(Glyph {
chr,
pos: pos2(paragraph.cursor_x, f32::NAN),
pos: pos2(font.round_to_pixel(paragraph.cursor_x), f32::NAN),
size: vec2(glyph_info.advance_width, font_height),
uv_rect: glyph_info.uv_rect,
section_index,
});

paragraph.cursor_x += glyph_info.advance_width;
paragraph.cursor_x = font.round_to_pixel(paragraph.cursor_x);
last_glyph_id = Some(glyph_info.id);
}
}
Expand Down