Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gtk: Implementable implementation for AccessibleText #1789

Merged
merged 4 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ libloading = { version = "0.8.5", optional = true }
im-rc = { version = "15", optional = true }
async-channel = { version = "2.3", optional = true }
tokio = { version = "1", features = ["full"], optional = true }
reqwest = { version = "0.12", features = ["json", "rustls-tls"], default-features = false, optional = true }
reqwest = { version = "0.12", features = [
"json",
"rustls-tls",
], default-features = false, optional = true }
serde = { version = "1.0", features = ["derive"], optional = true }

gtk.workspace = true
Expand All @@ -36,12 +39,18 @@ v4_6 = ["gtk/v4_6"]
v4_10 = ["gtk/v4_10"]
v4_12 = ["gtk/v4_12"]
v4_14 = ["gtk/v4_14"]
v4_16 = ["gtk/v4_16"]

[[bin]]
name = "about_dialog"
path = "about_dialog/main.rs"
required-features = ["v4_6"]

[[bin]]
name = "accessible_text"
path = "accessible_text/main.rs"
required-features = ["v4_16"]

[[bin]]
name = "basics"
path = "basics/main.rs"
Expand Down
9 changes: 8 additions & 1 deletion examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@ cargo run --bin basics
```

- [Basic example](./basics/)
- [About Dialog](./about_dialog/)
- [Implementing the Accessible Text Interface](./accessible_text/)
- [Using the Builder pattern](./builder_pattern/)
- [Clipboard](./clipboard/)
- [Clock example](./clock/)
- [Column View Datagrid Example](./column_view_datagrid/)
- [Composite Dialog](./composite_dialog/)
- [Composite Template](./composite_template/)
- [Confetti Snapshot Animation](./confetti_snapshot_animation/)
- [Content Provider](./content_provider/)
- [CSS](./css/)
- [Custom Application](./custom_application/)
Expand All @@ -36,10 +40,13 @@ cargo run --bin basics
- [FemtoVG Area](./femtovg_area/)
- [Fill and Stroke](./fill_and_stroke/)
- [FlowBox](./flow_box/)
- [GIF Paintable](./gif_paintable/)
- [Glium GL-Area](./glium_gl_area/)
- [Grid Packing](./grid_packing)
- [GtkBuilder example](./gtk_builder/)
- [GtkBuilder](./gtk_builder/)
- [ListBox and ListModel](./list_box_model/)
- [ListView: Applications Launcher](./list_view_apps_launcher/)
- [Menubar](./menubar/)
- [Rotation Bin](./rotation_bin/)
- [Scale](./scale/)
- [Scale Bin](./scale_bin/)
Expand Down
7 changes: 7 additions & 0 deletions examples/accessible_text/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Implementing the Accessible Text Interface

This example creates a custom text editing widget that implements the Accessible Text Interface. This interface is used for providing screen reader accessibility to custom widgets.

To test this implementation, enable the screen reader (Orca is enabled via Super+Alt+S) and edit / navigate the text.

The widget mostly just delegates its implementation to the parent `TextView`.
24 changes: 24 additions & 0 deletions examples/accessible_text/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
mod text_view;

use gtk::{glib, prelude::*};
use text_view::AccessibleTextView;

fn main() -> glib::ExitCode {
let application = gtk::Application::builder()
.application_id("com.github.gtk-rs.examples.accessible_text")
.build();
application.connect_activate(build_ui);
application.run()
}

fn build_ui(application: &gtk::Application) {
let window = gtk::ApplicationWindow::new(application);

window.set_title(Some("Accessible Text Example"));
window.set_default_size(260, 140);

let text_view = glib::Object::new::<AccessibleTextView>();
window.set_child(Some(&text_view));

window.present();
}
102 changes: 102 additions & 0 deletions examples/accessible_text/text_view.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
use gtk::glib;
use gtk::subclass::prelude::*;

mod imp {
use gtk::{graphene, ACCESSIBLE_ATTRIBUTE_OVERLINE, ACCESSIBLE_ATTRIBUTE_OVERLINE_SINGLE};

use super::*;

#[derive(Default)]
pub struct AccessibleTextView {}

#[glib::object_subclass]
impl ObjectSubclass for AccessibleTextView {
const NAME: &'static str = "AccessibleTextView";
type Type = super::AccessibleTextView;
type ParentType = gtk::TextView;
type Interfaces = (gtk::AccessibleText,);
}

impl ObjectImpl for AccessibleTextView {}
impl WidgetImpl for AccessibleTextView {}
impl AccessibleTextImpl for AccessibleTextView {
fn attributes(
&self,
offset: u32,
) -> Vec<(gtk::AccessibleTextRange, glib::GString, glib::GString)> {
let attributes = self.parent_attributes(offset);
println!("attributes({offset}) -> {attributes:?}");
attributes
}

fn caret_position(&self) -> u32 {
let pos = self.parent_caret_position();
println!("caret_position() -> {pos}");
pos
}

fn contents(&self, start: u32, end: u32) -> Option<glib::Bytes> {
let content = self.parent_contents(start, end);
println!(
"contents({start}, {end}) -> {:?}",
content
.as_ref()
.map(|c| std::str::from_utf8(c.as_ref()).unwrap())
);
content
}

fn contents_at(
&self,
offset: u32,
granularity: gtk::AccessibleTextGranularity,
) -> Option<(u32, u32, glib::Bytes)> {
let contents = self.parent_contents_at(offset, granularity);
println!(
"contents_at offset({offset}, {granularity:?}) -> {:?}",
contents
.as_ref()
.map(|(s, e, c)| (s, e, std::str::from_utf8(c.as_ref()).unwrap()))
);
contents
}

fn default_attributes(&self) -> Vec<(glib::GString, glib::GString)> {
let mut attrs = self.parent_default_attributes();

// Attributes can be added and removed
attrs.push((
ACCESSIBLE_ATTRIBUTE_OVERLINE.to_owned(),
ACCESSIBLE_ATTRIBUTE_OVERLINE_SINGLE.to_owned(),
));
println!("default_attributes() -> {attrs:?}");
attrs
}

fn selection(&self) -> Vec<gtk::AccessibleTextRange> {
let selection = self.parent_selection();
println!("selection() -> {selection:?}");
selection
}

fn extents(&self, start: u32, end: u32) -> Option<graphene::Rect> {
let extents = self.parent_extents(start, end);
println!("extents({start}, {end}) -> {extents:?}");
extents
}

fn offset(&self, point: &graphene::Point) -> Option<u32> {
let offset = self.parent_offset(point);
println!("offset({:?}) -> {offset:?}", point);
offset
}
}

impl TextViewImpl for AccessibleTextView {}
}

glib::wrapper! {
pub struct AccessibleTextView(ObjectSubclass<imp::AccessibleTextView>)
@extends gtk::Widget, gtk::TextView,
@implements gtk::Accessible, gtk::AccessibleText, gtk::Buildable, gtk::ConstraintTarget, gtk::Scrollable;
}
28 changes: 22 additions & 6 deletions gtk4/src/accessible_text_range.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,33 @@
// Take a look at the license at the top of the repository in the LICENSE file.

#[derive(Copy, Clone)]
#[doc(alias = "GtkAccessibleTextRange")]
#[repr(transparent)]
pub struct AccessibleTextRange(crate::ffi::GtkAccessibleTextRange);
use crate::ffi;
use glib::translate::*;

glib::wrapper! {
#[doc(alias = "GtkAccessibleTextRange")]
pub struct AccessibleTextRange(BoxedInline<ffi::GtkAccessibleTextRange>);
}
felinira marked this conversation as resolved.
Show resolved Hide resolved

impl AccessibleTextRange {
pub fn new(start: usize, length: usize) -> Self {
skip_assert_initialized!();
unsafe { AccessibleTextRange::unsafe_from(ffi::GtkAccessibleTextRange { start, length }) }
}

pub fn start(&self) -> usize {
self.0.start
self.inner.start
felinira marked this conversation as resolved.
Show resolved Hide resolved
}

pub fn set_start(&mut self, start: usize) {
self.inner.start = start;
}

pub fn length(&self) -> usize {
self.0.length
self.inner.length
}

pub fn set_length(&mut self, length: usize) {
self.inner.length = length
}
}

Expand Down
3 changes: 3 additions & 0 deletions gtk4/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,9 @@ mod tree_view;
mod tree_view_column;
mod widget;

#[cfg(feature = "v4_14")]
#[cfg_attr(docsrs, doc(cfg(feature = "v4_14")))]
pub use accessible_text_range::AccessibleTextRange;
pub use bitset_iter::BitsetIter;
pub use border::Border;
pub use builder_cscope::BuilderCScope;
Expand Down
Loading
Loading