44// the LICENSE-MIT file), at your option.
55
66use crate :: { Point , Rect } ;
7- use pyo3:: { prelude:: * , types:: PyList } ;
7+ use pyo3:: {
8+ prelude:: * ,
9+ types:: { PyList , PyTuple } ,
10+ IntoPyObjectExt ,
11+ } ;
812
913#[ derive( Clone ) ]
1014#[ pyclass( module = "accesskit" ) ]
@@ -151,7 +155,6 @@ impl From<accesskit::TextPosition> for TextPosition {
151155 }
152156}
153157
154- #[ derive( Clone ) ]
155158#[ pyclass( get_all, set_all, module = "accesskit" ) ]
156159pub struct TextSelection {
157160 pub anchor : Py < TextPosition > ,
@@ -178,15 +181,20 @@ impl From<&accesskit::TextSelection> for TextSelection {
178181impl From < TextSelection > for accesskit:: TextSelection {
179182 fn from ( selection : TextSelection ) -> Self {
180183 Python :: with_gil ( |py| accesskit:: TextSelection {
181- anchor : selection. anchor . as_ref ( py) . borrow ( ) . 0 ,
182- focus : selection. focus . as_ref ( py) . borrow ( ) . 0 ,
184+ anchor : selection. anchor . bind ( py) . borrow ( ) . 0 ,
185+ focus : selection. focus . bind ( py) . borrow ( ) . 0 ,
183186 } )
184187 }
185188}
186189
187- impl From < TextSelection > for Box < accesskit:: TextSelection > {
188- fn from ( selection : TextSelection ) -> Self {
189- Box :: new ( selection. into ( ) )
190+ impl From < & TextSelection > for Box < accesskit:: TextSelection > {
191+ fn from ( selection : & TextSelection ) -> Self {
192+ Python :: with_gil ( |py| {
193+ Box :: new ( accesskit:: TextSelection {
194+ anchor : selection. anchor . borrow ( py) . 0 ,
195+ focus : selection. focus . borrow ( py) . 0 ,
196+ } )
197+ } )
190198 }
191199}
192200
@@ -293,14 +301,14 @@ macro_rules! vec_property_methods {
293301 $( #[ pymethods]
294302 impl Node {
295303 #[ getter]
296- pub fn $getter( & self , py: Python ) -> Py <PyList > {
297- let values = self . inner( ) . $getter( ) . iter( ) . cloned( ) . map( <$py_item_type>:: from) . map ( |i| i . into_py ( py ) ) ;
298- PyList :: new( py, values) . into ( )
304+ pub fn $getter( & self , py: Python ) -> PyResult < Py <PyList > > {
305+ let values = self . inner( ) . $getter( ) . iter( ) . cloned( ) . map( <$py_item_type>:: from) ;
306+ Ok ( PyList :: new( py, values) ? . unbind ( ) )
299307 }
300- pub fn $setter( & mut self , values: & PyList ) {
308+ pub fn $setter( & mut self , values: & Bound < ' _ , PyList > ) {
301309 let values = values
302310 . iter( )
303- . map( PyAny :: extract:: <$py_item_type>)
311+ . map( |item| item . extract:: <$py_item_type>( ) )
304312 . filter_map( PyResult :: ok)
305313 . map( <$accesskit_item_type>:: from)
306314 . collect:: <Vec <$accesskit_item_type>>( ) ;
@@ -540,7 +548,7 @@ unique_enum_property_methods! {
540548property_methods ! {
541549 ( transform, option_getter, Option <crate :: Affine >, set_transform, simple_setter, crate :: Affine , clear_transform) ,
542550 ( bounds, option_getter, Option <crate :: Rect >, set_bounds, converting_setter, crate :: Rect , clear_bounds) ,
543- ( text_selection, option_getter, Option <TextSelection >, set_text_selection, simple_setter, TextSelection , clear_text_selection)
551+ ( text_selection, option_getter, Option <TextSelection >, set_text_selection, simple_setter, & TextSelection , clear_text_selection)
544552}
545553
546554vec_property_methods ! {
@@ -551,7 +559,6 @@ vec_property_methods! {
551559#[ pyclass( module = "accesskit" , get_all, set_all) ]
552560pub struct Tree {
553561 pub root : NodeId ,
554- pub app_name : Option < String > ,
555562 pub toolkit_name : Option < String > ,
556563 pub toolkit_version : Option < String > ,
557564}
@@ -562,7 +569,6 @@ impl Tree {
562569 pub fn new ( root : NodeId ) -> Self {
563570 Self {
564571 root,
565- app_name : None ,
566572 toolkit_name : None ,
567573 toolkit_version : None ,
568574 }
@@ -573,14 +579,12 @@ impl From<Tree> for accesskit::Tree {
573579 fn from ( tree : Tree ) -> Self {
574580 Self {
575581 root : tree. root . into ( ) ,
576- app_name : tree. app_name ,
577582 toolkit_name : tree. toolkit_name ,
578583 toolkit_version : tree. toolkit_version ,
579584 }
580585 }
581586}
582587
583- #[ derive( Clone ) ]
584588#[ pyclass( module = "accesskit" , get_all, set_all) ]
585589pub struct TreeUpdate {
586590 pub nodes : Py < PyList > ,
@@ -600,22 +604,21 @@ impl TreeUpdate {
600604 }
601605}
602606
603- impl From < TreeUpdate > for accesskit:: TreeUpdate {
604- fn from ( update : TreeUpdate ) -> Self {
607+ impl From < & TreeUpdate > for accesskit:: TreeUpdate {
608+ fn from ( update : & TreeUpdate ) -> Self {
605609 Python :: with_gil ( |py| Self {
606610 nodes : update
607611 . nodes
608- . as_ref ( py)
612+ . bind ( py)
609613 . iter ( )
610- . map ( PyAny :: extract :: < ( NodeId , Node ) > )
614+ . map ( |n| n . extract :: < ( NodeId , Node ) > ( ) )
611615 . filter_map ( Result :: ok)
612616 . map ( |( id, node) | ( id. into ( ) , node. into ( ) ) )
613617 . collect ( ) ,
614- tree : update. tree . map ( |tree| {
615- let tree = tree. as_ref ( py) . borrow ( ) ;
618+ tree : update. tree . as_ref ( ) . map ( |tree| {
619+ let tree = tree. bind ( py) . borrow ( ) ;
616620 accesskit:: Tree {
617621 root : tree. root . into ( ) ,
618- app_name : tree. app_name . clone ( ) ,
619622 toolkit_name : tree. toolkit_name . clone ( ) ,
620623 toolkit_version : tree. toolkit_version . clone ( ) ,
621624 }
@@ -625,8 +628,8 @@ impl From<TreeUpdate> for accesskit::TreeUpdate {
625628 }
626629}
627630
628- #[ derive( Clone ) ]
629- #[ pyclass( module = "accesskit" , rename_all = "SCREAMING_SNAKE_CASE" ) ]
631+ #[ derive( PartialEq ) ]
632+ #[ pyclass( module = "accesskit" , rename_all = "SCREAMING_SNAKE_CASE" , eq , eq_int ) ]
630633pub enum ActionDataKind {
631634 CustomAction ,
632635 Value ,
@@ -641,38 +644,46 @@ pub enum ActionDataKind {
641644pub struct ActionRequest {
642645 pub action : accesskit:: Action ,
643646 pub target : NodeId ,
644- pub data : Option < ( ActionDataKind , Py < PyAny > ) > ,
647+ pub data : Option < Py < PyTuple > > ,
645648}
646649
647650impl From < accesskit:: ActionRequest > for ActionRequest {
648651 fn from ( request : accesskit:: ActionRequest ) -> Self {
649652 Python :: with_gil ( |py| Self {
650653 action : request. action ,
651654 target : request. target . into ( ) ,
652- data : request. data . map ( |data| match data {
653- accesskit:: ActionData :: CustomAction ( action) => {
654- ( ActionDataKind :: CustomAction , action. into_py ( py) )
655+ data : request. data . map ( |data| {
656+ match data {
657+ accesskit:: ActionData :: CustomAction ( action) => (
658+ ActionDataKind :: CustomAction ,
659+ action. into_py_any ( py) . unwrap ( ) ,
660+ ) ,
661+ accesskit:: ActionData :: Value ( value) => {
662+ ( ActionDataKind :: Value , value. into_py_any ( py) . unwrap ( ) )
663+ }
664+ accesskit:: ActionData :: NumericValue ( value) => {
665+ ( ActionDataKind :: NumericValue , value. into_py_any ( py) . unwrap ( ) )
666+ }
667+ accesskit:: ActionData :: ScrollTargetRect ( rect) => (
668+ ActionDataKind :: ScrollTargetRect ,
669+ Rect :: from ( rect) . into_py_any ( py) . unwrap ( ) ,
670+ ) ,
671+ accesskit:: ActionData :: ScrollToPoint ( point) => (
672+ ActionDataKind :: ScrollToPoint ,
673+ Point :: from ( point) . into_py_any ( py) . unwrap ( ) ,
674+ ) ,
675+ accesskit:: ActionData :: SetScrollOffset ( point) => (
676+ ActionDataKind :: SetScrollOffset ,
677+ Point :: from ( point) . into_py_any ( py) . unwrap ( ) ,
678+ ) ,
679+ accesskit:: ActionData :: SetTextSelection ( selection) => (
680+ ActionDataKind :: SetTextSelection ,
681+ TextSelection :: from ( & selection) . into_py_any ( py) . unwrap ( ) ,
682+ ) ,
655683 }
656- accesskit:: ActionData :: Value ( value) => ( ActionDataKind :: Value , value. into_py ( py) ) ,
657- accesskit:: ActionData :: NumericValue ( value) => {
658- ( ActionDataKind :: NumericValue , value. into_py ( py) )
659- }
660- accesskit:: ActionData :: ScrollTargetRect ( rect) => (
661- ActionDataKind :: ScrollTargetRect ,
662- Rect :: from ( rect) . into_py ( py) ,
663- ) ,
664- accesskit:: ActionData :: ScrollToPoint ( point) => (
665- ActionDataKind :: ScrollToPoint ,
666- Point :: from ( point) . into_py ( py) ,
667- ) ,
668- accesskit:: ActionData :: SetScrollOffset ( point) => (
669- ActionDataKind :: SetScrollOffset ,
670- Point :: from ( point) . into_py ( py) ,
671- ) ,
672- accesskit:: ActionData :: SetTextSelection ( selection) => (
673- ActionDataKind :: SetTextSelection ,
674- TextSelection :: from ( & selection) . into_py ( py) ,
675- ) ,
684+ . into_pyobject ( py)
685+ . unwrap ( )
686+ . unbind ( )
676687 } ) ,
677688 } )
678689 }
@@ -690,9 +701,9 @@ impl accesskit::ActivationHandler for LocalPythonActivationHandler<'_> {
690701 fn request_initial_tree ( & mut self ) -> Option < accesskit:: TreeUpdate > {
691702 let result = self . handler . call0 ( self . py ) . unwrap ( ) ;
692703 result
693- . extract :: < Option < TreeUpdate > > ( self . py )
704+ . extract :: < Option < PyRef < TreeUpdate > > > ( self . py )
694705 . unwrap ( )
695- . map ( Into :: into)
706+ . map ( |tree| ( & * tree ) . into ( ) )
696707 }
697708}
698709
@@ -703,9 +714,9 @@ impl accesskit::ActivationHandler for PythonActivationHandler {
703714 Python :: with_gil ( |py| {
704715 let result = self . 0 . call0 ( py) . unwrap ( ) ;
705716 result
706- . extract :: < Option < TreeUpdate > > ( py)
717+ . extract :: < Option < PyRef < TreeUpdate > > > ( py)
707718 . unwrap ( )
708- . map ( Into :: into)
719+ . map ( |tree| ( & * tree ) . into ( ) )
709720 } )
710721 }
711722}
0 commit comments