Skip to content

Commit d8c9ebc

Browse files
committed
creating a windows focus & blur events section
1 parent ac9fcc6 commit d8c9ebc

File tree

5 files changed

+91
-15
lines changed

5 files changed

+91
-15
lines changed

assets/css/demo.css

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,19 @@
173173
word-break: break-word;
174174
}
175175

176+
.smooth-appear {
177+
opacity: 1;
178+
transition: opacity .5s ease-in-out;
179+
}
180+
181+
.disappear {
182+
opacity: 0;
183+
}
184+
.demo-button.smooth-disappear:focus {
185+
outline: inherit;
186+
border-color: inherit;
187+
background-color: inherit;
188+
}
176189

177190
/* ProTip ----------------------------- */
178191

@@ -189,3 +202,4 @@
189202
.demo-protip strong {
190203
font-weight: 600;
191204
}
205+

renderer-process/windows/manage-window.js

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ const BrowserWindow = require('electron').remote.BrowserWindow
22
const path = require('path')
33

44
const manageWindowBtn = document.getElementById('manage-window')
5-
const focusModalBtn = document.getElementById('focus-on-modal-window')
65
let win
76

87
manageWindowBtn.addEventListener('click', function (event) {
@@ -11,25 +10,15 @@ manageWindowBtn.addEventListener('click', function (event) {
1110
win.on('resize', updateReply)
1211
win.on('move', updateReply)
1312
win.on('close', function () {
14-
focusModalBtn.classList.add('no-display')
1513
win = null
1614
})
1715
win.loadURL(modalPath)
1816
win.show()
19-
showFocusBtn()
2017
function updateReply () {
2118
const manageWindowReply = document.getElementById('manage-window-reply')
2219
const message = `Size: ${win.getSize()} Position: ${win.getPosition()}`
23-
2420
manageWindowReply.innerText = message
2521
}
26-
function showFocusBtn () {
27-
focusModalBtn.classList.remove('no-display')
28-
}
29-
})
30-
31-
focusModalBtn.addEventListener('click', function (event) {
32-
win.focus()
3322
})
3423

3524

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const BrowserWindow = require('electron').remote.BrowserWindow
2+
const path = require('path')
3+
4+
const manageWindowBtn = document.getElementById('listen-to-window')
5+
const focusModalBtn = document.getElementById('focus-on-modal-window')
6+
let win
7+
8+
manageWindowBtn.addEventListener('click', () => {
9+
const modalPath = path.join('file://', __dirname, '../../sections/windows/modal-toggle-visibility.html')
10+
win = new BrowserWindow({ width: 600, height: 400 })
11+
win.on('focus', hideFocusBtn)
12+
win.on('blur', showFocusBtn)
13+
win.on('close', () => {
14+
hideFocusBtn()
15+
win = null
16+
})
17+
win.loadURL(modalPath)
18+
win.show()
19+
function showFocusBtn (btn) {
20+
if(!win) return
21+
focusModalBtn.classList.add('smooth-appear')
22+
focusModalBtn.classList.remove('disappear')
23+
focusModalBtn.addEventListener('click', () => win.focus())
24+
25+
}
26+
function hideFocusBtn () {
27+
focusModalBtn.classList.add('disappear')
28+
focusModalBtn.classList.remove('smooth-appear')
29+
}
30+
})
31+
32+
33+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<style>
2+
body {
3+
padding: 10px;
4+
font-family: system, -apple-system, '.SFNSText-Regular', 'SF UI Text', 'Lucida Grande', 'Segoe UI', Ubuntu, Cantarell, sans-serif;
5+
color: #fff;
6+
background-color: #8aba87;
7+
text-align: center;
8+
font-size: 34px;
9+
}
10+
11+
#close {
12+
color: white;
13+
opacity: 0.7;
14+
position: absolute;
15+
bottom: 20px;
16+
left: 50%;
17+
transform: translateX(-50%);
18+
font-size: 12px;
19+
text-decoration: none;
20+
}
21+
</style>
22+
<p>Click on the parent window to see how the "focus on demo" button appears.</p>
23+
<a id="close" href="javascript:window.close()">Close this Window</a>

sections/windows/windows.html

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,33 @@ <h2>ProTip</h2>
5151
<div class="demo-controls">
5252
<button class="demo-button" id="manage-window">View Demo</button>
5353
<span class="demo-response" id="manage-window-reply"></span>
54-
<button class="demo-button no-display" id="focus-on-modal-window">Focus on Demo</button>
5554
</div>
56-
<p>In this demo we create a new window and listen for <code>move</code> and <code>resize</code> events on it. Click the demo button, change the new window and see the dimensions and position update here, above.</p>
57-
<p>Click the focus on Window button to switch focus to the modal window if it's not visible anymore.</p>
58-
<p>There are a lot of methods for controlling the state of the window such as the size and location as well as events to listen to for window changes. Visit the <a href="http://electron.atom.io/docs/api/browser-window">documentation<span class="u-visible-to-screen-reader">(opens in new window)</span></a> for the full list.</p>
55+
<p>In this demo we create a new window and listen for <code>move</code> and <code>resize</code> events on it. Click the demo button, change the new window and see the dimensions and position update here, above.</p>
56+
<p>There are a lot of methods for controlling the state of the window such as the size, location, and focus status as well as events to listen to for window changes. Visit the <a href="http://electron.atom.io/docs/api/browser-window">documentation<span class="u-visible-to-screen-reader">(opens in new window)</span></a> for the full list.</p>
57+
5958
<h5>Renderer Process</h5>
6059
<pre><code data-path="renderer-process/windows/manage-window.js"></pre></code>
6160
</div>
6261
</div>
6362
</div>
6463

64+
<div class="demo">
65+
<div class="demo-wrapper">
66+
<button id="using-window-events-demo-toggle" class="js-container-target demo-toggle-button">Window events: blur and focus
67+
<div class="demo-meta u-avoid-clicks">Supports: Win, OS X, Linux <span class="demo-meta-divider">|</span> Process: Main</div>
68+
</button>
69+
<div class="demo-box">
70+
<div class="demo-controls">
71+
<button class="demo-button" id="listen-to-window">View Demo</button>
72+
<button class="demo-button disappear" id="focus-on-modal-window">Focus on Demo</button>
73+
</div>
74+
<p>In this demo, we create a new window and listen for <code>blur</code> event on it. Click the demo button to create a new modal window, and switch focus back to the parent window by clicking on it. You can click the <i>Focus on Demo</i> button to switch focus to the modal window again.</p>
75+
<h5>Renderer Process</h5>
76+
<pre><code data-path="renderer-process/windows/using-window-events.js"></pre></code>
77+
</div>
78+
</div>
79+
</div>
80+
6581
<div class="demo">
6682
<div class="demo-wrapper">
6783
<button class="js-container-target demo-toggle-button">Create a frameless window
@@ -98,6 +114,7 @@ <h5>Renderer Process</h5>
98114
<script type="text/javascript">
99115
require('./renderer-process/windows/create-window')
100116
require('./renderer-process/windows/manage-window')
117+
require('./renderer-process/windows/using-window-events')
101118
require('./renderer-process/windows/frameless-window')
102119
</script>
103120

0 commit comments

Comments
 (0)