Skip to content

Commit 0bfb5c3

Browse files
authored
fix, revision: docs, copypasta (#13)
* fix, revision: docs, copypasta * fix: rustfmt * fix: ci * fix: doc test, clippy
1 parent 095efca commit 0bfb5c3

File tree

9 files changed

+116
-107
lines changed

9 files changed

+116
-107
lines changed

.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"rust-analyzer.cargo.features": [
3+
"clipboard",
34
"geolocation",
45
"utils",
56
"i18n"

Cargo.toml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ categories = ["multimedia","os", "wasm"]
1616
#members = ["./examples/*"]
1717

1818
[features]
19-
clipboard = ["dep:clipboard"]
19+
clipboard = ["dep:copypasta"]
2020
notifications = ["dep:notify-rust"]
2121
geolocation = ["dep:windows", "dep:futures-util", "dep:futures", "dep:web-sys", "dep:wasm-bindgen", "utils"]
2222
color_scheme = ["dep:web-sys", "dep:wasm-bindgen", "dep:wasm-bindgen-futures"]
@@ -36,7 +36,7 @@ cfg-if = "1.0.0"
3636
uuid = { version = "1.3.2", features = ["v4"], optional = true }
3737
async-broadcast = { version = "0.5.1", optional = true }
3838
# clipboard
39-
clipboard = { version = "0.5.0", optional = true }
39+
copypasta = { version = "0.8.2", optional = true }
4040
# notifications
4141
notify-rust = { version = "4.8.0", optional = true }
4242
# geolocation
@@ -59,5 +59,7 @@ js-sys = "0.3.62"
5959
uuid = { version = "1.3.2", features = ["v4", "js"]}
6060

6161
[package.metadata.docs.rs]
62-
all-features = true
62+
default-target = "x86_64-pc-windows-msvc"
63+
no-default-features = true
64+
features = ["desktop-testing"]
6365

src/clipboard/clipboard.rs

Lines changed: 0 additions & 98 deletions
This file was deleted.

src/clipboard/mod.rs

Lines changed: 100 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,100 @@
1-
pub mod clipboard;
1+
//! Provides a clipboard abstraction to access the target system's clipboard.
2+
3+
use copypasta::{ClipboardContext, ClipboardProvider};
4+
use std::fmt;
5+
6+
/// Contains the context for interacting with the clipboard.
7+
///
8+
/// # Examples
9+
///
10+
/// ```
11+
/// use dioxus_std;
12+
///
13+
/// // Access the clipboard abstraction
14+
/// let mut clipboard = dioxus_std::clipboard::Clipboard::new().unwrap();
15+
///
16+
/// // Get clipboard content
17+
/// if let Ok(content) = clipboard.get_content() {
18+
/// println!("{}", content);
19+
/// }
20+
///
21+
/// // Set clipboard content
22+
/// clipboard.set_content("Hello, Dioxus!".to_string());;
23+
///
24+
/// ```
25+
pub struct Clipboard {
26+
ctx: ClipboardContext,
27+
}
28+
29+
impl Clipboard {
30+
/// Creates a new struct to utilize the clipboard abstraction.
31+
pub fn new() -> Result<Self, ClipboardError> {
32+
let ctx = match ClipboardContext::new() {
33+
Ok(ctx) => ctx,
34+
Err(e) => return Err(ClipboardError::FailedToInit(e.to_string())),
35+
};
36+
37+
Ok(Self { ctx })
38+
}
39+
40+
/// Provides a [`String`] of the target system's current clipboard content.
41+
pub fn get_content(&mut self) -> Result<String, ClipboardError> {
42+
match self.ctx.get_contents() {
43+
Ok(content) => Ok(content),
44+
Err(e) => Err(ClipboardError::FailedToFetchContent(e.to_string())),
45+
}
46+
}
47+
48+
/// Set the clipboard's content to the provided [`String`]
49+
pub fn set_content(&mut self, value: String) -> Result<(), ClipboardError> {
50+
match self.ctx.set_contents(value) {
51+
Ok(()) => Ok(()),
52+
Err(e) => Err(ClipboardError::FailedToSetContent(e.to_string())),
53+
}
54+
}
55+
}
56+
57+
/// Represents errors when utilizing the clipboard abstraction.
58+
#[derive(Debug)]
59+
pub enum ClipboardError {
60+
/// Failure when initializing the clipboard.
61+
FailedToInit(String),
62+
/// Failure to retrieve clipboard content.
63+
FailedToFetchContent(String),
64+
/// Failure to set clipboard content.
65+
FailedToSetContent(String),
66+
}
67+
68+
impl std::error::Error for ClipboardError {}
69+
impl fmt::Display for ClipboardError {
70+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
71+
match self {
72+
ClipboardError::FailedToInit(s) => write!(f, "{}", s),
73+
ClipboardError::FailedToFetchContent(s) => write!(f, "{}", s),
74+
ClipboardError::FailedToSetContent(s) => write!(f, "{}", s),
75+
}
76+
}
77+
}
78+
79+
// Tests
80+
// This doesn't work in CI.
81+
/*#[test]
82+
fn test_clipboard() {
83+
let mut clipboard = Clipboard::new().unwrap();
84+
85+
// Preserve user's clipboard contents when testing
86+
let initial_content = clipboard.get_content().unwrap();
87+
88+
// Set the content
89+
let new_content = String::from("Hello, Dioxus!");
90+
clipboard.set_content(new_content.clone()).unwrap();
91+
92+
// Get the new content
93+
let content = clipboard.get_content().unwrap();
94+
95+
// Return previous content - For some reason this only works if the test panics..?
96+
clipboard.set_content(initial_content).unwrap();
97+
98+
// Check if the abstraction worked
99+
assert_eq!(new_content, content);
100+
}*/

src/geolocation/platform/wasm.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ pub async fn get_coordinates(geolocator: &Geolocator) -> Result<Geocoordinates,
118118
)
119119
.map_err(|e| Error::DeviceError(format!("{:?}", e)))?;
120120

121-
while let Some(msg) = receiver.next().await {
121+
if let Some(msg) = receiver.next().await {
122122
receiver.close();
123123
return msg;
124124
}

src/geolocation/use_geolocation.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ pub fn use_geolocation(cx: &ScopeState) -> Result<Geocoordinates, Error> {
4444
});
4545

4646
// Get the result and return a clone
47-
let result = coords.read().map_err(|_| Error::Poisoned)?.clone();
48-
result
47+
coords.read().map_err(|_| Error::Poisoned)?.clone()
4948
}
5049

5150
/// Must be called before any use of the geolocation abstraction.

src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,9 @@ cfg_if::cfg_if! {
2323
pub mod i18n;
2424
}
2525
}
26+
27+
cfg_if::cfg_if! {
28+
if #[cfg(feature = "clipboard")] {
29+
pub mod clipboard;
30+
}
31+
}

src/utils/channel/use_channel.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ impl<MessageType: Clone> UseChannel<MessageType> {
2828
}
2929

3030
/// Create a receiver for the channel.
31-
/// You probably want to use [`use_listen_channel`].
31+
/// You probably want to use [`super::use_listen_channel()`].
3232
pub fn receiver(&mut self) -> Receiver<MessageType> {
3333
self.inactive_receiver.clone().activate()
3434
}

src/utils/rw/use_rw.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ impl<T> Clone for UseRw<T> {
3232

3333
impl<T> UseRw<T> {
3434
pub fn read(&self) -> Result<RwLockReadGuard<'_, T>, UseRwError> {
35-
Ok(self.value.read().map_err(|_| UseRwError::Poisoned)?)
35+
self.value.read().map_err(|_| UseRwError::Poisoned)
3636
}
3737

3838
pub fn write(&self, new_value: T) -> Result<(), UseRwError> {

0 commit comments

Comments
 (0)