|
| 1 | +use gtk::glib; |
| 2 | +use gtk::subclass::prelude::*; |
| 3 | + |
| 4 | +mod imp { |
| 5 | + use gtk::{graphene, ACCESSIBLE_ATTRIBUTE_OVERLINE, ACCESSIBLE_ATTRIBUTE_OVERLINE_SINGLE}; |
| 6 | + |
| 7 | + use super::*; |
| 8 | + |
| 9 | + #[derive(Default)] |
| 10 | + pub struct AccessibleTextView {} |
| 11 | + |
| 12 | + #[glib::object_subclass] |
| 13 | + impl ObjectSubclass for AccessibleTextView { |
| 14 | + const NAME: &'static str = "AccessibleTextView"; |
| 15 | + type Type = super::AccessibleTextView; |
| 16 | + type ParentType = gtk::TextView; |
| 17 | + type Interfaces = (gtk::AccessibleText,); |
| 18 | + } |
| 19 | + |
| 20 | + impl ObjectImpl for AccessibleTextView {} |
| 21 | + impl WidgetImpl for AccessibleTextView {} |
| 22 | + impl AccessibleTextImpl for AccessibleTextView { |
| 23 | + fn attributes( |
| 24 | + &self, |
| 25 | + offset: u32, |
| 26 | + ) -> Vec<(gtk::AccessibleTextRange, glib::GString, glib::GString)> { |
| 27 | + let attributes = self.parent_attributes(offset); |
| 28 | + println!("attributes({offset}) -> {attributes:?}"); |
| 29 | + attributes |
| 30 | + } |
| 31 | + |
| 32 | + fn caret_position(&self) -> u32 { |
| 33 | + let pos = self.parent_caret_position(); |
| 34 | + println!("caret_position() -> {pos}"); |
| 35 | + pos |
| 36 | + } |
| 37 | + |
| 38 | + fn contents(&self, start: u32, end: u32) -> Option<glib::Bytes> { |
| 39 | + let content = self.parent_contents(start, end); |
| 40 | + println!( |
| 41 | + "contents({start}, {end}) -> {:?}", |
| 42 | + content |
| 43 | + .as_ref() |
| 44 | + .map(|c| std::str::from_utf8(c.as_ref()).unwrap()) |
| 45 | + ); |
| 46 | + content |
| 47 | + } |
| 48 | + |
| 49 | + fn contents_at( |
| 50 | + &self, |
| 51 | + offset: u32, |
| 52 | + granularity: gtk::AccessibleTextGranularity, |
| 53 | + ) -> Option<(u32, u32, glib::Bytes)> { |
| 54 | + let contents = self.parent_contents_at(offset, granularity); |
| 55 | + println!( |
| 56 | + "contents_at offset({offset}, {granularity:?}) -> {:?}", |
| 57 | + contents |
| 58 | + .as_ref() |
| 59 | + .map(|(s, e, c)| (s, e, std::str::from_utf8(c.as_ref()).unwrap())) |
| 60 | + ); |
| 61 | + contents |
| 62 | + } |
| 63 | + |
| 64 | + fn default_attributes(&self) -> Vec<(glib::GString, glib::GString)> { |
| 65 | + let mut attrs = self.parent_default_attributes(); |
| 66 | + |
| 67 | + // Attributes can be added and removed |
| 68 | + attrs.push(( |
| 69 | + ACCESSIBLE_ATTRIBUTE_OVERLINE.to_owned(), |
| 70 | + ACCESSIBLE_ATTRIBUTE_OVERLINE_SINGLE.to_owned(), |
| 71 | + )); |
| 72 | + println!("default_attributes() -> {attrs:?}"); |
| 73 | + attrs |
| 74 | + } |
| 75 | + |
| 76 | + fn selection(&self) -> Vec<gtk::AccessibleTextRange> { |
| 77 | + let selection = self.parent_selection(); |
| 78 | + println!("selection() -> {selection:?}"); |
| 79 | + selection |
| 80 | + } |
| 81 | + |
| 82 | + fn extents(&self, start: u32, end: u32) -> Option<graphene::Rect> { |
| 83 | + let extents = self.parent_extents(start, end); |
| 84 | + println!("extents({start}, {end}) -> {extents:?}"); |
| 85 | + extents |
| 86 | + } |
| 87 | + |
| 88 | + fn offset(&self, point: &graphene::Point) -> Option<u32> { |
| 89 | + let offset = self.parent_offset(point); |
| 90 | + println!("offset({:?}) -> {offset:?}", point); |
| 91 | + offset |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + impl TextViewImpl for AccessibleTextView {} |
| 96 | +} |
| 97 | + |
| 98 | +glib::wrapper! { |
| 99 | + pub struct AccessibleTextView(ObjectSubclass<imp::AccessibleTextView>) |
| 100 | + @extends gtk::Widget, gtk::TextView, |
| 101 | + @implements gtk::Accessible, gtk::AccessibleText, gtk::Buildable, gtk::ConstraintTarget, gtk::Scrollable; |
| 102 | +} |
0 commit comments