telegram_webapp_sdk/webapp/lifecycle.rs
1// SPDX-FileCopyrightText: 2025 RAprogramm <andrey.rozanov.vl@gmail.com>
2// SPDX-License-Identifier: MIT
3
4use js_sys::Reflect;
5use wasm_bindgen::JsValue;
6
7use crate::webapp::TelegramWebApp;
8
9impl TelegramWebApp {
10 /// Call `WebApp.expand()`.
11 ///
12 /// # Errors
13 /// Returns [`JsValue`] if the underlying JS call fails.
14 pub fn expand(&self) -> Result<(), JsValue> {
15 self.call0("expand")
16 }
17
18 /// Call `WebApp.close()`.
19 ///
20 /// # Errors
21 /// Returns [`JsValue`] if the underlying JS call fails.
22 pub fn close(&self) -> Result<(), JsValue> {
23 self.call0("close")
24 }
25
26 /// Call `WebApp.enableClosingConfirmation()`.
27 ///
28 /// # Examples
29 /// ```no_run
30 /// # use telegram_webapp_sdk::webapp::TelegramWebApp;
31 /// # let app = TelegramWebApp::instance().unwrap();
32 /// app.enable_closing_confirmation().unwrap();
33 /// ```
34 ///
35 /// # Errors
36 /// Returns [`JsValue`] if the underlying JS call fails.
37 pub fn enable_closing_confirmation(&self) -> Result<(), JsValue> {
38 self.call0("enableClosingConfirmation")
39 }
40
41 /// Call `WebApp.disableClosingConfirmation()`.
42 ///
43 /// # Examples
44 /// ```no_run
45 /// # use telegram_webapp_sdk::webapp::TelegramWebApp;
46 /// # let app = TelegramWebApp::instance().unwrap();
47 /// app.disable_closing_confirmation().unwrap();
48 /// ```
49 ///
50 /// # Errors
51 /// Returns [`JsValue`] if the underlying JS call fails.
52 pub fn disable_closing_confirmation(&self) -> Result<(), JsValue> {
53 self.call0("disableClosingConfirmation")
54 }
55
56 /// Returns whether closing confirmation is currently enabled.
57 ///
58 /// # Examples
59 /// ```no_run
60 /// # use telegram_webapp_sdk::webapp::TelegramWebApp;
61 /// # let app = TelegramWebApp::instance().unwrap();
62 /// let _ = app.is_closing_confirmation_enabled();
63 /// ```
64 pub fn is_closing_confirmation_enabled(&self) -> bool {
65 Reflect::get(&self.inner, &"isClosingConfirmationEnabled".into())
66 .ok()
67 .and_then(|v| v.as_bool())
68 .unwrap_or(false)
69 }
70
71 /// Call `WebApp.requestFullscreen()`.
72 ///
73 /// # Examples
74 /// ```no_run
75 /// # use telegram_webapp_sdk::webapp::TelegramWebApp;
76 /// # let app = TelegramWebApp::instance().unwrap();
77 /// app.request_fullscreen().unwrap();
78 /// ```
79 ///
80 /// # Errors
81 /// Returns [`JsValue`] if the underlying JS call fails.
82 pub fn request_fullscreen(&self) -> Result<(), JsValue> {
83 self.call0("requestFullscreen")
84 }
85
86 /// Call `WebApp.exitFullscreen()`.
87 ///
88 /// # Examples
89 /// ```no_run
90 /// # use telegram_webapp_sdk::webapp::TelegramWebApp;
91 /// # let app = TelegramWebApp::instance().unwrap();
92 /// app.exit_fullscreen().unwrap();
93 /// ```
94 ///
95 /// # Errors
96 /// Returns [`JsValue`] if the underlying JS call fails.
97 pub fn exit_fullscreen(&self) -> Result<(), JsValue> {
98 self.call0("exitFullscreen")
99 }
100
101 /// Returns whether the app is displayed in fullscreen mode.
102 ///
103 /// # Examples
104 /// ```no_run
105 /// use telegram_webapp_sdk::webapp::TelegramWebApp;
106 ///
107 /// if let Some(app) = TelegramWebApp::instance() {
108 /// let _ = app.is_fullscreen();
109 /// }
110 /// ```
111 pub fn is_fullscreen(&self) -> bool {
112 Reflect::get(&self.inner, &"isFullscreen".into())
113 .ok()
114 .and_then(|v| v.as_bool())
115 .unwrap_or(false)
116 }
117
118 /// Call `WebApp.lockOrientation(orientation)`.
119 ///
120 /// # Examples
121 /// ```no_run
122 /// # use telegram_webapp_sdk::webapp::TelegramWebApp;
123 /// # let app = TelegramWebApp::instance().unwrap();
124 /// app.lock_orientation("portrait").unwrap();
125 /// ```
126 ///
127 /// # Errors
128 /// Returns [`JsValue`] if the underlying JS call fails.
129 pub fn lock_orientation(&self, orientation: &str) -> Result<(), JsValue> {
130 self.call1("lockOrientation", &orientation.into())
131 }
132
133 /// Call `WebApp.unlockOrientation()`.
134 ///
135 /// # Examples
136 /// ```no_run
137 /// # use telegram_webapp_sdk::webapp::TelegramWebApp;
138 /// # let app = TelegramWebApp::instance().unwrap();
139 /// app.unlock_orientation().unwrap();
140 /// ```
141 ///
142 /// # Errors
143 /// Returns [`JsValue`] if the underlying JS call fails.
144 pub fn unlock_orientation(&self) -> Result<(), JsValue> {
145 self.call0("unlockOrientation")
146 }
147
148 /// Returns whether the orientation is locked.
149 ///
150 /// # Examples
151 /// ```no_run
152 /// use telegram_webapp_sdk::webapp::TelegramWebApp;
153 ///
154 /// if let Some(app) = TelegramWebApp::instance() {
155 /// let _ = app.is_orientation_locked();
156 /// }
157 /// ```
158 pub fn is_orientation_locked(&self) -> bool {
159 Reflect::get(&self.inner, &"isOrientationLocked".into())
160 .ok()
161 .and_then(|v| v.as_bool())
162 .unwrap_or(false)
163 }
164
165 /// Call `WebApp.enableVerticalSwipes()`.
166 ///
167 /// # Examples
168 /// ```no_run
169 /// # use telegram_webapp_sdk::webapp::TelegramWebApp;
170 /// # let app = TelegramWebApp::instance().unwrap();
171 /// app.enable_vertical_swipes().unwrap();
172 /// ```
173 ///
174 /// # Errors
175 /// Returns [`JsValue`] if the underlying JS call fails.
176 pub fn enable_vertical_swipes(&self) -> Result<(), JsValue> {
177 self.call0("enableVerticalSwipes")
178 }
179
180 /// Call `WebApp.disableVerticalSwipes()`.
181 ///
182 /// # Examples
183 /// ```no_run
184 /// # use telegram_webapp_sdk::webapp::TelegramWebApp;
185 /// # let app = TelegramWebApp::instance().unwrap();
186 /// app.disable_vertical_swipes().unwrap();
187 /// ```
188 ///
189 /// # Errors
190 /// Returns [`JsValue`] if the underlying JS call fails.
191 pub fn disable_vertical_swipes(&self) -> Result<(), JsValue> {
192 self.call0("disableVerticalSwipes")
193 }
194
195 /// Returns whether vertical swipes are currently enabled.
196 ///
197 /// # Examples
198 /// ```no_run
199 /// use telegram_webapp_sdk::webapp::TelegramWebApp;
200 ///
201 /// if let Some(app) = TelegramWebApp::instance() {
202 /// let _ = app.is_vertical_swipes_enabled();
203 /// }
204 /// ```
205 pub fn is_vertical_swipes_enabled(&self) -> bool {
206 Reflect::get(&self.inner, &"isVerticalSwipesEnabled".into())
207 .ok()
208 .and_then(|v| v.as_bool())
209 .unwrap_or(false)
210 }
211
212 /// Returns whether the mini app is currently active (visible to the user).
213 ///
214 /// # Examples
215 /// ```no_run
216 /// use telegram_webapp_sdk::webapp::TelegramWebApp;
217 ///
218 /// if let Some(app) = TelegramWebApp::instance() {
219 /// let _ = app.is_active();
220 /// }
221 /// ```
222 pub fn is_active(&self) -> bool {
223 Reflect::get(&self.inner, &"isActive".into())
224 .ok()
225 .and_then(|v| v.as_bool())
226 .unwrap_or(false)
227 }
228
229 pub fn is_expanded(&self) -> bool {
230 Reflect::get(&self.inner, &"isExpanded".into())
231 .ok()
232 .and_then(|v| v.as_bool())
233 .unwrap_or(false)
234 }
235}