floaty

October 22, 2022

floaty

The floaty module provides functions to display custom hover windows on the screen, control the size and position of the hover window, etc.

The hover window is automatically closed when the script stops running, so to keep the hover window from being closed, an empty setInterval can be used, e.g.

setInterval(()=>{}, 1000);

floaty.window(layout)

  • layout {xml} | {View} The XML or View of the hover window interface

Specifies the layout of the hover window, creates and displays a hover window, and returns a FloatyWindow object.

The hover window comes with close, resize and reposition buttons, and can be shown or hidden by calling the setAdjustEnabled() function as needed.

Where the layout parameter can be an xml layout or a View, see the description of the ui module for more information.

Example.

var w = floaty.window(
    <frame gravity="center">
        <text id="text">Hover text</text
    </frame
);
setTimeout(()=>{
    w.close();
}, 2000);

This code will display hover text on the screen after it runs and will disappear after two seconds.

Also, since the script runs in a thread other than the UI thread, and all modifications to the control need to be performed in the UI thread, you need to use ui.run, for example:

ui.run(function(){
    w.text.setText("text");
});

For a description of the returned FloatyWindow object, see the FloatyWindow section below.

floaty.rawWindow(layout)

  • layout {xml} | {View} The XML or View of the hover window interface

Specifies the layout of the hover window, creating and displaying a raw hover window, returning a FloatyRawWindow object.

Unlike the floaty.window() function, this hover window does not add any additional facilities (e.g. resize, position buttons) and you can write any layout you need.

Moreover, the hover window supports full fullscreen and can override the status bar, so you can do applications like eye protection mode.

var w = floaty.rawWindow(
    <frame gravity="center">
        <text id="text">Hover text</text
    </frame
);

w.setPosition(500, 500);

setTimeout(()=>{
    w.close();
}, 2000);

This code will display hover text on the screen after it runs and will disappear after two seconds.

For a description of the returned FloatyRawWindow object, see the FloatyRawWindow section below.

floaty.closeAll()

Closes all hover windows of this script.

floaty.checkPermission()

  • Returns {boolean}

Returns whether or not the current application has hover window permissions. (no permission request action will be triggered)

floaty.requestPermission()

Jump to the system's hover permission request screen.

if (! $floaty.checkPermission()) {
    // No hover permission, prompt the user and jump the request
    toast("This script requires hover permission to display the hover window, please allow and rerun this script in a subsequent screen.") ;
    $floaty.requestPermission();
    exit();
} else {
    console.log('Hover window permission already exists');
}

Note that this function does not block execution and does not wait for hover permission to be granted.

FloatyWindow

The FloatyWindow object, which can be used to get the elements on the FloatyWindow interface via FloatyWindow.{id}. For example, if the id of a control on the window is aaa, then window.aaa will get the control, similar to ui.

window.setAdjustEnabled(enabled)

  • enabled {boolean} enable or disable hover window adjustment (size, position)

if enabled is true, display position- and size-adjustable markers in the top-left and top-right corners of the hover window, just like the console. If enabled is false, the above markers are hidden.

window.setPosition(x, y)

  • x {number} x
  • x {number} y

Set the position of the hover window.

window.getX()

Return the x coordinate of the hover window position.

window.getY()

Return the y coordinate of the hover window position.

window.setSize(width, height)

  • width {number} width
  • height {number} height

Set the width and height of the hover window.

window.getWidth()

Return the width of the hover window.

window.getHeight()

Return the height of the hover window.

window.close()

Close the hover window. If the hover window is already closed, this function will not perform any action.

The closed hover window cannot be displayed again.

window.exitOnClose()

Causes the hover window to automatically end the script when it is closed.

FloatyRawWindow

Raw hover window object that can be used to get the elements on the hover window interface via window.{id}. For example, if the id of a control on the hover window is aaa, then window.aaa will get that control, similar to ui.

window.setTouchable(touchable)

  • touchable {Boolean} whether touchable

Set whether the hover window is touchable or not. If true, the hover window will receive touch and click events and cannot be passed below the hover window; if false, touch and click events on the hover window will be passed directly below the hover window. For security reasons, touch events that are received by the hover window cannot be passed on to the lower level.

This feature can be used to create eye-protection mode scripts.

var w = floaty.rawWindow(
    <frame gravity="center" bg="#44ffcc00"/>
);

w.setSize(-1, -1);
w.setTouchable(false);

setTimeout(()=>{
    w.close();
}, 4000);

window.setPosition(x, y)

  • x {number} x
  • x {number} y

Set the hover window position.

window.getX()

Return the x coordinate of the hover window position.

window.getY()

Return the y coordinate of the hover window position.

window.setSize(width, height)

  • width {number} width
  • height {number} height

Set the width and height of the hover window.

In particular, if set to -1, it will fill the full screen; if set to -2, it will be based on the size of the hover window content. For example.

var w = floaty.rawWindow(
    <frame gravity="center" bg="#77ff0000">
        <text id="text">Hover text</text
    </frame
);

w.setSize(-1, -1);

setTimeout(()=>{
    w.close();
}, 2000);

window.getWidth()

Return the width of the hover window.

window.getHeight()

Return the height of the hover window.

window.close()

Close the hover window. If the hover window is already closed, this function will not perform any action.

The closed hover window cannot be displayed again.

window.exitOnClose()

Causes the hover window to automatically end the script when it is closed.

Last update:
Contributors: Bruce