Skip to content

Commit ebaac42

Browse files
committed
Improve input handling and scrolling logic in multiline text views
- Refined padding calculations in `view_text.v` to ensure correct scroll container height.
1 parent 73090b6 commit ebaac42

File tree

3 files changed

+29
-17
lines changed

3 files changed

+29
-17
lines changed

examples/multiline_input.v

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import gui
33
// Multiline Input Demo
44
// =============================
55

6+
const input_id_focus = 1
7+
const input_id_scroll = 1
8+
69
@[heap]
710
struct MultilineApp {
811
mut:
@@ -18,6 +21,7 @@ fn main() {
1821
cursor_blink: true
1922
on_init: fn (mut w gui.Window) {
2023
w.update_view(main_view)
24+
w.set_id_focus(input_id_focus)
2125
}
2226
)
2327
window.set_theme(gui.theme_dark_bordered)
@@ -34,8 +38,8 @@ fn main_view(window &gui.Window) gui.View {
3438
sizing: gui.fixed_fixed
3539
content: [
3640
gui.input(
37-
id_focus: 1
38-
id_scroll: 1
41+
id_focus: input_id_focus
42+
id_scroll: input_id_scroll
3943
scroll_mode: .vertical_only
4044
text: app.text
4145
mode: .multiline

view_input.v

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -164,24 +164,25 @@ pub fn input(cfg InputCfg) View {
164164
max_width: cfg.max_width
165165
min_height: cfg.min_height
166166
max_height: cfg.max_height
167-
padding: cfg.padding_border
167+
disabled: cfg.disabled
168168
color: cfg.color_border
169+
invisible: cfg.invisible
169170
fill: cfg.fill_border
170-
sizing: cfg.sizing
171+
padding: cfg.padding_border
171172
radius: cfg.radius_border
172-
disabled: cfg.disabled
173-
invisible: cfg.invisible
173+
sizing: cfg.sizing
174174
on_char: cfg.on_char
175-
amend_layout: cfg.amend_layout
176175
on_hover: cfg.hover
176+
amend_layout: cfg.amend_layout
177177
content: [
178178
row(
179179
name: 'input interior'
180180
color: cfg.color
181-
padding: cfg.padding
181+
clip: true
182182
fill: cfg.fill
183-
sizing: fill_fill
183+
padding: cfg.padding
184184
radius: cfg.radius
185+
sizing: fill_fill
185186
spacing: spacing_small
186187
on_click: cfg.on_click_interior
187188
content: [

view_text.v

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -228,11 +228,18 @@ fn (tv &TextView) mouse_up_locked(layout &Layout, mut e Event, mut w Window) {
228228
// adjusts the scroll offset if the cursor is outside the current visible
229229
// area.
230230
fn scroll_cursor_into_view(cursor_pos int, layout &Layout, _ &Event, mut w Window) {
231+
id_scroll_container := layout.shape.id_scroll_container
232+
231233
// Find the scroll container and calculate height. (need to start at the root layout)
232-
scroll_container := w.layout.find_layout(fn [layout] (ly Layout) bool {
233-
return ly.shape.id_scroll == layout.shape.id_scroll_container
234+
scroll_container := w.layout.find_layout(fn [id_scroll_container] (ly Layout) bool {
235+
return ly.shape.id_scroll == id_scroll_container
234236
}) or { return }
235-
scroll_view_height := scroll_container.shape.height - scroll_container.shape.padding.height()
237+
238+
mut padding_height := scroll_container.shape.padding.height()
239+
if scroll_container.children.len > 0 {
240+
padding_height += scroll_container.children[0].shape.padding.height()
241+
}
242+
scroll_view_height := scroll_container.shape.height - padding_height
236243

237244
// Find the index of the line where the cursor is located.
238245
mut line_idx := 0
@@ -252,18 +259,18 @@ fn scroll_cursor_into_view(cursor_pos int, layout &Layout, _ &Event, mut w Windo
252259
cursor_h_y := cursor_y + scroll_view_height - lh
253260

254261
// Calculate scroll offsets for current visible region
255-
current_scroll_y := w.view_state.scroll_y[layout.shape.id_scroll_container]
256-
current_scroll_h_y := current_scroll_y - scroll_view_height
262+
current_scroll_y := w.view_state.scroll_y[id_scroll_container]
263+
current_scroll_h_y := current_scroll_y - scroll_view_height + lh
257264

258265
// Determine if we need to scroll:
259266
// 1. If cursor is above the current view
260267
// 2. If cursor is below the current view
261268
new_scroll_y := match true {
262-
cursor_y > current_scroll_y { cursor_y }
263-
cursor_y <= current_scroll_h_y { cursor_h_y }
269+
cursor_y > current_scroll_y { cursor_y - 1 }
270+
cursor_y < current_scroll_h_y { cursor_h_y }
264271
else { current_scroll_y }
265272
}
266-
w.scroll_vertical_to(layout.shape.id_scroll_container, new_scroll_y)
273+
w.scroll_vertical_to(id_scroll_container, new_scroll_y)
267274
}
268275

269276
// mouse_cursor_pos determines the character index (cursor position) within

0 commit comments

Comments
 (0)