#![deny(missing_debug_implementations)]
#[cfg(not(any(target_os = "macos", target_os = "redox", windows)))]
#[cfg(feature = "wayland")]
#[macro_use]
extern crate dlib;
use std::fmt;
use std::os::raw;
#[derive(Clone, Copy, Debug)]
pub enum Scale {
FitScreen,
X1,
X2,
X4,
X8,
X16,
X32,
}
#[derive(PartialEq, Clone, Copy, Debug)]
pub enum KeyRepeat {
Yes,
No,
}
#[derive(PartialEq, Clone, Copy, Debug)]
pub enum MouseButton {
Left,
Middle,
Right,
}
#[derive(PartialEq, Clone, Copy, Debug)]
pub enum MouseMode {
Pass,
Clamp,
Discard,
}
#[derive(PartialEq, Clone, Copy, Debug)]
pub enum CursorStyle {
Arrow,
Ibeam,
Crosshair,
ClosedHand,
OpenHand,
ResizeLeftRight,
ResizeUpDown,
ResizeAll,
}
pub trait InputCallback {
fn add_char(&mut self, uni_char: u32);
fn set_key_state(&mut self, _key: Key, _state: bool) {}
}
mod error;
pub use self::error::Error;
pub type Result<T> = std::result::Result<T, Error>;
pub use icon::Icon;
pub use raw_window_handle::HasRawWindowHandle;
mod key;
pub use key::Key;
mod buffer_helper;
mod icon;
mod key_handler;
mod mouse_handler;
mod os;
mod rate;
mod window_flags;
#[cfg(target_os = "macos")]
use self::os::macos as imp;
#[cfg(any(
target_os = "linux",
target_os = "freebsd",
target_os = "dragonfly",
target_os = "netbsd",
target_os = "openbsd"
))]
use self::os::posix as imp;
#[cfg(target_os = "redox")]
use self::os::redox as imp;
#[cfg(target_arch = "wasm32")]
use self::os::wasm as imp;
#[cfg(target_os = "windows")]
use self::os::windows as imp;
pub struct Window(imp::Window);
impl fmt::Debug for Window {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_tuple("Window").field(&format_args!("..")).finish()
}
}
unsafe impl raw_window_handle::HasRawWindowHandle for Window {
fn raw_window_handle(&self) -> raw_window_handle::RawWindowHandle {
self.0.raw_window_handle()
}
}
pub fn clamp<T: PartialOrd>(low: T, value: T, high: T) -> T {
if value < low {
low
} else if value > high {
high
} else {
value
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum ScaleMode {
Stretch,
AspectRatioStretch,
Center,
UpperLeft,
}
#[derive(Clone, Copy, Debug)]
pub struct WindowOptions {
pub borderless: bool,
pub title: bool,
pub resize: bool,
pub scale: Scale,
pub scale_mode: ScaleMode,
pub topmost: bool,
pub transparency: bool,
pub none: bool,
}
impl Window {
pub fn new(name: &str, width: usize, height: usize, opts: WindowOptions) -> Result<Window> {
if opts.transparency && !opts.borderless {
return Err(Error::WindowCreate(
"Window transparency requires the borderless property".to_owned(),
));
}
imp::Window::new(name, width, height, opts).map(Window)
}
pub fn set_title(&mut self, title: &str) {
self.0.set_title(title)
}
pub fn set_icon(&mut self, icon: Icon) {
self.0.set_icon(icon)
}
#[inline]
pub fn get_window_handle(&self) -> *mut raw::c_void {
self.0.get_window_handle()
}
#[inline]
pub fn update_with_buffer(
&mut self,
buffer: &[u32],
width: usize,
height: usize,
) -> Result<()> {
self.0.update_rate();
self.0
.update_with_buffer_stride(buffer, width, height, width)
}
#[inline]
pub fn update(&mut self) {
self.0.update_rate();
self.0.update()
}
#[inline]
pub fn is_open(&self) -> bool {
self.0.is_open()
}
#[inline]
pub fn set_position(&mut self, x: isize, y: isize) {
self.0.set_position(x, y)
}
#[inline]
pub fn get_position(&self) -> (isize, isize) {
self.0.get_position()
}
#[inline]
pub fn topmost(&self, topmost: bool) {
self.0.topmost(topmost)
}
#[inline]
pub fn set_background_color(&mut self, red: usize, green: usize, blue: usize) {
let r = clamp(0, red, 255);
let g = clamp(0, green, 255);
let b = clamp(0, blue, 255);
self.0
.set_background_color(((r << 16) | (g << 8) | b) as u32);
}
#[inline]
pub fn set_cursor_visibility(&mut self, visibility: bool) {
self.0.set_cursor_visibility(visibility);
}
#[inline]
pub fn limit_update_rate(&mut self, time: Option<std::time::Duration>) {
self.0.set_rate(time)
}
#[inline]
pub fn get_size(&self) -> (usize, usize) {
self.0.get_size()
}
#[inline]
pub fn get_mouse_pos(&self, mode: MouseMode) -> Option<(f32, f32)> {
self.0.get_mouse_pos(mode)
}
#[inline]
pub fn get_unscaled_mouse_pos(&self, mode: MouseMode) -> Option<(f32, f32)> {
self.0.get_unscaled_mouse_pos(mode)
}
#[inline]
pub fn get_mouse_down(&self, button: MouseButton) -> bool {
self.0.get_mouse_down(button)
}
#[inline]
pub fn get_scroll_wheel(&self) -> Option<(f32, f32)> {
self.0.get_scroll_wheel()
}
pub fn set_cursor_style(&mut self, cursor: CursorStyle) {
self.0.set_cursor_style(cursor)
}
#[inline]
pub fn get_keys(&self) -> Vec<Key> {
self.0.get_keys()
}
#[inline]
pub fn get_keys_pressed(&self, repeat: KeyRepeat) -> Vec<Key> {
self.0.get_keys_pressed(repeat)
}
#[inline]
pub fn get_keys_released(&self) -> Vec<Key> {
self.0.get_keys_released()
}
#[inline]
pub fn is_key_down(&self, key: Key) -> bool {
self.0.is_key_down(key)
}
#[inline]
pub fn is_key_pressed(&self, key: Key, repeat: KeyRepeat) -> bool {
self.0.is_key_pressed(key, repeat)
}
#[inline]
pub fn is_key_released(&self, key: Key) -> bool {
self.0.is_key_released(key)
}
#[inline]
pub fn set_key_repeat_delay(&mut self, delay: f32) {
self.0.set_key_repeat_delay(delay)
}
#[inline]
pub fn set_key_repeat_rate(&mut self, rate: f32) {
self.0.set_key_repeat_rate(rate)
}
#[inline]
pub fn is_active(&mut self) -> bool {
self.0.is_active()
}
#[inline]
pub fn set_input_callback(&mut self, callback: Box<dyn InputCallback>) {
self.0.set_input_callback(callback)
}
#[inline]
pub fn add_menu(&mut self, menu: &Menu) -> MenuHandle {
self.0.add_menu(&menu.0)
}
#[inline]
pub fn remove_menu(&mut self, handle: MenuHandle) {
self.0.remove_menu(handle)
}
#[cfg(any(target_os = "macos", target_os = "windows", target_arch = "wasm32"))]
pub fn get_posix_menus(&self) -> Option<&Vec<UnixMenu>> {
None
}
#[cfg(any(
target_os = "linux",
target_os = "freebsd",
target_os = "dragonfly",
target_os = "netbsd",
target_os = "openbsd",
target_os = "redox"
))]
pub fn get_posix_menus(&self) -> Option<&Vec<UnixMenu>> {
self.0.get_posix_menus()
}
#[deprecated(
since = "0.17.0",
note = "`get_unix_menus` will be removed in 1.0.0, use `get_posix_menus` instead"
)]
pub fn get_unix_menus(&self) -> Option<&Vec<UnixMenu>> {
self.get_posix_menus()
}
#[inline]
pub fn is_menu_pressed(&mut self) -> Option<usize> {
self.0.is_menu_pressed()
}
}
pub const MENU_KEY_COMMAND: usize = 1;
pub const MENU_KEY_WIN: usize = 2;
pub const MENU_KEY_SHIFT: usize = 4;
pub const MENU_KEY_CTRL: usize = 8;
pub const MENU_KEY_ALT: usize = 16;
const MENU_ID_SEPARATOR: usize = 0xffffffff;
#[derive(Debug, Clone)]
pub struct UnixMenu {
pub name: String,
pub items: Vec<UnixMenuItem>,
#[doc(hidden)]
pub handle: MenuHandle,
#[doc(hidden)]
pub item_counter: MenuItemHandle,
}
#[derive(Debug, Clone)]
pub struct UnixMenuItem {
pub sub_menu: Option<Box<UnixMenu>>,
pub handle: MenuItemHandle,
pub id: usize,
pub label: String,
pub enabled: bool,
pub key: Key,
pub modifier: usize,
}
#[derive(Debug, Copy, Clone)]
#[doc(hidden)]
pub struct MenuItemHandle(pub u64);
#[derive(Debug, Copy, Clone, PartialEq)]
#[doc(hidden)]
pub struct MenuHandle(pub u64);
pub struct Menu(imp::Menu);
impl fmt::Debug for Menu {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_tuple("Menu").field(&format_args!("..")).finish()
}
}
impl Menu {
pub fn new(name: &str) -> Result<Menu> {
imp::Menu::new(name).map(Menu)
}
#[inline]
pub fn destroy_menu(&mut self) {
}
#[inline]
pub fn add_sub_menu(&mut self, name: &str, menu: &Menu) {
self.0.add_sub_menu(name, &menu.0)
}
pub fn add_separator(&mut self) {
self.add_menu_item(&MenuItem {
id: MENU_ID_SEPARATOR,
..MenuItem::default()
});
}
#[inline]
pub fn add_menu_item(&mut self, item: &MenuItem) -> MenuItemHandle {
self.0.add_menu_item(item)
}
#[inline]
pub fn add_item(&mut self, name: &str, id: usize) -> MenuItem {
MenuItem {
id,
label: name.to_owned(),
menu: Some(self),
..MenuItem::default()
}
}
#[inline]
pub fn remove_item(&mut self, item: &MenuItemHandle) {
self.0.remove_item(item)
}
}
#[derive(Debug)]
pub struct MenuItem<'a> {
pub id: usize,
pub label: String,
pub enabled: bool,
pub key: Key,
pub modifier: usize,
#[doc(hidden)]
pub menu: Option<&'a mut Menu>,
}
impl<'a> Default for MenuItem<'a> {
fn default() -> Self {
MenuItem {
id: MENU_ID_SEPARATOR,
label: "".to_owned(),
enabled: true,
key: Key::Unknown,
modifier: 0,
menu: None,
}
}
}
impl<'a> Clone for MenuItem<'a> {
fn clone(&self) -> Self {
MenuItem {
id: self.id,
label: self.label.clone(),
enabled: self.enabled,
key: self.key,
modifier: self.modifier,
menu: None,
}
}
}
impl<'a> MenuItem<'a> {
pub fn new(name: &str, id: usize) -> MenuItem {
MenuItem {
id,
label: name.to_owned(),
..MenuItem::default()
}
}
#[inline]
pub fn shortcut(self, key: Key, modifier: usize) -> Self {
MenuItem {
key,
modifier,
..self
}
}
#[inline]
pub fn separator(self) -> Self {
MenuItem {
id: MENU_ID_SEPARATOR,
..self
}
}
#[inline]
pub fn enabled(self, enabled: bool) -> Self {
MenuItem { enabled, ..self }
}
#[inline]
pub fn build(&mut self) -> MenuItemHandle {
let t = self.clone();
if let Some(ref mut menu) = self.menu {
menu.0.add_menu_item(&t)
} else {
MenuItemHandle(0)
}
}
}
impl Default for WindowOptions {
fn default() -> WindowOptions {
WindowOptions {
borderless: false,
transparency: false,
title: true,
resize: false,
scale: Scale::X1,
scale_mode: ScaleMode::Stretch,
topmost: false,
none: false,
}
}
}