
    >i6                        d dl mZ d dlZd dlZd dlZd dlmZ d dlmZm	Z	 d dl
mZmZ d dlmZmZ d dlmZ d dlmZ d d	lmZ erd d
lmZ  ed       G d d             Z G d d      ZeZy)    )annotationsN)	dataclass)TYPE_CHECKINGOptional)InputCommandsRuntimeCommands)ScriptsScrollPosition)CubicBezier)MouseEventType)EvaluateResponse)TabT)frozenc                      e Zd ZU dZdZded<   dZded<   dZded	<   d
Zded<   dZ	ded<   dZ
ded<   dZded<   dZded<   dZded<   dZded<   dZded<   y)ScrollTimingConfigz+Configuration for realistic scroll physics.g      ?floatmin_duration      ?max_duration)gp=
ף?g
ףp=
?gQ?      ?z!tuple[float, float, float, float]bezier_pointsg~jt?frame_interval   intdelta_jitter皙?micro_pause_probabilityg{Gz?micro_pause_minmicro_pause_max333333?overshoot_probabilitygRQ?overshoot_factor_mingHzG?overshoot_factor_maxN)__name__
__module____qualname____doc__r   __annotations__r   r   r   r   r   r   r   r!   r"   r#        `/var/www/python/tenders/Scrapper/venv/lib/python3.12/site-packages/pydoll/interactions/scroll.pyr   r      sx    5L%L%7QM4Q!NE!L#%)U)!OU!!OU!#'5'"&%&"&%&r*   r   c                      e Zd ZdZ	 d	 	 	 ddZ	 	 d	 	 	 	 	 	 	 ddZdddZdddZddZddZ		 	 	 	 	 	 	 	 	 	 dd	Z
dd
ZddZddZddZd dZd!dZd!dZe	 	 	 	 	 	 d"d       Zed#d       Zd$dZy)%Scrollz
    API for controlling page scroll behavior.

    Provides methods for scrolling the page in different directions,
    to specific positions, or by relative distances. Supports humanized
    scrolling with realistic physics simulation.
    Nc                8    || _         |xs
 t               | _        y)z
        Initialize the Scroll with a Tab instance.

        Args:
            tab: Tab instance to execute scroll commands on.
            timing: Optional custom timing configuration for humanized scroll.
        N)_tabr   _timing)selftabtimings      r+   __init__zScroll.__init__2   s     	5!3!5r*   c                  K   |r| j                  ||       d{    y| j                  ||      \  }}| j                  |      }t        j                  j                  |||      }| j                  |       d{    y7 g7 w)au  
        Scroll the page by a relative distance in the specified direction.

        Args:
            position: Direction to scroll (UP, DOWN, LEFT, RIGHT).
            distance: Number of pixels to scroll.
            smooth: Use smooth scrolling animation if True, instant if False.
            humanize: Simulate human-like scrolling with momentum and inertia.
        N)axisdistancebehavior)_scroll_humanized_get_axis_and_distance_get_behaviorr	   	SCROLL_BYformat_execute_script_await_promise)	r1   positionr7   smoothhumanizer6   scroll_distancer8   scripts	            r+   byz	Scroll.byA   s       ((8<<< $ ; ;Hh Oo%%f-""))$ * 
 00888 = 	9s"   BBA!B<B=BBc                   K   |r(| j                  t        j                         d{    y| j                  |      }t        j
                  j                  |      }| j                  |       d{    y7 P7 w)z
        Scroll to the top of the page (Y=0).

        Args:
            smooth: Use smooth scrolling animation if True, instant if False.
            humanize: Simulate human-like scrolling with momentum and inertia.
        Nr8   )_scroll_to_end_humanizedr
   UPr;   r	   SCROLL_TO_TOPr=   r>   r1   r@   rA   r8   rC   s        r+   to_topzScroll.to_top`   sq      //0A0ABBB%%f-&&--x-@00888 C
 	9"   %A<A8A
A<2A:3A<:A<c                   K   |r(| j                  t        j                         d{    y| j                  |      }t        j
                  j                  |      }| j                  |       d{    y7 P7 w)z
        Scroll to the bottom of the page (Y=document.body.scrollHeight).

        Args:
            smooth: Use smooth scrolling animation if True, instant if False.
            humanize: Simulate human-like scrolling with momentum and inertia.
        NrF   )rG   r
   DOWNr;   r	   SCROLL_TO_BOTTOMr=   r>   rJ   s        r+   	to_bottomzScroll.to_bottomp   sq      //0C0CDDD%%f-))00(0C00888 E
 	9rL   c                F  K   t        j                  dd      }d}d}d}t        d      }d}d}	 |t        j                  k(  r| j                          d	{   }	n| j                          d	{   }	|	|k  ry	t        |	|z
        |k\  }
|
rd}|
s|d
z  }||k\  ry	|	}t        |	|      }||k  r|	|kD  r|}| j                  ||       d	{    t        j                  dd      }t        j                  |       d	{    t        j                  dd      }7 7 7 R7 !w)z
        Scroll to top or bottom with multiple human-like flicks.

        Humans don't scroll thousands of pixels in one motion - they do
        multiple scroll gestures with brief pauses in between.
        iX  i        d   infr   
   N   r   r    )randomuniformr   r
   rN   _get_remaining_scroll_to_bottom_get_current_scroll_yabsminr9   asynciosleep)r1   r?   max_flick_distancemin_remaining_thresholdmin_stuck_thresholdmin_flick_distancelast_remainingstuck_countermax_stuck_attempts	remaininghas_progressedflick_distancepauses                r+   rG   zScroll._scroll_to_end_humanized   sB     $^^C6"$  u>..."&"F"F"HH	"&"<"<">>	33 !^!;<@SSN !!" $66&N ,>?N 22yCU7U!3((>BBBNN4.E--&&&!'T!:? H>, C 'sI   AD!DD!.D/AD!
D2D!=D>D!D!D!D!c                  K   |t         j                  t         j                  hv }|t         j                  t         j                  hv rdnd}| j	                  |      }| j                  |      }| j                  ||||       d{   }||kD  r\||kD  rV||z
  }| }	t        j                  t        j                  dd             d{    | j                  ||	|       d{    yyy7 f7 %7 w)a3  
        Perform scroll with realistic human-like physics.

        Simulates momentum-based scrolling with:
        - Smooth deceleration curve
        - Variable frame intervals
        - Random jitter in scroll deltas
        - Occasional micro-pauses
        - Optional overshoot and correction
        rW   Ng?皙?)is_vertical	directionr7   )r
   rH   rN   LEFT_calculate_effective_distance_calculate_duration_perform_scroll_loopr^   r_   rX   rY   _scroll_correction)
r1   r?   target_distancern   ro   effective_distancedurationscrolled_so_farcorrection_distancecorrection_directions
             r+   r9   zScroll._scroll_humanized   s     >#4#4n6I6I"JJ"~'8'8.:M:M&NNBTU	!??P++,>? $ 9 9+y!
 
 /Oo4U"1O"C$-: --sC 8999))'., *    5V/	
 :s7   BC1C+AC1C-C1#C/$C1-C1/C1c                  K   | j                   }t        |j                   }t        j                         j                         }d}d}	||k  rt        j                         j                         }
|
|z
  }||k\  r	 |	S ||z  }|j                  |      }||z  }||	z
  }t        j                  |j                   |j                        }||z  }t        |d      }|dk\  r@| j                  |rdnt        ||z        |rt        ||z        nd       d{    |	|z  }	|j                  t        j                  dd      z   }t        j                  |       d{    t        j                         |j                   k  rLt        j                  |j"                  |j$                        }t        j                  |       d{    ||z  }||k  r|	S 7 7 |7 w)z1Execute the main scroll loop using Bezier timing.        r   rW   delta_xdelta_yNgMb`gMb`?)r0   r   r   r^   get_running_looptimesolverX   randintr   max_dispatch_scroll_eventr   r   rY   r_   r   r   r   )r1   rv   rw   rn   ro   r3   bezier
start_timecurrent_timerx   nowprogresseased_progress
target_posdeltajitterframe_delaypause_durations                     r+   rs   zScroll._perform_scroll_loop   s     f223--/446
X%**,113C+Lx'8 5 $h.H#\\(3N+n<J0E^^V%8%8$8&:M:MNFVOEqMEz11!,A#ei6G2H6AC	 12q 2     5( //&..2OOK--,,,}}!?!??!'0F0FH^H^!_mmN333n,
? X%B  - 4s>   DGGAGGA'G GGGGGc                    | j                   }t        j                         |j                  k  }|r*t        j                  |j                  |j
                        nd}||z  S )z1Calculate effective distance including overshoot.r   )r0   rX   r!   rY   r"   r#   )r1   ru   r3   should_overshootovershoot_factors        r+   rq   z$Scroll._calculate_effective_distance  sY    !==?V-I-II   NN6668S8ST 	
 !111r*   c                    | j                   }t        j                  |j                  |j                        }|dd|dz  z  z   z  }t        |d      S )z,Calculate scroll duration based on distance.rW   rm   i  g      @)r0   rX   rY   r   r   r]   )r1   r7   r3   base_durationrw   s        r+   rr   zScroll._calculate_duration  sL    v':':F<O<OP Ax$(?$?@8S!!r*   c                  K   | j                   }d}|dz  |j                  z  }t        j                  t	        d|      t	        d|dz              }||k  r||j                  z  }t        |||z
        }| j                  |rdnt        ||z        |rt        ||z        nd       d{    ||z  }|d	z  }t        j                  |j                         d{    ||k  ryy7 =7 w)
z0Perform small correction scroll after overshoot.r|   r         r   r   r}   Ng333333?)
r0   r   rX   rY   r   r]   r   r   r^   r_   )	r1   rn   ro   r7   r3   scrolledmin_correction_velocitycorrection_velocityframe_deltas	            r+   rt   zScroll._scroll_correction  s    #+t#48M8M"M$nn,-s38ORU8U/V
 !-0E0EEKk8h+>?K--(c+	2I.J8CK)34 .   
 #H4'-- 5 5666 ! 7s*   B'C+)C'*0C+C)	C+%C+)C+c                   K   | j                          d{   }t        j                  t        j                  |d   |d   ||      }| j
                  j                  |       d{    y7 V7 w)z+Dispatch a mouse wheel event for scrolling.Nr   rW   )typexyr~   r   )_get_viewport_centerr   dispatch_mouse_eventr   MOUSE_WHEELr/   _execute_command)r1   r~   r   viewportcommands        r+   r   zScroll._dispatch_scroll_event/  si     224444++qkqk
 ii((111 5 	2s"   A1A-AA1'A/(A1/A1c                  K   t        j                  t        j                        }| j                  j                  |       d{   }|j                  di       j                  di       j                  dd      }d}	 t        j                  |      }|r:t        |t              r*t        |      |k(  rt        |d         t        |d         fS y	7 # t        j                  t        f$ r Y y	w xY ww)
z+Get the center coordinates of the viewport.
expressionNresultvaluez[]   r   rW   )r   i,  )r   evaluater	   GET_VIEWPORT_CENTERr/   r   getjsonloads
isinstancelistlenr   JSONDecodeError	TypeError)r1   r   r   	value_strexpected_dimensionsr   s         r+   r   zScroll._get_viewport_center;  s     !**g6Q6QR)-)C)CG)L#LJJx,002>BB7DQ		JJy)EE40SZCV5VE!Hs58}55  $M $$i0 		s7   AC2C8C2?AC C2C/,C2.C//C2c                  K   t        j                  t        j                        }| j                  j                  |       d{   }t        |j                  di       j                  di       j                  dd            S 7 ?w)z%Get current vertical scroll position.r   Nr   r   r   )r   r   r	   GET_SCROLL_Yr/   r   r   r   r1   r   r   s      r+   r[   zScroll._get_current_scroll_yJ  si     !**g6J6JK)-)C)CG)L#LVZZ"-11(B?CCGQOPP $M   ABBA Bc                  K   t        j                  t        j                        }| j                  j                  |       d{   }t        |j                  di       j                  di       j                  dd            S 7 ?w)z5Get remaining distance to scroll to reach the bottom.r   Nr   r   r   )r   r   r	   GET_REMAINING_SCROLL_TO_BOTTOMr/   r   r   r   r   s      r+   rZ   z&Scroll._get_remaining_scroll_to_bottomP  si     !**g6\6\])-)C)CG)L#LVZZ"-11(B?CCGQOPP $Mr   c                    | t         j                  t         j                  hv rd}| t         j                  k(  r| n|}||fS d}| t         j                  k(  r| n|}||fS )a\  
        Convert scroll position to axis and signed distance.

        Args:
            position: Direction to scroll.
            distance: Absolute distance to scroll.

        Returns:
            Tuple of (axis, signed_distance) where axis is 'left' or 'top'
            and signed_distance is positive or negative based on direction.
        topleft)r
   rH   rN   rp   )r?   r7   r6   rB   s       r+   r:   zScroll._get_axis_and_distanceV  si     ))>+>+>??D+3~7H7H+HxihO(('/>3F3F'F8)H_$$r*   c                    | rdS dS )z
        Convert smooth boolean to CSS scroll behavior value.

        Args:
            smooth: Whether to use smooth scrolling.

        Returns:
            'smooth' if smooth is True, 'auto' otherwise.
        r@   autor)   )r@   s    r+   r;   zScroll._get_behaviorn  s     "x-v-r*   c                   K   t        j                  |d      }| j                  j                  |       d{   S 7 w)z
        Execute JavaScript and await promise resolution.

        Args:
            script: JavaScript code that returns a Promise.
        T)r   await_promiseN)r   r   r/   r   )r1   rC   r   s      r+   r>   z$Scroll._execute_script_await_promise{  s5      "**fDQYY//8888s   6?=?)N)r2   r   r3   zOptional[ScrollTimingConfig])TF)r?   r
   r7   int | floatr@   boolrA   r   )r@   r   rA   r   )r?   r
   )r?   r
   ru   r   )
rv   r   rw   r   rn   r   ro   r   returnr   )ru   r   r   r   )r7   r   r   r   )rn   r   ro   r   r7   r   )r~   r   r   r   )r   ztuple[int, int])r   r   )r?   r
   r7   r   r   ztuple[str, int | float])r@   r   r   str)rC   r   )r$   r%   r&   r'   r4   rD   rK   rP   rG   r9   rs   rq   rr   rt   r   r   r[   rZ   staticmethodr:   r;   r>   r)   r*   r+   r-   r-   )   s    0466 -6& 9 9 9 	9
 9>9 9 0;dB0!0 0 	0
 0 
0d	2"70
2QQ % %,7%	 % %. 
. 
.9r*   r-   )
__future__r   r^   r   rX   dataclassesr   typingr   r   pydoll.commandsr   r   pydoll.constantsr	   r
   pydoll.interactions.utilsr   pydoll.protocol.input.typesr   pydoll.protocol.runtime.methodsr   pydoll.browser.tabr   r   r-   	ScrollAPIr)   r*   r+   <module>r      s^    "    ! * : 4 1 6 <& $' ' '*Z9 Z9|
 	r*   