1 use webcore::value::Reference;
2 use webcore::try_from::TryInto;
3 use webapi::event_target::{IEventTarget, EventTarget};
4 use webapi::window_or_worker::IWindowOrWorker;
5 use webapi::storage::Storage;
6 use webapi::location::Location;
7 use webapi::history::History;
8 use webapi::selection::Selection;
9 use webcore::once::Once;
10 use webcore::value::Value;
11
12 /// A handle to a pending animation frame request.
13 #[derive(Debug)]
14 pub struct RequestAnimationFrameHandle(Value);
15
16 impl RequestAnimationFrameHandle {
17 /// Cancels an animation frame request.
18 ///
19 /// [(Javascript docs)](https://developer.mozilla.org/en-US/docs/Web/API/Window/cancelAnimationFrame)
cancel( self )20 pub fn cancel( self ) {
21 js! { @(no_return)
22 var val = @{&self.0};
23 val.window.cancelAnimationFrame(val.request);
24 val.callback.drop();
25 }
26 }
27 }
28
29 /// The `Window` object represents a window containing a DOM document.
30 ///
31 /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/Window)
32 // https://html.spec.whatwg.org/#window
33 #[derive(Clone, Debug, PartialEq, Eq, ReferenceType)]
34 #[reference(instance_of = "Window")]
35 #[reference(subclass_of(EventTarget))]
36 pub struct Window( Reference );
37
38 impl IEventTarget for Window {}
39 impl IWindowOrWorker for Window {}
40
41 /// A global instance of [Window](struct.Window.html).
42 ///
43 /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/Window)
window() -> Window44 pub fn window() -> Window {
45 unsafe { js!( return window; ).into_reference_unchecked() }.unwrap()
46 }
47
48 impl Window {
49 /// The Window.alert() method displays an alert dialog
50 /// with the optional specified content and an OK button.
51 ///
52 /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/Window/alert)
53 // https://html.spec.whatwg.org/#the-window-object:dom-alert
alert( &self, message: &str )54 pub fn alert( &self, message: &str ) {
55 js!( @(no_return)
56 @{self}.alert( @{message} );
57 );
58 }
59
60 /// The Window.confirm() method displays a modal dialog
61 /// with an optional message and two buttons: OK and Cancel.
62 ///
63 /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/Window/confirm)
64 // https://html.spec.whatwg.org/#the-window-object:dom-confirm
confirm( &self, message: &str ) -> bool65 pub fn confirm( &self, message: &str ) -> bool {
66 js!(
67 return @{self}.confirm( @{message} );
68 ).try_into().unwrap()
69 }
70
71 /// The `local_storage` property allows you to access a local [Storage](struct.Storage.html)
72 /// object.
73 ///
74 /// It is similar to the [Window::session_storage](struct.Window.html#method.session_storage).
75 /// The only difference is that, while data stored in `local_storage` has
76 /// no expiration time, data stored in `session_storage` gets cleared when
77 /// the browsing session ends - that is, when the browser is closed.
78 ///
79 /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage)
80 // https://html.spec.whatwg.org/#the-localstorage-attribute:dom-localstorage
local_storage( &self ) -> Storage81 pub fn local_storage( &self ) -> Storage {
82 unsafe {
83 js!(
84 return @{self.as_ref()}.localStorage;
85 ).into_reference_unchecked().unwrap()
86 }
87 }
88
89 /// The `session_storage` property allows you to access a session [Storage](struct.Storage.html)
90 /// object for the current origin.
91 ///
92 /// It is similar to the [Window::local_storage](struct.Window.html#method.local_storage),
93 /// The only difference is that, while data stored in `local_storage` has
94 /// no expiration time, data stored in `session_storage` gets cleared when
95 /// the browsing session ends.
96 ///
97 /// A page session lasts for as long as the browser is open and survives over
98 /// page reloads and restores. Opening a page in a new tab or window will cause
99 /// a new session to be initiated, which differs from how session cookies work.
100 ///
101 /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/Window/sessionStorage)
102 // https://html.spec.whatwg.org/#the-sessionstorage-attribute:dom-sessionstorage
session_storage( &self ) -> Storage103 pub fn session_storage( &self ) -> Storage {
104 unsafe {
105 js!(
106 return @{self.as_ref()}.sessionStorage;
107 ).into_reference_unchecked().unwrap()
108 }
109 }
110
111 /// Returns a [Location](struct.Location.html) object which contains
112 /// information about the URL of the document and provides methods
113 /// for changing that URL and loading another URL.
114 ///
115 /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/Window/location)
116 // https://html.spec.whatwg.org/#the-window-object:dom-location
location( &self ) -> Option< Location >117 pub fn location( &self ) -> Option< Location > {
118 unsafe {
119 js!(
120 return @{self}.location;
121 ).into_reference_unchecked()
122 }
123 }
124
125 /// You should call this method whenever you're ready to update your animation onscreen.
126 /// This will request that your animation function be called before the browser performs the next repaint.
127 /// The number of callbacks is usually 60 times per second, but will generally match the display refresh
128 /// rate in most web browsers as per W3C recommendation. request_animation_frame() calls are paused in most browsers
129 /// when running in background tabs or hidden iframes in order to improve performance and battery life.
130 ///
131 /// The callback method is passed a single argument, a f64, which indicates the current time when
132 /// callbacks queued by requestAnimationFrame() begin to fire. Multiple callbacks in a single frame, therefore,
133 /// each receive the same timestamp even though time has passed during the computation of every previous callback's workload.
134 /// This timestamp is a decimal number, in milliseconds, but with a minimal precision of 1ms (1000 µs).
135 ///
136 /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame)
137 // https://html.spec.whatwg.org/#the-window-object:dom-window-requestanimationframe
request_animation_frame< F: FnOnce(f64) + 'static>( &self, callback: F) -> RequestAnimationFrameHandle138 pub fn request_animation_frame< F: FnOnce(f64) + 'static>( &self, callback: F) -> RequestAnimationFrameHandle {
139 let values: Value = js!{
140 var callback = @{Once(callback)};
141 var request = @{self}.requestAnimationFrame(callback);
142 return { request: request, callback: callback, window: @{self} };
143 };
144 RequestAnimationFrameHandle(values)
145 }
146
147 /// Returns the global [History](struct.History.html) object, which provides methods to
148 /// manipulate the browser history.
149 ///
150 /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/Window/history)
151 // https://html.spec.whatwg.org/#the-window-object:dom-history
history(&self) -> History152 pub fn history(&self) -> History {
153 unsafe {
154 js!(
155 return @{self}.history;
156 ).into_reference_unchecked().unwrap()
157 }
158 }
159
160 /// Returns the width (in pixels) of the browser window viewport including, if rendered,
161 /// the vertical scrollbar.
162 ///
163 /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/window/innerWidth)
164 // https://drafts.csswg.org/cssom-view/#ref-for-dom-window-innerwidth
inner_width(&self) -> i32165 pub fn inner_width(&self) -> i32 {
166 js!(
167 return @{self}.innerWidth;
168 ).try_into().unwrap()
169 }
170
171 /// Returns the height (in pixels) of the browser window viewport including, if rendered,
172 /// the horizontal scrollbar.
173 ///
174 /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/window/innerHeight)
175 // https://drafts.csswg.org/cssom-view/#ref-for-dom-window-innerheight
inner_height(&self) -> i32176 pub fn inner_height(&self) -> i32 {
177 js!(
178 return @{self}.innerHeight;
179 ).try_into().unwrap()
180 }
181
182 /// Returns the width of the outside of the browser window. It represents the width
183 /// of the whole browser window including sidebar (if expanded), window chrome
184 /// and window resizing borders/handles.
185 ///
186 /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/Window/outerWidth)
187 // https://drafts.csswg.org/cssom-view/#ref-for-dom-window-outerheight
outer_width(&self) -> i32188 pub fn outer_width(&self) -> i32 {
189 js!(
190 return @{self}.outerWidth;
191 ).try_into().unwrap()
192 }
193
194 /// Returns the height of the outside of the browser window. It represents the height
195 /// of the whole browser window including sidebar (if expanded), window chrome
196 /// and window resizing borders/handles.
197 ///
198 /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/Window/outerHeight)
199 // https://drafts.csswg.org/cssom-view/#ref-for-dom-window-outerheight
outer_height(&self) -> i32200 pub fn outer_height(&self) -> i32 {
201 js!(
202 return @{self}.outerHeight;
203 ).try_into().unwrap()
204 }
205
206 /// The read-only Window property pageYOffset is an alias for scrollY; as such, it returns
207 /// the number of pixels the document is currently scrolled along the vertical axis (that is,
208 /// up or down), with a value of 0.0 indicating that the top edge of the Document is currently
209 /// aligned with the top edge of the window's content area.
210 ///
211 /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/Window/pageYOffset)
212 // https://drafts.csswg.org/cssom-view/#ref-for-dom-window-pageyoffset
page_y_offset(&self) -> f64213 pub fn page_y_offset(&self) -> f64 {
214 js!(
215 return @{self}.pageYOffset;
216 ).try_into().unwrap()
217 }
218
219 /// This is an alias for scrollX.
220 ///
221 /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/Window/pageXOffset)
222 // https://drafts.csswg.org/cssom-view/#ref-for-dom-window-pagexoffset
page_x_offset(&self) -> f64223 pub fn page_x_offset(&self) -> f64 {
224 js!(
225 return @{self}.pageXOffset;
226 ).try_into().unwrap()
227 }
228
229 /// The ratio in resolution from physical pixels to CSS pixels
230 ///
231 /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/Window/devicePixelRatio)
232 // https://drafts.csswg.org/cssom-view/#ref-for-dom-window-devicepixelratio
device_pixel_ratio(&self) -> f64233 pub fn device_pixel_ratio(&self) -> f64 {
234 js! (
235 return @{self}.devicePixelRatio;
236 ).try_into().unwrap()
237 }
238
239 /// Returns a [Selection](struct.Selection.html) object representing the range of text selected
240 /// by the user or the current position of the caret.
241 /// [(Javascript docs)](https://developer.mozilla.org/en-US/docs/Web/API/Window/getSelection)
242 // https://w3c.github.io/selection-api/#dom-document-getselection
get_selection(&self) -> Option<Selection>243 pub fn get_selection(&self) -> Option<Selection> {
244 unsafe {
245 js!(
246 return @{self}.getSelection();
247 ).into_reference_unchecked()
248 }
249 }
250 }
251