web_extensions/tabs/
on_activated.rs1use super::prelude::*;
2
3pub fn on_activated() -> OnActivated {
5 OnActivated(tabs().on_activated())
6}
7
8pub struct OnActivated(sys::EventTarget);
10
11pub struct OnActivatedEventListener<'a>(EventListener<'a, dyn FnMut(sys::TabActiveInfo)>);
13
14impl OnActivatedEventListener<'_> {
15 pub fn forget(self) {
16 self.0.forget()
17 }
18}
19
20impl OnActivated {
21 pub fn add_listener<L>(&self, mut listener: L) -> OnActivatedEventListener
22 where
23 L: FnMut(ActiveInfo) + 'static,
24 {
25 let listener =
26 Closure::new(move |info: sys::TabActiveInfo| listener(ActiveInfo::from(info)));
27 OnActivatedEventListener(EventListener::raw_new(&self.0, listener))
28 }
29}
30
31#[derive(Debug, Deserialize)]
33#[serde(rename_all = "camelCase")]
34pub struct ActiveInfo {
35 pub tab_id: TabId,
36 pub window_id: i32,
37}
38
39impl From<sys::TabActiveInfo> for ActiveInfo {
40 fn from(info: sys::TabActiveInfo) -> Self {
41 let tab_id = TabId::from(info.tab_id());
42 ActiveInfo {
43 tab_id,
44 window_id: info.window_id(),
45 }
46 }
47}