gradum-kit - v0.1.0
    Preparing search index...

    Class GradumEventManager<ToolType>

    GradumEventManager

    Listens to native mouse, trackpad, touch, and keyboard input and turns it into Gradum's richer events — GradumEvent, GradumDragEvent, GradumKeyEvent, and GradumWheelEvent — so a click, a long press, and a drag arrive as distinct, named events rather than something each component has to derive itself. It also owns the current tool per ClickMode, and can map screen coordinates into document space for every event it fires.

    Most applications need only one, reached through GradumEventManager.instance.

    Type Parameters

    • ToolType extends string = string

      The union of tool names this manager recognizes.

    Hierarchy (View Summary)

    Index
    managers: GradumEventManager<string>[] = []

    Every manager that has been created, in creation order.

    The properties this manager was created with.

    defaultProperties: GradumEventManagerProperties = ...

    The MVC pieces and event-type switches a new manager starts with. Every event family is enabled by default; pass the matching EnabledGradumEventTypes flag to create to turn one off.

    keyOperator: GradumEventManagerKeyOperator
    wheelOperator: GradumEventManagerWheelOperator
    pointerOperator: GradumEventManagerPointerOperator
    dispatchOperator: GradumEventManagerDispatchOperator
    inputDevice: InputDevice

    The currently identified input device. It is not 100% accurate, especially when differentiating between mouse and trackpad.

    onInputDeviceChange: Delegate<(device: InputDevice) => void>

    Fired whenever the identified input device changes.

    currentClick: ClickMode

    The pointer button or input mode currently in use.

    currentKeys: string[]

    The keyboard keys currently held down.

    onToolChange: Delegate<(oldTool: Node, newTool: Node, type: ClickMode) => void>

    Fired when the tool held by a click mode changes, with the previous tool, the new tool, and the mode.

    authorizeEventScaling: boolean | (() => boolean)

    Whether events fired by this manager compute scaled positions. Assign a callback to decide per event.

    scaleEventPosition: (position: Point) => Point

    Converts a screen position into document space for every event this manager fires. Set it so events stay correct under a panned or zoomed canvas.

    moveThreshold: number

    How far, in pixels, a pointer must travel before the interaction counts as a drag rather than a click. Defaults to 10.

    longPressDuration: number

    How long, in milliseconds, a pointer must be held still before a long press fires. Defaults to 500.

    initialized: boolean

    Whether the element was initialized already or not.

    defaultFeedforwardProperties: GradumElementProperties

    The properties passed on to children created through feedforward, letting a parent seed its descendants with shared defaults.

    • get instance(): GradumEventManager

      Returns GradumEventManager

      The default manager. Creating one on first access, so reading this is always safe.

    • get allManagers(): GradumEventManager<string>[]

      Returns GradumEventManager<string>[]

      Every manager currently registered. Reading gives a copy, so mutating the result does not affect the registry; assign a new array to replace it.

    • set allManagers(managers: GradumEventManager<string>[]): void

      Parameters

      Returns void

    • get model(): GradumEventManagerModel

      Returns GradumEventManagerModel

      This manager's model, holding its live input state.

    • set keyEventsEnabled(value: boolean): void

      Parameters

      • value: boolean

      Returns void

      Whether keyboard input is listened to and turned into GradumKeyEvents. Setting it to false reverts key handling to the native event names.

    • set wheelEventsEnabled(value: boolean): void

      Parameters

      • value: boolean

      Returns void

      Whether wheel input is listened to and turned into GradumWheelEvents. Setting it to false reverts wheel handling to the native event names.

    • set moveEventsEnabled(value: boolean): void

      Parameters

      • value: boolean

      Returns void

      Whether pointer movement produces Gradum move events. Setting it to false reverts move handling to the native event names.

    • set mouseEventsEnabled(value: boolean): void

      Parameters

      • value: boolean

      Returns void

      Whether mouse input is processed. Setting it to false reverts mouse handling to the native event names.

    • set touchEventsEnabled(value: boolean): void

      Parameters

      • value: boolean

      Returns void

      Whether touch input is processed. Setting it to false reverts touch handling to the native event names.

    • set clickEventsEnabled(value: boolean): void

      Parameters

      • value: boolean

      Returns void

      Whether click, click start/end, and long-press events fire. Setting it to false reverts click handling to the native event names.

    • set dragEventsEnabled(value: boolean): void

      Parameters

      • value: boolean

      Returns void

      Whether drag and drag start/end events fire. Setting it to false reverts drag handling to the native event names.

    • get enabled(): boolean

      Returns boolean

      Whether the manager is processing input. Reading combines the manager's own setting with any active lock, so a lock can disable it without overwriting the underlying value; assigning changes only the manager's own setting.

    • set enabled(value: boolean): void

      Parameters

      • value: boolean

      Returns void

    • get preventDefaultWheel(): boolean

      Returns boolean

      Whether wheel input has its native default suppressed, blocking browser page zoom and scroll. Combines the manager's setting with any active lock, as GradumEventManager.enabled does.

    • set preventDefaultWheel(value: boolean): void

      Parameters

      • value: boolean

      Returns void

    • get preventDefaultMouse(): boolean

      Returns boolean

      Whether mouse input has its native default suppressed. Combines the manager's setting with any active lock, as GradumEventManager.enabled does.

    • set preventDefaultMouse(value: boolean): void

      Parameters

      • value: boolean

      Returns void

    • get preventDefaultTouch(): boolean

      Returns boolean

      Whether touch input has its native default suppressed, blocking native scrolling and pinch-zoom. Combines the manager's setting with any active lock, as GradumEventManager.enabled does.

    • set preventDefaultTouch(value: boolean): void

      Parameters

      • value: boolean

      Returns void

    • get preventDefaults(): boolean

      Returns boolean

      All three prevent-default settings at once. Note: the getter and setter are not symmetric — reading gives true when any of wheel, mouse, or touch is suppressed, while assigning sets all three to the given value.

    • set preventDefaults(value: boolean): void

      Parameters

      • value: boolean

      Returns void

    • get toolsArray(): Node[]

      Returns Node[]

      Every registered tool instance, across all tool names, flattened into one array.

    • Function

      initialize

      Returns void

      Start listening to pointer input on the document and clear any lock. Called automatically by the element lifecycle.

    • Function

      lock

      Parameters

      Returns void

      Temporarily override the manager's state on behalf of one node, for the duration of an interaction. Use it to impose settings mid-gesture — suppressing native touch scrolling while a drag is in flight, say — then call GradumEventManager.unlock to hand them back. Any existing lock is released first, so locks do not nest.

    • Function

      unlock

      Returns void

      Release the current lock, so the manager's own state applies again.

    • Function

      getCurrentTool

      Parameters

      • Optionalmode: ClickMode = ...

        The click mode to read. Defaults to the mode currently in use.

      Returns Node

      The tool held by that mode, or undefined if it holds none.

      Get the tool instance currently held by a click mode.

    • Function

      getCurrentTools

      Parameters

      • Optionalmode: ClickMode = ...

        The click mode to read. Defaults to the mode currently in use.

      Returns Node[]

      All instances of that tool, or an empty array if the mode holds none.

      Get every instance sharing the name of the tool currently held by a click mode. Use it when several elements — toolbar buttons in different places, say — represent the same tool.

    • Function

      getCurrentToolName

      Parameters

      • Optionalmode: ClickMode = ...

        The click mode to read. Defaults to the mode currently in use.

      Returns ToolType

      The tool's name, or undefined if the mode holds none.

      Get the name of the tool currently held by a click mode.

    • Function

      getToolName

      Parameters

      • tool: Node

        The tool instance to look up.

      Returns ToolType

      The registered name, or undefined if the node is not a registered tool.

      Get the name a tool instance is registered under.

    • Function

      getSimilarTools

      Parameters

      • tool: Node

        The tool instance to match against.

      Returns Node[]

      All instances sharing its name, or an empty array if it is not registered.

      Get every instance registered under the same name as the given tool, including the tool itself.

    • Function

      getToolsByName

      Parameters

      • name: ToolType

        The tool name to look up.

      Returns Node[]

      All instances registered under that name, or an empty array if there are none.

      Get every tool instance registered under a name.

    • Function

      getToolByName

      Parameters

      • name: ToolType

        The tool name to look up.

      • Optionalpredicate: (tool: Node) => boolean

        Chooses which instance to return. Without it, the first registered instance is returned.

      Returns Node

      The matching instance, or undefined if there is none.

      Get a single tool instance registered under a name. Pass a predicate to choose among several instances.

    • Function

      getToolsByKey

      Parameters

      • key: string

        The key the tool is mapped to.

      Returns Node[]

      All instances bound to that key, or an empty array if the key maps to nothing.

      Get every tool instance bound to a keyboard key.

    • Function

      getToolByKey

      Parameters

      • key: string

        The key the tool is mapped to.

      • Optionalpredicate: (tool: Element) => boolean

        Chooses which instance to return. Without it, the first one is returned.

      Returns Node

      The matching instance, or undefined if there is none.

      Get a single tool instance bound to a keyboard key. Pass a predicate to choose among several instances.

    • Function

      addTool

      Parameters

      • toolName: ToolType

        The name to register the instance under.

      • tool: Node

        The tool instance.

      • Optionalkey: string

        A keyboard key that selects this tool when pressed.

      Returns void

      Register a tool instance under a name, so the manager can make it current and find it again. Several instances may share one name.

    • Function

      setTool

      Parameters

      • tool: Node

        The tool instance to make current. Pass undefined to clear the mode.

      • type: ClickMode

        The click mode to bind the tool to.

      • Optionaloptions: SetToolOptions = {}

        Whether to select and activate the tool, and whether it also becomes the tool for ClickMode.none.

      Returns void

      Make a tool the current one for a click mode, so interactions in that mode are attributed to it. The previously held tool is deselected and deactivated first, and GradumEventManager.onToolChange fires once the swap is done. Passing a tool that is not registered with this manager does nothing.

    • Function

      setToolByKey

      Parameters

      • key: string

        The key whose tool should become current.

      Returns boolean

      Whether a tool was bound to that key and therefore set.

      Make the tool bound to a keyboard key current for ClickMode.key. The tool is activated but not visually selected.

    • Function

      setupCustomDispatcher

      Parameters

      • type: string

        The event type to dispatch.

      Returns void

      Start dispatching an additional event type through the Gradum two-pass dispatch, so tool behaviors and interactor listeners receive it like any built-in Gradum event. Registering the same type twice is a no-op.

    • Protected Function

      applyAndHookEvents

      Parameters

      • gradumEventNames: Record<string, string>

        The Gradum names for this family.

      • defaultEventNames: Record<string, string>

        The native names to fall back to.

      • applyGradumEvents: boolean

        Whether to use the Gradum names and hook the dispatcher, or revert to the native names and unhook it.

      Returns void

      Switch a family of events between its Gradum names and its native names, and hook or unhook the dispatcher for each. Backs the *EventsEnabled setters.

    • Function

      destroy

      Returns GradumEventManager<ToolType>

      Itself, allowing for method chaining.

      Shut the manager down: disable every event family, unhook its dispatchers, and clear the tool-change subscribers. Registered tools are left in place.

    • Function

      create

      Type Parameters

      Parameters

      • this: This
      • Optionalproperties: This["prototype"]["properties"] = {}

        Properties to set on the new instance.

      Returns This["prototype"]

      The created instance.

      Instantiate this class with the given properties. Defaults declared by every class in the inheritance chain are applied first, nearest ancestor last, so a subclass' defaultProperties win over its parent's. The return type follows the class it is called on, so a subclass gets its own type back.

    • Protected Function

      Parameters

      • properties: object

        Properties to set on the new instance, defaults already merged in.

      Returns object

      The created instance.

      customCreate

      The construction step behind create. Override it to change how instances of a class are built — to route through a factory, or to wrap the instance — while keeping the default-merging that create performs.

    • Function

      feedforward

      Parameters

      • Optionalproperties: FeedforwardProperties

        Properties to feed forward. Defaults to defaultFeedforwardProperties.

      Returns this

      Itself, allowing for method chaining.

      Push this element's feedforward properties down to its children, so newly added descendants pick up the same defaults.

    • Function

      clone

      Parameters

      Returns this

      The cloned element.

      Create a copy of this element. By default the copy carries the same properties and children but none of the bound listeners.