[gtk-web/issue-56] Replace destroy() method with the more idiomatic close()




commit 7ecb3ce64fe77eabb5c9771db3c6379ab5974eba
Author: Emmanuele Bassi <ebassi gnome org>
Date:   Thu Aug 27 16:53:44 2020 +0100

    Replace destroy() method with the more idiomatic close()
    
    Use `gtk_window_close()` on a `GtkWindow` instance is more idiomatic
    than calling `gtk_widget_destroy()`, and it's more in line with GTK4,
    once we've released it.
    
    For some languages, like Rust, the destroy() method is unsafe, as it
    operates on the reference counting of the C instance.
    
    Fixes: #56

 _data/sample_codes.yml           | 16 ++++++++--------
 collections/_docs/hello-world.md | 14 +++++++-------
 collections/_docs/javascript.md  |  2 +-
 collections/_docs/python.md      |  2 +-
 collections/_docs/rust.md        |  2 +-
 5 files changed, 18 insertions(+), 18 deletions(-)
---
diff --git a/_data/sample_codes.yml b/_data/sample_codes.yml
index dce34a6..360a11c 100644
--- a/_data/sample_codes.yml
+++ b/_data/sample_codes.yml
@@ -16,7 +16,7 @@ codes:
           // … with a button in it …
           let btn = new Gtk.Button({ label: 'Hello, World!' });
           // … which closes the window when clicked
-          btn.connect('clicked', () => { win.destroy(); });
+          btn.connect('clicked', () => { win.close(); });
           win.add(btn);
           win.show_all();
       });
@@ -40,7 +40,7 @@ codes:
           # … with a button in it…
           btn = Gtk.Button(label='Hello, World!')
           # … which closes the window when clicked
-          btn.connect('clicked', lambda x: win.destroy())
+          btn.connect('clicked', lambda x: win.close())
           win.add(btn)
           win.show_all()
 
@@ -66,7 +66,7 @@ codes:
           // … with a button in it …
           let button = gtk::Button::new_with_label("Hello World!");
           // … which closes the window when clicked
-          button.connect_clicked(clone!(@weak window => move |_| window.destroy()));
+          button.connect_clicked(clone!(@weak window => move |_| window.close()));
           window.add(&button);
           window.show_all();
       }
@@ -96,9 +96,9 @@ codes:
               // Create a new button
               var button = new Gtk.Button.with_label ("Hello, World!");
 
-              // When the button is clicked, destroy the window
+              // When the button is clicked, close the window
               button.clicked.connect (() => {
-                  window.destroy ();
+                  window.close ();
               });
 
               window.add (button);
@@ -120,8 +120,8 @@ codes:
         GtkWidget *window = gtk_application_window_new (app);
         // Create a new button
         GtkWidget *button = gtk_button_new_with_label ("Hello, World!");
-        // When the button is clicked, destroy the window passed as an argument
-        g_signal_connect_swapped (button, "clicked", G_CALLBACK (gtk_widget_destroy), window);
+        // When the button is clicked, close the window passed as an argument
+        g_signal_connect_swapped (button, "clicked", G_CALLBACK (gtk_window_close), window);
         gtk_container_add (GTK_CONTAINER (window), button);
         gtk_widget_show_all (window);
       }
@@ -156,7 +156,7 @@ codes:
           # … with a button in it …
           my $btn = Gtk3::Button->new('Hello World!');
           # … which closes the window when clicked
-          $btn->signal_connect(clicked => sub { $win->destroy });
+          $btn->signal_connect(clicked => sub { $win->close(); });
           $win->add($btn);
           $win->show_all();
         }
diff --git a/collections/_docs/hello-world.md b/collections/_docs/hello-world.md
index c2a6a22..ee43ff6 100644
--- a/collections/_docs/hello-world.md
+++ b/collections/_docs/hello-world.md
@@ -57,7 +57,7 @@ activate (GtkApplication *app,
 
   button = gtk_button_new_with_label ("Hello World");
   g_signal_connect (button, "clicked", G_CALLBACK (print_hello), NULL);
-  g_signal_connect_swapped (button, "clicked", G_CALLBACK (gtk_widget_destroy), window);
+  g_signal_connect_swapped (button, "clicked", G_CALLBACK (gtk_window_close), window);
   gtk_container_add (GTK_CONTAINER (button_box), button);
 
   gtk_widget_show_all (window);
@@ -180,13 +180,13 @@ functions is similar to a [`g_signal_connect()`](https://developer.gnome.org/gob
 with the difference lying in how the callback function is treated.
 Using `g_signal_connect_swapped()` allows you to specify what the callback
 function should take as the instance parameter by letting you pass it as
-data. In this case the function being called back is `gtk_widget_destroy()`
+data. In this case the function being called back is `gtk_window_close()`
 and the `window` pointer is passed to it. This has the effect that when the
-button is clicked, the whole GTK window is destroyed. In contrast, if a
-normal `g_signal_connect()` were used to connect the "clicked" signal with
-`gtk_widget_destroy()`, then the button would have been destroyed, not the
-window. More information about creating buttons can be found
-[here](https://wiki.gnome.org/HowDoI/Buttons).
+button is clicked, the window is closed as if the "close" button had been
+pressed. In contrast, if a normal `g_signal_connect()` were used to connect
+the "clicked" signal with `gtk_window_close()`, then GTK would warn that
+the button is not a `GtkWindow` instance. More information about creating
+buttons can be found [here](https://wiki.gnome.org/HowDoI/Buttons).
 
 ## Next steps
 
diff --git a/collections/_docs/javascript.md b/collections/_docs/javascript.md
index 8b18292..b9eb8ca 100644
--- a/collections/_docs/javascript.md
+++ b/collections/_docs/javascript.md
@@ -33,7 +33,7 @@ let app = new Gtk.Application({ application_id: 'org.gtk.ExampleApp' });
 app.connect('activate', () => {
     let win = new Gtk.ApplicationWindow({ application: app });
     let btn = new Gtk.Button({ label: 'Hello, World!' });
-    btn.connect('clicked', () => { win.destroy(); });
+    btn.connect('clicked', () => { win.close(); });
     win.add(btn);
     win.show_all();
 });
diff --git a/collections/_docs/python.md b/collections/_docs/python.md
index b44528f..e439c9e 100644
--- a/collections/_docs/python.md
+++ b/collections/_docs/python.md
@@ -37,7 +37,7 @@ from gi.repository import Gtk
 def on_activate(app):
     win = Gtk.ApplicationWindow(application=app)
     btn = Gtk.Button(label="Hello, World!")
-    btn.connect('clicked', lambda x: win.destroy())
+    btn.connect('clicked', lambda x: win.close())
     win.add(btn)
     win.show_all()
 
diff --git a/collections/_docs/rust.md b/collections/_docs/rust.md
index f738b40..3c59caf 100644
--- a/collections/_docs/rust.md
+++ b/collections/_docs/rust.md
@@ -29,7 +29,7 @@ fn on_activate(application: &gtk::Application) {
     // … with a button in it …
     let button = gtk::Button::new_with_label("Hello World!");
     // … which closes the window when clicked
-    button.connect_clicked(clone!(@weak window => move |_| window.destroy()));
+    button.connect_clicked(clone!(@weak window => move |_| window.close()));
     window.add(&button);
     window.show_all();
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]