Notes/src/public/app/widgets/title_bar_buttons.js

107 lines
3.6 KiB
JavaScript
Raw Normal View History

2020-01-15 19:40:17 +01:00
import BasicWidget from "./basic_widget.js";
import options from "../services/options.js";
2020-01-15 19:40:17 +01:00
import utils from "../services/utils.js";
const TPL = `
<div class="title-bar-buttons">
<style>
.title-bar-buttons {
2020-04-28 21:55:54 +02:00
flex-shrink: 0;
}
2022-12-17 13:28:53 +01:00
.title-bar-buttons div button {
border: none !important;
2022-12-17 13:28:53 +01:00
border-radius: 0;
background: none !important;
2020-04-28 21:55:54 +02:00
font-size: 150%;
height: 40px;
width: 40px;
2022-12-17 13:28:53 +01:00
display: flex;
align-items: center;
justify-content: center;
2020-01-15 19:40:17 +01:00
}
2020-05-20 19:14:07 +02:00
.title-bar-buttons div:hover button {
2020-05-20 19:14:07 +02:00
background-color: var(--accented-background-color) !important;
}
.title-bar-buttons div {
display: inline-block;
height: 40px;
width: 40px;
}
2023-05-23 20:50:03 +08:00
.title-bar-buttons .top-btn.active{
background-color:var(--accented-background-color);
}
2020-01-15 19:40:17 +01:00
</style>
<!-- divs act as a hitbox for the buttons, making them clickable on corners -->
2023-05-27 14:18:26 +08:00
<div class="top-btn" title="Keep this window on top. "><button class="btn bx bx-pin"></button></div>
2022-12-17 13:28:53 +01:00
<div class="minimize-btn"><button class="btn bx bx-minus"></button></div>
<div class="maximize-btn"><button class="btn bx bx-checkbox"></button></div>
<div class="close-btn"><button class="btn bx bx-x"></button></div>
2020-01-15 19:40:17 +01:00
</div>`;
export default class TitleBarButtonsWidget extends BasicWidget {
2020-01-22 20:48:56 +01:00
doRender() {
2020-02-08 18:25:07 +01:00
if (!utils.isElectron() || options.is('nativeTitleBarVisible')) {
return this.$widget = $('<div>');
}
2020-01-15 19:40:17 +01:00
2020-02-08 18:25:07 +01:00
this.$widget = $(TPL);
2021-06-26 13:35:36 +02:00
this.contentSized();
2020-02-08 18:25:07 +01:00
2023-05-23 10:32:24 +08:00
const $topBtn = this.$widget.find(".top-btn");
2020-02-08 18:25:07 +01:00
const $minimizeBtn = this.$widget.find(".minimize-btn");
const $maximizeBtn = this.$widget.find(".maximize-btn");
const $closeBtn = this.$widget.find(".close-btn");
2023-05-23 20:50:03 +08:00
2023-06-01 00:07:57 +02:00
// When the window is restarted, the window will not be reset when it is set to the top,
// so get the window status and set the icon background
setTimeout(() => {
2023-05-23 11:14:23 +08:00
const remote = utils.dynamicRequire('@electron/remote');
2023-06-01 00:07:57 +02:00
if (remote.BrowserWindow.getFocusedWindow()?.isAlwaysOnTop()) {
2023-05-23 20:50:03 +08:00
$topBtn.addClass('active');
2023-05-23 11:14:23 +08:00
}
2023-06-01 00:07:57 +02:00
}, 1000);
2023-05-23 10:32:24 +08:00
$topBtn.on('click', () => {
$topBtn.trigger('blur');
const remote = utils.dynamicRequire('@electron/remote');
const focusedWindow = remote.BrowserWindow.getFocusedWindow();
const isAlwaysOnTop = focusedWindow.isAlwaysOnTop()
if (isAlwaysOnTop) {
focusedWindow.setAlwaysOnTop(false)
2023-05-23 20:50:03 +08:00
$topBtn.removeClass('active');
2023-05-23 10:32:24 +08:00
} else {
focusedWindow.setAlwaysOnTop(true);
2023-05-23 20:50:03 +08:00
$topBtn.addClass('active');
2023-05-23 10:32:24 +08:00
}
});
2023-05-23 20:50:03 +08:00
2020-02-08 18:25:07 +01:00
$minimizeBtn.on('click', () => {
$minimizeBtn.trigger('blur');
2021-11-16 22:43:08 +01:00
const remote = utils.dynamicRequire('@electron/remote');
2020-02-08 18:25:07 +01:00
remote.BrowserWindow.getFocusedWindow().minimize();
});
$maximizeBtn.on('click', () => {
$maximizeBtn.trigger('blur');
2021-11-16 22:43:08 +01:00
const remote = utils.dynamicRequire('@electron/remote');
2020-02-08 18:25:07 +01:00
const focusedWindow = remote.BrowserWindow.getFocusedWindow();
if (focusedWindow.isMaximized()) {
focusedWindow.unmaximize();
} else {
focusedWindow.maximize();
}
});
$closeBtn.on('click', () => {
$closeBtn.trigger('blur');
2021-11-16 22:43:08 +01:00
const remote = utils.dynamicRequire('@electron/remote');
2020-02-08 18:25:07 +01:00
remote.BrowserWindow.getFocusedWindow().close();
});
2020-01-15 19:40:17 +01:00
}
2020-05-20 19:14:07 +02:00
}