widgetry/tools/
mod.rs

1mod choose_something;
2mod colors;
3mod lasso;
4mod load;
5mod popup;
6mod prompt_input;
7pub(crate) mod screenshot;
8mod url;
9pub(crate) mod warper;
10
11use anyhow::Result;
12
13pub use choose_something::ChooseSomething;
14pub use colors::{ColorLegend, ColorScale, DivergingScale};
15pub use lasso::{Lasso, PolyLineLasso};
16pub use load::{FileLoader, FutureLoader, RawBytes};
17pub use popup::PopupMsg;
18pub use prompt_input::PromptInput;
19pub use url::URLManager;
20
21use crate::{Color, GfxCtx};
22use geom::Polygon;
23
24/// Store a cached key/value pair, only recalculating when the key changes.
25pub struct Cached<K: PartialEq + Clone, V> {
26    contents: Option<(K, V)>,
27}
28
29impl<K: PartialEq + Clone, V> Cached<K, V> {
30    pub fn new() -> Cached<K, V> {
31        Cached { contents: None }
32    }
33
34    /// Get the current key.
35    pub fn key(&self) -> Option<K> {
36        self.contents.as_ref().map(|(k, _)| k.clone())
37    }
38
39    /// Get the current value.
40    pub fn value(&self) -> Option<&V> {
41        self.contents.as_ref().map(|(_, v)| v)
42    }
43
44    /// Get the current value, mutably.
45    pub fn value_mut(&mut self) -> Option<&mut V> {
46        self.contents.as_mut().map(|(_, v)| v)
47    }
48
49    /// Update the value if the key has changed.
50    pub fn update<F: FnMut(K) -> V>(&mut self, key: Option<K>, mut produce_value: F) {
51        if let Some(new_key) = key {
52            if self.key() != Some(new_key.clone()) {
53                self.contents = Some((new_key.clone(), produce_value(new_key)));
54            }
55        } else {
56            self.contents = None;
57        }
58    }
59
60    /// `update` is preferred, but sometimes `produce_value` needs to borrow the same struct that
61    /// owns this `Cached`. In that case, the caller can manually check `key` and call this.
62    pub fn set(&mut self, key: K, value: V) {
63        self.contents = Some((key, value));
64    }
65
66    pub fn clear(&mut self) {
67        self.contents = None;
68    }
69
70    /// Clears the current pair and returns it.
71    pub fn take(&mut self) -> Option<(K, V)> {
72        self.contents.take()
73    }
74}
75
76impl<K: PartialEq + Clone, V> Default for Cached<K, V> {
77    fn default() -> Self {
78        Cached::new()
79    }
80}
81
82pub fn open_browser<I: AsRef<str>>(url: I) {
83    let _ = webbrowser::open(url.as_ref());
84}
85
86fn grey_out_map(g: &mut GfxCtx) {
87    // This is a copy of grey_out_map from map_gui, with no dependencies on App
88    g.fork_screenspace();
89    g.draw_polygon(
90        Color::BLACK.alpha(0.6),
91        Polygon::rectangle(g.canvas.window_width, g.canvas.window_height),
92    );
93    g.unfork();
94}
95
96/// Only works on native
97pub fn set_clipboard(x: String) {
98    #[cfg(not(target_arch = "wasm32"))]
99    {
100        use clipboard::{ClipboardContext, ClipboardProvider};
101        if let Err(err) =
102            ClipboardProvider::new().and_then(|mut ctx: ClipboardContext| ctx.set_contents(x))
103        {
104            error!("Copying to clipboard broke: {}", err);
105        }
106    }
107
108    #[cfg(target_arch = "wasm32")]
109    {
110        let _ = x;
111    }
112}
113
114pub fn get_clipboard() -> Result<String> {
115    #[cfg(not(target_arch = "wasm32"))]
116    {
117        use clipboard::{ClipboardContext, ClipboardProvider};
118        // TODO The clipboard crate uses old nightly Errors. Converting to anyhow is weird.
119        let mut ctx: ClipboardContext = match ClipboardProvider::new() {
120            Ok(ctx) => ctx,
121            Err(err) => bail!("{}", err),
122        };
123        let contents = match ctx.get_contents() {
124            Ok(contents) => contents,
125            Err(err) => bail!("{}", err),
126        };
127        Ok(contents)
128    }
129
130    #[cfg(target_arch = "wasm32")]
131    {
132        bail!("Unsupported on web");
133    }
134}