Repository: manusa/electronim (js)

Issue:
Do not detect some windows resize correctly on Linux
Resizing works when you drag the corner of the window: the content is correctly updated with the new resize. But when the resize is done by turning full screen a window, it is not detected.


Pull request:
🐛 resize/maximize works properly on Linux
Fixes #78 

Patch:
diff --git a/src/main/index.js b/src/main/index.js
index d3d4212..71149a0 100644
--- a/src/main/index.js
+++ b/src/main/index.js
@@ -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);
@@ -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();


Test patch:
diff --git a/src/main/__tests__/index.test.js b/src/main/__tests__/index.test.js
index a93ee2a..9e6d593 100644
--- a/src/main/__tests__/index.test.js
+++ b/src/main/__tests__/index.test.js
@@ -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) => {
@@ -49,7 +51,6 @@ describe('Main module test suite', () => {
     };
     mockTabContainer = {};
     mockSettings = {};
-    jest.resetModules();
     jest.mock('electron', () => ({
       BrowserWindow: jest.fn(() => mockBrowserWindow),
       Notification: jest.fn(() => mockNotification),
@@ -120,9 +121,14 @@ 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
@@ -130,30 +136,37 @@ describe('Main module test suite', () => {
         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})),
@@ -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});