Skip to content

Commit

Permalink
Added script “toggleWinMax”
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Hund committed Feb 8, 2023
1 parent 39185c3 commit 6c0bbd1
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 7 deletions.
12 changes: 10 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,16 @@
"name": "better-touch-tool-scripts",
"packageManager": "yarn@3.4.1",
"version": "0.0.1",
"source": "src/getScreenWindowDisplayInfo.mjs",
"main": "dist/getScreenWindowDisplayInfo.js",
"targets": {
"getScreenWindowDisplayInfo": {
"source": "src/getScreenWindowDisplayInfo.mjs",
"distDir": "dist"
},
"toggleWinMax": {
"source": "src/toggleWinMax.mjs",
"distDir": "dist"
}
},
"scripts": {
"build": "parcel build"
},
Expand Down
17 changes: 17 additions & 0 deletions src/lib/config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export const nickNames = {
"DELL U3419W": "dell",
"Color LCD": "hinkel",
"Sidecar Display": "ipad",
};

export const stageManagerMarginWidth = 70;

export const menuBarHeight = {
normal: 25,
notch: 38,
};

export const hinkelScreenDimensions = {
width: 1728,
height: 1117,
};
36 changes: 36 additions & 0 deletions src/lib/focusedWindowIsMaximised.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import getFocusedScreenInfo from "./getFocusedScreenInfo.mjs";
import getMenuBarHeight from "./getMenuBarHeight.mjs";
import { stageManagerMarginWidth } from "./config.mjs";
import getFocusedWindowInfo from "./getFocusedWindowInfo.mjs";

export default async function focusedWindowIsMaximised() {
const { focusedScreenX, focusedScreenY, focusedScreenWidth, focusedScreenHeight } =
await getFocusedScreenInfo();
const { focusedWindowX, focusedWindowY, focusedWindowWidth, focusedWindowHeight } =
await getFocusedWindowInfo();
const menuBarHeight = getMenuBarHeight(focusedScreenWidth, focusedScreenHeight);

const isInUpperLeftCorner =
focusedWindowX === focusedScreenX + stageManagerMarginWidth &&
focusedWindowY === focusedScreenY + menuBarHeight;
const isMaximumSize =
focusedWindowWidth === focusedScreenWidth - stageManagerMarginWidth &&
focusedWindowHeight === focusedScreenHeight - menuBarHeight;
const isMaximised = isInUpperLeftCorner && isMaximumSize;

BTTLog(`focusedScreenX: ${focusedScreenX}`);
BTTLog(`focusedScreenY: ${focusedScreenY}`);
BTTLog(`focusedScreenWidth: ${focusedScreenWidth}`);
BTTLog(`focusedScreenHeight: ${focusedScreenHeight}`);
BTTLog(`focusedWindowX: ${focusedWindowX}`);
BTTLog(`focusedWindowY: ${focusedWindowY}`);
BTTLog(`focusedWindowWidth: ${focusedWindowWidth}`);
BTTLog(`focusedWindowHeight: ${focusedWindowHeight}`);
BTTLog(`menuBarHeight: ${menuBarHeight}`);
BTTLog(`stageManagerWidth: ${stageManagerMarginWidth}`);
BTTLog(`isInUpperLeftCorner: ${isInUpperLeftCorner}`);
BTTLog(`isMaximumSize: ${isMaximumSize}`);
BTTLog(`isMaximised: ${isMaximised}`);

return isMaximised;
}
7 changes: 2 additions & 5 deletions src/lib/getDisplaysInfo.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import{nickNames} from "./config.mjs";

function parsePixels({ _spdisplays_pixels, _spdisplays_resolution }) {
let width, height;
const regex = /^(\d+) x (\d+)/;
Expand All @@ -16,11 +18,6 @@ function parsePixels({ _spdisplays_pixels, _spdisplays_resolution }) {
}

export default async function getDisplaysInfo() {
const nickNames = {
"DELL U3419W": "dell",
"Color LCD": "hinkel",
"Sidecar Display": "ipad",
};

const displayDataString = await runShellScript({
script: "system_profiler SPDisplaysDataType -json",
Expand Down
25 changes: 25 additions & 0 deletions src/lib/getFocusedWindowInfo.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
export default async function getFocusedWindowInfo() {
const appleScript = `
tell application "System Events"
set appOfInterest to name of application processes whose frontmost is true
set currentApplication to item 1 of appOfInterest
set firstWindow to the first window of application process currentApplication
set windowDimensions to size of firstWindow
set windowPosition to position of firstWindow
end tell
return "[" & (item 1 of windowPosition as text) & "," & (item 2 of windowPosition as text) & "," & (item 1 of windowDimensions as text) & "," & (item 2 of windowDimensions as text) & "]"
`;

const resultStr = await runAppleScript(appleScript);
const result = JSON.parse(resultStr);
const [focusedWindowX, focusedWindowY, focusedWindowWidth, focusedWindowHeight] = result.map((str) =>
parseInt(str, 10)
);

return {
focusedWindowX,
focusedWindowY,
focusedWindowWidth,
focusedWindowHeight,
};
}
8 changes: 8 additions & 0 deletions src/lib/getMenuBarHeight.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { hinkelScreenDimensions, menuBarHeight } from "./config.mjs";

export default function getMenuBarHeight(screenWidth, screenHeight) {
if (screenWidth === hinkelScreenDimensions.width && screenHeight === hinkelScreenDimensions.height) {
return menuBarHeight.notch;
}
return menuBarHeight.normal;
}
7 changes: 7 additions & 0 deletions src/toggleWinMax.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import "./lib/setupMocks.mjs";
import focusedWindowIsMaximised from "./lib/focusedWindowIsMaximised.mjs";

(async () => {
const isMaximised = await focusedWindowIsMaximised();
returnToBTT(isMaximised ? "focused window is maximised" : "focused window is not maximised");
})();

0 comments on commit 6c0bbd1

Please sign in to comment.