HTMLWindow - itzmetanjim/py-positron GitHub Wiki
The HTMLWindow class provides JavaScript window object functionality within PyPositron. It's available as ui.htmlwindow
in your main function and represents the browser window that contains your HTML document. This class provides access to browser APIs, dialog methods, timing functions, and window management capabilities.
The HTMLWindow class is your gateway to browser-specific functionality in PyPositron. It allows you to:
- Display dialog boxes (alert, confirm, prompt)
- Manage timers and animations
- Control window properties and behavior
- Access browser objects (navigator, location, history)
- Handle window events and navigation
-
closed -> bool
: Returns true if a window is closed. W3Schools Reference | MDN Reference -
name -> str
: Sets or returns the name of a window. W3Schools Reference | MDN Reference
-
innerHeight -> int
: Returns the height of the window's content area (viewport) including scrollbars. W3Schools Reference | MDN Reference -
innerWidth -> int
: Returns the width of a window's content area (viewport) including scrollbars. W3Schools Reference | MDN Reference -
outerHeight -> int
: Returns the height of the browser window, including toolbars/scrollbars. W3Schools Reference | MDN Reference -
outerWidth -> int
: Returns the width of the browser window, including toolbars/scrollbars. W3Schools Reference | MDN Reference
-
screenLeft -> int
: Returns the horizontal coordinate of the window relative to the screen. W3Schools Reference | MDN Reference -
screenTop -> int
: Returns the vertical coordinate of the window relative to the screen. W3Schools Reference | MDN Reference -
screenX -> int
: Returns the horizontal coordinate of the window relative to the screen. W3Schools Reference | MDN Reference -
screenY -> int
: Returns the vertical coordinate of the window relative to the screen. W3Schools Reference | MDN Reference
-
pageXOffset -> int
: Returns the pixels the current document has been scrolled (horizontally) from the upper left corner of the window. W3Schools Reference | MDN Reference -
pageYOffset -> int
: Returns the pixels the current document has been scrolled (vertically) from the upper left corner of the window. W3Schools Reference | MDN Reference -
scrollX -> int
: An alias of pageXOffset. W3Schools Reference | MDN Reference -
scrollY -> int
: An alias of pageYOffset. W3Schools Reference | MDN Reference
-
console -> Console
: Returns the Console object for the window. W3Schools Reference | MDN Reference -
document -> Document
: Returns the Document object for the window. W3Schools Reference | MDN Reference -
history -> History
: Returns the History object for the window. W3Schools Reference | MDN Reference -
location -> Location
: Returns the Location object for the window. W3Schools Reference | MDN Reference -
navigator -> Navigator
: Returns the Navigator object for the window. W3Schools Reference | MDN Reference -
screen -> Screen
: Returns the Screen object for the window. W3Schools Reference | MDN Reference
-
frameElement
: Returns the frame in which the window runs. W3Schools Reference | MDN Reference -
frames
: Returns all window objects running in the window. W3Schools Reference | MDN Reference -
length -> int
: Returns the number of<iframe>
elements in the current window. W3Schools Reference | MDN Reference -
opener
: Returns a reference to the window that created the window. W3Schools Reference | MDN Reference -
parent
: Returns the parent window of the current window. W3Schools Reference | MDN Reference -
self
: Returns the current window. W3Schools Reference | MDN Reference -
top
: Returns the topmost browser window. W3Schools Reference | MDN Reference
-
localStorage
: Allows to save key/value pairs in a web browser. Stores the data with no expiration date. W3Schools Reference | MDN Reference -
sessionStorage
: Allows to save key/value pairs in a web browser. Stores the data for one session. W3Schools Reference | MDN Reference
-
alert(message: str)
: Displays an alert box with a message and an OK button. W3Schools Reference | MDN Reference -
confirm(message: str) -> bool
: Displays a dialog box with a message and an OK and a Cancel button. Returns True if OK is clicked, False if Cancel is clicked. W3Schools Reference | MDN Reference -
prompt(message: str, default_value: str = '') -> str
: Displays a dialog box that prompts the visitor for input. Returns the input string or None if cancelled. W3Schools Reference | MDN Reference
-
setTimeout(callback, milliseconds: int) -> int
: Calls a function or evaluates an expression after a specified number of milliseconds. Returns a timer ID. W3Schools Reference | MDN Reference -
clearTimeout(timeout_id: int)
: Clears a timer set with setTimeout(). W3Schools Reference | MDN Reference -
setInterval(callback, milliseconds: int) -> int
: Calls a function or evaluates an expression at specified intervals (in milliseconds). Returns an interval ID. W3Schools Reference | MDN Reference -
clearInterval(interval_id: int)
: Clears a timer set with setInterval(). W3Schools Reference | MDN Reference
-
requestAnimationFrame(callback) -> int
: Requests the browser to call a function to update an animation before the next repaint. W3Schools Reference | MDN Reference
-
open(url: str = '', name: str = '_blank', specs: str = '', replace: bool = False)
: Opens a new browser window. W3Schools Reference | MDN Reference -
close()
: Closes the current window. W3Schools Reference | MDN Reference -
focus()
: Sets focus to the current window. W3Schools Reference | MDN Reference -
blur()
: Removes focus from the current window. W3Schools Reference | MDN Reference
-
moveTo(x: int, y: int)
: Moves a window to the specified position. W3Schools Reference | MDN Reference -
moveBy(x: int, y: int)
: Moves a window relative to its current position. W3Schools Reference | MDN Reference -
resizeTo(width: int, height: int)
: Resizes the window to the specified width and height. W3Schools Reference | MDN Reference -
resizeBy(width: int, height: int)
: Resizes the window by the specified pixels. W3Schools Reference | MDN Reference
-
scrollTo(x: int, y: int)
: Scrolls the document to the specified coordinates. W3Schools Reference | MDN Reference -
scrollBy(x: int, y: int)
: Scrolls the document by the specified number of pixels. W3Schools Reference | MDN Reference
-
addEventListener(event_type: str, callback: Callable) -> bool
: Attaches an event handler to the window. W3Schools Reference | MDN Reference -
removeEventListener(event_type: str, callback=None)
: Removes an event handler from the window. W3Schools Reference | MDN Reference
-
print()
: Prints the content of the current window. W3Schools Reference | MDN Reference -
stop()
: Stops the window from loading. W3Schools Reference | MDN Reference
-
getComputedStyle(element: Element, pseudo_element: str = None)
: Gets the current computed CSS styles applied to an element. W3Schools Reference | MDN Reference -
matchMedia(media_query: str)
: Returns a MediaQueryList object representing the specified CSS media query string. W3Schools Reference | MDN Reference
-
getSelection()
: Returns a Selection object representing the range of text selected by the user. W3Schools Reference | MDN Reference -
atob(encoded_string: str) -> str
: Decodes a base-64 encoded string. W3Schools Reference | MDN Reference -
btoa(string: str) -> str
: Encodes a string in base-64. W3Schools Reference | MDN Reference
def main(ui):
def show_welcome():
ui.htmlwindow.alert("Welcome to PyPositron!")
if ui.htmlwindow.confirm("Would you like to continue?"):
name = ui.htmlwindow.prompt("What's your name?", "User")
if name:
ui.htmlwindow.alert(f"Hello, {name}!")
return name
return None
# Trigger welcome dialog
welcome_btn = ui.document.getElementById("welcomeBtn")
if welcome_btn:
welcome_btn.addEventListener("click", show_welcome)
def main(ui):
status_div = ui.document.getElementById("status")
def show_message(text, duration=3000):
status_div.innerText = text
status_div.style.opacity = "1"
# Auto-hide after duration
def hide_message():
status_div.style.opacity = "0"
ui.htmlwindow.setTimeout(hide_message, duration)
def start_countdown():
count = 5
countdown_display = ui.document.getElementById("countdown")
def update_countdown():
nonlocal count
if count > 0:
countdown_display.innerText = f"Starting in {count}..."
count -= 1
ui.htmlwindow.setTimeout(update_countdown, 1000)
else:
countdown_display.innerText = "Started!"
show_message("Process completed!", 2000)
update_countdown()
start_btn = ui.document.getElementById("startBtn")
if start_btn:
start_btn.addEventListener("click", start_countdown)
def main(ui):
editor = ui.document.getElementById("editor")
save_status = ui.document.getElementById("saveStatus")
auto_save_interval = None
def save_content():
content = editor.value
# Simulate saving to localStorage
ui.htmlwindow.localStorage.setItem("draft", content)
save_status.innerText = "Saved"
save_status.style.color = "green"
# Clear status after 2 seconds
ui.htmlwindow.setTimeout(lambda: setattr(save_status, 'innerText', ''), 2000)
def start_auto_save():
nonlocal auto_save_interval
# Save every 30 seconds
auto_save_interval = ui.htmlwindow.setInterval(save_content, 30000)
save_status.innerText = "Auto-save enabled"
def stop_auto_save():
nonlocal auto_save_interval
if auto_save_interval:
ui.htmlwindow.clearInterval(auto_save_interval)
auto_save_interval = None
save_status.innerText = "Auto-save disabled"
# Load saved content on startup
if editor:
saved_content = ui.htmlwindow.localStorage.getItem("draft")
if saved_content:
editor.value = saved_content
# Start auto-save when user types
def on_input():
if not auto_save_interval:
start_auto_save()
editor.addEventListener("input", on_input)
# Manual save button
save_btn = ui.document.getElementById("saveBtn")
if save_btn:
save_btn.addEventListener("click", save_content)
def main(ui):
def update_window_info():
info_div = ui.document.getElementById("windowInfo")
info_html = f"""
<h3>Window Information</h3>
<p><strong>Inner Size:</strong> {ui.htmlwindow.innerWidth} x {ui.htmlwindow.innerHeight}</p>
<p><strong>Outer Size:</strong> {ui.htmlwindow.outerWidth} x {ui.htmlwindow.outerHeight}</p>
<p><strong>Screen Position:</strong> ({ui.htmlwindow.screenX}, {ui.htmlwindow.screenY})</p>
<p><strong>Scroll Position:</strong> ({ui.htmlwindow.scrollX}, {ui.htmlwindow.scrollY})</p>
<p><strong>User Agent:</strong> {ui.htmlwindow.navigator.userAgent}</p>
<p><strong>Online:</strong> {'Yes' if ui.htmlwindow.navigator.onLine else 'No'}</p>
"""
info_div.innerHTML = info_html
# Update on window resize
ui.htmlwindow.addEventListener("resize", update_window_info)
ui.htmlwindow.addEventListener("scroll", update_window_info)
# Initial update
update_window_info()
# Refresh button
refresh_btn = ui.document.getElementById("refreshInfo")
if refresh_btn:
refresh_btn.addEventListener("click", update_window_info)
def main(ui):
def smooth_scroll_to_top():
def scroll_step():
current_scroll = ui.htmlwindow.scrollY
if current_scroll > 0:
ui.htmlwindow.scrollTo(0, current_scroll - 50)
ui.htmlwindow.requestAnimationFrame(scroll_step)
scroll_step()
def scroll_to_element(element_id):
element = ui.document.getElementById(element_id)
if element:
element.scrollIntoView(True)
# Scroll to top button
top_btn = ui.document.getElementById("scrollTopBtn")
if top_btn:
top_btn.addEventListener("click", smooth_scroll_to_top)
# Navigation links
nav_links = ui.document.getElementsByClassName("nav-link")
for link in nav_links:
target_id = link.getAttribute("data-target")
if target_id:
link.addEventListener("click", lambda: scroll_to_element(target_id))
def main(ui):
def check_responsive_breakpoint():
width = ui.htmlwindow.innerWidth
if width >= 1200:
return "xl"
elif width >= 992:
return "lg"
elif width >= 768:
return "md"
elif width >= 576:
return "sm"
else:
return "xs"
def apply_responsive_layout():
breakpoint = check_responsive_breakpoint()
body = ui.document.body
# Remove existing breakpoint classes
class_list = body.className.split()
class_list = [cls for cls in class_list if not cls.startswith("bp-")]
# Add current breakpoint class
class_list.append(f"bp-{breakpoint}")
body.className = " ".join(class_list)
# Show current breakpoint
breakpoint_display = ui.document.getElementById("currentBreakpoint")
if breakpoint_display:
breakpoint_display.innerText = f"Current breakpoint: {breakpoint}"
# Apply layout on resize
ui.htmlwindow.addEventListener("resize", apply_responsive_layout)
# Initial layout
apply_responsive_layout()
# Media query matching example
def setup_media_queries():
mobile_query = ui.htmlwindow.matchMedia("(max-width: 767px)")
def handle_mobile_change(mql):
mobile_nav = ui.document.getElementById("mobileNav")
desktop_nav = ui.document.getElementById("desktopNav")
if mql.matches:
# Mobile view
if mobile_nav:
mobile_nav.style.display = "block"
if desktop_nav:
desktop_nav.style.display = "none"
else:
# Desktop view
if mobile_nav:
mobile_nav.style.display = "none"
if desktop_nav:
desktop_nav.style.display = "block"
mobile_query.addEventListener("change", handle_mobile_change)
handle_mobile_change(mobile_query) # Initial check
setup_media_queries()
def main(ui):
def prepare_for_print():
# Hide non-printable elements
no_print_elements = ui.document.getElementsByClassName("no-print")
for element in no_print_elements:
element.style.display = "none"
# Add print-specific styling
print_style = ui.document.createElement("style")
print_style.innerHTML = """
@media print {
body { font-size: 12pt; }
.print-only { display: block !important; }
.no-print { display: none !important; }
}
"""
ui.document.head.appendChild(print_style)
def print_page():
prepare_for_print()
ui.htmlwindow.print()
print_btn = ui.document.getElementById("printBtn")
if print_btn:
print_btn.addEventListener("click", print_page)
-
Handle dialog return values: Always check return values from
confirm()
andprompt()
methods. -
Clean up timers: Always clear intervals and timeouts when they're no longer needed to prevent memory leaks.
-
Use requestAnimationFrame for animations: For smooth animations, prefer
requestAnimationFrame()
oversetTimeout()
. -
Check window properties safely: Some window properties may not be available in all contexts.
-
Responsive design: Use window dimension properties and media queries to create responsive layouts.
-
Save timer IDs: Store timer IDs returned by
setTimeout()
andsetInterval()
so you can clear them later.
# Good practice example
def main(ui):
timer_id = None
def start_timer():
nonlocal timer_id
if timer_id:
ui.htmlwindow.clearInterval(timer_id)
timer_id = ui.htmlwindow.setInterval(update_function, 1000)
def stop_timer():
nonlocal timer_id
if timer_id:
ui.htmlwindow.clearInterval(timer_id)
timer_id = None
You can listen for these events on the window object:
-
"resize"
: Window is resized -
"scroll"
: Window is scrolled -
"beforeunload"
: Before the window unloads -
"load"
: Window has finished loading -
"focus"
: Window gains focus -
"blur"
: Window loses focus