Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Electron Resizing a Frameless Window

I'm working on an app that has a frameless window, and has a html5 video element that displays the webcam filling the document.body at 100%.

I have my mainWindow set to resizable mainWindow.isResizable(true) but I can't resize the window.

When I debug with chrome dev tools I can only resize the window the side chrome dev tools is on. (not sure if it helps, but I'm developing this on an Ubuntu 14.04 based Linux distro called Elementary OS Freya)

Can someone explain why I can't resize my resizable window even though I have my mainWindow set to resizable?

In addition what can I do to solve this problem?

var app = require("app");
var BrowserWindow = require("browser-window");

var mainWindow = null;

// Quit when all windows are closed.
app.on("window-all-closed", function() {
  app.quit();
});

app.on("ready", function() {
  mainWindow = new BrowserWindow({
    width: 640,
    height: 480,
    frame: false
  });
  mainWindow.loadUrl("file://" + __dirname + "/index.html");

  // Set Window Resizable
  mainWindow.isResizable(true);

  mainWindow.focus();
});
like image 371
Michael Schwartz Avatar asked Oct 01 '15 19:10

Michael Schwartz


People also ask

How do you drag a frameless window in an Electron?

Draggable region​ By default, the frameless window is non-draggable. Apps need to specify -webkit-app-region: drag in CSS to tell Electron which regions are draggable (like the OS's standard titlebar), and apps can also use -webkit-app-region: no-drag to exclude the non-draggable area from the draggable region.

How do you determine the size of an Electron window?

Size of the window: The default size of an Electron window is 800*800. We can change this size by passing a few values in the BrowserWindow constructor. 1 2 3 4 5 6 7 const createWindow = () => { win = new BrowserWindow({ width: 800, height: 500 }); win. loadFile("index.

How do you make a custom titlebar for an Electron?

Your only option in Electron would be to create a frameless (aka borderless) window, and then create a "fake" title bar with CSS, including any UI elements you need. Show activity on this post. Then you could use the css properties -webkit-user-select and -webkit-app-region to specify the drag zone.


3 Answers

I managed to figure out what the problem was, and come to find out it wasn't even a javascript problem, All I had to do is mod my CSS for what I want draggable and what I don't.

Because my html and body are also set to draggable that's why I couldn't resize. Here's my solution...

I had to make a new element (div.dialog) and encase my content inside of that. Along with the following CSS.

.dialog {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: #000;
  background: rgba(0, 0, 0, 0.5);
  -webkit-app-region: no-drag;
}
.tips {
  position: absolute;
  top: 4px;
  left: 4px;
  right: 4px;
  bottom: 4px;
  -webkit-app-region: drag;
}

HTML:

<div class="dialog" data-action="toggle">
  <header class="tips" data-action="toggle">
    <div>
      <nav>
        [Space] for Snapshot<br>
        [Esc] to Close
        <p data-action="toggle-tips">[Tab] to Toggle Dialog</p>
      </nav>
    </div>
  </header>
</div>
like image 153
Michael Schwartz Avatar answered Oct 11 '22 03:10

Michael Schwartz


Answer mentioned at https://stackoverflow.com/a/32897808/11167389 is okay but there is an another approach which will not affect your any other style and can be dropped in any frameless window that you have. VS Code which is created using electron uses similar approach to allow resizing from header.

So, you mostly have structure like following with a title-bar having -webkit-app-region: drag;:

.title-bar {
  display: flex;
  height: 30px;
  border: 1px solid gray;
  align-items: center;
  -webkit-app-region: drag;
}

.title {
  margin-left: 10px;
}

.title-bar-btns {
  margin-left: auto;
  -webkit-app-region: no-drag;
}
<div class="title-bar">
  <div class="title">Window Header</div>
  <div class="title-bar-btns">
    <button id="min-btn">-</button>
    <button id="max-btn">+</button>
    <button id="close-btn">x</button>
  </div>
</div>

Now, you can add a div with class titlebar-drag-region inside your title-bar, set position:relative; to title-bar and remove -webkit-app-region: drag; from it.

What .titlebar-drag-region does is that it creates a draggable region that is slightly shorter in dimensions compared to header (title-bar). The 6px gap that we left at borders is then used for resizing. Thats it. You will have a header that is draggable as well as resizable from all edges. enter image description here

So, just create .titlebar-drag-region once and then drop <div class="titlebar-drag-region"> in headers of all windows.

.titlebar-drag-region {  /*added*/
  top: 6px;
  left: 6px;
  display: block;
  position: absolute;
  width: calc(100% - 12px);
  height: calc(100% - 6px);
  z-index: -1;
  -webkit-app-region: drag;
}

.title-bar {
  display: flex;
  height: 30px;
  border: 1px solid gray;
  align-items: center;
  position: relative;  /*added*/
  /*-webkit-app-region: drag;*/  /*removed*/
}

.title {
  margin-left: 10px;
}

.title-bar-btns {
  margin-left: auto;
  -webkit-app-region: no-drag;
}
<div class="title-bar">
  <div class="titlebar-drag-region"></div>  <! –– added ––>
  <div class="title">Window Header</div>
  <div class="title-bar-btns">
    <button id="min-btn">-</button>
    <button id="max-btn">+</button>
    <button id="close-btn">x</button>
  </div>
</div>
like image 42
Omkar Rajam Avatar answered Oct 11 '22 03:10

Omkar Rajam


You are using wrong method:

mainWindow.setResizable(false) Sets whether the window can be manually resized by user.

mainWindow.isResizable() Returns a Boolean - Whether the window can be manually resized by user.

like image 23
Sergio Avatar answered Oct 11 '22 03:10

Sergio