Skip to main content

Module controllers

Module controllers 

Source
Expand description

§Controllers

Reusable input controllers. A controller owns one input pattern (click, drag, scroll, hover, long-press, key), watches the raw Events a Widget receives, and reports high-level ControllerEvents, so a Widget never re-implements “press, track, release inside”, dragging thresholds, or press timing.

A Widget keeps its controllers in its WidgetCore with WidgetCore::add_controller. The default Widget::handle_event feeds every event to them and calls Widget::on_controller_event once per result:

use pushrod::controllers::{ControllerEvent, DragController};
use pushrod::event::{EventCtx, Propagation};
use pushrod::geometry::{point, size};
use pushrod::paint::{self, Painter};
use pushrod::widget::{Widget, WidgetCore};

pub struct Handle {
    core: WidgetCore,
}

impl Handle {
    pub fn new() -> Self {
        let mut core = WidgetCore::new(point(0, 0), size(10, 10));
        core.add_controller(DragController::new());
        Self { core }
    }
}

impl Widget for Handle {
    fn core(&self) -> &WidgetCore { &self.core }
    fn core_mut(&mut self) -> &mut WidgetCore { &mut self.core }
    fn paint(&self, p: &mut dyn Painter) { paint::fill(p, self.get_color()); }

    fn on_controller_event(&mut self, event: &ControllerEvent, _ctx: &mut EventCtx) -> Propagation {
        if let ControllerEvent::DragUpdated { offset, .. } = event {
            println!("dragged by {}, {}", offset.x, offset.y);
            return Propagation::Stop;
        }

        Propagation::Continue
    }
}

Controllers are plain state machines: they never touch the Widget, the clock, or SDL2, so they are tested by feeding events in. Time comes from Event::Tick only.

Structs§

ClickController
Reports clicks: press, track in and out of the Widget, release.
Controllers
The list of controllers a Widget owns.
DragController
Reports drags: a press followed by movement past a threshold.
HoverController
Reports the pointer entering and leaving the Widget.
KeyController
Reports key presses and releases, optionally only for chosen keys.
LongPressController
Reports a press held in place for a delay.
ScrollController
Reports wheel and trackpad scrolling.

Enums§

ControllerEvent
A high-level result reported by a controller.

Traits§

Controller
An input pattern that turns raw Events into ControllerEvents.