use std::cell::RefCell;
use std::rc::Rc;
use std::sync::Arc;
use uuid::Uuid;
use gpui::{
Bounds, Capslock, DispatchEventResult, DisplayId, GpuSpecs, HeadlessAtlas, Modifiers, Pixels,
PlatformAtlas, PlatformDisplay, PlatformInput, PlatformInputHandler, PlatformWindow, Point,
PromptButton, PromptLevel, RequestFrameOptions, Scene, Size, WindowAppearance,
WindowBackgroundAppearance, WindowBounds, WindowControlArea, WindowParams, WindowVisibility,
px,
};
#[derive(Debug)]
pub(crate) struct HeadlessDisplay {
bounds: Bounds<Pixels>,
}
impl HeadlessDisplay {
pub(crate) fn new() -> Self {
Self {
bounds: Bounds::from_corners(Point::default(), Point::new(px(1920.), px(1080.))),
}
}
}
impl PlatformDisplay for HeadlessDisplay {
fn id(&self) -> DisplayId {
DisplayId::new(0)
}
fn uuid(&self) -> anyhow::Result<Uuid> {
Ok(Uuid::nil())
}
fn bounds(&self) -> Bounds<Pixels> {
self.bounds
}
}
struct HeadlessWindowState {
bounds: Bounds<Pixels>,
display: Rc<dyn PlatformDisplay>,
input_handler: Option<PlatformInputHandler>,
title: Option<String>,
is_fullscreen: bool,
}
pub(crate) struct HeadlessWindow(Rc<RefCell<HeadlessWindowState>>);
impl raw_window_handle::HasWindowHandle for HeadlessWindow {
fn window_handle(
&self,
) -> Result<raw_window_handle::WindowHandle<'_>, raw_window_handle::HandleError> {
Err(raw_window_handle::HandleError::NotSupported)
}
}
impl raw_window_handle::HasDisplayHandle for HeadlessWindow {
fn display_handle(
&self,
) -> Result<raw_window_handle::DisplayHandle<'_>, raw_window_handle::HandleError> {
Err(raw_window_handle::HandleError::NotSupported)
}
}
impl HeadlessWindow {
pub(crate) fn new(params: WindowParams, display: Rc<dyn PlatformDisplay>) -> Self {
Self(Rc::new(RefCell::new(HeadlessWindowState {
bounds: params.bounds,
display,
input_handler: None,
title: None,
is_fullscreen: false,
})))
}
}
impl PlatformWindow for HeadlessWindow {
fn bounds(&self) -> Bounds<Pixels> {
self.0.borrow().bounds
}
fn is_maximized(&self) -> bool {
false
}
fn window_bounds(&self) -> WindowBounds {
WindowBounds::Windowed(self.bounds())
}
fn content_size(&self) -> Size<Pixels> {
self.bounds().size
}
fn resize(&mut self, size: Size<Pixels>) {
self.0.borrow_mut().bounds.size = size;
}
fn scale_factor(&self) -> f32 {
1.0
}
fn appearance(&self) -> WindowAppearance {
WindowAppearance::Dark
}
fn display(&self) -> Option<Rc<dyn PlatformDisplay>> {
Some(self.0.borrow().display.clone())
}
fn mouse_position(&self) -> Point<Pixels> {
Point::default()
}
fn modifiers(&self) -> Modifiers {
Modifiers::default()
}
fn capslock(&self) -> Capslock {
Capslock::default()
}
fn set_input_handler(&mut self, input_handler: PlatformInputHandler) {
self.0.borrow_mut().input_handler = Some(input_handler);
}
fn take_input_handler(&mut self) -> Option<PlatformInputHandler> {
self.0.borrow_mut().input_handler.take()
}
fn prompt(
&self,
_level: PromptLevel,
_msg: &str,
_detail: Option<&str>,
_answers: &[PromptButton],
) -> Option<futures::channel::oneshot::Receiver<usize>> {
None
}
fn activate(&self) {}
fn is_active(&self) -> bool {
false
}
fn visibility(&self) -> WindowVisibility {
WindowVisibility::Hidden
}
fn is_hovered(&self) -> bool {
false
}
fn background_appearance(&self) -> WindowBackgroundAppearance {
WindowBackgroundAppearance::Opaque
}
fn set_title(&mut self, title: &str) {
self.0.borrow_mut().title = Some(title.to_owned());
}
fn get_title(&self) -> String {
self.0.borrow().title.clone().unwrap_or_default()
}
fn set_background_appearance(&self, _background: WindowBackgroundAppearance) {}
fn minimize(&self) {}
fn zoom(&self) {}
fn toggle_fullscreen(&self) {
let mut state = self.0.borrow_mut();
state.is_fullscreen = !state.is_fullscreen;
}
fn is_fullscreen(&self) -> bool {
self.0.borrow().is_fullscreen
}
fn on_request_frame(&self, _callback: Box<dyn FnMut(RequestFrameOptions)>) {}
fn on_input(&self, _callback: Box<dyn FnMut(PlatformInput) -> DispatchEventResult>) {}
fn on_active_status_change(&self, _callback: Box<dyn FnMut(bool)>) {}
fn on_visibility_change(&self, _callback: Box<dyn FnMut(WindowVisibility)>) {}
fn on_hover_status_change(&self, _callback: Box<dyn FnMut(bool)>) {}
fn on_resize(&self, _callback: Box<dyn FnMut(Size<Pixels>, f32)>) {}
fn on_moved(&self, _callback: Box<dyn FnMut()>) {}
fn on_should_close(&self, _callback: Box<dyn FnMut() -> bool>) {}
fn on_close(&self, _callback: Box<dyn FnOnce()>) {}
fn on_hit_test_window_control(&self, _callback: Box<dyn FnMut() -> Option<WindowControlArea>>) {
}
fn on_appearance_changed(&self, _callback: Box<dyn FnMut()>) {}
fn draw(&self, _scene: &Scene) {}
fn sprite_atlas(&self) -> Arc<dyn PlatformAtlas> {
Arc::new(HeadlessAtlas::default())
}
fn is_subpixel_rendering_supported(&self) -> bool {
false
}
fn update_ime_position(&self, _bounds: Bounds<Pixels>) {}
fn gpu_specs(&self) -> Option<GpuSpecs> {
None
}
}