-
Notifications
You must be signed in to change notification settings - Fork 95
Simple headless example for Unix #634
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
Open
C-Loftus
wants to merge
4
commits into
AccessKit:main
Choose a base branch
from
C-Loftus:simpleHeadless
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| /// This is a simple example for creating a headless | ||
| /// application using accesskit_unix. It will not create any GUI | ||
| /// windows on the system. Rather it just creates the necessary | ||
| /// accessibility tree and updates it in a loop with a live announcement. | ||
| /// This will be spoken by Orca and works on both x11 and Wayland. | ||
| /// | ||
| /// To run this example: | ||
| /// cargo run -p accesskit_unix --example simple_headless | ||
| use accesskit::{ | ||
| ActionHandler, ActionRequest, ActivationHandler, DeactivationHandler, Live, Node, NodeId, Role, | ||
| Tree, TreeUpdate, | ||
| }; | ||
| use accesskit_unix::Adapter; | ||
|
|
||
| use std::error::Error; | ||
| use std::thread; | ||
| use std::time::Duration; | ||
|
|
||
| const ROOT_ID: NodeId = NodeId(0); | ||
| const ANNOUNCE_ID: NodeId = NodeId(1); | ||
|
|
||
| // These three structs are defined in order | ||
| // to fulfill the requirements for creating an | ||
| // adapter and initializing accesskit | ||
| struct MyActivationHandler; | ||
| struct MyActionHandler; | ||
| struct MyDeactivationHandler; | ||
|
|
||
| impl ActivationHandler for MyActivationHandler { | ||
| fn request_initial_tree(&mut self) -> Option<TreeUpdate> { | ||
| let mut root = Node::new(Role::Window); | ||
| root.set_children(vec![ANNOUNCE_ID]); | ||
| // The label on the window is equivalent to the frame | ||
| // title that Orca reads | ||
| root.set_label("Window for Headless Example"); | ||
|
|
||
| let mut label = Node::new(Role::Label); | ||
| label.set_value("Application started"); | ||
| label.set_live(Live::Assertive); | ||
|
|
||
| let tree = Tree::new(ROOT_ID); | ||
| Some(TreeUpdate { | ||
| // A node with id ANNOUNCE_ID must be in the nodes otherwise you will get an error | ||
| // This is since we are later updating it with a live announcement | ||
| nodes: vec![(ROOT_ID, root), (ANNOUNCE_ID, label)], | ||
| tree: Some(tree), | ||
| focus: ROOT_ID, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // In a full application you would implement the behavior of the actions | ||
| // within the GUI. We don't need to do anything here since we run | ||
| // the tree update in a loop. | ||
| impl ActionHandler for MyActionHandler { | ||
| fn do_action(&mut self, request: ActionRequest) { | ||
| println!("Got Action: {:?}", request); | ||
| } | ||
| } | ||
|
|
||
| // The deactivation handler runs a cleanup function before accessibility is deactivated | ||
| // In our case this can be skipped | ||
| impl DeactivationHandler for MyDeactivationHandler { | ||
| fn deactivate_accessibility(&mut self) { | ||
| println!("Accessibility deactivated"); | ||
| } | ||
| } | ||
|
|
||
| fn main() -> Result<(), Box<dyn Error>> { | ||
| println!("This is a simple headless example of accesskit_unix."); | ||
| println!("This will not show any GUI windows on the system, however it will create a live announcement in a loop."); | ||
| println!("Orca should speak these announcements."); | ||
| println!("Enable Orca with [Super]+[Alt]+[S]."); | ||
|
|
||
| // Create adapter and activate accessibility | ||
| let mut adapter = Adapter::new(MyActivationHandler, MyActionHandler, MyDeactivationHandler); | ||
| // At the start of the application we give the window focus; | ||
| // this is not needed for Orca to speak an announcement and | ||
| // could be skipped if you don't want to change the focus | ||
| adapter.update_window_focus_state(true); | ||
|
|
||
| adapter.update_if_active(|| MyActivationHandler.request_initial_tree().unwrap()); | ||
|
|
||
| let mut counter = 0; | ||
| loop { | ||
| counter += 1; | ||
| let message = format!("Announcement number {}", counter); | ||
| // Print to stdout for clarity and debugging | ||
| println!("Creating tree update with announcement: '{message}'"); | ||
|
|
||
| let mut label = Node::new(Role::Label); | ||
| label.set_value(message); | ||
| label.set_live(Live::Polite); | ||
|
|
||
| // when we update the tree with an announcement | ||
| // it can be detected by an assistive technology client | ||
| // like Orca and presented to the user | ||
| adapter.update_if_active(|| TreeUpdate { | ||
| nodes: vec![(ANNOUNCE_ID, label)], | ||
| tree: None, | ||
| focus: ROOT_ID, | ||
| }); | ||
|
|
||
| // sleep for 1 second so as to not spam the user | ||
| thread::sleep(Duration::from_secs(1)); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.