Skip to content

Commit

Permalink
examples: Prefer using Default trait when possible
Browse files Browse the repository at this point in the history
Otherwise clippy complains and we have to have a separate constructor for nothing useful
  • Loading branch information
bilelmoussaoui committed Oct 16, 2023
1 parent 7a3294d commit 84bfe7c
Show file tree
Hide file tree
Showing 40 changed files with 47 additions and 146 deletions.
14 changes: 5 additions & 9 deletions examples/column_view_datagrid/grid_cell/mod.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,22 @@
mod imp;
use gtk::{glib, subclass::prelude::*};

pub struct Entry {
pub name: String,
}

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

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
4 changes: 2 additions & 2 deletions examples/column_view_datagrid/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,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 @@ -62,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
5 changes: 1 addition & 4 deletions examples/confetti_snapshot_animation/confetti_widget/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,11 @@ glib::wrapper! {

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
2 changes: 1 addition & 1 deletion examples/confetti_snapshot_animation/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ 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
Expand Down
8 changes: 1 addition & 7 deletions examples/content_provider/content_provider/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,8 @@ glib::wrapper! {
@extends gdk::ContentProvider;
}

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

impl Default for ContentProvider {
fn default() -> Self {
Self::new()
glib::Object::new()
}
}
2 changes: 1 addition & 1 deletion examples/content_provider/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fn on_activate(application: &gtk::Application) {
let display = WidgetExt::display(&window);

window.connect_realize(glib::clone!(@weak display, @weak application => move |_| {
let provider = ContentProvider::new();
let provider = ContentProvider::default();
display.clipboard().set_content(Some(&provider)).unwrap();
glib::MainContext::default().spawn_local(glib::clone!(@weak display, @weak application => async move {
let text = display.clipboard().read_text_future().await.unwrap().unwrap();
Expand Down
6 changes: 0 additions & 6 deletions examples/custom_application/ex_application/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@ glib::wrapper! {

impl Default for ExApplication {
fn default() -> Self {
Self::new()
}
}

impl ExApplication {
pub fn new() -> Self {
glib::Object::builder()
.property("application-id", "org.gtk_rs.application-subclass")
.build()
Expand Down
2 changes: 1 addition & 1 deletion examples/custom_application/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ use ex_application::ExApplication;
use gtk::{glib, prelude::*};

fn main() -> glib::ExitCode {
let app = ExApplication::new();
let app = ExApplication::default();
app.run()
}
6 changes: 1 addition & 5 deletions examples/custom_editable/custom_editable/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,11 @@ glib::wrapper! {

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

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

pub fn add_tag(&self, tag: &CustomTag) {
tag.set_parent(self);
}
Expand Down
2 changes: 1 addition & 1 deletion examples/custom_editable/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ fn build_ui(application: &gtk::Application) {
container.set_valign(gtk::Align::Center);
container.set_halign(gtk::Align::Center);

let editable = CustomEditable::new();
let editable = CustomEditable::default();
editable.set_halign(gtk::Align::Fill);

container.append(&editable);
Expand Down
10 changes: 0 additions & 10 deletions examples/custom_layout_manager/custom_layout/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,7 @@ glib::wrapper! {
@extends gtk::LayoutManager;
}

impl Default for CustomLayout {
fn default() -> Self {
Self::new()
}
}

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

pub fn set_position(&self, position: f64) {
self.imp().position.set(position);
}
Expand Down
2 changes: 1 addition & 1 deletion examples/custom_layout_manager/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ fn main() -> glib::ExitCode {
.title("Custom Layout Manager")
.build();

let widget = simple_widget::SimpleWidget::new();
let widget = simple_widget::SimpleWidget::default();
for color in &COLORS {
let rgba = gdk::RGBA::from_str(color).unwrap();
let child = custom_layout_child::CustomLayoutChild::new(rgba);
Expand Down
6 changes: 1 addition & 5 deletions examples/custom_layout_manager/simple_widget/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,11 @@ glib::wrapper! {

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

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

pub fn add_child<W: IsA<gtk::Widget>>(&self, widget: &W) {
widget.set_parent(self);
}
Expand Down
6 changes: 0 additions & 6 deletions examples/custom_orientable/custom_orientable/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@ glib::wrapper! {

impl Default for CustomOrientable {
fn default() -> Self {
Self::new()
}
}

impl CustomOrientable {
pub fn new() -> Self {
glib::Object::new()
}
}
2 changes: 1 addition & 1 deletion examples/custom_orientable/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ fn main() -> glib::ExitCode {
application.connect_activate(|app| {
let window = gtk::ApplicationWindow::new(app);
let bx = gtk::Box::new(gtk::Orientation::Vertical, 6);
let orientable = CustomOrientable::new();
let orientable = CustomOrientable::default();
let button = gtk::Button::with_label("Switch orientation");

button.connect_clicked(glib::clone!(@weak orientable => move |_| {
Expand Down
6 changes: 0 additions & 6 deletions examples/custom_paintable/custom_paintable/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@ glib::wrapper! {

impl Default for CustomPaintable {
fn default() -> Self {
Self::new()
}
}

impl CustomPaintable {
pub fn new() -> Self {
glib::Object::new()
}
}
2 changes: 1 addition & 1 deletion examples/custom_paintable/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fn build_ui(application: &gtk::Application) {
window.set_title(Some("Custom Paintable"));
window.set_default_size(500, 500);

let paintable = CustomPaintable::new();
let paintable = CustomPaintable::default();

let picture = gtk::Picture::new();
picture.set_halign(gtk::Align::Center);
Expand Down
6 changes: 0 additions & 6 deletions examples/custom_widget/ex_button/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@ glib::wrapper! {

impl Default for ExButton {
fn default() -> Self {
Self::new()
}
}

impl ExButton {
pub fn new() -> Self {
glib::Object::new()
}
}
2 changes: 1 addition & 1 deletion examples/custom_widget/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fn main() -> glib::ExitCode {
.build();
application.connect_activate(|app| {
let window = gtk::ApplicationWindow::new(app);
let button = ExButton::new();
let button = ExButton::default();
button.set_margin_top(18);
button.set_margin_bottom(18);
button.set_margin_start(18);
Expand Down
3 changes: 1 addition & 2 deletions examples/expressions/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,7 @@ fn data() -> gio::ListStore {
let mut vector: Vec<Note> = Vec::new();

for i in 0..=100 {
let metadata = Metadata::new();
metadata.set_property("title", format!("Note ({i})"));
let metadata = Metadata::new(&format!("Note ({i})"));

let note = Note::new(&metadata);
vector.push(note);
Expand Down
6 changes: 3 additions & 3 deletions examples/expressions/metadata/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ glib::wrapper! {

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

impl Metadata {
pub fn new() -> Self {
glib::Object::new()
pub fn new(title: &str) -> Self {
glib::Object::builder().property("title", title).build()
}
}
2 changes: 1 addition & 1 deletion examples/fill_and_stroke/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fn main() -> glib::ExitCode {
let picture = gtk::Picture::builder()
.content_fit(gtk::ContentFit::ScaleDown)
.build();
let paintable = paintable::CustomPaintable::new();
let paintable = paintable::CustomPaintable::default();
picture.set_paintable(Some(&paintable));
win.set_child(Some(&picture));

Expand Down
6 changes: 0 additions & 6 deletions examples/fill_and_stroke/paintable/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@ glib::wrapper! {

impl Default for CustomPaintable {
fn default() -> Self {
Self::new()
}
}

impl CustomPaintable {
pub fn new() -> Self {
glib::Object::new()
}
}
12 changes: 4 additions & 8 deletions examples/gif_paintable/gif_paintable/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ glib::wrapper! {
pub struct GifPaintable(ObjectSubclass<imp::GifPaintable>) @implements gdk::Paintable;
}

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

impl GifPaintable {
/// Loads the bytes of a GIF into the paintable.
///
/// The loading consists of decoding the gif with a GIFDecoder, then storing
Expand Down Expand Up @@ -95,9 +97,3 @@ impl GifPaintable {
imp.current_idx.set(new_idx);
}
}

impl Default for GifPaintable {
fn default() -> Self {
Self::new()
}
}
2 changes: 1 addition & 1 deletion examples/gif_paintable/gif_paintable_window/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ impl GifPaintableWindow {
}

fn set_file(&self, file: gio::File) -> Result<(), Box<dyn std::error::Error>> {
let paintable = GifPaintable::new();
let paintable = GifPaintable::default();
let (bytes, _) = file.load_contents(gio::Cancellable::NONE)?;
paintable.load_from_bytes(&bytes)?;
self.imp().picture.set_paintable(Some(&paintable));
Expand Down
6 changes: 0 additions & 6 deletions examples/glium_gl_area/glium_gl_area/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@ glib::wrapper! {

impl Default for GliumGLArea {
fn default() -> Self {
Self::new()
}
}

impl GliumGLArea {
pub fn new() -> Self {
glib::Object::new()
}
}
Expand Down
2 changes: 1 addition & 1 deletion examples/glium_gl_area/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ fn build_ui(application: &gtk::Application) {
let window = gtk::ApplicationWindow::new(application);
window.set_title(Some("Glium in GLArea"));

let widget = GliumGLArea::new();
let widget = GliumGLArea::default();
window.set_child(Some(&widget));

window.present();
Expand Down
6 changes: 1 addition & 5 deletions examples/list_view_apps_launcher/application_row/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,11 @@ glib::wrapper! {

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

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

pub fn set_app_info(&self, app_info: &gio::AppInfo) {
let imp = self.imp();
imp.name.set_text(&app_info.name());
Expand Down
2 changes: 1 addition & 1 deletion examples/list_view_apps_launcher/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ fn build_ui(app: &gtk::Application) {
// generic for potentially future new APIs, it was switched to taking a GObject
// in 4.8
let item = item.downcast_ref::<gtk::ListItem>().unwrap();
let row = ApplicationRow::new();
let row = ApplicationRow::default();
item.set_child(Some(&row));
});

Expand Down
2 changes: 1 addition & 1 deletion examples/rotation_bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fn build_ui(application: &gtk::Application) {
.halign(gtk::Align::Center)
.valign(gtk::Align::Center)
.build();
let rotation_bin = RotationBin::new();
let rotation_bin = RotationBin::default();
let img = gtk::Image::builder()
.pixel_size(128)
.icon_name("audio-x-generic")
Expand Down
Loading

0 comments on commit 84bfe7c

Please sign in to comment.