我正在使用Go-Astilectron(Go的Electron框架)创建一个应用程序。
我的应用程序具有无框透明窗口,应根据其内容调整大小。据我所知,我有义务在Electron中设置窗口的width
和height
属性,否则它将默认设置为800x600。
我想知道的是,是否有Electron可以根据其内容自动调整窗口大小的方法。
我可以使用“一刀切”的方法,但是由于我的窗口是无框透明的,因此它的某些部分最终将位于其他事物之上,并且由于没有单击,用户会感到困惑当他实际上单击我的应用程序时,以为他正在单击其他应用程序。
这是我创建窗口的代码:
var w *astilectron.Window
log.Debug("Starting astilectron...")
window := []*bootstrap.Window{{
Homepage: "http://localhost:3000",
Adapter: func(i *astilectron.Window) {
w = i
},
Options: &astilectron.WindowOptions{
MinHeight: astilectron.PtrInt(50),
MinWidth: astilectron.PtrInt(50),
AlwaysOnTop: astilectron.PtrBool(true),
Transparent: astilectron.PtrBool(true),
Closable: astilectron.PtrBool(false),
Minimizable: astilectron.PtrBool(false),
Frame: astilectron.PtrBool(false),
Movable: astilectron.PtrBool(true),
SkipTaskbar: astilectron.PtrBool(false),
Resizable: astilectron.PtrBool(false),
},
}}
go func() {
err := bootstrap.Run(bootstrap.Options{
Windows: window,
Debug: true,
})
if err != nil {
log.WithError(err).Fatal("Error with Astilectron")
}
}()
答案 0 :(得分:0)
尝试做这样的事情
const { screen } = require('electron');
const size = screen.getPrimaryDisplay().workAreaSize;
// Create the browser window.
mainWindow = new BrowserWindow({
x: 0,
y: 0,
width: size.width,
height: size.height
});
希望它会起作用。
答案 1 :(得分:0)
尝试设置UseContentSize: astilectron.PtrBool(true)
。
UseContentSize
根据页面大小调整窗口大小。这就是文档中所说的:
宽度和高度将用作网页的大小,这意味着实际窗口的大小将包括窗口框架的大小,并且稍大一些。默认值为false。
答案 2 :(得分:-1)
使用这个, 这有效
function createWindow() {
// Create the browser window.
const mainWindow = new BrowserWindow({ useContentSize: true });
const view = new BrowserView();
mainWindow.setBrowserView(view);
view.setBounds({ x: 0, y: 0, width: 800, height: 600 });
view.setAutoResize({ width: true, height: true });
view.webContents.loadURL("https://youtube.com");
}