mirror of
https://github.com/wavetermdev/wails.git
synced 2026-08-05 13:53:43 -07:00
Support Screens API
This commit is contained in:
+6
-7
@@ -6,7 +6,6 @@ Informal and incomplete list of things needed in v3.
|
||||
|
||||
- [ ] Generate Bindings
|
||||
- [ ] Generate TS Models
|
||||
- [ ] Port NSIS creation
|
||||
- [ ] Dev Mode
|
||||
- [ ] Generate Info.Plist from `info.json`
|
||||
|
||||
@@ -16,11 +15,11 @@ Informal and incomplete list of things needed in v3.
|
||||
## Runtime
|
||||
|
||||
- [x] Pass window ID with window calls in JS
|
||||
- [ ] Implement alias for `window` in JS
|
||||
- [ ] Implement runtime dispatcher
|
||||
- [ ] Log
|
||||
- [x] Implement alias for `window` in JS
|
||||
- [x] Implement runtime dispatcher
|
||||
- [x] Log
|
||||
- [x] Same Window
|
||||
- [x] Other Window
|
||||
- [ ] Other Window
|
||||
- [x] Dialogs
|
||||
- [x] Info
|
||||
- [x] Warning
|
||||
@@ -29,9 +28,9 @@ Informal and incomplete list of things needed in v3.
|
||||
- [x] OpenFile
|
||||
- [x] SaveFile
|
||||
- [x] Events
|
||||
- [ ] Screens
|
||||
- [x] Screens
|
||||
- [x] Clipboard
|
||||
- [ ] Application
|
||||
- [x] Application
|
||||
- [ ] Create `.d.ts` file
|
||||
|
||||
## Templates
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Screens Demo</title>
|
||||
<style>
|
||||
body {
|
||||
padding-top: 75px;
|
||||
margin: 0 auto;
|
||||
display: flex;
|
||||
justify-content: space-around;
|
||||
flex-wrap: wrap;
|
||||
text-align: -webkit-center;
|
||||
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen", "Ubuntu", "Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue", sans-serif
|
||||
}
|
||||
|
||||
article {
|
||||
padding: 1em;
|
||||
}
|
||||
|
||||
|
||||
.monitor {
|
||||
width: 300px;
|
||||
height: 150px;
|
||||
overflow-y: scroll;
|
||||
border: solid 1em #333;
|
||||
border-radius: .5em;
|
||||
background: lightblue;
|
||||
}
|
||||
|
||||
.monitor::-webkit-scrollbar {
|
||||
width: 15px;
|
||||
}
|
||||
|
||||
.monitor::-webkit-scrollbar-thumb {
|
||||
background: #666;
|
||||
}
|
||||
|
||||
::-webkit-scrollbar-track {
|
||||
background-color: #888;
|
||||
}
|
||||
.center {
|
||||
text-align: -webkit-center;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<script>
|
||||
let monitors = wails.Screens.GetAll();
|
||||
monitors.then((result) => {
|
||||
console.log({result})
|
||||
let html = "";
|
||||
for (let i = 0; i < result.length; i++) {
|
||||
html += "<article class='center'><div class='monitor'><div style='padding:5px;'>";
|
||||
let hidpi = result[i].scale === 2 ? " (HiDPI)" : "";
|
||||
html += "<h3 class='center'>"+result[i].name+"</h3>";
|
||||
html += "<h4 class='center'>"+hidpi+"</h4>";
|
||||
html += "<h4 class='center'>"+result[i].size.width+"x"+result[i].size.height+"</h4>";
|
||||
html += "</div></div></article>";
|
||||
}
|
||||
document.body.innerHTML = html;
|
||||
})
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,43 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"embed"
|
||||
_ "embed"
|
||||
"log"
|
||||
|
||||
"github.com/wailsapp/wails/v3/pkg/application"
|
||||
)
|
||||
|
||||
//go:embed assets/*
|
||||
var assets embed.FS
|
||||
|
||||
func main() {
|
||||
|
||||
app := application.New(application.Options{
|
||||
Name: "Screen Demo",
|
||||
Description: "A demo of the Screen API",
|
||||
Mac: application.MacOptions{
|
||||
ApplicationShouldTerminateAfterLastWindowClosed: true,
|
||||
},
|
||||
})
|
||||
|
||||
app.NewWebviewWindowWithOptions(&application.WebviewWindowOptions{
|
||||
Title: "Screen Demo",
|
||||
Width: 800,
|
||||
Height: 600,
|
||||
Assets: application.AssetOptions{
|
||||
FS: assets,
|
||||
},
|
||||
Mac: application.MacWindow{
|
||||
Backdrop: application.MacBackdropTranslucent,
|
||||
TitleBar: application.MacTitleBarHiddenInsetUnified,
|
||||
InvisibleTitleBarHeight: 50,
|
||||
},
|
||||
})
|
||||
|
||||
err := app.Run()
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(err.Error())
|
||||
}
|
||||
}
|
||||
@@ -13,6 +13,7 @@ The electron alternative for Go
|
||||
import * as Clipboard from './clipboard';
|
||||
import * as Application from './application';
|
||||
import * as Log from './log';
|
||||
import * as Screens from './screens';
|
||||
|
||||
import {newWindow} from "./window";
|
||||
import {dispatchCustomEvent, Emit, Off, OffAll, On, Once, OnMultiple} from "./events";
|
||||
@@ -40,6 +41,7 @@ export function newRuntime(id) {
|
||||
...Application
|
||||
},
|
||||
Log,
|
||||
Screens,
|
||||
Dialog: {
|
||||
Info,
|
||||
Warning,
|
||||
|
||||
@@ -23,7 +23,7 @@ function runtimeCall(method, args) {
|
||||
.then(response => {
|
||||
if (response.ok) {
|
||||
// check content type
|
||||
if (response.headers.get("content-type") && response.headers.get("content-type").indexOf("application/json") !== -1) {
|
||||
if (response.headers.get("Content-Type") && response.headers.get("Content-Type").indexOf("application/json") !== -1) {
|
||||
return response.json();
|
||||
} else {
|
||||
return response.text();
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
_ __ _ __
|
||||
| | / /___ _(_) /____
|
||||
| | /| / / __ `/ / / ___/
|
||||
| |/ |/ / /_/ / / (__ )
|
||||
|__/|__/\__,_/_/_/____/
|
||||
The electron alternative for Go
|
||||
(c) Lea Anthony 2019-present
|
||||
*/
|
||||
|
||||
/* jshint esversion: 9 */
|
||||
|
||||
import {newRuntimeCaller} from "./runtime";
|
||||
|
||||
let call = newRuntimeCaller("screens");
|
||||
|
||||
export function GetAll() {
|
||||
return call("GetAll");
|
||||
}
|
||||
|
||||
export function GetPrimary() {
|
||||
return call("GetPrimary");
|
||||
}
|
||||
|
||||
export function GetCurrent() {
|
||||
return call("GetCurrent");
|
||||
}
|
||||
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@@ -69,6 +69,8 @@ func (m *MessageProcessor) HandleRuntimeCall(rw http.ResponseWriter, r *http.Req
|
||||
m.processLogMethod(method, rw, r, targetWindow, params)
|
||||
case "contextmenu":
|
||||
m.processContextMenuMethod(method, rw, r, targetWindow, params)
|
||||
case "screens":
|
||||
m.processScreensMethod(method, rw, r, targetWindow, params)
|
||||
default:
|
||||
m.httpError(rw, "Unknown runtime call: %s", object)
|
||||
}
|
||||
@@ -88,6 +90,7 @@ func (m *MessageProcessor) Info(message string, args ...any) {
|
||||
}
|
||||
|
||||
func (m *MessageProcessor) json(rw http.ResponseWriter, data any) {
|
||||
rw.Header().Set("Content-Type", "application/json")
|
||||
// convert data to json
|
||||
var jsonPayload = []byte("{}")
|
||||
var err error
|
||||
@@ -103,7 +106,6 @@ func (m *MessageProcessor) json(rw http.ResponseWriter, data any) {
|
||||
m.Error("Unable to write json payload. Please report this to the Wails team! Error: %s", err)
|
||||
return
|
||||
}
|
||||
rw.Header().Set("Content-Type", "application/json")
|
||||
m.ok(rw)
|
||||
}
|
||||
|
||||
@@ -114,7 +116,7 @@ func (m *MessageProcessor) text(rw http.ResponseWriter, data string) {
|
||||
return
|
||||
}
|
||||
rw.Header().Set("Content-Type", "text/plain")
|
||||
m.ok(rw)
|
||||
rw.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
||||
func (m *MessageProcessor) ok(rw http.ResponseWriter) {
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
package application
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func (m *MessageProcessor) processScreensMethod(method string, rw http.ResponseWriter, _ *http.Request, _ *WebviewWindow, _ QueryParams) {
|
||||
|
||||
switch method {
|
||||
case "GetAll":
|
||||
screens, err := globalApplication.GetScreens()
|
||||
if err != nil {
|
||||
m.Error("GetAll: %s", err.Error())
|
||||
return
|
||||
}
|
||||
m.json(rw, screens)
|
||||
case "GetPrimary":
|
||||
screen, err := globalApplication.GetPrimaryScreen()
|
||||
if err != nil {
|
||||
m.Error("GetPrimary: %s", err.Error())
|
||||
return
|
||||
}
|
||||
m.json(rw, screen)
|
||||
case "GetCurrent":
|
||||
screen, err := globalApplication.CurrentWindow().GetScreen()
|
||||
if err != nil {
|
||||
m.Error("GetCurrent: %s", err.Error())
|
||||
return
|
||||
}
|
||||
m.json(rw, screen)
|
||||
default:
|
||||
m.httpError(rw, "Unknown clipboard method: %s", method)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,26 +1,26 @@
|
||||
package application
|
||||
|
||||
type Screen struct {
|
||||
ID string // A unique identifier for the display
|
||||
Name string // The name of the display
|
||||
Scale float32 // The scale factor of the display
|
||||
X int // The x-coordinate of the top-left corner of the rectangle
|
||||
Y int // The y-coordinate of the top-left corner of the rectangle
|
||||
Size Size // The size of the display
|
||||
Bounds Rect // The bounds of the display
|
||||
WorkArea Rect // The work area of the display
|
||||
IsPrimary bool // Whether this is the primary display
|
||||
Rotation float32 // The rotation of the display
|
||||
ID string `json:"id,omitempty"` // A unique identifier for the display
|
||||
Name string `json:"name,omitempty"` // The name of the display
|
||||
Scale float32 `json:"scale,omitempty"` // The scale factor of the display
|
||||
X int `json:"x,omitempty"` // The x-coordinate of the top-left corner of the rectangle
|
||||
Y int `json:"y,omitempty"` // The y-coordinate of the top-left corner of the rectangle
|
||||
Size Size `json:"size"` // The size of the display
|
||||
Bounds Rect `json:"bounds"` // The bounds of the display
|
||||
WorkArea Rect `json:"work_area"` // The work area of the display
|
||||
IsPrimary bool `json:"is_primary,omitempty"` // Whether this is the primary display
|
||||
Rotation float32 `json:"rotation,omitempty"` // The rotation of the display
|
||||
}
|
||||
|
||||
type Rect struct {
|
||||
X int
|
||||
Y int
|
||||
Width int
|
||||
Height int
|
||||
X int `json:"x,omitempty"`
|
||||
Y int `json:"y,omitempty"`
|
||||
Width int `json:"width,omitempty"`
|
||||
Height int `json:"height,omitempty"`
|
||||
}
|
||||
|
||||
type Size struct {
|
||||
Width int
|
||||
Height int
|
||||
Width int `json:"width,omitempty"`
|
||||
Height int `json:"height,omitempty"`
|
||||
}
|
||||
|
||||
@@ -212,12 +212,9 @@ void windowSetResizable(void* nsWindow, bool resizable) {
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
WebviewWindow* window = (WebviewWindow*)nsWindow;
|
||||
if (resizable) {
|
||||
printf("Setting resizable to true\n");
|
||||
NSWindowStyleMask styleMask = [window styleMask] | NSWindowStyleMaskResizable;
|
||||
[window setStyleMask:styleMask];
|
||||
|
||||
} else {
|
||||
printf("Setting resizable to false\n");
|
||||
NSWindowStyleMask styleMask = [window styleMask] & ~NSWindowStyleMaskResizable;
|
||||
[window setStyleMask:styleMask];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user