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

examples: various cleanups #1515

Merged
merged 7 commits into from
Oct 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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
3 changes: 1 addition & 2 deletions examples/basics/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use gtk::glib;
use gtk::prelude::*;
use gtk::{glib, prelude::*};

fn main() -> glib::ExitCode {
let application = gtk::Application::builder()
Expand Down
20 changes: 9 additions & 11 deletions examples/builder_pattern/main.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,28 @@
use gtk::prelude::*;
use gtk::{glib, Align, Application, ApplicationWindow, Button};
use gtk::{glib, prelude::*};

fn main() -> glib::ExitCode {
let application = Application::new(
Some("com.github.gtk-rs.examples.builder_pattern"),
Default::default(),
);
let application = gtk::Application::builder()
.application_id("com.github.gtk-rs.examples.builder_pattern")
.build();
application.connect_activate(build_ui);
application.run()
}

fn build_ui(application: &Application) {
let window = ApplicationWindow::builder()
fn build_ui(application: &gtk::Application) {
let window = gtk::ApplicationWindow::builder()
.application(application)
.title("First GTK Program")
.default_width(350)
.default_height(70)
.build();

let button = Button::builder()
let button = gtk::Button::builder()
.margin_top(10)
.margin_bottom(10)
.margin_start(10)
.margin_end(10)
.halign(Align::Center)
.valign(Align::Center)
.halign(gtk::Align::Center)
.valign(gtk::Align::Center)
.label("Click Me!")
.build();

Expand Down
8 changes: 5 additions & 3 deletions examples/clipboard/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use glib::clone;
use gtk::prelude::*;
use gtk::{gdk, gio, glib};
use gtk::{
gdk, gio,
glib::{self, clone},
prelude::*,
};

fn main() -> glib::ExitCode {
let application = gtk::Application::builder()
Expand Down
18 changes: 9 additions & 9 deletions examples/clock/main.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
use chrono::Local;
use gtk::glib;
use gtk::prelude::*;
use gtk::{Application, ApplicationWindow, Label};
use gtk::{glib, prelude::*};

fn main() -> glib::ExitCode {
let application =
Application::new(Some("com.github.gtk-rs.examples.clock"), Default::default());
let application = gtk::Application::builder()
.application_id("com.github.gtk-rs.examples.clock")
.build();
application.connect_activate(build_ui);
application.run()
}

fn build_ui(application: &Application) {
let window = ApplicationWindow::new(application);
fn build_ui(application: &gtk::Application) {
let window = gtk::ApplicationWindow::new(application);

window.set_title(Some("Clock Example"));
window.set_default_size(260, 40);

let time = current_time();
let label = Label::new(None);
let label = gtk::Label::default();
label.set_text(&time);

window.set_child(Some(&label));

window.present();

// we are using a closure to capture the label (else we could also use a normal function)
// we are using a closure to capture the label (else we could also use a normal
// function)
let tick = move || {
let time = current_time();
label.set_text(&time);
Expand Down
8 changes: 4 additions & 4 deletions examples/column_view_datagrid/grid_cell/imp.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use gtk::glib;
use gtk::subclass::prelude::*;
use gtk::{glib, subclass::prelude::*};

#[derive(Debug, Default, gtk::CompositeTemplate)]
#[template(file = "grid_cell.ui")]
Expand All @@ -18,8 +17,9 @@ impl ObjectSubclass for GridCell {
type ParentType = gtk::Widget;

fn class_init(klass: &mut Self::Class) {
// When inheriting from GtkWidget directly, you have to either override the size_allocate/measure
// functions of WidgetImpl trait or use a layout manager which provides those functions for your widgets like below.
// When inheriting from GtkWidget directly, you have to either override the
// size_allocate/measure functions of WidgetImpl trait or use a layout
// manager which provides those functions for your widgets like below.
klass.set_layout_manager_type::<gtk::BinLayout>();
klass.bind_template();
}
Expand Down
17 changes: 6 additions & 11 deletions examples/column_view_datagrid/grid_cell/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
mod imp;
use gtk::glib;
use gtk::subclass::prelude::*;
use gtk::{glib, subclass::prelude::*};

pub struct Entry {
pub name: String,
}

glib::wrapper! {
pub struct GridCell(ObjectSubclass<imp::GridCell>)
Expand All @@ -9,19 +12,11 @@ glib::wrapper! {

impl Default for GridCell {
fn default() -> Self {
Self::new()
glib::Object::new()
}
}

pub struct Entry {
pub name: String,
}

impl GridCell {
pub fn new() -> Self {
glib::Object::new()
}

pub fn set_entry(&self, entry: &Entry) {
self.imp().name.set_text(Some(&entry.name));
}
Expand Down
16 changes: 9 additions & 7 deletions examples/column_view_datagrid/main.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
mod grid_cell;

use crate::grid_cell::Entry;
use crate::grid_cell::GridCell;
use gtk::glib::BoxedAnyObject;
use gtk::prelude::*;
use gtk::{gio, glib};
use gtk::{
gio,
glib::{self, BoxedAnyObject},
prelude::*,
};

use crate::grid_cell::{Entry, GridCell};

struct Row {
col1: String,
Expand Down Expand Up @@ -44,7 +46,7 @@ fn build_ui(application: &gtk::Application) {
let col2factory = gtk::SignalListItemFactory::new();
col1factory.connect_setup(move |_factory, item| {
let item = item.downcast_ref::<gtk::ListItem>().unwrap();
let row = GridCell::new();
let row = GridCell::default();
item.set_child(Some(&row));
});

Expand All @@ -60,7 +62,7 @@ fn build_ui(application: &gtk::Application) {
});
col2factory.connect_setup(move |_factory, item| {
let item = item.downcast_ref::<gtk::ListItem>().unwrap();
let row = GridCell::new();
let row = GridCell::default();
item.set_child(Some(&row));
});

Expand Down
6 changes: 2 additions & 4 deletions examples/composite_template/ex_application_window/imp.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
use gtk::prelude::*;
use gtk::subclass::prelude::*;
use gtk::{glib, CompositeTemplate};
use gtk::{glib, prelude::*, subclass::prelude::*};

use crate::ex_menu_button::ExMenuButton;

/// The private struct, which can hold widgets and other data.
#[derive(Debug, Default, CompositeTemplate)]
#[derive(Debug, Default, gtk::CompositeTemplate)]
#[template(file = "ex_application_window.ui")]
pub struct ExApplicationWindow {
// The #[template_child] attribute tells the CompositeTemplate macro
Expand Down
8 changes: 4 additions & 4 deletions examples/composite_template/ex_application_window/mod.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
mod imp;

use gtk::subclass::prelude::*;
use gtk::{gio, glib};
use gtk::{gio, glib, prelude::*, subclass::prelude::*};

glib::wrapper! {
pub struct ExApplicationWindow(ObjectSubclass<imp::ExApplicationWindow>)
@extends gtk::Widget, gtk::Window, gtk::ApplicationWindow, @implements gio::ActionMap, gio::ActionGroup;
@extends gtk::Widget, gtk::Window, gtk::ApplicationWindow,
@implements gio::ActionMap, gio::ActionGroup;
}

impl ExApplicationWindow {
pub fn new<P: glib::IsA<gtk::Application>>(app: &P) -> Self {
pub fn new<P: IsA<gtk::Application>>(app: &P) -> Self {
glib::Object::builder().property("application", app).build()
}

Expand Down
6 changes: 2 additions & 4 deletions examples/composite_template/ex_menu_button/imp.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use gtk::prelude::*;
use gtk::subclass::prelude::*;
use gtk::{glib, CompositeTemplate};
use gtk::{glib, prelude::*, subclass::prelude::*};

#[derive(Debug, Default, CompositeTemplate)]
#[derive(Debug, Default, gtk::CompositeTemplate)]
#[template(file = "ex_menu_button.ui")]
pub struct ExMenuButton {
#[template_child]
Expand Down
3 changes: 2 additions & 1 deletion examples/composite_template/ex_menu_button/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ mod imp;
use gtk::glib;

glib::wrapper! {
pub struct ExMenuButton(ObjectSubclass<imp::ExMenuButton>) @extends gtk::Widget;
pub struct ExMenuButton(ObjectSubclass<imp::ExMenuButton>)
@extends gtk::Widget;
}
3 changes: 1 addition & 2 deletions examples/composite_template/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ mod ex_application_window;
pub mod ex_menu_button;

use ex_application_window::ExApplicationWindow;
use gtk::glib;
use gtk::prelude::*;
use gtk::{glib, prelude::*};

fn main() -> glib::ExitCode {
let application = gtk::Application::builder()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use crate::Explosion;
use gtk::glib;
use gtk::subclass::prelude::*;
use std::cell::{Cell, RefCell};

use gtk::{glib, subclass::prelude::*};

use crate::Explosion;

#[derive(Default)]
pub struct AnimatedExplosion {
pub(super) explosion: RefCell<Option<Explosion>>,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
mod imp;

use gtk::{gdk, glib, graphene, prelude::*, subclass::prelude::*};

use crate::{Explosion, ExplosionParameters};
use glib::subclass::prelude::*;
use gtk::prelude::*;
use gtk::{gdk, glib, graphene};

glib::wrapper! {
pub struct AnimatedExplosion(ObjectSubclass<imp::AnimatedExplosion>);
Expand Down
8 changes: 4 additions & 4 deletions examples/confetti_snapshot_animation/confetti_widget/imp.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::{cell::RefCell, collections::HashSet};

use gtk::{glib, subclass::prelude::*};

use crate::AnimatedExplosion;
use gtk::glib;
use gtk::subclass::prelude::*;
use std::cell::RefCell;
use std::collections::HashSet;

#[derive(Default)]
pub struct ConfettiWidget {
Expand Down
15 changes: 7 additions & 8 deletions examples/confetti_snapshot_animation/confetti_widget/mod.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
mod imp;

use gtk::{
glib::{self, clone, ControlFlow},
prelude::*,
subclass::prelude::*,
};

use crate::{AnimatedExplosion, ExplosionParameters};
use glib::subclass::prelude::*;
use glib::{clone, ControlFlow};
use gtk::glib;
use gtk::prelude::*;

glib::wrapper! {
pub struct ConfettiWidget(ObjectSubclass<imp::ConfettiWidget>) @implements gtk::Widget;
}

impl Default for ConfettiWidget {
fn default() -> Self {
Self::new()
glib::Object::new()
}
}

impl ConfettiWidget {
pub fn new() -> Self {
glib::Object::new()
}
pub fn explode(&self, params: ExplosionParameters, duration: f64) -> AnimatedExplosion {
let exp = AnimatedExplosion::new(params);

Expand Down
3 changes: 2 additions & 1 deletion examples/confetti_snapshot_animation/explosion.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// Contains the needed data to run a simple particle simulation

use std::f32::consts::PI;

use glib::random_double;
use graphene::Vec2;
use gtk::{gdk, glib, graphene};
use std::f32::consts::PI;

pub struct ExplosionParameters {
pub quantity: usize,
Expand Down
8 changes: 4 additions & 4 deletions examples/confetti_snapshot_animation/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ pub use animated_explosion::*;
pub use confetti_widget::*;
pub use explosion::*;
use graphene::Vec2;
use gtk::prelude::*;
use gtk::{glib, graphene};
use gtk::{glib, graphene, prelude::*};

fn main() -> glib::ExitCode {
let application = gtk::Application::builder()
Expand All @@ -23,10 +22,11 @@ fn build_ui(application: &gtk::Application) {
window.set_title(Some("Confetti"));
window.set_default_size(640, 360);

let confetti = ConfettiWidget::new();
let confetti = ConfettiWidget::default();
window.set_child(Some(&confetti));

// To listen to click events, we need to add a `GestureClick` controller to our `ConfettiWidget`
// To listen to click events, we need to add a `GestureClick` controller to our
// `ConfettiWidget`
let ev_ctrl = gtk::GestureClick::new();
ev_ctrl.connect_pressed(move |event, _, x, y| {
let confetti = event.widget().downcast::<ConfettiWidget>().unwrap();
Expand Down
8 changes: 3 additions & 5 deletions examples/content_provider/content_provider/imp.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use gdk::subclass::prelude::*;
use gtk::prelude::*;
use gtk::{gdk, gio, glib};
use std::future::Future;
use std::pin::Pin;
use std::{future::Future, pin::Pin};

use gtk::{gdk, gio, glib, prelude::*, subclass::prelude::*};

#[derive(Default)]
pub struct ContentProvider {}
Expand Down
11 changes: 3 additions & 8 deletions examples/content_provider/content_provider/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,12 @@ mod imp;
use gtk::{gdk, glib};

glib::wrapper! {
pub struct ContentProvider(ObjectSubclass<imp::ContentProvider>) @extends gdk::ContentProvider;
}

impl ContentProvider {
pub fn new() -> Self {
glib::Object::new()
}
pub struct ContentProvider(ObjectSubclass<imp::ContentProvider>)
@extends gdk::ContentProvider;
}

impl Default for ContentProvider {
fn default() -> Self {
Self::new()
glib::Object::new()
}
}
Loading
Loading