Support Min/Max Size in Window runtime

This commit is contained in:
Lea Anthony
2021-01-28 18:48:07 +11:00
parent 7ae89d04bb
commit b81101414f
5 changed files with 74 additions and 2 deletions

View File

@@ -12,9 +12,10 @@ package ffenestri
import "C" import "C"
import ( import (
"github.com/wailsapp/wails/v2/pkg/options/dialog"
"strconv" "strconv"
"github.com/wailsapp/wails/v2/pkg/options/dialog"
"github.com/wailsapp/wails/v2/internal/logger" "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)) 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 // WindowSetColour sets the window colour
func (c *Client) WindowSetColour(colour int) { func (c *Client) WindowSetColour(colour int) {
r, g, b, a := intToColour(colour) r, g, b, a := intToColour(colour)

View File

@@ -899,6 +899,19 @@ void setMinMaxSize(struct Application *app)
{ {
msg(app->mainWindow, s("setMinSize:"), CGSizeMake(app->minWidth, app->minHeight)); 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) void SetMinWindowSize(struct Application *app, int minWidth, int minHeight)

View File

@@ -2,6 +2,7 @@ package messagedispatcher
import ( import (
"fmt" "fmt"
"github.com/wailsapp/wails/v2/internal/logger" "github.com/wailsapp/wails/v2/internal/logger"
"github.com/wailsapp/wails/v2/internal/messagedispatcher/message" "github.com/wailsapp/wails/v2/internal/messagedispatcher/message"
"github.com/wailsapp/wails/v2/internal/servicebus" "github.com/wailsapp/wails/v2/internal/servicebus"
@@ -26,6 +27,8 @@ type Client interface {
WindowUnminimise() WindowUnminimise()
WindowPosition(x int, y int) WindowPosition(x int, y int)
WindowSize(width int, height int) WindowSize(width int, height int)
WindowSetMinSize(width int, height int)
WindowSetMaxSize(width int, height int)
WindowFullscreen() WindowFullscreen()
WindowUnFullscreen() WindowUnFullscreen()
WindowSetColour(colour int) WindowSetColour(colour int)

View File

@@ -2,11 +2,12 @@ package messagedispatcher
import ( import (
"encoding/json" "encoding/json"
"github.com/wailsapp/wails/v2/pkg/options/dialog"
"strconv" "strconv"
"strings" "strings"
"sync" "sync"
"github.com/wailsapp/wails/v2/pkg/options/dialog"
"github.com/wailsapp/wails/v2/internal/crypto" "github.com/wailsapp/wails/v2/internal/crypto"
"github.com/wailsapp/wails/v2/internal/logger" "github.com/wailsapp/wails/v2/internal/logger"
"github.com/wailsapp/wails/v2/internal/messagedispatcher/message" "github.com/wailsapp/wails/v2/internal/messagedispatcher/message"
@@ -349,6 +350,38 @@ func (d *Dispatcher) processWindowMessage(result *servicebus.Message) {
for _, client := range d.clients { for _, client := range d.clients {
client.frontend.WindowSize(w, h) 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: default:
d.logger.Error("Unknown window command: %s", command) d.logger.Error("Unknown window command: %s", command)
} }

View File

@@ -18,6 +18,8 @@ type Window interface {
Unminimise() Unminimise()
SetTitle(title string) SetTitle(title string)
SetSize(width int, height int) SetSize(width int, height int)
SetMinSize(width int, height int)
SetMaxSize(width int, height int)
SetPosition(x int, y int) SetPosition(x int, y int)
Fullscreen() Fullscreen()
UnFullscreen() UnFullscreen()
@@ -85,6 +87,18 @@ func (w *window) SetSize(width int, height int) {
w.bus.Publish(message, "") 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 // SetPosition sets the position of the window
func (w *window) SetPosition(x int, y int) { func (w *window) SetPosition(x int, y int) {
message := fmt.Sprintf("window:position:%d:%d", x, y) message := fmt.Sprintf("window:position:%d:%d", x, y)