From b81101414f611be9d280f8ba02fdbed91250127a Mon Sep 17 00:00:00 2001 From: Lea Anthony Date: Thu, 28 Jan 2021 18:48:07 +1100 Subject: [PATCH] Support Min/Max Size in Window runtime --- v2/internal/ffenestri/ffenestri_client.go | 11 +++++- v2/internal/ffenestri/ffenestri_darwin.c | 13 +++++++ .../messagedispatcher/dispatchclient.go | 3 ++ .../messagedispatcher/messagedispatcher.go | 35 ++++++++++++++++++- v2/internal/runtime/window.go | 14 ++++++++ 5 files changed, 74 insertions(+), 2 deletions(-) diff --git a/v2/internal/ffenestri/ffenestri_client.go b/v2/internal/ffenestri/ffenestri_client.go index b0e7607a..851d5471 100644 --- a/v2/internal/ffenestri/ffenestri_client.go +++ b/v2/internal/ffenestri/ffenestri_client.go @@ -12,9 +12,10 @@ package ffenestri import "C" import ( - "github.com/wailsapp/wails/v2/pkg/options/dialog" "strconv" + "github.com/wailsapp/wails/v2/pkg/options/dialog" + "github.com/wailsapp/wails/v2/internal/logger" ) @@ -113,6 +114,14 @@ func (c *Client) WindowSize(width int, height int) { C.SetSize(c.app.app, C.int(width), C.int(height)) } +func (c *Client) WindowSetMinSize(width int, height int) { + C.SetMinWindowSize(c.app.app, C.int(width), C.int(height)) +} + +func (c *Client) WindowSetMaxSize(width int, height int) { + C.SetMaxWindowSize(c.app.app, C.int(width), C.int(height)) +} + // WindowSetColour sets the window colour func (c *Client) WindowSetColour(colour int) { r, g, b, a := intToColour(colour) diff --git a/v2/internal/ffenestri/ffenestri_darwin.c b/v2/internal/ffenestri/ffenestri_darwin.c index 8cb685ba..601d7312 100644 --- a/v2/internal/ffenestri/ffenestri_darwin.c +++ b/v2/internal/ffenestri/ffenestri_darwin.c @@ -899,6 +899,19 @@ void setMinMaxSize(struct Application *app) { msg(app->mainWindow, s("setMinSize:"), CGSizeMake(app->minWidth, app->minHeight)); } + + // Calculate if window needs resizing + int newWidth = app->width; + int newHeight = app->height; + if (app->width > app->maxWidth) newWidth = app->maxWidth; + if (app->width < app->minWidth) newWidth = app->minWidth; + if (app->height > app->maxHeight ) newHeight = app->maxHeight; + if (app->height < app->minHeight ) newHeight = app->minHeight; + + // If we have any change, resize window + if ( newWidth != app->width || newHeight != app->height ) { + SetSize(app, newWidth, newHeight); + } } void SetMinWindowSize(struct Application *app, int minWidth, int minHeight) diff --git a/v2/internal/messagedispatcher/dispatchclient.go b/v2/internal/messagedispatcher/dispatchclient.go index b2084113..d604690e 100644 --- a/v2/internal/messagedispatcher/dispatchclient.go +++ b/v2/internal/messagedispatcher/dispatchclient.go @@ -2,6 +2,7 @@ package messagedispatcher import ( "fmt" + "github.com/wailsapp/wails/v2/internal/logger" "github.com/wailsapp/wails/v2/internal/messagedispatcher/message" "github.com/wailsapp/wails/v2/internal/servicebus" @@ -26,6 +27,8 @@ type Client interface { WindowUnminimise() WindowPosition(x int, y int) WindowSize(width int, height int) + WindowSetMinSize(width int, height int) + WindowSetMaxSize(width int, height int) WindowFullscreen() WindowUnFullscreen() WindowSetColour(colour int) diff --git a/v2/internal/messagedispatcher/messagedispatcher.go b/v2/internal/messagedispatcher/messagedispatcher.go index 3001a293..e96b25c9 100644 --- a/v2/internal/messagedispatcher/messagedispatcher.go +++ b/v2/internal/messagedispatcher/messagedispatcher.go @@ -2,11 +2,12 @@ package messagedispatcher import ( "encoding/json" - "github.com/wailsapp/wails/v2/pkg/options/dialog" "strconv" "strings" "sync" + "github.com/wailsapp/wails/v2/pkg/options/dialog" + "github.com/wailsapp/wails/v2/internal/crypto" "github.com/wailsapp/wails/v2/internal/logger" "github.com/wailsapp/wails/v2/internal/messagedispatcher/message" @@ -349,6 +350,38 @@ func (d *Dispatcher) processWindowMessage(result *servicebus.Message) { for _, client := range d.clients { client.frontend.WindowSize(w, h) } + case "minsize": + // We need 2 arguments + if len(splitTopic) != 4 { + d.logger.Error("Invalid number of parameters for 'window:minsize' : %#v", result.Data()) + return + } + w, err1 := strconv.Atoi(splitTopic[2]) + h, err2 := strconv.Atoi(splitTopic[3]) + if err1 != nil || err2 != nil { + d.logger.Error("Invalid integer parameters for 'window:minsize' : %#v", result.Data()) + return + } + // Notifh clients + for _, client := range d.clients { + client.frontend.WindowSetMinSize(w, h) + } + case "maxsize": + // We need 2 arguments + if len(splitTopic) != 4 { + d.logger.Error("Invalid number of parameters for 'window:maxsize' : %#v", result.Data()) + return + } + w, err1 := strconv.Atoi(splitTopic[2]) + h, err2 := strconv.Atoi(splitTopic[3]) + if err1 != nil || err2 != nil { + d.logger.Error("Invalid integer parameters for 'window:maxsize' : %#v", result.Data()) + return + } + // Notifh clients + for _, client := range d.clients { + client.frontend.WindowSetMaxSize(w, h) + } default: d.logger.Error("Unknown window command: %s", command) } diff --git a/v2/internal/runtime/window.go b/v2/internal/runtime/window.go index 2cc73e3d..89ad7da3 100644 --- a/v2/internal/runtime/window.go +++ b/v2/internal/runtime/window.go @@ -18,6 +18,8 @@ type Window interface { Unminimise() SetTitle(title string) SetSize(width int, height int) + SetMinSize(width int, height int) + SetMaxSize(width int, height int) SetPosition(x int, y int) Fullscreen() UnFullscreen() @@ -85,6 +87,18 @@ func (w *window) SetSize(width int, height int) { w.bus.Publish(message, "") } +// SetSize sets the size of the window +func (w *window) SetMinSize(width int, height int) { + message := fmt.Sprintf("window:minsize:%d:%d", width, height) + w.bus.Publish(message, "") +} + +// SetSize sets the size of the window +func (w *window) SetMaxSize(width int, height int) { + message := fmt.Sprintf("window:maxsize:%d:%d", width, height) + w.bus.Publish(message, "") +} + // SetPosition sets the position of the window func (w *window) SetPosition(x int, y int) { message := fmt.Sprintf("window:position:%d:%d", x, y)