The primary selection is kept in step with other applications, an application can paste either selection and hear the middle button, raw keys carry the text dead keys compose, and an application can ask for a cursor where the pointer is
CI / build + test (push) Canceled after 0s
CI / cargo audit (push) Canceled after 0s

A terminal drawn by the application itself needed four things ltk did not give it: the selection the middle button pastes, a way to read the clipboard, the text a key types once dead keys are applied, and a text cursor over its text area that does not hide the window's resize cursors. Text fields gain the primary selection along the way, and the symbol fallback and a missed field in the previous commit are fixed.
The primary selection. `event_loop/primary_selection.rs` binds `zwp_primary_selection_device_manager_v1` when the compositor offers it, creates the device with the data device in `new_seat`, and holds the manager, the device, our source while we own the selection, its text and the inbox the reader threads post to. A selection offered by another client is read on a thread of its own as the clipboard is; a peer asking for ours is served from the cached text, and a cancelled source is dropped. `data_device.rs` gives both the pieces they share: `pick_text_mime`, `read_selection` and `write_selection`, so the clipboard behaves as before.
Text fields. Once a press ends, `sync_text_field` publishes a new selection in the focused text field as the primary selection; password fields never publish theirs. It remembers what it last published rather than comparing with the current primary selection, so a selection made in another application is not taken back. A middle-button press on a text field focuses it, puts the cursor where it landed and inserts the primary selection there.
The application's side. `App::take_primary_request` makes a text the primary selection, as `take_clipboard_request` does for the clipboard. `App::take_paste_request` names a `SelectionKind`, `Clipboard` or `Primary`, and the runtime answers with `App::on_paste` and that selection's text, never with an empty one. `App::on_pointer_middle` reports a middle-button press anywhere but on a text field. All default to doing nothing.
Raw keys. `App::on_raw_key_with_alt` gives way to `App::on_raw_key_event`, which takes a `RawKey` with the keysym, the keycode, the press, Ctrl, Shift and Alt, and `text`: what sctk reports the key types once dead keys and compose sequences are applied, `None` on release, halfway through a sequence and for keys that type nothing. A terminal that took the character from the keysym typed `a` for an acute accent followed by `a`. The default still forwards to `on_raw_key`, and prompt was the only application overriding the old hook. Every key press now also records its serial in `last_input_serial`, so a selection set from a keyboard shortcut carries a serial a strict compositor accepts rather than the last click's.
The cursor. `App::cursor_at( x, y )` is the cursor the application wants at a point of the main surface. It comes after the resize edges and `cursor_override`, and before the cursor of the widget under the pointer, so a terminal shows the text cursor over its cells and the resize cursors still show at the window's edges, which a `cursor_override` would hide.
Fonts. Noto Sans Symbols 2 and Noto Sans Symbols follow DejaVu Sans in the fallback chain, for the media controls, arrows and shapes DejaVu lacks, which drew as the missing glyph.
`TextEdit::map_msg` now carries `pad_h` over: the previous commit added the field but left it out there, and ltk did not build.
This commit is contained in:
2026-10-14 16:41:54 +02:00
parent e3b76423fb
commit 99b195ea13
14 changed files with 281 additions and 39 deletions
+42 -3
View File
@@ -363,6 +363,30 @@ pub enum BackAtRoot
Ignore,
}
/// A key press or release as [`App::on_raw_key_event`] reports it.
#[ derive( Debug, Clone ) ]
pub struct RawKey
{
pub keysym: Keysym,
pub keycode: u32,
pub pressed: bool,
pub ctrl: bool,
pub shift: bool,
pub alt: bool,
/// What the key types once dead keys and compose sequences are applied; `None` on release, halfway through a sequence and for keys that type nothing.
pub text: Option<String>,
}
/// Which of the two selections an app reads, through [`App::take_paste_request`].
#[ derive( Debug, Clone, Copy, PartialEq, Eq ) ]
pub enum SelectionKind
{
/// What Copy put there, pasted with Ctrl+V.
Clipboard,
/// The text last selected, pasted with the middle button.
Primary,
}
/// Trait that application types must implement to integrate with ltk.
pub trait App: 'static
{
@@ -882,6 +906,15 @@ pub trait App: 'static
/// Return `Some( text )` once to put it in the clipboard, for other applications as well.
fn take_clipboard_request( &mut self ) -> Option<String> { None }
/// Return `Some( text )` once to make it the primary selection, which the middle button pastes, here and in other applications.
fn take_primary_request( &mut self ) -> Option<String> { None }
/// Return `Some( kind )` once to have [`Self::on_paste`] called with the text that selection holds.
fn take_paste_request( &mut self ) -> Option<SelectionKind> { None }
/// The text of the selection [`Self::take_paste_request`] asked for; not called when it is empty.
fn on_paste( &mut self, _text: &str ) {}
/// Called on every pointer motion (mouse move, button press, button
/// release) and on every primary-finger touch down and motion. `x`, `y` are in physical pixels of the main surface,
/// the same coordinate space as widget rects. Default: no-op.
@@ -982,6 +1015,9 @@ pub trait App: 'static
/// A secondary-button press that no widget of ltk's own answered, in physical pixels. Default: no-op.
fn on_pointer_secondary( &mut self, _x: f32, _y: f32 ) {}
/// A middle-button press, in physical pixels. Default: no-op.
fn on_pointer_middle( &mut self, _x: f32, _y: f32 ) {}
/// Called on every keyboard event (press *and* release) before the
/// regular focus-aware dispatch ([`Self::on_key`] /
/// [`Self::on_key_with_modifiers`]) runs. Default: no-op.
@@ -1001,10 +1037,10 @@ pub trait App: 'static
/// input for the embedded system, and goes through [`Self::on_back`].
fn on_raw_key( &mut self, _keysym: Keysym, _keycode: u32, _pressed: bool, _ctrl: bool, _shift: bool ) {}
/// [`Self::on_raw_key`] with the Alt state too; the runtime calls this one, which forwards to `on_raw_key` unless overridden. Override it instead when Alt matters, as in a terminal.
fn on_raw_key_with_alt( &mut self, keysym: Keysym, keycode: u32, pressed: bool, ctrl: bool, shift: bool, _alt: bool )
/// [`Self::on_raw_key`] with the Alt state and the typed text too; the runtime calls this one, which forwards to `on_raw_key` unless overridden. Override it instead when Alt or dead keys matter, as in a terminal.
fn on_raw_key_event( &mut self, key: &RawKey )
{
self.on_raw_key( keysym, keycode, pressed, ctrl, shift );
self.on_raw_key( key.keysym, key.keycode, key.pressed, key.ctrl, key.shift );
}
/// `true` while every key belongs to [`Self::on_raw_key`], as in a terminal: ltk then neither dispatches keys to its widgets, so Enter and Space do not press the focused or hovered one, nor repeats them. `XF86_Back` still goes through [`Self::on_back`].
@@ -1071,6 +1107,9 @@ pub trait App: 'static
/// next iteration without any extra wiring.
fn cursor_override( &self ) -> Option<crate::types::CursorShape> { None }
/// The cursor the app wants at `( x, y )`, in physical pixels of the main surface: below the window's resize edges and [`Self::cursor_override`], above the cursor of the widget under it. `None`, the default, leaves the widget's.
fn cursor_at( &self, _x: f32, _y: f32 ) -> Option<crate::types::CursorShape> { None }
/// Decide whether a given key participates in held-key repeat.
///
/// Default: every non-modifier key repeats *except* `Escape`,