Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Dioxus 0.5 support #23

Merged
merged 29 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@ members = [
]

[workspace.dependencies]
dioxus-std = { path = "./std"}
dioxus-std = { path = "./std" }
dioxus = { git = "https://github.com/DioxusLabs/dioxus", rev = "ef101dd876ee8ecdd702a7e22addfd47f2ebd892" }
dioxus-web = { git = "https://github.com/DioxusLabs/dioxus", rev = "ef101dd876ee8ecdd702a7e22addfd47f2ebd892" }
dioxus-desktop = { git = "https://github.com/DioxusLabs/dioxus", rev = "ef101dd876ee8ecdd702a7e22addfd47f2ebd892" }
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ fn app(cx: Scope) -> Element {

match coords {
Ok(coords) => {
render! { p { format!("Latitude: {} | Longitude: {}", coords.latitude, coords.longitude) } }
rsx! { p { format!("Latitude: {} | Longitude: {}", coords.latitude, coords.longitude) } }
}
Err(Error::NotInitialized) => {
render! { p { "Initializing..." }}
rsx! { p { "Initializing..." }}
}
Err(e) => {
render! { p { "An error occurred {e}" }}
rsx! { p { "An error occurred {e}" }}
}
}
}
Expand All @@ -79,7 +79,7 @@ sudo apt-get install xorg-dev
You can add `dioxus-std` to your application by adding it to your dependencies.
```toml
[dependencies]
dioxus-std = { version = "0.4.2", features = [] }
dioxus-std = { version = "0.5", features = [] }
```

## License
Expand Down
4 changes: 2 additions & 2 deletions examples/channel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ edition = "2021"

[dependencies]
dioxus-std = { workspace = true, features = ["utils"]}
dioxus = "0.4"
dioxus-web = "0.4"
dioxus = { workspace = true }
dioxus-web = { workspace = true }

log = "0.4.6"

Expand Down
12 changes: 6 additions & 6 deletions examples/channel/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,27 @@ fn main() {
wasm_logger::init(wasm_logger::Config::default());
console_error_panic_hook::set_once();

dioxus_web::launch(app);
launch(app);
}

fn app(cx: Scope) -> Element {
let channel = use_channel::<String>(cx, 5);
fn app() -> Element {
let channel = use_channel::<String>(5);

use_listen_channel(cx, &channel, |message| async {
use_listen_channel(&channel, |message| async {
match message {
Ok(value) => log::info!("Incoming message: {value}"),
Err(err) => log::info!("Error: {err:?}"),
}
});

let send = |_: MouseEvent| {
let send = move |_: MouseEvent| {
to_owned![channel];
async move {
channel.send("Hello").await.ok();
}
};

render!(
rsx!(
button {
onclick: send,
"Send hello"
Expand Down
4 changes: 2 additions & 2 deletions examples/clipboard/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ edition = "2021"

[dependencies]
dioxus-std = { workspace = true, features = ["clipboard"] }
dioxus = "0.4"
dioxus-desktop = "0.4"
dioxus = { workspace = true }
dioxus-desktop = { workspace = true }
14 changes: 7 additions & 7 deletions examples/clipboard/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ fn main() {
dioxus_desktop::launch(app);
}

fn app(cx: Scope) -> Element {
let clipboard = use_clipboard(cx);
let text = use_state(cx, String::new);
fn app() -> Element {
let mut clipboard = use_clipboard();
let mut text = use_signal(String::new);

let oninput = |e: FormEvent| {
let oninput = move |e: FormEvent| {
text.set(e.data.value.clone());
};

let oncopy = {
to_owned![clipboard];
move |_| match clipboard.set(text.get().clone()) {
Ok(_) => println!("Copied to clipboard: {}", text.get()),
move |_| match clipboard.set(text.read().clone()) {
Ok(_) => println!("Copied to clipboard: {}", text.read()),
Err(err) => println!("Error on copy: {err:?}"),
}
};
Expand All @@ -29,7 +29,7 @@ fn app(cx: Scope) -> Element {
Err(err) => println!("Error on paste: {err:?}"),
};

render!(
rsx!(
input {
oninput: oninput,
value: "{text}"
Expand Down
4 changes: 2 additions & 2 deletions examples/color_scheme/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ edition = "2021"

[dependencies]
dioxus-std = { workspace = true, features = ["color_scheme"] }
dioxus = "0.4"
dioxus-web = "0.4"
dioxus = { workspace = true }
dioxus-web = { workspace = true }

log = "0.4.6"

Expand Down
6 changes: 3 additions & 3 deletions examples/color_scheme/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ fn main() {
dioxus_web::launch(app);
}

fn app(cx: Scope) -> Element {
let color_scheme = use_preferred_color_scheme(cx);
fn app() -> Element {
let color_scheme = use_preferred_color_scheme();

render!(
rsx!(
div {
style: "text-align: center;",
h1 { "🌗 Dioxus 🚀" }
Expand Down
6 changes: 3 additions & 3 deletions examples/geolocation/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ edition = "2021"

[dependencies]
dioxus-std = { workspace = true, features = ["geolocation"] }
dioxus = "0.4"
dioxus-desktop = "0.4"
#dioxus-web = "0.4"
dioxus = { workspace = true }
dioxus-desktop ={ workspace = true }
# dioxus-web ={ workspace = true }
10 changes: 4 additions & 6 deletions examples/geolocation/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@ fn main() {
//dioxus_web::launch(app);
}

fn app(cx: Scope) -> Element {
let geolocator = init_geolocator(cx, PowerMode::High).unwrap();
let initial_coords = use_future(cx, (), |_| async move {
geolocator.get_coordinates().await.unwrap()
});
let latest_coords = use_geolocation(cx);
fn app() -> Element {
let geolocator = init_geolocator(PowerMode::High).unwrap();
let initial_coords = use_future(|_| async move { geolocator.get_coordinates().await.unwrap() });
let latest_coords = use_geolocation();

let latest_coords = match latest_coords {
Ok(v) => v,
Expand Down
4 changes: 2 additions & 2 deletions examples/i18n/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ edition = "2021"

[dependencies]
dioxus-std = { workspace = true, features = ["i18n"] }
dioxus = "0.4"
dioxus-web = "0.4"
dioxus = { workspace = true }
dioxus-web = { workspace = true }

log = "0.4.6"

Expand Down
25 changes: 10 additions & 15 deletions examples/i18n/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ static EN_US: &str = include_str!("./en-US.json");
static ES_ES: &str = include_str!("./es-ES.json");

#[allow(non_snake_case)]
fn Body(cx: Scope) -> Element {
let i18 = use_i18(cx);
fn Body() -> Element {
let i18 = use_i18();

let change_to_english = move |_| i18.set_language("en-US".parse().unwrap());
let change_to_spanish = move |_| i18.set_language("es-ES".parse().unwrap());

render!(
rsx!(
button {
onclick: change_to_english,
label {
Expand All @@ -40,16 +40,11 @@ fn Body(cx: Scope) -> Element {
}

fn app(cx: Scope) -> Element {
use_init_i18n(
cx,
"en-US".parse().unwrap(),
"en-US".parse().unwrap(),
|| {
let en_us = Language::from_str(EN_US).unwrap();
let es_es = Language::from_str(ES_ES).unwrap();
vec![en_us, es_es]
},
);

render!(Body {})
use_init_i18n("en-US".parse().unwrap(), "en-US".parse().unwrap(), || {
let en_us = Language::from_str(EN_US).unwrap();
let es_es = Language::from_str(ES_ES).unwrap();
vec![en_us, es_es]
});

rsx!(Body {})
}
4 changes: 2 additions & 2 deletions std/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "dioxus-std"
version = "0.4.2"
version = "0.5.0"
authors = ["Jonathan Kelley", "Dioxus Labs", "ealmloff", "DogeDark", "marc2332"]
edition = "2021"
description = "Platform agnostic library for supercharging your productivity with Dioxus"
Expand All @@ -25,7 +25,7 @@ wasm-testing = ["geolocation", "color_scheme", "utils", "i18n"]
desktop-testing = ["clipboard", "notifications", "geolocation", "utils", "i18n"]

[dependencies]
dioxus = { version = "0.4" }
dioxus = { workspace = true }
cfg-if = "1.0.0"

# utils
Expand Down
134 changes: 69 additions & 65 deletions std/src/clipboard/use_clipboard.rs
Original file line number Diff line number Diff line change
@@ -1,65 +1,69 @@
//! Provides a clipboard abstraction to access the target system's clipboard.

use copypasta::{ClipboardContext, ClipboardProvider};
use dioxus::prelude::{RefCell, ScopeState};
use std::rc::Rc;

#[derive(Debug, PartialEq, Clone)]
pub enum ClipboardError {
FailedToRead,
FailedToSet,
}

/// Handle to access the ClipboardContext.
#[derive(Clone)]
pub struct UseClipboard {
clipboard: Rc<RefCell<ClipboardContext>>,
}

impl UseClipboard {
// Read from the clipboard
pub fn get(&self) -> Result<String, ClipboardError> {
self.clipboard
.borrow_mut()
.get_contents()
.map_err(|_| ClipboardError::FailedToRead)
}

// Write to the clipboard
pub fn set(&self, contents: String) -> Result<(), ClipboardError> {
self.clipboard
.borrow_mut()
.set_contents(contents)
.map_err(|_| ClipboardError::FailedToSet)
}
}

/// Access the clipboard.
///
/// # Examples
///
/// ```ignore
/// use dioxus_std::clipboard::use_clipboard;
///
/// // Get a handle to the clipboard
/// let clipboard = use_clipboard(cx);
///
/// // Read the clipboard content
/// if let Ok(content) = clipboard.get() {
/// println!("{}", content);
/// }
///
/// // Write to the clipboard
/// clipboard.set("Hello, Dioxus!".to_string());;
///
/// ```
pub fn use_clipboard(cx: &ScopeState) -> UseClipboard {
let clipboard = match cx.consume_context() {
Some(rt) => rt,
None => {
let clipboard = ClipboardContext::new().expect("Cannot create Clipboard.");
cx.provide_root_context(Rc::new(RefCell::new(clipboard)))
}
};
UseClipboard { clipboard }
}
//! Provides a clipboard abstraction to access the target system's clipboard.

use copypasta::{ClipboardContext, ClipboardProvider};
use dioxus::prelude::*;

#[derive(Debug, PartialEq, Clone)]
pub enum ClipboardError {
FailedToRead,
FailedToSet,
NotAvailable,
}

/// Handle to access the ClipboardContext.
#[derive(Clone)]
pub struct UseClipboard {
clipboard: Signal<Option<ClipboardContext>>,
}

impl UseClipboard {
// Read from the clipboard
pub fn get(&mut self) -> Result<String, ClipboardError> {
self.clipboard
.write()
.as_mut()
.ok_or_else(|| ClipboardError::NotAvailable)?
DogeDark marked this conversation as resolved.
Show resolved Hide resolved
.get_contents()
.map_err(|_| ClipboardError::FailedToRead)
DogeDark marked this conversation as resolved.
Show resolved Hide resolved
}

// Write to the clipboard
pub fn set(&mut self, contents: String) -> Result<(), ClipboardError> {
self.clipboard
.write()
.as_mut()
.ok_or_else(|| ClipboardError::NotAvailable)?
DogeDark marked this conversation as resolved.
Show resolved Hide resolved
.set_contents(contents)
.map_err(|_| ClipboardError::FailedToSet)
DogeDark marked this conversation as resolved.
Show resolved Hide resolved
}
}

/// Access the clipboard.
///
/// # Examples
///
/// ```ignore
/// use dioxus_std::clipboard::use_clipboard;
///
/// // Get a handle to the clipboard
/// let mut clipboard = use_clipboard();
///
/// // Read the clipboard content
/// if let Ok(content) = clipboard.get() {
/// println!("{}", content);
/// }
///
/// // Write to the clipboard
/// clipboard.set("Hello, Dioxus!".to_string());;
///
/// ```
pub fn use_clipboard() -> UseClipboard {
let clipboard = match try_consume_context() {
Some(rt) => rt,
None => {
let clipboard = ClipboardContext::new().ok();
provide_root_context(Signal::new(clipboard))
}
};
UseClipboard { clipboard }
}
Loading