|
1 | 1 | # WebUI X Events |
2 | 2 |
|
3 | | -Events to help you handle states and navigation. |
| 3 | +WebUI X offers a flexible event system that enables your modules to handle UI states, navigation, and custom actions. This system empowers developers to create interactive and responsive user experiences within their modules. |
4 | 4 |
|
5 | | -**Availability** |
| 5 | +## Availability |
6 | 6 |
|
7 | 7 | > [!IMPORTANT] |
8 | | -> This feature is in active development. |
| 8 | +> **WebUI X Events** are currently in active development. |
| 9 | +> Feature support varies by platform: |
| 10 | +> |
| 11 | +> - **MMRL**: Supported from `v33661` |
| 12 | +> - **KernelSU Next**: Not supported |
| 13 | +> - **WebUI X: Portable**: Not supported |
| 14 | +> - **SukiSU Ultra**: Not supported |
9 | 15 |
|
10 | | -- MMRL from `v33659` |
11 | | -- KernelSU Next ❌ |
12 | | -- WebUI X: Portable ❌ |
13 | | -- SukiSU Ultra ❌ |
| 16 | +## Enabling Event Handling |
14 | 17 |
|
15 | | -_Enable_ |
| 18 | +To enable event handling in your module, add the following options to your configuration file: |
16 | 19 |
|
17 | 20 | ```jsonc |
18 | 21 | { |
19 | | - // to enable `wxBack` |
20 | | - "backHandler": "js" |
| 22 | + // Enables default back button handling (ignored if `backInterceptor` is set) |
| 23 | + "backHandler": true, |
| 24 | + // Use JavaScript to control back button behavior |
| 25 | + "backInterceptor": "javascript" |
21 | 26 | } |
22 | 27 | ``` |
23 | 28 |
|
24 | | -```JavaScript |
25 | | -// Handle incoming messages and dispatch them as events on `window` |
26 | | -window.addEventListener("message", ({ data }) => { |
27 | | - console.log("Data received", data); |
28 | | - const event = new CustomEvent(data, { detail: data }); |
29 | | - window.dispatchEvent(event); |
| 29 | +- **`backHandler`**: Enables the default back button handler. |
| 30 | +- **`backInterceptor`**: Allows you to intercept and handle back events in JavaScript. If both are set, `backInterceptor` takes precedence. |
| 31 | + |
| 32 | +## Usage Example |
| 33 | + |
| 34 | +You can listen for events using the `WXEvent` API from the `webuix` package. Below are examples for handling the back button and custom pause actions: |
| 35 | + |
| 36 | +```javascript |
| 37 | +import { WXEvent } from "webuix"; |
| 38 | + |
| 39 | +// Initialize the event system (recommended for best compatibility) |
| 40 | +WXEvent.initialize(); |
| 41 | + |
| 42 | +// Handle back event for a specific element (e.g., appDetails) |
| 43 | +WXEvent.on(appDetails, "back", (event) => { |
| 44 | + if (appDetails.open) { |
| 45 | + event.stopImmediatePropagation(); |
| 46 | + appDetails.open = false; // Close the details panel |
| 47 | + } |
30 | 48 | }); |
31 | 49 |
|
32 | | -// Listen for "wxBack" event |
33 | | -window.addEventListener("wxBack", () => { |
34 | | - console.log("Called backEvent"); |
35 | | - const p = prompt("Type 'leave' to close"); |
| 50 | +// Handle back event at the window level |
| 51 | +WXEvent.on(window, "back", (event) => { |
| 52 | + const appDetails = document.getElementById("appDetails"); |
| 53 | + |
| 54 | + if (appDetails?.open) { |
| 55 | + appDetails.open = false; |
| 56 | + return; |
| 57 | + } |
36 | 58 |
|
37 | | - if (p === "leave") { |
38 | | - webui.exit(); |
39 | | - } else { |
40 | | - alert("Seems that you can't type bro"); |
41 | | - } |
| 59 | + if (confirm("Close the app?")) { |
| 60 | + webui.exit(); // Exit the WebUI app |
| 61 | + } |
42 | 62 | }); |
43 | 63 |
|
44 | | -// Listen for "wxPause" event |
45 | | -window.addEventListener("wxPause", () => { |
46 | | - console.log("Called resumeEvent"); |
47 | | - alert("Oh, hey! You're back?"); |
| 64 | +// Example: Listen for a custom pause event (replace with your own logic) |
| 65 | +WXEvent.on(window, "pause", (event) => { |
| 66 | + focusInput.focus(); |
| 67 | + statusEl.textContent = 'App paused - input focused'; |
| 68 | + statusEl.style.color = getCssVar('error'); |
48 | 69 | }); |
49 | 70 |
|
| 71 | +// Listen for resume events |
| 72 | +WXEvent.on(window, "resume", (event) => { |
| 73 | + statusEl.textContent = 'App resumed'; |
| 74 | + statusEl.style.color = getCssVar('success'); |
| 75 | + // Add any additional logic needed when the app resumes |
| 76 | +}); |
50 | 77 | ``` |
| 78 | +
|
| 79 | +### How It Works |
| 80 | +
|
| 81 | +- The first handler listens for the `back` event on a specific element (`appDetails`). If the panel is open, it closes it and prevents the event from propagating further. |
| 82 | +- The second handler listens for the `back` event on the `window`. If no panels are open, it prompts the user to confirm exiting the app. |
| 83 | +- The third handler demonstrates listening for a custom `pause` event, updating UI elements as needed. |
| 84 | +
|
| 85 | +--- |
| 86 | +
|
| 87 | +## Notes |
| 88 | +
|
| 89 | +- If both `backHandler` and `backInterceptor` are set, `backInterceptor` will override the default handler. |
| 90 | +- Use `event.stopImmediatePropagation()` to prevent other handlers from executing for the same event. |
| 91 | +- The event system is designed to give you fine-grained control over navigation and state transitions in your WebUI modules. |
0 commit comments