Skip to content

Commit

Permalink
🐛 resize/maximize works properly on Linux
Browse files Browse the repository at this point in the history
Signed-off-by: Marc Nuri <[email protected]>
  • Loading branch information
manusa committed Sep 24, 2022
1 parent 591754d commit 08b3a25
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 29 deletions.
37 changes: 25 additions & 12 deletions src/main/__tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ describe('Main module test suite', () => {
let userAgentModule;
let main;
beforeEach(() => {
jest.resetModules();
jest.useFakeTimers();
mockBrowserWindow = {
listeners: {},
on: jest.fn((eventName, func) => {
Expand All @@ -49,7 +51,6 @@ describe('Main module test suite', () => {
};
mockTabContainer = {};
mockSettings = {};
jest.resetModules();
jest.mock('electron', () => ({
BrowserWindow: jest.fn(() => mockBrowserWindow),
Notification: jest.fn(() => mockNotification),
Expand Down Expand Up @@ -120,40 +121,52 @@ describe('Main module test suite', () => {
});
});
describe('mainWindow events', () => {
let views;
beforeEach(() => {
jest.spyOn(global, 'setTimeout');
views = [];
mockBrowserWindow.getSize = jest.fn(() => ([13, 37]));
mockBrowserWindow.getContentBounds = jest.fn(() => ({x: 0, y: 0, width: 10, height: 34}));
mockBrowserWindow.getBrowserViews = jest.fn(() => (views));
main.init();
});
test('maximize, should set browserview to fit window and store new size in configuration file', () => {
// Given
const singleView = {
getBounds: jest.fn(() => ({x: 0, y: 0, width: 1, height: 1})),
setBounds: jest.fn()
};
mockBrowserWindow.getBrowserViews = jest.fn(() => ([singleView]));
main.init();
views.push(singleView);
// When
mockBrowserWindow.listeners.maximize();
mockBrowserWindow.listeners.maximize({sender: mockBrowserWindow});
jest.runAllTimers();
// Then
expect(settingsModule.updateSettings).toHaveBeenCalledWith({width: 13, height: 37});
expect(singleView.setBounds).toHaveBeenCalledWith({x: 0, y: 0, width: 10, height: 34});
});
describe('resize', () => {
test('single view, should set browserview to fit window and store new size in configuration file', () => {
test('#78: should be run in separate setTimeout timer function to resize properly in Linux', () => {
// When
mockBrowserWindow.listeners.resize({sender: mockBrowserWindow});
// Then
expect(setTimeout).toHaveBeenCalledTimes(1);
expect(mockBrowserWindow.getBrowserViews).not.toHaveBeenCalled();
});
test('single view, should set BrowserView to fit window and store new size in configuration file', () => {
// Given
const singleView = {
getBounds: jest.fn(() => ({x: 0, y: 0, width: 1, height: 1})),
setBounds: jest.fn()
};
mockBrowserWindow.getBrowserViews = jest.fn(() => ([singleView]));
main.init();
views.push(singleView);
// When
mockBrowserWindow.listeners.resize();
mockBrowserWindow.listeners.resize({sender: mockBrowserWindow});
jest.runAllTimers();
// Then
expect(settingsModule.updateSettings).toHaveBeenCalledWith({width: 13, height: 37});
expect(singleView.setBounds).toHaveBeenCalledWith({x: 0, y: 0, width: 10, height: 34});
});
test('multiple views, should set last browserview to fit window and store new size in configuration file', () => {
test('multiple views, should set last BrowserView to fit window and store new size in configuration file', () => {
// Given
const topBar = {
getBounds: jest.fn(() => ({x: 0, y: 0, width: 1, height: 1})),
Expand All @@ -163,10 +176,10 @@ describe('Main module test suite', () => {
getBounds: jest.fn(() => ({x: 1337, y: 1337, width: 1, height: 1})),
setBounds: jest.fn()
};
mockBrowserWindow.getBrowserViews = jest.fn(() => ([topBar, content]));
main.init();
views.push(topBar, content);
// When
mockBrowserWindow.listeners.resize();
mockBrowserWindow.listeners.resize({sender: mockBrowserWindow});
jest.runAllTimers();
// Then
expect(settingsModule.updateSettings).toHaveBeenCalledWith({width: 13, height: 37});
expect(topBar.setBounds).toHaveBeenCalledWith({x: 0, y: 0, width: 10, height: 1});
Expand Down
38 changes: 21 additions & 17 deletions src/main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,27 @@ const activateTab = tabId => {
}
};

const handleMainWindowResize = event => {
const window = event.sender;
const [windowWidth, windowHeight] = window.getSize();
updateSettings({width: windowWidth, height: windowHeight});

setTimeout(() => {
const {width: contentWidth, height: contentHeight} = window.getContentBounds();
let totalHeight = 0;
const isLast = (idx, array) => idx === array.length - 1;
window.getBrowserViews().forEach((bv, idx, array) => {
const {x: currentX, y: currentY, height: currentHeight} = bv.getBounds();
let newHeight = currentHeight;
if (isLast(idx, array)) {
newHeight = contentHeight - totalHeight;
}
bv.setBounds({x: currentX, y: currentY, width: contentWidth, height: newHeight});
totalHeight += currentHeight;
});
});
};

const handleTabReload = event => event.sender.reloadIgnoringCache();

const handleZoomIn = event => event.sender.setZoomFactor(event.sender.getZoomFactor() + 0.1);
Expand Down Expand Up @@ -155,23 +176,6 @@ const browserVersionsReady = () => {
tabContainer = initTabContainer(mainWindow);
};

const handleMainWindowResize = () => {
const [windowWidth, windowHeight] = mainWindow.getSize();
updateSettings({width: windowWidth, height: windowHeight});
const {width: contentWidth, height: contentHeight} = mainWindow.getContentBounds();
let totalHeight = 0;
const isLast = (idx, array) => idx === array.length - 1;
mainWindow.getBrowserViews().forEach((bv, idx, array) => {
const {x: currentX, y: currentY} = bv.getBounds();
let {height: currentHeight} = bv.getBounds();
if (isLast(idx, array)) {
currentHeight = contentHeight - totalHeight;
}
bv.setBounds({x: currentX, y: currentY, width: contentWidth, height: currentHeight});
totalHeight += currentHeight;
});
};

const init = () => {
fixUserDataLocation();
loadDictionaries();
Expand Down

0 comments on commit 08b3a25

Please sign in to comment.