5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-04 14:49:27 +08:00

V3 alpha feature/start window position (#3885)

* New InitialPosition setting

* Fix mac inverted Y issue.
Fix mac centering issue.

* Change position text in example

* Update changelog.md
This commit is contained in:
Lea Anthony 2024-11-16 10:10:40 +11:00 committed by GitHub
parent b4a61e11fa
commit 3e00b390c1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 78 additions and 18 deletions

View File

@ -20,10 +20,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added ### Added
- Events documentation to the mkdocs webite by [atterpac](https://github.com/atterpac) in [#3867](https://github.com/wailsapp/wails/pull/3867) - Events documentation to the mkdocs webite by [atterpac](https://github.com/atterpac) in [#3867](https://github.com/wailsapp/wails/pull/3867)
- Templates for sveltekit and sveltekit-ts that are set for non-SSR development by [atterpac](https://github.com/atterpac) in [#3829](https://github.com/wailsapp/wails/pull/3829) - Templates for sveltekit and sveltekit-ts that are set for non-SSR development by [atterpac](https://github.com/atterpac) in [#3829](https://github.com/wailsapp/wails/pull/3829)
- Update build assets using new `wails3 update build-assets` command by [leaanthony](https://github.com/leaanthony). - Update build assets using new `wails3 update build-assets` command by [leaanthony](https://github.com/leaanthony)
- Example to test the HTML Drag and Drop API by [FerroO2000](https://github.com/FerroO2000) in [#3856](https://github.com/wailsapp/wails/pull/3856) - Example to test the HTML Drag and Drop API by [FerroO2000](https://github.com/FerroO2000) in [#3856](https://github.com/wailsapp/wails/pull/3856)
- File Association support by [leaanthony](https://github.com/leaanthony) in [#3873](https://github.com/wailsapp/wails/pull/3873) - File Association support by [leaanthony](https://github.com/leaanthony) in [#3873](https://github.com/wailsapp/wails/pull/3873)
- New `wails3 generate runtime` command by [leaanthony](https://github.com/leaanthony) - New `wails3 generate runtime` command by [leaanthony](https://github.com/leaanthony)
- New `InitialPosition` option to specify if the window should be centered or positioned at the given X/Y location by [leaanthony](https://github.com/leaanthony) in [#3885](https://github.com/wailsapp/wails/pull/3885)
### Changed ### Changed
- Taskfile refactor by [leaanthony](https://github.com/leaanthony) in [#3748](https://github.com/wailsapp/wails/pull/3748) - Taskfile refactor by [leaanthony](https://github.com/leaanthony) in [#3748](https://github.com/wailsapp/wails/pull/3748)

View File

@ -176,6 +176,32 @@ func main() {
Show() Show()
windowCounter++ windowCounter++
}) })
myMenu.Add("New WebviewWindow (Centered)").
OnClick(func(ctx *application.Context) {
app.NewWebviewWindowWithOptions(application.WebviewWindowOptions{
MaximiseButtonState: application.ButtonHidden,
InitialPosition: application.WindowCentered,
}).
SetTitle("WebviewWindow " + strconv.Itoa(windowCounter)).
SetURL("https://wails.io").
Show()
windowCounter++
})
myMenu.Add("New WebviewWindow (Position 100,100)").
OnClick(func(ctx *application.Context) {
app.NewWebviewWindowWithOptions(application.WebviewWindowOptions{
MaximiseButtonState: application.ButtonHidden,
X: 100,
Y: 100,
InitialPosition: application.WindowXY,
}).
SetTitle("WebviewWindow " + strconv.Itoa(windowCounter)).
SetURL("https://wails.io").
Show()
windowCounter++
})
} }
if runtime.GOOS == "darwin" || runtime.GOOS == "windows" { if runtime.GOOS == "darwin" || runtime.GOOS == "windows" {
myMenu.Add("New WebviewWindow (Disable Close)"). myMenu.Add("New WebviewWindow (Disable Close)").

View File

@ -726,7 +726,7 @@ func (w *WebviewWindow) startResize(border string) error {
// Center centers the window on the screen // Center centers the window on the screen
func (w *WebviewWindow) Center() { func (w *WebviewWindow) Center() {
if w.impl == nil && !w.isDestroyed() { if w.impl == nil && !w.isDestroyed() {
w.options.Centered = true w.options.InitialPosition = WindowCentered
return return
} }
InvokeSync(w.impl.center) InvokeSync(w.impl.center)

View File

@ -514,9 +514,22 @@ void windowSetAppearanceTypeByName(void* nsWindow, const char *appearanceName) {
// Center window on current monitor // Center window on current monitor
void windowCenter(void* nsWindow) { void windowCenter(void* nsWindow) {
[(WebviewWindow*)nsWindow center]; WebviewWindow* window = (WebviewWindow*)nsWindow;
NSScreen* screen = [window screen];
if (screen == NULL) {
screen = [NSScreen mainScreen];
}
NSRect screenFrame = [screen frame];
NSRect windowFrame = [window frame];
CGFloat x = screenFrame.origin.x + (screenFrame.size.width - windowFrame.size.width) / 2;
CGFloat y = screenFrame.origin.y + (screenFrame.size.height - windowFrame.size.height) / 2;
[window setFrame:NSMakeRect(x, y, windowFrame.size.width, windowFrame.size.height) display:YES];
} }
// Get the current size of the window // Get the current size of the window
void windowGetSize(void* nsWindow, int* width, int* height) { void windowGetSize(void* nsWindow, int* width, int* height) {
NSRect frame = [(WebviewWindow*)nsWindow frame]; NSRect frame = [(WebviewWindow*)nsWindow frame];
@ -557,12 +570,18 @@ void windowGetPosition(void* nsWindow, int* x, int* y) {
} }
void windowSetPosition(void* nsWindow, int x, int y) { void windowSetPosition(void* nsWindow, int x, int y) {
NSRect frame = [(WebviewWindow*)nsWindow frame]; WebviewWindow* window = (WebviewWindow*)nsWindow;
frame.origin.x = x; NSScreen* screen = [window screen];
frame.origin.y = y; if (screen == NULL) {
[(WebviewWindow*)nsWindow setFrame:frame display:YES]; screen = [NSScreen mainScreen];
}
NSRect frame = [window frame];
frame.origin.x = x;
frame.origin.y = (screen.frame.size.height - frame.size.height) - y;
[window setFrame:frame display:YES];
} }
// Destroy window // Destroy window
void windowDestroy(void* nsWindow) { void windowDestroy(void* nsWindow) {
[(WebviewWindow*)nsWindow close]; [(WebviewWindow*)nsWindow close];
@ -1229,7 +1248,11 @@ func (w *macosWebviewWindow) run() {
w.fullscreen() w.fullscreen()
case WindowStateNormal: case WindowStateNormal:
} }
C.windowCenter(w.nsWindow) if w.parent.options.InitialPosition == WindowCentered {
C.windowCenter(w.nsWindow)
} else {
w.setPosition(options.X, options.Y)
}
startURL, err := assetserver.GetStartURL(options.URL) startURL, err := assetserver.GetStartURL(options.URL)
if err != nil { if err != nil {

View File

@ -295,11 +295,12 @@ func (w *linuxWebviewWindow) run() {
w.setFrameless(w.parent.options.Frameless) w.setFrameless(w.parent.options.Frameless)
if w.parent.options.X != 0 || w.parent.options.Y != 0 { if w.parent.options.InitialPosition == WindowCentered {
w.setRelativePosition(w.parent.options.X, w.parent.options.Y) C.windowCenter(w.nsWindow)
} else { } else {
w.center() w.setPosition(options.X, options.Y)
} }
switch w.parent.options.StartState { switch w.parent.options.StartState {
case WindowStateMaximised: case WindowStateMaximised:
w.maximise() w.maximise()
@ -352,10 +353,10 @@ func (w *linuxWebviewWindow) run() {
} }
if !w.parent.options.Hidden { if !w.parent.options.Hidden {
w.show() w.show()
if w.parent.options.X != 0 || w.parent.options.Y != 0 { if w.parent.options.InitialPosition == WindowCentered {
w.setRelativePosition(w.parent.options.X, w.parent.options.Y) w.center()
} else { } else {
w.center() // needs to be queued until after GTK starts up! w.setRelativePosition(w.parent.options.X, w.parent.options.Y)
} }
} }
if w.parent.options.DevToolsEnabled || globalApplication.isDebugMode { if w.parent.options.DevToolsEnabled || globalApplication.isDebugMode {

View File

@ -22,6 +22,13 @@ const (
ButtonHidden ButtonState = 2 ButtonHidden ButtonState = 2
) )
type WindowStartPosition int
const (
WindowCentered WindowStartPosition = 0
WindowXY WindowStartPosition = 1
)
type WebviewWindowOptions struct { type WebviewWindowOptions struct {
// Name is a unique identifier that can be given to a window. // Name is a unique identifier that can be given to a window.
Name string Name string
@ -63,9 +70,6 @@ type WebviewWindowOptions struct {
// Default: WindowStateNormal // Default: WindowStateNormal
StartState WindowState StartState WindowState
// Centered will center the window on the screen.
Centered bool
// BackgroundType is the type of background to use for the window. // BackgroundType is the type of background to use for the window.
// Default: BackgroundTypeSolid // Default: BackgroundTypeSolid
BackgroundType BackgroundType BackgroundType BackgroundType
@ -82,6 +86,9 @@ type WebviewWindowOptions struct {
// CSS is the CSS to load in the window. // CSS is the CSS to load in the window.
CSS string CSS string
// Initial Position
InitialPosition WindowStartPosition
// X is the starting X position of the window. // X is the starting X position of the window.
X int X int

View File

@ -370,8 +370,10 @@ func (w *windowsWebviewWindow) run() {
w.resizeDebouncer = debounce.New(time.Duration(options.Windows.ResizeDebounceMS) * time.Millisecond) w.resizeDebouncer = debounce.New(time.Duration(options.Windows.ResizeDebounceMS) * time.Millisecond)
} }
if options.Centered { if options.InitialPosition == WindowCentered {
w.center() w.center()
} else {
w.setPosition(options.X, options.Y)
} }
if options.Frameless { if options.Frameless {