ferroui-controls 0.3.0

The controls of FerroUI: the application model, top levels, the control set and automation peers
Documentation
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
use crate::diagnostics::IPopupHostProvider;
use crate::metadata::PseudoClassesAttribute;
use crate::primitives::{CustomPopupPlacementCallbackValue, IPopupHost, Popup, TemplatedControlImpl};
use crate::primitives::popup_positioning::CustomPopupPlacementCallback;
use crate::{ContentControl, ContentControlImpl, Control, ControlImpl, PlacementMode};
use ferroui_base::data::BindingPriority;
use ferroui_base::input::InputElementImpl;
use ferroui_base::interactivity::{
    CancelRoutedEventArgs, Interactive, InteractiveImpl, RoutedEvent, RoutedEventArgs, RoutedEventHandlerToken,
    RoutingStrategies,
};
use ferroui_base::layout::LayoutableImpl;
use ferroui_base::reactive::{CompositeDisposable, Disposable, IDisposable};
use ferroui_base::utilities::HandlerList;
use ferroui_base::{
    ferro_class, ferro_impl_classes, ferro_property, ferro_routed_event, instantiate, AnyValue, AttachedProperty, BoxedValue,
    FerroObjectExtensions, FerroObjectImpl, FerroProperty, FerroPropertyChangedEventArgs, Ref,
    StyledElementImpl, StyledPropertyOptions, VisualImpl, WeakRef,
};
use std::cell::RefCell;
use std::rc::Rc;

const PC_OPEN: &str = ":open";

/// A control which pops up a hint when a control is hovered.
///
/// You will probably not want to create a [`ToolTip`] control directly: if
/// added to the tree it will act as a simple [`ContentControl`] styled to
/// look like a tooltip. To add a tooltip to a control, use the `Tip`
/// attached property ([`ToolTip::tip_property`]), assigning the content
/// that you want displayed.
#[repr(C)]
pub struct ToolTip {
    base: ContentControl,
    popup: RefCell<Option<Ref<Popup>>>,
    popup_host_changed_handler: HandlerList<dyn Fn(Option<&Rc<dyn IPopupHost>>)>,
    subscriptions: RefCell<Option<Rc<CompositeDisposable>>>,
    adorned_control: RefCell<Option<WeakRef<Control>>>,
    closed: HandlerList<dyn Fn(&ToolTip)>,
}

ferro_class!(ToolTip: ContentControl);
ferroui_base::ferro_class_info!(ToolTip { new: ToolTip::new });
ferro_impl_classes!(
    ToolTip: StyledElementImpl,
    VisualImpl,
    LayoutableImpl,
    InteractiveImpl,
    InputElementImpl,
    TemplatedControlImpl,
    ContentControlImpl
);

impl ControlImpl for ToolTip {
    fn on_create_automation_peer(this: &Self) -> Ref<crate::automation::peers::AutomationPeer> {
        crate::automation::peers::ToolTipAutomationPeer::new(this).upcast()
    }
}

ferroui_base::ferro_impl_classes!(ToolTip: FerroObjectImpl);

impl ToolTip {
    /// The pseudoclasses set by the class.
    pub const PSEUDO_CLASSES: PseudoClassesAttribute = PseudoClassesAttribute::new(&[PC_OPEN]);
}

ferroui_base::ferro_properties! { impl ToolTip {
    ferro_property!(
        /// Defines the `ToolTip.Tip` attached property.
        pub fn tip_property() -> AttachedProperty<Option<BoxedValue>> {
            FerroProperty::register_attached::<ToolTip, Control, _>("Tip", None)
        }
    );

    ferro_property!(
        /// Defines the `ToolTip.IsOpen` attached property.
        pub fn is_open_property() -> AttachedProperty<bool> {
            FerroProperty::register_attached::<ToolTip, Control, _>("IsOpen", false)
        }
    );

    ferro_property!(
        /// Defines the `ToolTip.Placement` property.
        pub fn placement_property() -> AttachedProperty<PlacementMode> {
            FerroProperty::register_attached::<ToolTip, Control, _>("Placement", PlacementMode::Pointer)
        }
    );

    ferro_property!(
        /// Defines the `ToolTip.HorizontalOffset` property.
        pub fn horizontal_offset_property() -> AttachedProperty<f64> {
            FerroProperty::register_attached::<ToolTip, Control, _>("HorizontalOffset", 0.0)
        }
    );

    ferro_property!(
        /// Defines the `ToolTip.VerticalOffset` property.
        pub fn vertical_offset_property() -> AttachedProperty<f64> {
            FerroProperty::register_attached::<ToolTip, Control, _>("VerticalOffset", 20.0)
        }
    );

    ferro_property!(
        /// Defines the `ToolTip.CustomPopupPlacementCallback` property; see
        /// [`Popup::custom_popup_placement_callback_property`].
        pub fn custom_popup_placement_callback_property() -> AttachedProperty<Option<CustomPopupPlacementCallbackValue>> {
            FerroProperty::register_attached::<ToolTip, Control, _>("CustomPopupPlacementCallback", None)
        }
    );

    ferro_property!(
        /// Defines the `ToolTip.ShowDelay` property.
        pub fn show_delay_property() -> AttachedProperty<i32> {
            FerroProperty::register_attached::<ToolTip, Control, _>("ShowDelay", 400)
        }
    );

    ferro_property!(
        /// Defines the `ToolTip.BetweenShowDelay` property.
        pub fn between_show_delay_property() -> AttachedProperty<i32> {
            FerroProperty::register_attached::<ToolTip, Control, _>("BetweenShowDelay", 100)
        }
    );

    ferro_property!(
        /// Defines the `ToolTip.ShowOnDisabled` property.
        pub fn show_on_disabled_property() -> AttachedProperty<bool> {
            FerroProperty::register_attached_with::<ToolTip, Control, _>(
                "ShowOnDisabled",
                StyledPropertyOptions::new(false).inherits(true),
            )
        }
    );

    ferro_property!(
        /// Defines the `ToolTip.ServiceEnabled` property.
        pub fn service_enabled_property() -> AttachedProperty<bool> {
            FerroProperty::register_attached_with::<ToolTip, Control, _>(
                "ServiceEnabled",
                StyledPropertyOptions::new(true).inherits(true),
            )
        }
    );

    ferro_property!(
        /// Defines the `ToolTip.ShouldUseOverlayLayer` property; see
        /// [`Popup::should_use_overlay_layer`].
        pub fn should_use_overlay_layer_property() -> AttachedProperty<bool> {
            FerroProperty::register_attached::<ToolTip, Control, _>("ShouldUseOverlayLayer", false)
        }
    );

    ferro_property!(
        /// Stores the current [`ToolTip`] instance in the control (internal
        /// upstream).
        pub fn tool_tip_property() -> AttachedProperty<Option<Ref<ToolTip>>> {
            FerroProperty::register_attached::<ToolTip, Control, _>("ToolTip", None)
        }
    );
} }

impl ToolTip {
    ferro_routed_event!(
        /// The event raised when a tooltip is going to be shown on an
        /// element.
        ///
        /// To prevent a tooltip from appearing in the UI, your handler for
        /// `ToolTipOpening` can mark the event data as cancelled. Otherwise,
        /// the tooltip is displayed, using the value of the `Tip` property
        /// as the tooltip content. Another possible scenario is that you
        /// could write a handler that resets the value of the `Tip`
        /// property for the element that is the event source, just before
        /// the tooltip is displayed. `ToolTipOpening` will not be raised if
        /// the value of `Tip` is `None` or otherwise unset. Do not
        /// deliberately set `Tip` to `None` while a tooltip is open or
        /// opening; this will not have the effect of closing the tooltip,
        /// and will instead create an undesirable visual artifact in the UI.
        pub fn tool_tip_opening_event() -> RoutedEvent<CancelRoutedEventArgs> {
            RoutedEvent::register::<ToolTip, CancelRoutedEventArgs>("ToolTipOpening", RoutingStrategies::DIRECT)
        }
    );

    ferro_routed_event!(
        /// The event raised when a tooltip on an element that was shown
        /// should now be hidden.
        ///
        /// Marking the `ToolTipClosing` event as handled does not cancel
        /// closing the tooltip. Once the tooltip is displayed, closing the
        /// tooltip is done only in response to user interaction with the UI.
        pub fn tool_tip_closing_event() -> RoutedEvent<RoutedEventArgs> {
            RoutedEvent::register::<ToolTip, RoutedEventArgs>("ToolTipClosing", RoutingStrategies::DIRECT)
        }
    );

    /// Initializes static members of the [`ToolTip`] class.
    fn static_constructor() {
        // The subscription lives as long as the property.
        let _ = Self::is_open_property().changed().subscribe(Self::is_open_changed);
    }

    /// Creates the class data; see [`ferroui_base::FerroObject::construct`].
    pub fn construct() -> Self {
        Self {
            base: ContentControl::construct(),
            popup: RefCell::new(None),
            popup_host_changed_handler: HandlerList::new(),
            subscriptions: RefCell::new(None),
            adorned_control: RefCell::new(None),
            closed: HandlerList::new(),
        }
    }

    pub fn new() -> Ref<Self> {
        instantiate(Self::construct())
    }

    /// Gets the value of the `ToolTip.Tip` attached property: the content
    /// to be displayed in the tooltip of `element`.
    pub fn get_tip(element: &Control) -> Option<BoxedValue> {
        element.get_value(Self::tip_property())
    }

    /// Sets the value of the `ToolTip.Tip` attached property: the content
    /// to be displayed in the tooltip of `element`.
    pub fn set_tip(element: &Control, value: Option<BoxedValue>) {
        element.set_value(Self::tip_property(), value)
    }

    /// Gets the value of the `ToolTip.IsOpen` attached property: whether
    /// the tooltip of `element` is visible.
    pub fn get_is_open(element: &Control) -> bool {
        element.get_value(Self::is_open_property())
    }

    /// Sets the value of the `ToolTip.IsOpen` attached property: whether
    /// the tooltip of `element` is visible.
    pub fn set_is_open(element: &Control, value: bool) {
        element.set_value(Self::is_open_property(), value)
    }

    /// Gets the value of the `ToolTip.Placement` attached property: how the
    /// tooltip of `element` is positioned.
    pub fn get_placement(element: &Control) -> PlacementMode {
        element.get_value(Self::placement_property())
    }

    /// Sets the value of the `ToolTip.Placement` attached property: how the
    /// tooltip of `element` is positioned.
    pub fn set_placement(element: &Control, value: PlacementMode) {
        element.set_value(Self::placement_property(), value)
    }

    /// Gets the value of the `ToolTip.HorizontalOffset` attached property.
    pub fn get_horizontal_offset(element: &Control) -> f64 {
        element.get_value(Self::horizontal_offset_property())
    }

    /// Sets the value of the `ToolTip.HorizontalOffset` attached property.
    pub fn set_horizontal_offset(element: &Control, value: f64) {
        element.set_value(Self::horizontal_offset_property(), value)
    }

    /// Gets the value of the `ToolTip.VerticalOffset` attached property.
    pub fn get_vertical_offset(element: &Control) -> f64 {
        element.get_value(Self::vertical_offset_property())
    }

    /// Sets the value of the `ToolTip.VerticalOffset` attached property.
    pub fn set_vertical_offset(element: &Control, value: f64) {
        element.set_value(Self::vertical_offset_property(), value)
    }

    /// Gets the value of the `ToolTip.ShowDelay` attached property: the
    /// time, in milliseconds, before the tooltip of `element` opens.
    pub fn get_show_delay(element: &Control) -> i32 {
        element.get_value(Self::show_delay_property())
    }

    /// Sets the value of the `ToolTip.ShowDelay` attached property: the
    /// time, in milliseconds, before the tooltip of `element` opens.
    pub fn set_show_delay(element: &Control, value: i32) {
        element.set_value(Self::show_delay_property(), value)
    }

    /// Gets the number of milliseconds since the last tooltip closed during
    /// which the tooltip of `element` will open immediately, or a negative
    /// value indicating that the tooltip will always wait for the show
    /// delay before opening.
    pub fn get_between_show_delay(element: &Control) -> i32 {
        element.get_value(Self::between_show_delay_property())
    }

    /// Sets the number of milliseconds since the last tooltip closed during
    /// which the tooltip of `element` will open immediately.
    ///
    /// Setting a negative value disables the immediate opening behaviour.
    /// The tooltip of `element` will then always wait until the show delay
    /// elapses before showing.
    pub fn set_between_show_delay(element: &Control, value: i32) {
        element.set_value(Self::between_show_delay_property(), value)
    }

    /// Gets whether a control will display a tooltip even if it disabled.
    pub fn get_show_on_disabled(element: &Control) -> bool {
        element.get_value(Self::show_on_disabled_property())
    }

    /// Sets whether a control will display a tooltip even if it disabled.
    pub fn set_show_on_disabled(element: &Control, value: bool) {
        element.set_value(Self::show_on_disabled_property(), value)
    }

    /// Gets whether showing and hiding of a control's tooltip will be
    /// automatically controlled by the tooltip service.
    pub fn get_service_enabled(element: &Control) -> bool {
        element.get_value(Self::service_enabled_property())
    }

    /// Sets whether showing and hiding of a control's tooltip will be
    /// automatically controlled by the tooltip service.
    pub fn set_service_enabled(element: &Control, value: bool) {
        element.set_value(Self::service_enabled_property(), value)
    }

    /// Gets whether the tooltip popup for `element` is shown in the overlay
    /// layer. See [`Popup::should_use_overlay_layer`] for details.
    pub fn get_should_use_overlay_layer(element: &Control) -> bool {
        element.get_value(Self::should_use_overlay_layer_property())
    }

    /// Sets whether the tooltip popup for `element` is shown in the overlay
    /// layer. See [`Popup::should_use_overlay_layer`] for details.
    pub fn set_should_use_overlay_layer(element: &Control, value: bool) {
        element.set_value(Self::should_use_overlay_layer_property(), value)
    }

    /// Adds a handler for the `ToolTipOpening` attached event to `element`.
    pub fn add_tool_tip_opening_handler(
        element: &Control,
        handler: impl Fn(&Interactive, &CancelRoutedEventArgs) + 'static,
    ) -> RoutedEventHandlerToken {
        element.add_handler(Self::tool_tip_opening_event(), handler)
    }

    /// Removes a handler for the `ToolTipOpening` attached event from
    /// `element`.
    pub fn remove_tool_tip_opening_handler(element: &Control, handler: RoutedEventHandlerToken) {
        element.remove_handler(Self::tool_tip_opening_event(), handler);
    }

    /// Adds a handler for the `ToolTipClosing` attached event to `element`.
    pub fn add_tool_tip_closing_handler(
        element: &Control,
        handler: impl Fn(&Interactive, &RoutedEventArgs) + 'static,
    ) -> RoutedEventHandlerToken {
        element.add_handler(Self::tool_tip_closing_event(), handler)
    }

    /// Removes a handler for the `ToolTipClosing` attached event from
    /// `element`.
    pub fn remove_tool_tip_closing_handler(element: &Control, handler: RoutedEventHandlerToken) {
        element.remove_handler(Self::tool_tip_closing_event(), handler);
    }

    /// Gets the value of the `ToolTip.CustomPopupPlacementCallback`
    /// attached property.
    pub fn get_custom_popup_placement_callback(element: &Control) -> Option<CustomPopupPlacementCallback> {
        element.get_value(Self::custom_popup_placement_callback_property()).map(|value| value.0)
    }

    /// Sets the value of the `ToolTip.CustomPopupPlacementCallback`
    /// attached property.
    pub fn set_custom_popup_placement_callback(element: &Control, value: Option<CustomPopupPlacementCallback>) {
        element.set_value(
            Self::custom_popup_placement_callback_property(),
            value.map(CustomPopupPlacementCallbackValue),
        )
    }

    fn is_open_changed(e: &FerroPropertyChangedEventArgs<'_>) {
        let control = e.sender().downcast_ref::<Control>().expect("the tooltip properties are attached to controls");
        let new_value = e.get_new_value::<bool>();

        if new_value {
            let args = CancelRoutedEventArgs::with_event(Self::tool_tip_opening_event());
            control.raise_event(&args);
            if args.cancel() {
                control.set_current_value(Self::is_open_property(), false);
                return;
            }

            let Some(tip) = Self::get_tip(control) else {
                control.set_current_value(Self::is_open_property(), false);
                return;
            };

            let tip_tool_tip = tool_tip_from_boxed(&tip);
            let mut tool_tip = control.get_value(Self::tool_tip_property());
            let is_current = tool_tip.as_ref().is_some_and(|tool_tip| {
                tip_tool_tip.as_ref() == Some(tool_tip)
                    || tool_tip.content().is_some_and(|content| same_value(&content, &tip))
            });
            if !is_current {
                if let Some(tool_tip) = &tool_tip {
                    tool_tip.close();
                }

                let new_tool_tip = tip_tool_tip.unwrap_or_else(|| {
                    let tool_tip = ToolTip::new();
                    tool_tip.set_content(Some(tip));
                    tool_tip
                });
                control.set_value(Self::tool_tip_property(), Some(new_tool_tip.clone()));
                tool_tip = Some(new_tool_tip);
            }

            let tool_tip = tool_tip.expect("the tooltip was just created");
            let control = control.to_ref();
            *tool_tip.adorned_control.borrow_mut() = Some(control.downgrade());
            tool_tip.open(&control);
        } else if let Some(tool_tip) = control.get_value(Self::tool_tip_property()) {
            *tool_tip.adorned_control.borrow_mut() = None;
            tool_tip.close();
        }
    }

    /// The control this tooltip is shown for (internal upstream).
    pub fn adorned_control(&self) -> Option<Ref<Control>> {
        self.adorned_control.borrow().as_ref().and_then(WeakRef::upgrade)
    }

    /// Raised when the popup of the tooltip has closed (internal upstream).
    /// Disposing the returned handle unsubscribes.
    pub fn closed(&self, handler: impl Fn(&ToolTip) + 'static) -> Rc<dyn IDisposable> {
        let token = self.closed.add(Rc::new(handler));
        let weak = self.to_ref().downgrade();
        Disposable::create(move || {
            if let Some(this) = weak.upgrade() {
                this.closed.remove(token);
            }
        })
    }

    /// The host of the popup the tooltip is shown in, while it is open.
    pub fn popup_host(&self) -> Option<Rc<dyn IPopupHost>> {
        let popup = self.popup.borrow().clone();
        popup.and_then(|popup| popup.host())
    }

    /// Raised when the popup host changes (the popup host provider
    /// contract). Disposing the returned handle unsubscribes.
    pub fn popup_host_changed(
        &self,
        handler: impl Fn(Option<&Rc<dyn IPopupHost>>) + 'static,
    ) -> Rc<dyn IDisposable> {
        let token = self.popup_host_changed_handler.add(Rc::new(handler));
        let weak = self.to_ref().downgrade();
        Disposable::create(move || {
            if let Some(this) = weak.upgrade() {
                this.popup_host_changed_handler.remove(token);
            }
        })
    }

    /// The tooltip as a popup host provider (the diagnostics contract it
    /// implements upstream).
    pub fn to_popup_host_provider(&self) -> Rc<dyn IPopupHostProvider> {
        Rc::new(ToolTipPopupHostProvider(self.to_ref()))
    }

    fn open(&self, control: &Ref<Control>) {
        self.close();

        let existing = self.popup.borrow().clone();
        let popup = match existing {
            Some(popup) => popup,
            None => {
                let popup = Popup::new();
                popup.set_takes_focus_from_native_control(false);
                popup.set_window_manager_add_shadow_hint(false);

                // The popup belongs to the tooltip, which unsubscribes by
                // letting go of it.
                let weak = self.to_ref().downgrade();
                let _ = popup.opened({
                    let weak = weak.clone();
                    let weak_popup = popup.downgrade();
                    move || {
                        if let (Some(this), Some(popup)) = (weak.upgrade(), weak_popup.upgrade()) {
                            this.on_popup_opened(&popup);
                        }
                    }
                });
                let _ = popup.closed(move || {
                    if let Some(this) = weak.upgrade() {
                        this.on_popup_closed();
                    }
                });

                *self.popup.borrow_mut() = Some(popup.clone());
                popup
            }
        };

        // The popup holds the tooltip only while it is open: a tooltip and
        // its popup must not keep each other alive.
        popup.set_child(self.to_ref());

        let subscriptions = CompositeDisposable::from_disposables([
            popup.bind_typed_value(
                Popup::horizontal_offset_property(),
                control.get_binding_observable(Self::horizontal_offset_property()),
                BindingPriority::LocalValue,
            ),
            popup.bind_typed_value(
                Popup::vertical_offset_property(),
                control.get_binding_observable(Self::vertical_offset_property()),
                BindingPriority::LocalValue,
            ),
            popup.bind_typed_value(
                Popup::placement_property(),
                control.get_binding_observable(Self::placement_property()),
                BindingPriority::LocalValue,
            ),
            popup.bind_typed_value(
                Popup::custom_popup_placement_callback_property(),
                control.get_binding_observable(Self::custom_popup_placement_callback_property()),
                BindingPriority::LocalValue,
            ),
            popup.bind_typed_value(
                Popup::should_use_overlay_layer_property(),
                control.get_binding_observable(Self::should_use_overlay_layer_property()),
                BindingPriority::LocalValue,
            ),
        ]);
        *self.subscriptions.borrow_mut() = Some(Rc::new(subscriptions));

        popup.set_placement_target(control);
        popup.set_popup_parent(Some(control));

        popup.set_is_open(true);
    }

    fn close(&self) {
        if let Some(adorned_control) = self.adorned_control() {
            if Self::get_is_open(&adorned_control) {
                let args = RoutedEventArgs::with_event(Self::tool_tip_closing_event());
                adorned_control.raise_event(&args);
            }
        }

        let popup = self.popup.borrow().clone();
        if let Some(popup) = popup {
            popup.set_is_open(false);
            popup.set_popup_parent(None);
            popup.set_placement_target(None::<Ref<Control>>);
            if !popup.is_open() {
                popup.set_child(None::<Ref<Control>>);
            }
        }

        let subscriptions = self.subscriptions.borrow().clone();
        if let Some(subscriptions) = subscriptions {
            subscriptions.dispose();
        }
    }

    fn on_popup_closed(&self) {
        // This condition is true, when Popup was closed by any other reason outside of ToolTipService/ToolTip, keeping IsOpen=true.
        if let Some(adorned_control) = self.adorned_control() {
            if Self::get_is_open(&adorned_control) {
                adorned_control.set_current_value(Self::is_open_property(), false);
            }
        }

        for (_, handler) in self.popup_host_changed_handler.snapshot().iter() {
            handler(None);
        }
        self.update_pseudo_classes(false);
        for (_, handler) in self.closed.snapshot().iter() {
            handler(self);
        }
    }

    fn on_popup_opened(&self, popup: &Popup) {
        let host = popup.host();
        for (_, handler) in self.popup_host_changed_handler.snapshot().iter() {
            handler(host.as_ref());
        }
        self.update_pseudo_classes(true);
    }

    fn update_pseudo_classes(&self, new_value: bool) {
        self.pseudo_classes().set(PC_OPEN, new_value);
    }
}

/// The tooltip held by the untyped value of the `Tip` property, if it holds
/// one: a tooltip boxed as a control (see [`Control::boxed`]) or as itself.
fn tool_tip_from_boxed(value: &BoxedValue) -> Option<Ref<ToolTip>> {
    if let Some(control) = Control::from_boxed(value) {
        return control.cast::<ToolTip>();
    }
    let value: &dyn AnyValue = &**value;
    value.downcast_ref::<Ref<ToolTip>>().cloned()
}

/// Whether two untyped values are the same value: the same box, or boxes of
/// equal values.
pub(crate) fn same_value(a: &BoxedValue, b: &BoxedValue) -> bool {
    Rc::ptr_eq(a, b) || **a == **b
}

/// Whether the untyped value of the `Tip` property holds a tooltip.
pub(crate) fn is_tool_tip(value: &BoxedValue) -> bool {
    tool_tip_from_boxed(value).is_some()
}

/// The popup host provider contract of a tooltip.
struct ToolTipPopupHostProvider(Ref<ToolTip>);

impl IPopupHostProvider for ToolTipPopupHostProvider {
    fn popup_host(&self) -> Option<Rc<dyn IPopupHost>> {
        self.0.popup_host()
    }

    fn popup_host_changed(&self, handler: Rc<dyn Fn(Option<&Rc<dyn IPopupHost>>)>) -> Rc<dyn IDisposable> {
        self.0.popup_host_changed(move |host| handler(host))
    }
}