Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/v3-alpha' into v3-alpha
Browse files Browse the repository at this point in the history
  • Loading branch information
leaanthony committed Oct 5, 2023
2 parents 5958d9c + 8ddd29d commit 9ac6359
Show file tree
Hide file tree
Showing 21 changed files with 448 additions and 149 deletions.
3 changes: 3 additions & 0 deletions v3/internal/runtime/desktop/api/event_types.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ export const EventTypes = {
WindowFileDraggingPerformed: "mac:WindowFileDraggingPerformed",
WindowFileDraggingExited: "mac:WindowFileDraggingExited",
},
Linux: {
SystemThemeChanged: "linux:SystemThemeChanged",
},
Common: {
ApplicationStarted: "common:ApplicationStarted",
WindowMaximise: "common:WindowMaximise",
Expand Down
2 changes: 1 addition & 1 deletion v3/pkg/application/application_darwin_delegate.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//go:build darwin
#import "application_darwin_delegate.h"
#import "../events/events.h"
#import "../events/events_darwin.h"
extern bool hasListeners(unsigned int);
@implementation AppDelegate
- (void)dealloc
Expand Down
13 changes: 6 additions & 7 deletions v3/pkg/application/application_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@
package application

import (
"fmt"
"log"
"os"
"strings"
"sync"

"github.com/wailsapp/wails/v3/pkg/events"
)

func init() {
Expand Down Expand Up @@ -104,10 +101,12 @@ func (m *linuxApp) run() error {

// Add a hook to the ApplicationDidFinishLaunching event
// FIXME: add Wails specific events - i.e. Shouldn't platform specific ones be translated to Wails events?
m.parent.On(events.Mac.ApplicationDidFinishLaunching, func(evt *Event) {
// Do we need to do anything now?
fmt.Println("events.Mac.ApplicationDidFinishLaunching received!")
})
//m.parent.On(events.Mac.ApplicationDidFinishLaunching, func(evt *Event) {
// // Do we need to do anything now?
// fmt.Println("events.Mac.ApplicationDidFinishLaunching received!")
//})

m.setupCommonEvents()

return appRun(m.application)
}
Expand Down
6 changes: 4 additions & 2 deletions v3/pkg/application/clipboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ func newClipboard() *Clipboard {
}

func (c *Clipboard) SetText(text string) bool {
return c.impl.setText(text)
return InvokeSyncWithResult(func() bool {
return c.impl.setText(text)
})
}

func (c *Clipboard) Text() (string, bool) {
return c.impl.text()
return InvokeSyncWithResultAndOther(c.impl.text)
}
11 changes: 3 additions & 8 deletions v3/pkg/application/clipboard_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,14 @@ type linuxClipboard struct{}
func (m linuxClipboard) setText(text string) bool {
clipboardLock.Lock()
defer clipboardLock.Unlock()
// cText := C.CString(text)
// success := C.setClipboardText(cText)
// C.free(unsafe.Pointer(cText))
success := false
return bool(success)
clipboardSet(text)
return true
}

func (m linuxClipboard) text() (string, bool) {
clipboardLock.RLock()
defer clipboardLock.RUnlock()
// clipboardText := C.getClipboardText()
// result := C.GoString(clipboardText)
return "", false
return clipboardGet(), true
}

func newClipboardImpl() *linuxClipboard {
Expand Down
32 changes: 25 additions & 7 deletions v3/pkg/application/dialogs.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package application

import (
"fmt"
"strings"
"sync"
)
Expand Down Expand Up @@ -159,7 +160,7 @@ func (d *MessageDialog) SetMessage(message string) *MessageDialog {
}

type openFileDialogImpl interface {
show() ([]string, error)
show() (chan string, error)
}

type FileFilter struct {
Expand Down Expand Up @@ -265,10 +266,11 @@ func (d *OpenFileDialogStruct) PromptForSingleSelection() (string, error) {
if d.impl == nil {
d.impl = newOpenFileDialogImpl(d)
}
selection, err := InvokeSyncWithResultAndError(d.impl.show)

var result string
if len(selection) > 0 {
result = selection[0]
selections, err := InvokeSyncWithResultAndError(d.impl.show)
if err == nil {
result = <-selections
}

return result, err
Expand All @@ -289,7 +291,17 @@ func (d *OpenFileDialogStruct) PromptForMultipleSelection() ([]string, error) {
if d.impl == nil {
d.impl = newOpenFileDialogImpl(d)
}
return InvokeSyncWithResultAndError(d.impl.show)

selections, err := InvokeSyncWithResultAndError(d.impl.show)

var result []string
fmt.Println("Waiting for results:")
for filename := range selections {
fmt.Println(filename)
result = append(result, filename)
}

return result, err
}

func (d *OpenFileDialogStruct) SetMessage(message string) *OpenFileDialogStruct {
Expand Down Expand Up @@ -385,7 +397,7 @@ type SaveFileDialogStruct struct {
}

type saveFileDialogImpl interface {
show() (string, error)
show() (chan string, error)
}

func (d *SaveFileDialogStruct) SetOptions(options *SaveFileDialogOptions) {
Expand Down Expand Up @@ -448,7 +460,13 @@ func (d *SaveFileDialogStruct) PromptForSingleSelection() (string, error) {
if d.impl == nil {
d.impl = newSaveFileDialogImpl(d)
}
return InvokeSyncWithResultAndError(d.impl.show)

var result string
selections, err := InvokeSyncWithResultAndError(d.impl.show)
if err == nil {
result = <-selections
}
return result, err
}

func (d *SaveFileDialogStruct) SetButtonText(text string) *SaveFileDialogStruct {
Expand Down
38 changes: 18 additions & 20 deletions v3/pkg/application/dialogs_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ extern void saveFileDialogCallback(uint id, char* path);
static void showAboutBox(char* title, char *message, void *icon, int length) {
// run on main thread
dispatch_async(dispatch_get_main_queue(), ^{
// dispatch_async(dispatch_get_main_queue(), ^{
NSAlert *alert = [[NSAlert alloc] init];
if (title != NULL) {
[alert setMessageText:[NSString stringWithUTF8String:title]];
Expand All @@ -34,7 +34,7 @@ static void showAboutBox(char* title, char *message, void *icon, int length) {
}
[alert setAlertStyle:NSAlertStyleInformational];
[alert runModal];
});
// });
}
Expand Down Expand Up @@ -163,7 +163,7 @@ static void showOpenFileDialog(unsigned int dialogID,
void *window) {
// run on main thread
dispatch_async(dispatch_get_main_queue(), ^{
dispatch_async(dispatch_get_main_queue(), ^{
NSOpenPanel *panel = [NSOpenPanel openPanel];
Expand Down Expand Up @@ -229,7 +229,7 @@ static void showOpenFileDialog(unsigned int dialogID,
processOpenFileDialogResults(panel, result, dialogID);
}];
}
});
});
}
static void showSaveFileDialog(unsigned int dialogID,
Expand All @@ -246,7 +246,7 @@ static void showSaveFileDialog(unsigned int dialogID,
void *window) {
// run on main thread
dispatch_async(dispatch_get_main_queue(), ^{
dispatch_async(dispatch_get_main_queue(), ^{
NSSavePanel *panel = [NSSavePanel savePanel];
if (message != NULL) {
Expand Down Expand Up @@ -280,8 +280,8 @@ static void showSaveFileDialog(unsigned int dialogID,
[panel beginSheetModalForWindow:(__bridge NSWindow *)window completionHandler:^(NSInteger result) {
const char *path = NULL;
if (result == NSModalResponseOK) {
NSURL *url = [panel URL];
const char *path = [[url path] UTF8String];
NSURL *url = [panel URL];
path = [[url path] UTF8String];
}
saveFileDialogCallback(dialogID, (char *)path);
}];
Expand All @@ -290,12 +290,12 @@ static void showSaveFileDialog(unsigned int dialogID,
const char *path = NULL;
if (result == NSModalResponseOK) {
NSURL *url = [panel URL];
const char *path = [[url path] UTF8String];
path = [[url path] UTF8String];
}
saveFileDialogCallback(dialogID, (char *)path);
}];
}
});
});
}
*/
Expand All @@ -321,7 +321,9 @@ func (m *macosApp) showAboutDialog(title string, message string, icon []byte) {
if icon != nil {
iconData = unsafe.Pointer(&icon[0])
}
C.showAboutBox(C.CString(title), C.CString(message), iconData, C.int(len(icon)))
InvokeAsync(func() {
C.showAboutBox(C.CString(title), C.CString(message), iconData, C.int(len(icon)))
})
}

type macosDialog struct {
Expand All @@ -331,7 +333,7 @@ type macosDialog struct {
}

func (m *macosDialog) show() {
globalApplication.dispatchOnMainThread(func() {
InvokeAsync(func() {

// Mac can only have 4 Buttons on a dialog
if len(m.dialog.Buttons) > 4 {
Expand Down Expand Up @@ -419,7 +421,7 @@ func toCString(s string) *C.char {
return C.CString(s)
}

func (m *macosOpenFileDialog) show() ([]string, error) {
func (m *macosOpenFileDialog) show() (chan string, error) {
openFileResponses[m.dialog.id] = make(chan string)
nsWindow := unsafe.Pointer(nil)
if m.dialog.window != nil {
Expand All @@ -445,7 +447,6 @@ func (m *macosOpenFileDialog) show() ([]string, error) {
}
filterPatterns = strings.Join(allPatterns, ";")
}

C.showOpenFileDialog(C.uint(m.dialog.id),
C.bool(m.dialog.canChooseFiles),
C.bool(m.dialog.canChooseDirectories),
Expand All @@ -462,11 +463,8 @@ func (m *macosOpenFileDialog) show() ([]string, error) {
toCString(m.dialog.directory),
toCString(m.dialog.buttonText),
nsWindow)
var result []string
for filename := range openFileResponses[m.dialog.id] {
result = append(result, filename)
}
return result, nil

return openFileResponses[m.dialog.id], nil
}

//export openFileDialogCallback
Expand Down Expand Up @@ -504,7 +502,7 @@ func newSaveFileDialogImpl(d *SaveFileDialogStruct) *macosSaveFileDialog {
}
}

func (m *macosSaveFileDialog) show() (string, error) {
func (m *macosSaveFileDialog) show() (chan string, error) {
saveFileResponses[m.dialog.id] = make(chan string)
nsWindow := unsafe.Pointer(nil)
if m.dialog.window != nil {
Expand All @@ -524,7 +522,7 @@ func (m *macosSaveFileDialog) show() (string, error) {
toCString(m.dialog.buttonText),
toCString(m.dialog.filename),
nsWindow)
return <-saveFileResponses[m.dialog.id], nil
return saveFileResponses[m.dialog.id], nil
}

//export saveFileDialogCallback
Expand Down
4 changes: 2 additions & 2 deletions v3/pkg/application/dialogs_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func newOpenFileDialogImpl(d *OpenFileDialogStruct) *linuxOpenFileDialog {
}
}

func (m *linuxOpenFileDialog) show() ([]string, error) {
func (m *linuxOpenFileDialog) show() (chan string, error) {
return runOpenFileDialog(m.dialog)
}

Expand All @@ -67,6 +67,6 @@ func newSaveFileDialogImpl(d *SaveFileDialogStruct) *linuxSaveFileDialog {
}
}

func (m *linuxSaveFileDialog) show() (string, error) {
func (m *linuxSaveFileDialog) show() (chan string, error) {
return runSaveFileDialog(m.dialog)
}
23 changes: 18 additions & 5 deletions v3/pkg/application/dialogs_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func getDefaultFolder(folder string) (string, error) {
return filepath.Abs(folder)
}

func (m *windowOpenFileDialog) show() ([]string, error) {
func (m *windowOpenFileDialog) show() (chan string, error) {

defaultFolder, err := getDefaultFolder(m.dialog.directory)
if err != nil {
Expand Down Expand Up @@ -133,7 +133,14 @@ func (m *windowOpenFileDialog) show() ([]string, error) {
result = []string{temp.(string)}
}

return result, nil
files := make(chan string)
go func() {
for _, file := range result {
files <- file
}
close(files)
}()
return files, nil
}

type windowSaveFileDialog struct {
Expand All @@ -146,10 +153,12 @@ func newSaveFileDialogImpl(d *SaveFileDialogStruct) *windowSaveFileDialog {
}
}

func (m *windowSaveFileDialog) show() (string, error) {
func (m *windowSaveFileDialog) show() (chan string, error) {
files := make(chan string)
defaultFolder, err := getDefaultFolder(m.dialog.directory)
if err != nil {
return "", err
close(files)
return files, err
}

config := cfd.DialogConfig{
Expand All @@ -164,7 +173,11 @@ func (m *windowSaveFileDialog) show() (string, error) {
func() (cfd.Dialog, error) {
return cfd.NewSaveFileDialog(config)
}, false)
return result.(string), nil
go func() {
files <- result.(string)
close(files)
}()
return files, err
}

func calculateMessageDialogFlags(options MessageDialogOptions) uint32 {
Expand Down
20 changes: 20 additions & 0 deletions v3/pkg/application/events_common_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//go:build linux

package application

import "github.com/wailsapp/wails/v3/pkg/events"

var commonApplicationEventMap = map[events.ApplicationEventType]events.ApplicationEventType{
events.Linux.SystemThemeChanged: events.Common.ThemeChanged,
}

func (m *linuxApp) setupCommonEvents() {
for sourceEvent, targetEvent := range commonApplicationEventMap {
sourceEvent := sourceEvent
targetEvent := targetEvent
m.parent.On(sourceEvent, func(event *Event) {
event.Id = uint(targetEvent)
applicationEvents <- event
})
}
}
Loading

0 comments on commit 9ac6359

Please sign in to comment.