import React from "react"; import { connect } from "react-redux"; import classnames from "classnames"; import "../../../css/gen-window.css"; import { setWindowSize, closeWindow } from "../../actionCreators"; import { getWindowPixelSize } from "../../selectors"; import ResizeTarget from "../ResizeTarget"; import { AppState, WindowId, Dispatch } from "../../types"; import FocusTarget from "../FocusTarget"; interface TextProps { children: string; } const Text = ({ children }: TextProps) => { const letters = children.split(""); return ( {letters.map((letter, i) => (
))} ); }; const CHROME_WIDTH = 19; const CHROME_HEIGHT = 34; interface WindowSize { width: number; height: number; } interface OwnProps { windowId: WindowId; children: (windowSize: WindowSize) => React.ReactNode; title: string; onKeyDown?(e: KeyboardEvent): void; } interface DispatchProps { close: (windowId: WindowId) => void; setGenWindowSize: (windowId: WindowId, size: [number, number]) => void; } interface StateProps { windowSize: [number, number]; selected: boolean; height: number; width: number; } type Props = OwnProps & DispatchProps & StateProps; // Named export for testing export const GenWindow = ({ selected, children, close, title, windowId, windowSize, setGenWindowSize, width, height, onKeyDown, }: Props) => { return (
{title}
close(windowId)} />
{children({ width: width - CHROME_WIDTH, height: height - CHROME_HEIGHT, })}
setGenWindowSize(windowId, size)} id={"gen-resize-target"} />
); }; const mapStateToProps = (state: AppState, ownProps: OwnProps): StateProps => { const { width, height } = getWindowPixelSize(state)(ownProps.windowId); return { width, height, selected: state.windows.focused === ownProps.windowId, windowSize: state.windows.genWindows[ownProps.windowId].size, }; }; const mapDispatchToProps = (dispatch: Dispatch): DispatchProps => { return { close: (windowId: WindowId) => dispatch(closeWindow(windowId)), setGenWindowSize: (windowId: WindowId, size: [number, number]) => dispatch(setWindowSize(windowId, size)), }; }; export default connect(mapStateToProps, mapDispatchToProps)(GenWindow);