webui-dev / webui

Use any web browser or WebView as GUI, with your preferred language in the backend and modern web technologies in the frontend, all in a lightweight portable library.
https://webui.me
MIT License
2.37k stars 146 forks source link

Question: How to set position to the center of the screen? #317

Open Aukstkalnis opened 5 months ago

Aukstkalnis commented 5 months ago

Hi. I want to set window to the center of my screen. How do I do it? There is no current method to get total of the current screen and there is no method to set in directly to the center. I only have SetPosition method.

I am using Go programing language and fedora 39 OS.

jinzhongjia commented 5 months ago

mayebe you can use js api screen.width screen.height or screen.availWidth or screen.availHeight to get screent size then you can get browser size Then we can get x and y after a small amount of calculations This is just a solution I proposed, I have not tested whether it is feasible

AlbertShown commented 5 months ago

Try this:

function centerWindow() {
    const screenWidth = screen.width;
    const screenHeight = screen.height;
    const width = window.outerWidth;
    const height = window.outerHeight;
    const centerX = (screenWidth - width) / 2;
    const centerY = (screenHeight - height) / 2;
    window.moveTo(centerX, centerY);
}

centerWindow();
Aukstkalnis commented 5 months ago

It kinda works but I can see the change in position. I guess we can close this issue, but can be feature for the future to implement functions get position, monitor size, mult-imonitor positioning. etc.. Anyway, thanks :)

AlbertShown commented 5 months ago

webui is not like the other GUI libraries, a webui window is a web browser window, so just let the browser handle the size and position. If you still want to to get the current window info just use JavaScript, example run JS from your backend:

"return window.outerWidth;"
"return window.outerHeight;"