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