Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::collections::HashMap;
- #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq)]
- pub struct Window(pub u32);
- #[derive(Debug, Copy, Clone, Eq, PartialEq)]
- pub enum Event {
- BrowserOpened(Window),
- BrowserClosed(Window),
- BrowserMoved {
- window: Window,
- workspace: Workspace,
- },
- SimulationOpened(Window),
- SimulationClosed(Window),
- SimulationMoved {
- window: Window,
- workspace: Workspace,
- },
- SwitchedWorkspace(Workspace),
- }
- #[derive(Debug, Copy, Clone, Eq, PartialEq)]
- pub enum Workspace {
- Browser,
- Simulation,
- Other,
- }
- #[derive(Debug)]
- pub struct State {
- pub browser_windows: HashMap<Window, Workspace>,
- pub simulation_windows: HashMap<Window, Workspace>,
- pub focused_workspace: Workspace,
- }
- impl State {
- pub fn new() -> Self {
- State {
- browser_windows: Default::default(),
- simulation_windows: Default::default(),
- focused_workspace: Workspace::Other,
- }
- }
- pub fn handle_event(&mut self, event: Event) {
- match event {
- Event::BrowserOpened(win) => {
- move_window(win, Workspace::Browser);
- self.browser_windows.insert(win, Workspace::Browser);
- // if no simulation is running then show the browser
- // if there *is* a simulation running don't grab
- // the focus away from it.
- if self.simulation_windows.is_empty() {
- self.focus_browser();
- }
- }
- Event::BrowserClosed(win) => {
- self.browser_windows.remove(&win);
- if self.browser_windows.is_empty() {
- eprintln!("Browser crashed?");
- // just wait for systemd to do its thing.
- }
- }
- Event::BrowserMoved { window, workspace } => {
- let entry: Option<&mut Workspace> =
- self.browser_windows.get_mut(&window);
- if let Some(ws) = entry {
- *ws = workspace;
- }
- }
- Event::SimulationOpened(win) => {
- move_window(win, Workspace::Simulation);
- self.simulation_windows.insert(win, Workspace::Simulation);
- self.focus_simulation();
- }
- Event::SimulationClosed(win) => {
- self.simulation_windows.remove(&win);
- if self.simulation_windows.is_empty() {
- self.focus_browser();
- }
- }
- Event::SimulationMoved { window, workspace } => {
- let entry: Option<&mut Workspace> =
- self.simulation_windows.get_mut(&window);
- if let Some(ws) = entry {
- *ws = workspace;
- }
- }
- Event::SwitchedWorkspace(workspace) => {
- // Nothing fancy, just track the current workspace
- self.focused_workspace = workspace;
- }
- }
- }
- pub fn focus_simulation(&mut self) {
- self.focused_workspace = Workspace::Simulation;
- for (id, ws) in &mut self.simulation_windows {
- // if not on the correct workspace move it there
- if *ws != Workspace::Simulation {
- move_window(*id, Workspace::Simulation);
- *ws = Workspace::Simulation;
- }
- }
- }
- pub fn focus_browser(&mut self) {
- self.focused_workspace = Workspace::Simulation;
- for (id, ws) in &mut self.browser_windows {
- if *ws != Workspace::Browser {
- move_window(*id, Workspace::Browser);
- *ws = Workspace::Browser;
- }
- }
- }
- }
- fn move_window(window: Window, workspace: Workspace) {
- let _ = window;
- let _ = workspace;
- // left as an exercise for the reader.
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement