accesskit_android/
action.rs

1// Copyright 2025 The AccessKit Authors. All rights reserved.
2// Licensed under the Apache License, Version 2.0 (found in
3// the LICENSE-APACHE file) or the MIT license (found in
4// the LICENSE-MIT file), at your option.
5
6use jni::{objects::JObject, sys::jint, JNIEnv};
7
8use crate::util::*;
9
10pub(crate) enum PlatformActionInner {
11    Simple {
12        action: jint,
13    },
14    SetTextSelection {
15        anchor: jint,
16        focus: jint,
17    },
18    CollapseTextSelection,
19    TraverseText {
20        granularity: jint,
21        forward: bool,
22        extend_selection: bool,
23    },
24}
25
26pub struct PlatformAction(pub(crate) PlatformActionInner);
27
28impl PlatformAction {
29    pub fn from_java(env: &mut JNIEnv, action: jint, arguments: &JObject) -> Option<Self> {
30        match action {
31            ACTION_SET_SELECTION => {
32                if !(!arguments.is_null()
33                    && bundle_contains_key(env, arguments, ACTION_ARGUMENT_SELECTION_START_INT)
34                    && bundle_contains_key(env, arguments, ACTION_ARGUMENT_SELECTION_END_INT))
35                {
36                    return Some(Self(PlatformActionInner::CollapseTextSelection));
37                }
38                let anchor = bundle_get_int(env, arguments, ACTION_ARGUMENT_SELECTION_START_INT);
39                let focus = bundle_get_int(env, arguments, ACTION_ARGUMENT_SELECTION_END_INT);
40                Some(Self(PlatformActionInner::SetTextSelection {
41                    anchor,
42                    focus,
43                }))
44            }
45            ACTION_NEXT_AT_MOVEMENT_GRANULARITY | ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY => {
46                if arguments.is_null()
47                    || !bundle_contains_key(
48                        env,
49                        arguments,
50                        ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT,
51                    )
52                {
53                    return None;
54                }
55                let granularity =
56                    bundle_get_int(env, arguments, ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT);
57                let forward = action == ACTION_NEXT_AT_MOVEMENT_GRANULARITY;
58                let extend_selection =
59                    bundle_get_bool(env, arguments, ACTION_ARGUMENT_EXTEND_SELECTION_BOOLEAN);
60                Some(Self(PlatformActionInner::TraverseText {
61                    granularity,
62                    forward,
63                    extend_selection,
64                }))
65            }
66            _ => Some(Self(PlatformActionInner::Simple { action })),
67        }
68    }
69}