Advertisement
Guest User

Untitled

a guest
Jul 19th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1. use std::collections::HashMap;
  2.  
  3. #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq)]
  4. pub struct Window(pub u32);
  5.  
  6. #[derive(Debug, Copy, Clone, Eq, PartialEq)]
  7. pub enum Event {
  8. BrowserOpened(Window),
  9. BrowserClosed(Window),
  10. BrowserMoved {
  11. window: Window,
  12. workspace: Workspace,
  13. },
  14.  
  15. SimulationOpened(Window),
  16. SimulationClosed(Window),
  17. SimulationMoved {
  18. window: Window,
  19. workspace: Workspace,
  20. },
  21.  
  22. SwitchedWorkspace(Workspace),
  23. }
  24.  
  25. #[derive(Debug, Copy, Clone, Eq, PartialEq)]
  26. pub enum Workspace {
  27. Browser,
  28. Simulation,
  29. Other,
  30. }
  31.  
  32. #[derive(Debug)]
  33. pub struct State {
  34. pub browser_windows: HashMap<Window, Workspace>,
  35. pub simulation_windows: HashMap<Window, Workspace>,
  36.  
  37. pub focused_workspace: Workspace,
  38. }
  39.  
  40. impl State {
  41. pub fn new() -> Self {
  42. State {
  43. browser_windows: Default::default(),
  44. simulation_windows: Default::default(),
  45. focused_workspace: Workspace::Other,
  46. }
  47. }
  48.  
  49. pub fn handle_event(&mut self, event: Event) {
  50. match event {
  51. Event::BrowserOpened(win) => {
  52. move_window(win, Workspace::Browser);
  53.  
  54. self.browser_windows.insert(win, Workspace::Browser);
  55.  
  56. // if no simulation is running then show the browser
  57. // if there *is* a simulation running don't grab
  58. // the focus away from it.
  59. if self.simulation_windows.is_empty() {
  60. self.focus_browser();
  61. }
  62. }
  63. Event::BrowserClosed(win) => {
  64. self.browser_windows.remove(&win);
  65.  
  66. if self.browser_windows.is_empty() {
  67. eprintln!("Browser crashed?");
  68. // just wait for systemd to do its thing.
  69. }
  70. }
  71. Event::BrowserMoved { window, workspace } => {
  72. let entry: Option<&mut Workspace> =
  73. self.browser_windows.get_mut(&window);
  74.  
  75. if let Some(ws) = entry {
  76. *ws = workspace;
  77. }
  78. }
  79. Event::SimulationOpened(win) => {
  80. move_window(win, Workspace::Simulation);
  81.  
  82. self.simulation_windows.insert(win, Workspace::Simulation);
  83.  
  84. self.focus_simulation();
  85. }
  86. Event::SimulationClosed(win) => {
  87. self.simulation_windows.remove(&win);
  88.  
  89. if self.simulation_windows.is_empty() {
  90. self.focus_browser();
  91. }
  92. }
  93. Event::SimulationMoved { window, workspace } => {
  94. let entry: Option<&mut Workspace> =
  95. self.simulation_windows.get_mut(&window);
  96.  
  97. if let Some(ws) = entry {
  98. *ws = workspace;
  99. }
  100. }
  101. Event::SwitchedWorkspace(workspace) => {
  102. // Nothing fancy, just track the current workspace
  103. self.focused_workspace = workspace;
  104. }
  105. }
  106. }
  107.  
  108. pub fn focus_simulation(&mut self) {
  109. self.focused_workspace = Workspace::Simulation;
  110.  
  111. for (id, ws) in &mut self.simulation_windows {
  112. // if not on the correct workspace move it there
  113. if *ws != Workspace::Simulation {
  114. move_window(*id, Workspace::Simulation);
  115. *ws = Workspace::Simulation;
  116. }
  117. }
  118. }
  119.  
  120. pub fn focus_browser(&mut self) {
  121. self.focused_workspace = Workspace::Simulation;
  122.  
  123. for (id, ws) in &mut self.browser_windows {
  124. if *ws != Workspace::Browser {
  125. move_window(*id, Workspace::Browser);
  126. *ws = Workspace::Browser;
  127. }
  128. }
  129. }
  130. }
  131.  
  132. fn move_window(window: Window, workspace: Workspace) {
  133. let _ = window;
  134. let _ = workspace;
  135. // left as an exercise for the reader.
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement