use std::path::PathBuf;
#[must_use]
pub struct Window<'a> {
pub title: &'a str,
pub width: f32,
pub height: f32,
pub min_width: f32,
pub min_height: f32,
pub position: Option<(i32, i32)>,
pub screen: Option<usize>,
pub maximized: bool,
pub minimized: bool,
pub icons: &'a [(usize, &'a [u32])],
pub frameless: bool,
pub transparent: bool,
pub renderer: Renderer,
}
impl std::fmt::Debug for Window<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let mut said = f.debug_struct("Window");
said.field("title", &self.title).field("width", &self.width).field("height", &self.height).field("min_width", &self.min_width).field("min_height", &self.min_height);
said.field("position", &self.position).field("screen", &self.screen).field("maximized", &self.maximized).field("minimized", &self.minimized);
said.field("icons", &self.icons.len()).field("frameless", &self.frameless).field("transparent", &self.transparent).field("renderer", &self.renderer).finish()
}
}
impl Default for Window<'_> {
fn default() -> Self {
Window {
title: "",
width: 900.0,
height: 600.0,
min_width: 200.0,
min_height: 120.0,
position: None,
screen: None,
maximized: false,
minimized: false,
icons: &[],
frameless: false,
transparent: false,
renderer: Renderer::Auto,
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Renderer {
#[default]
Auto,
Cpu,
Gpu,
Direct3D11,
Direct3D12,
Vulkan,
OpenGL,
}
#[cfg(all(windows, feature = "gpu"))]
const BEST: [Renderer; 4] = [Renderer::Direct3D11, Renderer::Direct3D12, Renderer::Vulkan, Renderer::OpenGL];
#[cfg(all(unix, feature = "gpu"))]
#[allow(dead_code)] const BEST: [Renderer; 2] = [Renderer::OpenGL, Renderer::Vulkan];
impl Renderer {
#[cfg(feature = "gpu")]
pub(crate) fn chosen(self, said: Option<&str>) -> Renderer {
match said.map(str::to_ascii_lowercase).as_deref() {
Some("cpu") => Renderer::Cpu,
Some("gpu") => Renderer::Gpu,
Some("d3d11") => Renderer::Direct3D11,
Some("d3d12") => Renderer::Direct3D12,
Some("vulkan") => Renderer::Vulkan,
Some("opengl") => Renderer::OpenGL,
_ => self,
}
}
#[cfg(feature = "gpu")]
#[cfg_attr(unix, allow(dead_code))] pub(crate) fn tried(self) -> impl Iterator<Item = Renderer> {
let named = BEST.into_iter().filter(move |path| *path == self);
named.chain(BEST.into_iter().filter(move |path| *path != self && self != Renderer::Cpu))
}
}
#[cfg(all(unix, not(target_os = "macos"), feature = "gpu"))]
pub(crate) fn wants_gpu(asked: Renderer) -> Option<bool> {
match asked.chosen(std::env::var("TAUT_RENDERER").ok().as_deref()) {
Renderer::Cpu => None,
Renderer::Auto => Some(false),
_ => Some(true),
}
}
impl Window<'_> {
pub fn with_placement(self, place: Option<WindowPlacement>) -> Self {
let Some(WindowPlacement { position: at, width, height, maximized }) = place else { return self };
Window { position: Some(at), width, height, maximized, ..self }
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct WindowPlacement {
pub position: (i32, i32),
pub width: f32,
pub height: f32,
pub maximized: bool,
}
pub trait Clipboard {
fn get(&mut self) -> Option<String>;
fn set(&mut self, text: &str);
fn picture(&mut self) -> Option<(usize, Vec<u32>)> {
None
}
fn set_picture(&mut self, _width: usize, _pixels: &[u32]) {}
fn files(&mut self) -> Vec<PathBuf> {
Vec::new()
}
fn set_files(&mut self, _files: &[PathBuf]) {}
}
pub(crate) struct PicturesAndFiles<C> {
pub picture: TakesPicture<C>,
pub set_picture: fn(&mut C, usize, &[u32]),
pub files: fn(&mut C) -> Vec<PathBuf>,
pub set_files: fn(&mut C, &[PathBuf]),
}
type TakesPicture<C> = fn(&mut C) -> Option<(usize, Vec<u32>)>;
impl<C> Clone for PicturesAndFiles<C> {
fn clone(&self) -> Self {
*self
}
}
impl<C> Copy for PicturesAndFiles<C> {}
pub(crate) fn hand_pictures_and_files() {
#[cfg(windows)]
crate::windows::hand_pictures_and_files();
#[cfg(all(unix, not(target_os = "macos")))]
crate::x11::hand_pictures_and_files();
#[cfg(all(unix, not(target_os = "macos")))]
crate::wayland::hand_pictures_and_files();
}
pub(crate) fn hand_drops() {
#[cfg(windows)]
crate::windows::hand_drops();
#[cfg(all(unix, not(target_os = "macos")))]
crate::x11::hand_drops();
#[cfg(all(unix, not(target_os = "macos")))]
crate::wayland::hand_drops();
}
pub(crate) fn hands_out() {
#[cfg(windows)]
crate::windows::hands_out();
#[cfg(all(unix, not(target_os = "macos")))]
crate::x11::hands_out();
}
pub(crate) fn hand_file_dialog() {
#[cfg(windows)]
crate::windows::hand_file_dialog();
#[cfg(all(unix, not(target_os = "macos")))]
crate::portal::hand_file_dialog();
}
pub(crate) fn hand_desktop() {
#[cfg(windows)]
crate::windows::hand_desktop();
#[cfg(all(unix, not(target_os = "macos")))]
crate::x11::hand_desktop();
#[cfg(all(unix, not(target_os = "macos")))]
crate::wayland::hand_desktop();
}
#[derive(Default)]
pub struct LocalClipboard {
pub text: String,
pub picture: Option<(usize, Vec<u32>)>,
pub files: Vec<PathBuf>,
}
impl std::fmt::Debug for LocalClipboard {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let picture = self.picture.as_ref().map(|(width, pixels)| (*width, pixels.len() / (*width).max(1)));
f.debug_struct("LocalClipboard").field("text", &self.text).field("picture", &picture).field("files", &self.files).finish()
}
}
impl Clipboard for LocalClipboard {
fn get(&mut self) -> Option<String> {
(!self.text.is_empty()).then(|| self.text.clone())
}
fn set(&mut self, text: &str) {
*self = LocalClipboard { text: text.into(), ..Default::default() };
}
fn picture(&mut self) -> Option<(usize, Vec<u32>)> {
self.picture.clone()
}
fn set_picture(&mut self, width: usize, pixels: &[u32]) {
*self = LocalClipboard { picture: Some((width, pixels.to_vec())), ..Default::default() };
}
fn files(&mut self) -> Vec<PathBuf> {
self.files.clone()
}
fn set_files(&mut self, files: &[PathBuf]) {
*self = LocalClipboard { files: files.to_vec(), ..Default::default() };
}
}
pub fn system_text() -> Box<dyn crate::text::TextSystem> {
match SystemText::new() {
Some(text) => Box::new(text),
None => Box::new(crate::text::BuiltInFont),
}
}
#[cfg(windows)]
pub use crate::windows::{Messages, SystemClipboard};
#[cfg(all(unix, not(target_os = "macos")))]
#[cfg_attr(docsrs, doc(auto_cfg = false))]
pub use crate::wayland::run;
#[cfg(windows)]
#[cfg_attr(docsrs, doc(auto_cfg = false))]
pub use crate::windows::{SystemText, Waker, run};
#[cfg(all(unix, not(target_os = "macos")))]
#[cfg_attr(docsrs, doc(auto_cfg = false))]
pub use crate::x11::{SystemText, Waker};