Skip to content

Commit 7a20cce

Browse files
committed
Init app windows in centre of screen
1 parent ec7c74a commit 7a20cce

4 files changed

Lines changed: 29 additions & 6 deletions

File tree

src/components/BorderedApp/BorderedApp.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React, { useRef } from "react";
22
import useWindowManagerStore, {
33
BaseProps,
44
} from "../../stores/windowManagerStore";
5-
import { Dimensions } from "../../hooks/useDragToResize";
5+
import { Dimensions, Position } from "../../hooks/useDragToResize";
66
import BorderedAppMenu from "./BorderedAppMenu/BorderedAppMenu";
77
import usePositionableElement from "../../hooks/usePositionableElement";
88
import useSystemSettings from "../../stores/systemSettingsStore";
@@ -27,6 +27,7 @@ interface BorderedAppProps extends BaseProps {
2727
type: string;
2828
id: string;
2929
initialDimensions: Dimensions;
30+
initialPosition: Position;
3031
maxDimensions?: Dimensions;
3132
minDimensions?: Dimensions;
3233
menus?: Array<MenuItemProps>;
@@ -38,6 +39,7 @@ function BorderedApp({
3839
id,
3940
children,
4041
initialDimensions,
42+
initialPosition,
4143
minDimensions = { height: 350, width: 350 },
4244
menus,
4345
zIndex,
@@ -64,6 +66,7 @@ function BorderedApp({
6466
} = usePositionableElement({
6567
elementRef: appRef,
6668
minDimensions,
69+
initialPosition,
6770
windowType: type,
6871
windowId: id,
6972
});
@@ -78,6 +81,7 @@ function BorderedApp({
7881
ref={appRef}
7982
onMouseDown={() => winMan.focusWindow(type, id)}
8083
initialDimensions={initialDimensions}
84+
initialPosition={initialPosition}
8185
zIndex={zIndex}
8286
backgroundColor={settings.mainColor}
8387
display={hidden === true ? "none" : "grid"}

src/components/BorderedApp/styles.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import styled from "@emotion/styled";
2-
import { Dimensions } from "../../hooks/useDragToResize";
2+
import { Dimensions, Position } from "../../hooks/useDragToResize";
33

44
interface StyledBorderedAppProps {
55
initialDimensions: Dimensions;
6+
initialPosition: Position;
67
zIndex: number | undefined;
78
backgroundColor: string;
89
display: "none" | "grid";
@@ -25,6 +26,8 @@ export const StyledBorderedApp = styled.div<StyledBorderedAppProps>`
2526
z-index: ${(props) => props.zIndex};
2627
background-color: ${(props) => props.backgroundColor};
2728
display: ${(props) => props.display};
29+
left: ${(props) => props.initialPosition.x}px;
30+
top: ${(props) => props.initialPosition.y}px;
2831
`;
2932
export const StyledCorner = styled.div<{
3033
location: "ne" | "se" | "sw" | "nw";

src/components/BottomBar/Launcher/Launcher.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,27 @@ function Launcher({
3434

3535
function addWindow() {
3636
const id = windowId ?? uuid();
37+
const boundingRect = winMan.contentRef.current?.getBoundingClientRect();
38+
function getInitialPosition(axis: "x" | "y"): number {
39+
if (!boundingRect) return 0;
40+
const dimension = axis === "x" ? "width" : "height";
41+
return (
42+
(boundingRect[axis] ?? 0) +
43+
(boundingRect[dimension] ?? 0) / 2 -
44+
initialDimensions[dimension] / 2
45+
);
46+
}
3747
winMan.addWindow(windowType, id, {
3848
component: BorderedApp,
3949
props: {
4050
id,
4151
title: WindowTitle,
4252
type: windowType,
4353
initialDimensions,
54+
initialPosition: {
55+
x: getInitialPosition("x"),
56+
y: getInitialPosition("y"),
57+
},
4458
menus,
4559
},
4660
key: id,

src/hooks/usePositionableElement.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useEffect, useRef } from "react";
2-
import useDragToResize, { Dimensions, Rect } from "./useDragToResize";
2+
import useDragToResize, { Dimensions, Position, Rect } from "./useDragToResize";
33
import useDragToMove from "./useDragToMove";
44
import useWindowMinMax from "./useWindowMinMax";
55

@@ -8,6 +8,7 @@ interface UsePositionableElementProps {
88
minDimensions: Dimensions;
99
windowType: string;
1010
windowId: string;
11+
initialPosition: Position;
1112
}
1213

1314
/**
@@ -21,6 +22,7 @@ function usePositionableElement({
2122
minDimensions,
2223
windowType,
2324
windowId,
25+
initialPosition,
2426
}: UsePositionableElementProps) {
2527
// Hold a single ref for the elements rect,
2628
// so we dont have to call getBoundingClientRect every
@@ -40,11 +42,11 @@ function usePositionableElement({
4042
elementRect.current = {
4143
height: rect.height,
4244
width: rect.width,
43-
left: rect.left,
44-
top: rect.top,
45+
left: initialPosition.x,
46+
top: initialPosition.y,
4547
};
4648
}
47-
}, [elementRef]);
49+
}, [elementRef, initialPosition.x, initialPosition.y]);
4850

4951
// The resize hook allows the app to be resized
5052
// by dragging the corners or edges of the element.

0 commit comments

Comments
 (0)