Skip to content

canonical/gtk.dart

Repository files navigation

GTK+ utilities for Flutter Linux applications

license: MPL CI codecov

GtkSettings

GtkSettings provide a mechanism to share global settings between applications on Linux.

screenshot

GtkApplication

GtkApplication allows the primary Flutter GTK application instance to listen to remote application instances' command-line arguments and file open requests.

NOTE: linux/my_application.cc must be modified for this package to be able to function. See "Getting Started" below for details.
gtk-application.webm

Getting Started

Apply the following changes to linux/my_application.cc:

my_application_activate(): activate an existing window if present
diff --git a/example/linux/my_application.cc b/example/linux/my_application.cc
index 5cd43c6..94e7215 100644
--- a/linux/my_application.cc
+++ b/linux/my_application.cc
@@ -20,6 +20,12 @@ static void my_application_activate(GApplication* application) {
   GtkWindow* window =
       GTK_WINDOW(gtk_application_window_new(GTK_APPLICATION(application)));
 
+  GList* windows = gtk_application_get_windows(GTK_APPLICATION(application));
+  if (windows) {
+    gtk_window_present(GTK_WINDOW(windows->data));
+    return;
+  }
+
   // Use a header bar when running in GNOME as this is the common style used
   // by applications and is the setup most users will be using (e.g. Ubuntu
   // desktop).
my_application_local_command_line(): return FALSE to allow the package to handle the command line
--- a/linux/my_application.cc
+++ b/linux/my_application.cc
@@ -81,7 +81,7 @@ static gboolean my_application_local_command_line(GApplication* application,
   g_application_activate(application);
   *exit_status = 0;
 
-  return TRUE;
+  return FALSE;
 }
 
 // Implements GObject::dispose.
my_application_new(): replace G_APPLICATION_NON_UNIQUE with G_APPLICATION_HANDLES_COMMAND_LINE and G_APPLICATION_HANDLES_OPEN flags
--- a/linux/my_application.cc
+++ b/linux/my_application.cc
@@ -101,7 +101,8 @@ static void my_application_class_init(MyApplicationClass* klass) {
 static void my_application_init(MyApplication* self) {}
 
 MyApplication* my_application_new() {
-  return MY_APPLICATION(g_object_new(my_application_get_type(),
-                                     "application-id", APPLICATION_ID, "flags",
-                                     G_APPLICATION_NON_UNIQUE, nullptr));
+  return MY_APPLICATION(g_object_new(
+      my_application_get_type(), "application-id", APPLICATION_ID, "flags",
+      G_APPLICATION_HANDLES_COMMAND_LINE | G_APPLICATION_HANDLES_OPEN,
+      nullptr));
 }

Examples

The GtkApplication widget allows listening to remote application instances' command-line arguments and file open requests from within the widget tree.

import 'package:flutter/material.dart';
import 'package:gtk_application/gtk_application.dart';

void main() {
  runApp(
    MaterialApp(
      home: GtkApplication(
        onCommandLine: (args) => print('command-line: $args'),
        onOpen: (files, hint) => print('open ($hint): $files'),
        child: // ...
      ),
    ),
  );
}

The GtkApplicationNotifier object allows listening to remote application instances' command-line arguments and file open requests outside the widget tree.

import 'package:flutter/widgets.dart';
import 'package:gtk_application/gtk_application.dart';

void main() {
  WidgetsFlutterBinding.ensureInitialized();

  final notifier = GtkApplicationNotifier();
  notifier.addCommandLineListener((args) {
    print('command-line: $args');
  });
  notifier.addOpenListener((files, hint) {
    print('open ($hint): $files');
  });

  // ...
  // notifier.dispose();
}