a11y 0.2.2

无障碍(ACCESSIBILITY,A11Y,辅助功能)
Documentation
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
/*
 * Copyright (c) 2024. The RigelA open source project team and
 * its contributors reserve all rights.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software distributed under the
 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and limitations under the License.
 */

use crate::jab::{
    hypertext::AccessibleHypertext,
    jab_lib::{
        packages::{
            AccessibleActionInfo, AccessibleActions, AccessibleActionsToDo,
            AccessibleContext as AC, AccessibleContextInfo, JInt, MAX_ACTION_INFO, MAX_STRING_SIZE,
            SHORT_STRING_SIZE,
        },
        JabLib,
    },
    key_binding::AccessibleKeyBinding,
    relation::AccessibleRelation,
    role::AccessibleRole,
    table::AccessibleTable,
    text::AccessibleTextAttributes,
    version::AccessBridgeVersionInfo,
};
use std::{
    cmp::min,
    ffi::{c_char, CStr},
    fmt::{Debug, Formatter},
};
use win_wrap::{common::HWND, ext::StringExt};

pub struct AccessibleContext<'lib> {
    _lib: &'lib JabLib,
    _ac: AC,
    _vm_id: i32,
    _info: Option<AccessibleContextInfo>,
}

impl<'lib> AccessibleContext<'lib> {
    /**

     * 创建一个实例。
     * `lib` 库引用。
     * `vm_id` 虚拟机ID。
     * `ac` 原始上下文对象。
     * */
    pub(crate) fn new(lib: &'lib JabLib, vm_id: i32, ac: AC) -> Self {
        Self {
            _lib: &lib,
            _vm_id: vm_id,
            _ac: ac,
            _info: lib.get_accessible_context_info(vm_id, ac),
        }
    }
    pub(crate) fn from_hwnd(lib: &'lib JabLib, h_wnd: HWND) -> Option<Self> {
        if let Some((vm_id, ac)) = lib.get_accessible_context_from_hwnd(h_wnd) {
            return Some(Self::new(&lib, vm_id, ac));
        }
        None
    }

    pub(crate) fn from_focus(lib: &'lib JabLib, h_wnd: HWND) -> Option<AccessibleContext<'lib>> {
        if let Some((vm_id, ac)) = lib.get_accessible_context_with_focus(h_wnd) {
            return Some(Self::new(&lib, vm_id, ac));
        }
        None
    }

    /**

     * 从顶级窗口的AccessibleContext返回HWND。
     * */
    pub fn get_hwnd(&self) -> HWND {
        self._lib
            .get_hwnd_from_accessible_context(self._vm_id, self._ac)
    }

    /**

     * 获取唯一ID。
     * */
    pub fn get_unique_id(&self) -> i32 {
        self._ac as i32
    }

    /**

     * 查询窗口可访问上下文的对象或鼠标指针下的对象。
     * `x` X坐标。
     * `y` Y坐标。
     * */
    pub fn get_at(&self, x: i32, y: i32) -> Option<AccessibleContext<'lib>> {
        if let Some(ac) = self
            ._lib
            .get_accessible_context_at(self._vm_id, self._ac, x, y)
        {
            return Some(Self::new(&self._lib, self._vm_id, ac));
        }
        None
    }

    /**

     * 返回一个对象,该对象表示该可访问上下文对象的第n个子对象,其中n由值索引指定。
     * `index` 子对象索引。
     * */
    pub fn get_child(&self, index: i32) -> Option<AccessibleContext<'lib>> {
        if let Some(child) =
            self._lib
                .get_accessible_child_from_context(self._vm_id, self._ac, index)
        {
            return Some(Self::new(&self._lib, self._vm_id, child));
        }
        None
    }

    /**

     * 返回一个表示可访问上下文对象的父级的对象。
     * */
    pub fn get_parent(&self) -> Option<AccessibleContext<'lib>> {
        if let Some(parent) = self
            ._lib
            .get_accessible_parent_from_context(self._vm_id, self._ac)
        {
            return Some(Self::new(&self._lib, self._vm_id, parent));
        }
        None
    }

    /**

     * 返回具有指定角色的AccessibleContext,该角色是给定对象的祖先。如果没有具有指定角色的祖先对象,则返回None。
     * `role` 角色枚举。
     * */
    pub fn get_parent_with_role(&self, role: &AccessibleRole) -> Option<AccessibleContext<'lib>> {
        if let Some(parent) = self
            ._lib
            .get_parent_with_role(self._vm_id, self._ac, role.to_str())
        {
            return Some(Self::new(&self._lib, self._vm_id, parent));
        }
        None
    }

    /**

     * 返回具有指定角色的AccessibleContext,该角色是给定对象的祖先。如果具有指定角色的对象不存在,则返回Java窗口的顶级对象。出现错误时返回None。
     * `role` 角色枚举。
     * */
    pub fn get_parent_with_role_else_root(
        &self,
        role: &AccessibleRole,
    ) -> Option<AccessibleContext<'lib>> {
        if let Some(parent_or_root) =
            self._lib
                .get_parent_with_role_else_root(self._vm_id, self._ac, role.to_str())
        {
            return Some(Self::new(&self._lib, self._vm_id, parent_or_root));
        }
        None
    }

    /**

     * 返回Java窗口中顶级对象的AccessibleContext。这与从from_hwnd获得的AccessibleContext相同。出现错误时返回None。
     * */
    pub fn get_top_level(&self) -> Option<AccessibleContext<'lib>> {
        if let Some(top) = self._lib.get_top_level_object(self._vm_id, self._ac) {
            return Some(Self::new(&self._lib, self._vm_id, top));
        }
        None
    }

    /**

     * 返回对象的当前ActiveDescendent的AccessibleContext。此方法假定ActiveDescendent是当前在容器对象中选择的组件。出现错误或没有选择时返回None。
     * */
    pub fn get_active_descendent(&self) -> Option<AccessibleContext<'lib>> {
        if let Some(descendent) = self._lib.get_active_descendent(self._vm_id, self._ac) {
            return Some(Self::new(&self._lib, self._vm_id, descendent));
        }
        None
    }

    /**

     * 返回当前对象在对象层次结构中的深度。对象层次结构最顶端的对象的对象深度为0。出现错误时返回-1。
     * */
    pub fn get_depth(&self) -> i32 {
        self._lib.get_object_depth(self._vm_id, self._ac)
    }

    /**

     * 获取应用程序正在使用的 Java Access Bridge 实例的版本信息。您可以使用此信息来确定您的 Java Access Bridge 版本的可用功能。
     * */
    pub fn get_version(&self) -> Option<AccessBridgeVersionInfo> {
        if let Some(version_info) = self._lib.get_version_info(self._vm_id) {
            return Some(AccessBridgeVersionInfo::from(&version_info));
        }
        None
    }

    /**

     * 获取对象名称。
     * */
    pub fn get_name(&self) -> Option<String> {
        let Some(ref info) = self._info else {
            return None;
        };
        Some(info.name.to_string_utf16())
    }

    /**

     * 获取对象描述。
     * */
    pub fn get_description(&self) -> Option<String> {
        let Some(ref info) = self._info else {
            return None;
        };
        Some(info.description.to_string_utf16())
    }

    /**

     * 获取对象角色。
     * */
    pub fn get_role(&self) -> AccessibleRole {
        let Some(ref info) = self._info else {
            return AccessibleRole::Unknown;
        };
        AccessibleRole::from_str(&info.role.to_string_utf16())
    }

    /**

     * 获取对象状态。
     * */
    pub fn get_states(&self) -> Option<String> {
        let Some(ref info) = self._info else {
            return None;
        };
        Some(info.states.to_string_utf16())
    }

    /**

     * 获取对象状态(英文描述)。
     * */
    pub fn get_states_en_us(&self) -> Option<String> {
        let Some(ref info) = self._info else {
            return None;
        };
        Some(info.states_en_US.to_string_utf16())
    }

    /**

     * 获取对象在父对象中的索引。
     * */
    pub fn get_index_in_parent(&self) -> i32 {
        let Some(ref info) = self._info else {
            return -1;
        };
        info.indexInParent
    }

    /**

     * 获取子对象数量。
     * */
    pub fn get_child_count(&self) -> i32 {
        let Some(ref info) = self._info else {
            return -1;
        };
        info.childrenCount
    }

    /**

     * 获取对象矩形边框(左边、顶部、宽度、高度)。
     * */
    pub fn get_bound_rectangle(&self) -> Option<(i32, i32, i32, i32)> {
        let Some(ref info) = self._info else {
            return None;
        };
        Some((info.x, info.y, info.width, info.height))
    }

    /**

     * 将插入符号设置为文本位置。返回是否成功。
     * `position` 文本位置。
     * */
    pub fn set_caret_position(&self, position: i32) -> bool {
        self._lib
            .set_caret_position(self._vm_id, self._ac, position)
    }

    /**

     * 获取文本插入符号的位置(左边、顶部、宽度和高度)。
     * `index` 字符索引。
     * */
    pub fn get_caret_location(&self, index: i32) -> Option<(i32, i32, i32, i32)> {
        if let Some(location) = self._lib.get_caret_location(self._vm_id, self._ac, index) {
            return Some((location.x, location.y, location.width, location.height));
        }
        None
    }

    /**

     * 返回组件可以执行的操作列表。
     * */
    pub fn get_actions(&self) -> Vec<String> {
        if let Some(actions) = self._lib.get_accessible_actions(self._vm_id, self._ac) {
            let mut names = vec![];
            for i in 0..actions.actionsCount {
                names.push(actions.actionInfo[i as usize].name.to_string_utf16())
            }
            return names;
        }
        return vec![];
    }

    /**

     * 请求组件执行可访问操作的列表。如果执行了所有操作,则返回TRUE。当第一个请求的操作失败时返回FALSE,在这种情况下,“failure”包含失败操作的索引。
     * `actions_to_do` 要执行的动作列表。
     * */
    pub fn do_actions(&self, actions_to_do: &[String]) -> (bool, i32) {
        let mut actions = unsafe { std::mem::zeroed::<AccessibleActions>() };
        actions.actionsCount = min(actions_to_do.len() as JInt, MAX_ACTION_INFO as JInt);
        for i in 0..actions.actionsCount {
            let mut name: [u16; SHORT_STRING_SIZE as usize] = [0; SHORT_STRING_SIZE as usize];
            for (i, x) in actions_to_do[i as usize].encode_utf16().enumerate() {
                name[i] = x;
            }
            actions.actionInfo[i as usize] = AccessibleActionInfo { name };
        }
        self._lib.do_accessible_actions(
            self._vm_id,
            self._ac,
            &AccessibleActionsToDo::from_actions(&actions),
        )
    }
    /**

     * 返回有关对象的关系对象的信息。
     * */
    pub fn get_relations(&self) -> Vec<AccessibleRelation> {
        if let Some(r) = self._lib.get_accessible_relation_set(self._vm_id, self._ac) {
            let mut relations = vec![];
            for i in 0..r.relationCount {
                let item = &r.relations[i as usize];
                let mut targets = vec![];
                for j in 0..item.targetCount {
                    targets.push(Self::new(&self._lib, self._vm_id, item.targets[j as usize]))
                }
                relations.push(AccessibleRelation {
                    key: item.key.to_string_utf16(),
                    targets,
                });
            }
            return relations;
        }
        vec![]
    }

    /**

     * 返回与组件关联的键绑定的列表。
     * */
    pub fn get_key_bindings(&self) -> Vec<AccessibleKeyBinding> {
        if let Some(key) = self._lib.get_accessible_key_bindings(self._vm_id, self._ac) {
            let mut keys = vec![];
            for i in 0..key.keyBindingsCount {
                keys.push(AccessibleKeyBinding::from(&key.keyBindingInfo[i as usize]));
            }
            return keys;
        }
        vec![]
    }

    /**

     * 返回与组件关联的图标列表。
     * 每一个图标包含描述、宽度和高度信息。
     * */
    pub fn get_icons(&self) -> Vec<(String, i32, i32)> {
        if let Some(icons) = self._lib.get_accessible_icons(self._vm_id, self._ac) {
            let mut ret = vec![];
            for i in 0..icons.iconsCount {
                let item = &icons.iconInfo[i as usize];
                ret.push((item.description.to_string_utf16(), item.width, item.height));
            }
            return ret;
        }
        vec![]
    }

    /**

     * 获取基于JAWS算法的组件的AccessibleName。返回是否成功。
     * Bug ID 4916682-实现JAWS AccessibleName策略
     * `len` 名称长度。
     * */
    pub fn get_virtual_name(&self, len: i32) -> Option<String> {
        let Some(name) = self
            ._lib
            .get_virtual_accessible_name(self._vm_id, self._ac, len)
        else {
            return None;
        };
        Some(name.to_string_utf16())
    }

    /**

     * 获取当前值。
     * `len` 存放值的字符串长度。
     * */
    pub fn get_current_value(&self, len: i32) -> Option<String> {
        let Some(v) =
            self._lib
                .get_current_accessible_value_from_context(self._vm_id, self._ac, len as i16)
        else {
            return None;
        };
        let val = v.to_string_utf16();
        if val.is_empty() {
            return None;
        }
        Some(val)
    }

    /**

     * 获取最大值。
     * `len` 存放值的字符串长度。
     * */
    pub fn get_maximum_value(&self, len: i32) -> Option<String> {
        let Some(v) =
            self._lib
                .get_maximum_accessible_value_from_context(self._vm_id, self._ac, len as i16)
        else {
            return None;
        };
        let val = v.to_string_utf16();
        if val.is_empty() {
            return None;
        }
        Some(val)
    }

    /**

     * 获取最小值。
     * `len` 存放值的字符串长度。
     * */
    pub fn get_minimum_value(&self, len: i32) -> Option<String> {
        let Some(v) =
            self._lib
                .get_minimum_accessible_value_from_context(self._vm_id, self._ac, len as i16)
        else {
            return None;
        };
        let val = v.to_string_utf16();
        if val.is_empty() {
            return None;
        }
        Some(val)
    }

    /**

     * 获取表格对象。
     * */
    pub fn get_table(&self) -> Option<AccessibleTable> {
        AccessibleTable::new(&self._lib, self._vm_id, self._ac)
    }

    /**

     * 获取超文本对象。
     * */
    pub fn get_hypertext(&self) -> Option<AccessibleHypertext> {
        let Some(info) = self._lib.get_accessible_hypertext(self._vm_id, self._ac) else {
            return None;
        };
        Some(AccessibleHypertext::new(
            self._lib,
            self._vm_id,
            self._ac,
            info,
        ))
    }

    /**

     * 遍历组件中的超链接。返回从超链接索引start_index开始的组件的超文本对象。出现错误时返回None。
     * `start_index` 开始索引。
     * */
    pub fn get_hypertext_ext(&self, start_index: i32) -> Option<AccessibleHypertext> {
        let Some(info) = self
            ._lib
            .get_accessible_hypertext_ext(self._vm_id, self._ac, start_index)
        else {
            return None;
        };
        Some(AccessibleHypertext::new(
            self._lib,
            self._vm_id,
            self._ac,
            info,
        ))
    }

    /**

     * 请求组件的焦点。返回是否成功。
     * */
    pub fn request_focus(&self) -> bool {
        self._lib.request_focus(self._vm_id, self._ac)
    }

    /**

     * 返回组件的可见子对象数。出现错误时返回-1。
     * */
    pub fn get_visible_child_count(&self) -> i32 {
        self._lib.get_visible_children_count(self._vm_id, self._ac)
    }

    /**

     * 获取可见子对象。
     * `start_index` 起始索引。
     * */
    pub fn get_visible_children(&self, start_index: i32) -> Option<Vec<Self>> {
        let Some(info) = self
            ._lib
            .get_visible_children(self._vm_id, self._ac, start_index)
        else {
            return None;
        };
        let mut v = vec![];
        for i in 0..info.returnedChildrenCount {
            v.push(Self::new(self._lib, self._vm_id, info.children[i as usize]));
        }
        Some(v)
    }

    /**

     * 把某个项目添加到选区。
     * `index` 索引。
     * */
    pub fn add_selection(&self, index: i32) {
        self._lib
            .add_accessible_selection_from_context(self._vm_id, self._ac, index)
    }

    /**

     * 把某个项目从选区中移除。
     * `index` 索引。
     * */
    pub fn remove_selection(&self, index: i32) {
        self._lib
            .remove_accessible_selection_from_context(self._vm_id, self._ac, index)
    }

    /**

     * 清除选区。
     * */
    pub fn clear_selection(&self) {
        self._lib
            .clear_accessible_selection_from_context(self._vm_id, self._ac)
    }

    /**

     * 选择所有。
     * */
    pub fn select_all(&self) {
        self._lib
            .select_all_accessible_selection_from_context(self._vm_id, self._ac)
    }

    /**

     * 获取选区中的对象数量。
     * */
    pub fn get_selection_count(&self) -> i32 {
        self._lib
            .get_accessible_selection_count_from_context(self._vm_id, self._ac)
    }

    /**

     * 获取选区中的对象。
     * `index` 索引。
     * */
    pub fn get_selection(&self, index: i32) -> Option<Self> {
        let Some(obj) =
            self._lib
                .get_accessible_selection_from_context(self._vm_id, self._ac, index)
        else {
            return None;
        };
        Some(Self::new(self._lib, self._vm_id, obj as AC))
    }

    /**

     * 判断子对象是否被选中。
     * `index` 子对象索引。
     * */
    pub fn is_child_selected(&self, index: i32) -> bool {
        self._lib
            .is_accessible_child_selected_from_context(self._vm_id, self._ac, index)
    }

    /**

     * 判断对象是否支持选择模式。
     * */
    pub fn is_supported_selection(&self) -> bool {
        let Some(ref info) = self._info else {
            return false;
        };
        info.accessibleSelection != 0
    }

    /**

     * 在两个索引之间选择文本。所选内容包括起始索引处的文本和结束索引处的文字。返回是否成功。
     * `start_index` 开始索引。
     * `end_index` 结束索引。
     * */
    pub fn select_text_range(&self, start_index: i32, end_index: i32) -> bool {
        self._lib
            .select_text_range(self._vm_id, self._ac, start_index, end_index)
    }

    /**

     * 获取两个索引之间的文本属性。属性列表包括起始索引处的文本和结束索引处的文字。
     * `start_index` 开始位置。
     * `end_index` 结束位置。
     * */
    pub fn get_text_attributes_in_range(
        &self,
        start_index: i32,
        end_index: i32,
    ) -> Option<(AccessibleTextAttributes, i16)> {
        let Some(info) =
            self._lib
                .get_text_attributes_in_range(self._vm_id, self._ac, start_index, end_index)
        else {
            return None;
        };
        Some((AccessibleTextAttributes::new(info.0), info.1))
    }

    /**

     * 获取文本选区信息(开始索引、结束索引和选中文字)。
     * */
    pub fn get_text_selection(&self) -> Option<(i32, i32, String)> {
        if self._info.is_none() || self._info.as_ref().unwrap().accessibleText == 0 {
            return None;
        }
        let Some(info) = self
            ._lib
            .get_accessible_text_selection_info(self._vm_id, self._ac)
        else {
            return None;
        };
        Some((
            info.selectionStartIndex,
            info.selectionEndIndex,
            info.selectedText.to_string_utf16(),
        ))
    }

    /**

     * 获取文本信息(字符数、插入点索引、坐标处索引)。
     * `x` X坐标。
     * `y` Y坐标。
     * */
    pub fn get_text_info(&self, x: i32, y: i32) -> Option<(i32, i32, i32)> {
        if self._info.is_none() || self._info.as_ref().unwrap().accessibleText == 0 {
            return None;
        }
        let Some(info) = self
            ._lib
            .get_accessible_text_info(self._vm_id, self._ac, x, y)
        else {
            return None;
        };
        Some((info.charCount, info.caretIndex, info.indexAtPoint))
    }

    /**

     * 获取文本属性。
     * `index` 索引。
     * */
    pub fn get_text_attributes(&self, index: i32) -> Option<(AccessibleTextAttributes, String)> {
        if self._info.is_none() || self._info.as_ref().unwrap().accessibleText == 0 {
            return None;
        }
        let (text, info) = self
            ._lib
            .get_accessible_text_attributes(self._vm_id, self._ac, index);
        unsafe {
            Some((
                AccessibleTextAttributes::new(info),
                CStr::from_ptr(text as *const c_char)
                    .to_string_lossy()
                    .to_string(),
            ))
        }
    }

    /**

     * 获取文本项目(字符、单词和句子)。
     * `index` 索引。
     * */
    pub fn get_text_items(&self, index: i32) -> Option<(u16, String, String)> {
        if self._info.is_none() || self._info.as_ref().unwrap().accessibleText == 0 {
            return None;
        }
        let Some(info) = self
            ._lib
            .get_accessible_text_items(self._vm_id, self._ac, index)
        else {
            return None;
        };
        let word = info.word.to_string_utf16();
        let sentence = info.sentence.to_string_utf16();
        Some((info.letter, word, sentence))
    }

    /**

     * 获取文本范围。
     * `start_index` 开始索引。
     * `end_index` 结束索引。
     * */
    pub fn get_text_range(&self, start_index: i32, end_index: i32) -> Option<String> {
        if self._info.is_none() || self._info.as_ref().unwrap().accessibleText == 0 {
            return None;
        }
        let Some(info) = self._lib.get_accessible_text_range(
            self._vm_id,
            self._ac,
            start_index,
            end_index,
            (end_index - start_index).abs() as i16,
        ) else {
            return None;
        };
        Some(info.to_string_utf16())
    }

    /**

     * 获取文本行边界。
     * `index` 索引。
     * */
    pub fn get_text_line_bounds(&self, index: i32) -> Option<(i32, i32)> {
        if self._info.is_none() || self._info.as_ref().unwrap().accessibleText == 0 {
            return None;
        }
        self._lib
            .get_accessible_text_line_bounds(self._vm_id, self._ac, index)
    }

    /**

     * 获取文本矩形范围(左边、顶部、宽度和高度)。
     * `index` 索引。
     * */
    pub fn get_text_rect(&self, index: i32) -> Option<(i32, i32, i32, i32)> {
        if self._info.is_none() || self._info.as_ref().unwrap().accessibleText == 0 {
            return None;
        }
        let Some(info) = self
            ._lib
            .get_accessible_text_rect(self._vm_id, self._ac, index)
        else {
            return None;
        };
        Some((info.x, info.y, info.width, info.height))
    }

    /**

     * 设置可编辑的文本内容。返回是否成功。
     * `text` 文字内容。
     * */
    pub fn set_text_contents(&self, text: &str) -> bool {
        let mut value: [u16; (MAX_STRING_SIZE - 1) as usize] = [0; (MAX_STRING_SIZE - 1) as usize];
        text.encode_utf16().enumerate().for_each(|(i, c)| {
            if i < (MAX_STRING_SIZE - 1) as usize {
                value[i] = c
            }
        });
        self._lib
            .set_text_contents(self._vm_id, self._ac, value.as_ptr())
    }
}

impl<'lib> PartialEq for AccessibleContext<'lib> {
    fn eq(&self, other: &Self) -> bool {
        self._lib.is_same_object(self._vm_id, self._ac, other._ac)
    }
}

impl<'lib> Eq for AccessibleContext<'lib> {}

impl<'lib> Drop for AccessibleContext<'lib> {
    fn drop(&mut self) {
        self._lib.release_java_object(self._vm_id, self._ac);
    }
}

impl<'lib> Debug for AccessibleContext<'lib> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        let mut info = String::new();
        if let Some(name) = self.get_name() {
            info += format!("name:{},", name).as_str();
        }
        if let Some(description) = self.get_description() {
            info += format!("description:{},", description).as_str();
        }
        let role = self.get_role();
        info += format!("role:{},", role.to_str()).as_str();
        if let Some(states) = self.get_states() {
            info += format!("states:{},", states).as_str();
        }
        write!(f, "AccessibleContext({})", info)
    }
}

unsafe impl<'lib> Send for AccessibleContext<'lib> {}

unsafe impl<'lib> Sync for AccessibleContext<'lib> {}