refactor: split every monolithic module into focused submodules
Each source file that had grown beyond a single concern is replaced by an identically-named directory containing focused submodules. `src/event_loop/mod.rs` (878 lines) becomes a directory with clipboard, context_menu, cursor_shape, drag, focus, handlers, invalidation, overlays_reconcile, repeat, run, surface, text_editing, and tooltip. Every widget, input handler, and theme component follows the same split. Public interfaces are unchanged — only the internal file layout moves. image bumped from 0.25.2 to 0.25.9.
This commit is contained in:
@@ -0,0 +1,193 @@
|
||||
// SPDX-License-Identifier: LGPL-2.1-only
|
||||
// Copyright (C) 2026 Liberux Labs, S. L. <info@liberux.net>
|
||||
|
||||
use crate::app::App;
|
||||
use crate::event_loop::app_data::AppData;
|
||||
use crate::event_loop::surface::SurfaceFocus;
|
||||
|
||||
impl<A: App> AppData<A>
|
||||
{
|
||||
/// Read the current text value of the focused widget — preferring
|
||||
/// the pending typed-but-not-yet-applied value when present, falling
|
||||
/// back to the value snapshot from the per-frame handler list.
|
||||
pub( crate ) fn focused_text_value( &self, focus: SurfaceFocus ) -> Option<( usize, String )>
|
||||
{
|
||||
let idx = self.surface( focus ).focused_idx?;
|
||||
let value = if let Some( pending ) = self.surface( focus ).pending_text_values.get( &idx )
|
||||
{
|
||||
pending.clone()
|
||||
} else {
|
||||
crate::tree::find_handlers( &self.surface( focus ).widget_rects, idx )?
|
||||
.current_value()
|
||||
.map( |s| s.to_string() )
|
||||
.unwrap_or_default()
|
||||
};
|
||||
Some( ( idx, value ) )
|
||||
}
|
||||
|
||||
/// Move the text cursor one codepoint to the left. With `extend`,
|
||||
/// the selection anchor stays put (Shift+Left widens the
|
||||
/// selection); without it, the selection is collapsed to the new
|
||||
/// cursor position. No-op when no text input is focused.
|
||||
pub( crate ) fn handle_cursor_left( &mut self, focus: SurfaceFocus, extend: bool )
|
||||
{
|
||||
let ( idx, value ) = match self.focused_text_value( focus ) { Some( v ) => v, None => return };
|
||||
let cursor = self.surface( focus ).cursor_state.get( &idx ).copied()
|
||||
.unwrap_or( value.len() ).min( value.len() );
|
||||
let anchor = self.surface( focus ).selection_anchor.get( &idx ).copied()
|
||||
.unwrap_or( cursor ).min( value.len() );
|
||||
// Without shift: if a selection exists, collapse to its start.
|
||||
if !extend && anchor != cursor
|
||||
{
|
||||
let s = cursor.min( anchor );
|
||||
let ss = self.surface_mut( focus );
|
||||
*ss.cursor_state.entry( idx ).or_insert( 0 ) = s;
|
||||
*ss.selection_anchor.entry( idx ).or_insert( 0 ) = s;
|
||||
ss.request_redraw();
|
||||
return;
|
||||
}
|
||||
if cursor == 0 { return; }
|
||||
let prev_char = value[..cursor].chars().last();
|
||||
let step = prev_char.map( |c| c.len_utf8() ).unwrap_or( 1 );
|
||||
let new_cursor = cursor.saturating_sub( step );
|
||||
let ss = self.surface_mut( focus );
|
||||
*ss.cursor_state.entry( idx ).or_insert( 0 ) = new_cursor;
|
||||
if !extend { *ss.selection_anchor.entry( idx ).or_insert( 0 ) = new_cursor; }
|
||||
ss.request_redraw();
|
||||
}
|
||||
|
||||
/// Move the text cursor one codepoint to the right.
|
||||
pub( crate ) fn handle_cursor_right( &mut self, focus: SurfaceFocus, extend: bool )
|
||||
{
|
||||
let ( idx, value ) = match self.focused_text_value( focus ) { Some( v ) => v, None => return };
|
||||
let cursor = self.surface( focus ).cursor_state.get( &idx ).copied()
|
||||
.unwrap_or( value.len() ).min( value.len() );
|
||||
let anchor = self.surface( focus ).selection_anchor.get( &idx ).copied()
|
||||
.unwrap_or( cursor ).min( value.len() );
|
||||
if !extend && anchor != cursor
|
||||
{
|
||||
let e = cursor.max( anchor );
|
||||
let ss = self.surface_mut( focus );
|
||||
*ss.cursor_state.entry( idx ).or_insert( 0 ) = e;
|
||||
*ss.selection_anchor.entry( idx ).or_insert( 0 ) = e;
|
||||
ss.request_redraw();
|
||||
return;
|
||||
}
|
||||
if cursor >= value.len() { return; }
|
||||
let next_char = value[cursor..].chars().next();
|
||||
let step = next_char.map( |c| c.len_utf8() ).unwrap_or( 1 );
|
||||
let new_cursor = ( cursor + step ).min( value.len() );
|
||||
let ss = self.surface_mut( focus );
|
||||
*ss.cursor_state.entry( idx ).or_insert( 0 ) = new_cursor;
|
||||
if !extend { *ss.selection_anchor.entry( idx ).or_insert( 0 ) = new_cursor; }
|
||||
ss.request_redraw();
|
||||
}
|
||||
|
||||
/// Move the text cursor up one *visual* row. With `extend`, the
|
||||
/// selection anchor stays put.
|
||||
///
|
||||
/// In multiline mode the move follows the soft-wrapped layout — a
|
||||
/// long logical line that wraps onto N visual rows lets Up step
|
||||
/// through each of those rows in turn, instead of jumping straight
|
||||
/// over them to the previous hard `\n`. Single-line inputs and
|
||||
/// secure inputs both render as one visual row, so Up always
|
||||
/// returns `false` and the runtime falls through to sibling-widget
|
||||
/// keyboard navigation.
|
||||
pub( crate ) fn handle_cursor_up( &mut self, focus: SurfaceFocus, extend: bool ) -> bool
|
||||
{
|
||||
let ( idx, value ) = match self.focused_text_value( focus ) { Some( v ) => v, None => return false };
|
||||
let cursor = self.surface( focus ).cursor_state.get( &idx ).copied()
|
||||
.unwrap_or( value.len() ).min( value.len() );
|
||||
let ( rect, _, multiline, secure, _align, _font_size ) = match self.text_input_geometry( focus, idx )
|
||||
{
|
||||
Some( g ) => g,
|
||||
None => return false,
|
||||
};
|
||||
if !multiline || secure { return false; }
|
||||
let canvas = match self.surface( focus ).canvas.as_ref() { Some( c ) => c, None => return false };
|
||||
let new_cursor = match crate::widget::text_edit::cursor_visual_up( canvas, rect, &value, cursor )
|
||||
{
|
||||
Some( n ) => n,
|
||||
None => return false,
|
||||
};
|
||||
let ss = self.surface_mut( focus );
|
||||
*ss.cursor_state.entry( idx ).or_insert( 0 ) = new_cursor;
|
||||
if !extend { *ss.selection_anchor.entry( idx ).or_insert( 0 ) = new_cursor; }
|
||||
ss.request_redraw();
|
||||
true
|
||||
}
|
||||
|
||||
/// Move the text cursor down one *visual* row. Mirror of
|
||||
/// [`Self::handle_cursor_up`].
|
||||
pub( crate ) fn handle_cursor_down( &mut self, focus: SurfaceFocus, extend: bool ) -> bool
|
||||
{
|
||||
let ( idx, value ) = match self.focused_text_value( focus ) { Some( v ) => v, None => return false };
|
||||
let cursor = self.surface( focus ).cursor_state.get( &idx ).copied()
|
||||
.unwrap_or( value.len() ).min( value.len() );
|
||||
let ( rect, _, multiline, secure, _align, _font_size ) = match self.text_input_geometry( focus, idx )
|
||||
{
|
||||
Some( g ) => g,
|
||||
None => return false,
|
||||
};
|
||||
if !multiline || secure { return false; }
|
||||
let canvas = match self.surface( focus ).canvas.as_ref() { Some( c ) => c, None => return false };
|
||||
let new_cursor = match crate::widget::text_edit::cursor_visual_down( canvas, rect, &value, cursor )
|
||||
{
|
||||
Some( n ) => n,
|
||||
None => return false,
|
||||
};
|
||||
let ss = self.surface_mut( focus );
|
||||
*ss.cursor_state.entry( idx ).or_insert( 0 ) = new_cursor;
|
||||
if !extend { *ss.selection_anchor.entry( idx ).or_insert( 0 ) = new_cursor; }
|
||||
ss.request_redraw();
|
||||
true
|
||||
}
|
||||
|
||||
/// Move the cursor to the start of the current *visual* row (Home
|
||||
/// key). On a soft-wrapped logical line that lands at the wrap
|
||||
/// point of the row the caret is on — a second Home press from
|
||||
/// there does not move further, since the visual row already
|
||||
/// starts at that boundary.
|
||||
pub( crate ) fn handle_cursor_home( &mut self, focus: SurfaceFocus, extend: bool )
|
||||
{
|
||||
let ( idx, value ) = match self.focused_text_value( focus ) { Some( v ) => v, None => return };
|
||||
let cursor = self.surface( focus ).cursor_state.get( &idx ).copied()
|
||||
.unwrap_or( value.len() ).min( value.len() );
|
||||
let new_cursor = match self.text_input_geometry( focus, idx )
|
||||
{
|
||||
Some( ( rect, _, true, false, _, _ ) ) => match self.surface( focus ).canvas.as_ref()
|
||||
{
|
||||
Some( canvas ) => crate::widget::text_edit::cursor_visual_home( canvas, rect, &value, cursor ),
|
||||
None => value[..cursor].rfind( '\n' ).map( |p| p + 1 ).unwrap_or( 0 ),
|
||||
},
|
||||
_ => value[..cursor].rfind( '\n' ).map( |p| p + 1 ).unwrap_or( 0 ),
|
||||
};
|
||||
let ss = self.surface_mut( focus );
|
||||
*ss.cursor_state.entry( idx ).or_insert( 0 ) = new_cursor;
|
||||
if !extend { *ss.selection_anchor.entry( idx ).or_insert( 0 ) = new_cursor; }
|
||||
ss.request_redraw();
|
||||
}
|
||||
|
||||
/// Move the cursor to the end of the current *visual* row (End
|
||||
/// key). For a soft-wrapped row that's the wrap point; for a row
|
||||
/// terminated by `\n` it's the byte just before the newline.
|
||||
pub( crate ) fn handle_cursor_end( &mut self, focus: SurfaceFocus, extend: bool )
|
||||
{
|
||||
let ( idx, value ) = match self.focused_text_value( focus ) { Some( v ) => v, None => return };
|
||||
let cursor = self.surface( focus ).cursor_state.get( &idx ).copied()
|
||||
.unwrap_or( value.len() ).min( value.len() );
|
||||
let new_cursor = match self.text_input_geometry( focus, idx )
|
||||
{
|
||||
Some( ( rect, _, true, false, _, _ ) ) => match self.surface( focus ).canvas.as_ref()
|
||||
{
|
||||
Some( canvas ) => crate::widget::text_edit::cursor_visual_end( canvas, rect, &value, cursor ),
|
||||
None => value[cursor..].find( '\n' ).map( |p| cursor + p ).unwrap_or( value.len() ),
|
||||
},
|
||||
_ => value[cursor..].find( '\n' ).map( |p| cursor + p ).unwrap_or( value.len() ),
|
||||
};
|
||||
let ss = self.surface_mut( focus );
|
||||
*ss.cursor_state.entry( idx ).or_insert( 0 ) = new_cursor;
|
||||
if !extend { *ss.selection_anchor.entry( idx ).or_insert( 0 ) = new_cursor; }
|
||||
ss.request_redraw();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
// SPDX-License-Identifier: LGPL-2.1-only
|
||||
// Copyright (C) 2026 Liberux Labs, S. L. <info@liberux.net>
|
||||
|
||||
use smithay_client_toolkit::reexports::client::QueueHandle;
|
||||
use wayland_protocols::wp::text_input::zv3::client::zwp_text_input_v3;
|
||||
|
||||
use crate::app::App;
|
||||
use crate::event_loop::app_data::AppData;
|
||||
use crate::event_loop::surface::SurfaceFocus;
|
||||
use crate::tree::find_widget;
|
||||
use crate::types::Rect;
|
||||
use crate::widget::WidgetHandlers;
|
||||
|
||||
impl<A: App> AppData<A>
|
||||
{
|
||||
pub( crate ) fn activate_text_input( &mut self, qh: &QueueHandle<Self> )
|
||||
{
|
||||
if let ( Some( manager ), None ) = ( &self.text_input_manager, &self.text_input )
|
||||
{
|
||||
let seats: Vec<_> = self.seat_state.seats().collect();
|
||||
if let Some( seat ) = seats.into_iter().next()
|
||||
{
|
||||
let ti = manager.get_text_input( &seat, qh, () );
|
||||
ti.enable();
|
||||
ti.set_content_type(
|
||||
zwp_text_input_v3::ContentHint::None,
|
||||
zwp_text_input_v3::ContentPurpose::Normal,
|
||||
);
|
||||
ti.commit();
|
||||
self.text_input = Some( ti );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub( crate ) fn deactivate_text_input( &mut self )
|
||||
{
|
||||
if let Some( ti ) = self.text_input.take()
|
||||
{
|
||||
ti.disable();
|
||||
ti.commit();
|
||||
ti.destroy();
|
||||
}
|
||||
}
|
||||
|
||||
/// Snapshot a focused-widget's geometry needed by the pointer
|
||||
/// hit-testers for text editing. Returns `None` when the widget
|
||||
/// isn't a TextEdit or its rect is missing — the helper above
|
||||
/// short-circuits in that case.
|
||||
pub( crate ) fn text_input_geometry(
|
||||
&self,
|
||||
focus: SurfaceFocus,
|
||||
idx: usize,
|
||||
) -> Option<( Rect, String, bool, bool, crate::widget::text::TextAlign, f32 )>
|
||||
{
|
||||
let ss = self.surface( focus );
|
||||
let widget = find_widget( &ss.widget_rects, idx )?;
|
||||
let ( value_handler, multiline, secure, align, font_size ) = match &widget.handlers
|
||||
{
|
||||
WidgetHandlers::TextEdit { value, multiline, secure, align, font_size, .. } =>
|
||||
( value.clone(), *multiline, *secure, *align, *font_size ),
|
||||
_ => return None,
|
||||
};
|
||||
// Prefer the *pending* value (typed-but-not-yet-applied) when
|
||||
// it exists — that's what the user sees right now and what
|
||||
// the cursor measurements should be relative to.
|
||||
let value = ss.pending_text_values.get( &idx )
|
||||
.cloned()
|
||||
.unwrap_or( value_handler );
|
||||
Some( ( widget.rect, value, multiline, secure, align, font_size ) )
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,194 @@
|
||||
// SPDX-License-Identifier: LGPL-2.1-only
|
||||
// Copyright (C) 2026 Liberux Labs, S. L. <info@liberux.net>
|
||||
|
||||
use crate::app::App;
|
||||
use crate::event_loop::app_data::AppData;
|
||||
use crate::event_loop::surface::SurfaceFocus;
|
||||
use crate::tree::find_handlers;
|
||||
use crate::widget::WidgetHandlers;
|
||||
|
||||
impl<A: App> AppData<A>
|
||||
{
|
||||
pub( crate ) fn handle_text_insert( &mut self, focus: SurfaceFocus, text: &str )
|
||||
{
|
||||
let idx = match self.surface( focus ).focused_idx { Some( i ) => i, None => return };
|
||||
|
||||
// If a selection is active, replace it. The deletion path
|
||||
// folds anchor=cursor and updates `pending_text_values`, so
|
||||
// the rest of this fn keeps the same shape after.
|
||||
let _ = self.delete_selection( focus );
|
||||
|
||||
let current_value = if let Some( pending ) = self.surface( focus ).pending_text_values.get( &idx )
|
||||
{
|
||||
pending.clone()
|
||||
} else {
|
||||
find_handlers( &self.surface( focus ).widget_rects, idx )
|
||||
.and_then( |h| h.current_value() )
|
||||
.map( |s| s.to_string() )
|
||||
.unwrap_or_default()
|
||||
};
|
||||
|
||||
// `select_on_focus` fields keep the cursor pinned to the end
|
||||
// of the *displayed* value. The pending value the user just
|
||||
// typed (e.g. "2") may render shorter than the post-update
|
||||
// display value (e.g. "02" after a `format!("{:02}")`
|
||||
// normalisation in the app's on_change handler), so anchoring
|
||||
// the cursor at "end of pending" would land it mid-string in
|
||||
// the next render. `usize::MAX` is a sentinel: every consumer
|
||||
// (`draw`, arrow-key handlers, `byte_offset_at`) clamps via
|
||||
// `cursor.min( value.len() )`, which resolves to "end of
|
||||
// whatever value we eventually render". Without this, typing
|
||||
// a second digit would insert into the middle of the
|
||||
// normalised string ("0|2" + "3" → "032" → 32 instead of 23).
|
||||
let select_on_focus = matches!(
|
||||
find_handlers( &self.surface( focus ).widget_rects, idx ),
|
||||
Some( WidgetHandlers::TextEdit { select_on_focus: true, .. } ),
|
||||
);
|
||||
|
||||
let new_value;
|
||||
{
|
||||
let ss = self.surface_mut( focus );
|
||||
let cursor = ss.cursor_state.entry( idx ).or_insert( current_value.len() );
|
||||
let safe_cursor = (*cursor).min( current_value.len() );
|
||||
let mut v = current_value.clone();
|
||||
v.insert_str( safe_cursor, text );
|
||||
let next_cursor = if select_on_focus { usize::MAX } else { safe_cursor + text.len() };
|
||||
*ss.cursor_state.entry( idx ).or_insert( 0 ) = next_cursor;
|
||||
*ss.selection_anchor.entry( idx ).or_insert( 0 ) = next_cursor;
|
||||
ss.pending_text_values.insert( idx, v.clone() );
|
||||
ss.request_redraw();
|
||||
new_value = v;
|
||||
}
|
||||
|
||||
let msg = find_handlers( &self.surface( focus ).widget_rects, idx )
|
||||
.and_then( |h| h.text_change_msg( &new_value ) );
|
||||
if let Some( m ) = msg
|
||||
{
|
||||
self.pending_msgs.push( m );
|
||||
}
|
||||
}
|
||||
|
||||
/// Forward delete — the `Delete` (Supr) key. Mirrors
|
||||
/// [`Self::handle_backspace`] but removes the character *after*
|
||||
/// the cursor instead of before. Selection-aware: if a range is
|
||||
/// active it is removed in one step, exactly like backspace.
|
||||
pub( crate ) fn handle_delete_forward( &mut self, focus: SurfaceFocus )
|
||||
{
|
||||
let idx = match self.surface( focus ).focused_idx { Some( i ) => i, None => return };
|
||||
|
||||
if let Some( new_value ) = self.delete_selection( focus )
|
||||
{
|
||||
let msg = find_handlers( &self.surface( focus ).widget_rects, idx )
|
||||
.and_then( |h| h.text_change_msg( &new_value ) );
|
||||
if let Some( m ) = msg { self.pending_msgs.push( m ); }
|
||||
return;
|
||||
}
|
||||
|
||||
let current_value = if let Some( pending ) = self.surface( focus ).pending_text_values.get( &idx )
|
||||
{
|
||||
pending.clone()
|
||||
} else {
|
||||
find_handlers( &self.surface( focus ).widget_rects, idx )
|
||||
.and_then( |h| h.current_value() )
|
||||
.map( |s| s.to_string() )
|
||||
.unwrap_or_default()
|
||||
};
|
||||
|
||||
let cursor_val = self.surface( focus ).cursor_state.get( &idx ).copied()
|
||||
.unwrap_or( current_value.len() );
|
||||
let safe_cursor_pre = cursor_val.min( current_value.len() );
|
||||
if safe_cursor_pre >= current_value.len() { return; }
|
||||
// Width of the char *starting* at the cursor — UTF-8 aware so
|
||||
// `é` / `🦀` come out as one keypress.
|
||||
let next_char = current_value[safe_cursor_pre..].chars().next();
|
||||
let step = match next_char { Some( c ) => c.len_utf8(), None => return };
|
||||
let mut new_value = current_value.clone();
|
||||
new_value.replace_range( safe_cursor_pre..safe_cursor_pre + step, "" );
|
||||
// `select_on_focus` fields keep the cursor at the end of the
|
||||
// post-update displayed value via the `usize::MAX` sentinel —
|
||||
// see `handle_text_insert` for the reasoning.
|
||||
let select_on_focus = matches!(
|
||||
find_handlers( &self.surface( focus ).widget_rects, idx ),
|
||||
Some( WidgetHandlers::TextEdit { select_on_focus: true, .. } ),
|
||||
);
|
||||
let next_cursor = if select_on_focus { usize::MAX } else { safe_cursor_pre };
|
||||
{
|
||||
let ss = self.surface_mut( focus );
|
||||
// Cursor stays put — the char to its right is gone, so the
|
||||
// remaining tail shifts left under it.
|
||||
*ss.cursor_state.entry( idx ).or_insert( 0 ) = next_cursor;
|
||||
*ss.selection_anchor.entry( idx ).or_insert( 0 ) = next_cursor;
|
||||
ss.pending_text_values.insert( idx, new_value.clone() );
|
||||
ss.request_redraw();
|
||||
}
|
||||
|
||||
let msg = find_handlers( &self.surface( focus ).widget_rects, idx )
|
||||
.and_then( |h| h.text_change_msg( &new_value ) );
|
||||
if let Some( m ) = msg
|
||||
{
|
||||
self.pending_msgs.push( m );
|
||||
}
|
||||
}
|
||||
|
||||
pub( crate ) fn handle_backspace( &mut self, focus: SurfaceFocus )
|
||||
{
|
||||
let idx = match self.surface( focus ).focused_idx { Some( i ) => i, None => return };
|
||||
|
||||
// Selection-aware: if a selection is active, backspace just
|
||||
// deletes the range. The on_change message is emitted from
|
||||
// here so the app sees a single text update.
|
||||
if let Some( new_value ) = self.delete_selection( focus )
|
||||
{
|
||||
let msg = find_handlers( &self.surface( focus ).widget_rects, idx )
|
||||
.and_then( |h| h.text_change_msg( &new_value ) );
|
||||
if let Some( m ) = msg { self.pending_msgs.push( m ); }
|
||||
return;
|
||||
}
|
||||
|
||||
let current_value = if let Some( pending ) = self.surface( focus ).pending_text_values.get( &idx )
|
||||
{
|
||||
pending.clone()
|
||||
} else {
|
||||
find_handlers( &self.surface( focus ).widget_rects, idx )
|
||||
.and_then( |h| h.current_value() )
|
||||
.map( |s| s.to_string() )
|
||||
.unwrap_or_default()
|
||||
};
|
||||
|
||||
// Scoped block to release the immutable borrow of `self` (via
|
||||
// `self.surface( focus )`) before taking a mutable one below.
|
||||
let cursor_val = self.surface( focus ).cursor_state.get( &idx ).copied()
|
||||
.unwrap_or( current_value.len() );
|
||||
if cursor_val == 0 { return; }
|
||||
let safe_cursor = cursor_val.min( current_value.len() );
|
||||
let chars: Vec<char> = current_value[..safe_cursor].chars().collect();
|
||||
if chars.is_empty() { return; }
|
||||
|
||||
let removed_char = *chars.last().unwrap();
|
||||
let new_cursor = safe_cursor - removed_char.len_utf8();
|
||||
let mut new_value = current_value.clone();
|
||||
new_value.remove( new_cursor );
|
||||
// `select_on_focus` fields keep the cursor at the end of the
|
||||
// post-update displayed value via the `usize::MAX` sentinel —
|
||||
// see `handle_text_insert` for the reasoning.
|
||||
let select_on_focus = matches!(
|
||||
find_handlers( &self.surface( focus ).widget_rects, idx ),
|
||||
Some( WidgetHandlers::TextEdit { select_on_focus: true, .. } ),
|
||||
);
|
||||
let next_cursor = if select_on_focus { usize::MAX } else { new_cursor };
|
||||
{
|
||||
let ss = self.surface_mut( focus );
|
||||
*ss.cursor_state.entry( idx ).or_insert( 0 ) = next_cursor;
|
||||
*ss.selection_anchor.entry( idx ).or_insert( 0 ) = next_cursor;
|
||||
ss.pending_text_values.insert( idx, new_value.clone() );
|
||||
ss.request_redraw();
|
||||
}
|
||||
|
||||
let msg = find_handlers( &self.surface( focus ).widget_rects, idx )
|
||||
.and_then( |h| h.text_change_msg( &new_value ) );
|
||||
if let Some( m ) = msg
|
||||
{
|
||||
self.pending_msgs.push( m );
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
// SPDX-License-Identifier: LGPL-2.1-only
|
||||
// Copyright (C) 2026 Liberux Labs, S. L. <info@liberux.net>
|
||||
|
||||
//! Text-editing dispatch on [`super::app_data::AppData`]: cursor
|
||||
//! movement, insert/delete, selection, IME activation and clipboard
|
||||
//! integration. Lives on the runtime side; the corresponding widget
|
||||
//! (`crate::widget::text_edit`) only owns the layout / draw path.
|
||||
|
||||
pub( crate ) mod cursor;
|
||||
pub( crate ) mod insert_delete;
|
||||
pub( crate ) mod selection;
|
||||
pub( crate ) mod ime;
|
||||
@@ -0,0 +1,263 @@
|
||||
// SPDX-License-Identifier: LGPL-2.1-only
|
||||
// Copyright (C) 2026 Liberux Labs, S. L. <info@liberux.net>
|
||||
|
||||
use crate::app::App;
|
||||
use crate::event_loop::app_data::AppData;
|
||||
use crate::event_loop::surface::SurfaceFocus;
|
||||
use crate::tree::find_handlers;
|
||||
use crate::types::Point;
|
||||
use crate::widget::WidgetHandlers;
|
||||
|
||||
/// Compute the byte range of the "word" surrounding `byte_offset` in
|
||||
/// `value`. A word is a maximal run of alphanumeric / underscore
|
||||
/// chars; whitespace and punctuation count as separators. When
|
||||
/// `byte_offset` falls *between* two non-word chars, both bounds
|
||||
/// collapse to that offset (zero-width selection — the caller is
|
||||
/// expected to either accept that or fall back to a click-position
|
||||
/// cursor).
|
||||
fn word_bounds_at( value: &str, byte_offset: usize ) -> ( usize, usize )
|
||||
{
|
||||
if value.is_empty() { return ( 0, 0 ); }
|
||||
let bo = byte_offset.min( value.len() );
|
||||
let is_word = | c: char | c.is_alphanumeric() || c == '_';
|
||||
|
||||
// Walk left from `bo` until we cross a non-word boundary.
|
||||
let mut start = bo;
|
||||
{
|
||||
let mut iter = value[..bo].char_indices().rev();
|
||||
while let Some( ( i, c ) ) = iter.next()
|
||||
{
|
||||
if is_word( c ) { start = i; } else { break; }
|
||||
}
|
||||
}
|
||||
|
||||
// Walk right from `bo` until the same.
|
||||
let mut end = bo;
|
||||
for ( i, c ) in value[bo..].char_indices()
|
||||
{
|
||||
if is_word( c ) { end = bo + i + c.len_utf8(); } else { break; }
|
||||
}
|
||||
|
||||
( start, end )
|
||||
}
|
||||
|
||||
impl<A: App> AppData<A>
|
||||
{
|
||||
/// `true` when this press should be treated as the second click
|
||||
/// of a double-click pair. Looks at the timestamp + position of
|
||||
/// the previous press and compares against a 400 ms / 6 px
|
||||
/// threshold (matching the GTK / Cocoa defaults). Always updates
|
||||
/// the snapshot so the next press can pair with this one.
|
||||
pub( crate ) fn note_press_for_double_click( &mut self, pos: Point ) -> bool
|
||||
{
|
||||
const WINDOW_MS: u128 = 400;
|
||||
const RADIUS: f32 = 6.0;
|
||||
let now = std::time::Instant::now();
|
||||
let is_double = match ( self.last_press_time, self.last_press_pos )
|
||||
{
|
||||
( Some( t ), Some( p ) ) =>
|
||||
{
|
||||
let elapsed = now.duration_since( t ).as_millis();
|
||||
let dx = pos.x - p.x;
|
||||
let dy = pos.y - p.y;
|
||||
elapsed <= WINDOW_MS && dx.hypot( dy ) <= RADIUS
|
||||
}
|
||||
_ => false,
|
||||
};
|
||||
if is_double
|
||||
{
|
||||
// Consume the snapshot so a third quick click is not
|
||||
// re-paired with the same first click.
|
||||
self.last_press_time = None;
|
||||
self.last_press_pos = None;
|
||||
} else {
|
||||
self.last_press_time = Some( now );
|
||||
self.last_press_pos = Some( pos );
|
||||
}
|
||||
is_double
|
||||
}
|
||||
|
||||
/// Select the word under the press. The "word" is the maximal
|
||||
/// run of alphanumeric / underscore chars surrounding the byte
|
||||
/// offset that `pos` falls on; everything else (whitespace,
|
||||
/// punctuation) counts as a separator. Empty value → no-op.
|
||||
pub( crate ) fn handle_text_select_word( &mut self, focus: SurfaceFocus, idx: usize, pos: Point )
|
||||
{
|
||||
// `select_on_focus` fields are short-form: the whole value is
|
||||
// already the natural selection unit, so a double-click does
|
||||
// not need to add a word-bound selection on top.
|
||||
let select_on_focus = matches!(
|
||||
find_handlers( &self.surface( focus ).widget_rects, idx ),
|
||||
Some( WidgetHandlers::TextEdit { select_on_focus: true, .. } ),
|
||||
);
|
||||
if select_on_focus { return; }
|
||||
let ( rect, value, multiline, secure, align, font_size ) = match self.text_input_geometry( focus, idx )
|
||||
{
|
||||
Some( v ) => v,
|
||||
None => return,
|
||||
};
|
||||
if value.is_empty() { return; }
|
||||
let cursor_pos = self.surface( focus ).cursor_state.get( &idx ).copied().unwrap_or( 0 );
|
||||
let click_byte = {
|
||||
let ss = self.surface( focus );
|
||||
let canvas = match ss.canvas.as_ref() { Some( c ) => c, None => return };
|
||||
crate::widget::text_edit::byte_offset_at(
|
||||
canvas, rect, pos, &value, multiline, secure, cursor_pos, align, font_size,
|
||||
)
|
||||
};
|
||||
let ( start, end ) = word_bounds_at( &value, click_byte );
|
||||
let ss = self.surface_mut( focus );
|
||||
ss.selection_anchor.insert( idx, start );
|
||||
ss.cursor_state.insert( idx, end );
|
||||
ss.request_redraw();
|
||||
}
|
||||
|
||||
/// Position the text cursor at the click and start a fresh
|
||||
/// selection (anchor = cursor). Called from the pointer / touch
|
||||
/// press handler after focus has been moved to the TextEdit.
|
||||
pub( crate ) fn handle_text_pointer_down( &mut self, focus: SurfaceFocus, idx: usize, pos: Point )
|
||||
{
|
||||
// Fields with `select_on_focus` (numeric pickers, short-form
|
||||
// inputs) keep the whole-value selection that `set_focus`
|
||||
// just installed — clicking is the *focus* gesture and the
|
||||
// next keystroke is meant to replace, not insert. Without
|
||||
// this guard the click-to-position below would collapse the
|
||||
// selection right after `set_focus` produced it.
|
||||
let select_on_focus = matches!(
|
||||
find_handlers( &self.surface( focus ).widget_rects, idx ),
|
||||
Some( WidgetHandlers::TextEdit { select_on_focus: true, .. } ),
|
||||
);
|
||||
if select_on_focus { return; }
|
||||
let ( rect, value, multiline, secure, align, font_size ) = match self.text_input_geometry( focus, idx )
|
||||
{
|
||||
Some( v ) => v,
|
||||
None => return,
|
||||
};
|
||||
let cursor_pos = self.surface( focus ).cursor_state.get( &idx ).copied().unwrap_or( 0 );
|
||||
let byte_offset =
|
||||
{
|
||||
let ss = self.surface( focus );
|
||||
let canvas = match ss.canvas.as_ref() { Some( c ) => c, None => return };
|
||||
crate::widget::text_edit::byte_offset_at(
|
||||
canvas, rect, pos, &value, multiline, secure, cursor_pos, align, font_size,
|
||||
)
|
||||
};
|
||||
let ss = self.surface_mut( focus );
|
||||
ss.cursor_state.insert( idx, byte_offset );
|
||||
ss.selection_anchor.insert( idx, byte_offset );
|
||||
ss.request_redraw();
|
||||
}
|
||||
|
||||
/// Extend the selection by moving the cursor to the new pointer
|
||||
/// position. Called from the pointer / touch motion handler while
|
||||
/// a press is active and the original press landed on a TextEdit.
|
||||
/// The selection anchor (set on press) stays put.
|
||||
pub( crate ) fn handle_text_pointer_drag( &mut self, focus: SurfaceFocus, idx: usize, pos: Point )
|
||||
{
|
||||
// Same `select_on_focus` guard as the press-down path: the
|
||||
// short-form field stays whole-selected even if the user
|
||||
// drags the pointer over the digits.
|
||||
let select_on_focus = matches!(
|
||||
find_handlers( &self.surface( focus ).widget_rects, idx ),
|
||||
Some( WidgetHandlers::TextEdit { select_on_focus: true, .. } ),
|
||||
);
|
||||
if select_on_focus { return; }
|
||||
let ( rect, value, multiline, secure, align, font_size ) = match self.text_input_geometry( focus, idx )
|
||||
{
|
||||
Some( v ) => v,
|
||||
None => return,
|
||||
};
|
||||
let cursor_pos = self.surface( focus ).cursor_state.get( &idx ).copied().unwrap_or( 0 );
|
||||
let byte_offset =
|
||||
{
|
||||
let ss = self.surface( focus );
|
||||
let canvas = match ss.canvas.as_ref() { Some( c ) => c, None => return };
|
||||
crate::widget::text_edit::byte_offset_at(
|
||||
canvas, rect, pos, &value, multiline, secure, cursor_pos, align, font_size,
|
||||
)
|
||||
};
|
||||
let ss = self.surface_mut( focus );
|
||||
ss.cursor_state.insert( idx, byte_offset );
|
||||
ss.request_redraw();
|
||||
}
|
||||
|
||||
/// Collapse any active selection on the focused text input to its
|
||||
/// cursor position. Returns `true` when a collapse happened so
|
||||
/// the caller (Esc handler) can short-circuit other behaviour.
|
||||
pub( crate ) fn collapse_selection_if_any( &mut self, focus: SurfaceFocus ) -> bool
|
||||
{
|
||||
let idx = match self.surface( focus ).focused_idx { Some( i ) => i, None => return false };
|
||||
let cursor = self.surface( focus ).cursor_state.get( &idx ).copied();
|
||||
let anchor = self.surface( focus ).selection_anchor.get( &idx ).copied();
|
||||
let ( c, a ) = match ( cursor, anchor )
|
||||
{
|
||||
( Some( c ), Some( a ) ) if c != a => ( c, a ),
|
||||
_ => return false,
|
||||
};
|
||||
let _ = a;
|
||||
let ss = self.surface_mut( focus );
|
||||
ss.selection_anchor.insert( idx, c );
|
||||
ss.request_redraw();
|
||||
true
|
||||
}
|
||||
|
||||
/// Select the entire value of the focused text input. Used by
|
||||
/// Ctrl+A.
|
||||
pub( crate ) fn handle_select_all( &mut self, focus: SurfaceFocus )
|
||||
{
|
||||
let ( idx, value ) = match self.focused_text_value( focus ) { Some( v ) => v, None => return };
|
||||
let ss = self.surface_mut( focus );
|
||||
*ss.cursor_state.entry( idx ).or_insert( 0 ) = value.len();
|
||||
*ss.selection_anchor.entry( idx ).or_insert( 0 ) = 0;
|
||||
ss.request_redraw();
|
||||
}
|
||||
|
||||
/// Snapshot the currently-selected text for the focused widget.
|
||||
/// Returns `None` when no text input is focused or the selection
|
||||
/// is empty.
|
||||
pub( crate ) fn focused_selection_text( &self, focus: SurfaceFocus ) -> Option<String>
|
||||
{
|
||||
let ( idx, value ) = self.focused_text_value( focus )?;
|
||||
let cursor = self.surface( focus ).cursor_state.get( &idx ).copied()
|
||||
.unwrap_or( value.len() ).min( value.len() );
|
||||
let anchor = self.surface( focus ).selection_anchor.get( &idx ).copied()
|
||||
.unwrap_or( cursor ).min( value.len() );
|
||||
if anchor == cursor { return None; }
|
||||
let s = cursor.min( anchor );
|
||||
let e = cursor.max( anchor );
|
||||
Some( value[s..e].to_string() )
|
||||
}
|
||||
|
||||
/// Delete the current selection (if non-empty), updating the
|
||||
/// pending text value and cursor / anchor. Returns the resulting
|
||||
/// string when a deletion happened so the caller can dispatch the
|
||||
/// `on_change` message; returns `None` when there was no selection
|
||||
/// to delete.
|
||||
pub( crate ) fn delete_selection( &mut self, focus: SurfaceFocus ) -> Option<String>
|
||||
{
|
||||
let ( idx, value ) = self.focused_text_value( focus )?;
|
||||
let cursor = self.surface( focus ).cursor_state.get( &idx ).copied()
|
||||
.unwrap_or( value.len() ).min( value.len() );
|
||||
let anchor = self.surface( focus ).selection_anchor.get( &idx ).copied()
|
||||
.unwrap_or( cursor ).min( value.len() );
|
||||
if anchor == cursor { return None; }
|
||||
let s = cursor.min( anchor );
|
||||
let e = cursor.max( anchor );
|
||||
let mut new_value = value;
|
||||
new_value.replace_range( s..e, "" );
|
||||
// `select_on_focus` fields keep the cursor at the end of the
|
||||
// post-update displayed value via the `usize::MAX` sentinel —
|
||||
// see `handle_text_insert` for the reasoning.
|
||||
let select_on_focus = matches!(
|
||||
find_handlers( &self.surface( focus ).widget_rects, idx ),
|
||||
Some( WidgetHandlers::TextEdit { select_on_focus: true, .. } ),
|
||||
);
|
||||
let next_cursor = if select_on_focus { usize::MAX } else { s };
|
||||
let ss = self.surface_mut( focus );
|
||||
*ss.cursor_state.entry( idx ).or_insert( 0 ) = next_cursor;
|
||||
*ss.selection_anchor.entry( idx ).or_insert( 0 ) = next_cursor;
|
||||
ss.pending_text_values.insert( idx, new_value.clone() );
|
||||
ss.request_redraw();
|
||||
Some( new_value )
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user