crabshell_ui/widgets.rs
1//! The widgets, and the [`Widget`] trait they share.
2//!
3//! A widget value is only a description. Nothing happens until its window is
4//! created: then each description becomes a node in the window's tree, and
5//! each closure property becomes an effect that updates that node.
6
7use std::hash::Hash;
8use std::path::PathBuf;
9use std::rc::Rc;
10use std::time::Duration;
11
12use crabshell_reactive::{Memo, Signal};
13
14use crate::cx::Cx;
15use crate::dnd::DragContent;
16use crate::geometry::{Point, Rect, Size};
17use crate::image::ImageTransition;
18use crate::input::{
19 CursorShape, Drag, DragValue, DropTarget, Handler, Handlers, LinkHandler, Wheel,
20};
21use crate::keyboard::KeyEvent;
22use crate::keyed::{self, KeyedList, KeyedMount};
23use crate::prop::{IntoProp, Prop};
24use crate::rich::Span;
25use crate::screencopy::ScreencopyProps;
26use crate::scroll::ScrollProps;
27use crate::style::{Color, Style};
28use crate::text_input::InputProps;
29
30/// Something that can be placed in a window: one of this crate's widgets, or
31/// a type of your own that is made of them.
32///
33/// To make your own, return another widget's element. Calling
34/// `into_element` on a container describes everything inside it too, so the
35/// result is plain data:
36///
37/// ```
38/// use crabshell_ui::{Element, Row, Text, Widget};
39///
40/// struct Greeting;
41///
42/// impl Widget for Greeting {
43/// fn into_element(self: Box<Self>) -> Element {
44/// Box::new(Row::new().gap(4).child(Text::new("hello")).child(Text::new("world")))
45/// .into_element()
46/// }
47/// }
48/// ```
49pub trait Widget: 'static {
50 /// Turns the description, and everything inside it, into the element
51 /// the window builds nodes from. It runs when the window is created or a
52 /// list item first appears, before the window's tree is touched and
53 /// without recording what it reads, so it only describes.
54 fn into_element(self: Box<Self>) -> Element;
55}
56
57/// What a [`Widget`] turns into: plain data, with every child already turned
58/// into its element. Only this crate's widgets create one.
59pub struct Element {
60 pub(crate) name: &'static str,
61 pub(crate) common: Common,
62 pub(crate) kind: ElementKind,
63 pub(crate) children: Vec<ElementChild>,
64}
65
66pub(crate) enum ElementKind {
67 Flex(FlexProps),
68 Grid(GridProps),
69 Stack,
70 Spacer,
71 Text(TextProps),
72 Icon(IconProps),
73 Image(ImageProps),
74 Capture(ScreencopyProps),
75 Canvas(Box<dyn FnMut(&mut crate::canvas::Pen)>),
76 // Boxed: a button's handlers make it the largest by far.
77 Button(Box<ButtonProps>),
78 // Boxed: its scroll bar's style makes it large.
79 Scroll(Box<ScrollProps>),
80 Input(Box<InputProps>),
81}
82
83/// What every widget takes besides its own properties.
84#[derive(Default)]
85pub(crate) struct Common {
86 pub(crate) style: Option<Prop<Style>>,
87 pub(crate) visible: Option<Prop<bool>>,
88 pub(crate) geometry: Option<Signal<Rect>>,
89 pub(crate) mapped_geometry: Option<Signal<Rect>>,
90 pub(crate) accessible_name: Option<Prop<String>>,
91 /// Told the widget's size after each layout that changes it, with the
92 /// tree released. Lists measure their rows with it; configs have
93 /// `geometry`.
94 pub(crate) measure: Option<Measure>,
95}
96
97/// What a list runs with a row's size after a layout changes it.
98pub(crate) type Measure = Rc<dyn Fn(Size)>;
99
100/// One entry of a container's child list: a single widget, or a keyed list
101/// that grows and shrinks.
102pub(crate) enum Child {
103 One(Box<dyn Widget>),
104 Keyed(KeyedList),
105}
106
107/// A [`Child`] with its widget turned into an element.
108pub(crate) enum ElementChild {
109 One(Box<Element>),
110 Keyed(KeyedMount),
111}
112
113/// Turns every child widget into its element, which does the same for its
114/// own children.
115fn describe(children: Vec<Child>) -> Vec<ElementChild> {
116 children
117 .into_iter()
118 .map(|child| match child {
119 Child::One(widget) => ElementChild::One(Box::new(widget.into_element())),
120 Child::Keyed(list) => ElementChild::Keyed(list.into_mount()),
121 })
122 .collect()
123}
124
125pub(crate) struct FlexProps {
126 pub(crate) column: bool,
127 /// Takes the room left in its parent, like a spacer. Only `Menu` sets
128 /// it, so an entry's row fills its button.
129 pub(crate) grow: bool,
130 pub(crate) gap: Option<Prop<u32>>,
131 pub(crate) padding_vertical: Option<Prop<u32>>,
132 pub(crate) padding_horizontal: Option<Prop<u32>>,
133 pub(crate) align: Option<Prop<Align>>,
134 pub(crate) justify: Option<Prop<Justify>>,
135 pub(crate) wrap: Option<Prop<bool>>,
136 pub(crate) reverse: Option<Prop<bool>>,
137 /// Overrides `column` with a binding, for a list that turns.
138 pub(crate) column_binding: Option<Prop<bool>>,
139}
140
141pub(crate) struct GridProps {
142 pub(crate) columns: Option<Prop<u32>>,
143 pub(crate) gap: Option<Prop<u32>>,
144 pub(crate) row_gap: Option<Prop<u32>>,
145 pub(crate) column_gap: Option<Prop<u32>>,
146 pub(crate) padding_vertical: Option<Prop<u32>>,
147 pub(crate) padding_horizontal: Option<Prop<u32>>,
148}
149
150pub(crate) struct TextProps {
151 /// The plain text, or for a rich text `None` and its spans.
152 pub(crate) text: Option<Prop<String>>,
153 pub(crate) spans: Option<Prop<Vec<Span>>>,
154 pub(crate) elide: Option<Prop<Elide>>,
155 pub(crate) max_lines: Option<Prop<u32>>,
156 pub(crate) selectable: Option<Prop<bool>>,
157 pub(crate) on_link: Option<LinkHandler>,
158 pub(crate) link_hover_color: Option<Prop<Option<Color>>>,
159}
160
161pub(crate) struct IconProps {
162 pub(crate) source: Prop<IconSource>,
163 pub(crate) fallback: Option<Prop<IconSource>>,
164 pub(crate) size: Option<Prop<u32>>,
165 pub(crate) tint: Option<Prop<Option<Color>>>,
166}
167
168pub(crate) struct ImageProps {
169 pub(crate) source: Prop<PathBuf>,
170 pub(crate) width: Option<Prop<u32>>,
171 pub(crate) height: Option<Prop<u32>>,
172 pub(crate) fit: Option<Prop<Fit>>,
173 pub(crate) tint: Option<Prop<Option<Color>>>,
174 pub(crate) brightness: Option<Prop<f32>>,
175 pub(crate) contrast: Option<Prop<f32>>,
176 pub(crate) saturation: Option<Prop<f32>>,
177 pub(crate) blur: Option<Prop<f32>>,
178 pub(crate) playing: Option<Prop<bool>>,
179 pub(crate) transition: Option<Prop<ImageTransition>>,
180}
181
182/// The filters an [`Image`] is drawn through, for the renderer. Each
183/// number is finite and at least 0, and the default changes nothing.
184#[derive(Clone, Copy, Debug, PartialEq)]
185pub struct ImageFilters {
186 /// The color every pixel is painted in, keeping its alpha.
187 pub tint: Option<Color>,
188 /// Scales each color channel, as CSS's `brightness()`.
189 pub brightness: f32,
190 /// Moves each channel from or toward the middle gray, as CSS's
191 /// `contrast()`.
192 pub contrast: f32,
193 /// Moves each color from or toward its gray, as CSS's `saturate()`.
194 pub saturation: f32,
195 /// A Gaussian blur's standard deviation in logical pixels, as CSS's
196 /// `blur()`.
197 pub blur: f32,
198}
199
200impl Default for ImageFilters {
201 fn default() -> ImageFilters {
202 ImageFilters {
203 tint: None,
204 brightness: 1.0,
205 contrast: 1.0,
206 saturation: 1.0,
207 blur: 0.0,
208 }
209 }
210}
211
212/// How an [`Image`] fills its box when their shapes differ, as CSS's
213/// `object-fit`.
214#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
215pub enum Fit {
216 /// Stretched to the box. The default.
217 #[default]
218 Fill,
219 /// As large as fits whole, centered, keeping its shape.
220 Contain,
221 /// Covering the whole box, centered, keeping its shape; what sticks out
222 /// is cut off.
223 Cover,
224}
225
226pub(crate) struct ButtonProps {
227 pub(crate) hover_style: Option<Prop<Style>>,
228 pub(crate) pressed_style: Option<Prop<Style>>,
229 pub(crate) focus_style: Option<Prop<Style>>,
230 pub(crate) hovered: Option<Signal<bool>>,
231 pub(crate) position: Option<Signal<Option<Point>>>,
232 pub(crate) enabled: Option<Prop<bool>>,
233 pub(crate) focus: Option<Prop<bool>>,
234 pub(crate) cursor: Option<Prop<CursorShape>>,
235 pub(crate) drag_data: Option<Prop<Option<DragValue>>>,
236 pub(crate) drop_hovered: Option<Signal<bool>>,
237 pub(crate) handlers: Handlers,
238 /// Set by a slider on its body, for a screen reader.
239 pub(crate) value: Option<crate::slider::Range>,
240}
241
242/// How a [`Row`] or [`Column`] places children across its direction: up and
243/// down in a row, left and right in a column. As a child's own
244/// [`Style::align_self`] and [`Style::justify_self`], it also places the
245/// child in a [`Stack`]'s or grid's cell: start is the top or the left.
246#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
247pub enum Align {
248 /// At the start: the top of a row, the left of a column.
249 Start,
250 /// In the middle.
251 Center,
252 /// At the end: the bottom of a row, the right of a column.
253 End,
254 /// Stretched to fill. The default, as in CSS.
255 #[default]
256 Stretch,
257}
258
259/// How a [`Row`] or [`Column`] places children along its direction when
260/// they don't fill it.
261#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
262pub enum Justify {
263 /// Packed at the start. The default.
264 #[default]
265 Start,
266 /// Packed in the middle.
267 Center,
268 /// Packed at the end.
269 End,
270 /// The first child at the start, the last at the end, equal space between.
271 SpaceBetween,
272 /// Equal space around each child.
273 SpaceAround,
274 /// Equal space between children and at both ends.
275 SpaceEvenly,
276}
277
278/// What an [`Icon`] shows.
279#[derive(Clone, Debug, PartialEq)]
280pub enum IconSource {
281 /// A freedesktop icon name such as `"audio-volume-high"`, looked up in
282 /// the icon theme.
283 Name(String),
284 /// An SVG or PNG file.
285 File(PathBuf),
286 /// Raw pixels, as a tray item sends them.
287 Pixels(IconPixels),
288}
289
290/// Raw icon pixels: ARGB32 in network byte order (A, R, G, B bytes), row
291/// by row, as the StatusNotifierItem spec sends them.
292#[derive(Clone, Debug, PartialEq)]
293pub struct IconPixels {
294 /// Width in pixels.
295 pub width: u32,
296 /// Height in pixels.
297 pub height: u32,
298 /// `width * height * 4` bytes. Shared, so the same icon isn't copied.
299 pub data: Rc<[u8]>,
300}
301
302/// The methods every container has: `child`, `children_keyed`, `exit`
303/// and `displaced`.
304macro_rules! container_methods {
305 () => {
306 /// Adds `child` after the children added so far.
307 pub fn child(mut self, child: impl Widget) -> Self {
308 self.children.push(Child::One(Box::new(child)));
309 self
310 }
311
312 /// Adds one child per item, after the children added so far.
313 ///
314 /// `items` runs in an effect, so the list updates when a signal it
315 /// read changes. `key` names each item. `view` builds the child for
316 /// a key the first time it appears, and that child is kept while
317 /// its key stays in the list, even if it moves. `view` receives the
318 /// item's [`Cx`] and a [`Memo`] that updates when that item's value
319 /// changes. When a key leaves the list, its child and everything
320 /// created in `view` are freed, at once or after an
321 /// [`exit`](Self::exit). If two items share a key, only the first is
322 /// shown and the duplicate is logged.
323 pub fn children_keyed<T, K, W>(
324 mut self,
325 items: impl FnMut() -> Vec<T> + 'static,
326 key: impl FnMut(&T) -> K + 'static,
327 view: impl FnMut(&Cx, Memo<T>) -> W + 'static,
328 ) -> Self
329 where
330 T: Clone + PartialEq + 'static,
331 K: Eq + Hash + 'static,
332 W: Widget,
333 {
334 self
335 .children
336 .push(Child::Keyed(keyed::mount(items, key, view)));
337 self
338 }
339
340 /// Lets the children of the last [`children_keyed`](Self::children_keyed)
341 /// list added leave over `exit` instead of at once. A child whose key
342 /// leaves stays mounted and painted right after the child that was
343 /// before it, and its context's [`leaving`](Cx::leaving) turns `true`,
344 /// so its own `cx.animate` can fade or shrink it out. It is freed at
345 /// the first tick of the animation clock that finds nothing moving in
346 /// it, or after `exit` at the latest; motion the clock doesn't run in
347 /// the item, such as one stepped by `cx.interval`, doesn't hold it.
348 /// If its key comes back first, the same child, with its
349 /// state, takes the key's new place. A closure is read as each key
350 /// leaves, and an exit of 0 removes at once. Leaving children still
351 /// take pointer and keyboard input; a handler can check
352 /// [`cx.leaving()`](Cx::leaving) to ignore clicks during the exit.
353 pub fn exit(mut self, exit: impl IntoProp<Duration>) -> Self {
354 let last = self
355 .children
356 .iter_mut()
357 .rev()
358 .find_map(|child| match child {
359 Child::Keyed(list) => Some(list),
360 Child::One(_) => None,
361 });
362 match last {
363 Some(list) => list.exit = Some(exit.into_prop()),
364 None => tracing::warn!("`exit` was called before any `children_keyed`; it does nothing"),
365 }
366 self
367 }
368
369 /// Slides the children that a change of the last
370 /// [`children_keyed`](Self::children_keyed) list moves, instead of
371 /// jumping: when its keys are added, leave (once their
372 /// [`exit`](Self::exit) ends) or move, each child of this container
373 /// whose place changes goes from where it was drawn to its new place
374 /// over `transition`. It moves where it is drawn and hit, as
375 /// [`Style::translate`](crate::Style::translate) does, so a slide never
376 /// lays the window out again. A child moved again mid-slide starts from
377 /// where it is drawn. Other layout changes, such as a resize or text
378 /// growing, still move children at once, and a new child shows in its
379 /// place. A closure is read at each change of the list, and a
380 /// transition of 0 jumps.
381 pub fn displaced(mut self, transition: impl IntoProp<crate::Transition>) -> Self {
382 let last = self
383 .children
384 .iter_mut()
385 .rev()
386 .find_map(|child| match child {
387 Child::Keyed(list) => Some(list),
388 Child::One(_) => None,
389 });
390 match last {
391 Some(list) => list.displaced = Some(transition.into_prop()),
392 None => {
393 tracing::warn!("`displaced` was called before any `children_keyed`; it does nothing")
394 }
395 }
396 self
397 }
398 };
399}
400
401/// The `visible`, `geometry`, `mapped_geometry` and `accessible_name`
402/// methods every widget has.
403macro_rules! visibility_methods {
404 () => {
405 /// Shows or hides the widget. Hidden, it is taken out of the layout,
406 /// so its space goes to the others, and it isn't drawn, hit or
407 /// reached by Tab; it drops the keyboard focus and the pointer. Its
408 /// bindings and state stay, so it shows again as it was. Shown by
409 /// default.
410 pub fn visible(mut self, visible: impl $crate::IntoProp<bool>) -> Self {
411 self.common.visible = Some(visible.into_prop());
412 self
413 }
414
415 /// Shares where the widget is: after each layout that moves or
416 /// resizes it, its laid-out rectangle in the window's logical
417 /// pixels, scrolled with any scroll view around it but not moved by
418 /// a style's translate, scale or rotate, is written to `geometry`. A
419 /// hidden widget writes nothing. An effect that reads it may change
420 /// the layout: the window is laid out again at once, until nothing
421 /// moves.
422 pub fn geometry(mut self, geometry: ::crabshell_reactive::Signal<$crate::Rect>) -> Self {
423 self.common.geometry = Some(geometry);
424 self
425 }
426
427 /// Shares where the widget is drawn: the box around it once its own
428 /// and its ancestors' translate, scale and rotate have moved it, in
429 /// the window's logical pixels, scrolled with any scroll view around
430 /// it. It is written after each layout that moves it and after each
431 /// change of a transform or an offset that does, every frame of an
432 /// animated one. A hidden widget writes nothing. Where the window
433 /// itself is on its output is unknown: Wayland doesn't tell a client.
434 pub fn mapped_geometry(mut self, geometry: ::crabshell_reactive::Signal<$crate::Rect>) -> Self {
435 self.common.mapped_geometry = Some(geometry);
436 self
437 }
438
439 /// What a screen reader calls the widget, instead of what it shows:
440 /// a button's text, a text's own, or a field's placeholder. An icon
441 /// button needs one to be read at all.
442 pub fn accessible_name(mut self, name: impl $crate::IntoProp<String>) -> Self {
443 self.common.accessible_name = Some(name.into_prop());
444 self
445 }
446 };
447}
448
449/// The `style` method every widget has, with `visible` and `geometry`.
450macro_rules! style_method {
451 () => {
452 /// Sets the widget's [`Style`](crate::Style).
453 pub fn style(mut self, style: impl $crate::IntoProp<$crate::Style>) -> Self {
454 self.common.style = Some(style.into_prop());
455 self
456 }
457
458 $crate::widgets::visibility_methods!();
459 };
460}
461
462pub(crate) use visibility_methods;
463
464/// Defines `Row` and `Column`, which differ only in direction.
465macro_rules! flex_widget {
466 ($name:ident, $column:literal, $doc:literal) => {
467 #[doc = $doc]
468 ///
469 /// `gap`, `padding`, `align`, `justify`, `wrap` and `reverse` change
470 /// the layout. Each takes a value or a closure.
471 pub struct $name {
472 flex: FlexProps,
473 common: Common,
474 children: Vec<Child>,
475 }
476
477 impl $name {
478 /// An empty container.
479 pub fn new() -> Self {
480 $name {
481 flex: FlexProps {
482 column: $column,
483 grow: false,
484 gap: None,
485 padding_vertical: None,
486 padding_horizontal: None,
487 align: None,
488 justify: None,
489 wrap: None,
490 reverse: None,
491 column_binding: None,
492 },
493 common: Common::default(),
494 children: Vec::new(),
495 }
496 }
497
498 /// Sets the space between children in logical pixels.
499 pub fn gap(mut self, gap: impl IntoProp<u32>) -> Self {
500 self.flex.gap = Some(gap.into_prop());
501 self
502 }
503
504 /// Sets the padding: `vertical` above and below, `horizontal`
505 /// left and right, in logical pixels. It wins over the padding
506 /// of the container's [`Style`].
507 pub fn padding(
508 mut self,
509 vertical: impl IntoProp<u32>,
510 horizontal: impl IntoProp<u32>,
511 ) -> Self {
512 self.flex.padding_vertical = Some(vertical.into_prop());
513 self.flex.padding_horizontal = Some(horizontal.into_prop());
514 self
515 }
516
517 /// Sets how children are placed across the direction.
518 pub fn align(mut self, align: impl IntoProp<Align>) -> Self {
519 self.flex.align = Some(align.into_prop());
520 self
521 }
522
523 /// Sets how children are placed along the direction.
524 pub fn justify(mut self, justify: impl IntoProp<Justify>) -> Self {
525 self.flex.justify = Some(justify.into_prop());
526 self
527 }
528
529 /// When on, children that don't fit start a new line, `gap` apart
530 /// like the children, and the lines pack at the start. Off by
531 /// default.
532 pub fn wrap(mut self, wrap: impl IntoProp<bool>) -> Self {
533 self.flex.wrap = Some(wrap.into_prop());
534 self
535 }
536
537 /// When on, a row runs right to left and a column bottom to top, so
538 /// [`Justify::Start`] packs children at the right or the bottom.
539 /// Tab still moves through them in child order. Off by default.
540 pub fn reverse(mut self, reverse: impl IntoProp<bool>) -> Self {
541 self.flex.reverse = Some(reverse.into_prop());
542 self
543 }
544
545 container_methods!();
546 style_method!();
547 }
548
549 impl Default for $name {
550 fn default() -> Self {
551 Self::new()
552 }
553 }
554
555 impl Widget for $name {
556 fn into_element(self: Box<Self>) -> Element {
557 Element {
558 name: stringify!($name),
559 common: self.common,
560 kind: ElementKind::Flex(self.flex),
561 children: describe(self.children),
562 }
563 }
564 }
565 };
566}
567
568flex_widget!(
569 Row,
570 false,
571 "Lays its children out left to right, like a CSS flexbox row."
572);
573flex_widget!(
574 Column,
575 true,
576 "Lays its children out top to bottom, like a CSS flexbox column."
577);
578
579/// Places its children row by row into equal-width columns, each row as
580/// tall as its tallest child. A child takes several cells with
581/// [`Style::span`], and places itself in its cell with
582/// [`Style::align_self`] and [`Style::justify_self`]; it fills the cell by
583/// default.
584///
585/// `columns`, `gap`, `row_gap`, `column_gap` and `padding` change the
586/// layout. Each takes a value or a closure.
587///
588/// ```
589/// use crabshell_ui::{Grid, Style, Text};
590///
591/// let days = (1..=31).fold(Grid::new().columns(7).gap(4), |grid, day| {
592/// grid.child(Text::new(day.to_string()))
593/// });
594/// let wide = Grid::new()
595/// .columns(2)
596/// .child(Text::new("title").style(Style::new().span(2, 1)));
597/// ```
598pub struct Grid {
599 grid: GridProps,
600 common: Common,
601 children: Vec<Child>,
602}
603
604impl Grid {
605 /// An empty grid of one column.
606 pub fn new() -> Grid {
607 Grid {
608 grid: GridProps {
609 columns: None,
610 gap: None,
611 row_gap: None,
612 column_gap: None,
613 padding_vertical: None,
614 padding_horizontal: None,
615 },
616 common: Common::default(),
617 children: Vec::new(),
618 }
619 }
620
621 /// Sets the number of columns, all as wide. 1 by default, and 0
622 /// counts as 1.
623 pub fn columns(mut self, columns: impl IntoProp<u32>) -> Grid {
624 self.grid.columns = Some(columns.into_prop());
625 self
626 }
627
628 /// Sets the space between rows and between columns in logical pixels.
629 /// `row_gap` and `column_gap` win over it.
630 pub fn gap(mut self, gap: impl IntoProp<u32>) -> Grid {
631 self.grid.gap = Some(gap.into_prop());
632 self
633 }
634
635 /// Sets the space between rows in logical pixels.
636 pub fn row_gap(mut self, gap: impl IntoProp<u32>) -> Grid {
637 self.grid.row_gap = Some(gap.into_prop());
638 self
639 }
640
641 /// Sets the space between columns in logical pixels.
642 pub fn column_gap(mut self, gap: impl IntoProp<u32>) -> Grid {
643 self.grid.column_gap = Some(gap.into_prop());
644 self
645 }
646
647 /// Sets the padding: `vertical` above and below, `horizontal` left and
648 /// right, in logical pixels. It wins over the padding of the grid's
649 /// [`Style`], as a [`Row`]'s does.
650 pub fn padding(mut self, vertical: impl IntoProp<u32>, horizontal: impl IntoProp<u32>) -> Grid {
651 self.grid.padding_vertical = Some(vertical.into_prop());
652 self.grid.padding_horizontal = Some(horizontal.into_prop());
653 self
654 }
655
656 container_methods!();
657 style_method!();
658}
659
660impl Default for Grid {
661 fn default() -> Self {
662 Self::new()
663 }
664}
665
666impl Widget for Grid {
667 fn into_element(self: Box<Self>) -> Element {
668 Element {
669 name: "Grid",
670 common: self.common,
671 kind: ElementKind::Grid(self.grid),
672 children: describe(self.children),
673 }
674 }
675}
676
677/// Draws its children on top of each other in one cell, each stretched to
678/// the size of the largest. Later children are on top.
679pub struct Stack {
680 common: Common,
681 children: Vec<Child>,
682}
683
684impl Stack {
685 /// An empty stack.
686 pub fn new() -> Stack {
687 Stack {
688 common: Common::default(),
689 children: Vec::new(),
690 }
691 }
692
693 container_methods!();
694 style_method!();
695}
696
697impl Default for Stack {
698 fn default() -> Self {
699 Self::new()
700 }
701}
702
703impl Widget for Stack {
704 fn into_element(self: Box<Self>) -> Element {
705 Element {
706 name: "Stack",
707 common: self.common,
708 kind: ElementKind::Stack,
709 children: describe(self.children),
710 }
711 }
712}
713
714/// Takes the space left over in a [`Row`] or [`Column`]. Several spacers
715/// share it equally, or by their styles' [`grow`](Style::grow).
716pub struct Spacer {
717 common: Common,
718}
719
720impl Spacer {
721 /// A spacer.
722 pub fn new() -> Spacer {
723 Spacer {
724 common: Common::default(),
725 }
726 }
727
728 style_method!();
729}
730
731impl Default for Spacer {
732 fn default() -> Self {
733 Self::new()
734 }
735}
736
737impl Widget for Spacer {
738 fn into_element(self: Box<Self>) -> Element {
739 Element {
740 name: "Spacer",
741 common: self.common,
742 kind: ElementKind::Spacer,
743 children: Vec::new(),
744 }
745 }
746}
747
748/// Where text that doesn't fit is cut, with "…" in its place.
749#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
750pub enum Elide {
751 /// Not cut: the text keeps its width and overflows. The default.
752 #[default]
753 None,
754 /// The start of the last line shown.
755 Start,
756 /// The middle of the last line shown.
757 Middle,
758 /// The end of the last line shown.
759 End,
760}
761
762/// A line of text.
763///
764/// The text, `elide` and `max_lines` change the layout. Color, font,
765/// alignment, line height and decorations come from the nearest widget
766/// whose [`Style`] sets each, starting with the text's own.
767pub struct Text {
768 text: TextProps,
769 common: Common,
770}
771
772impl Text {
773 /// Text showing `text`: a string, or a closure that returns a `String`.
774 pub fn new(text: impl IntoProp<String>) -> Text {
775 Text {
776 text: TextProps {
777 text: Some(text.into_prop()),
778 spans: None,
779 elide: None,
780 max_lines: None,
781 selectable: None,
782 on_link: None,
783 link_hover_color: None,
784 },
785 common: Common::default(),
786 }
787 }
788
789 /// Text made of styled spans: a `Vec<Span>`, or a closure that returns
790 /// one. Each span takes what it doesn't set from the text, and
791 /// everything else a `Text` takes works the same.
792 pub fn rich(spans: impl IntoProp<Vec<Span>>) -> Text {
793 Text {
794 text: TextProps {
795 text: None,
796 spans: Some(spans.into_prop()),
797 elide: None,
798 max_lines: None,
799 selectable: None,
800 on_link: None,
801 link_hover_color: None,
802 },
803 common: Common::default(),
804 }
805 }
806
807 /// Runs `handler` with a link span's URL when a left click lands on
808 /// the span's glyphs. Over a link the cursor is the pointing hand and
809 /// the click goes to nothing else, not even a button around the text;
810 /// elsewhere the text lets the pointer through as before. In a
811 /// [`selectable`](Self::selectable) text, a press and release on one
812 /// link that selected nothing opens it, and a drag selects. Tab stops
813 /// on each link shown, in text order, and Enter or Space opens the one
814 /// focused. A link under the pointer or the focus draws underlined, in
815 /// [`link_hover_color`](Self::link_hover_color) if set. A handler that
816 /// panics is logged and disabled, and its links are plain text.
817 pub fn on_link(mut self, handler: impl Fn(&Cx, &str) + 'static) -> Text {
818 self.text.on_link = Some(Rc::new(Handler::new(handler)));
819 self
820 }
821
822 /// The color a link is drawn in while the pointer is over it or the
823 /// keyboard focus is on it: a color, `None`, or a closure returning
824 /// either. Either way the link is underlined; with `None`, the default,
825 /// it keeps its own color.
826 pub fn link_hover_color(mut self, color: impl IntoProp<Option<Color>>) -> Text {
827 self.text.link_hover_color = Some(color.into_prop());
828 self
829 }
830
831 /// When on, the text takes the pointer with an I-beam: a press, a drag,
832 /// a double click on a word, a triple click on a line and Shift+click
833 /// select it, as in a text field, and a
834 /// press takes the keyboard focus, so Ctrl+C copies the selection.
835 /// Inside a [`Button`], it takes the press, so the button doesn't
836 /// click. Tab doesn't stop on it. Off by default.
837 pub fn selectable(mut self, selectable: impl IntoProp<bool>) -> Text {
838 self.text.selectable = Some(selectable.into_prop());
839 self
840 }
841
842 /// When on, text that doesn't fit ends in "…" instead of being cut off.
843 /// Off by default. The same as [`elide`](Self::elide) with
844 /// [`Elide::End`] or [`Elide::None`]; whichever is called last wins.
845 pub fn ellipsis(mut self, ellipsis: impl IntoProp<bool>) -> Text {
846 let elide = ellipsis
847 .into_prop()
848 .map(|on| if on { Elide::End } else { Elide::None });
849 self.text.elide = Some(elide);
850 self
851 }
852
853 /// Where text that doesn't fit is cut, with "…" in its place:
854 /// [`Elide::None`] by default. With `max_lines` above 1, the last line
855 /// shown is cut.
856 pub fn elide(mut self, elide: impl IntoProp<Elide>) -> Text {
857 self.text.elide = Some(elide.into_prop());
858 self
859 }
860
861 /// Lets the text wrap onto at most this many lines, at word boundaries,
862 /// or inside a word too long for a line. The default is 1, which never
863 /// wraps. 0 counts as 1.
864 pub fn max_lines(mut self, max_lines: impl IntoProp<u32>) -> Text {
865 self.text.max_lines = Some(max_lines.into_prop());
866 self
867 }
868
869 style_method!();
870}
871
872impl Widget for Text {
873 fn into_element(self: Box<Self>) -> Element {
874 Element {
875 name: "Text",
876 common: self.common,
877 kind: ElementKind::Text(self.text),
878 children: Vec::new(),
879 }
880 }
881}
882
883/// An icon, drawn as a square.
884pub struct Icon {
885 icon: IconProps,
886 common: Common,
887}
888
889impl Icon {
890 /// The default size in logical pixels.
891 pub const DEFAULT_SIZE: u32 = 16;
892
893 /// An icon showing `source`: an icon name (`&str`), a file (`&Path` or
894 /// `PathBuf`), an [`IconSource`], or a closure that returns one.
895 pub fn new(source: impl IntoProp<IconSource>) -> Icon {
896 Icon {
897 icon: IconProps {
898 source: source.into_prop(),
899 fallback: None,
900 size: None,
901 tint: None,
902 },
903 common: Common::default(),
904 }
905 }
906
907 /// Sets the width and height in logical pixels.
908 pub fn size(mut self, size: impl IntoProp<u32>) -> Icon {
909 self.icon.size = Some(size.into_prop());
910 self
911 }
912
913 /// Paints the icon's pixels in `tint`, keeping their alpha, as a
914 /// symbolic icon's color follows the theme. `None`, the default, draws
915 /// it as it is.
916 pub fn tint(mut self, tint: impl IntoProp<Option<Color>>) -> Icon {
917 self.icon.tint = Some(tint.into_prop());
918 self
919 }
920
921 /// What to draw when the icon's own source isn't in the theme or its
922 /// file can't be loaded, taking what [`Icon::new`] takes. A chain of
923 /// fallbacks picks one with `cx.icon_path`.
924 pub fn fallback(mut self, fallback: impl IntoProp<IconSource>) -> Icon {
925 self.icon.fallback = Some(fallback.into_prop());
926 self
927 }
928
929 style_method!();
930}
931
932/// A picture from a PNG, JPEG, GIF, WebP or SVG file, drawn at a fixed
933/// size.
934///
935/// A GIF of more than one frame plays while it is drawn, at its own
936/// delays, looping as the file says. The filters run once on the picture
937/// at the size drawn, and the result is cached, so a filtered image costs
938/// what a plain one does to draw.
939pub struct Image {
940 image: ImageProps,
941 common: Common,
942}
943
944impl Image {
945 /// The default width and height in logical pixels.
946 pub const DEFAULT_SIZE: u32 = 64;
947
948 /// An image of the file at `source`: a path (`&str`, `String`, `&Path`
949 /// or `PathBuf`) or a closure that returns one.
950 pub fn new(source: impl IntoProp<PathBuf>) -> Image {
951 Image {
952 image: ImageProps {
953 source: source.into_prop(),
954 width: None,
955 height: None,
956 fit: None,
957 tint: None,
958 brightness: None,
959 contrast: None,
960 saturation: None,
961 blur: None,
962 playing: None,
963 transition: None,
964 },
965 common: Common::default(),
966 }
967 }
968
969 /// Sets the width in logical pixels.
970 pub fn width(mut self, width: impl IntoProp<u32>) -> Image {
971 self.image.width = Some(width.into_prop());
972 self
973 }
974
975 /// Sets the height in logical pixels.
976 pub fn height(mut self, height: impl IntoProp<u32>) -> Image {
977 self.image.height = Some(height.into_prop());
978 self
979 }
980
981 /// Sets how the picture fills the image's box when their shapes differ.
982 pub fn fit(mut self, fit: impl IntoProp<Fit>) -> Image {
983 self.image.fit = Some(fit.into_prop());
984 self
985 }
986
987 /// Paints the picture's pixels in `tint`, keeping their alpha, as
988 /// [`Icon::tint`] does. `None`, the default, draws it as it is.
989 pub fn tint(mut self, tint: impl IntoProp<Option<Color>>) -> Image {
990 self.image.tint = Some(tint.into_prop());
991 self
992 }
993
994 /// Scales the colors, as CSS's `brightness()`: 1, the default, leaves
995 /// them, 0 is black and 2 twice as bright. A number that isn't finite
996 /// counts as 1, and one below 0 as 0.
997 pub fn brightness(mut self, brightness: impl IntoProp<f32>) -> Image {
998 self.image.brightness = Some(brightness.into_prop());
999 self
1000 }
1001
1002 /// Sets the contrast, as CSS's `contrast()`: 1, the default, leaves the
1003 /// colors, 0 is middle gray and more than 1 is stronger. A number that
1004 /// isn't finite counts as 1, and one below 0 as 0.
1005 pub fn contrast(mut self, contrast: impl IntoProp<f32>) -> Image {
1006 self.image.contrast = Some(contrast.into_prop());
1007 self
1008 }
1009
1010 /// Sets the saturation, as CSS's `saturate()`: 1, the default, leaves
1011 /// the colors, 0 is gray and more than 1 is more vivid. A number that
1012 /// isn't finite counts as 1, and one below 0 as 0.
1013 pub fn saturation(mut self, saturation: impl IntoProp<f32>) -> Image {
1014 self.image.saturation = Some(saturation.into_prop());
1015 self
1016 }
1017
1018 /// Blurs the picture, as CSS's `blur()`: the Gaussian's standard
1019 /// deviation in logical pixels, 0 by default. Pixels past the picture's
1020 /// edge count as its edge pixels, so a blurred picture keeps its edges
1021 /// opaque. A number that isn't finite or is below 0 counts as 0. The
1022 /// blur is for still pictures, such as album art and wallpapers: a
1023 /// blurred GIF shows its first frame, still.
1024 pub fn blur(mut self, blur: impl IntoProp<f32>) -> Image {
1025 self.image.blur = Some(blur.into_prop());
1026 self
1027 }
1028
1029 /// Whether an animated GIF plays, true by default. False pauses it on
1030 /// the frame it shows.
1031 pub fn playing(mut self, playing: impl IntoProp<bool>) -> Image {
1032 self.image.playing = Some(playing.into_prop());
1033 self
1034 }
1035
1036 /// How a new source comes in: [`ImageTransition::None`], the default,
1037 /// shows it at once; the others bring it in over the old picture on the
1038 /// animation clock, repainting only the image's box. A new source
1039 /// mid-transition starts again from the picture most of the box shows:
1040 /// the one leaving before halfway, the one coming in from halfway on.
1041 /// A new source that can't be loaded keeps the old picture; the clock
1042 /// still runs and repaints the box for the transition's length, since
1043 /// only the renderer learns the file is bad.
1044 pub fn transition(mut self, transition: impl IntoProp<ImageTransition>) -> Image {
1045 self.image.transition = Some(transition.into_prop());
1046 self
1047 }
1048
1049 style_method!();
1050}
1051
1052impl Widget for Image {
1053 fn into_element(self: Box<Self>) -> Element {
1054 Element {
1055 name: "Image",
1056 common: self.common,
1057 kind: ElementKind::Image(self.image),
1058 children: Vec::new(),
1059 }
1060 }
1061}
1062
1063impl Widget for Icon {
1064 fn into_element(self: Box<Self>) -> Element {
1065 Element {
1066 name: "Icon",
1067 common: self.common,
1068 kind: ElementKind::Icon(self.icon),
1069 children: Vec::new(),
1070 }
1071 }
1072}
1073
1074/// A clickable box around one child, which it centers.
1075///
1076/// While the pointer is over it, `hover_style` is laid over its style, and
1077/// while it is also held down, `pressed_style` on top of that. Handlers run
1078/// on the main thread and receive the button's [`Cx`]. A handler that panics
1079/// is logged and never called again.
1080pub struct Button {
1081 pub(crate) button: ButtonProps,
1082 common: Common,
1083 child: Box<dyn Widget>,
1084}
1085
1086impl Button {
1087 /// A button around `child`.
1088 pub fn new(child: impl Widget) -> Button {
1089 Button {
1090 child: Box::new(child),
1091 button: ButtonProps {
1092 hover_style: None,
1093 pressed_style: None,
1094 focus_style: None,
1095 hovered: None,
1096 position: None,
1097 enabled: None,
1098 focus: None,
1099 cursor: None,
1100 drag_data: None,
1101 drop_hovered: None,
1102 handlers: Handlers::default(),
1103 value: None,
1104 },
1105 common: Common::default(),
1106 }
1107 }
1108
1109 /// Sets the style laid over the normal one while the pointer is over the
1110 /// button.
1111 pub fn hover_style(mut self, style: impl IntoProp<Style>) -> Button {
1112 self.button.hover_style = Some(style.into_prop());
1113 self
1114 }
1115
1116 /// Shares whether the pointer is over the button: the button sets
1117 /// `hovered` to `true` while the pointer is over it or anything inside
1118 /// it, buttons too, and back to `false` when it leaves, the window loses
1119 /// it, or the button goes. Unlike `hover_style`, which only the deepest
1120 /// button under the pointer shows, an outer button stays hovered while
1121 /// the pointer is over one inside it, so it can show controls on hover.
1122 /// A drag of this button or one inside it keeps it hovered until the
1123 /// release.
1124 pub fn hovered(mut self, hovered: Signal<bool>) -> Button {
1125 self.button.hovered = Some(hovered);
1126 self
1127 }
1128
1129 /// Sets the style laid over the hover style while the button is held
1130 /// down.
1131 pub fn pressed_style(mut self, style: impl IntoProp<Style>) -> Button {
1132 self.button.pressed_style = Some(style.into_prop());
1133 self
1134 }
1135
1136 /// Sets the style laid over the normal one while the button has the
1137 /// keyboard focus, which Tab moves between buttons.
1138 pub fn focus_style(mut self, style: impl IntoProp<Style>) -> Button {
1139 self.button.focus_style = Some(style.into_prop());
1140 self
1141 }
1142
1143 /// Runs `handler` for each key pressed while the button has the
1144 /// keyboard focus. It returns `true` when it used the key; otherwise the
1145 /// window's `on_key` gets it, and then Tab, Enter and Space do what
1146 /// they do by default.
1147 pub fn on_key(mut self, handler: impl Fn(&Cx, &KeyEvent) -> bool + 'static) -> Button {
1148 self.button.handlers.key = Some(Rc::new(Handler::new(handler)));
1149 self
1150 }
1151
1152 /// Runs `handler` as soon as the left button goes down over this
1153 /// button, while it is held: the time to call
1154 /// [`Cx::start_move`](crate::Cx::start_move), as a title bar does.
1155 pub fn on_press(mut self, handler: impl Fn(&Cx) + 'static) -> Button {
1156 self.button.handlers.press = Some(Rc::new(Handler::new(handler)));
1157 self
1158 }
1159
1160 /// Runs `handler` when the left button comes up after going down on
1161 /// this button, wherever the pointer is by then, before `on_click`.
1162 pub fn on_release(mut self, handler: impl Fn(&Cx) + 'static) -> Button {
1163 self.button.handlers.release = Some(Rc::new(Handler::new(handler)));
1164 self
1165 }
1166
1167 /// Runs `handler` when the left button is pressed and released over this
1168 /// button, unless that ended a drag or a long press, or made a double
1169 /// click.
1170 pub fn on_click(mut self, handler: impl Fn(&Cx) + 'static) -> Button {
1171 self.button.handlers.click = Some(Rc::new(Handler::new(handler)));
1172 self
1173 }
1174
1175 /// Runs `handler` instead of a second `on_click` when a click comes
1176 /// within 400 ms and 5 logical pixels of the one before, as Qt's
1177 /// defaults say. A third click starts over. Without it, every click
1178 /// runs `on_click`.
1179 pub fn on_double_click(mut self, handler: impl Fn(&Cx) + 'static) -> Button {
1180 self.button.handlers.double_click = Some(Rc::new(Handler::new(handler)));
1181 self
1182 }
1183
1184 /// Runs `handler` once the left button has been held on this button for
1185 /// 800 ms without moving 10 logical pixels, as Qt's press and hold. The
1186 /// release after it doesn't click, though moving on can still drag.
1187 pub fn on_long_press(mut self, handler: impl Fn(&Cx) + 'static) -> Button {
1188 self.button.handlers.long_press = Some(Rc::new(Handler::new(handler)));
1189 self
1190 }
1191
1192 /// Runs `handler` as the left button drags this button: a
1193 /// [`DragPhase::Start`](crate::DragPhase::Start) once it moves 10
1194 /// logical pixels from where it went down, a `Move` for each motion
1195 /// after, and an `End` when it comes up, the pointer leaves the window,
1196 /// or the button is disabled. A button with no `on_click`,
1197 /// `on_double_click` or `on_long_press` starts at the press instead, so
1198 /// a slider moves from the first touch.
1199 ///
1200 /// While it drags, the button gets every motion, even outside itself or
1201 /// the window, stays hovered, and doesn't click.
1202 pub fn on_drag(mut self, handler: impl Fn(&Cx, Drag) + 'static) -> Button {
1203 self.button.handlers.drag = Some(Rc::new(Handler::new(handler)));
1204 self
1205 }
1206
1207 /// Runs `handler` when the right button is pressed and released over this
1208 /// button.
1209 pub fn on_right_click(mut self, handler: impl Fn(&Cx) + 'static) -> Button {
1210 self.button.handlers.right_click = Some(Rc::new(Handler::new(handler)));
1211 self
1212 }
1213
1214 /// Runs `handler` when the middle button is pressed and released over
1215 /// this button.
1216 pub fn on_middle_click(mut self, handler: impl Fn(&Cx) + 'static) -> Button {
1217 self.button.handlers.middle_click = Some(Rc::new(Handler::new(handler)));
1218 self
1219 }
1220
1221 /// Runs `handler` when the mouse's back side button is pressed and
1222 /// released over this button.
1223 pub fn on_back(mut self, handler: impl Fn(&Cx) + 'static) -> Button {
1224 self.button.handlers.back = Some(Rc::new(Handler::new(handler)));
1225 self
1226 }
1227
1228 /// Runs `handler` when the mouse's forward side button is pressed and
1229 /// released over this button.
1230 pub fn on_forward(mut self, handler: impl Fn(&Cx) + 'static) -> Button {
1231 self.button.handlers.forward = Some(Rc::new(Handler::new(handler)));
1232 self
1233 }
1234
1235 /// Runs `handler` with whole scroll steps while the pointer is over this
1236 /// button: positive for up, negative for down. A mouse wheel gives one
1237 /// step per notch; touchpad scrolling gives one step per 15 logical
1238 /// pixels. Sideways scrolling gives none.
1239 pub fn on_scroll(mut self, handler: impl Fn(&Cx, i32) + 'static) -> Button {
1240 self.button.handlers.scroll = Some(Rc::new(Handler::new(handler)));
1241 self
1242 }
1243
1244 /// Runs `handler` with every scroll event while the pointer is over
1245 /// this button, on both axes: a wheel's notches, a touchpad's pixels,
1246 /// where it came from, and the `stop` when the fingers lift. Positive
1247 /// is down and right. With `on_scroll` too, both run.
1248 pub fn on_wheel(mut self, handler: impl Fn(&Cx, Wheel) + 'static) -> Button {
1249 self.button.handlers.wheel = Some(Rc::new(Handler::new(handler)));
1250 self
1251 }
1252
1253 /// Shares where the pointer is: the button writes `position`, in its
1254 /// own coordinates with (0, 0) at its top-left, while the pointer is
1255 /// over it or anything inside it, and `None` when it leaves. While a
1256 /// button inside it, or this one, is dragged, it follows the pointer
1257 /// wherever it goes, even outside the button. It is written only on a
1258 /// change.
1259 pub fn pointer_position(mut self, position: Signal<Option<Point>>) -> Button {
1260 self.button.position = Some(position);
1261 self
1262 }
1263
1264 /// Turns the button on or off; it is on by default. Off, none of its
1265 /// handlers run, it shows no hover, pressed or focus style and the
1266 /// default cursor, and Tab skips it. The pointer passes through it to
1267 /// the button around it, as through a disabled Qt `MouseArea`. Turning
1268 /// it off while it is held lets go, ending a drag with its `End`, and
1269 /// drops its focus.
1270 pub fn enabled(mut self, enabled: impl IntoProp<bool>) -> Button {
1271 self.button.enabled = Some(enabled.into_prop());
1272 self
1273 }
1274
1275 /// Takes the keyboard focus whenever this turns true, as
1276 /// [`TextInput::focus`](crate::TextInput::focus) does. Tab and a field
1277 /// may take it away again.
1278 pub fn focus(mut self, focus: impl IntoProp<bool>) -> Button {
1279 self.button.focus = Some(focus.into_prop());
1280 self
1281 }
1282
1283 /// Makes a drag of this button carry `data`, which a button whose
1284 /// [`on_drop`](Self::on_drop) takes a `T` is given when the drag is
1285 /// released over it. A closure is read as it changes, and a drop gets
1286 /// the value it holds at the release. While it holds a value, the button
1287 /// drags as one with [`on_drag`](Self::on_drag) does, and its `on_drag`
1288 /// still runs, so it can follow the pointer with a translate; its `End`
1289 /// says whether it was dropped. While it holds `None`, the button acts as
1290 /// if it had no data: it clicks, and drags only with its own `on_drag`,
1291 /// so `editing.get().then_some(id)` drags only in an edit mode. Once
1292 /// the pointer leaves the window, the drag reaches buttons in the
1293 /// shell's other windows too, and other programs see one they can't
1294 /// take.
1295 pub fn drag_data<T: Clone + 'static>(mut self, data: impl IntoProp<Option<T>>) -> Button {
1296 let data = data
1297 .into_prop()
1298 .map(|data| data.map(|value| Rc::new(value) as DragValue));
1299 self.button.drag_data = Some(data);
1300 self
1301 }
1302
1303 /// Runs `handler` with a copy of the [`drag_data`](Self::drag_data) of a
1304 /// drag released over this button, or over anything inside it, when
1305 /// that data is a `T`. Data of any other type passes over it, as if it
1306 /// weren't there. The deepest button under the pointer that takes it is
1307 /// the one dropped on, and the dragged button never takes its own.
1308 pub fn on_drop<T: Clone + 'static>(mut self, handler: impl Fn(&Cx, T) + 'static) -> Button {
1309 let take = move |cx: &Cx, value: &dyn std::any::Any| {
1310 if let Some(value) = value.downcast_ref::<T>() {
1311 handler(cx, value.clone());
1312 }
1313 };
1314 self.button.handlers.drop = Some(DropTarget {
1315 accepts: std::any::TypeId::of::<T>(),
1316 handler: Rc::new(Handler::new(take)),
1317 name: "on_drop",
1318 });
1319 self
1320 }
1321
1322 /// Makes a drag of this button carry `content`, as
1323 /// [`drag_data`](Self::drag_data) does, to buttons in any of the
1324 /// shell's windows and, once the pointer leaves the window, to other
1325 /// programs, as text or a URI list. It replaces any `drag_data`.
1326 pub fn drag_content(self, content: impl IntoProp<Option<DragContent>>) -> Button {
1327 self.drag_data(content)
1328 }
1329
1330 /// Runs `handler` with what a drag from another program, or a
1331 /// [`drag_content`](Self::drag_content) of the shell's, carries when it
1332 /// is released over this button: URIs when the program offers them,
1333 /// else text. It replaces any [`on_drop`](Self::on_drop).
1334 pub fn on_drop_content(mut self, handler: impl Fn(&Cx, DragContent) + 'static) -> Button {
1335 self = self.on_drop(handler);
1336 if let Some(target) = &mut self.button.handlers.drop {
1337 target.name = "on_drop_content";
1338 }
1339 self
1340 }
1341
1342 /// Shares whether a drop is waiting: the button writes `hovered` true
1343 /// while a drag whose data its [`on_drop`](Self::on_drop) would take is
1344 /// over it, as Qt's `DropArea.containsDrag`, and false when the drag
1345 /// leaves, ends, or either button goes, is hidden or is turned off.
1346 pub fn drop_hovered(mut self, hovered: Signal<bool>) -> Button {
1347 self.button.drop_hovered = Some(hovered);
1348 self
1349 }
1350
1351 /// Sets the cursor shown over the button, instead of the pointing hand.
1352 /// While the button drags, the pointing hand and the open hand show as
1353 /// the grabbing hand until the release; any other shape stays.
1354 pub fn cursor(mut self, cursor: impl IntoProp<CursorShape>) -> Button {
1355 self.button.cursor = Some(cursor.into_prop());
1356 self
1357 }
1358
1359 style_method!();
1360}
1361
1362impl Widget for Button {
1363 fn into_element(self: Box<Self>) -> Element {
1364 Element {
1365 name: "Button",
1366 common: self.common,
1367 kind: ElementKind::Button(Box::new(self.button)),
1368 children: vec![ElementChild::One(Box::new(self.child.into_element()))],
1369 }
1370 }
1371}