View Javadoc
1   /*
2    * Copyright 2021-2022 The OSHI Project Contributors
3    * SPDX-License-Identifier: MIT
4    */
5   package oshi.software.os;
6   
7   import java.awt.Rectangle;
8   
9   import com.sun.jna.platform.win32.WinDef.HWND;
10  
11  import oshi.annotation.concurrent.Immutable;
12  
13  /**
14   * This class encapsulates information about a window on the operating system's GUI desktop
15   */
16  @Immutable
17  public class OSDesktopWindow {
18      private final long windowId;
19      private final String title;
20      private final String command;
21      private final Rectangle locAndSize;
22      private final long owningProcessId;
23      private final int order;
24      private final boolean visible;
25  
26      public OSDesktopWindow(long windowId, String title, String command, Rectangle locAndSize, long owningProcessId,
27              int order, boolean visible) {
28          super();
29          this.windowId = windowId;
30          this.title = title;
31          this.command = command;
32          this.locAndSize = locAndSize;
33          this.owningProcessId = owningProcessId;
34          this.order = order;
35          this.visible = visible;
36      }
37  
38      /**
39       * Gets the operating system's handle, window ID, or other unique identifier for this window.
40       * <p>
41       * On Winodws, this can be converted to a {@link HWND} using {@code new HWND(new Pointer(windowId))}. On macOS, this
42       * is the Core Graphics Window ID. On Unix-like systems, this is the X11 Window ID.
43       *
44       * @return the windowId
45       */
46      public long getWindowId() {
47          return windowId;
48      }
49  
50      /**
51       * Gets the Window title, if any.
52       *
53       * @return the title, which may be an empty string if the window has no title.
54       */
55      public String getTitle() {
56          return title;
57      }
58  
59      /**
60       * Gets the command name (possibly the full file path) of the window's executable program, if known.
61       *
62       * @return the command
63       */
64      public String getCommand() {
65          return command;
66      }
67  
68      /**
69       * Gets a {@link Rectangle} representing the window's location and size.
70       *
71       * @return the location and size
72       */
73      public Rectangle getLocAndSize() {
74          return locAndSize;
75      }
76  
77      /**
78       * Gets the process ID of the process which owns this window, if known.
79       *
80       * @return the owningProcessId
81       */
82      public long getOwningProcessId() {
83          return owningProcessId;
84      }
85  
86      /**
87       * Makes a best effort to get the order in which this window appears on the desktop. Higher values are more in the
88       * foreground.
89       * <p>
90       * On Windows, this represents the Z Order of the window, and is not guaranteed to be atomic, as it could be
91       * impacted by race conditions.
92       * <p>
93       * On macOS this is the window layer. Note that multiple windows may share the same layer.
94       * <p>
95       * On X11 this represents the stacking order of the windows.
96       *
97       * @return a best effort identification of the window's Z order relative to other windows
98       */
99      public int getOrder() {
100         return order;
101     }
102 
103     /**
104      * Makes a best effort to report whether the window is visible to the user. A "visible" window may be completely
105      * transparent.
106      *
107      * @return {@code true} if the window is visible to users or if visibility can not be determined, {@code false}
108      *         otherwise.
109      */
110     public boolean isVisible() {
111         return visible;
112     }
113 
114     @Override
115     public String toString() {
116         return "OSDesktopWindow [windowId=" + windowId + ", title=" + title + ", command=" + command + ", locAndSize="
117                 + locAndSize.toString() + ", owningProcessId=" + owningProcessId + ", order=" + order + ", visible="
118                 + visible + "]";
119     }
120 }