Skip to main content

ferroui_base/input/
context_requested_event_args.rs

1use super::{HoldingRoutedEventArgs, InputElement, PointerEventArgs, PointerType};
2use crate::interactivity::RoutedEventArgs;
3use crate::{ferro_routed_event_args, Point};
4
5/// Provides event data for the context requested event.
6#[derive(Clone)]
7pub struct ContextRequestedEventArgs {
8    base: RoutedEventArgs,
9    pointer_event_args: Option<PointerEventArgs>,
10    is_holding: bool,
11}
12
13ferro_routed_event_args!(ContextRequestedEventArgs: RoutedEventArgs);
14
15impl Default for ContextRequestedEventArgs {
16    fn default() -> Self {
17        Self::new()
18    }
19}
20
21impl ContextRequestedEventArgs {
22    /// Creates args for a context request that has no position, e.g. one
23    /// made with the keyboard.
24    pub fn new() -> Self {
25        Self {
26            base: RoutedEventArgs::with_event(InputElement::context_requested_event()),
27            pointer_event_args: None,
28            is_holding: false,
29        }
30    }
31
32    /// Creates args for a context request made with a pointer.
33    pub fn from_pointer_event_args(pointer_event_args: &PointerEventArgs) -> Self {
34        Self { pointer_event_args: Some(pointer_event_args.clone()), ..Self::new() }
35    }
36
37    /// Creates args from other context requested args, keeping the position.
38    pub fn from_context_requested_event_args(context_requested_event_args: &ContextRequestedEventArgs) -> Self {
39        Self { pointer_event_args: context_requested_event_args.pointer_event_args.clone(), ..Self::new() }
40    }
41
42    pub(crate) fn from_holding(hold_routed_event_args: &HoldingRoutedEventArgs) -> Self {
43        Self {
44            pointer_event_args: Some(hold_routed_event_args.pointer_event_args().clone()),
45            is_holding: true,
46            ..Self::new()
47        }
48    }
49
50    /// Gets the point on which the context was requested, relative to the
51    /// specified element (or to the root when `None`).
52    ///
53    /// Returns `None` if the context was not requested with a pointer, e.g.
54    /// when it was requested with the keyboard.
55    pub fn try_get_position(&self, relative_to: Option<&InputElement>) -> Option<Point> {
56        self.pointer_event_args.as_ref().map(|e| e.get_position(relative_to.map(|element| &****element)))
57    }
58
59    /// Whether the context was requested by holding a pointer down.
60    ///
61    /// Internal in the reference, where the controls assembly sees it.
62    pub fn is_holding(&self) -> bool {
63        self.is_holding
64    }
65
66    #[allow(dead_code)]
67    pub fn pointer_type(&self) -> Option<PointerType> {
68        self.pointer_event_args.as_ref().map(|e| e.pointer().type_())
69    }
70}