web_extensions/tabs/
on_attached.rs

1use super::prelude::*;
2
3/// <https://developer.chrome.com/docs/extensions/reference/tabs/#event-onAttached>
4pub fn on_attached() -> OnAttached {
5    OnAttached(tabs().on_attached())
6}
7
8/// <https://developer.chrome.com/docs/extensions/reference/tabs/#event-onAttached>
9pub struct OnAttached(sys::EventTarget);
10
11pub struct OnAttachedEventListener<'a>(EventListener<'a, dyn FnMut(i32, sys::TabAttachInfo)>);
12
13impl OnAttachedEventListener<'_> {
14    pub fn forget(self) {
15        self.0.forget()
16    }
17}
18
19impl OnAttached {
20    pub fn add_listener<L>(&self, mut listener: L) -> OnAttachedEventListener
21    where
22        L: FnMut(TabId, AttachInfo) + 'static,
23    {
24        let listener = Closure::new(move |tab_id: i32, info: sys::TabAttachInfo| {
25            listener(TabId::from(tab_id), AttachInfo::from(info))
26        });
27        OnAttachedEventListener(EventListener::raw_new(&self.0, listener))
28    }
29}
30
31#[derive(Debug, Deserialize)]
32#[serde(rename_all = "camelCase")]
33pub struct AttachInfo {
34    pub new_window_id: i32,
35    pub new_position: u32,
36}
37
38impl From<sys::TabAttachInfo> for AttachInfo {
39    fn from(info: sys::TabAttachInfo) -> Self {
40        Self {
41            new_position: info.new_position(),
42            new_window_id: info.new_window_id(),
43        }
44    }
45}