aboutsummaryrefslogtreecommitdiff
path: root/users/ryan/modules/simple-bar/simple-bar-source/lib/components/yabai/space.jsx
blob: 3b5e761bd86b7b9312b5556baf880c7ef3acdd45 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import * as Uebersicht from "uebersicht";
import OpenedApps from "./opened-apps.jsx";
import SpaceOptions from "./space-options.jsx";
import { useYabaiContext } from "../yabai-context.jsx";
import { useSimpleBarContext } from "../simple-bar-context.jsx";
import * as Utils from "../../utils.js";
import * as Yabai from "../../yabai.js";

const { React } = Uebersicht;

/**
 * Component representing a space in yabai window manager.
 * @param {Object} props - The component props.
 * @param {Object} props.space - The space object.
 * @param {number} props.display - The display index.
 * @param {number} props.currentSpaceIndex - The current space index.
 * @param {boolean} props.lastOfSpace - Whether this is the last space.
 * @returns {JSX.Element|null} The rendered component.
 */
export default function Space({
  space,
  display,
  currentSpaceIndex,
  lastOfSpace,
}) {
  const { windows } = useYabaiContext();
  const { SIPDisabled, settings } = useSimpleBarContext();
  const { spacesDisplay } = settings;
  const {
    displayAllSpacesOnAllScreens,
    exclusionsAsRegex,
    displayStickyWindowsSeparately,
    hideDuplicateAppsInSpaces,
    showOptionsOnHover,
  } = spacesDisplay;

  const labelRef = React.useRef();
  const [hovered, setHovered] = React.useState(false);
  const [noDelay, setNoDelay] = React.useState(false);
  const [editable, setEditable] = React.useState(false);
  const {
    index,
    label,
    "has-focus": hasFocus,
    focused: __legacyHasFocus,
    "is-visible": isVisible,
    visible: __legacyIsVisible,
    "is-native-fullscreen": isNativeFullscreen,
    "native-fullscreen": __legacyIsNativeFullscreen,
    type,
  } = space;
  const [spaceLabel, setSpaceLabel] = React.useState(
    label?.length ? label : index,
  );

  // Return null if the space should not be displayed on the current screen
  if (!displayAllSpacesOnAllScreens && display.index !== space.display)
    return null;

  const exclusions = exclusionsAsRegex
    ? spacesDisplay.exclusions
    : spacesDisplay.exclusions.split(", ");
  const titleExclusions = exclusionsAsRegex
    ? spacesDisplay.titleExclusions
    : spacesDisplay.titleExclusions.split(", ");

  /**
   * Handle mouse enter event.
   * @param {MouseEvent} e - The mouse event.
   */
  const onMouseEnter = (e) => {
    if (!showOptionsOnHover) return;
    const { altKey, metaKey } = e;
    if (altKey) return;
    setHovered(true);
    if (metaKey) setNoDelay(true);
  };

  /**
   * Handle mouse leave event.
   */
  const onMouseLeave = () => {
    setHovered(false);
    setNoDelay(false);
    setEditable(false);
    window.getSelection().removeAllRanges();
  };

  /**
   * Handle click event.
   * @param {MouseEvent} e - The mouse event.
   */
  const onClick = (e) => {
    onMouseLeave(e);
    if (e.altKey) {
      setEditable(true);
      labelRef.current?.select();
      return;
    }
    if (hasFocus || __legacyHasFocus) return;
    if (SIPDisabled && !spacesDisplay.switchSpacesWithoutYabai) {
      Yabai.goToSpace(index);
      Utils.clickEffect(e);
      return;
    }
    Utils.switchSpace(currentSpaceIndex, index);
    Utils.clickEffect(e);
  };

  /**
   * Handle right-click event.
   */
  const onRightClick = () => {
    setHovered(true);
    setNoDelay(true);
  };

  /**
   * Handle change event for the space label input.
   * @param {Event} e - The change event.
   */
  const onChange = (e) => {
    if (!editable) return;
    const newLabel = e.target.value;
    setSpaceLabel(newLabel);
    Yabai.renameSpace(index, newLabel);
  };

  const { nonStickyWindows: apps, stickyWindows } =
    Utils.stickyWindowWorkaround({
      windows,
      uniqueApps: hideDuplicateAppsInSpaces,
      currentDisplay: display,
      currentSpace: index,
      exclusions,
      titleExclusions,
      exclusionsAsRegex,
    });
  const allApps = [...apps, ...stickyWindows];

  // Determine if the space should be hidden
  const hidden =
    !(hasFocus ?? __legacyHasFocus) &&
    !(isVisible ?? __legacyHasFocus) &&
    !allApps.length &&
    spacesDisplay.hideEmptySpaces;

  if (hidden) return null;

  const classes = Utils.classNames(`space space--${type}`, {
    "space--focused": hasFocus ?? __legacyHasFocus,
    "space--visible": isVisible ?? __legacyIsVisible,
    "space--fullscreen": isNativeFullscreen ?? __legacyIsNativeFullscreen,
    "space--hovered": hovered,
    "space--no-delay": noDelay,
    "space--empty": allApps.length,
    "space--editable": editable,
  });

  const labelSize = (
    typeof spaceLabel === "number" ? spaceLabel.toString() : spaceLabel
  ).length;

  return (
    <React.Fragment>
      {displayAllSpacesOnAllScreens && lastOfSpace && (
        <div className="spaces__separator" />
      )}
      <div
        className={classes}
        onMouseLeave={onMouseLeave}
        onMouseEnter={onMouseEnter}
      >
        <button
          className="space__inner"
          onClick={onClick}
          onContextMenu={onRightClick}
        >
          <input
            ref={labelRef}
            type="text"
            className="space__label"
            onChange={onChange}
            value={spaceLabel}
            style={{ width: `${labelSize}ch` }}
            readOnly={!editable}
          />
          <OpenedApps apps={displayStickyWindowsSeparately ? apps : allApps} />
        </button>
        {SIPDisabled && <SpaceOptions index={index} setHovered={setHovered} />}
      </div>
    </React.Fragment>
  );
}