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

Window opacity methods #867

Merged
merged 2 commits into from
Mar 10, 2019
Merged
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
28 changes: 28 additions & 0 deletions src/sdl2/video.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1472,6 +1472,34 @@ impl Window {
Err(get_error())
}
}

/// Set the transparency of the window. The given value will be clamped internally between
/// `0.0` (fully transparent), and `1.0` (fully opaque).
///
/// This method returns an error if opacity isn't supported by the current platform.
pub fn set_opacity(&mut self, opacity: f32) -> Result<(), String> {
germangb marked this conversation as resolved.
Show resolved Hide resolved
let result = unsafe { sys::SDL_SetWindowOpacity(self.context.raw, opacity) };
if result < 0 {
Err(get_error())
} else {
Ok(())
}
}

/// Returns the transparency of the window, as a value between `0.0` (fully transparent), and
/// `1.0` (fully opaque).
///
/// If opacity isn't supported by the current platform, this method returns `Ok(1.0)` instead
/// of an error.
pub fn opacity(&self) -> Result<f32, String> {
let mut opacity = 0.0;
let result = unsafe { sys::SDL_GetWindowOpacity(self.context.raw, &mut opacity) };
if result < 0 {
Err(get_error())
} else {
Ok(opacity)
}
}
}

#[derive(Copy, Clone)]
Expand Down