From a48828e7a30f49ddedd28a8106a0753b2b7447c3 Mon Sep 17 00:00:00 2001 From: Nikhil Mohite Date: Tue, 17 Jan 2023 17:18:35 +0530 Subject: [PATCH] Improve performance by removing signal-based zoom-in, zoom-out, etc functionality from the runtime environment. #5723 --- runtime/src/js/pgadmin.js | 155 ++++++++++++++---- web/pgadmin/browser/__init__.py | 86 +--------- .../browser/static/js/MainMenuFactory.js | 29 ++++ web/pgadmin/browser/static/js/browser.js | 2 +- web/pgadmin/browser/static/js/runtime.js | 64 -------- web/pgadmin/static/js/helpers/Menu.js | 2 +- web/pgadmin/static/js/utils.js | 2 +- .../debugger/static/js/DebuggerModule.js | 4 +- .../components/DebuggerArgumentComponent.jsx | 2 +- .../schema_diff/static/js/SchemaDiffModule.js | 2 +- web/webpack.shim.js | 1 - 11 files changed, 157 insertions(+), 192 deletions(-) delete mode 100644 web/pgadmin/browser/static/js/runtime.js diff --git a/runtime/src/js/pgadmin.js b/runtime/src/js/pgadmin.js index 3d9e36082..9e543e607 100644 --- a/runtime/src/js/pgadmin.js +++ b/runtime/src/js/pgadmin.js @@ -100,41 +100,7 @@ function startDesktopMode() { pgadminServerProcess.stderr.setEncoding('utf8'); pgadminServerProcess.stderr.on('data', (chunk) => { - if (chunk.indexOf('Runtime Open Configuration') > -1) { - // Create and launch new window and open pgAdmin url - nw.Window.open('src/html/configure.html', { - 'frame': true, - 'width': 600, - 'height': 585, - 'position': 'center', - 'resizable': false, - 'focus': true, - 'show': true, - }); - } else if (chunk.indexOf('Runtime Open View Log') > -1) { - // Create and launch new window and open pgAdmin url - nw.Window.open('src/html/view_log.html', { - 'frame': true, - 'width': 790, - 'height': 425, - 'position': 'center', - 'resizable': false, - 'focus': true, - 'show': true, - }); - } else if (chunk.indexOf('Runtime Zoom In') >= 0) { - misc.zoomIn(); - } else if (chunk.indexOf('Runtime Zoom Out') >= 0) { - misc.zoomOut(); - } else if (chunk.indexOf('Runtime Actual Size') >= 0) { - misc.actualSize(); - } else if (chunk.indexOf('Runtime Toggle Full Screen') >= 0) { - misc.toggleFullScreen(); - } else if (chunk.indexOf('Runtime new window opened') >= 0) { - misc.setZoomLevelForAllWindows(); - } else { - misc.writeServerLog(chunk); - } + misc.writeServerLog(chunk); }); // This function is used to ping the pgAdmin4 server whether it @@ -298,6 +264,7 @@ function launchPgAdminWindow() { pgadminWindow.window.pgAdmin.Browser.Events.on('pgadmin:nw-enable-disable-menu-items', enableDisableMenuItem); pgadminWindow.window.pgAdmin.Browser.Events.on('pgadmin:nw-refresh-menu-item', refreshMenuItems); pgadminWindow.window.pgAdmin.Browser.Events.on('pgadmin:nw-update-checked-menu-item', updateCheckedMenuItem); + pgadminWindow.window.pgAdmin.Browser.Events.on('pgadmin:nw-set-new-window-open-size', setNewWindowSize) // Add Main Menus to native menu. pgadminWindow.window.pgAdmin.Browser.MainMenus.forEach((menu)=> { addMenu(menu) @@ -381,6 +348,10 @@ splashWindow.on('close', function () { misc.cleanupAndQuitApp(); }); +function setNewWindowSize(){ + misc.setZoomLevelForAllWindows(); +} + function addCommonMenus(menu) { let _menu = new gui.Menu(); @@ -403,6 +374,11 @@ function addCommonMenus(menu) { _menu.append(_menuItem); }); + if (menu.name == 'file') { + let runtimeMenu = getRuntimeMenu(); + _menu.append(runtimeMenu); + } + if (menu.menuItems.length == 0) { let _menuItem = new gui.MenuItem({ label: 'No object selected', @@ -429,6 +405,113 @@ function addCommonMenus(menu) { } +function getRuntimeMenu() { + let controlKey = platform() === 'darwin' ? 'cmd' : 'ctrl'; + let fullScreenKey = platform() === 'darwin' ? 'F' : 'F10'; + let subMenus = new gui.Menu(); + let rtmenudt = pgAdminMainScreen.window.pgAdmin.Browser.RUNTIME_MENUS_OPTIONS['runtime'] + let runtimeSubMenus = pgAdminMainScreen.window.pgAdmin.Browser.RUNTIME_MENUS_OPTIONS['runtime']['submenus'] + subMenus.append(new gui.MenuItem({ + label: runtimeSubMenus['configure'].label, + enabled: runtimeSubMenus['configure'].enable, + priority: runtimeSubMenus['configure'].priority, + type: 'normal', + checked: false, + click: function () { + // Create and launch new window and open pgAdmin url + nw.Window.open('src/html/configure.html', { + 'frame': true, + 'width': 600, + 'height': 585, + 'position': 'center', + 'resizable': false, + 'focus': true, + 'show': true, + }); + }, + })); + subMenus.append(new gui.MenuItem({ + label: runtimeSubMenus['view_log'].label, + enabled: runtimeSubMenus['view_log'].enable, + priority: runtimeSubMenus['view_log'].priority, + type: 'normal', + checked: false, + click: function () { + // Create and launch new window and open pgAdmin url + nw.Window.open('src/html/view_log.html', { + 'frame': true, + 'width': 790, + 'height': 425, + 'position': 'center', + 'resizable': false, + 'focus': true, + 'show': true, + }); + }, + })); + subMenus.append(new nw.MenuItem({ type: 'separator' })); + subMenus.append(new gui.MenuItem({ + label: runtimeSubMenus['enter_full_screen'].label, + enabled: runtimeSubMenus['enter_full_screen'].enable, + priority: runtimeSubMenus['enter_full_screen'].priority, + type: 'normal', + checked: false, + key: runtimeSubMenus['enter_full_screen'].key, + modifiers: runtimeSubMenus['enter_full_screen'].modifiers, + click: function () { + misc.toggleFullScreen(); + }, + })); + subMenus.append(new gui.MenuItem({ + label: runtimeSubMenus['actual_size'].label, + enabled: runtimeSubMenus['actual_size'].enable, + priority: runtimeSubMenus['actual_size'].priority, + type: 'normal', + checked: false, + key: runtimeSubMenus['actual_size'].key, + modifiers: runtimeSubMenus['actual_size'].modifiers, + click: function () { + misc.actualSize(); + }, + })); + subMenus.append(new gui.MenuItem({ + label: runtimeSubMenus['zoom_in'].label, + enabled: runtimeSubMenus['zoom_in'].enable, + priority: runtimeSubMenus['zoom_in'].priority, + type: 'normal', + checked: false, + key: runtimeSubMenus['zoom_in'].key, + modifiers: runtimeSubMenus['zoom_in'].modifiers, + click: function () { + misc.zoomIn(); + }, + })); + subMenus.append(new gui.MenuItem({ + label: runtimeSubMenus['zoom_out'].label, + enabled: runtimeSubMenus['zoom_out'].enable, + priority: runtimeSubMenus['zoom_out'].priority, + type: 'normal', + checked: false, + key: runtimeSubMenus['zoom_out'].key, + modifiers: runtimeSubMenus['zoom_out'].modifiers, + click: function () { + misc.zoomOut(); + }, + })); + + let runtimeMenu = new gui.MenuItem({ + label: rtmenudt.label, + enabled: true, + priority: rtmenudt.priority, + type: 'normal', + checked: false, + submenu: subMenus, +}) + + return runtimeMenu; + +} + function getSubMenu(menuItem) { let submenu = new gui.Menu(); if (menuItem.menu_items) { @@ -485,6 +568,8 @@ function addMacMenu(menu) { }), indx); indx++; }); + let runtimeMenu = getRuntimeMenu(); + rootMenu.insert(runtimeMenu, indx++); let separator_menu = new nw.MenuItem({ type: 'separator' }); rootMenu.insert(separator_menu, indx); indx++; diff --git a/web/pgadmin/browser/__init__.py b/web/pgadmin/browser/__init__.py index 481c6ef51..37a8ec77f 100644 --- a/web/pgadmin/browser/__init__.py +++ b/web/pgadmin/browser/__init__.py @@ -135,66 +135,6 @@ class BrowserModule(PgAdminModule): ] } - # We need 'Configure...' and 'View log...' Menu only in runtime. - if current_app.PGADMIN_RUNTIME: - full_screen_label = gettext('Enter Full Screen (F10)') - actual_size_label = gettext('Actual Size (Ctrl 0)') - zoom_in_label = gettext('Zoom In (Ctrl +)') - zoom_out_label = gettext('Zoom Out (Ctrl -)') - - if sys.platform == 'darwin': - full_screen_label = gettext('Enter Full Screen (Cmd Ctrl F)') - actual_size_label = gettext('Actual Size (Cmd 0)') - zoom_in_label = gettext('Zoom In (Cmd +)') - zoom_out_label = gettext('Zoom Out (Cmd -)') - - menus['file_items'].append( - MenuItem( - name='mnu_runtime', - module=PGADMIN_BROWSER, - label=gettext('Runtime'), - priority=999, - menu_items=[MenuItem( - name='mnu_configure_runtime', - module=PGADMIN_BROWSER, - callback='mnu_configure_runtime', - priority=0, - label=gettext('Configure...') - ), MenuItem( - name='mnu_viewlog_runtime', - module=PGADMIN_BROWSER, - callback='mnu_viewlog_runtime', - priority=1, - label=gettext('View log...'), - below=True, - ), MenuItem( - name='mnu_toggle_fullscreen_runtime', - module=PGADMIN_BROWSER, - callback='mnu_toggle_fullscreen_runtime', - priority=2, - label=full_screen_label - ), MenuItem( - name='mnu_actual_size_runtime', - module=PGADMIN_BROWSER, - callback='mnu_actual_size_runtime', - priority=3, - label=actual_size_label - ), MenuItem( - name='mnu_zoomin_runtime', - module=PGADMIN_BROWSER, - callback='mnu_zoomin_runtime', - priority=4, - label=zoom_in_label - ), MenuItem( - name='mnu_zoomout_runtime', - module=PGADMIN_BROWSER, - callback='mnu_zoomout_runtime', - priority=5, - label=zoom_out_label - )] - ) - ) - return menus def register_preferences(self): @@ -211,7 +151,7 @@ class BrowserModule(PgAdminModule): 'browser.set_master_password', 'browser.reset_master_password', 'browser.lock_layout', - 'browser.signal_runtime'] + ] def register(self, app, options): """ @@ -907,30 +847,6 @@ def lock_layout(): return make_json_response() -@blueprint.route("/signal_runtime", endpoint="signal_runtime", - methods=["POST"]) -def signal_runtime(): - # If not runtime then no need to send signal - if current_app.PGADMIN_RUNTIME: - data = None - - if hasattr(request.data, 'decode'): - data = request.data.decode('utf-8') - - if data != '': - data = json.loads(data) - - # Add Info Handler to current app just to send signal to runtime - tmp_handler = logging.StreamHandler() - tmp_handler.setLevel(logging.INFO) - current_app.logger.addHandler(tmp_handler) - # Send signal to runtime - current_app.logger.info(data['command']) - # Remove the temporary handler - current_app.logger.removeHandler(tmp_handler) - - return make_json_response() - # Only register route if SECURITY_CHANGEABLE is set to True # We can't access app context here so cannot # use app.config['SECURITY_CHANGEABLE'] diff --git a/web/pgadmin/browser/static/js/MainMenuFactory.js b/web/pgadmin/browser/static/js/MainMenuFactory.js index e0f1df1b0..7d26c1550 100644 --- a/web/pgadmin/browser/static/js/MainMenuFactory.js +++ b/web/pgadmin/browser/static/js/MainMenuFactory.js @@ -12,6 +12,8 @@ import Menu, { MenuItem } from '../../../static/js/helpers/Menu'; import getApiInstance from '../../../static/js/api_instance'; import url_for from 'sources/url_for'; import Notifier from '../../../static/js/helpers/Notifier'; +import { getBrowser } from '../../../static/js/utils'; +import { isMac } from '../../../static/js/keyboard_shortcuts'; const MAIN_MENUS = [ { label: gettext('File'), name: 'file', id: 'mnu_file', index: 0, addSepratior: true }, @@ -20,6 +22,33 @@ const MAIN_MENUS = [ { label: gettext('Help'), name: 'help', id: 'mnu_help', index: 5, addSepratior: false } ]; +let { name: browser } = getBrowser(); +if (browser == 'Nwjs') { + let controlKey = isMac() ? 'cmd' : 'ctrl'; + let fullScreenKey = isMac() ? 'F' : 'F10'; + + const RUNTIME_MENUS_OPTIONS = { + runtime : { + label: gettext('Runtime'), + name: 'runtime', + priority: 999, + submenus: { + configure: { label: gettext('Configure...'), name: 'configure', priority: 0, enable: true}, + view_log: { label: gettext('View log...'), name: 'view_log', priority: 1, enable: true}, + enter_full_screen: { label: gettext('Enter Full Screen'), name: 'enter_full_screen', enable: true, priority: 2, key: fullScreenKey, modifiers: isMac() ?`${controlKey}+ctrl` : controlKey}, + actual_size: { label: gettext('Actual Size'), name: 'actual_size', priority: 3, enable: true, key: '0', modifiers: controlKey}, + zoom_in: { label: gettext('Zoom In'), name: 'zoom_in', priority: 4, enable: true, key: '+', modifiers: controlKey}, + zoom_out: { label: gettext('Zoom Out'), name: 'zoom_out', enable: true, priority: 5, key: '-', modifiers: controlKey}, + } + } + }; + + pgAdmin.Browser.RUNTIME_MENUS_OPTIONS = RUNTIME_MENUS_OPTIONS; +} + + + + export default class MainMenuFactory { static createMainMenus() { pgAdmin.Browser.MainMenus = []; diff --git a/web/pgadmin/browser/static/js/browser.js b/web/pgadmin/browser/static/js/browser.js index 293209c08..6410b5bf3 100644 --- a/web/pgadmin/browser/static/js/browser.js +++ b/web/pgadmin/browser/static/js/browser.js @@ -25,7 +25,7 @@ define('pgadmin.browser', [ 'pgadmin.browser.utils', 'wcdocker', 'jquery.contextmenu', 'pgadmin.browser.preferences', 'pgadmin.browser.messages', 'pgadmin.browser.panel', 'pgadmin.browser.layout', - 'pgadmin.browser.runtime', 'pgadmin.browser.error', 'pgadmin.browser.frame', + 'pgadmin.browser.error', 'pgadmin.browser.frame', 'pgadmin.browser.node', 'pgadmin.browser.collection', 'pgadmin.browser.activity', 'sources/codemirror/addon/fold/pgadmin-sqlfoldcode', 'pgadmin.browser.keyboard', 'sources/tree/pgadmin_tree_save_state' diff --git a/web/pgadmin/browser/static/js/runtime.js b/web/pgadmin/browser/static/js/runtime.js deleted file mode 100644 index 9c52e426c..000000000 --- a/web/pgadmin/browser/static/js/runtime.js +++ /dev/null @@ -1,64 +0,0 @@ -///////////////////////////////////////////////////////////// -// -// pgAdmin 4 - PostgreSQL Tools -// -// Copyright (C) 2013 - 2023, The pgAdmin Development Team -// This software is released under the PostgreSQL Licence -// -////////////////////////////////////////////////////////////// - -import pgAdmin from 'sources/pgadmin'; -import url_for from 'sources/url_for'; -import $ from 'jquery'; -import gettext from 'sources/gettext'; -import Notify from '../../../static/js/helpers/Notifier'; - -const pgBrowser = pgAdmin.Browser = pgAdmin.Browser || {}; - -_.extend(pgBrowser, { - // This function is used to send signal to runtime. - send_signal_to_runtime: function(cmd_string) { - $.ajax({ - url: url_for('browser.signal_runtime'), - method: 'POST', - contentType: 'application/json', - data: JSON.stringify({ - 'command': cmd_string, - }), - }).fail(function(xhr, error) { - Notify.pgNotifier(error, xhr, gettext('Failed to send signal to runtime.')); - }); - }, - - // This function is callback function when 'Configure...' menu is clicked. - mnu_configure_runtime: function() { - this.send_signal_to_runtime('Runtime Open Configuration'); - }, - - // This function is callback function when 'View log...' menu is clicked. - mnu_viewlog_runtime: function() { - this.send_signal_to_runtime('Runtime Open View Log'); - }, - - // This function is callback function when 'Enter Full Screen' menu is clicked. - mnu_toggle_fullscreen_runtime: function() { - this.send_signal_to_runtime('Runtime Toggle Full Screen'); - }, - - // This function is callback function when 'Actual Size' menu is clicked. - mnu_actual_size_runtime: function() { - this.send_signal_to_runtime('Runtime Actual Size'); - }, - - // This function is callback function when 'Zoom In' menu is clicked. - mnu_zoomin_runtime: function() { - this.send_signal_to_runtime('Runtime Zoom In'); - }, - - // This function is callback function when 'Zoom Out' menu is clicked. - mnu_zoomout_runtime: function() { - this.send_signal_to_runtime('Runtime Zoom Out'); - } -}); - -export {pgBrowser}; diff --git a/web/pgadmin/static/js/helpers/Menu.js b/web/pgadmin/static/js/helpers/Menu.js index 83bfc61c4..9fac9034b 100644 --- a/web/pgadmin/static/js/helpers/Menu.js +++ b/web/pgadmin/static/js/helpers/Menu.js @@ -110,7 +110,7 @@ export class MenuItem { let menu_opts = [ 'name', 'label', 'priority', 'module', 'callback', 'data', 'enable', 'category', 'target', 'url', 'node', - 'checked', 'below', 'menu_items', 'is_checkbox', 'action', 'applies', 'is_native_only', 'type' + 'checked', 'below', 'menu_items', 'is_checkbox', 'action', 'applies', 'is_native_only', 'type', ]; let defaults = { url: '#', diff --git a/web/pgadmin/static/js/utils.js b/web/pgadmin/static/js/utils.js index ec0b88947..d0f5629e9 100644 --- a/web/pgadmin/static/js/utils.js +++ b/web/pgadmin/static/js/utils.js @@ -696,7 +696,7 @@ function openWindow(toolForm, title) { let pgBrowser = window.pgAdmin.Browser; // Send the signal to runtime, so that proper zoom level will be set. setTimeout(function() { - pgBrowser.send_signal_to_runtime('Runtime new window opened'); + pgBrowser.Events.trigger('pgadmin:nw-set-new-window-open-size'); }, 500); } else { return false; diff --git a/web/pgadmin/tools/debugger/static/js/DebuggerModule.js b/web/pgadmin/tools/debugger/static/js/DebuggerModule.js index 7a6750af7..52f799f0e 100644 --- a/web/pgadmin/tools/debugger/static/js/DebuggerModule.js +++ b/web/pgadmin/tools/debugger/static/js/DebuggerModule.js @@ -400,7 +400,7 @@ export default class DebuggerModule { window.open(url, '_blank'); // Send the signal to runtime, so that proper zoom level will be set. setTimeout(function () { - self.pgBrowser.send_signal_to_runtime('Runtime new window opened'); + self.pgBrowser.Events.trigger('pgadmin:nw-set-new-window-open-size'); }, 500); } else { self.pgBrowser.Events.once( @@ -582,7 +582,7 @@ export default class DebuggerModule { window.open(url, '_blank'); // Send the signal to runtime, so that proper zoom level will be set. setTimeout(function () { - self.pgBrowser.send_signal_to_runtime('Runtime new window opened'); + self.pgBrowser.Browser.Events.trigger('pgadmin:nw-set-new-window-open-size'); }, 500); } else { self.pgBrowser.Events.once( diff --git a/web/pgadmin/tools/debugger/static/js/components/DebuggerArgumentComponent.jsx b/web/pgadmin/tools/debugger/static/js/components/DebuggerArgumentComponent.jsx index 383605bc5..b9607fff3 100644 --- a/web/pgadmin/tools/debugger/static/js/components/DebuggerArgumentComponent.jsx +++ b/web/pgadmin/tools/debugger/static/js/components/DebuggerArgumentComponent.jsx @@ -722,7 +722,7 @@ export default function DebuggerArgumentComponent({ debuggerInfo, restartDebug, window.open(url, '_blank'); // Send the signal to runtime, so that proper zoom level will be set. setTimeout(function () { - pgAdmin.Browser.send_signal_to_runtime('Runtime new window opened'); + pgAdmin.Browser.Events.trigger('pgadmin:nw-set-new-window-open-size'); }, 500); } else { pgAdmin.Browser.Events.once( diff --git a/web/pgadmin/tools/schema_diff/static/js/SchemaDiffModule.js b/web/pgadmin/tools/schema_diff/static/js/SchemaDiffModule.js index b0ec02d6e..b843348f7 100644 --- a/web/pgadmin/tools/schema_diff/static/js/SchemaDiffModule.js +++ b/web/pgadmin/tools/schema_diff/static/js/SchemaDiffModule.js @@ -120,7 +120,7 @@ export default class SchemaDiff { window.open(baseUrl, '_blank'); // Send the signal to runtime, so that proper zoom level will be set. setTimeout(function () { - this.pgBrowser.send_signal_to_runtime('Runtime new window opened'); + self.pgBrowser.Events.trigger('pgadmin:nw-set-new-window-open-size'); }, 500); } else { this.pgBrowser.Events.once( diff --git a/web/webpack.shim.js b/web/webpack.shim.js index a5e8580ea..797fd7248 100644 --- a/web/webpack.shim.js +++ b/web/webpack.shim.js @@ -128,7 +128,6 @@ let webpackShimConfig = { 'pgadmin.browser.frame': path.join(__dirname, './pgadmin/browser/static/js/frame'), 'pgadmin.browser.keyboard': path.join(__dirname, './pgadmin/browser/static/js/keyboard'), 'pgadmin.browser.layout': path.join(__dirname, './pgadmin/browser/static/js/layout'), - 'pgadmin.browser.runtime': path.join(__dirname, './pgadmin/browser/static/js/runtime'), 'pgadmin.browser.preferences': path.join(__dirname, './pgadmin/browser/static/js/preferences'), 'pgadmin.browser.activity': path.join(__dirname, './pgadmin/browser/static/js/activity'), 'pgadmin.browser.messages': '/browser/js/messages',