ltk/platform/wayland/
text_input.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// SPDX-License-Identifier: LGPL-2.1-only
// Copyright (C) 2026 Liberux Labs, S. L. <info@liberux.net>

use wayland_protocols::wp::text_input::zv3::client::zwp_text_input_v3::{ ContentHint, ContentPurpose };

use crate::app::App;
use crate::osk::Purpose;

use super::WaylandPlatform;

pub( super ) fn activate<A: App>( platform: &mut WaylandPlatform<A>, secure: bool, purpose: Purpose )
{
	let ( hint, purpose ) = content_type( secure, purpose );
	match ( &platform.text_input_manager, &platform.text_input )
	{
		( Some( manager ), None ) =>
		{
			let Some( seat ) = platform.seat_state.seats().next() else
			{
				eprintln!( "ltk: activate_text_input: no seat available" );
				return;
			};
			let ti = manager.get_text_input( &seat, &platform.qh, () );
			ti.enable();
			ti.set_content_type( hint, purpose );
			ti.commit();
			platform.text_input = Some( ti );
		}
		( None, _ ) =>
			eprintln!( "ltk: activate_text_input: no text_input_manager (compositor did not advertise zwp_text_input_manager_v3)" ),
		( Some( _ ), Some( ti ) ) =>
		{
			ti.enable();
			ti.set_content_type( hint, purpose );
			ti.commit();
		}
	}
}

pub( super ) fn deactivate<A: App>( platform: &mut WaylandPlatform<A> )
{
	if let Some( ti ) = platform.text_input.take()
	{
		ti.disable();
		ti.commit();
		ti.destroy();
	}
}

pub( super ) fn reenable<A: App>( platform: &WaylandPlatform<A>, secure: bool, purpose: Purpose )
{
	if let Some( ti ) = &platform.text_input
	{
		let ( hint, purpose ) = content_type( secure, purpose );
		ti.enable();
		ti.set_content_type( hint, purpose );
		ti.commit();
	}
}

/// Secure fields are flagged `Password` with `SensitiveData | HiddenText` so the input method skips prediction, autocorrect and storing the value.
fn content_type( secure: bool, purpose: Purpose ) -> ( ContentHint, ContentPurpose )
{
	if secure
	{
		return ( ContentHint::SensitiveData | ContentHint::HiddenText, ContentPurpose::Password );
	}
	let purpose = match purpose
	{
		Purpose::Normal   => ContentPurpose::Normal,
		Purpose::Terminal => ContentPurpose::Terminal,
	};
	( ContentHint::None, purpose )
}