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