objects: Use Default where makes sense

This commit is contained in:
Bilal Elmoussaoui 2022-06-11 11:37:27 +02:00
parent dc06be7ecb
commit a889337d66
16 changed files with 47 additions and 109 deletions

View file

@ -27,6 +27,7 @@ mod imp {
// The basic struct that holds our state and widgets
// (Ref)Cells are used for members which need to be mutable
#[derive(Default)]
pub struct Application {
pub window: RefCell<Option<WeakRef<Window>>>,
pub model: ProvidersModel,
@ -43,21 +44,6 @@ mod imp {
const NAME: &'static str = "Application";
type ParentType = adw::Application;
type Type = super::Application;
// Initialize with default values
fn new() -> Self {
let model = ProvidersModel::new();
Self {
window: RefCell::new(None),
model,
settings: OnceCell::default(),
can_be_locked: Cell::new(false),
lock_timeout_id: RefCell::default(),
locked: Cell::new(false),
search_provider: RefCell::default(),
}
}
}
// Overrides GObject vfuncs

View file

@ -63,13 +63,13 @@ mod imp {
fn new() -> Self {
Self {
id: Cell::new(0),
id: Cell::default(),
counter: Cell::new(otp::HOTP_DEFAULT_COUNTER),
name: RefCell::new("".to_string()),
otp: RefCell::new("".to_string()),
token_id: RefCell::new("".to_string()),
provider: RefCell::new(None),
token: OnceCell::new(),
name: RefCell::default(),
otp: RefCell::default(),
token_id: RefCell::default(),
provider: RefCell::default(),
token: OnceCell::default(),
}
}
}

View file

@ -40,11 +40,6 @@ glib::wrapper! {
}
impl AccountsModel {
#[allow(clippy::new_without_default)]
pub fn new() -> Self {
glib::Object::new(&[]).expect("Failed to create AccountsModel")
}
pub fn append(&self, account: &Account) {
let pos = {
let mut data = self.imp().0.borrow_mut();
@ -92,3 +87,9 @@ impl AccountsModel {
None
}
}
impl Default for AccountsModel {
fn default() -> Self {
glib::Object::new(&[]).expect("Failed to create AccountsModel")
}
}

View file

@ -93,22 +93,22 @@ mod imp {
type Type = super::Provider;
fn new() -> Self {
let model = AccountsModel::new();
let model = AccountsModel::default();
Self {
id: Cell::new(0),
id: Cell::default(),
default_counter: Cell::new(otp::HOTP_DEFAULT_COUNTER),
algorithm: RefCell::new(Algorithm::default().to_string()),
digits: Cell::new(otp::DEFAULT_DIGITS),
name: RefCell::new("".to_string()),
website: RefCell::new(None),
help_url: RefCell::new(None),
image_uri: RefCell::new(None),
name: RefCell::default(),
website: RefCell::default(),
help_url: RefCell::default(),
image_uri: RefCell::default(),
method: RefCell::new(OTPMethod::default().to_string()),
period: Cell::new(otp::TOTP_DEFAULT_PERIOD),
filter_model: gtk::FilterListModel::new(Some(&model), gtk::Filter::NONE),
accounts: model,
tick_callback: RefCell::default(),
remaining_time: Cell::new(0),
remaining_time: Cell::default(),
}
}
}

View file

@ -40,11 +40,6 @@ glib::wrapper! {
}
impl ProvidersModel {
#[allow(clippy::new_without_default)]
pub fn new() -> Self {
glib::Object::new(&[]).expect("Failed to create Model")
}
#[allow(clippy::too_many_arguments)]
pub fn find_or_create(
&self,
@ -230,3 +225,9 @@ impl ProvidersModel {
self.imp().1.set(true);
}
}
impl Default for ProvidersModel {
fn default() -> Self {
glib::Object::new(&[]).expect("Failed to create Model")
}
}

View file

@ -158,11 +158,6 @@ glib::wrapper! {
}
impl AccountDetailsPage {
#[allow(clippy::new_without_default)]
pub fn new() -> Self {
glib::Object::new(&[]).expect("Failed to create AccountDetailsPage")
}
fn init_widgets(&self) {
let imp = self.imp();
imp.qrcode_picture

View file

@ -135,7 +135,7 @@ mod imp {
Self {
paintable: CameraPaintable::new(sender),
receiver,
started: Cell::new(false),
started: Cell::default(),
previous: TemplateChild::default(),
spinner: TemplateChild::default(),
stack: TemplateChild::default(),
@ -184,11 +184,6 @@ glib::wrapper! {
}
impl Camera {
#[allow(clippy::new_without_default)]
pub fn new() -> Self {
glib::Object::new(&[]).expect("Failed to create a Camera")
}
pub fn start(&self) {
let imp = self.imp();
if !imp.started.get() {
@ -296,3 +291,9 @@ impl Camera {
}));
}
}
impl Default for Camera {
fn default() -> Self {
glib::Object::new(&[]).expect("Failed to create a Camera")
}
}

View file

@ -46,11 +46,6 @@ glib::wrapper! {
}
impl ErrorRevealer {
#[allow(clippy::new_without_default)]
pub fn new() -> Self {
glib::Object::new(&[]).unwrap()
}
pub fn popup(&self, text: &str) {
let imp = self.imp();
imp.label.set_text(text);

View file

@ -18,7 +18,7 @@ mod imp {
use super::*;
#[derive(Debug, CompositeTemplate)]
#[derive(Debug, Default, CompositeTemplate)]
#[template(resource = "/com/belmoussaoui/Authenticator/preferences_password_page.ui")]
pub struct PasswordPage {
pub actions: OnceCell<gio::SimpleActionGroup>,
@ -44,20 +44,6 @@ mod imp {
type Type = super::PasswordPage;
type ParentType = gtk::Box;
fn new() -> Self {
Self {
has_set_password: Cell::new(false), // state is synced later on from the window
current_password_entry: TemplateChild::default(),
password_entry: TemplateChild::default(),
confirm_password_entry: TemplateChild::default(),
current_password_row: TemplateChild::default(),
error_revealer: TemplateChild::default(),
status_page: TemplateChild::default(),
actions: OnceCell::new(),
default_password_signal: RefCell::default(),
}
}
fn class_init(klass: &mut Self::Class) {
klass.bind_template();
}

View file

@ -69,11 +69,11 @@ mod imp {
Self {
settings,
has_set_password: Cell::new(false), // Synced from the application
has_set_password: Cell::default(), // Synced from the application
camera_page: CameraPage::new(actions.clone()),
password_page: PasswordPage::new(actions.clone()),
actions,
model: OnceCell::new(),
model: OnceCell::default(),
backup_actions: gio::SimpleActionGroup::new(),
restore_actions: gio::SimpleActionGroup::new(),
auto_lock: TemplateChild::default(),

View file

@ -98,18 +98,7 @@ glib::wrapper! {
@extends gtk::Widget;
}
impl Default for ProgressIcon {
fn default() -> Self {
glib::Object::new(&[]).unwrap()
}
}
impl ProgressIcon {
/// Creates a new [`ProgressIcon`].
pub fn new() -> Self {
Self::default()
}
fn size(&self) -> i32 {
let width = self.width_request();
let height = self.width_request();

View file

@ -150,10 +150,8 @@ impl ProvidersDialog {
imp.providers_list
.bind_model(Some(&selection_model), move |obj| {
let provider = obj.clone().downcast::<Provider>().unwrap();
let row = ProviderActionRow::new();
let row = ProviderActionRow::default();
row.set_provider(provider);
row.upcast::<gtk::Widget>()
});
@ -329,11 +327,6 @@ mod row {
}
impl ProviderActionRow {
#[allow(clippy::new_without_default)]
pub fn new() -> Self {
glib::Object::new(&[]).expect("Failed to create ProviderActionRow")
}
fn setup_widgets(&self) {
let imp = self.imp();
let hbox = gtk::Box::builder()
@ -360,4 +353,10 @@ mod row {
self.property("provider")
}
}
impl Default for ProviderActionRow {
fn default() -> Self {
glib::Object::new(&[]).expect("Failed to create ProviderActionRow")
}
}
}

View file

@ -51,9 +51,9 @@ mod imp {
stack: TemplateChild::default(),
image: TemplateChild::default(),
spinner: TemplateChild::default(),
provider: RefCell::new(None),
signal_id: Default::default(),
join_handle: Default::default(),
provider: RefCell::default(),
signal_id: RefCell::default(),
join_handle: RefCell::default(),
}
}
@ -125,11 +125,6 @@ glib::wrapper! {
pub struct ProviderImage(ObjectSubclass<imp::ProviderImage>) @extends gtk::Widget, gtk::Box;
}
impl ProviderImage {
#[allow(clippy::new_without_default)]
pub fn new() -> Self {
glib::Object::new(&[]).expect("Failed to create ProviderImage")
}
pub fn set_provider(&self, provider: Option<&Provider>) {
let imp = self.imp();
if let Some(provider) = provider {

View file

@ -71,11 +71,6 @@ glib::wrapper! {
pub struct ProvidersList(ObjectSubclass<imp::ProvidersList>) @extends gtk::Widget, gtk::Box;
}
impl ProvidersList {
#[allow(clippy::new_without_default)]
pub fn new() -> Self {
glib::Object::new(&[]).expect("Failed to create ProvidersList")
}
pub fn set_view(&self, view: ProvidersListView) {
let imp = self.imp();
match view {

View file

@ -70,11 +70,6 @@ glib::wrapper! {
}
impl UrlRow {
#[allow(clippy::new_without_default)]
pub fn new() -> Self {
glib::Object::new(&[]).expect("Failed to create UrlRow")
}
fn setup_widgets(&self) {
let gesture = gtk::GestureClick::new();
gesture.connect_pressed(clone!(@weak self as row => move |_,_,_,_| {

View file

@ -79,7 +79,7 @@ mod imp {
Self {
settings,
providers: TemplateChild::default(),
model: OnceCell::new(),
model: OnceCell::default(),
account_details: TemplateChild::default(),
search_entry: TemplateChild::default(),
click_gesture: TemplateChild::default(),