Tk Source Code

Artifact [c9daf08b]
Login

Artifact c9daf08b7da21fa18827659dc27fe2597afc9a6a7e21d6af2597cb4e98ab61e6:

Attachment "active-window.patch" to ticket [595d72d0] added by 0xjnml 2026-10-04 14:55:30. (unpublished)
--- a/unix/tkUnixAccessibility.c
+++ b/unix/tkUnixAccessibility.c
@@ -84,6 +84,53 @@
 	mainWindow = NULL;
     }
 }
+
+/*
+ * The toplevel that contains the focus reports ATK_STATE_ACTIVE, and changes
+ * of it are announced. Screen readers find the active window by that state
+ * (Orca's flat review reads nothing without it). The update runs at idle,
+ * after a focus transfer has settled; activeToplevel is cleared when its
+ * window is destroyed.
+ */
+static Tk_Window activeToplevel = NULL;
+static int activeUpdatePending = 0;
+Tk_Window GetToplevelOfWidget(Tk_Window tkwin);
+AtkObject *GetAtkObjectForTkWindow(Tk_Window tkwin);
+
+static void UpdateActiveToplevel(TCL_UNUSED(void *))
+{
+    activeUpdatePending = 0;
+    if (!mainWindow) {
+	return;
+    }
+    TkWindow *focusPtr = TkGetFocusWin((TkWindow *)mainWindow);
+    Tk_Window top = focusPtr ? GetToplevelOfWidget((Tk_Window)focusPtr) : NULL;
+    if (top == activeToplevel) {
+	return;
+    }
+    Tk_Window old = activeToplevel;
+    activeToplevel = top;
+    if (old) {
+	AtkObject *o = GetAtkObjectForTkWindow(old);
+	if (o) {
+	    atk_object_notify_state_change(o, ATK_STATE_ACTIVE, FALSE);
+	}
+    }
+    if (top) {
+	AtkObject *n = GetAtkObjectForTkWindow(top);
+	if (n) {
+	    atk_object_notify_state_change(n, ATK_STATE_ACTIVE, TRUE);
+	}
+    }
+}
+
+static void ScheduleActiveToplevelUpdate(void)
+{
+    if (!activeUpdatePending) {
+	activeUpdatePending = 1;
+	Tcl_DoWhenIdle(UpdateActiveToplevel, NULL);
+    }
+}
 static GList *toplevel_accessible_objects = NULL;
 static GHashTable *tk_to_atk_map = NULL;
 extern Tcl_HashTable *TkAccessibilityObject;
@@ -675,6 +722,11 @@
 	}
     }
 
+    /* The toplevel that contains the focus is the active window. */
+    if (acc->tkwin && acc->tkwin == activeToplevel) {
+	atk_state_set_add_state(state_set, ATK_STATE_ACTIVE);
+    }
+
     /* Add FOCUSED if widget has focus. */
     if (acc->is_focused) {
 	atk_state_set_add_state(state_set, ATK_STATE_FOCUSED);
@@ -1666,6 +1718,10 @@
     TkAtkAccessible *acc = (TkAtkAccessible *)clientData;
     if (!acc) return;
 
+    if (acc->tkwin && acc->tkwin == activeToplevel) {
+	activeToplevel = NULL;
+    }
+
     GObject *obj =  (GObject*)acc;
 
     tk_atk_accessible_finalize(obj);
@@ -1724,6 +1780,8 @@
     AtkObject *obj = ATK_OBJECT(acc);
     AtkRole role = GetAtkRoleForWidget(acc->tkwin);
 
+    ScheduleActiveToplevelUpdate();
+
     /* Update this widget's focus state. */
     acc->is_focused = focused;
     atk_object_notify_state_change(obj, ATK_STATE_FOCUSED, focused);