jabr 0.1.4

JavaAccessBridge wrapper for Rust
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
810
811
/*
 * 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.
 * Modified by igaisin 2025
 */

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 windows::Win32::Foundation::HWND;
use crate::utils::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
    }

    /**

     * Возвращает HWND верхнего уровня окна AccessibleContext.
     */
    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
    }

    /**

     * Запрос объекта AccessibleContext окна или объекта под указателем мыши.
     * `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
    }

    /**

     * Возвращает AccessibleContext верхнего уровня объекта Java окна. Это то же самое, что и AccessibleContext, полученный из from_hwnd. В случае ошибки возвращает 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
    }

    /**

     * Возвращает AccessibleContext текущего ActiveDescendent объекта. Этот метод предполагает, что 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. Возвращает успех операции.
     * 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> {}