1#![allow(unused)]
2
3use std::{
4 path::{Path, PathBuf},
5 rc::Rc,
6 sync::Arc,
7 time::Duration,
8};
9
10use async_task::Runnable;
11use futures::channel::oneshot;
12use parking_lot::Mutex;
13use time::UtcOffset;
14
15use collections::{HashMap, HashSet};
16
17use crate::platform::linux::client::Client;
18use crate::platform::linux::client_dispatcher::ClientDispatcher;
19use crate::platform::{X11Client, X11ClientDispatcher, XcbAtoms};
20use crate::{
21 Action, AnyWindowHandle, BackgroundExecutor, Bounds, ClipboardItem, CursorStyle, DisplayId,
22 ForegroundExecutor, Keymap, LinuxDispatcher, LinuxTextSystem, Menu, PathPromptOptions,
23 Platform, PlatformDisplay, PlatformInput, PlatformTextSystem, PlatformWindow, Point, Result,
24 SemanticVersion, Size, Task, WindowAppearance, WindowOptions, X11Display, X11Window,
25 X11WindowState,
26};
27
28#[derive(Default)]
29pub(crate) struct Callbacks {
30 open_urls: Option<Box<dyn FnMut(Vec<String>)>>,
31 become_active: Option<Box<dyn FnMut()>>,
32 resign_active: Option<Box<dyn FnMut()>>,
33 pub(crate) quit: Option<Box<dyn FnMut()>>,
34 reopen: Option<Box<dyn FnMut()>>,
35 event: Option<Box<dyn FnMut(PlatformInput) -> bool>>,
36 app_menu_action: Option<Box<dyn FnMut(&dyn Action)>>,
37 will_open_app_menu: Option<Box<dyn FnMut()>>,
38 validate_app_menu_command: Option<Box<dyn FnMut(&dyn Action) -> bool>>,
39}
40
41pub(crate) struct LinuxPlatformInner {
42 pub(crate) background_executor: BackgroundExecutor,
43 pub(crate) foreground_executor: ForegroundExecutor,
44 pub(crate) main_receiver: flume::Receiver<Runnable>,
45 pub(crate) text_system: Arc<LinuxTextSystem>,
46 pub(crate) callbacks: Mutex<Callbacks>,
47 pub(crate) state: Mutex<LinuxPlatformState>,
48}
49
50pub(crate) struct LinuxPlatform {
51 client: Arc<dyn Client>,
52 inner: Arc<LinuxPlatformInner>,
53}
54
55pub(crate) struct LinuxPlatformState {
56 pub(crate) quit_requested: bool,
57}
58
59impl Default for LinuxPlatform {
60 fn default() -> Self {
61 Self::new()
62 }
63}
64
65impl LinuxPlatform {
66 pub(crate) fn new() -> Self {
67 let (xcb_connection, x_root_index) =
68 xcb::Connection::connect_with_extensions(None, &[xcb::Extension::Present], &[])
69 .unwrap();
70 let atoms = XcbAtoms::intern_all(&xcb_connection).unwrap();
71
72 let xcb_connection = Arc::new(xcb_connection);
73 let (main_sender, main_receiver) = flume::unbounded::<Runnable>();
74 let client_dispatcher: Arc<dyn ClientDispatcher + Send + Sync> =
75 Arc::new(X11ClientDispatcher::new(&xcb_connection, x_root_index));
76 let dispatcher = LinuxDispatcher::new(main_sender, &client_dispatcher);
77 let dispatcher = Arc::new(dispatcher);
78
79 let inner = LinuxPlatformInner {
80 background_executor: BackgroundExecutor::new(dispatcher.clone()),
81 foreground_executor: ForegroundExecutor::new(dispatcher.clone()),
82 main_receiver,
83 text_system: Arc::new(LinuxTextSystem::new()),
84 callbacks: Mutex::new(Callbacks::default()),
85 state: Mutex::new(LinuxPlatformState {
86 quit_requested: false,
87 }),
88 };
89 let inner = Arc::new(inner);
90
91 let x11client = X11Client::new(Arc::clone(&inner), xcb_connection, x_root_index, atoms);
92 let x11client = Arc::new(x11client);
93
94 Self {
95 client: x11client,
96 inner: Arc::clone(&inner),
97 }
98 }
99}
100
101impl Platform for LinuxPlatform {
102 fn background_executor(&self) -> BackgroundExecutor {
103 self.inner.background_executor.clone()
104 }
105
106 fn foreground_executor(&self) -> ForegroundExecutor {
107 self.inner.foreground_executor.clone()
108 }
109
110 fn text_system(&self) -> Arc<dyn PlatformTextSystem> {
111 self.inner.text_system.clone()
112 }
113
114 fn run(&self, on_finish_launching: Box<dyn FnOnce()>) {
115 self.client.run(on_finish_launching)
116 }
117
118 fn quit(&self) {
119 self.inner.state.lock().quit_requested = true;
120 }
121
122 //todo!(linux)
123 fn restart(&self) {}
124
125 //todo!(linux)
126 fn activate(&self, ignoring_other_apps: bool) {}
127
128 //todo!(linux)
129 fn hide(&self) {}
130
131 //todo!(linux)
132 fn hide_other_apps(&self) {}
133
134 //todo!(linux)
135 fn unhide_other_apps(&self) {}
136
137 fn displays(&self) -> Vec<Rc<dyn PlatformDisplay>> {
138 self.client.displays()
139 }
140
141 fn display(&self, id: DisplayId) -> Option<Rc<dyn PlatformDisplay>> {
142 self.client.display(id)
143 }
144
145 //todo!(linux)
146 fn active_window(&self) -> Option<AnyWindowHandle> {
147 None
148 }
149
150 fn open_window(
151 &self,
152 handle: AnyWindowHandle,
153 options: WindowOptions,
154 ) -> Box<dyn PlatformWindow> {
155 self.client.open_window(handle, options)
156 }
157
158 fn open_url(&self, url: &str) {
159 unimplemented!()
160 }
161
162 fn on_open_urls(&self, callback: Box<dyn FnMut(Vec<String>)>) {
163 self.inner.callbacks.lock().open_urls = Some(callback);
164 }
165
166 fn prompt_for_paths(
167 &self,
168 options: PathPromptOptions,
169 ) -> oneshot::Receiver<Option<Vec<PathBuf>>> {
170 unimplemented!()
171 }
172
173 fn prompt_for_new_path(&self, directory: &Path) -> oneshot::Receiver<Option<PathBuf>> {
174 unimplemented!()
175 }
176
177 fn reveal_path(&self, path: &Path) {
178 unimplemented!()
179 }
180
181 fn on_become_active(&self, callback: Box<dyn FnMut()>) {
182 self.inner.callbacks.lock().become_active = Some(callback);
183 }
184
185 fn on_resign_active(&self, callback: Box<dyn FnMut()>) {
186 self.inner.callbacks.lock().resign_active = Some(callback);
187 }
188
189 fn on_quit(&self, callback: Box<dyn FnMut()>) {
190 self.inner.callbacks.lock().quit = Some(callback);
191 }
192
193 fn on_reopen(&self, callback: Box<dyn FnMut()>) {
194 self.inner.callbacks.lock().reopen = Some(callback);
195 }
196
197 fn on_event(&self, callback: Box<dyn FnMut(PlatformInput) -> bool>) {
198 self.inner.callbacks.lock().event = Some(callback);
199 }
200
201 fn on_app_menu_action(&self, callback: Box<dyn FnMut(&dyn Action)>) {
202 self.inner.callbacks.lock().app_menu_action = Some(callback);
203 }
204
205 fn on_will_open_app_menu(&self, callback: Box<dyn FnMut()>) {
206 self.inner.callbacks.lock().will_open_app_menu = Some(callback);
207 }
208
209 fn on_validate_app_menu_command(&self, callback: Box<dyn FnMut(&dyn Action) -> bool>) {
210 self.inner.callbacks.lock().validate_app_menu_command = Some(callback);
211 }
212
213 fn os_name(&self) -> &'static str {
214 "Linux"
215 }
216
217 fn double_click_interval(&self) -> Duration {
218 Duration::default()
219 }
220
221 fn os_version(&self) -> Result<SemanticVersion> {
222 Ok(SemanticVersion {
223 major: 1,
224 minor: 0,
225 patch: 0,
226 })
227 }
228
229 fn app_version(&self) -> Result<SemanticVersion> {
230 Ok(SemanticVersion {
231 major: 1,
232 minor: 0,
233 patch: 0,
234 })
235 }
236
237 fn app_path(&self) -> Result<PathBuf> {
238 unimplemented!()
239 }
240
241 //todo!(linux)
242 fn set_menus(&self, menus: Vec<Menu>, keymap: &Keymap) {}
243
244 fn local_timezone(&self) -> UtcOffset {
245 UtcOffset::UTC
246 }
247
248 fn path_for_auxiliary_executable(&self, name: &str) -> Result<PathBuf> {
249 unimplemented!()
250 }
251
252 //todo!(linux)
253 fn set_cursor_style(&self, style: CursorStyle) {}
254
255 //todo!(linux)
256 fn should_auto_hide_scrollbars(&self) -> bool {
257 false
258 }
259
260 //todo!(linux)
261 fn write_to_clipboard(&self, item: ClipboardItem) {}
262
263 //todo!(linux)
264 fn read_from_clipboard(&self) -> Option<ClipboardItem> {
265 None
266 }
267
268 fn write_credentials(&self, url: &str, username: &str, password: &[u8]) -> Task<Result<()>> {
269 unimplemented!()
270 }
271
272 fn read_credentials(&self, url: &str) -> Task<Result<Option<(String, Vec<u8>)>>> {
273 unimplemented!()
274 }
275
276 fn delete_credentials(&self, url: &str) -> Task<Result<()>> {
277 unimplemented!()
278 }
279
280 fn window_appearance(&self) -> crate::WindowAppearance {
281 crate::WindowAppearance::Light
282 }
283}
284
285#[cfg(test)]
286mod tests {
287 use super::*;
288
289 fn build_platform() -> LinuxPlatform {
290 let platform = LinuxPlatform::new();
291 platform
292 }
293}