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: impl IntoProp<Transition>,
) -> ReadSignal<T>
pub fn animate<T: Animate>( &self, target: impl FnMut() -> T + 'static, transition: impl IntoProp<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: impl IntoProp<Transition>,
) -> Animation<T>
pub fn animation<T: Animate>( &self, target: impl FnMut() -> T + 'static, transition: impl IntoProp<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,
) -> Animation<f32>
pub fn spring( &self, target: impl FnMut() -> f32 + 'static, spring: Spring, ) -> Animation<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. The
Animation’s running() is true from a new target until it comes
to rest, and never for a jump.
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.value().get()` in a binding, such as a style's padding.
}Sourcepub fn sequence<T: Animate, K: PartialEq + 'static>(
&self,
initial: T,
trigger: impl FnMut() -> K + 'static,
steps: impl IntoProp<Vec<Step<T>>>,
) -> Animation<T>
pub fn sequence<T: Animate, K: PartialEq + 'static>( &self, initial: T, trigger: impl FnMut() -> K + 'static, steps: impl IntoProp<Vec<Step<T>>>, ) -> Animation<T>
A value that plays steps one after another each time trigger’s
value changes, as Qt’s SequentialAnimation: a shake on a wrong
password, bumped by a counter, or a pause and then a slide. It starts
at initial, and trigger’s first value plays nothing. Each step
moves from wherever the value is, so a new trigger mid-play starts
over from there, and once played the value rests at the last step’s
target. steps is a value or a closure, read untracked as each play
starts; an empty list plays nothing.
The Animation’s running() is true from the trigger until the
last step ends, holds included, so an effect on it runs what comes
after. A hold, or a delay, takes the value off the animation clock:
the loop sleeps until it ends instead of waking each frame. A panic in
trigger is logged with this call’s place and stops the sequence
where it is for good; one in steps or a config’s own lerp stops
that play.
use crabshell_ui::{Cx, Easing, Step, Transition};
const SHAKE: [Step<f32>; 3] = [
Step::to(-14.0, Transition::new(70).easing(Easing::OutQuad)),
Step::to(12.0, Transition::new(90).easing(Easing::InOutQuad)),
Step::to(0.0, Transition::new(95).easing(Easing::OutQuad)),
];
fn pill(cx: &Cx) {
let wrong = cx.signal(0_u32); // bumped on each wrong password
let shake = cx.sequence(0.0, move || wrong.get(), SHAKE);
// Read `shake.value().get()` in a binding, such as a translate.
}Sourcepub fn sequence_loop<T: Animate>(
&self,
initial: T,
running: impl FnMut() -> bool + 'static,
steps: impl IntoProp<Vec<Step<T>>>,
) -> Animation<T>
pub fn sequence_loop<T: Animate>( &self, initial: T, running: impl FnMut() -> bool + 'static, steps: impl IntoProp<Vec<Step<T>>>, ) -> Animation<T>
A value that plays steps over and over while running gives
true, each round with its holds, as a SequentialAnimation with
infinite loops: a pulse while something loads. It starts at
initial; round 0 moves from where the value is, and each later
round from the last step’s target. When running turns false, the
round it is in runs to its end and the value rests at the last step’s
target; true again before then keeps it going, and after it starts
again at round 0. A round of no time plays once. Holds sleep off the
clock as in Cx::sequence. A panic in running is logged with
this call’s place and ends it after its round.
use crabshell_ui::{Cx, Easing, Step, Transition};
const SINE: Transition = Transition::new(1100).easing(Easing::InOutSine);
fn tile(cx: &Cx, index: u32) {
let loading = cx.signal(true);
let steps = [Step::hold(index * 140), Step::to(0.4, SINE), Step::to(1.0, SINE)];
let opacity = cx.sequence_loop(1.0, move || loading.get(), steps);
}Source§impl Cx
impl Cx
Sourcepub fn capture(&self, source: impl IntoProp<CaptureSource>) -> Capture
pub fn capture(&self, source: impl IntoProp<CaptureSource>) -> Capture
A capture of source: a CaptureSource, or a closure that
returns one. It takes one frame as soon as the source is there,
waiting while it isn’t, as for an output not plugged in yet. A new
source drops the frame and takes one of the new source. The capture
belongs to this context and goes with it.
The frame, about 8 MB for a 1920 by 1080 output, stays in memory
until the capture’s context goes, even once no view shows it. So
make a capture for a passing effect in a child context, from
Cx::child, and dispose that context’s owner after use.
use crabshell_reactive::Runtime;
use crabshell_ui::{CaptureSource, Cx, Rect, ScreencopyView, Style};
let cx = Cx::new(Runtime::current().new_owner());
let screen = cx.capture(CaptureSource::Output(String::from("DP-1")));
let whole = ScreencopyView::of(screen).style(Style::new().width(1920.0).height(1080.0));
let corner = ScreencopyView::of(screen)
.crop(Some(Rect::new(0.0, 0.0, 480.0, 270.0)))
.style(Style::new().width(480.0).height(270.0));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, and while an item of
such a list around it is leaving. 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.
Sourcepub fn memo<T: PartialEq + 'static>(
&self,
f: impl FnMut() -> T + 'static,
) -> Memo<T>
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.
Sourcepub fn effect(&self, f: impl FnMut() + 'static) -> Effect
pub fn effect(&self, f: impl FnMut() + 'static) -> Effect
Creates an Effect that reruns f when something it read changes.
See Owner::effect.
Sourcepub fn measure_text(&self, text: &str, font: Font) -> TextMetrics
pub fn measure_text(&self, text: &str, font: Font) -> TextMetrics
Measures text as a Text with font lays it out,
with the same fonts and fallbacks. Each line break starts another
line: the width is the widest line’s and the height a line height per
line. TextMetrics::ink is the box the glyphs draw, for sizing an
icon glyph by its ink rather than its advance. It isn’t reactive
itself; a binding that calls it runs again when the signals it read
change.
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), || {}).stopped();
// 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.