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
impl Cx
Sourcepub fn animate<T: Animate>(
&self,
target: impl FnMut() -> T + 'static,
transition: Transition,
) -> ReadSignal<T>
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.
}Sourcepub fn animation<T: Animate>(
&self,
target: impl FnMut() -> T + 'static,
transition: Transition,
) -> Animation<T>
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);
}
});
}Sourcepub fn cycle(
&self,
running: impl FnMut() -> bool + 'static,
transition: Transition,
) -> ReadSignal<f32>
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
}Sourcepub fn frames(
&self,
running: impl FnMut() -> bool + 'static,
) -> ReadSignal<Duration>
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()
}Sourcepub fn spring(
&self,
target: impl FnMut() -> f32 + 'static,
spring: Spring,
) -> ReadSignal<f32>
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
impl Cx
Sourcepub fn new(owner: Owner) -> Cx
pub fn new(owner: Owner) -> Cx
A context that creates everything under owner, with no services
yet.
Sourcepub fn owner(&self) -> Owner
pub fn owner(&self) -> Owner
The owner that signals, memos and effects created through this context belong to.
Sourcepub fn child(&self) -> Cx
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.
Sourcepub fn leaving(&self) -> ReadSignal<bool>
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.
Sourcepub fn provide<T: 'static>(&self, service: T) -> Rc<T>
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.
Sourcepub fn signal<T: 'static>(&self, value: T) -> Signal<T>
pub fn signal<T: 'static>(&self, value: T) -> Signal<T>
Creates a Signal holding value. See Owner::signal.
Source§impl Cx
impl Cx
Sourcepub fn start_move(&self)
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.
Sourcepub fn start_resize(&self, edges: impl Into<Anchors>)
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§impl Cx
impl Cx
Sourcepub fn open_popup(&self, build: impl FnOnce(&Cx) -> PopupWindow)
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.
Sourcepub fn close_popup(&self)
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
impl Cx
Sourcepub fn after(&self, delay: Duration, f: impl FnMut() + 'static) -> Timer
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.
}Sourcepub fn interval(&self, period: Duration, f: impl FnMut() + 'static) -> Timer
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.