Struct druid::LayoutCtx

source ·
pub struct LayoutCtx<'a, 'b> { /* private fields */ }
Expand description

A context provided to layout-handling methods of widgets.

As of now, the main service provided is access to a factory for creating text layout objects, which are likely to be useful during widget layout.

Implementations§

source§

impl LayoutCtx<'_, '_>

source

pub fn widget_id(&self) -> WidgetId

get the WidgetId of the current widget.

source

pub fn window(&self) -> &WindowHandle

Returns a reference to the current WindowHandle.

Examples found in repository?
examples/input_region.rs (line 162)
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
    fn layout(
        &mut self,
        ctx: &mut druid::LayoutCtx,
        bc: &druid::BoxConstraints,
        data: &AppState,
        env: &druid::Env,
    ) -> druid::Size {
        let mut interactable_area = Region::EMPTY;
        let smaller_bc = BoxConstraints::new(
            Size::new(0.0, 0.0),
            Size::new(bc.max().width - 100.0, bc.max().height - 100.0),
        );
        let full_bc = BoxConstraints::new(Size::new(0.0, 0.0), bc.max());
        let _label_size = self.info_label.layout(ctx, &smaller_bc, data, env);
        let controls_size = self.controls.layout(ctx, &full_bc, data, env);

        let text_origin_point = Point::new(50.0, 50.0 + controls_size.height);
        self.info_label.set_origin(ctx, text_origin_point);
        let controls_origin_point = Point::new(EXAMPLE_BORDER_SIZE, EXAMPLE_BORDER_SIZE);
        self.controls.set_origin(ctx, controls_origin_point);

        // Add side rects to clarify the dimensions of the window.
        let left_rect = Rect::new(0.0, 0.0, EXAMPLE_BORDER_SIZE, bc.max().height);
        let right_rect = Rect::new(
            bc.max().width - EXAMPLE_BORDER_SIZE,
            0.0,
            bc.max().width,
            bc.max().height,
        );
        let bottom_rect = Rect::new(
            0.0,
            bc.max().height - EXAMPLE_BORDER_SIZE,
            bc.max().width,
            bc.max().height,
        );
        interactable_area.add_rect(left_rect);
        interactable_area.add_rect(right_rect);
        interactable_area.add_rect(bottom_rect);
        interactable_area.add_rect(self.info_label.layout_rect());
        interactable_area.add_rect(self.controls.layout_rect());

        if data.limit_input_region {
            ctx.window().set_input_region(Some(interactable_area));
        } else {
            ctx.window().set_input_region(None);
        }

        bc.max()
    }
source

pub fn window_id(&self) -> WindowId

Get the WindowId of the current window.

source

pub fn text(&mut self) -> &mut PietText

Get an object which can create text layouts.

source

pub fn scale(&self) -> Scale

The current window’s Scale.

The returned Scale is a copy and thus its information will be stale after the platform changes the window’s scale. This means you can only rely on it until the next Event::WindowScale event happens.

source§

impl LayoutCtx<'_, '_>

source

pub fn view_context_changed(&mut self)

Indicate that your ViewContext has changed.

This event is sent after layout is done and before paint is called. Note that you can still receive this event even if there was no prior call to layout.

Widgets must call this method after changing the clip region of their children. Changes to the other parts of ViewContext (cursor position and global origin) are tracked internally.

source§

impl LayoutCtx<'_, '_>

source

pub fn submit_command(&mut self, cmd: impl Into<Command>)

Submit a Command to be run after this event is handled.

Commands are run in the order they are submitted; all commands submitted during the handling of an event are executed before the update method is called; events submitted during update are handled after painting.

Target::Auto commands will be sent to the window containing the widget.

source

pub fn get_external_handle(&self) -> ExtEventSink

Returns an ExtEventSink that can be moved between threads, and can be used to submit commands back to the application.

source

pub fn request_timer(&mut self, deadline: Duration) -> TimerToken

Request a timer event.

The return value is a token, which can be used to associate the request with the event.

source§

impl<'a, 'b> LayoutCtx<'a, 'b>

source

pub fn set_paint_insets(&mut self, insets: impl Into<Insets>)

Set explicit paint Insets for this widget.

You are not required to set explicit paint bounds unless you need to paint outside of your layout bounds. In this case, the argument should be an Insets struct that indicates where your widget needs to overpaint, relative to its bounds.

For more information, see WidgetPod::paint_insets.

Examples found in repository?
examples/scroll.rs (line 57)
56
57
58
59
    fn layout(&mut self, ctx: &mut LayoutCtx, bc: &BoxConstraints, _: &T, _: &Env) -> Size {
        ctx.set_paint_insets(INSETS);
        bc.constrain(Size::new(100., 100.))
    }
source

pub fn set_baseline_offset(&mut self, baseline: f64)

Set an explicit baseline position for this widget.

The baseline position is used to align widgets that contain text, such as buttons, labels, and other controls. It may also be used by other widgets that are opinionated about how they are aligned relative to neighbouring text, such as switches or checkboxes.

The provided value should be the distance from the bottom of the widget to the baseline.

Auto Trait Implementations§

§

impl<'a, 'b> !RefUnwindSafe for LayoutCtx<'a, 'b>

§

impl<'a, 'b> !Send for LayoutCtx<'a, 'b>

§

impl<'a, 'b> !Sync for LayoutCtx<'a, 'b>

§

impl<'a, 'b> Unpin for LayoutCtx<'a, 'b>where 'b: 'a,

§

impl<'a, 'b> !UnwindSafe for LayoutCtx<'a, 'b>

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> RoundFrom<T> for T

§

fn round_from(x: T) -> T

Performs the conversion.
§

impl<T, U> RoundInto<U> for Twhere U: RoundFrom<T>,

§

fn round_into(self) -> U

Performs the conversion.
source§

impl<T> Same<T> for T

§

type Output = T

Should always be Self
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more