Skip to content

Commit a86e68c

Browse files
committed
Address review comments
1 parent 909c93c commit a86e68c

File tree

6 files changed

+59
-70
lines changed

6 files changed

+59
-70
lines changed

bindings/python/examples/pygame/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ This directory contains a cross-platform application that demonstrates how to in
44

55
## Prerequisites
66

7-
- Python 3.7 or higher
7+
- Python 3.9 or higher
88
- A virtual environment: `python -m venv .venv` (activating it will vary based on your platform)
99
- `pip install -r requirements.txt`
1010

bindings/python/src/common.rs

Lines changed: 47 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// the LICENSE-APACHE file) or the MIT license (found in
44
// the LICENSE-MIT file), at your option.
55

6+
use crate::{Point, Rect};
67
use pyo3::{prelude::*, types::PyList};
78

89
#[pyclass(module = "accesskit")]
@@ -257,7 +258,7 @@ macro_rules! simple_getter {
257258
};
258259
}
259260

260-
macro_rules! convertion_getter {
261+
macro_rules! converting_getter {
261262
($struct_name:ident, $getter:ident, $type:ty) => {
262263
#[pymethods]
263264
impl $struct_name {
@@ -292,7 +293,7 @@ macro_rules! simple_setter {
292293
};
293294
}
294295

295-
macro_rules! convertion_setter {
296+
macro_rules! converting_setter {
296297
($setter:ident, $setter_param:ty) => {
297298
#[pymethods]
298299
impl NodeBuilder {
@@ -379,7 +380,7 @@ macro_rules! node_id_vec_property_methods {
379380
macro_rules! node_id_property_methods {
380381
($(($getter:ident, $setter:ident, $clearer:ident)),+) => {
381382
$(property_methods! {
382-
($getter, option_getter, Option<NodeId>, $setter, convertion_setter, NodeId, $clearer)
383+
($getter, option_getter, Option<NodeId>, $setter, converting_setter, NodeId, $clearer)
383384
})*
384385
}
385386
}
@@ -411,15 +412,15 @@ macro_rules! color_property_methods {
411412
macro_rules! text_decoration_property_methods {
412413
($(($getter:ident, $setter:ident, $clearer:ident)),+) => {
413414
$(property_methods! {
414-
($getter, option_getter, Option<accesskit::TextDecoration>, $setter, convertion_setter, accesskit::TextDecoration, $clearer)
415+
($getter, option_getter, Option<accesskit::TextDecoration>, $setter, converting_setter, accesskit::TextDecoration, $clearer)
415416
})*
416417
}
417418
}
418419

419420
macro_rules! length_slice_property_methods {
420421
($(($getter:ident, $setter:ident, $clearer:ident)),+) => {
421422
$(property_methods! {
422-
($getter, convertion_getter, Vec<u8>, $setter, simple_setter, Vec<u8>, $clearer)
423+
($getter, converting_getter, Vec<u8>, $setter, simple_setter, Vec<u8>, $clearer)
423424
})*
424425
}
425426
}
@@ -588,7 +589,7 @@ unique_enum_property_methods! {
588589

589590
property_methods! {
590591
(transform, option_getter, Option<crate::Affine>, set_transform, simple_setter, crate::Affine, clear_transform),
591-
(bounds, option_getter, Option<crate::Rect>, set_bounds, convertion_setter, crate::Rect, clear_bounds),
592+
(bounds, option_getter, Option<crate::Rect>, set_bounds, converting_setter, crate::Rect, clear_bounds),
592593
(text_selection, option_getter, Option<TextSelection>, set_text_selection, simple_setter, TextSelection, clear_text_selection)
593594
}
594595

@@ -602,7 +603,7 @@ pub struct Tree {
602603
pub root: NodeId,
603604
pub app_name: Option<String>,
604605
pub toolkit_name: Option<String>,
605-
toolkit_version: Option<String>,
606+
pub toolkit_version: Option<String>,
606607
}
607608

608609
#[pymethods]
@@ -674,68 +675,55 @@ impl From<TreeUpdate> for accesskit::TreeUpdate {
674675
}
675676
}
676677

677-
#[pyclass(module = "accesskit")]
678-
pub struct ActionData(accesskit::ActionData);
679-
680-
#[pymethods]
681-
impl ActionData {
682-
#[staticmethod]
683-
pub fn custom_action(action: i32) -> Self {
684-
accesskit::ActionData::CustomAction(action).into()
685-
}
686-
687-
#[staticmethod]
688-
pub fn value(value: &str) -> Self {
689-
accesskit::ActionData::Value(value.into()).into()
690-
}
691-
692-
#[staticmethod]
693-
pub fn numeric_value(value: f64) -> Self {
694-
accesskit::ActionData::NumericValue(value).into()
695-
}
696-
697-
#[staticmethod]
698-
pub fn scroll_target_rect(rect: crate::Rect) -> Self {
699-
accesskit::ActionData::ScrollTargetRect(rect.into()).into()
700-
}
701-
702-
#[staticmethod]
703-
pub fn scroll_to_point(point: crate::Point) -> Self {
704-
accesskit::ActionData::ScrollToPoint(point.into()).into()
705-
}
706-
707-
#[staticmethod]
708-
pub fn set_scroll_offset(offset: crate::Point) -> Self {
709-
accesskit::ActionData::SetScrollOffset(offset.into()).into()
710-
}
711-
712-
#[staticmethod]
713-
pub fn set_text_selection(selection: TextSelection) -> Self {
714-
accesskit::ActionData::SetTextSelection(selection.into()).into()
715-
}
716-
}
717-
718-
impl From<accesskit::ActionData> for ActionData {
719-
fn from(data: accesskit::ActionData) -> Self {
720-
Self(data)
721-
}
722-
}
723-
724-
#[pyclass(get_all, set_all, module = "accesskit")]
678+
#[derive(Clone)]
679+
#[pyclass(module = "accesskit", rename_all = "SCREAMING_SNAKE_CASE")]
680+
pub enum ActionDataKind {
681+
CustomAction,
682+
Value,
683+
NumericValue,
684+
ScrollTargetRect,
685+
ScrollToPoint,
686+
SetScrollOffset,
687+
SetTextSelection,
688+
}
689+
690+
#[pyclass(get_all, module = "accesskit")]
725691
pub struct ActionRequest {
726692
pub action: accesskit::Action,
727693
pub target: NodeId,
728-
pub data: Option<Py<ActionData>>,
694+
pub data: Option<(ActionDataKind, Py<PyAny>)>,
729695
}
730696

731697
impl From<accesskit::ActionRequest> for ActionRequest {
732698
fn from(request: accesskit::ActionRequest) -> Self {
733699
Python::with_gil(|py| Self {
734700
action: request.action,
735701
target: request.target.into(),
736-
data: request
737-
.data
738-
.map(|data| Py::new(py, ActionData::from(data)).unwrap()),
702+
data: request.data.map(|data| match data {
703+
accesskit::ActionData::CustomAction(action) => {
704+
(ActionDataKind::CustomAction, action.into_py(py))
705+
}
706+
accesskit::ActionData::Value(value) => (ActionDataKind::Value, value.into_py(py)),
707+
accesskit::ActionData::NumericValue(value) => {
708+
(ActionDataKind::NumericValue, value.into_py(py))
709+
}
710+
accesskit::ActionData::ScrollTargetRect(rect) => (
711+
ActionDataKind::ScrollTargetRect,
712+
Rect::from(rect).into_py(py),
713+
),
714+
accesskit::ActionData::ScrollToPoint(point) => (
715+
ActionDataKind::ScrollToPoint,
716+
Point::from(point).into_py(py),
717+
),
718+
accesskit::ActionData::SetScrollOffset(point) => (
719+
ActionDataKind::SetScrollOffset,
720+
Point::from(point).into_py(py),
721+
),
722+
accesskit::ActionData::SetTextSelection(selection) => (
723+
ActionDataKind::SetTextSelection,
724+
TextSelection::from(&selection).into_py(py),
725+
),
726+
}),
739727
})
740728
}
741729
}

bindings/python/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ fn accesskit(py: Python<'_>, m: &PyModule) -> PyResult<()> {
4646
m.add_class::<NodeBuilder>()?;
4747
m.add_class::<Tree>()?;
4848
m.add_class::<TreeUpdate>()?;
49+
m.add_class::<ActionDataKind>()?;
50+
m.add_class::<ActionRequest>()?;
4951
m.add_class::<Affine>()?;
5052
m.add_class::<Point>()?;
5153
m.add_class::<Rect>()?;

bindings/python/src/macos.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,19 @@ impl Adapter {
6464
self.0.update_view_focus_state(is_focused).into()
6565
}
6666

67-
pub fn view_children(&self) -> isize {
68-
self.0.view_children() as _
67+
pub fn view_children(&self, py: Python<'_>) -> PyResult<Py<PyCapsule>> {
68+
let ptr: isize = self.0.view_children() as _;
69+
Ok(PyCapsule::new(py, ptr, None)?.into())
6970
}
7071

71-
pub fn focus(&self) -> isize {
72-
self.0.focus() as _
72+
pub fn focus(&self, py: Python<'_>) -> PyResult<Py<PyCapsule>> {
73+
let ptr: isize = self.0.focus() as _;
74+
Ok(PyCapsule::new(py, ptr, None)?.into())
7375
}
7476

75-
pub fn hit_test(&self, x: f64, y: f64) -> isize {
76-
self.0.hit_test(NSPoint::new(x, y)) as _
77+
pub fn hit_test(&self, py: Python<'_>, x: f64, y: f64) -> PyResult<Py<PyCapsule>> {
78+
let ptr: isize = self.0.hit_test(NSPoint::new(x, y)) as _;
79+
Ok(PyCapsule::new(py, ptr, None)?.into())
7780
}
7881
}
7982

bindings/python/src/windows.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ impl Adapter {
8585
}
8686
}
8787

88-
/// This class must only be used from the main thread.
8988
#[pyclass(module = "accesskit.windows", unsendable)]
9089
pub struct SubclassingAdapter(accesskit_windows::SubclassingAdapter);
9190

pyproject.toml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@ build-backend = "maturin"
55
[tool.maturin]
66
manifest-path = "bindings/python/Cargo.toml"
77
features = ["pyo3/extension-module"]
8-
exclude = [
9-
"bindings/python/examples/**"
10-
]
118
include = [
129
"LICENSE*"
1310
]

0 commit comments

Comments
 (0)