This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
from .block import Block, BlockType, Paragraph, Heading, HeadingLevel, Quote, CodeBlock
|
||||
from .block import HList, ListItem, ListStyle, Table, TableRow, TableCell
|
||||
from .block import HorizontalRule, Image
|
||||
from .interactive_image import InteractiveImage
|
||||
from .inline import Word, FormattedSpan, LineBreak
|
||||
from .document import Document, MetadataType, Chapter, Book
|
||||
from .functional import Link, LinkType, Button, Form, FormField, FormFieldType
|
||||
|
||||
@@ -61,18 +61,21 @@ class Link(Interactable):
|
||||
"""Get the title/tooltip for this link"""
|
||||
return self._title
|
||||
|
||||
def execute(self) -> Any:
|
||||
def execute(self, point=None) -> Any:
|
||||
"""
|
||||
Execute the link action based on its type.
|
||||
|
||||
|
||||
For internal and external links, returns the location.
|
||||
For API and function links, executes the callback with the provided parameters.
|
||||
|
||||
|
||||
Args:
|
||||
point: Optional interaction point passed from the interact() method
|
||||
|
||||
Returns:
|
||||
The result of the link execution, which depends on the link type.
|
||||
"""
|
||||
if self._link_type in (LinkType.API, LinkType.FUNCTION) and self._callback:
|
||||
return self._callback(self._location, **self._params)
|
||||
return self._callback(self._location, point, **self._params)
|
||||
else:
|
||||
# For INTERNAL and EXTERNAL links, return the location
|
||||
# The renderer/browser will handle the navigation
|
||||
@@ -129,15 +132,18 @@ class Button(Interactable):
|
||||
"""Get the button parameters"""
|
||||
return self._params
|
||||
|
||||
def execute(self) -> Any:
|
||||
def execute(self, point=None) -> Any:
|
||||
"""
|
||||
Execute the button's callback function if the button is enabled.
|
||||
|
||||
|
||||
Args:
|
||||
point: Optional interaction point passed from the interact() method
|
||||
|
||||
Returns:
|
||||
The result of the callback function, or None if the button is disabled.
|
||||
"""
|
||||
if self._enabled and self._callback:
|
||||
return self._callback(**self._params)
|
||||
return self._callback(point, **self._params)
|
||||
return None
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,154 @@
|
||||
"""
|
||||
Interactive and queryable image for pyWebLayout.
|
||||
|
||||
Provides an InteractiveImage class that combines Image with Interactable
|
||||
and Queriable capabilities, allowing images to respond to tap events with
|
||||
proper bounding box detection.
|
||||
"""
|
||||
|
||||
from typing import Optional, Callable, Tuple
|
||||
import numpy as np
|
||||
|
||||
from .block import Image, BlockType
|
||||
from ..core.base import Interactable, Queriable
|
||||
|
||||
|
||||
class InteractiveImage(Image, Interactable, Queriable):
|
||||
"""
|
||||
An image that can be interacted with and queried for hit detection.
|
||||
|
||||
This combines pyWebLayout's Image block with Interactable and Queriable
|
||||
capabilities, allowing the image to:
|
||||
- Have a callback that fires when tapped
|
||||
- Know its rendered position (origin)
|
||||
- Detect if a point is within its bounds
|
||||
|
||||
Example:
|
||||
>>> img = InteractiveImage(
|
||||
... source="cover.png",
|
||||
... alt_text="Book Title",
|
||||
... callback=lambda point: "/path/to/book.epub"
|
||||
... )
|
||||
>>> # After rendering, origin is set automatically
|
||||
>>> # Check if tap is inside
|
||||
>>> result = img.interact((120, 250))
|
||||
>>> # Returns "/path/to/book.epub" if inside, None if outside
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
source: str = "",
|
||||
alt_text: str = "",
|
||||
width: Optional[int] = None,
|
||||
height: Optional[int] = None,
|
||||
callback: Optional[Callable] = None
|
||||
):
|
||||
"""
|
||||
Initialize an interactive image.
|
||||
|
||||
Args:
|
||||
source: The image source URL or path
|
||||
alt_text: Alternative text for accessibility
|
||||
width: Optional image width in pixels
|
||||
height: Optional image height in pixels
|
||||
callback: Function to call when image is tapped (receives point coordinates)
|
||||
"""
|
||||
# Initialize Image
|
||||
Image.__init__(self, source=source, alt_text=alt_text, width=width, height=height)
|
||||
|
||||
# Initialize Interactable
|
||||
Interactable.__init__(self, callback=callback)
|
||||
|
||||
# Initialize position tracking
|
||||
self._origin = np.array([0, 0]) # Will be set during rendering
|
||||
self.size = (width or 0, height or 0) # Will be updated during rendering
|
||||
|
||||
def interact(self, point: np.generic) -> Optional[any]:
|
||||
"""
|
||||
Handle interaction at the given point.
|
||||
|
||||
Only triggers the callback if the point is within the image bounds.
|
||||
|
||||
Args:
|
||||
point: The coordinates of the interaction (x, y)
|
||||
|
||||
Returns:
|
||||
The result of the callback if point is inside, None otherwise
|
||||
"""
|
||||
# Check if point is inside this image
|
||||
if self.in_object(point):
|
||||
# Point is inside, trigger callback
|
||||
if self._callback is not None:
|
||||
return self._callback(point)
|
||||
|
||||
return None
|
||||
|
||||
def in_object(self, point: np.generic) -> bool:
|
||||
"""
|
||||
Check if a point is within the image bounds.
|
||||
|
||||
Args:
|
||||
point: The coordinates to check (x, y)
|
||||
|
||||
Returns:
|
||||
True if point is inside the image, False otherwise
|
||||
"""
|
||||
point_array = np.array(point)
|
||||
relative_point = point_array - self._origin
|
||||
return np.all((0 <= relative_point) & (relative_point < self.size))
|
||||
|
||||
@classmethod
|
||||
def create_and_add_to(
|
||||
cls,
|
||||
parent,
|
||||
source: str,
|
||||
alt_text: str = "",
|
||||
width: Optional[int] = None,
|
||||
height: Optional[int] = None,
|
||||
callback: Optional[Callable] = None
|
||||
) -> 'InteractiveImage':
|
||||
"""
|
||||
Create an interactive image and add it to a parent block.
|
||||
|
||||
This is a convenience method that mimics the Image.create_and_add_to API
|
||||
but creates an InteractiveImage instead.
|
||||
|
||||
Args:
|
||||
parent: Parent block to add this image to
|
||||
source: The image source URL or path
|
||||
alt_text: Alternative text for accessibility
|
||||
width: Optional image width in pixels
|
||||
height: Optional image height in pixels
|
||||
callback: Function to call when image is tapped
|
||||
|
||||
Returns:
|
||||
The created InteractiveImage instance
|
||||
"""
|
||||
img = cls(
|
||||
source=source,
|
||||
alt_text=alt_text,
|
||||
width=width,
|
||||
height=height,
|
||||
callback=callback
|
||||
)
|
||||
|
||||
# Add to parent's children
|
||||
if hasattr(parent, 'add_child'):
|
||||
parent.add_child(img)
|
||||
elif hasattr(parent, '_children'):
|
||||
parent._children.append(img)
|
||||
|
||||
return img
|
||||
|
||||
def set_rendered_bounds(self, origin: Tuple[int, int], size: Tuple[int, int]):
|
||||
"""
|
||||
Set the rendered position and size of this image.
|
||||
|
||||
This should be called by the renderer after it places the image.
|
||||
|
||||
Args:
|
||||
origin: (x, y) coordinates of top-left corner
|
||||
size: (width, height) of the rendered image
|
||||
"""
|
||||
self._origin = np.array(origin)
|
||||
self.size = size
|
||||
@@ -62,21 +62,7 @@ class LinkText(Text, Interactable, Queriable):
|
||||
"""Set the hover state for visual feedback"""
|
||||
self._hovered = hovered
|
||||
|
||||
def interact(self, point: np.generic):
|
||||
"""
|
||||
Handle interaction at the given point.
|
||||
Override to call the callback without passing the point.
|
||||
|
||||
Args:
|
||||
point: The coordinates of the interaction
|
||||
|
||||
Returns:
|
||||
The result of calling the callback function
|
||||
"""
|
||||
if self._callback is None:
|
||||
return None
|
||||
return self._callback() # Don't pass the point to the callback
|
||||
|
||||
|
||||
def render(self, next_text: Optional['Text'] = None, spacing: int = 0):
|
||||
"""
|
||||
Render the link text with optional hover effects.
|
||||
@@ -165,21 +151,7 @@ class ButtonText(Text, Interactable, Queriable):
|
||||
"""Set the hover state"""
|
||||
self._hovered = hovered
|
||||
|
||||
def interact(self, point: np.generic):
|
||||
"""
|
||||
Handle interaction at the given point.
|
||||
Override to call the callback without passing the point.
|
||||
|
||||
Args:
|
||||
point: The coordinates of the interaction
|
||||
|
||||
Returns:
|
||||
The result of calling the callback function
|
||||
"""
|
||||
if self._callback is None:
|
||||
return None
|
||||
return self._callback() # Don't pass the point to the callback
|
||||
|
||||
|
||||
def render(self):
|
||||
"""
|
||||
Render the button with background, border, and text.
|
||||
|
||||
Reference in New Issue
Block a user