From 6210614ce7138f871818df4941cdd04d65e6cc1d Mon Sep 17 00:00:00 2001
From: Michael Telatynski <7t3chguy@gmail.com>
Date: Fri, 12 Mar 2021 17:37:15 +0000
Subject: [PATCH 1/5] Rebuild space panel layout to resolve weirdness with
 selections and nesting

---
 res/css/structures/_SpacePanel.scss | 42 ++++++++---------------------
 1 file changed, 11 insertions(+), 31 deletions(-)

diff --git a/res/css/structures/_SpacePanel.scss b/res/css/structures/_SpacePanel.scss
index 9937117086..4ced807f66 100644
--- a/res/css/structures/_SpacePanel.scss
+++ b/res/css/structures/_SpacePanel.scss
@@ -16,9 +16,8 @@ limitations under the License.
 
 $topLevelHeight: 32px;
 $nestedHeight: 24px;
-$gutterSize: 17px;
-$activeStripeSize: 4px;
-$activeBorderTransparentGap: 2px;
+$gutterSize: 16px;
+$activeBorderTransparentGap: 1px;
 
 $activeBackgroundColor: $roomtile-selected-bg-color;
 $activeBorderColor: $secondary-fg-color;
@@ -102,14 +101,11 @@ $activeBorderColor: $secondary-fg-color;
         &.mx_SpaceButton_active {
             &:not(.mx_SpaceButton_narrow) .mx_SpaceButton_selectionWrapper {
                 background-color: $activeBackgroundColor;
-                border-radius: 8px;
             }
 
-            &.mx_SpaceButton_narrow {
-                .mx_BaseAvatar, .mx_SpaceButton_avatarPlaceholder {
-                    border: 2px $activeBorderColor solid;
-                    border-radius: 11px;
-                }
+            &.mx_SpaceButton_narrow .mx_SpaceButton_selectionWrapper {
+                padding: $activeBorderTransparentGap;
+                border: 3px $activeBorderColor solid;
             }
         }
 
@@ -117,6 +113,8 @@ $activeBorderColor: $secondary-fg-color;
             display: flex;
             flex: 1;
             align-items: center;
+            border-radius: 12px;
+            padding: 4px;
         }
 
         .mx_SpaceButton_name {
@@ -133,8 +131,8 @@ $activeBorderColor: $secondary-fg-color;
         }
 
         .mx_SpaceButton_toggleCollapse {
-            width: calc($gutterSize - $activeStripeSize);
-            margin-left: 1px;
+            width: $gutterSize;
+            margin-right: -4px;
             height: 20px;
             mask-position: center;
             mask-size: 20px;
@@ -172,11 +170,6 @@ $activeBorderColor: $secondary-fg-color;
             }
         }
 
-        .mx_SpaceButton_avatarPlaceholder {
-            border: $activeBorderTransparentGap transparent solid;
-            padding: $activeBorderTransparentGap;
-        }
-
         &.mx_SpaceButton_new .mx_SpaceButton_icon {
             background-color: $accent-color;
             transition: all .1s ease-in-out; // TODO transition
@@ -196,21 +189,8 @@ $activeBorderColor: $secondary-fg-color;
             }
         }
 
-        .mx_BaseAvatar {
-            /* moving the border-radius to this element from _image
-            element so we can add a border to it without the initials being displaced */
-            overflow: hidden;
-            border: 2px transparent solid;
-            padding: $activeBorderTransparentGap;
-
-            .mx_BaseAvatar_initial {
-                top: $activeBorderTransparentGap;
-                left: $activeBorderTransparentGap;
-            }
-
-            .mx_BaseAvatar_image {
-                border-radius: 8px;
-            }
+        .mx_BaseAvatar_image {
+            border-radius: 8px;
         }
 
         .mx_SpaceButton_menuButton {

From f9eace1729bbe52d65378c8d66d02820d59d357a Mon Sep 17 00:00:00 2001
From: Michael Telatynski <7t3chguy@gmail.com>
Date: Mon, 15 Mar 2021 11:59:46 +0000
Subject: [PATCH 2/5] Rebuild alignment of the menu buttons and notification
 badges

---
 res/css/structures/_SpacePanel.scss | 63 +++++++++++++++++++++--------
 1 file changed, 46 insertions(+), 17 deletions(-)

diff --git a/res/css/structures/_SpacePanel.scss b/res/css/structures/_SpacePanel.scss
index 4ced807f66..f489d7b8b5 100644
--- a/res/css/structures/_SpacePanel.scss
+++ b/res/css/structures/_SpacePanel.scss
@@ -62,21 +62,26 @@ $activeBorderColor: $secondary-fg-color;
     }
 
     .mx_AutoHideScrollbar {
-        padding: 16px 12px 16px 0;
+        padding: 16px 0;
     }
 
     .mx_SpaceButton_toggleCollapse {
         cursor: pointer;
     }
 
-    .mx_SpaceItem.collapsed {
-        .mx_SpaceButton {
-            .mx_NotificationBadge {
-                right: -4px;
-                top: -4px;
-            }
-        }
+    .mx_SpaceTreeLevel {
+        display: flex;
+        flex-direction: column;
+        max-width: 250px;
+        flex-grow: 1;
+    }
 
+    .mx_SpaceItem {
+        display: inline-flex;
+        flex-flow: wrap;
+    }
+
+    .mx_SpaceItem.collapsed {
         & > .mx_SpaceButton > .mx_SpaceButton_toggleCollapse {
             transform: rotate(-90deg);
         }
@@ -88,15 +93,16 @@ $activeBorderColor: $secondary-fg-color;
 
     .mx_SpaceItem:not(.hasSubSpaces) > .mx_SpaceButton {
         margin-left: $gutterSize;
+        min-width: 40px;
     }
 
     .mx_SpaceButton {
         border-radius: 8px;
-        position: relative;
         margin-bottom: 2px;
         display: flex;
         align-items: center;
-        padding: 4px;
+        padding: 4px 4px 4px 0;
+        width: 100%;
 
         &.mx_SpaceButton_active {
             &:not(.mx_SpaceButton_narrow) .mx_SpaceButton_selectionWrapper {
@@ -110,6 +116,7 @@ $activeBorderColor: $secondary-fg-color;
         }
 
         .mx_SpaceButton_selectionWrapper {
+            position: relative;
             display: flex;
             flex: 1;
             align-items: center;
@@ -117,12 +124,23 @@ $activeBorderColor: $secondary-fg-color;
             padding: 4px;
         }
 
+        &:not(.mx_SpaceButton_narrow) {
+            .mx_SpaceButton_selectionWrapper {
+                width: 100%;
+                padding-right: 16px;
+                overflow: hidden;
+            }
+        }
+
+        &.mx_SpaceButton_narrow {
+            margin-right: 10px;
+        }
+
         .mx_SpaceButton_name {
             flex: 1;
             margin-left: 8px;
             white-space: nowrap;
             display: block;
-            max-width: 150px;
             text-overflow: ellipsis;
             overflow: hidden;
             padding-right: 8px;
@@ -199,8 +217,9 @@ $activeBorderColor: $secondary-fg-color;
             height: 20px;
             margin-top: auto;
             margin-bottom: auto;
-            position: relative;
             display: none;
+            position: absolute;
+            right: 4px;
 
             &::before {
                 top: 2px;
@@ -219,9 +238,8 @@ $activeBorderColor: $secondary-fg-color;
     }
 
     .mx_SpacePanel_badgeContainer {
+        position: absolute;
         height: 16px;
-        // don't set width so that it takes no space when there is no badge to show
-        margin: auto 0; // vertically align
 
         // Create a flexbox to make aligning dot badges easier
         display: flex;
@@ -241,14 +259,25 @@ $activeBorderColor: $secondary-fg-color;
     &.collapsed {
         .mx_SpaceButton {
             .mx_SpacePanel_badgeContainer {
-                position: absolute;
-                right: 0px;
-                top: 2px;
+                right: -3px;
+                top: -3px;
+            }
+
+            &.mx_SpaceButton_active .mx_SpacePanel_badgeContainer {
+                // when we draw the selection border we move the relative bounds of our parent
+                // so update our position within the bounds of the parent to maintain position overall
+                right: -6px;
+                top: -6px;
             }
         }
     }
 
     &:not(.collapsed) {
+        .mx_SpacePanel_badgeContainer {
+            position: absolute;
+            right: 4px;
+        }
+
         .mx_SpaceButton:hover,
         .mx_SpaceButton:focus-within,
         .mx_SpaceButton_hasMenuOpen {

From 272f06d57a84c714f92ad6570a6f8d506aeccec9 Mon Sep 17 00:00:00 2001
From: Michael Telatynski <7t3chguy@gmail.com>
Date: Mon, 15 Mar 2021 12:16:48 +0000
Subject: [PATCH 3/5] Fix Space Panel exploding with cyclic hierarchies

---
 .../views/spaces/SpaceTreeLevel.tsx           | 23 ++++++++++++++-----
 1 file changed, 17 insertions(+), 6 deletions(-)

diff --git a/src/components/views/spaces/SpaceTreeLevel.tsx b/src/components/views/spaces/SpaceTreeLevel.tsx
index 04d6c02208..83bc2296e7 100644
--- a/src/components/views/spaces/SpaceTreeLevel.tsx
+++ b/src/components/views/spaces/SpaceTreeLevel.tsx
@@ -51,6 +51,7 @@ interface IItemProps {
     isNested?: boolean;
     isPanelCollapsed?: boolean;
     onExpand?: Function;
+    parents?: Set<string>;
 }
 
 interface IItemState {
@@ -299,7 +300,8 @@ export class SpaceItem extends React.PureComponent<IItemProps, IItemState> {
         const isNarrow = this.props.isPanelCollapsed;
         const collapsed = this.state.collapsed || forceCollapsed;
 
-        const childSpaces = SpaceStore.instance.getChildSpaces(space.roomId);
+        const childSpaces = SpaceStore.instance.getChildSpaces(space.roomId)
+            .filter(s => !this.props.parents?.has(s.roomId));
         const isActive = activeSpaces.includes(space);
         const itemClasses = classNames({
             "mx_SpaceItem": true,
@@ -312,11 +314,17 @@ export class SpaceItem extends React.PureComponent<IItemProps, IItemState> {
             mx_SpaceButton_narrow: isNarrow,
         });
         const notificationState = SpaceStore.instance.getNotificationState(space.roomId);
-        const childItems = childSpaces && !collapsed ? <SpaceTreeLevel
-            spaces={childSpaces}
-            activeSpaces={activeSpaces}
-            isNested={true}
-        /> : null;
+
+        let childItems;
+        if (childSpaces && !collapsed) {
+            childItems = <SpaceTreeLevel
+                spaces={childSpaces}
+                activeSpaces={activeSpaces}
+                isNested={true}
+                parents={new Set(this.props.parents).add(this.props.space.roomId)}
+            />;
+        }
+
         let notifBadge;
         if (notificationState) {
             notifBadge = <div className="mx_SpacePanel_badgeContainer">
@@ -383,12 +391,14 @@ interface ITreeLevelProps {
     spaces: Room[];
     activeSpaces: Room[];
     isNested?: boolean;
+    parents: Set<string>;
 }
 
 const SpaceTreeLevel: React.FC<ITreeLevelProps> = ({
     spaces,
     activeSpaces,
     isNested,
+    parents,
 }) => {
     return <ul className="mx_SpaceTreeLevel">
         {spaces.map(s => {
@@ -397,6 +407,7 @@ const SpaceTreeLevel: React.FC<ITreeLevelProps> = ({
                 activeSpaces={activeSpaces}
                 space={s}
                 isNested={isNested}
+                parents={parents}
             />);
         })}
     </ul>;

From 62983ca48efd4ec4087a5d29cce3b11acce6d34a Mon Sep 17 00:00:00 2001
From: Michael Telatynski <7t3chguy@gmail.com>
Date: Mon, 15 Mar 2021 12:57:35 +0000
Subject: [PATCH 4/5] Fix space panel alignments when no scrollbar is present

---
 res/css/structures/_SpacePanel.scss         | 5 +----
 res/css/structures/_SpaceRoomDirectory.scss | 1 +
 2 files changed, 2 insertions(+), 4 deletions(-)

diff --git a/res/css/structures/_SpacePanel.scss b/res/css/structures/_SpacePanel.scss
index f489d7b8b5..afcf48ec4d 100644
--- a/res/css/structures/_SpacePanel.scss
+++ b/res/css/structures/_SpacePanel.scss
@@ -35,6 +35,7 @@ $activeBorderColor: $secondary-fg-color;
 
     .mx_SpacePanel_spaceTreeWrapper {
         flex: 1;
+        overflow-y: scroll;
     }
 
     .mx_SpacePanel_toggleCollapse {
@@ -132,10 +133,6 @@ $activeBorderColor: $secondary-fg-color;
             }
         }
 
-        &.mx_SpaceButton_narrow {
-            margin-right: 10px;
-        }
-
         .mx_SpaceButton_name {
             flex: 1;
             margin-left: 8px;
diff --git a/res/css/structures/_SpaceRoomDirectory.scss b/res/css/structures/_SpaceRoomDirectory.scss
index c96398594f..b14e92a1af 100644
--- a/res/css/structures/_SpaceRoomDirectory.scss
+++ b/res/css/structures/_SpaceRoomDirectory.scss
@@ -132,6 +132,7 @@ limitations under the License.
                 height: min-content;
                 margin-left: auto;
                 margin-right: 16px;
+                display: inline-flex;
             }
         }
 

From 5ce3a05867e839eb41f66773899c176c3cbd80dc Mon Sep 17 00:00:00 2001
From: Michael Telatynski <7t3chguy@gmail.com>
Date: Tue, 16 Mar 2021 16:34:28 +0000
Subject: [PATCH 5/5] Update _SpacePanel.scss

---
 res/css/structures/_SpacePanel.scss | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/res/css/structures/_SpacePanel.scss b/res/css/structures/_SpacePanel.scss
index afcf48ec4d..d3e7d7efee 100644
--- a/res/css/structures/_SpacePanel.scss
+++ b/res/css/structures/_SpacePanel.scss
@@ -147,6 +147,8 @@ $activeBorderColor: $secondary-fg-color;
 
         .mx_SpaceButton_toggleCollapse {
             width: $gutterSize;
+            // negative margin to place it correctly even with the complex
+            // 4px selection border each space button has when active
             margin-right: -4px;
             height: 20px;
             mask-position: center;