Skip to content

Conversation

geoah
Copy link
Contributor

@geoah geoah commented Mar 27, 2020

I needed a way to detect whether the window got in or out of focus in order to enable/disable notifications, re-check connections and other things, so I wanted to expose the GLFW SetFocusCallback method.

Please let me know if there is a better way of doing this or feel free to close the PR if you are not interested in merging something like this.

Thank you :)

@geoah
Copy link
Contributor Author

geoah commented Mar 27, 2020

I'm also interested in exposing window.SetDropCallback to allow drag-dropping so any ways to improve the way we expose GLFW callbacks would be appreciated.

@pchampio pchampio closed this Mar 27, 2020
@pchampio
Copy link
Member

pchampio commented Mar 27, 2020

Such feature are already exposed through wiki: GLFWPlugins

@pchampio
Copy link
Member

pchampio commented Mar 27, 2020

option.go

package main

import (
	"github.com/go-flutter-desktop/go-flutter"
	"github.com/go-flutter-desktop/go-flutter/plugin"
	"github.com/go-gl/glfw/v3.3/glfw"
)

var options = []flutter.Option{
	flutter.WindowInitialDimensions(400, 300),
	flutter.AddPlugin(&FocusCallback{}),
}

// FocusCallback is a GLFWplugin
type FocusCallback struct {
	channel *plugin.MethodChannel
}

var _ flutter.Plugin = &FocusCallback{}     // compile-time type check
var _ flutter.PluginGLFW = &FocusCallback{} // compile-time type check
// FocusCallback struct must implement InitPlugin and InitPluginGLFW

// InitPlugin creates a MethodChannel for "samples.go-flutter.dev/focus"
func (p *FocusCallback) InitPlugin(messenger plugin.BinaryMessenger) error {
	p.channel = plugin.NewMethodChannel(messenger, "samples.go-flutter.dev/focus", plugin.StandardMethodCodec{})
	return nil
}

// InitPluginGLFW is used to gain control over the glfw.Window
func (p *FocusCallback) InitPluginGLFW(window *glfw.Window) error {
	window.SetFocusCallback(func(_ *glfw.Window, focused bool) {
		p.channel.InvokeMethod("push/focus", map[interface{}]interface{}{
			"focused": focused,
		})
	})
	return nil
}

Dart code:

  const platform_channel_draggable =
      MethodChannel('samples.go-flutter.dev/focus');
  platform_channel_draggable
      .setMethodCallHandler((MethodCall methodCall) async {
    if (methodCall.method == "push/focus") {
      print("Focused: " + methodCall.arguments["focused"].toString());
    }
  });

@pchampio
Copy link
Member

Another example for window.SetDropCallback:

option.go:
(golang []string needs to be converted to []interface{} more info here)

package main

import (
	"github.com/go-flutter-desktop/go-flutter"
	"github.com/go-flutter-desktop/go-flutter/plugin"
	"github.com/go-gl/glfw/v3.3/glfw"
)

var options = []flutter.Option{
	flutter.WindowInitialDimensions(400, 300),
	flutter.AddPlugin(&FocusCallback{}),
}

// FocusCallback is a plugin that makes moving the bordreless window possible
type FocusCallback struct {
	channel *plugin.MethodChannel
}

var _ flutter.Plugin = &FocusCallback{}     // compile-time type check
var _ flutter.PluginGLFW = &FocusCallback{} // compile-time type check
// FocusCallback struct must implement InitPlugin and InitPluginGLFW

// InitPlugin creates a MethodChannel for "samples.go-flutter.dev/draggable"
func (p *FocusCallback) InitPlugin(messenger plugin.BinaryMessenger) error {
	p.channel = plugin.NewMethodChannel(messenger, "samples.go-flutter.dev/glfw", plugin.StandardMethodCodec{})
	return nil
}

// InitPluginGLFW is used to gain control over the glfw.Window
func (p *FocusCallback) InitPluginGLFW(window *glfw.Window) error {
	window.SetFocusCallback(func(_ *glfw.Window, focused bool) {
		p.channel.InvokeMethod("push/focus", map[interface{}]interface{}{
			"focused": focused,
		})
	})
	window.SetDropCallback(func(_ *glfw.Window, names []string) {
		//converting a []string to a []interface{}
		y := make([]interface{}, len(names))
		for i, v := range names {
			y[i] = v
		}
		p.channel.InvokeMethod("push/drop", map[interface{}]interface{}{
			"drop": y,
		})
	})
	return nil
}

dart:

  const platform_channel_draggable =
      MethodChannel('samples.go-flutter.dev/glfw');
  platform_channel_draggable
      .setMethodCallHandler((MethodCall methodCall) async {
    if (methodCall.method == "push/focus") {
      print("Focused: " + methodCall.arguments["focused"].toString());
    }
    if (methodCall.method == "push/drop") {
      print("dropped: " + methodCall.arguments.toString());
    }
  });

@geoah
Copy link
Contributor Author

geoah commented Mar 27, 2020

@pchampio thank you, sorry I'm totally blind. :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants