users/ryan/modules/simple-bar/simple-bar-source/lib/components/yabai/spaces.jsx
2bead7efb473f0202dc6ef189fbc0e4a5401d41f
· 3.3 KB · 109 lines
raw
| 1 | import * as Uebersicht from "uebersicht"; |
| 2 | import Space from "./space.jsx"; |
| 3 | import Stickies from "./stickies.jsx"; |
| 4 | import * as Icons from "../icons/icons.jsx"; |
| 5 | import { SuspenseIcon } from "../icons/icon.jsx"; |
| 6 | import { useYabaiContext } from "../yabai-context.jsx"; |
| 7 | import { useSimpleBarContext } from "../simple-bar-context.jsx"; |
| 8 | import * as Utils from "../../utils"; |
| 9 | import * as Yabai from "../../yabai"; |
| 10 | |
| 11 | export { spacesStyles as styles } from "../../styles/components/spaces/spaces"; |
| 12 | |
| 13 | const { React } = Uebersicht; |
| 14 | |
| 15 | /** |
| 16 | * Spaces component to display spaces and manage space-related actions. |
| 17 | * @returns {JSX.Element|null} The rendered component. |
| 18 | */ |
| 19 | const Component = React.memo(() => { |
| 20 | // Get spaces and windows data from Yabai context |
| 21 | const { spaces, windows } = useYabaiContext(); |
| 22 | // Get various settings and display information from simple-bar context |
| 23 | const { SIPDisabled, displayIndex, displays, settings } = |
| 24 | useSimpleBarContext(); |
| 25 | const { spacesDisplay, process } = settings; |
| 26 | const { |
| 27 | displayStickyWindowsSeparately, |
| 28 | spacesExclusions, |
| 29 | exclusionsAsRegex, |
| 30 | hideCreateSpaceButton, |
| 31 | showOnDisplay, |
| 32 | } = spacesDisplay; |
| 33 | // Determine if the component should be visible on the current display |
| 34 | const visible = Utils.isVisibleOnDisplay(displayIndex, showOnDisplay); |
| 35 | const isProcessVisible = Utils.isVisibleOnDisplay( |
| 36 | displayIndex, |
| 37 | process.showOnDisplay, |
| 38 | ); |
| 39 | |
| 40 | if (!visible) return null; |
| 41 | |
| 42 | if (!spaces && !windows) { |
| 43 | return <div className="spaces spaces--empty" />; |
| 44 | } |
| 45 | |
| 46 | // Find the current space index |
| 47 | const { index: currentSpaceIndex } = |
| 48 | spaces.find((space) => { |
| 49 | const { "has-focus": hasFocus, focused: __legacyHasFocus } = space; |
| 50 | return hasFocus ?? __legacyHasFocus; |
| 51 | }) || {}; |
| 52 | |
| 53 | return displays.map((display, i) => { |
| 54 | if (display.index !== displayIndex) return null; |
| 55 | |
| 56 | /** |
| 57 | * Handle click event to create a new space. |
| 58 | * @param {Event} e - The click event. |
| 59 | */ |
| 60 | const onClick = async (e) => { |
| 61 | Utils.clickEffect(e); |
| 62 | await Yabai.createSpace(displayIndex); |
| 63 | }; |
| 64 | |
| 65 | return ( |
| 66 | <div key={i} className="spaces"> |
| 67 | {displayStickyWindowsSeparately && <Stickies display={display} />} |
| 68 | {spaces.map((space, i) => { |
| 69 | const { label, index } = space; |
| 70 | const lastOfSpace = |
| 71 | i !== 0 && space.display !== spaces[i - 1].display; |
| 72 | |
| 73 | const key = label?.length ? label : index; |
| 74 | const spacesExclusionsList = spacesExclusions.replace(/ /g, "").split(","); |
| 75 | const isExcluded = Utils.isSpaceExcluded( |
| 76 | key, |
| 77 | spacesExclusionsList, |
| 78 | exclusionsAsRegex, |
| 79 | ); |
| 80 | |
| 81 | if (isExcluded) return null; |
| 82 | |
| 83 | return ( |
| 84 | <Space |
| 85 | key={key} |
| 86 | display={display} |
| 87 | space={space} |
| 88 | currentSpaceIndex={currentSpaceIndex} |
| 89 | lastOfSpace={lastOfSpace} |
| 90 | /> |
| 91 | ); |
| 92 | })} |
| 93 | {SIPDisabled && !hideCreateSpaceButton ? ( |
| 94 | <button className="spaces__add" onClick={onClick}> |
| 95 | <SuspenseIcon> |
| 96 | <Icons.Add /> |
| 97 | </SuspenseIcon> |
| 98 | </button> |
| 99 | ) : ( |
| 100 | isProcessVisible && <div className="spaces__end-separator" /> |
| 101 | )} |
| 102 | </div> |
| 103 | ); |
| 104 | }); |
| 105 | }); |
| 106 | |
| 107 | Component.displayName = "Spaces"; |
| 108 | |
| 109 | export default Component; |