Grab GeoJSON from the system clipboard on native. #523

This commit is contained in:
Dustin Carlino 2021-03-13 12:54:14 -08:00
parent 607ccd0e78
commit c252e4a82d
3 changed files with 107 additions and 4 deletions

62
Cargo.lock generated
View File

@ -448,6 +448,28 @@ dependencies = [
"vec_map",
]
[[package]]
name = "clipboard"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "25a904646c0340239dcf7c51677b33928bf24fdf424b79a57909c0109075b2e7"
dependencies = [
"clipboard-win",
"objc",
"objc-foundation",
"objc_id",
"x11-clipboard",
]
[[package]]
name = "clipboard-win"
version = "2.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e3a093d6fed558e5fe24c3dfc85a68bb68f1c824f440d3ba5aca189e2998786b"
dependencies = [
"winapi 0.3.9",
]
[[package]]
name = "cocoa"
version = "0.23.0"
@ -2150,6 +2172,7 @@ dependencies = [
"abstio",
"abstutil",
"anyhow",
"clipboard",
"colorous",
"contour",
"flate2",
@ -2551,6 +2574,26 @@ dependencies = [
"malloc_buf",
]
[[package]]
name = "objc-foundation"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1add1b659e36c9607c7aab864a76c7a4c2760cd0cd2e120f3fb8b952c7e22bf9"
dependencies = [
"block",
"objc",
"objc_id",
]
[[package]]
name = "objc_id"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c92d4ddb4bd7b50d730c215ff871754d0da6b2178849f8a2a2ab69712d0c073b"
dependencies = [
"objc",
]
[[package]]
name = "object"
version = "0.23.0"
@ -4524,6 +4567,15 @@ dependencies = [
"winapi-build",
]
[[package]]
name = "x11-clipboard"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "89bd49c06c9eb5d98e6ba6536cf64ac9f7ee3a009b2f53996d405b3944f6bcea"
dependencies = [
"xcb",
]
[[package]]
name = "x11-dl"
version = "2.18.5"
@ -4545,6 +4597,16 @@ dependencies = [
"libc",
]
[[package]]
name = "xcb"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5e917a3f24142e9ff8be2414e36c649d47d6cc2ba81f16201cdef96e533e02de"
dependencies = [
"libc",
"log",
]
[[package]]
name = "xcursor"
version = "0.3.3"

View File

@ -5,7 +5,7 @@ authors = ["Dustin Carlino <dabreegster@gmail.com>"]
edition = "2018"
[features]
native = ["reqwest", "tokio"]
native = ["clipboard", "reqwest", "tokio"]
wasm = ["js-sys", "wasm-bindgen", "wasm-bindgen-futures", "web-sys"]
# A marker to use a named release from S3 instead of dev for updating files
release_s3 = []
@ -15,6 +15,7 @@ aabb-quadtree = "0.1.0"
abstio = { path = "../abstio" }
abstutil = { path = "../abstutil" }
anyhow = "1.0.38"
clipboard = { version = "0.5.0", optional = true }
colorous = "1.0.3"
contour = "0.3.0"
flate2 = "1.0.20"

View File

@ -1,6 +1,11 @@
use std::io::Write;
use anyhow::Result;
use clipboard::{ClipboardContext, ClipboardProvider};
use widgetry::{EventCtx, Line, Panel, SimpleState, State, TextExt, Toggle, Transition, Widget};
use crate::tools::open_browser;
use crate::tools::{open_browser, PopupMsg};
use crate::AppLike;
pub struct ImportCity;
@ -63,10 +68,45 @@ impl<A: AppLike + 'static> SimpleState<A> for ImportCity {
}
"Import the area from your clipboard" => {
let drive_on_left = !panel.is_checked("driving side");
println!("drive on left? {}", drive_on_left);
Transition::Keep
match grab_geojson_from_clipboard() {
Ok(()) => {
println!("drive on left? {}", drive_on_left);
Transition::Keep
}
Err(err) => {
return Transition::Push(PopupMsg::new(
ctx,
"Error",
vec![
"Couldn't get GeoJSON from your clipboard".to_string(),
err.to_string(),
],
));
}
}
}
_ => unreachable!(),
}
}
}
fn grab_geojson_from_clipboard() -> Result<()> {
// TODO The clipboard crate uses old nightly Errors. Converting to anyhow is weird.
let mut ctx: ClipboardContext = match ClipboardProvider::new() {
Ok(ctx) => ctx,
Err(err) => bail!("{}", err),
};
let contents = match ctx.get_contents() {
Ok(contents) => contents,
Err(err) => bail!("{}", err),
};
if contents.parse::<geojson::GeoJson>().is_err() {
bail!(
"Your clipboard doesn't seem to have GeoJSON. Got: {}",
contents
);
}
let mut f = std::fs::File::create("boundary.geojson")?;
write!(f, "{}", contents)?;
Ok(())
}