Skip to content

Commit

Permalink
add RunNative on desktop.Window
Browse files Browse the repository at this point in the history
  • Loading branch information
dweymouth committed May 21, 2024
1 parent aecc4f0 commit 385652c
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 2 deletions.
8 changes: 8 additions & 0 deletions driver/desktop/window.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package desktop

// Window defines the desktop specific extensions to a fyne.Window.
//
// Since: 2.5
type Window interface {
RunNative(func(ctx any) error) error
}
14 changes: 12 additions & 2 deletions driver/native.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
package driver

// AndroidContext is passed to the `RunNative` callback when it is executed on an Android device.
// AndroidContext is passed to the `driver.RunNative` callback when it is executed on an Android device.
// The VM, Env and Ctx pointers are reqiured to make various calls into JVM methods.
//
// Since: 2.3
type AndroidContext struct {
VM, Env, Ctx uintptr
}

// UnknownContext is passed to the `RunNative` callback when it is executed on devices without special native context.
// UnknownContext is passed to the RunNative callback when it is executed
// on devices without special native context.
//
// Since: 2.3
type UnknownContext struct{}

// WindowsContext is passed to the `(desktop.Window).RunNative` callback
// when it is executed on a Microsoft Windows desktop device.
//
// Since: 2.5
type WindowsContext struct {
// HWND is the WinAPI HWND for the window.
HWND uintptr
}
16 changes: 16 additions & 0 deletions internal/driver/glfw/window_notwindows.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ package glfw

import (
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/driver"
"fyne.io/fyne/v2/driver/desktop"
"fyne.io/fyne/v2/internal/scale"
)

Expand All @@ -13,3 +15,17 @@ func (w *window) setDarkMode() {
func (w *window) computeCanvasSize(width, height int) fyne.Size {
return fyne.NewSize(scale.ToFyneCoordinate(w.canvas, width), scale.ToFyneCoordinate(w.canvas, height))
}

// assert we are implementing desktop.Window
var _ desktop.Window = (*window)(nil)

func (w *window) RunNative(f func(any) error) error {
var err error
done := make(chan struct{})
runOnMain(func() {
err = f(driver.UnknownContext{})
close(done)
})
<-done
return err
}
18 changes: 18 additions & 0 deletions internal/driver/glfw/window_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"unsafe"

"fyne.io/fyne/v2"
"fyne.io/fyne/v2/driver"
"fyne.io/fyne/v2/driver/desktop"
"fyne.io/fyne/v2/internal/scale"

"golang.org/x/sys/windows/registry"
Expand Down Expand Up @@ -50,3 +52,19 @@ func (w *window) computeCanvasSize(width, height int) fyne.Size {
}
return fyne.NewSize(scale.ToFyneCoordinate(w.canvas, width), scale.ToFyneCoordinate(w.canvas, height))
}

// assert we are implementing desktop.Window
var _ desktop.Window = (*window)(nil)

func (w *window) RunNative(f func(any) error) error {
var err error
done := make(chan struct{})
runOnMain(func() {
err = f(driver.WindowsContext{
HWND: uintptr(unsafe.Pointer(w.view().GetWin32Window())),
})
close(done)
})
<-done
return err
}

0 comments on commit 385652c

Please sign in to comment.