-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add electron support for AutoResizerRootView
- Loading branch information
1 parent
8ecd9af
commit 684b4cc
Showing
3 changed files
with
73 additions
and
3 deletions.
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
apps/menu-bar/electron/modules/AutoResizerRootViewManager/main.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { BrowserWindow } from 'electron'; | ||
|
||
async function setPopoverSize(width: number, height: number, event: Electron.IpcMainInvokeEvent) { | ||
for (const window of BrowserWindow.getAllWindows()) { | ||
if (event.sender === window.webContents) { | ||
window.setSize(width, height, true); | ||
} | ||
} | ||
} | ||
|
||
const AutoResizerRootViewManager: { | ||
name: string; | ||
setPopoverSize: (width: number, height: number, event: any) => void; | ||
} = { | ||
name: 'AutoResizerRootViewManager', | ||
setPopoverSize, | ||
}; | ||
|
||
export default AutoResizerRootViewManager; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 52 additions & 2 deletions
54
apps/menu-bar/src/components/AutoResizerRootView/index.web.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,53 @@ | ||
import { View } from 'react-native'; | ||
import React, { useEffect, useRef } from 'react'; | ||
import { requireElectronModule } from 'react-native-electron-modules/build/requireElectronModule'; | ||
|
||
export default View; | ||
export const AutoResizerRootViewManager = requireElectronModule<{ | ||
setPopoverSize: (width: number, height: number) => void; | ||
}>('AutoResizerRootViewManager'); | ||
|
||
const AutoResizerRootView = ({ | ||
maxRelativeHeight, | ||
enabled, | ||
children, | ||
style, | ||
}: { | ||
enabled: boolean; | ||
maxRelativeHeight: number; | ||
} & React.DetailedHTMLProps<React.HTMLAttributes<HTMLDivElement>, HTMLDivElement>) => { | ||
const divRef = useRef<HTMLDivElement>(null); | ||
|
||
useEffect(() => { | ||
if (!divRef.current) { | ||
return; | ||
} | ||
|
||
const observer = new ResizeObserver((entries) => { | ||
const { width, height } = entries[0].contentRect; | ||
|
||
if (!enabled || !height) { | ||
return; | ||
} | ||
|
||
const screenHeight = window.screen.height; | ||
const maxHeight = screenHeight * maxRelativeHeight; | ||
|
||
const newHeight = height <= maxHeight ? height : maxHeight; | ||
|
||
AutoResizerRootViewManager.setPopoverSize(width, Math.round(newHeight)); | ||
}); | ||
|
||
observer.observe(divRef.current); | ||
|
||
return () => { | ||
observer.unobserve(); | ||
}; | ||
}, [enabled, maxRelativeHeight]); | ||
|
||
return ( | ||
<div ref={divRef} style={{ height: 'fit-content', ...style }}> | ||
{children} | ||
</div> | ||
); | ||
}; | ||
|
||
export default AutoResizerRootView; |