Orientation¶
The Orientation class represents device orientation information with support for orientation change event handling.
Dependencies¶
Orientation Class¶
Constructor¶
Parameters:
- angle: Orientation angle in degrees
- type: Orientation type from OrientationType enum
- on_change: Optional callback function for orientation changes
Properties¶
angle¶
The current orientation angle in degrees.
type¶
The orientation type (from OrientationType enum).
on_change (Property with Setter)¶
Callback function that gets called when orientation changes.
Getter: Returns the current change handler function.
Setter: Sets the orientation change event handler.
- Accepts None to remove the handler
- Validates that the value is callable
- Raises TypeError if value is not callable
Methods¶
The Orientation class primarily uses properties for data access and event handling.
Usage Example¶
from pyweber.models.window import Orientation
from pyweber.utils.types import OrientationType
# Define orientation change handler
def handle_orientation_change():
print("Device orientation changed!")
# Create orientation instance
orientation = Orientation(
angle=90,
type=OrientationType.landscape,
on_change=handle_orientation_change
)
# Access properties
print(orientation.angle) # 90
print(orientation.type) # OrientationType.landscape
# Update orientation change handler
def new_handler():
print("New orientation detected!")
orientation.on_change = new_handler
# Remove orientation change handler
orientation.on_change = None
Event Handling¶
Setting Event Handlers¶
# Method 1: During initialization
orientation = Orientation(
angle=0,
type=OrientationType.portrait,
on_change=my_callback
)
# Method 2: Using property setter
orientation.on_change = my_callback
# Method 3: Remove handler
orientation.on_change = None
Event Handler Requirements¶
- Must be a callable function
- Can accept no parameters (orientation data available through instance)
- Should handle orientation change logic
Error Handling¶
Type Validation¶
TypeError: Raised whenon_changeis set to a non-callable valueNonevalues are accepted to remove event handlers
Integration¶
The Orientation class is used by:
- Screen class for device screen orientation
- Window class for browser window orientation events
- Device orientation APIs for mobile web applications
Common Use Cases¶
- Responsive Design: Adjusting UI layout based on device orientation
- Game Development: Handling device rotation in web games
- Media Applications: Optimizing video/image display for orientation
- Mobile Web Apps: Providing native-like orientation handling
OrientationType Values¶
The orientation type typically includes values like:
- portrait: Device held vertically
- landscape: Device held horizontally
- portrait-primary: Primary portrait orientation
- portrait-secondary: Inverted portrait orientation
- landscape-primary: Primary landscape orientation
- landscape-secondary: Inverted landscape orientation
Note: Exact values depend on the OrientationType enum implementation.