pub fn restore_state(&mut self, state: &State) {
        // Restore tags.
        for old_tag in state.tags.all() {
            if let Some(tag) = self.tags.get_mut(old_tag.id) {
                tag.hidden = old_tag.hidden;
                tag.layout = old_tag.layout;
                tag.layout_rotation = old_tag.layout_rotation;
                tag.flipped_vertical = old_tag.flipped_vertical;
                tag.flipped_horizontal = old_tag.flipped_horizontal;
                tag.main_width_percentage = old_tag.main_width_percentage;
            }
        }

        let are_tags_equal = self.tags.all().eq(&state.tags.all());

        // Restore windows.
        let mut ordered = vec![];
        let mut had_strut = false;
        state.windows.iter().for_each(|old_window| {
            if let Some((index, new_window)) = self
                .windows
                .clone()
                .iter_mut()
                .enumerate()
                .find(|w| w.1.handle == old_window.handle)
            {
                had_strut = old_window.strut.is_some() || had_strut;

                new_window.set_floating(old_window.floating());
                new_window.set_floating_offsets(old_window.get_floating_offsets());
                new_window.apply_margin_multiplier(old_window.margin_multiplier);
                new_window.pid = old_window.pid;
                new_window.normal = old_window.normal;
                if are_tags_equal {
                    new_window.tags = old_window.tags.clone();
                } else {
                    let mut new_tags = old_window.tags.clone();
                    // Only retain the tags, that still exist.
                    new_tags.retain(|&tag_id| self.tags.get(tag_id).is_some());
                    // If there are no tags, add tag '1', so the window will not be lost.
                    if new_tags.is_empty() {
                        new_tags.push(1);
                    }
                    new_window.clear_tags();
                    new_tags.iter().for_each(|&tag_id| new_window.tag(&tag_id));
                }
                new_window.strut = old_window.strut;
                new_window.set_states(old_window.states());
                ordered.push(new_window.clone());
                self.windows.remove(index);
            }
        });
        if had_strut {
            self.update_static();
        }
        self.windows.append(&mut ordered);

        // This is needed due to mutable/immutable borrows.
        let tags = &self.tags;

        // Restore workspaces.
        for workspace in &mut self.workspaces {
            if let Some(old_workspace) = state.workspaces.iter().find(|w| w.id == workspace.id) {
                workspace.layout = old_workspace.layout;
                workspace.main_width_percentage = old_workspace.main_width_percentage;
                workspace.margin_multiplier = old_workspace.margin_multiplier;
                if are_tags_equal {
                    workspace.tags = old_workspace.tags.clone();
                } else {
                    let mut new_tags = old_workspace.tags.clone();
                    // Only retain the tags, that still exist.
                    new_tags.retain(|&tag_id| tags.get(tag_id).is_some());
                    // If there are no tags, add tag '1', so the workspace has a tag.
                    if new_tags.is_empty() {
                        new_tags.push(1);
                    }
                    new_tags
                        .iter()
                        .for_each(|&tag_id| workspace.tags = vec![tag_id]);
                }
            }
        }

        // Restore scratchpads.
        for (scratchpad, id) in &state.active_scratchpads {
            self.active_scratchpads.insert(scratchpad.clone(), *id);
        }

        // Restore focus.
        self.focus_manager.tags_last_window = state.focus_manager.tags_last_window.clone();
        self.focus_manager
            .tags_last_window
            .retain(|&id, _| tags.get(id).is_some());
        let tag_id = match state.focus_manager.tag(0) {
            // If the tag still exists it should be displayed on a workspace.
            Some(tag_id) if self.tags.get(tag_id).is_some() => tag_id,
            // If the tag doesn't exist, tag 1 should be displayed on a workspace.
            Some(_) => 1,
            // If we don't have any tag history (We should), focus the tag on workspace 1.
            None => match self.workspaces.first() {
                Some(ws) => ws.tags[0],
                // This should never happen.
                None => 1,
            },
        };
        self.focus_tag(&tag_id);
    }
/// Apply saved state to a running manager.
Apply saved state to a running manager.
Apply saved state to a running manager .
pub fn restore_state(&mut self, state: &State) {
        // Restore tags.
        for old_tag in state.tags.all() {
            if let Some(tag) = self.tags.get_mut(old_tag.id) {
                tag.hidden = old_tag.hidden;
                tag.layout = old_tag.layout;
                tag.layout_rotation = old_tag.layout_rotation;
                tag.flipped_vertical = old_tag.flipped_vertical;
                tag.flipped_horizontal = old_tag.flipped_horizontal;
                tag.main_width_percentage = old_tag.main_width_percentage;
            }
        }
        let are_tags_equal = self.tags.all().eq(&state.tags.all());
        // Restore windows.
        let mut ordered = vec![];
        let mut had_strut = false;
        state.windows.iter().for_each(|old_window| {
            if let Some((index, new_window)) = self
                .windows
                .clone()
                .iter_mut()
                .enumerate()
                .find(|w| w.1.handle == old_window.handle)
            {
                had_strut = old_window.strut.is_some() || had_strut;
                new_window.set_floating(old_window.floating());
                new_window.set_floating_offsets(old_window.get_floating_offsets());
                new_window.apply_margin_multiplier(old_window.margin_multiplier);
                new_window.pid = old_window.pid;
                new_window.normal = old_window.normal;
                if are_tags_equal {
                    new_window.tags = old_window.tags.clone();
                } else {
                    let mut new_tags = old_window.tags.clone();
                    // Only retain the tags, that still exist.
                    new_tags.retain(|&tag_id| self.tags.get(tag_id).is_some());
                    // If there are no tags, add tag '1', so the window will not be lost.
                    if new_tags.is_empty() {
                        new_tags.push(1);
                    }
                    new_window.clear_tags();
                    new_tags.iter().for_each(|&tag_id| new_window.tag(&tag_id));
                }
                new_window.strut = old_window.strut;
                new_window.set_states(old_window.states());
                ordered.push(new_window.clone());
                self.windows.remove(index);
            }
        });
        if had_strut {
            self.update_static();
        }
        self.windows.append(&mut ordered);
        // This is needed due to mutable/immutable borrows.
        let tags = &self.tags;
        // Restore workspaces.
        for workspace in &mut self.workspaces {
            if let Some(old_workspace) = state.workspaces.iter().find(|w| w.id == workspace.id) {
                workspace.layout = old_workspace.layout;
                workspace.main_width_percentage = old_workspace.main_width_percentage;
                workspace.margin_multiplier = old_workspace.margin_multiplier;
                if are_tags_equal {
                    workspace.tags = old_workspace.tags.clone();
                } else {
                    let mut new_tags = old_workspace.tags.clone();
                    // Only retain the tags, that still exist.
                    new_tags.retain(|&tag_id| tags.get(tag_id).is_some());
                    // If there are no tags, add tag '1', so the workspace has a tag.
                    if new_tags.is_empty() {
                        new_tags.push(1);
                    }
                    new_tags
                        .iter()
                        .for_each(|&tag_id| workspace.tags = vec![tag_id]);
                }
            }
        }
        // Restore scratchpads.
        for (scratchpad, id) in &state.active_scratchpads {
            self.active_scratchpads.insert(scratchpad.clone(), *id);
        }
        // Restore focus.
        self.focus_manager.tags_last_window = state.focus_manager.tags_last_window.clone();
        self.focus_manager
            .tags_last_window
            .retain(|&id, _| tags.get(id).is_some());
        let tag_id = match state.focus_manager.tag(0) {
            // If the tag still exists it should be displayed on a workspace.
            Some(tag_id) if self.tags.get(tag_id).is_some() => tag_id,
            // If the tag doesn't exist, tag 1 should be displayed on a workspace.
            Some(_) => 1,
            // If we don't have any tag history (We should), focus the tag on workspace 1.
            None => match self.workspaces.first() {
                Some(ws) => ws.tags[0],
                // This should never happen.
                None => 1,
            },
        };
        self.focus_tag(&tag_id);
    }
pub fn restore_state ( & mut self , state : & State ) { // Restore tags. for old_tag in state . tags . all ( ) { if let Some ( tag ) = self . tags . get_mut ( old_tag . id ) { tag . hidden = old_tag . hidden ; tag . layout = old_tag . layout ; tag . layout_rotation = old_tag . layout_rotation ; tag . flipped_vertical = old_tag . flipped_vertical ; tag . flipped_horizontal = old_tag . flipped_horizontal ; tag . main_width_percentage = old_tag . main_width_percentage ; } } let are_tags_equal = self . tags . all ( ) . eq ( & state . tags . all ( ) ) ; // Restore windows. let mut ordered = vec ! [ ] ; let mut had_strut = false ; state . windows . iter ( ) . for_each ( | old_window | { if let Some ( ( index , new_window ) ) = self . windows . clone ( ) . iter_mut ( ) . enumerate ( ) . find ( | w | w . 1 . handle == old_window . handle ) { had_strut = old_window . strut . is_some ( ) || had_strut ; new_window . set_floating ( old_window . floating ( ) ) ; new_window . set_floating_offsets ( old_window . get_floating_offsets ( ) ) ; new_window . apply_margin_multiplier ( old_window . margin_multiplier ) ; new_window . pid = old_window . pid ; new_window . normal = old_window . normal ; if are_tags_equal { new_window . tags = old_window . tags . clone ( ) ; } else { let mut new_tags = old_window . tags . clone ( ) ; // Only retain the tags, that still exist. new_tags . retain ( | & tag_id | self . tags . get ( tag_id ) . is_some ( ) ) ; // If there are no tags, add tag '1', so the window will not be lost. if new_tags . is_empty ( ) { new_tags . push ( 1 ) ; } new_window . clear_tags ( ) ; new_tags . iter ( ) . for_each ( | & tag_id | new_window . tag ( & tag_id ) ) ; } new_window . strut = old_window . strut ; new_window . set_states ( old_window . states ( ) ) ; ordered . push ( new_window . clone ( ) ) ; self . windows . remove ( index ) ; } } ) ; if had_strut { self . update_static ( ) ; } self . windows . append ( & mut ordered ) ; // This is needed due to mutable/immutable borrows. let tags = & self . tags ; // Restore workspaces. for workspace in & mut self . workspaces { if let Some ( old_workspace ) = state . workspaces . iter ( ) . find ( | w | w . id == workspace . id ) { workspace . layout = old_workspace . layout ; workspace . main_width_percentage = old_workspace . main_width_percentage ; workspace . margin_multiplier = old_workspace . margin_multiplier ; if are_tags_equal { workspace . tags = old_workspace . tags . clone ( ) ; } else { let mut new_tags = old_workspace . tags . clone ( ) ; // Only retain the tags, that still exist. new_tags . retain ( | & tag_id | tags . get ( tag_id ) . is_some ( ) ) ; // If there are no tags, add tag '1', so the workspace has a tag. if new_tags . is_empty ( ) { new_tags . push ( 1 ) ; } new_tags . iter ( ) . for_each ( | & tag_id | workspace . tags = vec ! [ tag_id ] ) ; } } } // Restore scratchpads. for ( scratchpad , id ) in & state . active_scratchpads { self . active_scratchpads . insert ( scratchpad . clone ( ) , * id ) ; } // Restore focus. self . focus_manager . tags_last_window = state . focus_manager . tags_last_window . clone ( ) ; self . focus_manager . tags_last_window . retain ( | & id , _ | tags . get ( id ) . is_some ( ) ) ; let tag_id = match state . focus_manager . tag ( 0 ) { // If the tag still exists it should be displayed on a workspace. Some ( tag_id ) if self . tags . get ( tag_id ) . is_some ( ) => tag_id , // If the tag doesn't exist, tag 1 should be displayed on a workspace. Some ( _ ) => 1 , // If we don't have any tag history (We should), focus the tag on workspace 1. None => match self . workspaces . first ( ) { Some ( ws ) => ws . tags [ 0 ] , // This should never happen. None => 1 , } , } ; self . focus_tag ( & tag_id ) ; }
Apply saved state to a running manager.
Apply saved state to a running manager .
&mut self
state
&State
&mut self
state
&State