Skip to main content

Cx

Struct Cx 

Source
pub struct Cx { /* private fields */ }
Expand description

The context widget code runs in.

A Cx wraps the reactive Owner of one part of the shell: a window, a widget or one item of a keyed list. Signals, memos and effects created through it belong to that owner, so they are freed when that part of the shell goes away. Event handlers receive the Cx of their button, and children_keyed views receive the Cx of their item.

A context also carries the shell’s services, keyed by type: the platform, and later the clock, Sway, audio and the tray. Every context made from it shares the same map, so a service provided once at the root reaches every widget and handler.

Implementations§

Source§

impl Cx

Source

pub fn animate<T: Animate>( &self, target: impl FnMut() -> T + 'static, transition: Transition, ) -> ReadSignal<T>

A value that follows target, moving to each new one over transition instead of jumping. It starts at target’s first value. A new target while it moves starts a new animation from where it is. The animation goes when this context’s owner does. It is Cx::animation’s value.

use crabshell_ui::{Cx, Transition};

fn slide(cx: &Cx) {
    let open = cx.signal(false);
    let offset = cx.animate(move || if open.get() { 0.0 } else { -200.0 }, Transition::new(200));
    // Read `offset.get()` in a binding, such as a style's width.
}
Source

pub fn animation<T: Animate>( &self, target: impl FnMut() -> T + 'static, transition: Transition, ) -> Animation<T>

A value that follows target as Cx::animate’s does, and whether it moves. A panic in target is logged with this call’s place, and the value stays where it was.

use crabshell_ui::{Cx, Transition};

fn slide_in_then_focus(cx: &Cx) {
    let open = cx.signal(false);
    let ready = cx.signal(false);
    let slide = cx.animation(move || if open.get() { 0.0 } else { -200.0 }, Transition::new(200));
    cx.effect(move || {
        if open.get() && !slide.running().get() {
            ready.set(true);
        }
    });
}
Source

pub fn cycle( &self, running: impl FnMut() -> bool + 'static, transition: Transition, ) -> ReadSignal<f32>

Progress from 0 to 1, eased, repeating every transition.ms while running gives true, for spinners and pulses. With Transition::alternate it comes back, 0 to 1 to 0, each way taking ms. It waits out the delay once, at the start. When running turns false, the round it is in runs to its end, then it rests at 0 and the clock stops; true again before then keeps it going, and after it begins at 0. A 0 ms cycle never moves. A panic in running is logged with this call’s place and ends the cycle after its round.

use crabshell_ui::{Cx, Easing, Transition};

const SPIN: Transition = Transition::new(1000).easing(Easing::Linear);
const PULSE: Transition = Transition::new(600).easing(Easing::InOutSine).alternate();

fn busy(cx: &Cx) {
    let loading = cx.signal(true);
    let turn = cx.cycle(move || loading.get(), SPIN); // turns: turn.get() * 360.0
    let glow = cx.cycle(move || loading.get(), PULSE); // an opacity: 0.5 + glow.get() / 2.0
}
Source

pub fn frames( &self, running: impl FnMut() -> bool + 'static, ) -> ReadSignal<Duration>

The time since running turned true, updated at each tick of the animation clock while it stays true, as Qt’s FrameAnimation gives. Nothing ticks while it is false, and the time stays where it was until the next run starts again from 0. A panic in running is logged with this call’s place and stops it.

use crabshell_ui::Cx;

fn wave(cx: &Cx) {
    let shown = cx.signal(true);
    let time = cx.frames(move || shown.get());
    // A bar's height: 10.0 + 5.0 * (time.get().as_secs_f32() * 6.0).sin()
}
Source

pub fn spring( &self, target: impl FnMut() -> f32 + 'static, spring: Spring, ) -> ReadSignal<f32>

A value that follows target on spring, as Qt’s SpringAnimation: a new target while it moves keeps its speed, so a value chasing a moving target never stops dead. It starts at target’s first value and comes to rest exactly on the target, when the clock stops. A target that isn’t finite, or a spring that isn’t valid (see Spring), jumps there at once. A panic in target is logged with this call’s place, and the value stops where it is.

use crabshell_ui::{Cx, Spring};

fn indicator(cx: &Cx) {
    let hovered = cx.signal(0_usize);
    let x = cx.spring(move || hovered.get() as f32 * 40.0, Spring::new(380.0, 0.8));
    // Read `x.get()` in a binding, such as a style's padding.
}
Source§

impl Cx

Source

pub fn new(owner: Owner) -> Cx

A context that creates everything under owner, with no services yet.

Source

pub fn owner(&self) -> Owner

The owner that signals, memos and effects created through this context belong to.

Source

pub fn child(&self) -> Cx

A context for a part of the shell inside this one: what it creates is freed when this context’s owner is. The services are shared with the child.

Source

pub fn leaving(&self) -> ReadSignal<bool>

Whether the keyed item this context is in is leaving: true from when its key leaves a list with an exit until it is removed, or until the key comes back. Every context made inside the item reads the same signal, and outside one it stays false.

Source

pub fn provide<T: 'static>(&self, service: T) -> Rc<T>

Makes service reachable from every context of this shell, through service, and returns it shared. A service of the same type provided before is replaced; whoever holds it keeps it.

Source

pub fn service<T: 'static>(&self) -> Option<Rc<T>>

The service of type T, if one was provided.

Source

pub fn signal<T: 'static>(&self, value: T) -> Signal<T>

Creates a Signal holding value. See Owner::signal.

Source

pub fn memo<T: PartialEq + 'static>( &self, f: impl FnMut() -> T + 'static, ) -> Memo<T>

Creates a Memo that derives its value with f. See Owner::memo.

Source

pub fn effect(&self, f: impl FnMut() + 'static) -> Effect

Creates an Effect that reruns f when something it read changes. See Owner::effect.

Source§

impl Cx

Source

pub fn start_move(&self)

Lets the user drag the floating window this widget is in, as by a title bar. Call it from a button’s on_press, while the pointer button is held.

Source

pub fn start_resize(&self, edges: impl Into<Anchors>)

Lets the user resize the floating window this widget is in from edges, such as Edge::Bottom | Edge::Right. Call it from a button’s on_press.

Source

pub fn minimize(&self)

Asks to minimize the floating window this widget is in.

Source§

impl Cx

Source

pub fn open_popup(&self, build: impl FnOnce(&Cx) -> PopupWindow)

Opens a popup next to the widget this context belongs to, such as the button whose handler got it. build describes the popup and gets its context: what it creates there goes when the popup does.

The popup belongs to the widget, so it closes when the widget goes, as well as on close_popup and when the user clicks outside it. If build panics, that is logged with the widget’s path and nothing opens. A context of no widget, such as the one per_output gives, logs that and opens nothing.

Source

pub fn close_popup(&self)

Closes the popup this context’s widget is in, with every popup it opened and every popup that opened it, as a menu does once one of its entries is clicked. Outside a popup it logs that and does nothing.

Source§

impl Cx

Source

pub fn after(&self, delay: Duration, f: impl FnMut() + 'static) -> Timer

Runs f once, delay from now. The timer belongs to this context and goes with it; Timer::restart runs it again.

use std::time::Duration;
use crabshell_ui::Cx;

fn osd(cx: &Cx) {
    let hide = cx.after(Duration::from_millis(1500), || {});
    // Show the OSD while `hide.running()`, and call `hide.restart()`
    // on each change.
}
Source

pub fn interval(&self, period: Duration, f: impl FnMut() + 'static) -> Timer

Runs f every period, the first time period from now. A late wakeup fires once and keeps the schedule; after a stall longer than a period the next fire is a period from then, never a burst. The timer belongs to this context and goes with it.

Trait Implementations§

Source§

impl Clone for Cx

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Cx

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl !RefUnwindSafe for Cx

§

impl !Send for Cx

§

impl !Sync for Cx

§

impl !UnwindSafe for Cx

§

impl Forget for Cx

§

impl Freeze for Cx

§

impl Move for Cx

§

impl Unpin for Cx

§

impl UnsafeUnpin for Cx

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

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 T
where U: From<T>,

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.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

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

Source§

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

The type returned in the event of a conversion error.
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