Skip to content

Added binding for SDL_OpenURL #1138

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

Merged
merged 1 commit into from
Aug 10, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ when upgrading from a version of rust-sdl2 to another.
* **BREAKING CHANGE** Update `sdl2-sys/sdl_bindings.rs` to use enums instead of consts. If you were using `sdl2-sys`'s
enum variants directly in your project, you may be affected. If you only used sdl2 calls, there should not be any problems.

[PR #1138](https://github.com/Rust-SDL2/rust-sdl2/pull/1138) Added binding for `SDL_OpenURL`

### v0.34.5

[PR #1100](https://github.com/Rust-SDL2/rust-sdl2/pull/1100) Added binding for `SDL_GetDisplayUsableBounds`
Expand Down
1 change: 1 addition & 0 deletions src/sdl2/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ mod sdl;
pub mod surface;
pub mod timer;
pub mod touch;
pub mod url;
pub mod version;
pub mod video;

Expand Down
73 changes: 73 additions & 0 deletions src/sdl2/url.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
//! Opening URLs in default system handlers

use std::error;
use std::ffi::{CString, NulError};
use std::fmt;

use crate::get_error;

use crate::sys;

#[derive(Debug, Clone)]
pub enum OpenUrlError {
InvalidUrl(NulError),
SdlError(String),
}

impl fmt::Display for OpenUrlError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use self::OpenUrlError::*;

match *self {
InvalidUrl(ref e) => write!(f, "Invalid URL: {}", e),
SdlError(ref e) => write!(f, "SDL error: {}", e),
}
}
}

impl error::Error for OpenUrlError {
fn description(&self) -> &str {
use self::OpenUrlError::*;

match *self {
InvalidUrl(_) => "invalid URL",
SdlError(ref e) => e,
}
}
}

/// Opens a URL/URI in the default system-provided application.
///
/// This will most likely open a web browser for http:// and https:// links,
/// the default handler application for file:// links, but this varies
/// between platforms and is not supported on all of them.
/// It might also cause your window to lose focus, or pause your process on mobile.
///
/// There is no way to tell if the system successfully opened the provided URL,
/// an `Ok` result only means that something was launched to try to handle it.
///
/// # Examples
///
/// ```no_run
/// use sdl2::url::open_url;
///
/// open_url("https://github.com/Rust-SDL2/rust-sdl2")
/// .expect("Opening URLs not supported on this platform");
/// ```
#[doc(alias = "SDL_OpenURL")]
pub fn open_url(url: &str) -> Result<(), OpenUrlError> {
use self::OpenUrlError::*;
let result = unsafe {
let url = match CString::new(url) {
Ok(s) => s,
Err(err) => return Err(InvalidUrl(err)),
};
sys::SDL_OpenURL(url.as_ptr())
} == 0;

if result {
Ok(())
} else {
Err(SdlError(get_error()))
}
}