use std::rc::Rc;
use std::sync::Arc;
use teksilo_core::{PlatformError, PlatformTitleBarHost, ResizeEdge, TitleBarHostCallbacks};
use winit::window::{ResizeDirection, Window};
#[cfg_attr(target_os = "macos", allow(dead_code))]
pub(crate) fn edge_to_direction(edge: ResizeEdge) -> ResizeDirection {
match edge {
ResizeEdge::Top => ResizeDirection::North,
ResizeEdge::TopRight => ResizeDirection::NorthEast,
ResizeEdge::Right => ResizeDirection::East,
ResizeEdge::BottomRight => ResizeDirection::SouthEast,
ResizeEdge::Bottom => ResizeDirection::South,
ResizeEdge::BottomLeft => ResizeDirection::SouthWest,
ResizeEdge::Left => ResizeDirection::West,
ResizeEdge::TopLeft => ResizeDirection::NorthWest,
}
}
#[cfg(target_os = "macos")]
mod macos;
#[cfg(all(unix, not(target_os = "macos")))]
mod wayland;
#[cfg(target_os = "windows")]
mod windows;
#[cfg(all(unix, not(target_os = "macos")))]
mod x11;
#[cfg(target_os = "macos")]
pub use macos::MacOsHost;
#[cfg(all(unix, not(target_os = "macos")))]
pub use wayland::WaylandHost;
#[cfg(target_os = "windows")]
pub use windows::WindowsHost;
#[cfg(all(unix, not(target_os = "macos")))]
pub use x11::X11Host;
pub fn create_title_bar_host(
window: Arc<Window>,
callbacks: TitleBarHostCallbacks,
) -> Result<Rc<dyn PlatformTitleBarHost>, PlatformError> {
#[cfg(target_os = "windows")]
{
WindowsHost::new(window, callbacks).map(|h| Rc::new(h) as Rc<dyn PlatformTitleBarHost>)
}
#[cfg(target_os = "macos")]
{
MacOsHost::new(window, callbacks).map(|h| Rc::new(h) as Rc<dyn PlatformTitleBarHost>)
}
#[cfg(all(unix, not(target_os = "macos")))]
{
use winit::raw_window_handle::HasDisplayHandle;
use crate::window_system::{WindowSystem, window_system_for_display_handle};
let display = window
.display_handle()
.map_err(|e| PlatformError::Os(e.to_string()))?;
match window_system_for_display_handle(&display.as_raw()) {
WindowSystem::Wayland => WaylandHost::new(window, callbacks)
.map(|h| Rc::new(h) as Rc<dyn PlatformTitleBarHost>),
WindowSystem::X11 => {
X11Host::new(window, callbacks).map(|h| Rc::new(h) as Rc<dyn PlatformTitleBarHost>)
}
WindowSystem::Unknown => {
eprintln!(
"teksilo-platform: window reports neither an X11 nor a Wayland \
display handle; custom TitleBar disabled"
);
Err(PlatformError::Unsupported)
}
}
}
#[cfg(not(any(target_os = "windows", target_os = "macos", unix)))]
{
let _ = (window, callbacks);
Err(PlatformError::Unsupported)
}
}