All files / main windowing.ts

96% Statements 24/25
83.33% Branches 10/12
100% Functions 7/7
95.45% Lines 21/22

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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      2x                                                                                                                               2x         2x 2x         2x       3x 3x             3x 1x     2x     2x                                             2x 1x 1x 1x   1x 1x     2x 1x 1x 1x    
import { MINI_WINDOW_HEIGHT, MINI_WINDOW_WIDTH } from '@shared/windowMetrics'
 
export { MINI_WINDOW_HEIGHT, MINI_WINDOW_WIDTH }
export const MINI_WINDOW_PADDING = 24
 
interface Point {
  x: number
  y: number
}
 
interface WorkArea extends Point {
  width: number
  height: number
}
 
interface DisplayLike {
  workArea: WorkArea
}
 
interface ResolveMiniWindowPositionOptions {
  savedPosition: Point | null
  displays: DisplayLike[]
}
 
interface MainWindowLike {
  isMinimized(): boolean
  restore(): void
  show(): void
  focus(): void
}
 
interface MiniWindowLike {
  hide(): void
}
 
interface MainWindowHideLike {
  hide(): void
}
 
interface MiniWindowShowLike {
  show(): void
  focus(): void
}
 
interface MiniWindowChromeOptions {
  width: number
  height: number
  minWidth: number
  minHeight: number
  maxWidth: number
  maxHeight: number
  x: number
  y: number
  show: false
  frame: false
  transparent: true
  thickFrame: false
  resizable: false
  minimizable: false
  maximizable: false
  fullscreenable: false
  alwaysOnTop: true
  skipTaskbar: true
  autoHideMenuBar: true
  backgroundColor: '#00000000'
}
 
const resolveDefaultPosition = ({ x, y, width, height }: WorkArea): Point => ({
  x: x + width - MINI_WINDOW_WIDTH - MINI_WINDOW_PADDING,
  y: y + height - MINI_WINDOW_HEIGHT - MINI_WINDOW_PADDING
})
 
const positionFitsWorkArea = (position: Point, workArea: WorkArea): boolean =>
  position.x >= workArea.x &&
  position.y >= workArea.y &&
  position.x + MINI_WINDOW_WIDTH <= workArea.x + workArea.width &&
  position.y + MINI_WINDOW_HEIGHT <= workArea.y + workArea.height
 
export const resolveMiniWindowPosition = ({
  savedPosition,
  displays
}: ResolveMiniWindowPositionOptions): Point => {
  const [primaryDisplay] = displays
  Iif (!primaryDisplay) {
    return {
      x: MINI_WINDOW_PADDING,
      y: MINI_WINDOW_PADDING
    }
  }
 
  if (savedPosition && displays.some((display) => positionFitsWorkArea(savedPosition, display.workArea))) {
    return savedPosition
  }
 
  return resolveDefaultPosition(primaryDisplay.workArea)
}
 
export const createMiniWindowChromeOptions = (position: Point): MiniWindowChromeOptions => ({
  width: MINI_WINDOW_WIDTH,
  height: MINI_WINDOW_HEIGHT,
  minWidth: MINI_WINDOW_WIDTH,
  minHeight: MINI_WINDOW_HEIGHT,
  maxWidth: MINI_WINDOW_WIDTH,
  maxHeight: MINI_WINDOW_HEIGHT,
  x: position.x,
  y: position.y,
  show: false,
  frame: false,
  transparent: true,
  thickFrame: false,
  resizable: false,
  minimizable: false,
  maximizable: false,
  fullscreenable: false,
  alwaysOnTop: true,
  skipTaskbar: true,
  autoHideMenuBar: true,
  backgroundColor: '#00000000'
})
 
export const activateMainWindow = (mainWindow: MainWindowLike, miniWindow?: MiniWindowLike | null): void => {
  miniWindow?.hide()
  Eif (mainWindow.isMinimized()) {
    mainWindow.restore()
  }
  mainWindow.show()
  mainWindow.focus()
}
 
export const activateMiniWindow = (mainWindow: MainWindowHideLike | null, miniWindow: MiniWindowShowLike): void => {
  mainWindow?.hide()
  miniWindow.show()
  miniWindow.focus()
}