Skip to content

Commit 3272bf1

Browse files
committed
Refactor animation callbacks to include Animate parameter for improved delay handling and flexibility across modulesRefactor animation callbacks to include Animate parameter for improved delay handling and dynamic behavior updates.
1 parent 3a39713 commit 3272bf1

File tree

4 files changed

+33
-10
lines changed

4 files changed

+33
-10
lines changed

animation.v

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,21 @@ const animation_delay = 500 * time.millisecond
88
interface Animation {
99
id string
1010
mut:
11+
delay time.Duration
1112
start time.Time
1213
stopped bool
1314
}
1415

1516
// Animate waits the specified delay duration and then executes the callback.
1617
pub struct Animate implements Animation {
1718
pub:
18-
id string @[required]
19-
callback fn (mut Window) @[required]
20-
delay time.Duration = animation_delay
19+
id string @[required]
20+
callback fn (mut Animate, mut Window) @[required]
2121
mut:
22+
delay time.Duration = animation_delay
2223
start time.Time
23-
repeat bool
2424
stopped bool
25+
repeat bool
2526
}
2627

2728
// animation_add registers a new animation to the window's animation queue.
@@ -72,7 +73,7 @@ fn (mut window Window) animation_loop() {
7273
fn update_animate(mut an Animate, mut w Window) bool {
7374
if !an.stopped {
7475
if time.since(an.start) > an.delay {
75-
an.callback(mut w)
76+
an.callback(mut an, mut w)
7677
match an.repeat {
7778
true { an.start = time.now() }
7879
else { an.stopped = true }

view_text.v

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,9 @@ fn (tv &TextView) mouse_move_locked(layout &Layout, mut e Event, mut w Window) {
200200
if !w.has_animation(id_auto_scroll_animation) {
201201
w.animation_add(mut Animate{
202202
id: id_auto_scroll_animation
203-
callback: fn [tv, id_focus, id_scroll_container] (mut w Window) {
204-
tv.auto_scroll_cursor(id_focus, id_scroll_container, mut w)
203+
callback: fn [tv, id_focus, id_scroll_container] (mut an Animate, mut w Window) {
204+
tv.auto_scroll_cursor(id_focus, id_scroll_container, mut an, mut
205+
w)
205206
}
206207
delay: auto_scroll_delay
207208
repeat: true
@@ -261,7 +262,7 @@ fn (tv &TextView) mouse_up_locked(layout &Layout, mut e Event, mut w Window) {
261262
// callback when the user drags to select text beyond the view boundaries. The function
262263
// scrolls one line at a time (up or down) and updates the cursor position and selection
263264
// range accordingly, providing smooth auto-scrolling behavior during drag selection.
264-
fn (tv &TextView) auto_scroll_cursor(id_focus u32, id_scroll_container u32, mut w Window) {
265+
fn (tv &TextView) auto_scroll_cursor(id_focus u32, id_scroll_container u32, mut an Animate, mut w Window) {
265266
mut layout := w.layout.find_layout(fn [id_focus] (ly Layout) bool {
266267
return ly.shape.id_scroll == id_focus
267268
}) or { return }
@@ -318,6 +319,27 @@ fn (tv &TextView) auto_scroll_cursor(id_focus u32, id_scroll_container u32, mut
318319
}
319320

320321
scroll_cursor_into_view(mouse_cursor_pos, layout, mut w)
322+
323+
// Find the scroll container
324+
scroll_container := w.layout.find_layout(fn [id_scroll_container] (ly Layout) bool {
325+
return ly.shape.id_scroll == id_scroll_container
326+
}) or { return }
327+
328+
evs := event_relative_to(scroll_container.shape, e)
329+
330+
diff := match evs.mouse_y < 0 {
331+
true { -evs.mouse_y }
332+
else { evs.mouse_y - scroll_container.shape.height }
333+
}
334+
335+
lh := line_height(layout.shape)
336+
if diff > 2 * lh {
337+
an.delay = 30 * time.millisecond
338+
} else if diff > lh {
339+
an.delay = 80 * time.millisecond
340+
} else {
341+
an.delay = auto_scroll_delay
342+
}
321343
}
322344

323345
// cursor_pos_to_scroll_y calculates the vertical scroll offset needed to make a cursor

view_tooltip.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ fn (cfg TooltipCfg) animation_tooltip() Animate {
6161
id := cfg.id
6262
return Animate{
6363
id: '___tooltip___'
64-
callback: fn [id] (mut w Window) {
64+
callback: fn [id] (mut an Animate, mut w Window) {
6565
if point_in_rectangle(w.ui.mouse_pos_x, w.ui.mouse_pos_y, gui_tooltip.bounds) {
6666
gui_tooltip.id = id
6767
}

xtra_window.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ fn (mut window Window) blinky_cursor_animation() {
99
id: '___blinky_cursor_animation___'
1010
delay: 600 * time.millisecond
1111
repeat: true
12-
callback: fn (mut w Window) {
12+
callback: fn (mut an Animate, mut w Window) {
1313
if w.view_state.cursor_on_sticky {
1414
w.view_state.input_cursor_on = true
1515
w.view_state.cursor_on_sticky = false

0 commit comments

Comments
 (0)