Merge pull request #520 from deemoun/hide-scrollbar

Hide the Scrollbar when it isn't needed #507
This commit is contained in:
Dmitry Yarygin 2026-03-03 01:17:47 +07:00 committed by GitHub
parent 7b511d737f
commit e11902c5bf
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -579,7 +579,9 @@ where
let mut start_line_opt = None; let mut start_line_opt = None;
let mut end_line = 0; let mut end_line = 0;
let mut max_line_width = 0.0; let mut max_line_width = 0.0;
let mut layout_run_count = 0usize;
for run in buffer.layout_runs() { for run in buffer.layout_runs() {
layout_run_count += 1;
end_line = run.line_i; end_line = run.line_i;
if start_line_opt.is_none() { if start_line_opt.is_none() {
start_line_opt = Some(end_line); start_line_opt = Some(end_line);
@ -594,6 +596,13 @@ where
let start_y = (start_line * image_h as usize) / lines; let start_y = (start_line * image_h as usize) / lines;
let end_y = ((end_line + 1) * image_h as usize) / lines; let end_y = ((end_line + 1) * image_h as usize) / lines;
let original_condition = start_line > 0 || (end_line + 1) < lines;
let visible_runs = (((image_h as f32 / scale_factor) / metrics.line_height)
.floor() as usize)
.max(1);
let wrap_overflow = layout_run_count > visible_runs;
let scrollable = original_condition || wrap_overflow;
if scrollable {
let rect = Rectangle::new( let rect = Rectangle::new(
[image_w as f32 / scale_factor, start_y as f32 / scale_factor].into(), [image_w as f32 / scale_factor, start_y as f32 / scale_factor].into(),
Size::new( Size::new(
@ -601,7 +610,10 @@ where
(end_y as f32 - start_y as f32) / scale_factor, (end_y as f32 - start_y as f32) / scale_factor,
), ),
); );
state.scrollbar_v_rect.set(rect); state.scrollbar_v_rect.set(Some(rect));
} else {
state.scrollbar_v_rect.set(None);
}
let (buffer_w_opt, buffer_h_opt) = buffer.size(); let (buffer_w_opt, buffer_h_opt) = buffer.size();
let buffer_w = buffer_w_opt.unwrap_or(0.0); let buffer_w = buffer_w_opt.unwrap_or(0.0);
@ -723,8 +735,7 @@ where
}); });
// Draw vertical scrollbar // Draw vertical scrollbar
{ if let Some(scrollbar_v_rect) = state.scrollbar_v_rect.get() {
let scrollbar_v_rect = state.scrollbar_v_rect.get();
// neutral_3, 0.7 // neutral_3, 0.7
let track_color = cosmic_theme let track_color = cosmic_theme
@ -1158,7 +1169,8 @@ where
} }
state.click = Some((click_kind, Instant::now())); state.click = Some((click_kind, Instant::now()));
state.dragging = Some(Dragging::Buffer); state.dragging = Some(Dragging::Buffer);
} else if scrollbar_v_rect.contains(Point::new(x_logical, y_logical)) { } else if let Some(scrollbar_v_rect) = scrollbar_v_rect {
if scrollbar_v_rect.contains(Point::new(x_logical, y_logical)) {
state.dragging = Some(Dragging::ScrollbarV { state.dragging = Some(Dragging::ScrollbarV {
start_y: y, start_y: y,
start_scroll: editor.with_buffer(|buffer| buffer.scroll()), start_scroll: editor.with_buffer(|buffer| buffer.scroll()),
@ -1181,6 +1193,7 @@ where
}); });
} }
} }
}
// Update context menu state // Update context menu state
if let Some(on_context_menu) = &self.on_context_menu { if let Some(on_context_menu) = &self.on_context_menu {
@ -1358,7 +1371,7 @@ pub struct State {
is_focused: bool, is_focused: bool,
emit_focus: bool, emit_focus: bool,
scale_factor: Cell<f32>, scale_factor: Cell<f32>,
scrollbar_v_rect: Cell<Rectangle<f32>>, scrollbar_v_rect: Cell<Option<Rectangle<f32>>>,
scrollbar_h_rect: Cell<Option<Rectangle<f32>>>, scrollbar_h_rect: Cell<Option<Rectangle<f32>>>,
handle_opt: Mutex<Option<image::Handle>>, handle_opt: Mutex<Option<image::Handle>>,
shift_anchor: Mutex<Option<Cursor>>, shift_anchor: Mutex<Option<Cursor>>,
@ -1375,7 +1388,7 @@ impl State {
is_focused: false, is_focused: false,
emit_focus: false, emit_focus: false,
scale_factor: Cell::new(1.0), scale_factor: Cell::new(1.0),
scrollbar_v_rect: Cell::new(Rectangle::default()), scrollbar_v_rect: Cell::new(None),
scrollbar_h_rect: Cell::new(None), scrollbar_h_rect: Cell::new(None),
handle_opt: Mutex::new(None), handle_opt: Mutex::new(None),
shift_anchor: Mutex::new(None), shift_anchor: Mutex::new(None),