Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

book: Extend memory management chapter #1459

Merged
merged 4 commits into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions book/listings/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ path = "g_object_memory_management/3/main.rs"
name = "g_object_memory_management_4"
path = "g_object_memory_management/4/main.rs"

[[bin]]
name = "g_object_memory_management_5"
path = "g_object_memory_management/5/main.rs"

# g_object_properties
[[bin]]
name = "g_object_properties_1"
Expand Down
42 changes: 13 additions & 29 deletions book/listings/g_object_memory_management/1/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use std::cell::Cell;
use std::rc::Rc;

use gtk::prelude::*;
use gtk::{self, glib, Application, ApplicationWindow, Button, Orientation};
use gtk::{glib, Application, ApplicationWindow, Button};
use std::cell::Cell;

const APP_ID: &str = "org.gtk_rs.GObjectMemoryManagement1";

Expand All @@ -16,7 +14,9 @@ fn main() -> glib::ExitCode {
// Run the application
app.run()
}
fn build_ui(app: &Application) {

// ANCHOR: build_ui
fn build_ui(application: &Application) {
// Create two buttons
let button_increase = Button::builder()
.label("Increase")
Expand All @@ -25,38 +25,22 @@ fn build_ui(app: &Application) {
.margin_start(12)
.margin_end(12)
.build();
let button_decrease = Button::builder()
.label("Decrease")
.margin_top(12)
.margin_bottom(12)
.margin_start(12)
.margin_end(12)
.build();

// ANCHOR: callback
// Reference-counted object with inner-mutability
let number = Rc::new(Cell::new(0));
// A mutable integer
let number = Cell::new(0);

// Connect callbacks, when a button is clicked `number` will be changed
let number_copy = number.clone();
button_increase.connect_clicked(move |_| number_copy.set(number_copy.get() + 1));
button_decrease.connect_clicked(move |_| number.set(number.get() - 1));
// ANCHOR_END: callback

// Add buttons to `gtk_box`
let gtk_box = gtk::Box::builder()
.orientation(Orientation::Vertical)
.build();
gtk_box.append(&button_increase);
gtk_box.append(&button_decrease);
// Connect callbacks
// When a button is clicked, `number` should be changed
button_increase.connect_clicked(move |_| number.set(number.get() + 1));

// Create a window
let window = ApplicationWindow::builder()
.application(app)
.application(application)
.title("My GTK App")
.child(&gtk_box)
.child(&button_increase)
.build();

// Present the window
window.present();
}
// ANCHOR_END: build_ui
18 changes: 7 additions & 11 deletions book/listings/g_object_memory_management/2/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::cell::Cell;
use std::rc::Rc;

use glib::clone;
use gtk::prelude::*;
use gtk::{self, glib, Application, ApplicationWindow, Button, Orientation};

Expand Down Expand Up @@ -34,17 +33,14 @@ fn build_ui(app: &Application) {
.margin_end(12)
.build();

// Reference-counted object with inner mutability
let number = Rc::new(Cell::new(0));
// Connect callbacks
// When a button is clicked, `number` will be changed
// ANCHOR: callback
button_increase.connect_clicked(clone!(@strong number => move |_| {
number.set(number.get() + 1);
}));
button_decrease.connect_clicked(move |_| {
number.set(number.get() - 1);
});
// Reference-counted object with inner-mutability
let number = Rc::new(Cell::new(0));

// Connect callbacks, when a button is clicked `number` will be changed
let number_copy = number.clone();
button_increase.connect_clicked(move |_| number_copy.set(number_copy.get() + 1));
button_decrease.connect_clicked(move |_| number.set(number.get() - 1));
// ANCHOR_END: callback

// Add buttons to `gtk_box`
Expand Down
21 changes: 8 additions & 13 deletions book/listings/g_object_memory_management/3/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ fn main() -> glib::ExitCode {
// Run the application
app.run()
}

fn build_ui(app: &Application) {
// Create two buttons
let button_increase = Button::builder()
Expand All @@ -35,21 +34,17 @@ fn build_ui(app: &Application) {
.margin_end(12)
.build();

// Reference-counted object with inner mutability
let number = Rc::new(Cell::new(0));

// ANCHOR: callback
// Connect callbacks
// When a button is clicked, `number` and label of the other button will be changed
button_increase.connect_clicked(clone!(@weak number, @strong button_decrease =>
move |_| {
number.set(number.get() + 1);
button_decrease.set_label(&number.get().to_string());
}));
button_decrease.connect_clicked(clone!(@strong button_increase =>
move |_| {
number.set(number.get() - 1);
button_increase.set_label(&number.get().to_string());
// When a button is clicked, `number` will be changed
// ANCHOR: callback
button_increase.connect_clicked(clone!(@strong number => move |_| {
number.set(number.get() + 1);
}));
button_decrease.connect_clicked(move |_| {
number.set(number.get() - 1);
});
// ANCHOR_END: callback

// Add buttons to `gtk_box`
Expand Down
9 changes: 2 additions & 7 deletions book/listings/g_object_memory_management/4/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,41 +35,36 @@ fn build_ui(app: &Application) {
.margin_end(12)
.build();

// Reference-counted object with inner mutability
let number = Rc::new(Cell::new(0));

// ANCHOR: callback
// Connect callbacks
// When a button is clicked, `number` and label of the other button will be changed
button_increase.connect_clicked(clone!(@weak number, @weak button_decrease =>
button_increase.connect_clicked(clone!(@weak number, @strong button_decrease =>
move |_| {
number.set(number.get() + 1);
button_decrease.set_label(&number.get().to_string());
}));
button_decrease.connect_clicked(clone!(@weak button_increase =>
button_decrease.connect_clicked(clone!(@strong button_increase =>
move |_| {
number.set(number.get() - 1);
button_increase.set_label(&number.get().to_string());
}));
// ANCHOR_END: callback

// ANCHOR: box_append
// Add buttons to `gtk_box`
let gtk_box = gtk::Box::builder()
.orientation(Orientation::Vertical)
.build();
gtk_box.append(&button_increase);
gtk_box.append(&button_decrease);
// ANCHOR_END: box_append

// ANCHOR: window_child
// Create a window
let window = ApplicationWindow::builder()
.application(app)
.title("My GTK App")
.child(&gtk_box)
.build();
// ANCHOR_END: window_child

// Present the window
window.present();
Expand Down
76 changes: 76 additions & 0 deletions book/listings/g_object_memory_management/5/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
use std::cell::Cell;
use std::rc::Rc;

use glib::clone;
use gtk::prelude::*;
use gtk::{self, glib, Application, ApplicationWindow, Button, Orientation};

const APP_ID: &str = "org.gtk_rs.GObjectMemoryManagement5";

fn main() -> glib::ExitCode {
// Create a new application
let app = Application::builder().application_id(APP_ID).build();

// Connect to "activate" signal of `app`
app.connect_activate(build_ui);

// Run the application
app.run()
}

fn build_ui(app: &Application) {
// Create two buttons
let button_increase = Button::builder()
.label("Increase")
.margin_top(12)
.margin_bottom(12)
.margin_start(12)
.margin_end(12)
.build();
let button_decrease = Button::builder()
.label("Decrease")
.margin_top(12)
.margin_bottom(12)
.margin_start(12)
.margin_end(12)
.build();

// Reference-counted object with inner mutability
let number = Rc::new(Cell::new(0));

// ANCHOR: callback
// Connect callbacks
// When a button is clicked, `number` and label of the other button will be changed
button_increase.connect_clicked(clone!(@weak number, @weak button_decrease =>
move |_| {
number.set(number.get() + 1);
button_decrease.set_label(&number.get().to_string());
}));
button_decrease.connect_clicked(clone!(@weak button_increase =>
move |_| {
number.set(number.get() - 1);
button_increase.set_label(&number.get().to_string());
}));
// ANCHOR_END: callback

// ANCHOR: box_append
// Add buttons to `gtk_box`
let gtk_box = gtk::Box::builder()
.orientation(Orientation::Vertical)
.build();
gtk_box.append(&button_increase);
gtk_box.append(&button_decrease);
// ANCHOR_END: box_append

// ANCHOR: window_child
// Create a window
let window = ApplicationWindow::builder()
.application(app)
.title("My GTK App")
.child(&gtk_box)
.build();
// ANCHOR_END: window_child

// Present the window
window.present();
}
Loading
Loading