webview_java icon indicating copy to clipboard operation
webview_java copied to clipboard

Support "setFullScreen()" and "maximizeWindow()"

Open isinvon opened this issue 1 year ago • 4 comments

isinvon avatar Oct 28 '24 13:10 isinvon

I wrote an "SetFullScreenUtil" that makes the app window full screen covering the screen.

depandency

 <dependency>
            <groupId>net.java.dev.jna</groupId>
            <artifactId>jna</artifactId>
            <version>5.15.0</version> 
</dependency>

SetFullScreenUtil

package com.sinvon.api.utils;

import com.sun.jna.platform.win32.WinDef;
import com.sun.jna.platform.win32.User32;

public class SetFullScreenUtil {

    public static void setFullScreen(String windowTitle) {
        if (windowTitle == null || windowTitle.isEmpty()) {
            System.out.println("The provided window title is null or empty; unable to set the window to full screen.");
            return;
        }

        User32 user32 = User32.INSTANCE;

        // Find window handle
        WinDef.HWND hWnd = user32.FindWindow(null, windowTitle);

        if (hWnd == null) {
            System.out.println("Failed to retrieve the window handle; unable to set the window to full screen.");
            return;
        }

        // Get the screen dimensions
        WinDef.RECT rect = new WinDef.RECT();
        user32.GetWindowRect(hWnd, rect);

        // Get screen width and height
        int screenWidth = User32.INSTANCE.GetSystemMetrics(User32.SM_CXSCREEN);
        int screenHeight = User32.INSTANCE.GetSystemMetrics(User32.SM_CYSCREEN);

        // Set the window to cover the entire screen
        user32.SetWindowLong(hWnd, User32.GWL_STYLE, user32.GetWindowLong(hWnd, User32.GWL_STYLE) & ~User32.WS_OVERLAPPEDWINDOW);
        user32.SetWindowPos(hWnd, null, 0, 0, screenWidth, screenHeight, User32.SWP_NOZORDER | User32.SWP_SHOWWINDOW);

        System.out.println("The window has been set to full screen.");
    }

use the utils.

public static void main(String[] args) {
       SpringApplication.run(ApiApplication.class, args);
       Webview wv = new Webview(false);
       String windowTitle = "gale-app";
       wv.setTitle(windowTitle);
       // Full screen coverage
       SetFullScreenUtil.setFullScreen(windowTitle);
       wv.loadURL("https://promisesaplus.com/");
       wv.run();
       wv.close();
   }

the results show

https://github.com/user-attachments/assets/1b4c0ddd-3182-4e97-a3c4-701896f12d23

There are some problem.The transition animations of "webview_java" are not very smooth. I would prefer it to directly display the size I want, such as full screen or a small-sized window, instead of having a process of the window size changing. This might feel rather jarring.

isinvon avatar Oct 30 '24 07:10 isinvon

I wrote a "maximizeWindow()" that maximizes the app window, but for now I've only tested it on the window, and it works successfully

depandency


<dependency>
        <groupId>net.java.dev.jna</groupId>
        <artifactId>jna</artifactId>
        <version>5.15.0</version>
    </dependency>
    <dependency>
        <groupId>net.java.dev.jna</groupId>
        <artifactId>jna-platform</artifactId>
        <version>5.15.0</version>
    </dependency>

WindowUtils


package com.sinvon.api.utils;

import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinDef;
import com.sun.jna.platform.win32.WinDef.HWND;

public class WindowUtils {

    public interface User32Library extends Library {
        User32Library INSTANCE = Native.load("user32", User32Library.class);

        void ShowWindow(HWND hWnd, int nCmdShow);
    }

    public static void maximizeWindow(String windowTitle) {
        WinDef.HWND hwnd = User32.INSTANCE.FindWindow(null, windowTitle); // Retrieve handle through window title
        if (hwnd != null) {
            // 调用最大化方法
            User32Library.INSTANCE.ShowWindow(hwnd, 3); // 3 = SW_MAXIMIZE
        } else {
            System.out.println("Window not found.");
        }
    }
}

main.java

 public static void main(String[] args) {
        SpringApplication.run(ApiApplication.class, args);
        Webview wv = new Webview(false);
        String windowTitle = "gale-app";
        wv.setTitle(windowTitle);
        // maximize the window
        WindowUtils.maximizeWindow(windowTitle);
        wv.loadURL("https://promisesaplus.com/");
        wv.run();
        wv.close();
    }

Look at the result

https://github.com/user-attachments/assets/6dc05067-e6f3-48e5-93b5-652832b47b06

isinvon avatar Oct 30 '24 13:10 isinvon

Code works! Both implemented in https://github.com/NotJustAnna/webview_java/commit/f7e7114f0fb435daa34dc4f95cd067dbc743e3ce

NotJustAnna avatar Apr 08 '25 23:04 NotJustAnna

@NotJustAnna ​Thank you so much for providing the code!​​ 🙌
This issue has been open for over ​​six months, and your code contribution is incredibly helpful. I really appreciate your time and expertise in tackling this problem. It means a lot to me and the project!

isinvon avatar Apr 16 '25 02:04 isinvon