
    Ui*                     l    d Z ddlZddlZddlZddlmZmZmZmZ ddl	m
Z
 ddlmZ  G d d          ZdS )z
Zoom Page Primitive Operation

This module provides page zooming functionality with human-like mouse movement.
Simulates natural zoom behavior using Ctrl+scroll wheel.
    N)DictAnyTupleList)Page)ManagedPagec                       e Zd ZdZd Zddedededed	ed
ee	e
f         fdZd Zd Zdded	ed
ee	e
f         fdZd Zdededed	efdZ	 ddededededed
eeeef                  fdZdS )ZoomPagea  
    Zoom Page with Human-like Mouse Movement

    Implements realistic page zoom behavior:
    - Human-like mouse movement to zoom coordinates
    - Natural Ctrl+scroll wheel zoom
    - Realistic timing delays

    Usage:
        zoomer = ZoomPage()
        result = await zoomer.zoom(managed_page, x=500, y=300, zoom_level=3)
        reset_result = await zoomer.reset(managed_page)
    c                 $    t          d           dS )z!Initialize the zoom page handler.u   🔍 ZoomPage initializedN)print)selfs    </home/byschii/byschiidev/penelope/lib/primitive/zoom_page.py__init__zZoomPage.__init__!   s    )*****       d   managed_pagexy
zoom_levelslowmoreturnc                   K   	 t          d| d| d|            |j        }|                                 d{V  |                     ||||           d{V  t	          j        |dz  t          j        dd          z             d{V  |j        	                    d           d{V  t	          j        t          j        d	d
                     d{V  | 
                    ||||dk    rdnd           d{V  |j                            d           d{V  t	          j        |dz  t          j        dd          z             d{V  ||_        ||_        d|||d|j        d}t          d|            |S # t          $ r1}d|t!          |          d}	t          d|            |	cY d}~S d}~ww xY w)a  
        Zoom the page at specified coordinates.

        Args:
            managed_page (ManagedPage): Managed page to zoom
            x (float): X coordinate for zoom center
            y (float): Y coordinate for zoom center
            zoom_level (int): Number of zoom ticks (default: 1)
            slowmo (int): Base delay in milliseconds for timing (default: 100)

        Returns:
            Dict containing zoom result and information
        u   🔍 Zooming at (z, z) with level N        ?      ?Control皙?333333?r   g      ?皙?T)r   r   )successr   positionurlu   ✅ Page zoomed: level F)r!   r   erroru   ❌ Page zoom failed: )r   pagebring_to_front_move_mouse_human_likeasynciosleeprandomuniformkeyboarddown_zoom_at_mouseuplast_known_mouse_xlast_known_mouse_yr#   	Exceptionstr)
r   r   r   r   r   r   r%   resulteerror_results
             r   zoomzZoomPage.zoom%   sn     *	 GaGG1GG:GGHHH$D%%'''''''''--lAq&IIIIIIIII-sC0H0H HIIIIIIIII -$$Y/////////-tT : :;;;;;;;;;%%dAq*q..$$cRRRRRRRRR -""9----------sC0H0H HIIIIIIIII /0L+./L+  ("#!,,x	 F 8J88999M 	  	  	  (Q L
 .1..///	 s   F F 
G &F;5G ;G c                 J   K   |                     d|||d           d{V  dS )z:Zoom the page around a specific point using CSS transformsa  
            ({x, y, zoom}) => {
                const body = document.body;
                const html = document.documentElement;
                
                // Get current transform
                const currentTransform = body.style.transform || 'scale(1)';
                const currentScale = parseFloat(currentTransform.match(/scale\(([^)]+)\)/)?.[1] || 1);
                
                // Calculate new scale
                const newScale = currentScale * zoom;
                
                // Calculate transform origin to zoom around mouse
                const originX = (x / window.innerWidth) * 100;
                const originY = (y / window.innerHeight) * 100;
                
                // Apply transform
                body.style.transformOrigin = `${originX}% ${originY}%`;
                body.style.transform = `scale(${newScale})`;
                html.style.transformOrigin = `${originX}% ${originY}%`;
                html.style.transform = `scale(${newScale})`;
            }
        )r   r   r7   N)evaluate)r   r%   mouse_xmouse_yzoom_factors        r   r.   zZoomPage._zoom_at_mouse_   sc      mm , +>>-@ @ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@r   c                    K   |j                             |           d {V }|                    dd|i           d {V  d S )NEmulation.setPageScaleFactorpageScaleFactorcontextnew_cdp_sessionsend)r   r%   r   cdps       r   	_set_zoomzZoomPage._set_zoomy   sz      L0066666666 hh5z8
   	 	 	 	 	 	 	 	 	r   c                   K   	 t          d           |j        }|j                            d           d{V  t	          j        t          j        dd                     d{V  |                     |           d{V  t	          j        t          j        dd                     d{V  |j        	                    d           d{V  t	          j        |dz  t          j        d	d
          z             d{V  d|j
        d}t          d           |S # t          $ r0}dt          |          d}t          d|            |cY d}~S d}~ww xY w)a&  
        Reset page zoom to default level (100%).

        Args:
            managed_page (ManagedPage): Managed page to reset zoom
            slowmo (int): Base delay in milliseconds for timing (default: 100)

        Returns:
            Dict containing reset result and information
        u   🔍 Resetting page zoomr   Nr   r   g?皙?r   r   r   T)r!   r#   u   ✅ Page zoom resetF)r!   r$   u   ❌ Page zoom reset failed: )r   r%   r,   r-   r(   r)   r*   r+   _reset_zoomr/   r#   r2   r3   )r   r   r   r%   r4   r5   r6   s          r   resetzZoomPage.reset   s     	 ,---$D -$$Y/////////-tT : :;;;;;;;;;""4(((((((((-sC 8 8999999999 -""9----------sC0H0H HIIIIIIIII  x F
 ()))M 	  	  	  Q L 444555	 s   DD 
E'%EEEc                    K   |j                             |           d {V }|                    dddi           d {V  d S )Nr>   r?   r   r@   )r   r%   rD   s      r   rH   zZoomPage._reset_zoom   sz      L0066666666 hh5s8
   	 	 	 	 	 	 	 	 	r   target_xtarget_yc                   K   |j         }|j        }|j        j                            ||d           d{V  t          j        t          j        dd                     d{V  | 	                    ||||          }t          |          }t          |          D ]\  }	\  }
}t          j        dd          }t          j        dd          }|
|z   }||z   }|j        j                            ||d           d{V  |	|z  }|dk     s|d	k    r|d
z  |z  t          j        dd          z  }n|d
z  |z  t          j        dd          z  }t          j        |           d{V  dS )a  
        Move mouse from current position to target with human-like curved path.

        Uses cubic Bezier curves with random control points to create natural-looking
        curved movements. Adds jitter and variable speed for realism.

        Args:
            managed_page (ManagedPage): Managed page object
            target_x (float): Target X coordinate
            target_y (float): Target Y coordinate
            slowmo (int): Base delay for timing
        r   )stepsNg{Gz?r   g      g      ?rG   r    r      r   )r0   r1   r%   mousemover(   r)   r*   r+   _generate_bezier_pathlen	enumerate)r   r   rK   rL   r   start_xstart_ypath_pointstotal_pointsir   r   jitter_xjitter_yfinal_xfinal_yprogressdelays                     r   r'   zZoomPage._move_mouse_human_like   s      11 %**7G1*EEEEEEEEEmFN466777777777 00'8XVV ;''";// 	' 	'IAv1~dC00H~dC00H(lG(lG #)..wq.IIIIIIIII <'H#~~C4v~a7K7KK 4v~c37O7OO-&&&&&&&&&&)	' 	'r      rU   rV   
num_pointsc                    t          j        ||z
  dz  ||z
  dz  z             }||z   dz  }||z   dz  }t          j        dd          }	||	z  }
||z
  }||z
  }t          j        ||          t           j        dz  z   }||dz  z   t          j        |          |
z  t          j        dd          z  z   }||dz  z   t          j        |          |
z  t          j        dd          z  z   }||dz  z   t          j        |          |
z  t          j        dd	          z  z   }||dz  z   t          j        |          |
z  t          j        dd	          z  z   }g }t          |          D ]}||d
z
  z  }d
|z
  dz  |z  dd
|z
  dz  z  |z  |z  z   dd
|z
  z  |dz  z  |z  z   |dz  |z  z   }d
|z
  dz  |z  dd
|z
  dz  z  |z  |z  z   dd
|z
  z  |dz  z  |z  z   |dz  |z  z   }|	                    ||f           |S )a  
        Generate a curved path using cubic Bezier curve with random control points.

        Args:
            start_x (float): Starting X coordinate
            start_y (float): Starting Y coordinate
            target_x (float): Target X coordinate
            target_y (float): Target Y coordinate
            num_points (int): Number of points along the curve (default: 30)

        Returns:
            List of (x, y) tuples representing the path
        rO   r   gffffff?gQ?r   r   gq=
ףp?g      g      r      )
mathsqrtr*   r+   atan2picossinrangeappend)r   rU   rV   rK   rL   ra   distancemid_xmid_ycurve_intensityoffsetdxdyanglecp1_xcp1_ycp2_xcp2_ypointsrY   tr   r   s                          r   rR   zZoomPage._generate_bezier_path   s   " 9h01478JQ7NNOO 8#q(8#q( !.t44O+ 
2r""TWq[0 "t)#dhuoo&>PSUXAYAY&YY"t)#dhuoo&>PSUXAYAY&YY "t)#dhuoo&>PTVZA[A[&[["t)#dhuoo&>PTVZA[A[&[[ z"" 	" 	"AZ!^$A
 Q
W$QUQJ"U*+QUad"U*+ 1x   Q
W$QUQJ"U*+QUad"U*+ 1x   MM1a&!!!!r   N)r   r   )r   )r`   )__name__
__module____qualname____doc__r   r   floatintr   r3   r   r7   r.   rE   rI   rH   r'   r   r   rR    r   r   r
   r
      s        + + +8  8 { 8 u 8  8 TW 8 eh 8 swx{  ~A  yA  tB 8  8  8  8 t@ @ @4  )  )  ) S ) 4PSUXPX> )  )  )  ) V  /' /'PU /'af /'ps /' /' /' /'f 13C CU CU C(-C9>C*-C7;E%,<O7PC C C C C Cr   r
   )r}   r(   r*   rd   typingr   r   r   r   patchright.async_apir   lib.page_managementr   r
   r   r   r   <module>r      s        ) ) ) ) ) ) ) ) ) ) ) ) % % % % % % + + + + + +Y Y Y Y Y Y Y Y Y Yr   