Skip to content

Commit 3ea5ea4

Browse files
committed
Refactor hover and auto-scroll logic; optimize cursor updates and introduce dynamic scroll speeds.
1 parent 3272bf1 commit 3ea5ea4

File tree

3 files changed

+19
-36
lines changed

3 files changed

+19
-36
lines changed

view_input.v

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -532,19 +532,20 @@ fn (cfg &InputCfg) amend_layout(mut layout Layout, mut w Window) {
532532
if layout.shape.disabled {
533533
return
534534
}
535-
536535
if layout.shape.id_focus > 0 && layout.shape.id_focus == w.id_focus() {
537536
layout.shape.color = cfg.color_border_focus
538537
}
539538
}
540539

541540
fn (cfg &InputCfg) hover(mut layout Layout, mut e Event, mut w Window) {
542-
if !w.is_focus(layout.shape.id_focus) {
541+
if w.is_focus(layout.shape.id_focus) {
542+
w.set_mouse_cursor_ibeam()
543+
} else {
543544
layout.children[0].shape.color = cfg.color_hover
544545
}
545546
}
546547

547-
fn (cfg &InputCfg) hover_icon(mut layout Layout, mut e Event, mut w Window) {
548+
fn (_ &InputCfg) hover_icon(mut layout Layout, mut e Event, mut w Window) {
548549
if layout.shape.on_click != unsafe { nil } {
549550
w.set_mouse_cursor_pointing_hand()
550551
}

view_text.v

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ import math
1010
import time
1111

1212
const id_auto_scroll_animation = 'auto_scroll_animation'
13-
const auto_scroll_delay = 150 * time.millisecond
13+
const auto_scroll_slow = 150 * time.millisecond
14+
const auto_scroll_medium = 80 * time.millisecond
15+
const auto_scroll_fast = 30 * time.millisecond
1416

1517
// TextMode controls how a text view renders text.
1618
pub enum TextMode as u8 {
@@ -130,9 +132,6 @@ pub fn text(cfg TextView) View {
130132
// It sets up mouse locking for drag selection updates and positions the text cursor
131133
// based on the click coordinates.
132134
fn (tv &TextView) on_click(layout &Layout, mut e Event, mut w Window) {
133-
if w.is_focus(layout.shape.id_focus) {
134-
w.set_mouse_cursor_ibeam()
135-
}
136135
if e.mouse_button == .left && w.is_focus(layout.shape.id_focus) {
137136
id_focus := layout.shape.id_focus
138137
cursor_pos := tv.mouse_cursor_pos(layout.shape, e, mut w)
@@ -177,9 +176,6 @@ fn (tv &TextView) on_click(layout &Layout, mut e Event, mut w Window) {
177176
// It updates the text selection range based on the current mouse position relative to the
178177
// starting cursor position.
179178
fn (tv &TextView) mouse_move_locked(layout &Layout, mut e Event, mut w Window) {
180-
if w.is_focus(layout.shape.id_focus) {
181-
w.set_mouse_cursor_ibeam()
182-
}
183179
// mouse_move events don't have mouse button info. Use context.
184180
if w.ui.mouse_buttons == .left && w.is_focus(layout.shape.id_focus) {
185181
if tv.placeholder_active {
@@ -204,7 +200,7 @@ fn (tv &TextView) mouse_move_locked(layout &Layout, mut e Event, mut w Window) {
204200
tv.auto_scroll_cursor(id_focus, id_scroll_container, mut an, mut
205201
w)
206202
}
207-
delay: auto_scroll_delay
203+
delay: auto_scroll_slow
208204
repeat: true
209205
})
210206
}
@@ -233,26 +229,9 @@ fn (tv &TextView) mouse_move_locked(layout &Layout, mut e Event, mut w Window) {
233229
}
234230

235231
// mouse_up_locked handles mouse up events while the mouse is locked (after a drag selection).
236-
// It finalizes the cursor position based on the mouse release location, updates the view state
237-
// with the new cursor position, and ensures the cursor is scrolled into view.
238232
fn (tv &TextView) mouse_up_locked(layout &Layout, mut e Event, mut w Window) {
239233
if w.is_focus(layout.shape.id_focus) {
240-
w.set_mouse_cursor_ibeam()
241234
w.remove_animation(id_auto_scroll_animation)
242-
243-
// determine the cursor position from the mouse position
244-
ev := event_relative_to(layout.shape, e)
245-
mouse_cursor_pos := tv.mouse_cursor_pos(layout.shape, ev, mut w)
246-
247-
// update the view state with the new mouse_cursor_pos
248-
input_state := w.view_state.input_state[layout.shape.id_focus]
249-
w.view_state.input_state[layout.shape.id_focus] = InputState{
250-
...input_state
251-
cursor_pos: mouse_cursor_pos
252-
cursor_offset: -1
253-
}
254-
255-
scroll_cursor_into_view(mouse_cursor_pos, layout, mut w)
256235
e.is_handled = true
257236
}
258237
}
@@ -320,25 +299,28 @@ fn (tv &TextView) auto_scroll_cursor(id_focus u32, id_scroll_container u32, mut
320299

321300
scroll_cursor_into_view(mouse_cursor_pos, layout, mut w)
322301

302+
// Decide how fast the scroll animation should be based on the distance
303+
// the mouse is outside the scroll container view.
304+
//
323305
// Find the scroll container
324306
scroll_container := w.layout.find_layout(fn [id_scroll_container] (ly Layout) bool {
325307
return ly.shape.id_scroll == id_scroll_container
326308
}) or { return }
327309

328310
evs := event_relative_to(scroll_container.shape, e)
329311

330-
diff := match evs.mouse_y < 0 {
312+
distance := match evs.mouse_y < 0 {
331313
true { -evs.mouse_y }
332314
else { evs.mouse_y - scroll_container.shape.height }
333315
}
334316

335317
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
318+
if distance > 2 * lh {
319+
an.delay = auto_scroll_fast
320+
} else if distance > lh {
321+
an.delay = auto_scroll_medium
340322
} else {
341-
an.delay = auto_scroll_delay
323+
an.delay = auto_scroll_slow
342324
}
343325
}
344326

xtra_window.v

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ pub fn (mut window Window) dialog_is_visible() bool {
6969
return window.dialog_cfg.visible
7070
}
7171

72-
// default_view creates an empty view
72+
// empty_view - default_view creates an empty view
7373
fn empty_view(window &Window) View {
7474
w, h := window.window_size()
7575
return column(
@@ -80,7 +80,7 @@ fn empty_view(window &Window) View {
8080

8181
// get_dropped_file_paths gets the paths names of the dropped files.
8282
// Use in EventType.dropped_files. See `drop_files_demo.v` in examples.
83-
pub fn (window &Window) get_dropped_file_paths() []string {
83+
pub fn (_ &Window) get_dropped_file_paths() []string {
8484
len := sapp.get_num_dropped_files()
8585
mut paths := []string{cap: len}
8686
for i in 0 .. len {

0 commit comments

Comments
 (0)