Skip to main content

waft_winui/controls/
hyperlink_button.rs

1//! XAML `HyperlinkButton` with the `DefaultHyperlinkButtonStyle` of `HyperlinkButton_themeresources.xaml`.
2//!
3//! The template is one `ContentPresenter` (`x:Name="ContentPresenter"`) with `BackgroundSizing="OuterBorderEdge"`, `ButtonPadding`, `HyperlinkButtonBorderThemeThickness` and a `BrushTransition` of 83 ms on `Background`; the `CommonStates` storyboards swap `Foreground`, `Background` and `BorderBrush` per state. The text carries no underline: `Hyperlink_themeresources.xaml` sets `HyperlinkUnderlineVisible` to `False`, which `HyperlinkButton::UpdateContentPresenterTextUnderline` reads (the underline returns only in high contrast). `HyperlinkButton::Initialize` sets the hand cursor.
4
5use crate::{
6    BackgroundSizing, Brush, ColorTransition, CommonState, CommonStates, ControlBorder,
7    ControlStates, FocusVisual, ThemeResources, control_text_style,
8};
9use std::{fmt, rc::Rc};
10use waft_embedder::{Color, FontWeight};
11use waft_foundation::{App, Listener};
12use waft_services::SystemMouseCursors;
13use waft_widgets::*;
14
15/// The `FocusVisualMargin` the style sets.
16pub const HYPERLINK_BUTTON_FOCUS_VISUAL_MARGIN: [f64; 4] = [-3.0; 4];
17
18/// The brushes the style resolves to in one state.
19#[derive(Clone, Copy, Debug, PartialEq)]
20pub struct HyperlinkButtonBrushes {
21    pub background: Color,
22    pub border: Brush,
23    pub foreground: Color,
24}
25
26/// The `{ThemeResource …}` values the `CommonStates` storyboards name for `state`.
27pub fn hyperlink_button_brushes(
28    resources: &ThemeResources,
29    state: CommonState,
30) -> HyperlinkButtonBrushes {
31    let r = resources;
32    match state {
33        CommonState::Normal => HyperlinkButtonBrushes {
34            background: r.hyperlink_button_background,
35            border: Brush::Solid(r.hyperlink_button_border_brush),
36            foreground: r.hyperlink_button_foreground,
37        },
38        CommonState::PointerOver => HyperlinkButtonBrushes {
39            background: r.hyperlink_button_background_pointer_over,
40            border: Brush::Solid(r.hyperlink_button_border_brush_pointer_over),
41            foreground: r.hyperlink_button_foreground_pointer_over,
42        },
43        CommonState::Pressed => HyperlinkButtonBrushes {
44            background: r.hyperlink_button_background_pressed,
45            border: Brush::Solid(r.hyperlink_button_border_brush_pressed),
46            foreground: r.hyperlink_button_foreground_pressed,
47        },
48        CommonState::Disabled => HyperlinkButtonBrushes {
49            background: r.hyperlink_button_background_disabled,
50            border: Brush::Solid(r.hyperlink_button_border_brush_disabled),
51            foreground: r.hyperlink_button_foreground_disabled,
52        },
53    }
54}
55
56/// XAML `HyperlinkButton`: `Content` in the accent foreground, `Click` raised on activation. `NavigateUri` is not offered: launching it is the Windows shell's (`Launcher::TryInvokeLauncher`); a caller opens its link in `click`.
57#[derive(Clone)]
58pub struct HyperlinkButton {
59    pub content: WidgetRef,
60    pub click: Listener,
61    pub is_enabled: bool,
62    pub key: Option<KeyRef>,
63}
64
65impl HyperlinkButton {
66    #[cfg_attr(debug_assertions, track_caller)]
67    pub fn new<K>(content: impl IntoWidget<K>, click: Listener) -> HyperlinkButton {
68        HyperlinkButton {
69            content: content.into_widget(),
70            click,
71            is_enabled: true,
72            key: None,
73        }
74    }
75
76    /// A text `Content`, as `<HyperlinkButton Content="…"/>`.
77    pub fn text(text: impl Into<String>, click: Listener) -> HyperlinkButton {
78        HyperlinkButton::new(Text::new(text), click)
79    }
80
81    /// XAML `IsEnabled`.
82    pub fn is_enabled(mut self, enabled: bool) -> HyperlinkButton {
83        self.is_enabled = enabled;
84        self
85    }
86
87    pub fn key(mut self, key: KeyRef) -> HyperlinkButton {
88        self.key = Some(key);
89        self
90    }
91}
92
93impl fmt::Debug for HyperlinkButton {
94    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
95        f.debug_struct("HyperlinkButton")
96            .field("is_enabled", &self.is_enabled)
97            .finish_non_exhaustive()
98    }
99}
100
101impl StatelessWidget for HyperlinkButton {
102    fn key(&self) -> Option<&KeyRef> {
103        self.key.as_ref()
104    }
105
106    fn build(&self, _app: &mut App, _context: BuildContext) -> WidgetRef {
107        let content = self.content.clone();
108        let states = CommonStates::new(self.click.clone(), move |app, context, states| {
109            template(app, context, content.clone(), states)
110        })
111        .is_enabled(self.is_enabled);
112        // `HyperlinkButton::Initialize`: `SetCursor(MouseCursorHand)`.
113        MouseRegion::new()
114            .cursor(Rc::new(SystemMouseCursors::CLICK))
115            .child(states)
116            .into_widget()
117    }
118}
119
120/// The `ControlTemplate` of `DefaultHyperlinkButtonStyle` for one set of states.
121fn template(
122    app: &mut App,
123    context: BuildContext,
124    content: WidgetRef,
125    states: ControlStates,
126) -> WidgetRef {
127    let resources = ThemeResources::of(app, context);
128    let brushes = hyperlink_button_brushes(&resources, states.common);
129    let text = control_text_style(
130        &resources,
131        resources.control_content_theme_font_size,
132        FontWeight::W400,
133        brushes.foreground,
134    );
135    // `ContentPresenter`: `HorizontalContentAlignment` and `VerticalContentAlignment` are Center.
136    let label = Center::new()
137        .width_factor(1.0)
138        .height_factor(1.0)
139        .child(DefaultTextStyle::new(text, content))
140        .into_widget();
141    let border_thickness = resources.hyperlink_button_border_theme_thickness[0];
142    let (corner_radius, padding) = (resources.control_corner_radius[0], resources.button_padding);
143    let presenter = ColorTransition::new(
144        brushes.background,
145        resources.control_faster_animation_duration,
146        move |_app, background| {
147            ControlBorder::new(Brush::Solid(background), brushes.border)
148                .border_thickness(border_thickness)
149                .corner_radius(corner_radius)
150                .background_sizing(BackgroundSizing::OuterBorderEdge)
151                .padding(padding)
152                .child(label.clone())
153                .into_widget()
154        },
155    );
156    FocusVisual::new(presenter, resources.theme)
157        .visible(states.focused)
158        .margin(HYPERLINK_BUTTON_FOCUS_VISUAL_MARGIN)
159        .corner_radius(resources.control_corner_radius[0])
160        .into_widget()
161}