Skip to content

Commit

Permalink
Allow horizontal scroll to fit cursor, related to #59
Browse files Browse the repository at this point in the history
  • Loading branch information
jackpot51 committed May 30, 2024
1 parent 102a3f1 commit 68dd4c3
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 33 deletions.
40 changes: 20 additions & 20 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,4 @@ wgpu = ["libcosmic/wgpu", "cosmic-files/wgpu"]

[profile.release-with-debug]
inherits = "release"
debug = true
debug = true
2 changes: 1 addition & 1 deletion src/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use cosmic::widget::menu::key_bind::KeyBind;
use cosmic::widget::menu::{items as menu_items, root as menu_root, Item as MenuItem};
use cosmic::{
iced::{widget::column, widget::horizontal_rule, Alignment, Background, Length},
iced::{widget::column, widget::horizontal_rule, Background, Length},
iced_core::Border,
theme,
widget::{
Expand Down
25 changes: 14 additions & 11 deletions src/text_box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,12 @@ where
}

// Draw editor
let scroll_x = editor.with_buffer(|buffer| buffer.scroll().horizontal as i32);
editor.draw(font_system.raw(), &mut swash_cache, |x, y, w, h, color| {
if x < scroll_x {
//TODO: modify width?
return;
}
draw_rect(
pixels,
Canvas {
Expand All @@ -581,7 +586,7 @@ where
h: h as i32,
},
Offset {
x: editor_offset_x + x,
x: editor_offset_x + x - scroll_x,
y,
},
color,
Expand Down Expand Up @@ -921,12 +926,11 @@ where
&& x_logical < (scrollbar_rect.x + scrollbar_rect.width)
{
editor.with_buffer_mut(|buffer| {
let mut scroll = buffer.scroll();
let scroll_line =
((y / buffer.size().1) * buffer.lines.len() as f32) as i32;
buffer.set_scroll(Scroll::new(
scroll_line.try_into().unwrap_or_default(),
0,
));
scroll.line = scroll_line.try_into().unwrap_or_default();
buffer.set_scroll(scroll);
state.dragging = Some(Dragging::Scrollbar {
start_y: y,
start_scroll: buffer.scroll(),
Expand Down Expand Up @@ -973,15 +977,14 @@ where
start_scroll,
} => {
editor.with_buffer_mut(|buffer| {
let mut scroll = buffer.scroll();
let scroll_offset = (((y - start_y) / buffer.size().1)
* buffer.lines.len() as f32)
as i32;
buffer.set_scroll(Scroll::new(
(start_scroll.line as i32 + scroll_offset)
.try_into()
.unwrap_or_default(),
0,
));
scroll.line = (start_scroll.line as i32 + scroll_offset)
.try_into()
.unwrap_or_default();
buffer.set_scroll(scroll);
});
}
}
Expand Down

0 comments on commit 68dd4c3

Please sign in to comment.