1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
use crate::prelude::*;
/// A simple push button with a contained view.
///
/// # Examples
///
/// ## Button with an action
///
/// A button can be used to call an action when interacted with. Usually this is an
/// event that is being emitted when the button is [pressed](crate::modifiers::ActionModifiers::on_press).
///
/// ```
/// # use vizia_core::prelude::*;
/// #
/// # enum AppEvent {
/// # Action,
/// # }
/// #
/// # let cx = &mut Context::default();
/// #
/// Button::new(cx, |cx| Label::new(cx, "Text"))
/// .on_press(|ex| ex.emit(AppEvent::Action))
/// ```
///
/// ## Button without an action
///
/// A button can be used without an action and therefore do nothing when pressed.
/// This is useful for prototyping and testing out the different styling options of
/// a button without having to add an action.
///
/// ```
/// # use vizia_core::prelude::*;
/// #
/// # let cx = &mut Context::default();
/// #
/// Button::new(cx, |cx| Label::new(cx, "Text"));
/// ```
///
/// ## Button containing multiple views
///
/// A button can contain more than just a single view or label inside of it. This can
/// for example be done by using a [`HStack`](crate::prelude::HStack) or [`VStack`](crate::prelude::VStack).
///
/// ```
/// # use vizia_core::prelude::*;
/// #
/// # let cx = &mut Context::default();
/// #
/// Button::new(
/// cx,
/// |cx| {
/// HStack::new(cx, |cx| {
/// Label::new(cx, "Hello");
/// Label::new(cx, "World");
/// })
/// },
/// );
/// ```
///
/// # Button Variants
///
/// The style of a button can be modified using the [`variant`](ButtonModifiers::variant) modifier from the [`ButtonModifiers`] trait
/// by specifying the [`ButtonVariant`].
///
/// ```
/// # use vizia_core::prelude::*;
/// #
/// # let cx = &mut Context::default();
/// #
/// Button::new(cx, |cx| Label::new(cx, "Text"))
/// .variant(ButtonVariant::Accent);
/// ```
pub struct Button {
pub(crate) action: Option<Box<dyn Fn(&mut EventContext)>>,
}
impl Button {
/// Creates a new button with specified content.
///
/// # Example
/// ```
/// # use vizia_core::prelude::*;
/// #
/// # let cx = &mut Context::default();
/// #
/// Button::new(cx, |cx| Label::new(cx, "Press Me"));
/// ```
pub fn new<C, V>(cx: &mut Context, content: C) -> Handle<Self>
where
C: FnOnce(&mut Context) -> Handle<V>,
V: View,
{
Self { action: None }
.build(cx, move |cx| {
(content)(cx).hoverable(false);
})
.role(Role::Button)
.default_action_verb(DefaultActionVerb::Click)
.navigable(true)
}
}
impl View for Button {
fn element(&self) -> Option<&'static str> {
Some("button")
}
fn event(&mut self, cx: &mut EventContext, event: &mut Event) {
event.map(|window_event, meta| match window_event {
WindowEvent::PressDown { mouse: _ } => {
if meta.target == cx.current {
cx.focus();
}
}
WindowEvent::Press { .. } => {
if meta.target == cx.current {
if let Some(action) = &self.action {
(action)(cx);
}
}
}
WindowEvent::ActionRequest(action) => match action.action {
Action::Default => {
if let Some(action) = &self.action {
(action)(cx);
}
}
_ => {}
},
_ => {}
});
}
}
/// Used in conjunction with the [`variant`](ButtonModifiers::variant) modifier for selecting the style variant of a button or button group.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ButtonVariant {
Normal,
Accent,
Outline,
Text,
}
/// Modifiers for changing the appearance of buttons.
pub trait ButtonModifiers {
/// Selects the style variant to be used by the button or button group.
///
/// # Example
/// ```
/// # use vizia_core::prelude::*;
/// #
/// #
/// # let cx = &mut Context::default();
/// #
/// Button::new(cx, |cx| Label::new(cx, "Text"))
/// .variant(ButtonVariant::Accent);
/// ```
fn variant<U: Into<ButtonVariant>>(self, variant: impl Res<U>) -> Self;
}
impl<'a> ButtonModifiers for Handle<'a, Button> {
fn variant<U: Into<ButtonVariant>>(mut self, variant: impl Res<U>) -> Self {
let entity = self.entity();
variant.set_or_bind(self.context(), entity, |cx, val| {
let var: ButtonVariant = val.get(cx).into();
match var {
ButtonVariant::Normal => {
cx.toggle_class("accent", false);
cx.toggle_class("outline", false);
cx.toggle_class("text", false);
}
ButtonVariant::Accent => {
cx.toggle_class("accent", true);
cx.toggle_class("outline", false);
cx.toggle_class("text", false);
}
ButtonVariant::Outline => {
cx.toggle_class("accent", false);
cx.toggle_class("outline", true);
cx.toggle_class("text", false);
}
ButtonVariant::Text => {
cx.toggle_class("accent", false);
cx.toggle_class("outline", false);
cx.toggle_class("text", true);
}
}
});
self
}
}
pub struct ButtonGroup {}
impl ButtonGroup {
/// Creates a new button group.
///
/// # Example
/// ```
/// # use vizia_core::prelude::*;
/// #
/// # enum AppEvent {
/// # Share,
/// # }
///
/// # let cx = &mut Context::default();
/// #
/// use vizia::icons::ICON_SHARE;
///
/// ButtonGroup::new(cx, |cx| {
/// Button::new(cx, |cx| Label::new(cx, "ONE"));
/// Button::new(cx, |cx| Label::new(cx, "TWO"));
/// Button::new(cx, |cx| Label::new(cx, "THREE"));
/// });
/// ```
pub fn new<C>(cx: &mut Context, content: C) -> Handle<Self>
where
C: FnOnce(&mut Context),
{
Self {}.build(cx, |cx| {
(content)(cx);
})
}
}
impl View for ButtonGroup {
fn element(&self) -> Option<&'static str> {
Some("button-group")
}
}
impl<'a> Handle<'a, ButtonGroup> {
pub fn vertical(self, is_vertical: impl Res<bool>) -> Self {
self.toggle_class("vertical", is_vertical)
}
}
impl<'a> ButtonModifiers for Handle<'a, ButtonGroup> {
fn variant<U: Into<ButtonVariant>>(mut self, variant: impl Res<U>) -> Self {
let entity = self.entity();
variant.set_or_bind(self.context(), entity, |cx, val| {
let var: ButtonVariant = val.get(cx).into();
match var {
ButtonVariant::Normal => {
cx.toggle_class("accent", false);
cx.toggle_class("outline", false);
cx.toggle_class("text", false);
}
ButtonVariant::Accent => {
cx.toggle_class("accent", true);
cx.toggle_class("outline", false);
cx.toggle_class("text", false);
}
ButtonVariant::Outline => {
cx.toggle_class("accent", false);
cx.toggle_class("outline", true);
cx.toggle_class("text", false);
}
ButtonVariant::Text => {
cx.toggle_class("accent", false);
cx.toggle_class("outline", false);
cx.toggle_class("text", true);
}
}
});
self
}
}