web_extensions/tabs/
on_updated.rs

1use super::{prelude::*, MutedInfo, Status, Tab};
2
3/// <https://developer.chrome.com/docs/extensions/reference/tabs/#event-onUpdated>
4pub fn on_updated() -> OnUpdated {
5    OnUpdated(tabs().on_updated())
6}
7
8/// <https://developer.chrome.com/docs/extensions/reference/tabs/#event-onUpdated>
9pub struct OnUpdated(sys::EventTarget);
10
11/// <https://developer.chrome.com/docs/extensions/reference/tabs/#method-onUpdated-callback>
12pub struct OnUpdatedEventListener<'a>(
13    EventListener<'a, dyn FnMut(i32, sys::TabChangeInfo, sys::Tab)>,
14);
15
16impl OnUpdatedEventListener<'_> {
17    pub fn forget(self) {
18        self.0.forget()
19    }
20}
21
22impl OnUpdated {
23    pub fn add_listener<L>(&self, mut listener: L) -> OnUpdatedEventListener
24    where
25        L: FnMut(TabId, ChangeInfo, Tab) + 'static,
26    {
27        let listener = Closure::new(
28            move |tab_id: i32, info: sys::TabChangeInfo, tab: sys::Tab| {
29                listener(TabId::from(tab_id), ChangeInfo::from(info), Tab::from(tab))
30            },
31        );
32        OnUpdatedEventListener(EventListener::raw_new(&self.0, listener))
33    }
34}
35
36/// <https://developer.chrome.com/docs/extensions/reference/tabs/#type-onUpdated-callback-changeInfo>
37#[derive(Debug, Deserialize)]
38#[serde(rename_all = "camelCase")]
39pub struct ChangeInfo {
40    /// The tab's new audible state.
41    pub audible: Option<bool>,
42
43    /// The tab's new auto-discardable state.
44    pub auto_discardable: Option<bool>,
45
46    /// The tab's new discarded state.
47    pub discarded: Option<bool>,
48
49    /// The tab's new favicon URL.
50    pub fav_icon_url: Option<String>,
51
52    /// The tab's new group.
53    pub group_id: Option<i32>,
54
55    /// The tab's new muted state and the reason for the change.
56    pub muted_info: Option<MutedInfo>,
57
58    /// The tab's new pinned state.
59    pub pinned: Option<bool>,
60
61    /// The tab's loading status.
62    pub status: Option<Status>,
63
64    /// The tab's new title.
65    pub title: Option<String>,
66
67    /// The tab's URL if it has changed.
68    pub url: Option<String>,
69}
70
71impl From<sys::TabChangeInfo> for ChangeInfo {
72    fn from(info: sys::TabChangeInfo) -> Self {
73        let status = info.status().map(|s| Status::try_from(s).expect("status"));
74        let muted_info = info.muted_info().map(MutedInfo::from);
75        Self {
76            status,
77            muted_info,
78            audible: info.audible(),
79            auto_discardable: info.auto_discardable(),
80            discarded: info.discarded(),
81            fav_icon_url: info.fav_icon_url(),
82            group_id: info.group_id(),
83            pinned: info.pinned(),
84            title: info.title(),
85            url: info.url(),
86        }
87    }
88}