[][src]Trait pugl_ui::widget::Widget

pub trait Widget: DowncastSync {
    fn stub(&self) -> &WidgetStub;
fn stub_mut(&mut self) -> &mut WidgetStub; fn event(&mut self, ev: Event) -> Option<Event> { ... }
fn exposed(&mut self, _expose: &ExposeArea, _cr: &Context) { ... }
fn min_size(&self) -> Size { ... }
fn width_expandable(&self) -> bool { ... }
fn height_expandable(&self) -> bool { ... }
fn takes_focus(&self) -> bool { ... }
fn pointer_enter(&mut self) { ... }
fn pointer_leave(&mut self) { ... }
fn reminder_handler(&mut self) -> bool { ... }
fn ask_for_repaint(&mut self) { ... }
fn request_reminder(&mut self, timeout: f64) { ... }
fn reminder_request(&mut self) -> Option<f64> { ... }
fn has_focus(&self) -> bool { ... }
fn size(&self) -> Size { ... }
fn pos(&self) -> Coord { ... }
fn geometry(&self) -> (f64, f64, f64, f64, f64, f64) { ... }
fn rect(&self) -> (f64, f64, f64, f64) { ... }
fn sized_width(&self) -> bool { ... }
fn sized_height(&self) -> bool { ... }
fn set_width(&mut self, width: f64) { ... }
fn set_height(&mut self, height: f64) { ... }
fn expand_width(&mut self, amount: f64) { ... }
fn expand_height(&mut self, amount: f64) { ... }
fn set_pos(&mut self, pos: &Coord) { ... }
fn set_size(&mut self, size: &Size) { ... }
fn layout(&self) -> Layout { ... }
fn set_layout(&mut self, layout: &Layout) { ... }
fn is_sensitive(&self) -> bool { ... }
fn is_hovered(&self) -> bool { ... }
fn is_hit_by(&self, pos: Coord) -> bool { ... }
fn intersects_with(&self, pos: Coord, size: Size) -> bool { ... }
fn set_focus(&mut self, yn: bool) { ... }
fn needs_repaint(&mut self) -> bool { ... }
fn pointer_enter_wrap(&mut self) { ... }
fn pointer_leave_wrap(&mut self) { ... } }

The Widget trait.

Widgets need to implement this trait. Most of the methods have default implementations, so that simple widgets can be easily defined. Eeven layouts are internally treated as widgets.

Data common to all widgets is kept in the struct WidgetStub accessible from the widget by the methods stub() and stub_mut().

Required methods

fn stub(&self) -> &WidgetStub

Supposed to return a reference to the WidgetStub of the widget

usually implemented by the macro widget_stub!().

fn stub_mut(&mut self) -> &mut WidgetStub

Supposed to return a mutable reference to the WidgetStub of the widget.

Usually implemented by the macro widget_stub!().

Loading content...

Provided methods

fn event(&mut self, ev: Event) -> Option<Event>

Called by the UI to pass an event to the widget.

The widget is supposed to process the Event and return None if the widget has processed the event. If the widget has not processed the event it shoud return Some(ev) so that the event can be passed to its parent widget.

There is EventState and the macros event_processed!() and event_not_processed!() to do this.

The default implementation just passes the event without touching it.

let ev = Event {
    data: EventType::MouseButtonPress(MouseButton {
        num: 1,
        modifiers: Modifiers::default()
    }),
    context: Default::default()
};
let mut widget = DummyWidget::default();

assert_eq!(widget.event(ev), Some(ev));

fn exposed(&mut self, _expose: &ExposeArea, _cr: &Context)

Called when the widget has to draw itself.

Parameters

  • expose: &ExposeArea – a pugl_sys::pugl::ExposeArea carrying the information which rectangle of the widget actually needs to be redrawn.

Default implementation does nothing.

fn min_size(&self) -> Size

Supposed to return the minimum size of the widget.

Default: zero size

fn width_expandable(&self) -> bool

Suposed to return true iff the widget is expandable in x-direction

Default: false

fn height_expandable(&self) -> bool

Suposed to return true iff the widget is expandable in y-direction

Default: false

fn takes_focus(&self) -> bool

Supposed to return true iff the widget can take the focus.

Default: false

fn pointer_enter(&mut self)

Called when the mouse pointer is entering the widget's layout.

Default implementation does nothing.

fn pointer_leave(&mut self)

Called when the mouse pointer is leaving the widget's layout.

Default implementation does nothing.

fn reminder_handler(&mut self) -> bool

Called when the requested reminding time is passed

Supposed to return true, iff the reminder is still needed

Default implementation does nothing and returns false.

fn ask_for_repaint(&mut self)

fn request_reminder(&mut self, timeout: f64)

The widget can request a reminder after timeout seconds. When the time has passed `reminder_handler() is called.

Usually not to be reimplemented.

let mut widget = DummyWidget::default();
widget.request_reminder(5.0);
assert_eq!(widget.reminder_request(), Some(5.0));

fn reminder_request(&mut self) -> Option<f64>

Hands the reminder request over to the UI

Only to be called by the UI as it consumes the reminder request. Usually not to be reimplemented.

let mut widget = DummyWidget::default();
assert_eq!(widget.reminder_request(), None);
widget.request_reminder(5.0);
assert_eq!(widget.reminder_request(), Some(5.0));
assert_eq!(widget.reminder_request(), None);

fn has_focus(&self) -> bool

Returns true iff the widget is currently focused.

Usually not to be reimplemented.

let mut widget = DummyWidget::default();
assert!(!widget.has_focus());
widget.set_focus(true);
assert!(widget.has_focus());
widget.set_focus(false);
assert!(!widget.has_focus());

fn size(&self) -> Size

Returns the size of the widget after layouting.

Usually not to be reimplemented.

let mut widget = DummyWidget::default();
let layout = Layout {
    pos: Coord { x: 23., y: 42. },
    size: Size { w: 137., h: 93. }
};
widget.set_layout(&layout);
assert_eq!(widget.size(), Size { w: 137., h: 93.});

fn pos(&self) -> Coord

Returns the positon (upper left corner of the widget)

Usually not to be reimplemented.

let mut widget = DummyWidget::default();
let layout = Layout {
    pos: Coord { x: 23., y: 42. },
    size: Size { w: 137., h: 93. }
};
widget.set_layout(&layout);
assert_eq!(widget.pos(), Coord { x: 23., y: 42. });

fn geometry(&self) -> (f64, f64, f64, f64, f64, f64)

Returns the six scalar values to conveniently describe the widget's geometry (left, right, top, bottom, width, height)

Usually not to be reimplemented

Useful in implementations of ::exposed() when used as let (left, right, top, bottom, width, height) = self.geometry();

let mut widget = DummyWidget::default();
let layout = Layout {
    pos: Coord { x: 23., y: 42. },
    size: Size { w: 137., h: 93. }
};
widget.set_layout(&layout);
assert_eq!(widget.geometry(), (23., 160., 42., 135., 137., 93.));

fn rect(&self) -> (f64, f64, f64, f64)

Returns four scalar values to conveniently describe the widget's rectangle (x, y, width, height)

Usually not to be reimplemented

Useful in implementations of ::exposed() when used as let (x, y, w, h) = self.rect();

let mut widget = DummyWidget::default();
let layout = Layout {
    pos: Coord { x: 23., y: 42. },
    size: Size { w: 137., h: 93. }
};
widget.set_layout(&layout);
assert_eq!(widget.rect(), (23., 42., 137., 93.));

fn sized_width(&self) -> bool

Returns true iff the widget has a defined minimum width

Usually not to be reimplemented.

fn sized_height(&self) -> bool

Returns true iff the widget has a defined minimum height

Usually not to be reimplemented.

fn set_width(&mut self, width: f64)

Sets the actual width of the widget to width.

Usually called by the layouter. Usually not to be reimplemented.

let mut widget = DummyWidget::default();
widget.set_width(23.);
assert_eq!(widget.size().w, 23.);

fn set_height(&mut self, height: f64)

Sets the actual height of the widget to height.

Usually called by the layouter. Usually not to be reimplemented.

let mut widget = DummyWidget::default();
widget.set_height(23.);
assert_eq!(widget.size().h, 23.);

fn expand_width(&mut self, amount: f64)

Expands the width of the widget by amount.

Usually called by the layouter. Usually not to be reimplemented.

let mut widget = DummyWidget::default();
widget.set_width(23.);
widget.expand_width(42.);
assert_eq!(widget.size().w, 65.);

fn expand_height(&mut self, amount: f64)

Expands the width of the widget by amount.

Usually called by the layouter. Usually not to be reimplemented.

let mut widget = DummyWidget::default();
widget.set_height(23.);
widget.expand_height(42.);
assert_eq!(widget.size().h, 65.);

fn set_pos(&mut self, pos: &Coord)

Sets the position of the widget to pos.

Usually called by the layouter. Usually not to be reimplemented.

let mut widget = DummyWidget::default();
widget.set_pos(&Coord { x: 23., y: 42. });
assert_eq!(widget.pos(), Coord { x: 23., y: 42. });

fn set_size(&mut self, size: &Size)

Sets the position of the widget to size.

Usually called by the layouter. Usually not to be reimplemented.

let mut widget = DummyWidget::default();
widget.set_size(&Size { w: 23., h: 42. });
assert_eq!(widget.size(), Size { w: 23., h: 42. });

fn layout(&self) -> Layout

Returns the Layout (drawing rect) of the widget.

Usually not to be reimplemented.

let mut widget = DummyWidget::default();
let layout = Layout {
    pos: Coord { x: 0., y: 0. },
    size: Size { w: 0., h: 0. }
};
assert_eq!(widget.layout(), layout);

fn set_layout(&mut self, layout: &Layout)

sets the Layout of the widget to layout

Probably not needed as only used once in UI

let mut widget = DummyWidget::default();
let layout = Layout {
    pos: Coord { x: 23., y: 42. },
    size: Size { w: 137., h: 93. }
};
widget.set_layout(&layout);
assert_eq!(widget.layout(), layout);

fn is_sensitive(&self) -> bool

Returns true iff the widget is sensitive to user evnets.

Usually not to be reimplemented.

fn is_hovered(&self) -> bool

Returns true iff the widget is currently hovered.

Usually not to be reimplemented.

let mut widget = DummyWidget::default();
assert!(!widget.is_hovered());
widget.pointer_enter_wrap();
assert!(widget.is_hovered());
widget.pointer_leave_wrap();
assert!(!widget.is_hovered());

fn is_hit_by(&self, pos: Coord) -> bool

Returns true iff the widget's Layout is containing pos.

Usually not to be reimplemented.

let mut widget = DummyWidget::default();
let layout = Layout {
    pos: Coord { x: 23., y: 42. },
    size: Size { w: 137., h: 93. }
};
widget.set_layout(&layout);
assert!(widget.is_hit_by(Coord { x: 25., y: 45. }));
assert!(!widget.is_hit_by(Coord { x: 15., y: 45. }));
assert!(!widget.is_hit_by(Coord { x: 163., y: 45. }));
assert!(!widget.is_hit_by(Coord { x: 25., y: 35. }));
assert!(!widget.is_hit_by(Coord { x: 25., y: 137. }));

fn intersects_with(&self, pos: Coord, size: Size) -> bool

Returns true iff the widget's Layout is intersecting pos.

Usually not to be reimplemented.

let mut widget = DummyWidget::default();
let layout = Layout {
    pos: Coord { x: 23., y: 42. },
    size: Size { w: 137., h: 93. }
};
widget.set_layout(&layout);
assert!(widget.intersects_with(
    Coord { x: 12., y: 23. },
    Size { w: 23., h: 23. }
));
assert!(widget.intersects_with(
    Coord { x: 150., y: 120. },
    Size { w: 23., h: 23. }
));
assert!(!widget.intersects_with(
    Coord { x: 162., y: 132. },
    Size { w: 23., h: 23. }
));
assert!(!widget.intersects_with(
    Coord { x: 12., y: 23. },
    Size { w: 3., h: 3. }
));
assert!(widget.intersects_with(
    Coord { x: 12., y: 23. },
    Size { w: 163., h: 143. }
));

fn set_focus(&mut self, yn: bool)

Sets the widget's focus state to yn.

Usually not to be reimplemented.

fn needs_repaint(&mut self) -> bool

Returns true iff the widget needs to be repainted.

Usually not to be reimplemented.

fn pointer_enter_wrap(&mut self)

Wrapper for the pointer_enter() event function.

Usually only called by the UI. Usually not to be reimplemented.

fn pointer_leave_wrap(&mut self)

Wrapper for the pointer_leave() event function.

Usually only called by the UI. Usually not to be reimplemented.

Loading content...

Implementations

impl dyn Widget[src]

pub fn is<__T: Widget>(&self) -> bool[src]

Returns true if the trait object wraps an object of type __T.

pub fn downcast<__T: Widget>(self: Box<Self>) -> Result<Box<__T>, Box<Self>>[src]

Returns a boxed object from a boxed trait object if the underlying object is of type __T. Returns the original boxed trait if it isn't.

pub fn downcast_rc<__T: Widget>(self: Rc<Self>) -> Result<Rc<__T>, Rc<Self>>[src]

Returns an Rc-ed object from an Rc-ed trait object if the underlying object is of type __T. Returns the original Rc-ed trait if it isn't.

pub fn downcast_ref<__T: Widget>(&self) -> Option<&__T>[src]

Returns a reference to the object within the trait object if it is of type __T, or None if it isn't.

pub fn downcast_mut<__T: Widget>(&mut self) -> Option<&mut __T>[src]

Returns a mutable reference to the object within the trait object if it is of type __T, or None if it isn't.

pub fn downcast_arc<__T: Widget>(self: Arc<Self>) -> Result<Arc<__T>, Arc<Self>> where
    __T: Any + Send + Sync
[src]

Returns an Arc-ed object from an Arc-ed trait object if the underlying object is of type __T. Returns the original Arc-ed trait if it isn't.

Implementors

impl Widget for Spacer[src]

impl Widget for LayoutWidget[src]

Loading content...