window.rs

  1// todo(linux): remove
  2#![allow(unused)]
  3
  4use crate::{
  5    platform::blade::{BladeRenderer, BladeSurfaceConfig},
  6    size, Bounds, DevicePixels, ForegroundExecutor, Modifiers, Pixels, Platform, PlatformAtlas,
  7    PlatformDisplay, PlatformInput, PlatformInputHandler, PlatformWindow, Point, PromptLevel,
  8    Scene, Size, WindowAppearance, WindowBackgroundAppearance, WindowBounds, WindowOptions,
  9    WindowParams, X11Client, X11ClientState, X11ClientStatePtr,
 10};
 11
 12use blade_graphics as gpu;
 13use parking_lot::Mutex;
 14use raw_window_handle as rwh;
 15use util::ResultExt;
 16use x11rb::{
 17    connection::{Connection as _, RequestConnection as _},
 18    protocol::{
 19        render,
 20        xinput::{self, ConnectionExt as _},
 21        xproto::{
 22            self, Atom, ClientMessageEvent, ConnectionExt as _, CreateWindowAux, EventMask,
 23            TranslateCoordinatesReply,
 24        },
 25    },
 26    resource_manager::Database,
 27    wrapper::ConnectionExt as _,
 28    xcb_ffi::XCBConnection,
 29};
 30
 31use std::ops::Deref;
 32use std::rc::Weak;
 33use std::{
 34    cell::{Ref, RefCell, RefMut},
 35    collections::HashMap,
 36    ffi::c_void,
 37    iter::Zip,
 38    mem,
 39    num::NonZeroU32,
 40    ops::Div,
 41    ptr::NonNull,
 42    rc::Rc,
 43    sync::{self, Arc},
 44};
 45
 46use super::{X11Display, XINPUT_MASTER_DEVICE};
 47
 48x11rb::atom_manager! {
 49    pub XcbAtoms: AtomsCookie {
 50        UTF8_STRING,
 51        WM_PROTOCOLS,
 52        WM_DELETE_WINDOW,
 53        WM_CHANGE_STATE,
 54        _NET_WM_NAME,
 55        _NET_WM_STATE,
 56        _NET_WM_STATE_MAXIMIZED_VERT,
 57        _NET_WM_STATE_MAXIMIZED_HORZ,
 58        _NET_WM_STATE_FULLSCREEN,
 59        _NET_WM_STATE_HIDDEN,
 60        _NET_WM_STATE_FOCUSED,
 61        _NET_WM_MOVERESIZE,
 62        _GTK_SHOW_WINDOW_MENU,
 63    }
 64}
 65
 66fn query_render_extent(xcb_connection: &XCBConnection, x_window: xproto::Window) -> gpu::Extent {
 67    let reply = xcb_connection
 68        .get_geometry(x_window)
 69        .unwrap()
 70        .reply()
 71        .unwrap();
 72    gpu::Extent {
 73        width: reply.width as u32,
 74        height: reply.height as u32,
 75        depth: 1,
 76    }
 77}
 78
 79#[derive(Debug)]
 80struct Visual {
 81    id: xproto::Visualid,
 82    colormap: u32,
 83    depth: u8,
 84}
 85
 86struct VisualSet {
 87    inherit: Visual,
 88    opaque: Option<Visual>,
 89    transparent: Option<Visual>,
 90    root: u32,
 91    black_pixel: u32,
 92}
 93
 94fn find_visuals(xcb_connection: &XCBConnection, screen_index: usize) -> VisualSet {
 95    let screen = &xcb_connection.setup().roots[screen_index];
 96    let mut set = VisualSet {
 97        inherit: Visual {
 98            id: screen.root_visual,
 99            colormap: screen.default_colormap,
100            depth: screen.root_depth,
101        },
102        opaque: None,
103        transparent: None,
104        root: screen.root,
105        black_pixel: screen.black_pixel,
106    };
107
108    for depth_info in screen.allowed_depths.iter() {
109        for visual_type in depth_info.visuals.iter() {
110            let visual = Visual {
111                id: visual_type.visual_id,
112                colormap: 0,
113                depth: depth_info.depth,
114            };
115            log::debug!("Visual id: {}, class: {:?}, depth: {}, bits_per_value: {}, masks: 0x{:x} 0x{:x} 0x{:x}",
116                visual_type.visual_id,
117                visual_type.class,
118                depth_info.depth,
119                visual_type.bits_per_rgb_value,
120                visual_type.red_mask, visual_type.green_mask, visual_type.blue_mask,
121            );
122
123            if (
124                visual_type.red_mask,
125                visual_type.green_mask,
126                visual_type.blue_mask,
127            ) != (0xFF0000, 0xFF00, 0xFF)
128            {
129                continue;
130            }
131            let color_mask = visual_type.red_mask | visual_type.green_mask | visual_type.blue_mask;
132            let alpha_mask = color_mask as usize ^ ((1usize << depth_info.depth) - 1);
133
134            if alpha_mask == 0 {
135                if set.opaque.is_none() {
136                    set.opaque = Some(visual);
137                }
138            } else {
139                if set.transparent.is_none() {
140                    set.transparent = Some(visual);
141                }
142            }
143        }
144    }
145
146    set
147}
148
149struct RawWindow {
150    connection: *mut c_void,
151    screen_id: usize,
152    window_id: u32,
153    visual_id: u32,
154}
155
156#[derive(Default)]
157pub struct Callbacks {
158    request_frame: Option<Box<dyn FnMut()>>,
159    input: Option<Box<dyn FnMut(PlatformInput) -> crate::DispatchEventResult>>,
160    active_status_change: Option<Box<dyn FnMut(bool)>>,
161    resize: Option<Box<dyn FnMut(Size<Pixels>, f32)>>,
162    moved: Option<Box<dyn FnMut()>>,
163    should_close: Option<Box<dyn FnMut() -> bool>>,
164    close: Option<Box<dyn FnOnce()>>,
165    appearance_changed: Option<Box<dyn FnMut()>>,
166}
167
168pub(crate) struct X11WindowState {
169    client: X11ClientStatePtr,
170    executor: ForegroundExecutor,
171    atoms: XcbAtoms,
172    x_root_window: xproto::Window,
173    raw: RawWindow,
174    bounds: Bounds<i32>,
175    scale_factor: f32,
176    renderer: BladeRenderer,
177    display: Rc<dyn PlatformDisplay>,
178    input_handler: Option<PlatformInputHandler>,
179    appearance: WindowAppearance,
180}
181
182#[derive(Clone)]
183pub(crate) struct X11WindowStatePtr {
184    pub(crate) state: Rc<RefCell<X11WindowState>>,
185    pub(crate) callbacks: Rc<RefCell<Callbacks>>,
186    xcb_connection: Rc<XCBConnection>,
187    x_window: xproto::Window,
188}
189
190// todo(linux): Remove other RawWindowHandle implementation
191impl rwh::HasWindowHandle for RawWindow {
192    fn window_handle(&self) -> Result<rwh::WindowHandle, rwh::HandleError> {
193        let non_zero = NonZeroU32::new(self.window_id).unwrap();
194        let mut handle = rwh::XcbWindowHandle::new(non_zero);
195        handle.visual_id = NonZeroU32::new(self.visual_id);
196        Ok(unsafe { rwh::WindowHandle::borrow_raw(handle.into()) })
197    }
198}
199impl rwh::HasDisplayHandle for RawWindow {
200    fn display_handle(&self) -> Result<rwh::DisplayHandle, rwh::HandleError> {
201        let non_zero = NonNull::new(self.connection).unwrap();
202        let handle = rwh::XcbDisplayHandle::new(Some(non_zero), self.screen_id as i32);
203        Ok(unsafe { rwh::DisplayHandle::borrow_raw(handle.into()) })
204    }
205}
206
207impl rwh::HasWindowHandle for X11Window {
208    fn window_handle(&self) -> Result<rwh::WindowHandle, rwh::HandleError> {
209        unimplemented!()
210    }
211}
212impl rwh::HasDisplayHandle for X11Window {
213    fn display_handle(&self) -> Result<rwh::DisplayHandle, rwh::HandleError> {
214        unimplemented!()
215    }
216}
217
218impl X11WindowState {
219    #[allow(clippy::too_many_arguments)]
220    pub fn new(
221        client: X11ClientStatePtr,
222        executor: ForegroundExecutor,
223        params: WindowParams,
224        xcb_connection: &Rc<XCBConnection>,
225        x_main_screen_index: usize,
226        x_window: xproto::Window,
227        atoms: &XcbAtoms,
228        scale_factor: f32,
229        appearance: WindowAppearance,
230    ) -> Self {
231        let x_screen_index = params
232            .display_id
233            .map_or(x_main_screen_index, |did| did.0 as usize);
234
235        let visual_set = find_visuals(&xcb_connection, x_screen_index);
236        let visual_maybe = match params.window_background {
237            WindowBackgroundAppearance::Opaque => visual_set.opaque,
238            WindowBackgroundAppearance::Transparent | WindowBackgroundAppearance::Blurred => {
239                visual_set.transparent
240            }
241        };
242        let visual = match visual_maybe {
243            Some(visual) => visual,
244            None => {
245                log::warn!(
246                    "Unable to find a matching visual for {:?}",
247                    params.window_background
248                );
249                visual_set.inherit
250            }
251        };
252        log::info!("Using {:?}", visual);
253
254        let colormap = if visual.colormap != 0 {
255            visual.colormap
256        } else {
257            let id = xcb_connection.generate_id().unwrap();
258            log::info!("Creating colormap {}", id);
259            xcb_connection
260                .create_colormap(xproto::ColormapAlloc::NONE, id, visual_set.root, visual.id)
261                .unwrap()
262                .check()
263                .unwrap();
264            id
265        };
266
267        let win_aux = xproto::CreateWindowAux::new()
268            .background_pixel(x11rb::NONE)
269            // https://stackoverflow.com/questions/43218127/x11-xlib-xcb-creating-a-window-requires-border-pixel-if-specifying-colormap-wh
270            .border_pixel(visual_set.black_pixel)
271            .colormap(colormap)
272            .event_mask(
273                xproto::EventMask::EXPOSURE
274                    | xproto::EventMask::STRUCTURE_NOTIFY
275                    | xproto::EventMask::ENTER_WINDOW
276                    | xproto::EventMask::LEAVE_WINDOW
277                    | xproto::EventMask::FOCUS_CHANGE
278                    | xproto::EventMask::KEY_PRESS
279                    | xproto::EventMask::KEY_RELEASE,
280            );
281
282        xcb_connection
283            .create_window(
284                visual.depth,
285                x_window,
286                visual_set.root,
287                params.bounds.origin.x.0 as i16,
288                params.bounds.origin.y.0 as i16,
289                params.bounds.size.width.0 as u16,
290                params.bounds.size.height.0 as u16,
291                0,
292                xproto::WindowClass::INPUT_OUTPUT,
293                visual.id,
294                &win_aux,
295            )
296            .unwrap()
297            .check()
298            .unwrap();
299
300        if let Some(titlebar) = params.titlebar {
301            if let Some(title) = titlebar.title {
302                xcb_connection
303                    .change_property8(
304                        xproto::PropMode::REPLACE,
305                        x_window,
306                        xproto::AtomEnum::WM_NAME,
307                        xproto::AtomEnum::STRING,
308                        title.as_bytes(),
309                    )
310                    .unwrap();
311            }
312        }
313
314        xcb_connection
315            .change_property32(
316                xproto::PropMode::REPLACE,
317                x_window,
318                atoms.WM_PROTOCOLS,
319                xproto::AtomEnum::ATOM,
320                &[atoms.WM_DELETE_WINDOW],
321            )
322            .unwrap();
323
324        xcb_connection
325            .xinput_xi_select_events(
326                x_window,
327                &[xinput::EventMask {
328                    deviceid: XINPUT_MASTER_DEVICE,
329                    mask: vec![
330                        xinput::XIEventMask::MOTION
331                            | xinput::XIEventMask::BUTTON_PRESS
332                            | xinput::XIEventMask::BUTTON_RELEASE
333                            | xinput::XIEventMask::LEAVE,
334                    ],
335                }],
336            )
337            .unwrap();
338
339        xcb_connection.map_window(x_window).unwrap();
340        xcb_connection.flush().unwrap();
341
342        let raw = RawWindow {
343            connection: as_raw_xcb_connection::AsRawXcbConnection::as_raw_xcb_connection(
344                xcb_connection,
345            ) as *mut _,
346            screen_id: x_screen_index,
347            window_id: x_window,
348            visual_id: visual.id,
349        };
350        let gpu = Arc::new(
351            unsafe {
352                gpu::Context::init_windowed(
353                    &raw,
354                    gpu::ContextDesc {
355                        validation: false,
356                        capture: false,
357                        overlay: false,
358                    },
359                )
360            }
361            .unwrap(),
362        );
363
364        let config = BladeSurfaceConfig {
365            // Note: this has to be done after the GPU init, or otherwise
366            // the sizes are immediately invalidated.
367            size: query_render_extent(xcb_connection, x_window),
368            transparent: params.window_background != WindowBackgroundAppearance::Opaque,
369        };
370
371        Self {
372            client,
373            executor,
374            display: Rc::new(X11Display::new(xcb_connection, x_screen_index).unwrap()),
375            raw,
376            x_root_window: visual_set.root,
377            bounds: params.bounds.map(|v| v.0),
378            scale_factor,
379            renderer: BladeRenderer::new(gpu, config),
380            atoms: *atoms,
381            input_handler: None,
382            appearance,
383        }
384    }
385
386    fn content_size(&self) -> Size<Pixels> {
387        let size = self.renderer.viewport_size();
388        Size {
389            width: size.width.into(),
390            height: size.height.into(),
391        }
392    }
393}
394
395pub(crate) struct X11Window(pub X11WindowStatePtr);
396
397impl Drop for X11Window {
398    fn drop(&mut self) {
399        let mut state = self.0.state.borrow_mut();
400        state.renderer.destroy();
401
402        self.0.xcb_connection.unmap_window(self.0.x_window).unwrap();
403        self.0
404            .xcb_connection
405            .destroy_window(self.0.x_window)
406            .unwrap();
407        self.0.xcb_connection.flush().unwrap();
408
409        let this_ptr = self.0.clone();
410        let client_ptr = state.client.clone();
411        state
412            .executor
413            .spawn(async move {
414                this_ptr.close();
415                client_ptr.drop_window(this_ptr.x_window);
416            })
417            .detach();
418        drop(state);
419    }
420}
421
422enum WmHintPropertyState {
423    Remove = 0,
424    Add = 1,
425    Toggle = 2,
426}
427
428impl X11Window {
429    #[allow(clippy::too_many_arguments)]
430    pub fn new(
431        client: X11ClientStatePtr,
432        executor: ForegroundExecutor,
433        params: WindowParams,
434        xcb_connection: &Rc<XCBConnection>,
435        x_main_screen_index: usize,
436        x_window: xproto::Window,
437        atoms: &XcbAtoms,
438        scale_factor: f32,
439        appearance: WindowAppearance,
440    ) -> Self {
441        Self(X11WindowStatePtr {
442            state: Rc::new(RefCell::new(X11WindowState::new(
443                client,
444                executor,
445                params,
446                xcb_connection,
447                x_main_screen_index,
448                x_window,
449                atoms,
450                scale_factor,
451                appearance,
452            ))),
453            callbacks: Rc::new(RefCell::new(Callbacks::default())),
454            xcb_connection: xcb_connection.clone(),
455            x_window,
456        })
457    }
458
459    fn set_wm_hints(&self, wm_hint_property_state: WmHintPropertyState, prop1: u32, prop2: u32) {
460        let state = self.0.state.borrow();
461        let message = ClientMessageEvent::new(
462            32,
463            self.0.x_window,
464            state.atoms._NET_WM_STATE,
465            [wm_hint_property_state as u32, prop1, prop2, 1, 0],
466        );
467        self.0
468            .xcb_connection
469            .send_event(
470                false,
471                state.x_root_window,
472                EventMask::SUBSTRUCTURE_REDIRECT | EventMask::SUBSTRUCTURE_NOTIFY,
473                message,
474            )
475            .unwrap();
476    }
477
478    fn get_wm_hints(&self) -> Vec<u32> {
479        let reply = self
480            .0
481            .xcb_connection
482            .get_property(
483                false,
484                self.0.x_window,
485                self.0.state.borrow().atoms._NET_WM_STATE,
486                xproto::AtomEnum::ATOM,
487                0,
488                u32::MAX,
489            )
490            .unwrap()
491            .reply()
492            .unwrap();
493        // Reply is in u8 but atoms are represented as u32
494        reply
495            .value
496            .chunks_exact(4)
497            .map(|chunk| u32::from_ne_bytes([chunk[0], chunk[1], chunk[2], chunk[3]]))
498            .collect()
499    }
500
501    fn get_root_position(&self, position: Point<Pixels>) -> TranslateCoordinatesReply {
502        let state = self.0.state.borrow();
503        self.0
504            .xcb_connection
505            .translate_coordinates(
506                self.0.x_window,
507                state.x_root_window,
508                (position.x.0 * state.scale_factor) as i16,
509                (position.y.0 * state.scale_factor) as i16,
510            )
511            .unwrap()
512            .reply()
513            .unwrap()
514    }
515}
516
517impl X11WindowStatePtr {
518    pub fn should_close(&self) -> bool {
519        let mut cb = self.callbacks.borrow_mut();
520        if let Some(mut should_close) = cb.should_close.take() {
521            let result = (should_close)();
522            cb.should_close = Some(should_close);
523            result
524        } else {
525            true
526        }
527    }
528
529    pub fn close(&self) {
530        let mut callbacks = self.callbacks.borrow_mut();
531        if let Some(fun) = callbacks.close.take() {
532            fun()
533        }
534    }
535
536    pub fn refresh(&self) {
537        let mut cb = self.callbacks.borrow_mut();
538        if let Some(ref mut fun) = cb.request_frame {
539            fun();
540        }
541    }
542
543    pub fn handle_input(&self, input: PlatformInput) {
544        if let Some(ref mut fun) = self.callbacks.borrow_mut().input {
545            if !fun(input.clone()).propagate {
546                return;
547            }
548        }
549        if let PlatformInput::KeyDown(event) = input {
550            let mut state = self.state.borrow_mut();
551            if let Some(mut input_handler) = state.input_handler.take() {
552                if let Some(ime_key) = &event.keystroke.ime_key {
553                    drop(state);
554                    input_handler.replace_text_in_range(None, ime_key);
555                    state = self.state.borrow_mut();
556                }
557                state.input_handler = Some(input_handler);
558            }
559        }
560    }
561
562    pub fn handle_ime_commit(&self, text: String) {
563        let mut state = self.state.borrow_mut();
564        if let Some(mut input_handler) = state.input_handler.take() {
565            drop(state);
566            input_handler.replace_text_in_range(None, &text);
567            let mut state = self.state.borrow_mut();
568            state.input_handler = Some(input_handler);
569        }
570    }
571
572    pub fn handle_ime_preedit(&self, text: String) {
573        let mut state = self.state.borrow_mut();
574        if let Some(mut input_handler) = state.input_handler.take() {
575            drop(state);
576            input_handler.replace_and_mark_text_in_range(None, &text, None);
577            let mut state = self.state.borrow_mut();
578            state.input_handler = Some(input_handler);
579        }
580    }
581
582    pub fn get_ime_area(&self) -> Option<Bounds<Pixels>> {
583        let mut state = self.state.borrow_mut();
584        let mut bounds: Option<Bounds<Pixels>> = None;
585        if let Some(mut input_handler) = state.input_handler.take() {
586            drop(state);
587            if let Some(range) = input_handler.selected_text_range() {
588                bounds = input_handler.bounds_for_range(range);
589            }
590            let mut state = self.state.borrow_mut();
591            state.input_handler = Some(input_handler);
592        };
593        bounds
594    }
595
596    pub fn configure(&self, bounds: Bounds<i32>) {
597        let mut resize_args = None;
598        let do_move;
599        {
600            let mut state = self.state.borrow_mut();
601            let old_bounds = mem::replace(&mut state.bounds, bounds);
602            do_move = old_bounds.origin != bounds.origin;
603            // todo(linux): use normal GPUI types here, refactor out the double
604            // viewport check and extra casts ( )
605            let gpu_size = query_render_extent(&self.xcb_connection, self.x_window);
606            if state.renderer.viewport_size() != gpu_size {
607                state
608                    .renderer
609                    .update_drawable_size(size(gpu_size.width as f64, gpu_size.height as f64));
610                resize_args = Some((state.content_size(), state.scale_factor));
611            }
612        }
613
614        let mut callbacks = self.callbacks.borrow_mut();
615        if let Some((content_size, scale_factor)) = resize_args {
616            if let Some(ref mut fun) = callbacks.resize {
617                fun(content_size, scale_factor)
618            }
619        }
620        if do_move {
621            if let Some(ref mut fun) = callbacks.moved {
622                fun()
623            }
624        }
625    }
626
627    pub fn set_focused(&self, focus: bool) {
628        if let Some(ref mut fun) = self.callbacks.borrow_mut().active_status_change {
629            fun(focus);
630        }
631    }
632
633    pub fn set_appearance(&mut self, appearance: WindowAppearance) {
634        self.state.borrow_mut().appearance = appearance;
635
636        let mut callbacks = self.callbacks.borrow_mut();
637        if let Some(ref mut fun) = callbacks.appearance_changed {
638            (fun)()
639        }
640    }
641}
642
643impl PlatformWindow for X11Window {
644    fn bounds(&self) -> Bounds<DevicePixels> {
645        self.0.state.borrow().bounds.map(|v| v.into())
646    }
647
648    fn is_maximized(&self) -> bool {
649        let state = self.0.state.borrow();
650        let wm_hints = self.get_wm_hints();
651        // A maximized window that gets minimized will still retain its maximized state.
652        !wm_hints.contains(&state.atoms._NET_WM_STATE_HIDDEN)
653            && wm_hints.contains(&state.atoms._NET_WM_STATE_MAXIMIZED_VERT)
654            && wm_hints.contains(&state.atoms._NET_WM_STATE_MAXIMIZED_HORZ)
655    }
656
657    fn window_bounds(&self) -> WindowBounds {
658        let state = self.0.state.borrow();
659        WindowBounds::Windowed(state.bounds.map(|p| DevicePixels(p)))
660    }
661
662    fn content_size(&self) -> Size<Pixels> {
663        // We divide by the scale factor here because this value is queried to determine how much to draw,
664        // but it will be multiplied later by the scale to adjust for scaling.
665        let state = self.0.state.borrow();
666        state
667            .content_size()
668            .map(|size| size.div(state.scale_factor))
669    }
670
671    fn scale_factor(&self) -> f32 {
672        self.0.state.borrow().scale_factor
673    }
674
675    fn appearance(&self) -> WindowAppearance {
676        self.0.state.borrow().appearance
677    }
678
679    fn display(&self) -> Rc<dyn PlatformDisplay> {
680        self.0.state.borrow().display.clone()
681    }
682
683    fn mouse_position(&self) -> Point<Pixels> {
684        let reply = self
685            .0
686            .xcb_connection
687            .query_pointer(self.0.x_window)
688            .unwrap()
689            .reply()
690            .unwrap();
691        Point::new((reply.root_x as u32).into(), (reply.root_y as u32).into())
692    }
693
694    // todo(linux)
695    fn modifiers(&self) -> Modifiers {
696        Modifiers::default()
697    }
698
699    fn set_input_handler(&mut self, input_handler: PlatformInputHandler) {
700        self.0.state.borrow_mut().input_handler = Some(input_handler);
701    }
702
703    fn take_input_handler(&mut self) -> Option<PlatformInputHandler> {
704        self.0.state.borrow_mut().input_handler.take()
705    }
706
707    fn prompt(
708        &self,
709        _level: PromptLevel,
710        _msg: &str,
711        _detail: Option<&str>,
712        _answers: &[&str],
713    ) -> Option<futures::channel::oneshot::Receiver<usize>> {
714        None
715    }
716
717    fn activate(&self) {
718        let win_aux = xproto::ConfigureWindowAux::new().stack_mode(xproto::StackMode::ABOVE);
719        self.0
720            .xcb_connection
721            .configure_window(self.0.x_window, &win_aux)
722            .log_err();
723    }
724
725    fn is_active(&self) -> bool {
726        let state = self.0.state.borrow();
727        self.get_wm_hints()
728            .contains(&state.atoms._NET_WM_STATE_FOCUSED)
729    }
730
731    fn set_title(&mut self, title: &str) {
732        self.0
733            .xcb_connection
734            .change_property8(
735                xproto::PropMode::REPLACE,
736                self.0.x_window,
737                xproto::AtomEnum::WM_NAME,
738                xproto::AtomEnum::STRING,
739                title.as_bytes(),
740            )
741            .unwrap();
742
743        self.0
744            .xcb_connection
745            .change_property8(
746                xproto::PropMode::REPLACE,
747                self.0.x_window,
748                self.0.state.borrow().atoms._NET_WM_NAME,
749                self.0.state.borrow().atoms.UTF8_STRING,
750                title.as_bytes(),
751            )
752            .unwrap();
753    }
754
755    fn set_app_id(&mut self, app_id: &str) {
756        let mut data = Vec::with_capacity(app_id.len() * 2 + 1);
757        data.extend(app_id.bytes()); // instance https://unix.stackexchange.com/a/494170
758        data.push(b'\0');
759        data.extend(app_id.bytes()); // class
760
761        self.0
762            .xcb_connection
763            .change_property8(
764                xproto::PropMode::REPLACE,
765                self.0.x_window,
766                xproto::AtomEnum::WM_CLASS,
767                xproto::AtomEnum::STRING,
768                &data,
769            )
770            .unwrap();
771    }
772
773    // todo(linux)
774    fn set_edited(&mut self, edited: bool) {}
775
776    fn set_background_appearance(&mut self, background_appearance: WindowBackgroundAppearance) {
777        let mut inner = self.0.state.borrow_mut();
778        let transparent = background_appearance != WindowBackgroundAppearance::Opaque;
779        inner.renderer.update_transparency(transparent);
780    }
781
782    // todo(linux), this corresponds to `orderFrontCharacterPalette` on macOS,
783    // but it looks like the equivalent for Linux is GTK specific:
784    //
785    // https://docs.gtk.org/gtk3/signal.Entry.insert-emoji.html
786    //
787    // This API might need to change, or we might need to build an emoji picker into GPUI
788    fn show_character_palette(&self) {
789        unimplemented!()
790    }
791
792    fn minimize(&self) {
793        let state = self.0.state.borrow();
794        const WINDOW_ICONIC_STATE: u32 = 3;
795        let message = ClientMessageEvent::new(
796            32,
797            self.0.x_window,
798            state.atoms.WM_CHANGE_STATE,
799            [WINDOW_ICONIC_STATE, 0, 0, 0, 0],
800        );
801        self.0
802            .xcb_connection
803            .send_event(
804                false,
805                state.x_root_window,
806                EventMask::SUBSTRUCTURE_REDIRECT | EventMask::SUBSTRUCTURE_NOTIFY,
807                message,
808            )
809            .unwrap();
810    }
811
812    fn zoom(&self) {
813        let state = self.0.state.borrow();
814        self.set_wm_hints(
815            WmHintPropertyState::Toggle,
816            state.atoms._NET_WM_STATE_MAXIMIZED_VERT,
817            state.atoms._NET_WM_STATE_MAXIMIZED_HORZ,
818        );
819    }
820
821    fn toggle_fullscreen(&self) {
822        let state = self.0.state.borrow();
823        self.set_wm_hints(
824            WmHintPropertyState::Toggle,
825            state.atoms._NET_WM_STATE_FULLSCREEN,
826            xproto::AtomEnum::NONE.into(),
827        );
828    }
829
830    fn is_fullscreen(&self) -> bool {
831        let state = self.0.state.borrow();
832        self.get_wm_hints()
833            .contains(&state.atoms._NET_WM_STATE_FULLSCREEN)
834    }
835
836    fn on_request_frame(&self, callback: Box<dyn FnMut()>) {
837        self.0.callbacks.borrow_mut().request_frame = Some(callback);
838    }
839
840    fn on_input(&self, callback: Box<dyn FnMut(PlatformInput) -> crate::DispatchEventResult>) {
841        self.0.callbacks.borrow_mut().input = Some(callback);
842    }
843
844    fn on_active_status_change(&self, callback: Box<dyn FnMut(bool)>) {
845        self.0.callbacks.borrow_mut().active_status_change = Some(callback);
846    }
847
848    fn on_resize(&self, callback: Box<dyn FnMut(Size<Pixels>, f32)>) {
849        self.0.callbacks.borrow_mut().resize = Some(callback);
850    }
851
852    fn on_moved(&self, callback: Box<dyn FnMut()>) {
853        self.0.callbacks.borrow_mut().moved = Some(callback);
854    }
855
856    fn on_should_close(&self, callback: Box<dyn FnMut() -> bool>) {
857        self.0.callbacks.borrow_mut().should_close = Some(callback);
858    }
859
860    fn on_close(&self, callback: Box<dyn FnOnce()>) {
861        self.0.callbacks.borrow_mut().close = Some(callback);
862    }
863
864    fn on_appearance_changed(&self, callback: Box<dyn FnMut()>) {
865        self.0.callbacks.borrow_mut().appearance_changed = Some(callback);
866    }
867
868    fn draw(&self, scene: &Scene) {
869        let mut inner = self.0.state.borrow_mut();
870        inner.renderer.draw(scene);
871    }
872
873    fn sprite_atlas(&self) -> sync::Arc<dyn PlatformAtlas> {
874        let inner = self.0.state.borrow();
875        inner.renderer.sprite_atlas().clone()
876    }
877
878    fn show_window_menu(&self, position: Point<Pixels>) {
879        let state = self.0.state.borrow();
880        let coords = self.get_root_position(position);
881        let message = ClientMessageEvent::new(
882            32,
883            self.0.x_window,
884            state.atoms._GTK_SHOW_WINDOW_MENU,
885            [
886                XINPUT_MASTER_DEVICE as u32,
887                coords.dst_x as u32,
888                coords.dst_y as u32,
889                0,
890                0,
891            ],
892        );
893        self.0
894            .xcb_connection
895            .send_event(
896                false,
897                state.x_root_window,
898                EventMask::SUBSTRUCTURE_REDIRECT | EventMask::SUBSTRUCTURE_NOTIFY,
899                message,
900            )
901            .unwrap();
902    }
903
904    fn start_system_move(&self) {
905        let state = self.0.state.borrow();
906        let pointer = self
907            .0
908            .xcb_connection
909            .query_pointer(self.0.x_window)
910            .unwrap()
911            .reply()
912            .unwrap();
913        const MOVERESIZE_MOVE: u32 = 8;
914        let message = ClientMessageEvent::new(
915            32,
916            self.0.x_window,
917            state.atoms._NET_WM_MOVERESIZE,
918            [
919                pointer.root_x as u32,
920                pointer.root_y as u32,
921                MOVERESIZE_MOVE,
922                1, // Left mouse button
923                1,
924            ],
925        );
926        self.0
927            .xcb_connection
928            .send_event(
929                false,
930                state.x_root_window,
931                EventMask::SUBSTRUCTURE_REDIRECT | EventMask::SUBSTRUCTURE_NOTIFY,
932                message,
933            )
934            .unwrap();
935    }
936
937    fn should_render_window_controls(&self) -> bool {
938        false
939    }
940}