use std::cell::Cell;
use gtk::glib;
use gtk::glib::translate::ToGlibPtr;
use gtk::prelude::*;
const AUTO: &str = "prism-a11y-auto-name";
const LABEL: &str = "prism-a11y-label";
const HIDDEN: &str = "prism-a11y-hidden";
const PROPERTY_LABEL: i32 = 4;
const RELATION_LABELLED_BY: i32 = 10;
unsafe extern "C" {
fn gtk_test_accessible_has_property(accessible: *mut std::ffi::c_void, property: i32) -> i32;
fn gtk_test_accessible_has_relation(accessible: *mut std::ffi::c_void, relation: i32) -> i32;
}
thread_local! {
static SCHEDULED: Cell<bool> = const { Cell::new(false) };
}
fn needed() -> bool {
gtk::major_version() == 4 && gtk::minor_version() < 12
}
pub fn schedule() {
if !needed() || SCHEDULED.with(|s| s.replace(true)) {
return;
}
glib::idle_add_local_full(glib::Priority::LOW, || {
SCHEDULED.with(|s| s.set(false));
for window in gtk::Window::list_toplevels() {
sweep(&window);
}
glib::ControlFlow::Break
});
}
pub fn set_label(w: >k::Widget, label: &str) {
unsafe {
w.set_data(LABEL, label.to_owned());
let _ = w.steal_data::<String>(AUTO);
}
}
pub fn take_label(w: >k::Widget) -> bool {
unsafe { w.steal_data::<String>(LABEL).is_some() }
}
pub fn set_hidden(w: >k::Widget, hidden: bool) {
unsafe {
if hidden {
w.set_data(HIDDEN, true);
} else {
let _ = w.steal_data::<bool>(HIDDEN);
}
}
}
pub fn has_label(w: >k::Widget) -> bool {
label_of(w).is_some()
}
fn label_of(w: >k::Widget) -> Option<String> {
unsafe { w.data::<String>(LABEL).map(|p| p.as_ref().clone()) }
}
fn hidden(w: >k::Widget) -> bool {
unsafe { w.data::<bool>(HIDDEN).is_some() }
}
fn auto_name(o: &impl IsA<glib::Object>) -> Option<String> {
unsafe { o.data::<String>(AUTO).map(|p| p.as_ref().clone()) }
}
fn named_from_content(role: gtk::AccessibleRole) -> bool {
use gtk::AccessibleRole as R;
matches!(
role,
R::Button
| R::Cell
| R::Checkbox
| R::ColumnHeader
| R::GridCell
| R::Heading
| R::Label
| R::Link
| R::MenuItem
| R::MenuItemCheckbox
| R::MenuItemRadio
| R::Option
| R::Radio
| R::Row
| R::RowHeader
| R::Switch
| R::Tab
| R::Tooltip
| R::TreeItem
| R::Widget
)
}
fn structural(role: gtk::AccessibleRole) -> bool {
use gtk::AccessibleRole as R;
matches!(
role,
R::Form
| R::Group
| R::Generic
| R::Landmark
| R::ListItem
| R::Region
| R::Search
| R::Separator
)
}
fn role_of(acc: &impl IsA<glib::Object>) -> gtk::AccessibleRole {
acc.property("accessible-role")
}
fn shown(w: >k::Widget) -> bool {
w.is_visible() && w.is_child_visible() && !hidden(w)
}
fn content_name(w: >k::Widget) -> String {
let mut parts = Vec::new();
let mut child = w.first_child();
while let Some(c) = child {
collect(&c, &mut parts);
child = c.next_sibling();
}
parts.join(" ")
}
fn collect(w: >k::Widget, parts: &mut Vec<String>) {
if !shown(w) {
return;
}
let text = if let Some(label) = label_of(w) {
label
} else if let Some(l) = w.downcast_ref::<gtk::Label>() {
l.text().to_string()
} else if let Some(e) = w.dynamic_cast_ref::<gtk::Editable>() {
e.text().to_string()
} else if let Some(b) = w.downcast_ref::<gtk::Button>()
&& let Some(label) = b.label()
{
label.to_string()
} else {
let name = content_name(w);
if !name.is_empty() {
parts.push(name);
}
return;
};
let text = text.trim();
if !text.is_empty() {
parts.push(text.to_owned());
}
}
fn sweep(w: &impl IsA<gtk::Widget>) {
let w = w.as_ref();
let role = role_of(w);
let name = match w.downcast_ref::<gtk::Label>() {
Some(l) => l.text().to_string(),
None if named_from_content(role) => content_name(w),
None => String::new(),
};
fix(w, role, name);
if let Some(stack) = w.downcast_ref::<gtk::Stack>() {
let pages = stack.pages();
for i in 0..pages.n_items() {
if let Some(page) = pages.item(i).and_downcast::<gtk::StackPage>() {
fix(&page, role_of(&page), String::new());
}
}
}
let mut child = w.first_child();
while let Some(c) = child {
sweep(&c);
child = c.next_sibling();
}
}
fn fix(acc: &impl IsA<glib::Object>, role: gtk::AccessibleRole, name: String) {
let previous = auto_name(acc);
if previous.is_none() {
let ptr: *mut glib::gobject_ffi::GObject = acc.as_ref().to_glib_none().0;
let labelled = unsafe {
gtk_test_accessible_has_property(ptr.cast(), PROPERTY_LABEL) != 0
|| gtk_test_accessible_has_relation(ptr.cast(), RELATION_LABELLED_BY) != 0
};
let leaks = gtk::minor_version() < 10 && !structural(role);
if labelled || (name.is_empty() && !leaks) {
return;
}
} else if previous.as_deref() == Some(name.as_str()) {
return;
}
crate::imp::update_string(acc, PROPERTY_LABEL, &name);
unsafe { acc.set_data(AUTO, name) };
}