<s> """<STR_LIT>""" <EOL> from kivy . uix . recycleview . layout import RecycleLayoutManagerBehavior <EOL> from kivy . uix . layout import Layout <EOL> from kivy . properties import ObjectProperty , StringProperty <EOL> from kivy . factory import Factory <EOL> __all__ = ( '<STR_LIT>' , ) <EOL> class RecycleLayout ( RecycleLayoutManagerBehavior , Layout ) : <EOL> default_size = ObjectProperty ( ( <NUM_LIT:100> , <NUM_LIT:100> ) ) <EOL> '''<STR_LIT>''' <EOL> key_size = StringProperty ( None , allownone = True ) <EOL> default_size_hint = ObjectProperty ( ( None , None ) ) <EOL> key_size_hint = StringProperty ( None , allownone = True ) <EOL> default_pos_hint = ObjectProperty ( { } ) <EOL> key_pos_hint = StringProperty ( None , allownone = True ) <EOL> initial_size = ObjectProperty ( ( <NUM_LIT:100> , <NUM_LIT:100> ) ) <EOL> view_opts = [ ] <EOL> _size_needs_update = False <EOL> _changed_views = [ ] <EOL> view_indices = { } <EOL> def __init__ ( self , ** kwargs ) : <EOL> self . view_indices = { } <EOL> self . _updated_views = [ ] <EOL> self . _trigger_layout = self . _catch_layout_trigger <EOL> super ( RecycleLayout , self ) . __init__ ( ** kwargs ) <EOL> def attach_recycleview ( self , rv ) : <EOL> super ( RecycleLayout , self ) . attach_recycleview ( rv ) <EOL> if rv : <EOL> fbind = self . fbind <EOL> fbind ( '<STR_LIT>' , rv . refresh_from_data ) <EOL> fbind ( '<STR_LIT>' , rv . refresh_from_data ) <EOL> fbind ( '<STR_LIT>' , rv . refresh_from_data ) <EOL> fbind ( '<STR_LIT>' , rv . refresh_from_data ) <EOL> fbind ( '<STR_LIT>' , rv . refresh_from_data ) <EOL> fbind ( '<STR_LIT>' , rv . refresh_from_data ) <EOL> def detach_recycleview ( self ) : <EOL> rv = self . recycleview <EOL> if rv : <EOL> funbind = self . funbind <EOL> funbind ( '<STR_LIT>' , rv . refresh_from_data ) <EOL> funbind ( '<STR_LIT>' , rv . refresh_from_data ) <EOL> funbind ( '<STR_LIT>' , rv . refresh_from_data ) <EOL> funbind ( '<STR_LIT>' , rv . refresh_from_data ) <EOL> funbind ( '<STR_LIT>' , rv . refresh_from_data ) <EOL> funbind ( '<STR_LIT>' , rv . refresh_from_data ) <EOL> super ( RecycleLayout , self ) . detach_recycleview ( ) <EOL> def _catch_layout_trigger ( self , instance = None , value = None ) : <EOL> rv = self . recycleview <EOL> if rv is None : <EOL> return <EOL> idx = self . view_indices . get ( instance ) <EOL> if idx is not None : <EOL> if self . _size_needs_update : <EOL> return <EOL> opt = self . view_opts [ idx ] <EOL> if ( instance . size == opt [ '<STR_LIT:size>' ] and <EOL> instance . size_hint == opt [ '<STR_LIT>' ] and <EOL> instance . pos_hint == opt [ '<STR_LIT>' ] ) : <EOL> return <EOL> self . _size_needs_update = True <EOL> rv . refresh_from_layout ( view_size = True ) <EOL> else : <EOL> rv . refresh_from_layout ( ) <EOL> def compute_sizes_from_data ( self , data , flags ) : <EOL> if [ f for f in flags if not f ] : <EOL> self . clear_layout ( ) <EOL> opts = self . view_opts = [ None for _ in data ] <EOL> else : <EOL> opts = self . view_opts <EOL> changed = False <EOL> for flag in flags : <EOL> for k , v in flag . items ( ) : <EOL> changed = True <EOL> if k == '<STR_LIT>' : <EOL> del opts [ v ] <EOL> elif k == '<STR_LIT>' : <EOL> opts . extend ( [ None , ] * ( v . stop - v . start ) ) <EOL> elif k == '<STR_LIT>' : <EOL> opts . insert ( v , None ) <EOL> elif k == '<STR_LIT>' : <EOL> start , stop , step = v . start , v . stop , v . step <EOL> r = range ( start , stop ) if step == None else range ( start , stop , step ) <EOL> for i in r : <EOL> opts [ i ] = None <EOL> else : <EOL> raise Exception ( '<STR_LIT>' . format ( k ) ) <EOL> if changed : <EOL> self . clear_layout ( ) <EOL> assert len ( data ) == len ( opts ) <EOL> ph_key = self . key_pos_hint <EOL> ph_def = self . default_pos_hint <EOL> sh_key = self . key_size_hint <EOL> sh_def = self . default_size_hint <EOL> s_key = self . key_size <EOL> s_def = self . default_size <EOL> viewcls_def = self . viewclass <EOL> viewcls_key = self . key_viewclass <EOL> iw , ih = self . initial_size <EOL> sh = [ ] <EOL> for i , item in enumerate ( data ) : <EOL> if opts [ i ] is not None : <EOL> continue <EOL> ph = ph_def if ph_key is None else item . get ( ph_key , ph_def ) <EOL> ph = item . get ( '<STR_LIT>' , ph ) <EOL> sh = sh_def if sh_key is None else item . get ( sh_key , sh_def ) <EOL> sh = item . get ( '<STR_LIT>' , sh ) <EOL> sh = [ item . get ( '<STR_LIT>' , sh [ <NUM_LIT:0> ] ) , <EOL> item . get ( '<STR_LIT>' , sh [ <NUM_LIT:1> ] ) ] <EOL> s = s_def if s_key is None else item . get ( s_key , s_def ) <EOL> s = item . get ( '<STR_LIT:size>' , s ) <EOL> w , h = s = item . get ( '<STR_LIT:width>' , s [ <NUM_LIT:0> ] ) , item . get ( '<STR_LIT>' , s [ <NUM_LIT:1> ] ) <EOL> viewcls = None <EOL> if viewcls_key is not None : <EOL> viewcls = item . get ( viewcls_key ) <EOL> if viewcls is not None : <EOL> viewcls = getattr ( Factory , viewcls ) <EOL> if viewcls is None : <EOL> viewcls = viewcls_def <EOL> opts [ i ] = { <EOL> '<STR_LIT:size>' : [ ( iw if w is None else w ) , ( ih if h is None else h ) ] , <EOL> '<STR_LIT>' : sh , '<STR_LIT>' : None , '<STR_LIT>' : ph , <EOL> '<STR_LIT>' : viewcls , '<STR_LIT>' : w is None , <EOL> '<STR_LIT>' : h is None } <EOL> def compute_layout ( self , data , flags ) : <EOL> self . _size_needs_update = False <EOL> opts = self . view_opts <EOL> changed = [ ] <EOL> for widget , index in self . view_indices . items ( ) : <EOL> opt = opts [ index ] <EOL> s = opt [ '<STR_LIT:size>' ] <EOL> w , h = sn = widget . size <EOL> sh = opt [ '<STR_LIT>' ] <EOL> shnw , shnh = shn = widget . size_hint <EOL> ph = opt [ '<STR_LIT>' ] <EOL> phn = widget . pos_hint <EOL> if s != sn or sh != shn or ph != phn : <EOL> changed . append ( ( index , widget , s , sn , sh , shn , ph , phn ) ) <EOL> if shnw is None : <EOL> if shnh is None : <EOL> opt [ '<STR_LIT:size>' ] = sn <EOL> else : <EOL> opt [ '<STR_LIT:size>' ] = [ w , s [ <NUM_LIT:1> ] ] <EOL> elif shnh is None : <EOL> opt [ '<STR_LIT:size>' ] = [ s [ <NUM_LIT:0> ] , h ] <EOL> opt [ '<STR_LIT>' ] = shn <EOL> opt [ '<STR_LIT>' ] = phn <EOL> if [ f for f in flags if not f ] : <EOL> self . _changed_views = [ ] <EOL> else : <EOL> self . _changed_views = changed if changed else None <EOL> def do_layout ( self , * largs ) : <EOL> assert False <EOL> def set_visible_views ( self , indices , data , viewport ) : <EOL> view_opts = self . view_opts <EOL> new , remaining , old = self . recycleview . view_adapter . set_visible_views ( <EOL> indices , data , view_opts ) <EOL> remove = self . remove_widget <EOL> view_indices = self . view_indices <EOL> for _ , widget in old : <EOL> remove ( widget ) <EOL> del view_indices [ widget ] <EOL> refresh_view_layout = self . refresh_view_layout <EOL> for index , widget in new : <EOL> opt = view_opts [ index ] <EOL> refresh_view_layout ( <EOL> index , opt [ '<STR_LIT>' ] , opt [ '<STR_LIT>' ] , opt [ '<STR_LIT:size>' ] , <EOL> opt [ '<STR_LIT>' ] , widget , viewport ) <EOL> add = self . add_widget <EOL> for index , widget in new : <EOL> view_indices [ widget ] = index <EOL> if widget . parent is None : <EOL> add ( widget ) <EOL> changed = False <EOL> for index , widget in new : <EOL> opt = view_opts [ index ] <EOL> if ( changed or widget . size == opt [ '<STR_LIT:size>' ] and <EOL> widget . size_hint == opt [ '<STR_LIT>' ] and <EOL> widget . pos_hint == opt [ '<STR_LIT>' ] ) : <EOL> continue <EOL> changed = True <EOL> if changed : <EOL> self . _size_needs_update = True <EOL> self . recycleview . refresh_from_layout ( view_size = True ) <EOL> def refresh_view_layout ( self , index , pos , pos_hint , size , size_hint , view , <EOL> viewport ) : <EOL> opt = self . view_opts [ index ] <EOL> w , h = size <EOL> shw , shh = size_hint <EOL> if shw is None and opt [ '<STR_LIT>' ] : <EOL> w = None <EOL> if shh is None and opt [ '<STR_LIT>' ] : <EOL> h = None <EOL> super ( RecycleLayout , self ) . refresh_view_layout ( <EOL> index , pos , pos_hint , ( w , h ) , size_hint , view , viewport ) <EOL> def remove_views ( self ) : <EOL> super ( RecycleLayout , self ) . remove_views ( ) <EOL> self . clear_widgets ( ) <EOL> self . view_indices = { } <EOL> def remove_view ( self , view , index ) : <EOL> super ( RecycleLayout , self ) . remove_view ( view , index ) <EOL> self . remove_widget ( view ) <EOL> del self . view_indices [ view ] <EOL> def clear_layout ( self ) : <EOL> super ( RecycleLayout , self ) . clear_layout ( ) <EOL> self . clear_widgets ( ) <EOL> self . view_indices = { } <EOL> self . _size_needs_update = False </s>
