Add Show() and Hide() to runtime to show/hide application (#1599)

* Add Show() and Hide() to runtime to show/hide application

* Fix devserver

* Update API docs
This commit is contained in:
Lea Anthony
2022-07-20 20:59:49 +10:00
committed by GitHub
parent b21a92ecdb
commit 29912785fa
19 changed files with 173 additions and 20 deletions
@@ -36,6 +36,8 @@ void Maximise(void* ctx);
void UnMaximise(void* ctx);
void Hide(void* ctx);
void Show(void* ctx);
void HideApplication(void* ctx);
void ShowApplication(void* ctx);
void SetBackgroundColour(void* ctx, int r, int g, int b, int a);
void ExecJS(void* ctx, const char*);
void Quit(void*);
@@ -219,6 +219,21 @@ void Show(void *inctx) {
);
}
void HideApplication(void *inctx) {
WailsContext *ctx = (__bridge WailsContext*) inctx;
ON_MAIN_THREAD(
[ctx HideApplication];
);
}
void ShowApplication(void *inctx) {
WailsContext *ctx = (__bridge WailsContext*) inctx;
ON_MAIN_THREAD(
[ctx ShowApplication];
);
}
NSString* safeInit(const char* input) {
NSString *result = nil;
if (input != nil) {
@@ -75,6 +75,8 @@
- (void) ShowMouse;
- (void) Hide;
- (void) Show;
- (void) HideApplication;
- (void) ShowApplication;
- (void) Quit;
-(void) MessageDialog :(NSString*)dialogType :(NSString*)title :(NSString*)message :(NSString*)button1 :(NSString*)button2 :(NSString*)button3 :(NSString*)button4 :(NSString*)defaultButton :(NSString*)cancelButton :(void*)iconData :(int)iconDataLength;
@@ -356,6 +356,16 @@
[NSApp activateIgnoringOtherApps:YES];
}
- (void) HideApplication {
[[NSApplication sharedApplication] hide:self];
}
- (void) ShowApplication {
[[NSApplication sharedApplication] unhide:self];
[[NSApplication sharedApplication] activateIgnoringOtherApps:TRUE];
}
- (void) Maximise {
if (![self.mainWindow isZoomed]) {
[self.mainWindow zoom:nil];
@@ -194,6 +194,13 @@ func (f *Frontend) WindowShow() {
func (f *Frontend) WindowHide() {
f.mainWindow.Hide()
}
func (f *Frontend) Show() {
f.mainWindow.ShowApplication()
}
func (f *Frontend) Hide() {
f.mainWindow.HideApplication()
}
func (f *Frontend) WindowMaximise() {
f.mainWindow.Maximise()
}
@@ -199,6 +199,13 @@ func (w *Window) Show() {
func (w *Window) Hide() {
C.Hide(w.context)
}
func (w *Window) ShowApplication() {
C.ShowApplication(w.context)
}
func (w *Window) HideApplication() {
C.HideApplication(w.context)
}
func parseIntDuo(temp string) (int, int) {
split := strings.Split(temp, ",")
@@ -186,6 +186,14 @@ func (f *Frontend) WindowShow() {
func (f *Frontend) WindowHide() {
f.mainWindow.Hide()
}
func (f *Frontend) Show() {
f.mainWindow.Show()
}
func (f *Frontend) Hide() {
f.mainWindow.Hide()
}
func (f *Frontend) WindowMaximise() {
f.mainWindow.Maximise()
}
@@ -7,6 +7,18 @@ import (
"context"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"net/http/httptest"
"net/url"
"runtime"
"strconv"
"strings"
"sync"
"text/template"
"time"
"github.com/bep/debounce"
"github.com/wailsapp/wails/v2/internal/binding"
"github.com/wailsapp/wails/v2/internal/frontend"
@@ -19,17 +31,6 @@ import (
"github.com/wailsapp/wails/v2/internal/system/operatingsystem"
"github.com/wailsapp/wails/v2/pkg/options"
"github.com/wailsapp/wails/v2/pkg/options/windows"
"io"
"log"
"net/http"
"net/http/httptest"
"net/url"
"runtime"
"strconv"
"strings"
"sync"
"text/template"
"time"
)
const startURL = "http://wails.localhost/"
@@ -357,6 +358,14 @@ func (f *Frontend) ScreenGetAll() ([]Screen, error) {
return screens, err
}
func (f *Frontend) Show() {
f.mainWindow.Show()
}
func (f *Frontend) Hide() {
f.mainWindow.Hide()
}
func (f *Frontend) Quit() {
if f.frontendOptions.OnBeforeClose != nil && f.frontendOptions.OnBeforeClose(f.ctx) {
return
@@ -48,6 +48,14 @@ type DevWebServer struct {
devServerAddr string
}
func (d *DevWebServer) Hide() {
d.desktopFrontend.Hide()
}
func (d *DevWebServer) Show() {
d.desktopFrontend.Show()
}
func (d *DevWebServer) WindowSetSystemDefaultTheme() {
d.desktopFrontend.WindowSetSystemDefaultTheme()
}
@@ -2,6 +2,7 @@ package dispatcher
import (
"context"
"github.com/pkg/errors"
"github.com/wailsapp/wails/v2/internal/binding"
"github.com/wailsapp/wails/v2/internal/frontend"
@@ -44,6 +45,12 @@ func (d *Dispatcher) ProcessMessage(message string, sender frontend.Frontend) (s
case 'Q':
sender.Quit()
return "", nil
case 'S':
sender.Show()
return "", nil
case 'H':
sender.Hide()
return "", nil
default:
return "", errors.New("Unknown message from front end: " + message)
}
+2
View File
@@ -65,6 +65,8 @@ type MessageDialogOptions struct {
type Frontend interface {
Run(context.Context) error
Hide()
Show()
Quit()
// Dialog
@@ -21,6 +21,14 @@ export function Quit() {
window.WailsInvoke('Q');
}
export function Show() {
window.WailsInvoke('S');
}
export function Hide() {
window.WailsInvoke('H');
}
export function Environment() {
return Call(":wails:Environment");
}
@@ -37,6 +45,8 @@ window.runtime = {
EventsEmit,
EventsOff,
Environment,
Show,
Hide,
Quit
};
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
+8
View File
@@ -193,3 +193,11 @@ export function Environment(): Promise<EnvironmentInfo>;
// [Quit](https://wails.io/docs/reference/runtime/intro#quit)
// Quits the application.
export function Quit(): void;
// [Hide](https://wails.io/docs/reference/runtime/intro#hide)
// Hides the application.
export function Hide(): void;
// [Show](https://wails.io/docs/reference/runtime/intro#show)
// Shows the application.
export function Show(): void;
@@ -164,3 +164,11 @@ export function Environment() {
export function Quit() {
window.runtime.Quit();
}
export function Hide() {
window.runtime.Hide();
}
export function Show() {
window.runtime.Show();
}
+21 -2
View File
@@ -2,10 +2,11 @@ package runtime
import (
"context"
"github.com/wailsapp/wails/v2/internal/frontend"
"github.com/wailsapp/wails/v2/internal/logger"
"log"
goruntime "runtime"
"github.com/wailsapp/wails/v2/internal/frontend"
"github.com/wailsapp/wails/v2/internal/logger"
)
const contextError = `An invalid context was passed. This method requires the specific context given in the lifecycle hooks:
@@ -67,6 +68,24 @@ func Quit(ctx context.Context) {
appFrontend.Quit()
}
// Hide the application
func Hide(ctx context.Context) {
if ctx == nil {
log.Fatalf("cannot call Hide: context is nil")
}
appFrontend := getFrontend(ctx)
appFrontend.Hide()
}
// Show the application if it is hidden
func Show(ctx context.Context) {
if ctx == nil {
log.Fatalf("cannot call Show: context is nil")
}
appFrontend := getFrontend(ctx)
appFrontend.Show()
}
// EnvironmentInfo contains information about the environment
type EnvironmentInfo struct {
BuildType string `json:"buildType"`
+23
View File
@@ -24,6 +24,29 @@ The Javascript library is available to the frontend via the `window.runtime` map
mode that provides Typescript declarations for the runtime. This should be located in the `wailsjs` directory in your
frontend directory.
### Hide
Go Signature: `Hide(ctx context.Context)`
Hides the application.
:::info Note
On Mac, this will hide the application in the same way as the `Hide` menu item in standard Mac applications.
This is different to hiding the window, but the application still being in the foreground.
For Windows and Linux, this is currently the same as `WindowHide`.
:::
### Show
Go Signature: `Show(ctx context.Context)`
Shows the application.
:::info Note
On Mac, this will bring the application back into the foreground.
For Windows and Linux, this is currently the same as `WindowShow`.
:::
### Quit
Go Signature: `Quit(ctx context.Context)`