replace explicit Result types with `WmResult` alias in `window_manager`
Browse files
+24 −32 src/window_manager.rs
| @@ -36,7 +36,6 @@ pub struct WindowManager { | |||
| 36 | 36 | ||
| 37 | // TODO: Eliminate all library level `anyhow::Error` | 37 | // TODO: Eliminate all library level `anyhow::Error` |
| 38 | #[derive(Debug)] | 38 | #[derive(Debug)] |
| 39 | -#[non_exhaustive] | ||
| 40 | pub enum WmError { | 39 | pub enum WmError { |
| 41 | X11(X11Error), | 40 | X11(X11Error), |
| 42 | Io(std::io::Error), | 41 | Io(std::io::Error), |
| @@ -44,7 +43,6 @@ pub enum WmError { | |||
| 44 | } | 43 | } |
| 45 | 44 | ||
| 46 | #[derive(Debug)] | 45 | #[derive(Debug)] |
| 47 | -#[non_exhaustive] | ||
| 48 | pub enum X11Error { | 46 | pub enum X11Error { |
| 49 | ConnectError(x11rb::errors::ConnectError), | 47 | ConnectError(x11rb::errors::ConnectError), |
| 50 | ConnectionError(x11rb::errors::ConnectionError), | 48 | ConnectionError(x11rb::errors::ConnectionError), |
| @@ -52,6 +50,8 @@ pub enum X11Error { | |||
| 52 | ReplyOrIdError(x11rb::errors::ReplyOrIdError), | 50 | ReplyOrIdError(x11rb::errors::ReplyOrIdError), |
| 53 | } | 51 | } |
| 54 | 52 | ||
| 53 | +type WmResult<T> = Result<T, WmError>; | ||
| 54 | + | ||
| 55 | impl std::fmt::Display for WmError { | 55 | impl std::fmt::Display for WmError { |
| 56 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | 56 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 57 | match self { | 57 | match self { |
| @@ -113,7 +113,7 @@ impl From<anyhow::Error> for WmError { | |||
| 113 | } | 113 | } |
| 114 | 114 | ||
| 115 | impl WindowManager { | 115 | impl WindowManager { |
| 116 | - pub fn new(config: Config) -> std::result::Result<Self, WmError> { | 116 | + pub fn new(config: Config) -> WmResult<Self> { |
| 117 | let (connection, screen_number) = x11rb::connect(None)?; | 117 | let (connection, screen_number) = x11rb::connect(None)?; |
| 118 | let root = connection.setup().roots[screen_number].root; | 118 | let root = connection.setup().roots[screen_number].root; |
| 119 | let screen = connection.setup().roots[screen_number].clone(); | 119 | let screen = connection.setup().roots[screen_number].clone(); |
| @@ -200,7 +200,7 @@ impl WindowManager { | |||
| 200 | connection: &RustConnection, | 200 | connection: &RustConnection, |
| 201 | root: Window, | 201 | root: Window, |
| 202 | tag_count: usize, | 202 | tag_count: usize, |
| 203 | - ) -> std::result::Result<TagMask, WmError> { | 203 | + ) -> WmResult<TagMask> { |
| 204 | let net_current_desktop = connection | 204 | let net_current_desktop = connection |
| 205 | .intern_atom(false, b"_NET_CURRENT_DESKTOP")? | 205 | .intern_atom(false, b"_NET_CURRENT_DESKTOP")? |
| 206 | .reply()? | 206 | .reply()? |
| @@ -230,7 +230,7 @@ impl WindowManager { | |||
| 230 | Ok(tag_mask(0)) | 230 | Ok(tag_mask(0)) |
| 231 | } | 231 | } |
| 232 | 232 | ||
| 233 | - fn scan_existing_windows(&mut self) -> std::result::Result<(), WmError> { | 233 | + fn scan_existing_windows(&mut self) -> WmResult<()> { |
| 234 | let tree = self.connection.query_tree(self.root)?.reply()?; | 234 | let tree = self.connection.query_tree(self.root)?.reply()?; |
| 235 | let net_client_info = self | 235 | let net_client_info = self |
| 236 | .connection | 236 | .connection |
| @@ -297,11 +297,7 @@ impl WindowManager { | |||
| 297 | Ok(()) | 297 | Ok(()) |
| 298 | } | 298 | } |
| 299 | 299 | ||
| 300 | - fn get_saved_tag( | 300 | + fn get_saved_tag(&self, window: Window, net_client_info: Atom) -> WmResult<TagMask> { |
| 301 | - &self, | ||
| 302 | - window: Window, | ||
| 303 | - net_client_info: Atom, | ||
| 304 | - ) -> std::result::Result<TagMask, WmError> { | ||
| 305 | match self | 301 | match self |
| 306 | .connection | 302 | .connection |
| 307 | .get_property(false, window, net_client_info, AtomEnum::CARDINAL, 0, 2)? | 303 | .get_property(false, window, net_client_info, AtomEnum::CARDINAL, 0, 2)? |
| @@ -328,7 +324,7 @@ impl WindowManager { | |||
| 328 | Ok(self.selected_tags) | 324 | Ok(self.selected_tags) |
| 329 | } | 325 | } |
| 330 | 326 | ||
| 331 | - fn save_client_tag(&self, window: Window, tag: TagMask) -> std::result::Result<(), WmError> { | 327 | + fn save_client_tag(&self, window: Window, tag: TagMask) -> WmResult<()> { |
| 332 | let net_client_info = self | 328 | let net_client_info = self |
| 333 | .connection | 329 | .connection |
| 334 | .intern_atom(false, b"_NET_CLIENT_INFO")? | 330 | .intern_atom(false, b"_NET_CLIENT_INFO")? |
| @@ -351,7 +347,7 @@ impl WindowManager { | |||
| 351 | Ok(()) | 347 | Ok(()) |
| 352 | } | 348 | } |
| 353 | 349 | ||
| 354 | - fn set_wm_state(&self, window: Window, state: u32) -> std::result::Result<(), WmError> { | 350 | + fn set_wm_state(&self, window: Window, state: u32) -> WmResult<()> { |
| 355 | let wm_state_atom = self | 351 | let wm_state_atom = self |
| 356 | .connection | 352 | .connection |
| 357 | .intern_atom(false, b"WM_STATE")? | 353 | .intern_atom(false, b"WM_STATE")? |
| @@ -375,7 +371,7 @@ impl WindowManager { | |||
| 375 | Ok(()) | 371 | Ok(()) |
| 376 | } | 372 | } |
| 377 | 373 | ||
| 378 | - pub fn run(&mut self) -> std::result::Result<bool, WmError> { | 374 | + pub fn run(&mut self) -> WmResult<bool> { |
| 379 | println!("oxwm started on display {}", self.screen_number); | 375 | println!("oxwm started on display {}", self.screen_number); |
| 380 | 376 | ||
| 381 | // TODO: Identify errors | 377 | // TODO: Identify errors |
| @@ -400,7 +396,7 @@ impl WindowManager { | |||
| 400 | } | 396 | } |
| 401 | } | 397 | } |
| 402 | 398 | ||
| 403 | - fn toggle_fullscreen(&mut self) -> std::result::Result<(), WmError> { | 399 | + fn toggle_fullscreen(&mut self) -> WmResult<()> { |
| 404 | if let Some(focused) = self.focused_window { | 400 | if let Some(focused) = self.focused_window { |
| 405 | if self.fullscreen_window == Some(focused) { | 401 | if self.fullscreen_window == Some(focused) { |
| 406 | self.fullscreen_window = None; | 402 | self.fullscreen_window = None; |
| @@ -434,7 +430,7 @@ impl WindowManager { | |||
| 434 | Ok(()) | 430 | Ok(()) |
| 435 | } | 431 | } |
| 436 | 432 | ||
| 437 | - fn update_bar(&mut self) -> std::result::Result<(), WmError> { | 433 | + fn update_bar(&mut self) -> WmResult<()> { |
| 438 | let mut occupied_tags: TagMask = 0; | 434 | let mut occupied_tags: TagMask = 0; |
| 439 | for &tags in self.window_tags.values() { | 435 | for &tags in self.window_tags.values() { |
| 440 | occupied_tags |= tags; | 436 | occupied_tags |= tags; |
| @@ -447,11 +443,7 @@ impl WindowManager { | |||
| 447 | Ok(()) | 443 | Ok(()) |
| 448 | } | 444 | } |
| 449 | 445 | ||
| 450 | - fn handle_key_action( | 446 | + fn handle_key_action(&mut self, action: KeyAction, arg: &Arg) -> WmResult<()> { |
| 451 | - &mut self, | ||
| 452 | - action: KeyAction, | ||
| 453 | - arg: &Arg, | ||
| 454 | - ) -> std::result::Result<(), WmError> { | ||
| 455 | match action { | 447 | match action { |
| 456 | KeyAction::Spawn => handlers::handle_spawn_action(action, arg)?, | 448 | KeyAction::Spawn => handlers::handle_spawn_action(action, arg)?, |
| 457 | KeyAction::KillClient => { | 449 | KeyAction::KillClient => { |
| @@ -521,7 +513,7 @@ impl WindowManager { | |||
| 521 | .collect() | 513 | .collect() |
| 522 | } | 514 | } |
| 523 | 515 | ||
| 524 | - fn update_window_visibility(&self) -> std::result::Result<(), WmError> { | 516 | + fn update_window_visibility(&self) -> WmResult<()> { |
| 525 | for &window in &self.windows { | 517 | for &window in &self.windows { |
| 526 | if self.is_window_visible(window) { | 518 | if self.is_window_visible(window) { |
| 527 | self.connection.map_window(window)?; | 519 | self.connection.map_window(window)?; |
| @@ -533,7 +525,7 @@ impl WindowManager { | |||
| 533 | Ok(()) | 525 | Ok(()) |
| 534 | } | 526 | } |
| 535 | 527 | ||
| 536 | - pub fn view_tag(&mut self, tag_index: usize) -> std::result::Result<(), WmError> { | 528 | + pub fn view_tag(&mut self, tag_index: usize) -> WmResult<()> { |
| 537 | if tag_index >= self.config.tags.len() { | 529 | if tag_index >= self.config.tags.len() { |
| 538 | return Ok(()); | 530 | return Ok(()); |
| 539 | } | 531 | } |
| @@ -557,7 +549,7 @@ impl WindowManager { | |||
| 557 | Ok(()) | 549 | Ok(()) |
| 558 | } | 550 | } |
| 559 | 551 | ||
| 560 | - fn save_selected_tags(&self) -> std::result::Result<(), WmError> { | 552 | + fn save_selected_tags(&self) -> WmResult<()> { |
| 561 | let net_current_desktop = self | 553 | let net_current_desktop = self |
| 562 | .connection | 554 | .connection |
| 563 | .intern_atom(false, b"_NET_CURRENT_DESKTOP")? | 555 | .intern_atom(false, b"_NET_CURRENT_DESKTOP")? |
| @@ -581,7 +573,7 @@ impl WindowManager { | |||
| 581 | Ok(()) | 573 | Ok(()) |
| 582 | } | 574 | } |
| 583 | 575 | ||
| 584 | - pub fn move_to_tag(&mut self, tag_index: usize) -> std::result::Result<(), WmError> { | 576 | + pub fn move_to_tag(&mut self, tag_index: usize) -> WmResult<()> { |
| 585 | if tag_index >= self.config.tags.len() { | 577 | if tag_index >= self.config.tags.len() { |
| 586 | return Ok(()); | 578 | return Ok(()); |
| 587 | } | 579 | } |
| @@ -600,7 +592,7 @@ impl WindowManager { | |||
| 600 | Ok(()) | 592 | Ok(()) |
| 601 | } | 593 | } |
| 602 | 594 | ||
| 603 | - pub fn cycle_focus(&mut self, direction: i32) -> std::result::Result<(), WmError> { | 595 | + pub fn cycle_focus(&mut self, direction: i32) -> WmResult<()> { |
| 604 | let visible = self.visible_windows(); | 596 | let visible = self.visible_windows(); |
| 605 | 597 | ||
| 606 | if visible.is_empty() { | 598 | if visible.is_empty() { |
| @@ -626,7 +618,7 @@ impl WindowManager { | |||
| 626 | Ok(()) | 618 | Ok(()) |
| 627 | } | 619 | } |
| 628 | 620 | ||
| 629 | - pub fn set_focus(&mut self, window: Option<Window>) -> std::result::Result<(), WmError> { | 621 | + pub fn set_focus(&mut self, window: Option<Window>) -> WmResult<()> { |
| 630 | self.focused_window = window; | 622 | self.focused_window = window; |
| 631 | 623 | ||
| 632 | if let Some(win) = window { | 624 | if let Some(win) = window { |
| @@ -639,7 +631,7 @@ impl WindowManager { | |||
| 639 | Ok(()) | 631 | Ok(()) |
| 640 | } | 632 | } |
| 641 | 633 | ||
| 642 | - fn update_focus_visuals(&self) -> std::result::Result<(), WmError> { | 634 | + fn update_focus_visuals(&self) -> WmResult<()> { |
| 643 | for &window in &self.windows { | 635 | for &window in &self.windows { |
| 644 | let (border_color, border_width) = if self.focused_window == Some(window) { | 636 | let (border_color, border_width) = if self.focused_window == Some(window) { |
| 645 | (self.config.border_focused, self.config.border_width) | 637 | (self.config.border_focused, self.config.border_width) |
| @@ -661,7 +653,7 @@ impl WindowManager { | |||
| 661 | Ok(()) | 653 | Ok(()) |
| 662 | } | 654 | } |
| 663 | 655 | ||
| 664 | - fn move_mouse(&mut self, window: Window) -> std::result::Result<(), WmError> { | 656 | + fn move_mouse(&mut self, window: Window) -> WmResult<()> { |
| 665 | self.floating_windows.insert(window); | 657 | self.floating_windows.insert(window); |
| 666 | 658 | ||
| 667 | let geometry = self.connection.get_geometry(window)?.reply()?; | 659 | let geometry = self.connection.get_geometry(window)?.reply()?; |
| @@ -714,7 +706,7 @@ impl WindowManager { | |||
| 714 | Ok(()) | 706 | Ok(()) |
| 715 | } | 707 | } |
| 716 | 708 | ||
| 717 | - fn resize_mouse(&mut self, window: Window) -> std::result::Result<(), WmError> { | 709 | + fn resize_mouse(&mut self, window: Window) -> WmResult<()> { |
| 718 | self.floating_windows.insert(window); | 710 | self.floating_windows.insert(window); |
| 719 | 711 | ||
| 720 | let geometry = self.connection.get_geometry(window)?.reply()?; | 712 | let geometry = self.connection.get_geometry(window)?.reply()?; |
| @@ -778,7 +770,7 @@ impl WindowManager { | |||
| 778 | Ok(()) | 770 | Ok(()) |
| 779 | } | 771 | } |
| 780 | 772 | ||
| 781 | - fn handle_event(&mut self, event: Event) -> std::result::Result<Option<bool>, WmError> { | 773 | + fn handle_event(&mut self, event: Event) -> WmResult<Option<bool>> { |
| 782 | match event { | 774 | match event { |
| 783 | Event::MapRequest(event) => { | 775 | Event::MapRequest(event) => { |
| 784 | let attrs = match self.connection.get_window_attributes(event.window)?.reply() { | 776 | let attrs = match self.connection.get_window_attributes(event.window)?.reply() { |
| @@ -861,7 +853,7 @@ impl WindowManager { | |||
| 861 | Ok(None) | 853 | Ok(None) |
| 862 | } | 854 | } |
| 863 | 855 | ||
| 864 | - fn apply_layout(&self) -> std::result::Result<(), WmError> { | 856 | + fn apply_layout(&self) -> WmResult<()> { |
| 865 | if self.fullscreen_window.is_some() { | 857 | if self.fullscreen_window.is_some() { |
| 866 | return Ok(()); | 858 | return Ok(()); |
| 867 | } | 859 | } |
| @@ -917,7 +909,7 @@ impl WindowManager { | |||
| 917 | Ok(()) | 909 | Ok(()) |
| 918 | } | 910 | } |
| 919 | 911 | ||
| 920 | - fn remove_window(&mut self, window: Window) -> std::result::Result<(), WmError> { | 912 | + fn remove_window(&mut self, window: Window) -> WmResult<()> { |
| 921 | let initial_count = self.windows.len(); | 913 | let initial_count = self.windows.len(); |
| 922 | self.windows.retain(|&w| w != window); | 914 | self.windows.retain(|&w| w != window); |
| 923 | self.window_tags.remove(&window); | 915 | self.window_tags.remove(&window); |