@abmcode/electron-titlebar 中文文档教程

发布于 4年前 浏览 24 项目主页 更新于 3年前

Custom Electron Titlebar

这个项目是一个 electron 的 typescript 库,它允许你配置一个完全可定制的标题栏。

It is a library for electron, it cannot be used on a basic website.

告别是悲伤的。 但由于我没有时间保留这个项目,我决定在2020年8月14日存档。

我要感谢数百个支持和使用自定义电子标题栏的人,没有你这是不可能实现的。 对于 electron 10+,这是新项目:https://github.com/Treverix/custom-electron-titlebar

LICENSENPM 版本

预览 1

预览 2

Preview 3

Install

npm i custom-electron-titlebar

或使用示例文件夹来初始化带有此标题栏的基本电子项目。

Usage

Step 1

在您的renderer 文件或HTML 脚本标记 中添加:

const customTitlebar = require('custom-electron-titlebar');

new customTitlebar.Titlebar({
    backgroundColor: customTitlebar.Color.fromHex('#444')
});

如果您使用的是typescript

import { Titlebar, Color } from 'custom-electron-titlebar'

new Titlebar({
    backgroundColor: Color.fromHex('#ECECEC')
});

参数backgroundColor: Color是必需的,这应该是 Color 类型。 (查看​​更新背景了解更多详情)。

Step 2

更新启动浏览器窗口

var mainWindow = new BrowserWindow({
      width: 1000,
      height: 600,
      titleBarStyle: "hidden", // add this line
});

Options

的代码管理接口[TitleBarOptions],它有以下标题栏的可配置选项。 一些参数是可选的。

ParameterTypeDescriptionDefault
backgroundColor (required)ColorThe background color of the titlebar.#444444
iconstringThe icon shown on the left side of the title bar.null
iconsThemeThemeStyle of the icons.Themebar.win
shadowbooleanThe shadow of the titlebar.false
dragbooleanDefine whether or not you can drag the window by holding the click on the title bar.true
minimizablebooleanEnables or disables the option to minimize the window by clicking on the corresponding button in the title bar.true
maximizablebooleanEnables or disables the option to maximize and un-maximize the window by clicking on the corresponding button in the title bar.true
closeablebooleanEnables or disables the option of the close window by clicking on the corresponding button in the title bar.true
orderstringSet the order of the elements on the title bar. (inverted, first-buttons)null
titleHorizontalAlignmentstringSet horizontal alignment of the window title. (left, center, right)center
menuElectron.MenuThe menu to show in the title bar.Menu.getApplicationMenu()
menuPositionstringThe position of menubar on titlebar.left
enableMnemonicsbooleanEnable the mnemonics on menubar and menu items.true
itemBackgroundColorColorThe background color when the mouse is over the item.rgba(0, 0, 0, .14)
hideWhenClickingClosebooleanWhen the close button is clicked, the window is hidden instead of closed.false
overflowstringThe overflow of the container (auto, visible, hidden)auto
unfocusEffectbooleanEnables or disables the blur option in the title bar.false

Methods

Update Background

这会改变标题栏的颜色,并检查颜色是浅色还是深色,以便图标的颜色适应标题栏的背景。

titlebar.updateBackground(new Color(new RGBA(0, 0, 0, .7)));

要分配颜色,您可以使用以下选项:Color.fromHex()new Color(new RGBA(r, g, b, a))new颜色(新 HSLA(h,s,l,a))新颜色(新 HSVA(h,s,v,a))Color.BLUE、Color.RED 等。

Update Items Background Color

此方法在悬停菜单栏项目时更改背景颜色。

titlebar.updateItemBGColor(new Color(new RGBA(0, 0, 0, .7)));

要分配颜色,您可以使用以下选项:Color.fromHex()new Color(new RGBA(r, g, b, a))new颜色(新 HSLA(h,s,l,a))新颜色(新 HSVA(h,s,v,a))Color.BLUE, Color.RED等。

Update Title

该方法更新了标题栏的title,如果改变了title标签的内容,需要调用该方法进行更新标题。

document.title = 'My new title';
titlebar.updateTitle();

// Or you can do as follows and avoid writing document.title
titlebar.updateTitle('New Title');

如果调用此方法并添加 title 参数,文档的标题将更改为参数的标题。

Update Icon

使用此方法,您可以更新图标。 此方法接收图像的 url (建议使用透明图像格式)

titlebar.updateIcon('./images/my-icon.svg');

Update Menu

此方法更新或创建菜单,使用 remote.Menu 和 remote.MenuItem 创建菜单。

const menu = new Menu();
menu.append(new MenuItem({
    label: 'Item 1',
    submenu: [
        {
            label: 'Subitem 1',
            click: () => console.log('Click on subitem 1')
        },
        {
            type: 'separator'
        }
    ]
}));

menu.append(new MenuItem({
    label: 'Item 2',
    submenu: [
        {
            label: 'Subitem checkbox',
            type: 'checkbox',
            checked: true
        },
        {
            type: 'separator'
        },
        {
            label: 'Subitem with submenu',
            submenu: [
                {
                    label: 'Submenu &item 1',
                    accelerator: 'Ctrl+T'
                }
            ]
        }
    ]
}));

titlebar.updateMenu(menu);

Update Menu Position

您可以更改菜单栏的位置。 leftbottom 是允许的。

titlebar.updateMenuPosition('bottom');

Set Horizontal Alignment

setHorizo​​ntalAlignment 方法由 @MairwunNx 贡献:punch:

left, centerright 被允许

titlebar.setHorizontalAlignment('right');

Dispose

此方法完全删除标题栏和所有记录的事件。

titlebar.dispose();

CSS Classes

存在以下 CSS 类,可用于自定义标题栏

Class nameDescription
.titlebarStyles the titlebar.
.window-appiconStyles the app icon on the titlebar.
.window-titleStyles the window title. (Example: font-size)
.window-controls-containerStyles the window controls section.
.resizer topDescription missing
.resizer leftDescription missing
.menubarDescription missing
.menubar-menu-buttonStyles the main menu elements. (Example: color)
.menubar-menu-button openDescription missing
.menubar-menu-titleDescription missing
.action-itemDescription missing
.action-menu-itemStyles action menu elements. (Example: color)

Contributing

非常感谢所有通过问题和请求请求支持该项目的人。 如果您想为这个项目做出贡献,欢迎提出所有问题和拉取请求。

您还可以:
给我买杯咖啡

License

这个项目在 MIT 许可证。

Custom Electron Titlebar

This project is a typescript library for electron that allows you to configure a fully customizable title bar.

It is a library for electron, it cannot be used on a basic website.

The farewells are sad. But since I don't have time to keep this project, I have decided to archive it on August 14, 2020.

I have to thank the hundreds of people who supported and used custom electron titlebar, without you this would not have been achieved. For electron 10+ this is the new project: https://github.com/Treverix/custom-electron-titlebar

LICENSENPM Version

Preview 1

Preview 2

Preview 3

Install

npm i custom-electron-titlebar

or use example folder to init basic electron project with this titlebar.

Usage

Step 1

In your renderer file or in an HTML script tag add:

const customTitlebar = require('custom-electron-titlebar');

new customTitlebar.Titlebar({
    backgroundColor: customTitlebar.Color.fromHex('#444')
});

if you are using typescript

import { Titlebar, Color } from 'custom-electron-titlebar'

new Titlebar({
    backgroundColor: Color.fromHex('#ECECEC')
});

The parameter backgroundColor: Color is required, this should be Color type. (View Update Background for more details).

Step 2

Update the code that launches browser window

var mainWindow = new BrowserWindow({
      width: 1000,
      height: 600,
      titleBarStyle: "hidden", // add this line
});

Options

The interface [TitleBarOptions] is managed, which has the following configurable options for the title bar. Some parameters are optional.

ParameterTypeDescriptionDefault
backgroundColor (required)ColorThe background color of the titlebar.#444444
iconstringThe icon shown on the left side of the title bar.null
iconsThemeThemeStyle of the icons.Themebar.win
shadowbooleanThe shadow of the titlebar.false
dragbooleanDefine whether or not you can drag the window by holding the click on the title bar.true
minimizablebooleanEnables or disables the option to minimize the window by clicking on the corresponding button in the title bar.true
maximizablebooleanEnables or disables the option to maximize and un-maximize the window by clicking on the corresponding button in the title bar.true
closeablebooleanEnables or disables the option of the close window by clicking on the corresponding button in the title bar.true
orderstringSet the order of the elements on the title bar. (inverted, first-buttons)null
titleHorizontalAlignmentstringSet horizontal alignment of the window title. (left, center, right)center
menuElectron.MenuThe menu to show in the title bar.Menu.getApplicationMenu()
menuPositionstringThe position of menubar on titlebar.left
enableMnemonicsbooleanEnable the mnemonics on menubar and menu items.true
itemBackgroundColorColorThe background color when the mouse is over the item.rgba(0, 0, 0, .14)
hideWhenClickingClosebooleanWhen the close button is clicked, the window is hidden instead of closed.false
overflowstringThe overflow of the container (auto, visible, hidden)auto
unfocusEffectbooleanEnables or disables the blur option in the title bar.false

Methods

Update Background

This change the color of titlebar and it's checked whether the color is light or dark, so that the color of the icons adapts to the background of the title bar.

titlebar.updateBackground(new Color(new RGBA(0, 0, 0, .7)));

To assign colors you can use the following options: Color.fromHex(), new Color(new RGBA(r, g, b, a)), new Color(new HSLA(h, s, l, a)), new Color(new HSVA(h, s, v, a)) or Color.BLUE, Color.RED, etc.

Update Items Background Color

This method change background color on hover of items of menubar.

titlebar.updateItemBGColor(new Color(new RGBA(0, 0, 0, .7)));

To assign colors you can use the following options: Color.fromHex(), new Color(new RGBA(r, g, b, a)), new Color(new HSLA(h, s, l, a)), new Color(new HSVA(h, s, v, a)) or Color.BLUE, Color.RED, etc.

Update Title

This method updated the title of the title bar, If you change the content of the title tag, you should call this method for update the title.

document.title = 'My new title';
titlebar.updateTitle();

// Or you can do as follows and avoid writing document.title
titlebar.updateTitle('New Title');

if this method is called and the title parameter is added, the title of the document is changed to that of the parameter.

Update Icon

With this method you can update the icon. This method receives the url of the image (it is advisable to use transparent image formats)

titlebar.updateIcon('./images/my-icon.svg');

Update Menu

This method updates or creates the menu, to create the menu use remote.Menu and remote.MenuItem.

const menu = new Menu();
menu.append(new MenuItem({
    label: 'Item 1',
    submenu: [
        {
            label: 'Subitem 1',
            click: () => console.log('Click on subitem 1')
        },
        {
            type: 'separator'
        }
    ]
}));

menu.append(new MenuItem({
    label: 'Item 2',
    submenu: [
        {
            label: 'Subitem checkbox',
            type: 'checkbox',
            checked: true
        },
        {
            type: 'separator'
        },
        {
            label: 'Subitem with submenu',
            submenu: [
                {
                    label: 'Submenu &item 1',
                    accelerator: 'Ctrl+T'
                }
            ]
        }
    ]
}));

titlebar.updateMenu(menu);

Update Menu Position

You can change the position of the menu bar. left and bottom are allowed.

titlebar.updateMenuPosition('bottom');

Set Horizontal Alignment

setHorizontalAlignment method was contributed by @MairwunNx :punch:

left, center and right are allowed

titlebar.setHorizontalAlignment('right');

Dispose

This method removes the title bar completely and all recorded events.

titlebar.dispose();

CSS Classes

The following CSS classes exist and can be used to customize the titlebar

Class nameDescription
.titlebarStyles the titlebar.
.window-appiconStyles the app icon on the titlebar.
.window-titleStyles the window title. (Example: font-size)
.window-controls-containerStyles the window controls section.
.resizer topDescription missing
.resizer leftDescription missing
.menubarDescription missing
.menubar-menu-buttonStyles the main menu elements. (Example: color)
.menubar-menu-button openDescription missing
.menubar-menu-titleDescription missing
.action-itemDescription missing
.action-menu-itemStyles action menu elements. (Example: color)

Contributing

Many thanks to all the people who support this project through issues and pull request. If you want to contribute with this project, all the issues and pull request are welcome.

You can also:
Buy Me A Coffee

License

This project is under the MIT license.

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文