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

Implement sdl2::hint::video_minimize_on_focus_lost #639

Merged
merged 1 commit into from
Apr 19, 2017
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
65 changes: 65 additions & 0 deletions src/sdl2/hint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,77 @@ use sys::hint as ll;
use std::ptr;
use libc::c_char;

const VIDEO_MINIMIZE_ON_FOCUS_LOST: &'static str = "SDL_VIDEO_MINIMIZE_ON_FOCUS_LOST";

pub enum Hint {
Default,
Normal,
Override
}

/// A hint that specifies whether a fullscreen [Window](../video/Window.t.html) will be
/// minimized if key focus is lost.
///
/// [Official SDL documentation](https://wiki.libsdl.org/SDL_HINT_VIDEO_MINIMIZE_ON_FOCUS_LOSS)
///
/// # Default
/// This is enabled by default.
///
/// # Example
/// ```rust,no_run
/// sdl2::hint::set_video_minimize_on_focus_lost(false);
/// ```
///
/// * `value`: `true` to enable minimizing of the Window if it loses key focus when in fullscreen mode,
/// `false` to disable this feature.
pub fn set_video_minimize_on_focus_lost(value: bool) -> bool {
set(VIDEO_MINIMIZE_ON_FOCUS_LOST, if value { "1" } else { "0" })
}

/// A hint that specifies whether a fullscreen [Window](../video/Window.t.html) will be
/// minimized if key focus is lost.
///
/// [Official SDL documentation](https://wiki.libsdl.org/SDL_HINT_VIDEO_MINIMIZE_ON_FOCUS_LOSS)
///
/// # Example
/// ```rust,no_run
/// sdl2::hint::set_video_minimize_on_focus_lost_with_priority(false, sdl2::hint::Hint::Override);
/// ```
///
/// * `value`: `true` to enable minimizing of the Window if it loses key focus when in fullscreen mode,
/// `false` to disable this feature.
/// * `priority`: The priority controls the behavior when setting a hint that already has a value.
/// Hints will replace existing hints of their priority and lower.
/// Environment variables are considered to have override priority.
pub fn set_video_minimize_on_focus_lost_with_priority(value: bool, priority: Hint) -> bool {
set_with_priority(VIDEO_MINIMIZE_ON_FOCUS_LOST, if value { "1" } else { "0" }, priority)
}

/// A hint that specifies whether a fullscreen [Window](../video/Window.t.html) will be
/// minimized if key focus is lost.
///
/// [Official SDL documentation](https://wiki.libsdl.org/SDL_HINT_VIDEO_MINIMIZE_ON_FOCUS_LOSS)
///
/// # Default
/// By default this will return `true`.
///
/// # Example
/// ```rust,no_run
/// assert_eq!(sdl2::hint::get_video_minimize_on_focus_lost(), true);
///
/// sdl2::hint::set_video_minimize_on_focus_lost(false);
/// assert_eq!(sdl2::hint::get_video_minimize_on_focus_lost(), false);
/// ```
pub fn get_video_minimize_on_focus_lost() -> bool {
match get(VIDEO_MINIMIZE_ON_FOCUS_LOST) {
Some(value) => match &*value {
"1" => true,
Copy link
Contributor Author

@phrohdoh phrohdoh Apr 19, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I deliberately chose to map anything non-"1" to false because a consumer could set a hint to "foobar" if they wanted to for whatever reason but, AFAIK, that will not enable the feature (ignoring the default of being enabled for this particular hint).

_ => false,
},
_ => true
}
}

pub fn set(name: &str, value: &str) -> bool{
let name = CString::new(name).unwrap();
let value = CString::new(value).unwrap();
Expand Down