github.com/secoba/wails/v2@v2.6.4/internal/frontend/runtime/desktop/main.js (about)

     1  /*
     2   _	   __	  _ __
     3  | |	 / /___ _(_) /____
     4  | | /| / / __ `/ / / ___/
     5  | |/ |/ / /_/ / / (__  )
     6  |__/|__/\__,_/_/_/____/
     7  The electron alternative for Go
     8  (c) Lea Anthony 2019-present
     9  */
    10  /* jshint esversion: 9 */
    11  import * as Log from './log';
    12  import {eventListeners, EventsEmit, EventsNotify, EventsOff, EventsOn, EventsOnce, EventsOnMultiple} from './events';
    13  import {Call, Callback, callbacks} from './calls';
    14  import {SetBindings} from "./bindings";
    15  import * as Window from "./window";
    16  import * as Screen from "./screen";
    17  import * as Browser from "./browser";
    18  import * as Clipboard from "./clipboard";
    19  import * as ContextMenu from "./contextmenu";
    20  
    21  
    22  export function Quit() {
    23      window.WailsInvoke('Q');
    24  }
    25  
    26  export function Show() {
    27      window.WailsInvoke('S');
    28  }
    29  
    30  export function Hide() {
    31      window.WailsInvoke('H');
    32  }
    33  
    34  export function Environment() {
    35      return Call(":wails:Environment");
    36  }
    37  
    38  // The JS runtime
    39  window.runtime = {
    40      ...Log,
    41      ...Window,
    42      ...Browser,
    43      ...Screen,
    44      ...Clipboard,
    45      EventsOn,
    46      EventsOnce,
    47      EventsOnMultiple,
    48      EventsEmit,
    49      EventsOff,
    50      Environment,
    51      Show,
    52      Hide,
    53      Quit
    54  };
    55  
    56  // Internal wails endpoints
    57  window.wails = {
    58      Callback,
    59      EventsNotify,
    60      SetBindings,
    61      eventListeners,
    62      callbacks,
    63      flags: {
    64          disableScrollbarDrag: false,
    65          disableDefaultContextMenu: false,
    66          enableResize: false,
    67          defaultCursor: null,
    68          borderThickness: 6,
    69          shouldDrag: false,
    70          deferDragToMouseMove: true,
    71          cssDragProperty: "--wails-draggable",
    72          cssDragValue: "drag",
    73      }
    74  };
    75  
    76  // Set the bindings
    77  if (window.wailsbindings) {
    78      window.wails.SetBindings(window.wailsbindings);
    79      delete window.wails.SetBindings;
    80  }
    81  
    82  // (bool) This is evaluated at build time in package.json
    83  if (!DEBUG) {
    84      delete window.wailsbindings;
    85  }
    86  
    87  let dragTest = function (e) {
    88      var val = window.getComputedStyle(e.target).getPropertyValue(window.wails.flags.cssDragProperty);
    89      if (val) {
    90        val = val.trim();
    91      }
    92      
    93      if (val !== window.wails.flags.cssDragValue) {
    94          return false;
    95      }
    96  
    97      if (e.buttons !== 1) {
    98          // Do not start dragging if not the primary button has been clicked.
    99          return false;
   100      }
   101  
   102      if (e.detail !== 1) {
   103          // Do not start dragging if more than once has been clicked, e.g. when double clicking
   104          return false;
   105      }
   106  
   107      return true;
   108  };
   109  
   110  window.wails.setCSSDragProperties = function (property, value) {
   111      window.wails.flags.cssDragProperty = property;
   112      window.wails.flags.cssDragValue = value;
   113  }
   114  
   115  window.addEventListener('mousedown', (e) => {
   116  
   117      // Check for resizing
   118      if (window.wails.flags.resizeEdge) {
   119          window.WailsInvoke("resize:" + window.wails.flags.resizeEdge);
   120          e.preventDefault();
   121          return;
   122      }
   123  
   124      if (dragTest(e)) {
   125          if (window.wails.flags.disableScrollbarDrag) {
   126              // This checks for clicks on the scroll bar
   127              if (e.offsetX > e.target.clientWidth || e.offsetY > e.target.clientHeight) {
   128                  return;
   129              }
   130          }
   131          if (window.wails.flags.deferDragToMouseMove) {
   132              window.wails.flags.shouldDrag = true;
   133          } else {
   134              e.preventDefault()
   135              window.WailsInvoke("drag");
   136          }
   137          return;
   138      } else {
   139          window.wails.flags.shouldDrag = false;
   140      }
   141  });
   142  
   143  window.addEventListener('mouseup', () => {
   144      window.wails.flags.shouldDrag = false;
   145  });
   146  
   147  function setResize(cursor) {
   148      document.documentElement.style.cursor = cursor || window.wails.flags.defaultCursor;
   149      window.wails.flags.resizeEdge = cursor;
   150  }
   151  
   152  window.addEventListener('mousemove', function (e) {
   153      if (window.wails.flags.shouldDrag) {
   154          window.wails.flags.shouldDrag = false;
   155          let mousePressed = e.buttons !== undefined ? e.buttons : e.which;
   156          if (mousePressed > 0) {
   157              window.WailsInvoke("drag");
   158              return;
   159          }
   160      }
   161      if (!window.wails.flags.enableResize) {
   162          return;
   163      }
   164      if (window.wails.flags.defaultCursor == null) {
   165          window.wails.flags.defaultCursor = document.documentElement.style.cursor;
   166      }
   167      if (window.outerWidth - e.clientX < window.wails.flags.borderThickness && window.outerHeight - e.clientY < window.wails.flags.borderThickness) {
   168          document.documentElement.style.cursor = "se-resize";
   169      }
   170      let rightBorder = window.outerWidth - e.clientX < window.wails.flags.borderThickness;
   171      let leftBorder = e.clientX < window.wails.flags.borderThickness;
   172      let topBorder = e.clientY < window.wails.flags.borderThickness;
   173      let bottomBorder = window.outerHeight - e.clientY < window.wails.flags.borderThickness;
   174  
   175      // If we aren't on an edge, but were, reset the cursor to default
   176      if (!leftBorder && !rightBorder && !topBorder && !bottomBorder && window.wails.flags.resizeEdge !== undefined) {
   177          setResize();
   178      } else if (rightBorder && bottomBorder) setResize("se-resize");
   179      else if (leftBorder && bottomBorder) setResize("sw-resize");
   180      else if (leftBorder && topBorder) setResize("nw-resize");
   181      else if (topBorder && rightBorder) setResize("ne-resize");
   182      else if (leftBorder) setResize("w-resize");
   183      else if (topBorder) setResize("n-resize");
   184      else if (bottomBorder) setResize("s-resize");
   185      else if (rightBorder) setResize("e-resize");
   186  
   187  });
   188  
   189  // Setup context menu hook
   190  window.addEventListener('contextmenu', function (e) {
   191      // always show the contextmenu in debug & dev
   192      if (DEBUG) return;
   193  
   194      if (window.wails.flags.disableDefaultContextMenu) {
   195          e.preventDefault();
   196      } else {
   197          ContextMenu.processDefaultContextMenu(e);
   198      }
   199  });
   200  
   201  window.WailsInvoke("runtime:ready");