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 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
use floem_reactive::{create_effect, create_updater};
use floem_winit::keyboard::{Key, ModifiersState};
use kurbo::{Point, Rect};
use crate::{
action::{set_window_menu, set_window_title, update_window_scale},
animate::Animation,
event::{Event, EventListener},
menu::Menu,
style::{Style, StyleClass, StyleSelector},
view::View,
EventPropagation,
};
pub trait Decorators: View + Sized {
/// Alter the style of the view.
///
/// Earlier applications of `style` have lower priority than later calls.
/// ```rust
/// # use floem::{peniko::Color, view::View, views::{Decorators, label, stack}};
/// fn view() -> impl View {
/// label(|| "Hello".to_string())
/// .style(|s| s.font_size(20.0).color(Color::RED))
/// }
///
/// fn other() -> impl View {
/// stack((
/// view(), // will be red and size 20
/// // will be green and default size due to the previous style being overwritten
/// view().style(|s| s.color(Color::GREEN)),
/// ))
/// }
/// ```
fn style(mut self, style: impl Fn(Style) -> Style + 'static) -> Self {
let id = self.id();
let offset = self.view_data_mut().style.next_offset();
let style = create_updater(
move || style(Style::new()),
move |style| id.update_style(style, offset),
);
self.view_data_mut().style.push(style);
self
}
/// The visual style to apply when the mouse hovers over the element
fn dragging_style(self, style: impl Fn(Style) -> Style + 'static) -> Self {
let id = self.id();
create_effect(move |_| {
let style = style(Style::new());
id.update_style_selector(style, StyleSelector::Dragging);
});
self
}
fn class<C: StyleClass>(self, _class: C) -> Self {
self.id().update_class(C::class_ref());
self
}
/// Allows the element to be navigated to with the keyboard. Similar to setting tabindex="0" in html.
fn keyboard_navigatable(self) -> Self {
let id = self.id();
id.keyboard_navigatable();
self
}
fn draggable(self) -> Self {
let id = self.id();
id.draggable();
self
}
fn disabled(self, disabled_fn: impl Fn() -> bool + 'static) -> Self {
let id = self.id();
create_effect(move |_| {
let is_disabled = disabled_fn();
id.update_disabled(is_disabled);
});
self
}
/// Add an event handler for the given [EventListener].
fn on_event(
self,
listener: EventListener,
action: impl Fn(&Event) -> EventPropagation + 'static,
) -> Self {
let id = self.id();
id.update_event_listener(listener, Box::new(action));
self
}
/// Add an handler for pressing down a specific key.
fn on_key_down(
mut self,
key: Key,
modifiers: ModifiersState,
action: impl Fn(&Event) + 'static,
) -> Self {
self.view_data_mut().event_handlers.push(Box::new(move |e| {
if let Event::KeyDown(ke) = e {
if ke.key.logical_key == key && ke.modifiers == modifiers {
action(e);
return EventPropagation::Stop;
}
}
EventPropagation::Continue
}));
self
}
/// Add an handler for a specific key being released.
fn on_key_up(
mut self,
key: Key,
modifiers: ModifiersState,
action: impl Fn(&Event) + 'static,
) -> Self {
self.view_data_mut().event_handlers.push(Box::new(move |e| {
if let Event::KeyUp(ke) = e {
if ke.key.logical_key == key && ke.modifiers == modifiers {
action(e);
return EventPropagation::Stop;
}
}
EventPropagation::Continue
}));
self
}
/// Add an event handler for the given [EventListener]. This event will be handled with
/// the given handler and the event will continue propagating.
fn on_event_cont(self, listener: EventListener, action: impl Fn(&Event) + 'static) -> Self {
self.on_event(listener, move |e| {
action(e);
EventPropagation::Continue
})
}
/// Add an event handler for the given [EventListener]. This event will be handled with
/// the given handler and the event will stop propagating.
fn on_event_stop(self, listener: EventListener, action: impl Fn(&Event) + 'static) -> Self {
self.on_event(listener, move |e| {
action(e);
EventPropagation::Stop
})
}
/// Add an event handler for [EventListener::Click].
fn on_click(self, action: impl Fn(&Event) -> EventPropagation + 'static) -> Self {
let id = self.id();
id.update_event_listener(EventListener::Click, Box::new(action));
self
}
/// Add an event handler for [EventListener::Click]. This event will be handled with
/// the given handler and the event will continue propagating.
fn on_click_cont(self, action: impl Fn(&Event) + 'static) -> Self {
self.on_click(move |e| {
action(e);
EventPropagation::Continue
})
}
/// Add an event handler for [EventListener::Click]. This event will be handled with
/// the given handler and the event will stop propagating.
fn on_click_stop(self, action: impl Fn(&Event) + 'static) -> Self {
self.on_click(move |e| {
action(e);
EventPropagation::Stop
})
}
/// Add an event handler for [EventListener::DoubleClick]
fn on_double_click(self, action: impl Fn(&Event) -> EventPropagation + 'static) -> Self {
let id = self.id();
id.update_event_listener(EventListener::DoubleClick, Box::new(action));
self
}
/// Add an event handler for [EventListener::DoubleClick]. This event will be handled with
/// the given handler and the event will continue propagating.
fn on_double_click_cont(self, action: impl Fn(&Event) + 'static) -> Self {
self.on_double_click(move |e| {
action(e);
EventPropagation::Continue
})
}
/// Add an event handler for [EventListener::DoubleClick]. This event will be handled with
/// the given handler and the event will stop propagating.
fn on_double_click_stop(self, action: impl Fn(&Event) + 'static) -> Self {
self.on_double_click(move |e| {
action(e);
EventPropagation::Stop
})
}
/// Add an event handler for [EventListener::SecondaryClick]. This is most often the "Right" click.
fn on_secondary_click(self, action: impl Fn(&Event) -> EventPropagation + 'static) -> Self {
let id = self.id();
id.update_event_listener(EventListener::SecondaryClick, Box::new(action));
self
}
/// Add an event handler for [EventListener::SecondaryClick]. This is most often the "Right" click.
/// This event will be handled with the given handler and the event will continue propagating.
fn on_secondary_click_cont(self, action: impl Fn(&Event) + 'static) -> Self {
self.on_secondary_click(move |e| {
action(e);
EventPropagation::Continue
})
}
/// Add an event handler for [EventListener::SecondaryClick]. This is most often the "Right" click.
/// This event will be handled with the given handler and the event will stop propagating.
fn on_secondary_click_stop(self, action: impl Fn(&Event) + 'static) -> Self {
self.on_secondary_click(move |e| {
action(e);
EventPropagation::Stop
})
}
fn on_resize(self, action: impl Fn(Rect) + 'static) -> Self {
let id = self.id();
id.update_resize_listener(Box::new(action));
self
}
fn on_move(self, action: impl Fn(Point) + 'static) -> Self {
let id = self.id();
id.update_move_listener(Box::new(action));
self
}
fn on_cleanup(self, action: impl Fn() + 'static) -> Self {
let id = self.id();
id.update_cleanup_listener(Box::new(action));
self
}
fn animation(self, anim: Animation) -> Self {
let id = self.id();
create_effect(move |_| {
id.update_animation(anim.clone());
});
self
}
fn clear_focus(self, when: impl Fn() + 'static) -> Self {
let id = self.id();
create_effect(move |_| {
when();
id.clear_focus();
});
self
}
fn request_focus(self, when: impl Fn() + 'static) -> Self {
let id = self.id();
create_effect(move |_| {
when();
id.request_focus();
});
self
}
fn window_scale(self, scale_fn: impl Fn() -> f64 + 'static) -> Self {
create_effect(move |_| {
let window_scale = scale_fn();
update_window_scale(window_scale);
});
self
}
fn window_title(self, title_fn: impl Fn() -> String + 'static) -> Self {
create_effect(move |_| {
let window_title = title_fn();
set_window_title(window_title);
});
self
}
fn window_menu(self, menu_fn: impl Fn() -> Menu + 'static) -> Self {
create_effect(move |_| {
let menu = menu_fn();
set_window_menu(menu);
});
self
}
/// Adds a secondary-click context menu to the view, which opens at the mouse position.
fn context_menu(self, menu: impl Fn() -> Menu + 'static) -> Self {
let id = self.id();
id.update_context_menu(Box::new(menu));
self
}
/// Adds a primary-click context menu, which opens below the view.
fn popout_menu(self, menu: impl Fn() -> Menu + 'static) -> Self {
let id = self.id();
id.update_popout_menu(Box::new(menu));
self
}
}
impl<V: View> Decorators for V {}