1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
use crate::{input::mouse, Column, Element, Event, Layout, MouseCursor, Point}; use std::hash::Hasher; use stretch::result; /// A set of interactive graphical elements with a specific [`Layout`]. /// /// It can be updated and drawn. /// /// Iced tries to avoid dictating how to write your event loop. You are in /// charge of using this type in your system in any way you want. /// /// [`Layout`]: struct.Layout.html #[derive(Debug)] pub struct UserInterface<'a, Message, Renderer> { hash: u64, root: Element<'a, Message, Renderer>, layout: result::Layout, cursor_position: Point, } impl<'a, Message, Renderer> UserInterface<'a, Message, Renderer> { /// Builds a user interface for an [`Element`]. /// /// It is able to avoid expensive computations when using a [`Cache`] /// obtained from a previous instance of a [`UserInterface`]. /// /// [`Element`]: struct.Element.html /// [`Cache`]: struct.Cache.html /// [`UserInterface`]: struct.UserInterface.html /// /// # Example /// Imagine we want to build a [`UserInterface`] for /// [the counter example that we previously wrote](index.html#usage). Here /// is naive way to set up our application loop: /// /// ```no_run /// use iced::{UserInterface, Cache}; /// use iced_wgpu::Renderer; /// /// # mod iced_wgpu { /// # pub struct Renderer; /// # /// # impl Renderer { /// # pub fn new() -> Self { Renderer } /// # } /// # } /// # /// # use iced::Column; /// # /// # pub struct Counter; /// # /// # impl Counter { /// # pub fn new() -> Self { Counter } /// # pub fn view(&self) -> Column<(), Renderer> { /// # Column::new() /// # } /// # } /// // Initialization /// let mut counter = Counter::new(); /// let mut cache = Cache::new(); /// let mut renderer = Renderer::new(); /// /// // Application loop /// loop { /// // Process system events here... /// /// // Build the user interface /// let user_interface = UserInterface::build( /// counter.view(), /// cache, /// &renderer, /// ); /// /// // Update and draw the user interface here... /// // ... /// /// // Obtain the cache for the next iteration /// cache = user_interface.into_cache(); /// } /// ``` pub fn build<E: Into<Element<'a, Message, Renderer>>>( root: E, cache: Cache, renderer: &Renderer, ) -> Self { let root = root.into(); let hasher = &mut crate::Hasher::default(); root.hash_layout(hasher); let hash = hasher.finish(); let layout = if hash == cache.hash { cache.layout } else { root.compute_layout(renderer) }; UserInterface { hash, root, layout, cursor_position: cache.cursor_position, } } /// Updates the [`UserInterface`] by processing each provided [`Event`]. /// /// It returns __messages__ that may have been produced as a result of user /// interactions. You should feed these to your __update logic__. /// /// [`UserInterface`]: struct.UserInterface.html /// [`Event`]: enum.Event.html /// /// # Example /// Let's allow our [counter](index.html#usage) to change state by completing /// [the previous example](#example): /// /// ```no_run /// use iced::{UserInterface, Cache}; /// use iced_wgpu::Renderer; /// /// # mod iced_wgpu { /// # pub struct Renderer; /// # /// # impl Renderer { /// # pub fn new() -> Self { Renderer } /// # } /// # } /// # /// # use iced::Column; /// # /// # pub struct Counter; /// # /// # impl Counter { /// # pub fn new() -> Self { Counter } /// # pub fn view(&self) -> Column<(), Renderer> { /// # Column::new() /// # } /// # pub fn update(&mut self, message: ()) {} /// # } /// let mut counter = Counter::new(); /// let mut cache = Cache::new(); /// let mut renderer = Renderer::new(); /// /// // Initialize our event storage /// let mut events = Vec::new(); /// /// loop { /// // Process system events... /// /// let mut user_interface = UserInterface::build( /// counter.view(), /// cache, /// &renderer, /// ); /// /// // Update the user interface /// let messages = user_interface.update(events.drain(..)); /// /// cache = user_interface.into_cache(); /// /// // Process the produced messages /// for message in messages { /// counter.update(message); /// } /// } /// ``` pub fn update( &mut self, events: impl Iterator<Item = Event>, ) -> Vec<Message> { let mut messages = Vec::new(); for event in events { if let Event::Mouse(mouse::Event::CursorMoved { x, y }) = event { self.cursor_position = Point::new(x, y); } self.root.widget.on_event( event, Layout::new(&self.layout), self.cursor_position, &mut messages, ); } messages } /// Draws the [`UserInterface`] with the provided [`Renderer`]. /// /// It returns the current state of the [`MouseCursor`]. You should update /// the icon of the mouse cursor accordingly in your system. /// /// [`UserInterface`]: struct.UserInterface.html /// [`Renderer`]: trait.Renderer.html /// [`MouseCursor`]: enum.MouseCursor.html /// /// # Example /// We can finally draw our [counter](index.html#usage) by /// [completing the last example](#example-1): /// /// ```no_run /// use iced::{UserInterface, Cache}; /// use iced_wgpu::Renderer; /// /// # mod iced_wgpu { /// # pub struct Renderer; /// # /// # impl Renderer { /// # pub fn new() -> Self { Renderer } /// # } /// # } /// # /// # use iced::Column; /// # /// # pub struct Counter; /// # /// # impl Counter { /// # pub fn new() -> Self { Counter } /// # pub fn view(&self) -> Column<(), Renderer> { /// # Column::new() /// # } /// # pub fn update(&mut self, message: ()) {} /// # } /// let mut counter = Counter::new(); /// let mut cache = Cache::new(); /// let mut renderer = Renderer::new(); /// let mut events = Vec::new(); /// /// loop { /// // Process system events... /// /// let mut user_interface = UserInterface::build( /// counter.view(), /// cache, /// &renderer, /// ); /// /// let messages = user_interface.update(events.drain(..)); /// /// // Draw the user interface /// let mouse_cursor = user_interface.draw(&mut renderer); /// /// cache = user_interface.into_cache(); /// /// for message in messages { /// counter.update(message); /// } /// /// // Update mouse cursor icon... /// // Flush rendering operations... /// } /// ``` pub fn draw(&self, renderer: &mut Renderer) -> MouseCursor { self.root.widget.draw( renderer, Layout::new(&self.layout), self.cursor_position, ) } /// Extract the [`Cache`] of the [`UserInterface`], consuming it in the /// process. /// /// [`Cache`]: struct.Cache.html /// [`UserInterface`]: struct.UserInterface.html pub fn into_cache(self) -> Cache { Cache { hash: self.hash, layout: self.layout, cursor_position: self.cursor_position, } } } /// Reusable data of a specific [`UserInterface`]. /// /// [`UserInterface`]: struct.UserInterface.html #[derive(Debug, Clone)] pub struct Cache { hash: u64, layout: result::Layout, cursor_position: Point, } impl Cache { /// Creates an empty [`Cache`]. /// /// You should use this to initialize a [`Cache`] before building your first /// [`UserInterface`]. /// /// [`Cache`]: struct.Cache.html /// [`UserInterface`]: struct.UserInterface.html pub fn new() -> Cache { let root: Element<'_, (), ()> = Column::new().into(); let hasher = &mut crate::Hasher::default(); root.hash_layout(hasher); Cache { hash: hasher.finish(), layout: root.compute_layout(&()), cursor_position: Point::new(0.0, 0.0), } } } impl Default for Cache { fn default() -> Cache { Cache::new() } } impl PartialEq for Cache { fn eq(&self, other: &Cache) -> bool { self.hash == other.hash && self.cursor_position == other.cursor_position } } impl Eq for Cache {}