Skip to content

Commit

Permalink
feat(compact mode): prevent user from resizing the window
Browse files Browse the repository at this point in the history
  • Loading branch information
roldanjr authored and sekwah41 committed Dec 20, 2022
1 parent b142a47 commit dd69232
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 19 deletions.
2 changes: 1 addition & 1 deletion app/main/src/helpers/globalShortcuts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export function activateFullScreenShortcuts(
);
}

export function deactivateFullScreenSchortcuts() {
export function deactivateFullScreenShortcuts() {
globalShortcut.unregister(EXIT_SHORTCUTS.ESCAPE);
globalShortcut.unregister(EXIT_SHORTCUTS.QUIT);
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describe("Fullscreen break", () => {
{
win: window,
contextMenu: Menu.buildFromTemplate([{ label: "Mock Label" }]),
fullscreenState,
isFullscreen: fullscreenState.isFullscreen,
trayTooltip,
tray,
}
Expand Down Expand Up @@ -105,7 +105,7 @@ describe("Fullscreen break", () => {
{
win: window,
contextMenu: Menu.buildFromTemplate([{ label: "Mock Label" }]),
fullscreenState,
isFullscreen: fullscreenState.isFullscreen,
trayTooltip,
tray,
}
Expand Down
17 changes: 8 additions & 9 deletions app/main/src/lifecycleEventHandlers/fullScreenBreak.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
activateFullScreenShortcuts,
deactivateFullScreenSchortcuts,
deactivateFullScreenShortcuts,
} from "../helpers";
import { BrowserWindow, Menu, Tray } from "electron";

Expand All @@ -18,14 +18,14 @@ type AppArgs = {
trayTooltip: string;
win: BrowserWindow | null;
contextMenu: Menu;
fullscreenState: FullscreenState;
isFullscreen: FullscreenState["isFullscreen"];
};

const setFullScreen = (
flag: boolean,
alwaysOnTop: boolean,
win: BrowserWindow | null,
fullscreenState: FullscreenState
isFullscreen: FullscreenState["isFullscreen"]
) => {
win?.setFullScreenable(true);
win?.setAlwaysOnTop(alwaysOnTop, "screen-saver");
Expand All @@ -34,7 +34,7 @@ const setFullScreen = (
win?.show();
win?.focus();

fullscreenState.isFullscreen = flag;
isFullscreen = flag;
};

/**
Expand All @@ -48,11 +48,10 @@ export const setFullscreenBreakHandler = (
appArgs: AppArgs
) => {
const { shouldFullscreen, alwaysOnTop } = fullscreenArgs;
const { tray, trayTooltip, win, contextMenu, fullscreenState } =
appArgs;
const { tray, trayTooltip, win, contextMenu, isFullscreen } = appArgs;

if (shouldFullscreen) {
setFullScreen(true, alwaysOnTop, win, fullscreenState);
setFullScreen(true, alwaysOnTop, win, isFullscreen);

activateFullScreenShortcuts(() => {});

Expand All @@ -65,9 +64,9 @@ export const setFullscreenBreakHandler = (
])
);
} else {
setFullScreen(false, alwaysOnTop, win, fullscreenState);
setFullScreen(false, alwaysOnTop, win, isFullscreen);

deactivateFullScreenSchortcuts();
deactivateFullScreenShortcuts();
tray?.setToolTip(trayTooltip);
tray?.setContextMenu(contextMenu);
}
Expand Down
34 changes: 27 additions & 7 deletions app/main/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const notificationIcon = path.join(

const trayIcon = path.join(__dirname, "./assets/tray-dark.png");

const onlySingleIntance = app.requestSingleInstanceLock();
const onlySingleInstance = app.requestSingleInstanceLock();

Menu.setApplicationMenu(null);

Expand All @@ -70,7 +70,14 @@ let tray: Tray | null = null;

let win: BrowserWindow | null;

const fullscreenState: FullscreenState = { isFullscreen: false };
type WindowStateProps = {
isOnCompactMode: boolean;
} & FullscreenState;

const windowState: WindowStateProps = {
isFullscreen: false,
isOnCompactMode: false,
};

function createMainWindow() {
win = new BrowserWindow({
Expand Down Expand Up @@ -115,7 +122,7 @@ function createMainWindow() {
if (win) {
const data = await getFromStorage(win, "state");
if (data.settings.minimizeToTray) {
if (!fullscreenState.isFullscreen) {
if (!windowState.isFullscreen) {
win?.hide();
if (tray === null && data.settings.minimizeToTray) {
createSystemTray();
Expand All @@ -132,6 +139,15 @@ function createMainWindow() {
)
);

win.on("leave-full-screen", () => {
// This is a workaround of resize issue after leaving the full screen mode.
if (windowState.isOnCompactMode) {
win?.setSize(340, 100);
} else {
win?.setSize(340, getFrameHeight());
}
});

win.on(
"close",
debounce(
Expand All @@ -143,7 +159,7 @@ function createMainWindow() {
if (!data.settings.closeToTray) {
app.exit();
} else {
if (!fullscreenState.isFullscreen) {
if (!windowState.isFullscreen) {
win?.hide();
if (tray === null && data.settings.closeToTray) {
createSystemTray();
Expand Down Expand Up @@ -201,7 +217,7 @@ function createSystemTray() {
});
}

if (!onlySingleIntance) {
if (!onlySingleInstance) {
app.quit();
} else {
app.on("second-instance", () => {
Expand Down Expand Up @@ -307,21 +323,25 @@ ipcMain.on(SET_ALWAYS_ON_TOP, (e, { alwaysOnTop }) => {

ipcMain.on(SET_FULLSCREEN_BREAK, (e, args) => {
setFullscreenBreakHandler(args, {
win,
tray,
trayTooltip,
fullscreenState,
win,
contextMenu,
isFullscreen: windowState.isFullscreen,
});
});

ipcMain.on(SET_COMPACT_MODE, (e, args) => {
if (args.compactMode) {
win?.setMinimumSize(340, 100);
win?.setSize(340, 100);
win?.setResizable(false);
windowState.isOnCompactMode = true;
} else {
win?.setMinimumSize(340, getFrameHeight());
win?.setSize(340, getFrameHeight());
win?.setResizable(true);
windowState.isOnCompactMode = true;
}
});

Expand Down

0 comments on commit dd69232

Please sign in to comment.