pix_engine/gui/
system.rs

1//! Operating System related methods.
2//!
3//! Provided [`PixState`] methods:
4//!
5//! - [`PixState::clipboard_text`]
6//! - [`PixState::set_clipboard_text`]
7//! - [`PixState::open_url`]
8//!
9//! # Example
10//!
11//! ```
12//! # use pix_engine::prelude::*;
13//! # struct App { text_entry: String };
14//! # impl PixEngine for App {
15//! fn on_update(&mut self, s: &mut PixState) -> PixResult<()> {
16//!     if s.button("Open Homepage")? {
17//!         s.open_url("https://example.com")?;
18//!     }
19//!     Ok(())
20//! }
21//!
22//! fn on_key_pressed(&mut self, s: &mut PixState, event: KeyEvent) -> PixResult<bool> {
23//!     match (event.keymod, event.key) {
24//!         (KeyMod::CTRL, Key::C) => s.set_clipboard_text(&self.text_entry)?,
25//!         (KeyMod::CTRL, Key::V) => self.text_entry = s.clipboard_text(),
26//!         _ => (),
27//!     }
28//!     Ok(false)
29//! }
30//! # }
31//! ```
32
33use crate::{prelude::*, renderer::Rendering};
34
35impl PixState {
36    /// Get clipboard text from the system clipboard.
37    ///
38    /// # Example
39    ///
40    /// ```
41    /// # use pix_engine::prelude::*;
42    /// # struct App { checkbox: bool, text_field: String };
43    /// # impl PixEngine for App {
44    /// fn on_update(&mut self, s: &mut PixState) -> PixResult<()> {
45    ///     println!("Current clipboard text: {}", s.clipboard_text());
46    ///     Ok(())
47    /// }
48    /// # }
49    /// ```
50    #[inline]
51    #[must_use]
52    pub fn clipboard_text(&self) -> String {
53        self.renderer.clipboard_text()
54    }
55
56    /// Set clipboard text to the system clipboard.
57    ///
58    /// # Errors
59    ///
60    /// If the renderer fails to retrieve the clipboard text, then an error is returned.
61    ///
62    /// # Example
63    ///
64    /// ```
65    /// # use pix_engine::prelude::*;
66    /// # struct App { checkbox: bool, text_field: String };
67    /// # impl PixEngine for App {
68    /// fn on_update(&mut self, s: &mut PixState) -> PixResult<()> {
69    ///     s.set_clipboard_text("Copied to clipboard!")?;
70    ///     Ok(())
71    /// }
72    /// # }
73    /// ```
74    #[inline]
75    pub fn set_clipboard_text<S>(&self, value: S) -> PixResult<()>
76    where
77        S: AsRef<str>,
78    {
79        self.renderer.set_clipboard_text(value.as_ref())
80    }
81
82    /// Open a URL in the default system browser.
83    ///
84    /// # Errors
85    ///
86    /// If the renderer fails to launch an external application, then an error is returned. Note,
87    /// there is no way to know if the URL opened successfully or not. An `Ok` result only means an
88    /// application was successfully launched to handle the URL.
89    ///
90    /// # Example
91    ///
92    /// ```
93    /// # use pix_engine::prelude::*;
94    /// # struct App;
95    /// # impl PixEngine for App {
96    /// fn on_update(&mut self, s: &mut PixState) -> PixResult<()> {
97    ///     if s.button("Open Homepage")? {
98    ///         s.open_url("https://example.com")?;
99    ///     }
100    ///     Ok(())
101    /// }
102    /// # }
103    /// ```
104    #[inline]
105    pub fn open_url<S>(&self, url: S) -> PixResult<()>
106    where
107        S: AsRef<str>,
108    {
109        self.renderer.open_url(url.as_ref())
110    }
111}