diff --git a/cosmic-settings/src/pages/applications/default_apps.rs b/cosmic-settings/src/pages/applications/default_apps.rs index a8c5f42..1584218 100644 --- a/cosmic-settings/src/pages/applications/default_apps.rs +++ b/cosmic-settings/src/pages/applications/default_apps.rs @@ -121,10 +121,10 @@ impl page::Page for Page { let mut local_list = mime_apps::List::default(); - if let Some(path) = mime_apps::local_list_path() { - if let Ok(buffer) = std::fs::read_to_string(&path) { - local_list.load_from(&buffer); - } + if let Some(path) = mime_apps::local_list_path() + && let Ok(buffer) = std::fs::read_to_string(&path) + { + local_list.load_from(&buffer); } let assocs = mime_apps::associations::by_app(); @@ -245,10 +245,11 @@ impl Page { meta.selected = Some(id); let appid = &meta.app_ids[id]; - if category == Category::Terminal && self.shortcuts_config.is_some() { - if let Some(config) = self.shortcuts_config.as_ref() { - assign_default_terminal(config, appid); - } + if category == Category::Terminal + && self.shortcuts_config.is_some() + && let Some(config) = self.shortcuts_config.as_ref() + { + assign_default_terminal(config, appid); } for mime in mime_types { @@ -450,13 +451,13 @@ fn assign_default_terminal(config: &cosmic_config::Config, appid: &str) { if let Some(resolved_path) = resolved_path { let desktop_entry = DesktopEntry::from_path(resolved_path, Some(&get_languages_from_env())); - if let Ok(desktop_entry) = desktop_entry { - if let Some(exec) = desktop_entry.exec() { - actions.insert(System::Terminal, String::from(exec)); + if let Ok(desktop_entry) = desktop_entry + && let Some(exec) = desktop_entry.exec() + { + actions.insert(System::Terminal, String::from(exec)); - if let Err(why) = config.set("system_actions", actions) { - tracing::error!(?why, "Unable to set system_actions shortcuts config"); - } + if let Err(why) = config.set("system_actions", actions) { + tracing::error!(?why, "Unable to set system_actions shortcuts config"); } } } @@ -500,10 +501,10 @@ async fn load_defaults(assocs: &BTreeMap, Arc>, for_mimes: &[&str] let mut icons = Vec::new(); for (id, (appid, app)) in unsorted.iter().enumerate() { - if let Some(current_app) = current_app { - if app.name.as_ref() == current_app.name.as_ref() { - selected = Some(id); - } + if let Some(current_app) = current_app + && app.name.as_ref() == current_app.name.as_ref() + { + selected = Some(id); } app_ids.push(appid.as_ref().into()); @@ -562,21 +563,21 @@ async fn load_terminal_apps(assocs: &BTreeMap, Arc>) -> AppMeta { // Scan desktop entries for terminal applications for path in DesktopEntryIter::new(default_paths()) { - if let Ok(bytes) = std::fs::read_to_string(&path) { - if let Ok(entry) = DesktopEntry::from_str(&path, &bytes, None::<&[&str]>) { - // Check if it's a terminal application - if entry - .categories() - .map(|cats| cats.iter().any(|c| *c == "TerminalEmulator")) - .unwrap_or(false) - { - let id = entry.id(); - if let Some(app) = assocs.get(id) { - if current_appid.as_ref().map(|c| *c == id).unwrap_or(false) { - current_app = Some(app.clone()); - } - terminals.push((Arc::from(id), app.clone())); + if let Ok(bytes) = std::fs::read_to_string(&path) + && let Ok(entry) = DesktopEntry::from_str(&path, &bytes, None::<&[&str]>) + { + // Check if it's a terminal application + if entry + .categories() + .map(|cats| cats.contains(&"TerminalEmulator")) + .unwrap_or(false) + { + let id = entry.id(); + if let Some(app) = assocs.get(id) { + if current_appid.as_ref().map(|c| *c == id).unwrap_or(false) { + current_app = Some(app.clone()); } + terminals.push((Arc::from(id), app.clone())); } } } @@ -591,10 +592,10 @@ async fn load_terminal_apps(assocs: &BTreeMap, Arc>) -> AppMeta { let mut icons = Vec::new(); for (id, (appid, app)) in terminals.iter().enumerate() { - if let Some(ref current_app) = current_app { - if app.name.as_ref() == current_app.name.as_ref() { - selected = Some(id); - } + if let Some(ref current_app) = current_app + && app.name.as_ref() == current_app.name.as_ref() + { + selected = Some(id); } app_ids.push(appid.to_string()); diff --git a/cosmic-settings/src/pages/applications/legacy_applications.rs b/cosmic-settings/src/pages/applications/legacy_applications.rs index f12c66b..93ca01c 100644 --- a/cosmic-settings/src/pages/applications/legacy_applications.rs +++ b/cosmic-settings/src/pages/applications/legacy_applications.rs @@ -138,10 +138,10 @@ impl page::Page for Page { let (randr_task, randr_handle) = Task::stream(async_fn_stream::fn_stream(|emitter| async move { while let Ok(message) = rx.recv().await { - if let cosmic_randr::Message::ManagerDone = message { - if !refresh_pending.swap(true, Ordering::SeqCst) { - _ = emitter.emit(on_enter().await).await; - } + if let cosmic_randr::Message::ManagerDone = message + && !refresh_pending.swap(true, Ordering::SeqCst) + { + _ = emitter.emit(on_enter().await).await; } } })) diff --git a/cosmic-settings/src/pages/applications/startup_apps.rs b/cosmic-settings/src/pages/applications/startup_apps.rs index 0308a1f..d5626d7 100644 --- a/cosmic-settings/src/pages/applications/startup_apps.rs +++ b/cosmic-settings/src/pages/applications/startup_apps.rs @@ -157,36 +157,35 @@ impl page::Page for Page { } fn dialog(&self) -> Option> { - if let Some(app_to_remove) = &self.app_to_remove { - if let Some(cached_startup_apps) = &self.cached_startup_apps { - if let Some(target_directory_type) = &self.target_directory_type { - return Some( - widget::dialog() - .title(fl!( - "startup-apps", - "remove-dialog-title", - name = app_to_remove.name(&cached_startup_apps.locales) - )) - .icon(icon::from_name("dialog-warning").size(64)) - .body(fl!("startup-apps", "remove-dialog-description")) - .primary_action( - button::suggested(fl!("remove")).on_press( - Message::RemoveStartupApplication( - target_directory_type.clone(), - app_to_remove.clone(), - true, - ) - .into(), - ), + if let Some(app_to_remove) = &self.app_to_remove + && let Some(cached_startup_apps) = &self.cached_startup_apps + && let Some(target_directory_type) = &self.target_directory_type + { + return Some( + widget::dialog() + .title(fl!( + "startup-apps", + "remove-dialog-title", + name = app_to_remove.name(&cached_startup_apps.locales) + )) + .icon(icon::from_name("dialog-warning").size(64)) + .body(fl!("startup-apps", "remove-dialog-description")) + .primary_action( + button::suggested(fl!("remove")).on_press( + Message::RemoveStartupApplication( + target_directory_type.clone(), + app_to_remove.clone(), + true, ) - .secondary_action( - button::standard(fl!("cancel")) - .on_press(Message::CancelRemoveStartupApplication.into()), - ) - .apply(Element::from), - ); - } - } + .into(), + ), + ) + .secondary_action( + button::standard(fl!("cancel")) + .on_press(Message::CancelRemoveStartupApplication.into()), + ) + .apply(Element::from), + ); } None } @@ -228,32 +227,31 @@ impl Page { _ = std::fs::create_dir_all(directory_to_target.as_path()); - if let Ok(exists) = std::fs::exists(directory_to_target.join(file_name.clone())) { - if !exists { - // when adding an application, we want to symlink to be more user-friendly - // this ensures that, as an application gets updated / removed, so does the - // symlink - match std::os::unix::fs::symlink( - app.clone().path, - directory_to_target.join(file_name), - ) { - Ok(_) => { - if let Some(ref mut cached_startup_apps) = self.cached_startup_apps - { - let target_apps = cached_startup_apps.apps.get(&directory_type); - if let Some(target_apps) = target_apps { - let mut new_apps = target_apps.clone(); - new_apps.push(app.clone()); + if let Ok(exists) = std::fs::exists(directory_to_target.join(file_name.clone())) + && !exists + { + // when adding an application, we want to symlink to be more user-friendly + // this ensures that, as an application gets updated / removed, so does the + // symlink + match std::os::unix::fs::symlink( + app.clone().path, + directory_to_target.join(file_name), + ) { + Ok(_) => { + if let Some(ref mut cached_startup_apps) = self.cached_startup_apps { + let target_apps = cached_startup_apps.apps.get(&directory_type); + if let Some(target_apps) = target_apps { + let mut new_apps = target_apps.clone(); + new_apps.push(app.clone()); - cached_startup_apps - .apps - .insert(directory_type.clone(), new_apps); - } + cached_startup_apps + .apps + .insert(directory_type.clone(), new_apps); } } - Err(e) => { - error!(?e, "Failed to symlink"); - } + } + Err(e) => { + error!(?e, "Failed to symlink"); } } } @@ -273,33 +271,30 @@ impl Page { let directory_to_target = directories.first().expect("Always at least one directory"); if let Ok(exists) = std::fs::exists(directory_to_target.join(file_name.clone())) + && exists { - if exists { - // remove_file works for both regular files & symlinks - match std::fs::remove_file(directory_to_target.join(file_name)) { - Ok(_) => { - if let Some(ref mut cached_startup_apps) = - self.cached_startup_apps - { - let target_apps = - cached_startup_apps.apps.get(&directory_type); - if let Some(target_apps) = target_apps { - let mut new_apps = Vec::new(); - for old_app in target_apps { - if old_app != &app { - new_apps.push(old_app.clone()); - } + // remove_file works for both regular files & symlinks + match std::fs::remove_file(directory_to_target.join(file_name)) { + Ok(_) => { + if let Some(ref mut cached_startup_apps) = self.cached_startup_apps + { + let target_apps = cached_startup_apps.apps.get(&directory_type); + if let Some(target_apps) = target_apps { + let mut new_apps = Vec::new(); + for old_app in target_apps { + if old_app != &app { + new_apps.push(old_app.clone()); } - - cached_startup_apps - .apps - .insert(directory_type.clone(), new_apps); } + + cached_startup_apps + .apps + .insert(directory_type.clone(), new_apps); } } - Err(e) => { - error!(?e, "Failed to remove file"); - } + } + Err(e) => { + error!(?e, "Failed to remove file"); } } } @@ -330,39 +325,30 @@ impl Page { if let Some(startup_apps) = &self.cached_startup_apps { for app in &startup_apps.all_apps { - if let Some(name) = app.name(&startup_apps.locales) { - if let Some(exec) = app.exec() { - if search_input.is_empty() - || exec.to_lowercase().contains(search_input) - || name.to_lowercase().contains(search_input) - { - let mut row = widget::row::with_capacity(3) - .spacing(space_xs) - .align_y(Alignment::Center); + if let Some(name) = app.name(&startup_apps.locales) + && let Some(exec) = app.exec() + && (search_input.is_empty() + || exec.to_lowercase().contains(search_input) + || name.to_lowercase().contains(search_input)) + { + let mut row = widget::row::with_capacity(3) + .spacing(space_xs) + .align_y(Alignment::Center); - row = row.push( - icon::from_name(app.icon().unwrap_or("application-default")) - .size(32), - ); + row = row.push( + icon::from_name(app.icon().unwrap_or("application-default")).size(32), + ); - if let Some(name) = app.name(&startup_apps.locales) { - row = row.push(text(name).width(Length::Fill)); - } else { - row = row.push(text(&app.appid).width(Length::Fill)); - } - row = row.push( - widget::button::text(fl!("add")).on_press( - Message::AddStartupApplication( - directory_type.clone(), - app.clone(), - ) - .into(), - ), - ); - - list = list.add(row) - } + if let Some(name) = app.name(&startup_apps.locales) { + row = row.push(text(name).width(Length::Fill)); + } else { + row = row.push(text(&app.appid).width(Length::Fill)); } + row = row.push(widget::button::text(fl!("add")).on_press( + Message::AddStartupApplication(directory_type.clone(), app.clone()).into(), + )); + + list = list.add(row) } } } @@ -467,16 +453,16 @@ fn get_all_apps(locales: Vec) -> Vec { } // skip if we can't run this in COSMIC - if let Some(only_show_in) = entry.only_show_in() { - if !only_show_in.contains(&"COSMIC") { - continue; - } + if let Some(only_show_in) = entry.only_show_in() + && !only_show_in.contains(&"COSMIC") + { + continue; } - if let Some(not_show_in) = entry.not_show_in() { - if not_show_in.contains(&"COSMIC") { - continue; - } + if let Some(not_show_in) = entry.not_show_in() + && not_show_in.contains(&"COSMIC") + { + continue; } result.push(entry.clone()); diff --git a/cosmic-settings/src/pages/bluetooth/mod.rs b/cosmic-settings/src/pages/bluetooth/mod.rs index 6d85d5e..68a498f 100644 --- a/cosmic-settings/src/pages/bluetooth/mod.rs +++ b/cosmic-settings/src/pages/bluetooth/mod.rs @@ -98,11 +98,12 @@ impl Model { self.update_status(); - if let Some((path, existing)) = self.get_selected_adapter_mut() { - if existing.enabled == Active::Enabled && existing.scanning == Active::Disabled { - existing.scanning = Active::Enabling; - return Some(start_discovery(connection, path)); - } + if let Some((path, existing)) = self.get_selected_adapter_mut() + && existing.enabled == Active::Enabled + && existing.scanning == Active::Disabled + { + existing.scanning = Active::Enabling; + return Some(start_discovery(connection, path)); } None diff --git a/cosmic-settings/src/pages/desktop/appearance/drawer.rs b/cosmic-settings/src/pages/desktop/appearance/drawer.rs index ada25b7..a086800 100644 --- a/cosmic-settings/src/pages/desktop/appearance/drawer.rs +++ b/cosmic-settings/src/pages/desktop/appearance/drawer.rs @@ -136,10 +136,10 @@ impl Content { Some(c) => self.font_config.search(input.to_string(), c), }, FontMessage::Select(font) => { - if let Some(context_view) = context_view { - if let Some(task) = self.font_config.select(font.to_string(), context_view) { - return task; - } + if let Some(context_view) = context_view + && let Some(task) = self.font_config.select(font.to_string(), context_view) + { + return task; } Task::none() } diff --git a/cosmic-settings/src/pages/desktop/appearance/icon_themes.rs b/cosmic-settings/src/pages/desktop/appearance/icon_themes.rs index fee1e47..153a57b 100644 --- a/cosmic-settings/src/pages/desktop/appearance/icon_themes.rs +++ b/cosmic-settings/src/pages/desktop/appearance/icon_themes.rs @@ -185,25 +185,25 @@ pub async fn fetch() -> Message { if is_hidden.trim() == "true" { continue 'icon_dir; } - } else if name.is_none() { - if let Some(value) = buffer.strip_prefix("Name=") { - name = Some(value.trim().to_owned()); - } + } else if name.is_none() + && let Some(value) = buffer.strip_prefix("Name=") + { + name = Some(value.trim().to_owned()); } - if valid_dirs.is_empty() { - if let Some(value) = buffer.strip_prefix("Inherits=") { - valid_dirs.extend(value.trim().split(',').map(|fallback| { - if let Some(path) = theme_paths.get(fallback) { - path.iter() - .last() - .and_then(|os| os.to_str().map(ToOwned::to_owned)) - .unwrap_or_else(|| fallback.to_owned()) - } else { - fallback.to_owned() - } - })); - } + if valid_dirs.is_empty() + && let Some(value) = buffer.strip_prefix("Inherits=") + { + valid_dirs.extend(value.trim().split(',').map(|fallback| { + if let Some(path) = theme_paths.get(fallback) { + path.iter() + .next_back() + .and_then(|os| os.to_str().map(ToOwned::to_owned)) + .unwrap_or_else(|| fallback.to_owned()) + } else { + fallback.to_owned() + } + })); } buffer.clear(); @@ -213,7 +213,7 @@ pub async fn fetch() -> Message { // Name of the directory theme was found in (e.g. Pop for Pop) valid_dirs.push( path.iter() - .last() + .next_back() .and_then(|os| os.to_str().map(ToOwned::to_owned)) .unwrap_or_else(|| name.clone()), ); @@ -288,7 +288,7 @@ fn preview_handles(theme: String, inherits: Vec) -> [icon::Handle; ICON_ fn icon_handle(icon_name: &str, alternate: &str, valid_dirs: &[String]) -> icon::Handle { ICON_TRY_SIZES .iter() - .zip(std::iter::repeat(icon_name).take(ICON_TRY_SIZES.len())) + .zip(std::iter::repeat_n(icon_name, ICON_TRY_SIZES.len())) // Try fallback icon name after the default .chain( ICON_TRY_SIZES @@ -316,8 +316,10 @@ fn icon_handle(icon_name: &str, alternate: &str, valid_dirs: &[String]) -> icon: theme_dir = parent; } - if let Some(dir_name) = - theme_dir.iter().last().and_then(std::ffi::OsStr::to_str) + if let Some(dir_name) = theme_dir + .iter() + .next_back() + .and_then(std::ffi::OsStr::to_str) { valid_dirs .iter() diff --git a/cosmic-settings/src/pages/desktop/appearance/mod.rs b/cosmic-settings/src/pages/desktop/appearance/mod.rs index 60a0b94..da9fa11 100644 --- a/cosmic-settings/src/pages/desktop/appearance/mod.rs +++ b/cosmic-settings/src/pages/desktop/appearance/mod.rs @@ -247,12 +247,12 @@ impl Page { } Message::DrawerColor(u) => { - if let Some(context_view) = self.context_view.as_ref() { - if self.drawer.update_color(&mut tasks, u, context_view) { - theme_staged = self - .theme_manager - .set_color(self.drawer.current_color(context_view), context_view); - } + if let Some(context_view) = self.context_view.as_ref() + && self.drawer.update_color(&mut tasks, u, context_view) + { + theme_staged = self + .theme_manager + .set_color(self.drawer.current_color(context_view), context_view); } } @@ -462,10 +462,10 @@ impl Page { Message::ImportSuccess(builder) => { tracing::trace!("Import successful"); let new_is_dark = builder.palette.is_dark(); - if new_is_dark != self.theme_manager.mode().is_dark { - if let Err(err) = self.theme_manager.dark_mode(new_is_dark) { - tracing::error!(?err, "Error setting dark mode"); - } + if new_is_dark != self.theme_manager.mode().is_dark + && let Err(err) = self.theme_manager.dark_mode(new_is_dark) + { + tracing::error!(?err, "Error setting dark mode"); } self.theme_manager @@ -552,33 +552,33 @@ impl Page { (panel_config.name == "Dock").then_some(panel_config) }); - if let Some(panel_config_helper) = panel_config_helper.as_ref() { - if let Some(panel_config) = panel_config.as_mut() { - let radii = if panel_config.anchor_gap || !panel_config.expand_to_edges { - let cornder_radii: CornerRadii = roundness.into(); - cornder_radii.radius_xl[0] as u32 - } else { - 0 - }; + if let Some(panel_config_helper) = panel_config_helper.as_ref() + && let Some(panel_config) = panel_config.as_mut() + { + let radii = if panel_config.anchor_gap || !panel_config.expand_to_edges { + let cornder_radii: CornerRadii = roundness.into(); + cornder_radii.radius_xl[0] as u32 + } else { + 0 + }; - if let Err(why) = panel_config.set_border_radius(panel_config_helper, radii) { - tracing::error!(?why, "Error updating panel corner radii"); - } + if let Err(why) = panel_config.set_border_radius(panel_config_helper, radii) { + tracing::error!(?why, "Error updating panel corner radii"); } }; - if let Some(dock_config_helper) = dock_config_helper.as_ref() { - if let Some(dock_config) = dock_config.as_mut() { - let radii = if dock_config.anchor_gap || !dock_config.expand_to_edges { - let cornder_radii: CornerRadii = roundness.into(); - cornder_radii.radius_xl[0] as u32 - } else { - 0 - }; + if let Some(dock_config_helper) = dock_config_helper.as_ref() + && let Some(dock_config) = dock_config.as_mut() + { + let radii = if dock_config.anchor_gap || !dock_config.expand_to_edges { + let cornder_radii: CornerRadii = roundness.into(); + cornder_radii.radius_xl[0] as u32 + } else { + 0 + }; - if let Err(why) = dock_config.set_border_radius(dock_config_helper, radii) { - tracing::error!(?why, "Error updating dock corner radii"); - } + if let Err(why) = dock_config.set_border_radius(dock_config_helper, radii) { + tracing::error!(?why, "Error updating dock corner radii"); } }; } @@ -596,29 +596,29 @@ impl Page { (panel_config.name == "Dock").then_some(panel_config) }); - if let Some(panel_config_helper) = panel_config_helper.as_ref() { - if let Some(panel_config) = panel_config.as_mut() { - let spacing = match density { - Density::Compact => 0, - _ => 4, - }; - let update = panel_config.set_spacing(panel_config_helper, spacing); - if let Err(err) = update { - tracing::error!(?err, "Error updating panel spacing"); - } + if let Some(panel_config_helper) = panel_config_helper.as_ref() + && let Some(panel_config) = panel_config.as_mut() + { + let spacing = match density { + Density::Compact => 0, + _ => 4, + }; + let update = panel_config.set_spacing(panel_config_helper, spacing); + if let Err(err) = update { + tracing::error!(?err, "Error updating panel spacing"); } }; - if let Some(dock_config_helper) = dock_config_helper.as_ref() { - if let Some(dock_config) = dock_config.as_mut() { - let spacing = match density { - Density::Compact => 0, - _ => 4, - }; - let update = dock_config.set_spacing(dock_config_helper, spacing); - if let Err(err) = update { - tracing::error!(?err, "Error updating dock spacing"); - } + if let Some(dock_config_helper) = dock_config_helper.as_ref() + && let Some(dock_config) = dock_config.as_mut() + { + let spacing = match density { + Density::Compact => 0, + _ => 4, + }; + let update = dock_config.set_spacing(dock_config_helper, spacing); + if let Err(err) = update { + tracing::error!(?err, "Error updating dock spacing"); } }; } diff --git a/cosmic-settings/src/pages/desktop/panel/applets_inner.rs b/cosmic-settings/src/pages/desktop/panel/applets_inner.rs index 06cd20f..a973690 100644 --- a/cosmic-settings/src/pages/desktop/panel/applets_inner.rs +++ b/cosmic-settings/src/pages/desktop/panel/applets_inner.rs @@ -244,21 +244,20 @@ impl Page { .filter(|a| a.matches(&self.search)) { if let Some(config) = self.current_config.as_ref() { - if let Some(center) = config.plugins_center.as_ref() { - if center.iter().any(|a| a.as_str() == info.id.as_ref()) { - continue; - } + if let Some(center) = config.plugins_center.as_ref() + && center.iter().any(|a| a.as_str() == info.id.as_ref()) + { + continue; } - if let Some(wings) = config.plugins_wings.as_ref() { - if wings + if let Some(wings) = config.plugins_wings.as_ref() + && wings .0 .iter() .chain(wings.1.iter()) .any(|a| a.as_str() == info.id.as_ref()) - { - continue; - } + { + continue; } } has_some = true; diff --git a/cosmic-settings/src/pages/desktop/panel/inner.rs b/cosmic-settings/src/pages/desktop/panel/inner.rs index 85614d0..6e5c346 100644 --- a/cosmic-settings/src/pages/desktop/panel/inner.rs +++ b/cosmic-settings/src/pages/desktop/panel/inner.rs @@ -468,10 +468,10 @@ impl PageInner { } } Message::FullReset => { - if let Some(container) = self.system_container.as_ref() { - if let Err(err) = container.write_entries() { - tracing::error!(?err, "Error fully resetting the panel config."); - } + if let Some(container) = self.system_container.as_ref() + && let Err(err) = container.write_entries() + { + tracing::error!(?err, "Error fully resetting the panel config."); } } _ => {} @@ -571,10 +571,10 @@ impl PageInner { return Task::none(); } Message::OutputRemoved(output) => { - if let Some((name, _)) = self.outputs_map.remove(&output.id()) { - if let Some(pos) = self.outputs.iter().position(|o| o == &name) { - self.outputs.remove(pos); - } + if let Some((name, _)) = self.outputs_map.remove(&output.id()) + && let Some(pos) = self.outputs.iter().position(|o| o == &name) + { + self.outputs.remove(pos); } } Message::PanelConfig(c) => { diff --git a/cosmic-settings/src/pages/desktop/wallpaper/config.rs b/cosmic-settings/src/pages/desktop/wallpaper/config.rs index d655aa6..29fa2e6 100644 --- a/cosmic-settings/src/pages/desktop/wallpaper/config.rs +++ b/cosmic-settings/src/pages/desktop/wallpaper/config.rs @@ -109,15 +109,15 @@ impl Config { #[must_use] pub fn default_folder() -> PathBuf { - if let Some(data_dirs) = env::var_os("XDG_DATA_DIRS") { - if let Some(data_dirs) = data_dirs.to_str() { - let data_dirs = data_dirs.split(":"); + if let Some(data_dirs) = env::var_os("XDG_DATA_DIRS") + && let Some(data_dirs) = data_dirs.to_str() + { + let data_dirs = data_dirs.split(":"); - for data_dir in data_dirs { - let potential_path = PathBuf::from(data_dir).join(BACKGROUNDS_DIR); - if let Ok(true) = &potential_path.try_exists() { - return potential_path; - } + for data_dir in data_dirs { + let potential_path = PathBuf::from(data_dir).join(BACKGROUNDS_DIR); + if let Ok(true) = &potential_path.try_exists() { + return potential_path; } } } diff --git a/cosmic-settings/src/pages/desktop/wallpaper/mod.rs b/cosmic-settings/src/pages/desktop/wallpaper/mod.rs index e3e92e0..8233583 100644 --- a/cosmic-settings/src/pages/desktop/wallpaper/mod.rs +++ b/cosmic-settings/src/pages/desktop/wallpaper/mod.rs @@ -587,10 +587,10 @@ impl Page { } Category::RecentFolder(id) => { - if let Some(path) = self.config.recent_folders().get(id).cloned() { - if let Err(why) = self.config.set_current_folder(Some(path.clone())) { - tracing::error!(?path, ?why, "failed to set current folder"); - } + if let Some(path) = self.config.recent_folders().get(id).cloned() + && let Err(why) = self.config.set_current_folder(Some(path.clone())) + { + tracing::error!(?path, ?why, "failed to set current folder"); } } @@ -815,10 +815,10 @@ impl Page { } Message::ImageRemove(image) => { - if let Some(path) = self.selection.remove_custom_image(image) { - if let Err(why) = self.config.remove_custom_image(&path) { - tracing::error!(?why, "could not remove custom image from config"); - } + if let Some(path) = self.selection.remove_custom_image(image) + && let Err(why) = self.config.remove_custom_image(&path) + { + tracing::error!(?why, "could not remove custom image from config"); } } @@ -858,18 +858,14 @@ impl Page { self.selection.active = Choice::Slideshow; self.cache_display_image(); } else { - if let Some(output) = self.config_output() { - if let Some(Source::Path(path)) = self.config.current_image(output) { - if let Some(entity) = self.wallpaper_id_from_path(&path) { - if let Some(entry) = - self.config_wallpaper_entry(output.to_owned(), path) - { - self.select_wallpaper(&entry, entity, false); - self.config_apply(); - return Task::none(); - } - } - } + if let Some(output) = self.config_output() + && let Some(Source::Path(path)) = self.config.current_image(output) + && let Some(entity) = self.wallpaper_id_from_path(&path) + && let Some(entry) = self.config_wallpaper_entry(output.to_owned(), path) + { + self.select_wallpaper(&entry, entity, false); + self.config_apply(); + return Task::none(); } self.select_first_wallpaper(); diff --git a/cosmic-settings/src/pages/desktop/window_management.rs b/cosmic-settings/src/pages/desktop/window_management.rs index 8daf386..d7ac04a 100644 --- a/cosmic-settings/src/pages/desktop/window_management.rs +++ b/cosmic-settings/src/pages/desktop/window_management.rs @@ -337,11 +337,9 @@ pub fn focus_navigation() -> Section { fn super_key_active_config() -> Option { let super_binding = Binding::new(shortcuts::Modifiers::new().logo(), None); - let config = shortcuts::context().ok()?; - let shortcuts = shortcuts::shortcuts(&config); - let new_id = shortcuts + shortcuts::shortcuts(&config) .iter() .find(|(binding, _action)| binding == &&super_binding) .and_then(|(_, action)| match action { @@ -349,9 +347,7 @@ fn super_key_active_config() -> Option { Action::System(shortcuts::action::System::WorkspaceOverview) => Some(1), Action::System(shortcuts::action::System::AppLibrary) => Some(2), _ => None, - }); - - new_id + }) } fn super_key_set(action: Option) { diff --git a/cosmic-settings/src/pages/display/arrangement.rs b/cosmic-settings/src/pages/display/arrangement.rs index d4f0165..71a29ed 100644 --- a/cosmic-settings/src/pages/display/arrangement.rs +++ b/cosmic-settings/src/pages/display/arrangement.rs @@ -226,20 +226,20 @@ impl Widget for Arrangement<'_ | core::Event::Touch(touch::Event::FingerLifted { .. }) => { let state = tree.state.downcast_mut::(); if let Some((output_key, region)) = state.dragging.take() { - if let Some(position) = cursor.position() { - if position.distance(state.drag_from) < 4.0 { - if let Some(ref on_select) = self.on_select { - for id in self.tab_model.iter() { - if let Some(&key) = self.tab_model.data::(id) { - if key == output_key { - shell.publish(on_select(id)); - } - } + if let Some(position) = cursor.position() + && position.distance(state.drag_from) < 4.0 + { + if let Some(ref on_select) = self.on_select { + for id in self.tab_model.iter() { + if let Some(&key) = self.tab_model.data::(id) + && key == output_key + { + shell.publish(on_select(id)); } } - - return event::Status::Captured; } + + return event::Status::Captured; } if let Some(ref on_placement) = self.on_placement { @@ -309,10 +309,10 @@ impl Widget for Arrangement<'_ display_regions(self.tab_model, self.list, &bounds, state.max_dimensions).enumerate() { // If the output is being dragged, show its dragged position instead. - if let Some((dragged_key, dragged_region)) = state.dragging { - if dragged_key == output_key { - region = dragged_region; - } + if let Some((dragged_key, dragged_region)) = state.dragging + && dragged_key == output_key + { + region = dragged_region; } let (background, border_color) = if Some(&output_key) == active_key { diff --git a/cosmic-settings/src/pages/display/mod.rs b/cosmic-settings/src/pages/display/mod.rs index f0cc336..4a0db76 100644 --- a/cosmic-settings/src/pages/display/mod.rs +++ b/cosmic-settings/src/pages/display/mod.rs @@ -33,7 +33,7 @@ static DPI_SCALE_LABELS: LazyLock> = #[derive(Clone, Copy, Debug)] pub struct ColorDepth(usize); -/// Identifies the content to display in the context drawer +// /// Identifies the content to display in the context drawer // pub enum ContextDrawer { // NightLight, // } @@ -287,10 +287,10 @@ impl page::Page for Page { let (randr_task, randr_handle) = Task::stream(async_fn_stream::fn_stream(|emitter| async move { while let Ok(message) = rx.recv().await { - if let cosmic_randr::Message::ManagerDone = message { - if !refreshing_page.swap(true, Ordering::SeqCst) { - _ = emitter.emit(on_enter().await).await; - } + if let cosmic_randr::Message::ManagerDone = message + && !refreshing_page.swap(true, Ordering::SeqCst) + { + _ = emitter.emit(on_enter().await).await; } } })) @@ -931,14 +931,13 @@ impl Page { return Task::none(); }; - if let Some(ref resolution) = self.config.resolution { - if let Some(rates) = self.cache.modes.get(resolution) { - if let Some(&rate) = rates.get(option) { - self.cache.refresh_rate_selected = Some(option); - self.config.refresh_rate = Some(rate); - return self.exec_randr(output, Randr::RefreshRate(rate)); - } - } + if let Some(ref resolution) = self.config.resolution + && let Some(rates) = self.cache.modes.get(resolution) + && let Some(&rate) = rates.get(option) + { + self.cache.refresh_rate_selected = Some(option); + self.config.refresh_rate = Some(rate); + return self.exec_randr(output, Randr::RefreshRate(rate)); } Task::none() @@ -1329,7 +1328,7 @@ pub fn display_configuration() -> Section { .button_alignment(Alignment::Center) .on_activate(Message::Display); - let mut display_enable = (page + let mut display_enable = if page // Don't allow disabling display if it's the only active .list .outputs @@ -1337,22 +1336,23 @@ pub fn display_configuration() -> Section { .filter(|display| display.enabled) .count() > 1 - || !active_output.enabled) - .then(|| { - list_column() - .add(widget::settings::item( - &descriptions[enable_label], - toggler(active_output.enabled).on_toggle(Message::DisplayToggle), - )) - .add(widget::settings::item( - &descriptions[mirroring_label], - widget::dropdown::multi::dropdown( - &page.mirror_menu, - Message::Mirroring, - ), - )) - }) - .unwrap_or_else(list_column); + || !active_output.enabled + { + list_column() + .add(widget::settings::item( + &descriptions[enable_label], + toggler(active_output.enabled).on_toggle(Message::DisplayToggle), + )) + .add(widget::settings::item( + &descriptions[mirroring_label], + widget::dropdown::multi::dropdown( + &page.mirror_menu, + Message::Mirroring, + ), + )) + } else { + list_column() + }; if let Some(items) = display_options { for item in items { diff --git a/cosmic-settings/src/pages/input/keyboard/mod.rs b/cosmic-settings/src/pages/input/keyboard/mod.rs index 8f22cd5..ac9e630 100644 --- a/cosmic-settings/src/pages/input/keyboard/mod.rs +++ b/cosmic-settings/src/pages/input/keyboard/mod.rs @@ -455,22 +455,20 @@ impl Page { SourceContext::MoveDown(id) => { if let Some(pos) = self.active_layouts.iter().position(|&active| active == id) + && pos + 1 < self.active_layouts.len() { - if pos + 1 < self.active_layouts.len() { - self.active_layouts.swap(pos, pos + 1); - self.update_xkb_config(); - } + self.active_layouts.swap(pos, pos + 1); + self.update_xkb_config(); } } SourceContext::MoveUp(id) => { if let Some(pos) = self.active_layouts.iter().position(|&active| active == id) + && pos > 0 { - if pos > 0 { - self.active_layouts.swap(pos, pos - 1); - self.update_xkb_config(); - } + self.active_layouts.swap(pos, pos - 1); + self.update_xkb_config(); } } diff --git a/cosmic-settings/src/pages/input/keyboard/shortcuts/common.rs b/cosmic-settings/src/pages/input/keyboard/shortcuts/common.rs index fc5a054..c98eaf6 100644 --- a/cosmic-settings/src/pages/input/keyboard/shortcuts/common.rs +++ b/cosmic-settings/src/pages/input/keyboard/shortcuts/common.rs @@ -197,37 +197,35 @@ impl Model { } pub(super) fn dialog(&self) -> Option> { - if let Some(&(id, _, _, ref action)) = self.replace_dialog.as_ref() { - if let Some(short_id) = self.shortcut_context { - if let Some(model) = self.shortcut_models.get(short_id) { - if let Some(shortcut) = model.bindings.get(id) { - let primary_action = button::suggested(fl!("replace")) - .on_press(ShortcutMessage::ApplyReplace); + if let Some(&(id, _, _, ref action)) = self.replace_dialog.as_ref() + && let Some(short_id) = self.shortcut_context + && let Some(model) = self.shortcut_models.get(short_id) + && let Some(shortcut) = model.bindings.get(id) + { + let primary_action = + button::suggested(fl!("replace")).on_press(ShortcutMessage::ApplyReplace); - let secondary_action = button::standard(fl!("cancel")) - .on_press(ShortcutMessage::CancelReplace); + let secondary_action = + button::standard(fl!("cancel")).on_press(ShortcutMessage::CancelReplace); - let dialog = widget::dialog() - .title(fl!("replace-shortcut-dialog")) - .icon(icon::from_name("dialog-warning").size(64)) - .body(fl!( - "replace-shortcut-dialog", - "desc", - shortcut = shortcut.input.clone(), - name = shortcut - .binding - .description - .as_ref() - .unwrap_or(action) - .to_owned() - )) - .primary_action(primary_action) - .secondary_action(secondary_action); + let dialog = widget::dialog() + .title(fl!("replace-shortcut-dialog")) + .icon(icon::from_name("dialog-warning").size(64)) + .body(fl!( + "replace-shortcut-dialog", + "desc", + shortcut = shortcut.input.clone(), + name = shortcut + .binding + .description + .as_ref() + .unwrap_or(action) + .to_owned() + )) + .primary_action(primary_action) + .secondary_action(secondary_action); - return Some(dialog.into()); - } - } - } + return Some(dialog.into()); } None @@ -257,17 +255,15 @@ impl Model { } pub(super) fn on_context_drawer_close(&mut self) { - if let Some(short_id) = self.shortcut_context.take() { - if let Some(model) = self.shortcut_models.get_mut(short_id) { - if let Some(remove_id) = model - .bindings - .iter() - .find(|(_, binding)| !binding.is_saved) - .map(|(id, _)| id) - { - model.bindings.remove(remove_id); - } - } + if let Some(short_id) = self.shortcut_context.take() + && let Some(model) = self.shortcut_models.get_mut(short_id) + && let Some(remove_id) = model + .bindings + .iter() + .find(|(_, binding)| !binding.is_saved) + .map(|(id, _)| id) + { + model.bindings.remove(remove_id); } self.editing = None; @@ -314,107 +310,103 @@ impl Model { pub(super) fn update(&mut self, message: ShortcutMessage) -> Task { match message { ShortcutMessage::AddAnotherKeybinding => { - if let Some(short_id) = self.shortcut_context { - if let Some(model) = self.shortcut_models.get_mut(short_id) { - // If an empty entry exists, focus it instead of creating a new input. - for (binding_id, shortcut) in &mut model.bindings { - if shortcut.binding.is_set() - || Binding::from_str(&shortcut.input).is_ok() - { - continue; - } + if let Some(short_id) = self.shortcut_context + && let Some(model) = self.shortcut_models.get_mut(short_id) + { + // If an empty entry exists, focus it instead of creating a new input. + for (binding_id, shortcut) in &mut model.bindings { + if shortcut.binding.is_set() || Binding::from_str(&shortcut.input).is_ok() { + continue; + } - self.editing = Some(binding_id); - shortcut.reset(); + self.editing = Some(binding_id); + shortcut.reset(); - return Task::batch(vec![ + return Task::batch(vec![ iced_winit::platform_specific::commands::keyboard_shortcuts_inhibit::inhibit_shortcuts(true).discard(), widget::text_input::focus(shortcut.id.clone()), widget::text_input::select_all(shortcut.id.clone()) ]); - } + } - // Create a new input and focus it. - let id = widget::Id::unique(); - self.editing = Some(model.bindings.insert(ShortcutBinding { - id: id.clone(), - binding: Binding::default(), - pending: Binding::default(), - input: String::new(), - is_default: false, - is_saved: false, - })); + // Create a new input and focus it. + let id = widget::Id::unique(); + self.editing = Some(model.bindings.insert(ShortcutBinding { + id: id.clone(), + binding: Binding::default(), + pending: Binding::default(), + input: String::new(), + is_default: false, + is_saved: false, + })); - return Task::batch(vec![ + return Task::batch(vec![ iced_winit::platform_specific::commands::keyboard_shortcuts_inhibit::inhibit_shortcuts(true).discard(), widget::text_input::focus(id.clone()), widget::text_input::select_all(id) ]); - } } } ShortcutMessage::ApplyReplace => { - if let Some((id, new_binding, ..)) = self.replace_dialog.take() { - if let Some(short_id) = self.shortcut_context { - // Remove conflicting bindings that are saved on disk. - self.config_remove(&new_binding); + if let Some((id, new_binding, ..)) = self.replace_dialog.take() + && let Some(short_id) = self.shortcut_context + { + // Remove conflicting bindings that are saved on disk. + self.config_remove(&new_binding); - // Clear any binding that matches this in the current model - for (_, model) in &mut self.shortcut_models { - if let Some(id) = model - .bindings - .iter() - .find(|(_, shortcut)| shortcut.binding == new_binding) - .map(|(id, _)| id) - { - model.bindings.remove(id); - break; - } + // Clear any binding that matches this in the current model + for (_, model) in &mut self.shortcut_models { + if let Some(id) = model + .bindings + .iter() + .find(|(_, shortcut)| shortcut.binding == new_binding) + .map(|(id, _)| id) + { + model.bindings.remove(id); + break; } - - // Update the current model and save the binding to disk. - if let Some(model) = self.shortcut_models.get_mut(short_id) { - if let Some(shortcut) = model.bindings.get_mut(id) { - let prev_binding = shortcut.binding.clone(); - - shortcut.binding = new_binding.clone(); - shortcut.input.clear(); - - if self.editing == Some(id) { - self.editing = None; - } - - let action = model.action.clone(); - self.config_remove(&prev_binding); - self.config_add(action, new_binding); - } - } - - _ = self.on_enter(); } + + // Update the current model and save the binding to disk. + if let Some(model) = self.shortcut_models.get_mut(short_id) + && let Some(shortcut) = model.bindings.get_mut(id) + { + let prev_binding = shortcut.binding.clone(); + + shortcut.binding = new_binding.clone(); + shortcut.input.clear(); + + if self.editing == Some(id) { + self.editing = None; + } + + let action = model.action.clone(); + self.config_remove(&prev_binding); + self.config_add(action, new_binding); + } + + _ = self.on_enter(); } } ShortcutMessage::CancelReplace => { if let Some(((id, _, _, _), short_id)) = self.replace_dialog.take().zip(self.shortcut_context) + && let Some(model) = self.shortcut_models.get_mut(short_id) + && let Some(binding) = model.bindings.get_mut(id) { - if let Some(model) = self.shortcut_models.get_mut(short_id) { - if let Some(binding) = model.bindings.get_mut(id) { - binding.reset(); - return cosmic::widget::text_input::focus(binding.id.clone()); - } - } + binding.reset(); + return cosmic::widget::text_input::focus(binding.id.clone()); } } ShortcutMessage::DeleteBinding(id) => { - if let Some(short_id) = self.shortcut_context { - if let Some(model) = self.shortcut_models.get_mut(short_id) { - let shortcut = model.bindings.remove(id); - if shortcut.is_default { - self.config_add(Action::Disable, shortcut.binding.clone()); - } else { - self.config_remove(&shortcut.binding); - } + if let Some(short_id) = self.shortcut_context + && let Some(model) = self.shortcut_models.get_mut(short_id) + { + let shortcut = model.bindings.remove(id); + if shortcut.is_default { + self.config_add(Action::Disable, shortcut.binding.clone()); + } else { + self.config_remove(&shortcut.binding); } } } @@ -427,34 +419,32 @@ impl Model { ShortcutMessage::EditBinding(id, enable) => { if !enable && self.editing == Some(id) { self.editing = None; - if let Some(short_id) = self.shortcut_context { - if let Some(model) = self.shortcut_models.get_mut(short_id) { - if let Some(shortcut) = model.bindings.get_mut(id) { - shortcut.pending = shortcut.binding.clone(); - shortcut.input = shortcut.binding.to_string(); - } - } + if let Some(short_id) = self.shortcut_context + && let Some(model) = self.shortcut_models.get_mut(short_id) + && let Some(shortcut) = model.bindings.get_mut(id) + { + shortcut.pending = shortcut.binding.clone(); + shortcut.input = shortcut.binding.to_string(); } return Task::batch(vec![ cosmic::widget::text_input::focus(self.add_keybindings_button_id.clone()), iced_winit::platform_specific::commands::keyboard_shortcuts_inhibit::inhibit_shortcuts(false).discard() ]); } - if let Some(short_id) = self.shortcut_context { - if let Some(model) = self.shortcut_models.get_mut(short_id) { - if let Some(shortcut) = model.bindings.get_mut(id) { - if enable { - self.editing = Some(id); - shortcut.input = shortcut.binding.to_string(); - return Task::batch(vec![ + if let Some(short_id) = self.shortcut_context + && let Some(model) = self.shortcut_models.get_mut(short_id) + && let Some(shortcut) = model.bindings.get_mut(id) + { + if enable { + self.editing = Some(id); + shortcut.input = shortcut.binding.to_string(); + return Task::batch(vec![ iced_winit::platform_specific::commands::keyboard_shortcuts_inhibit::inhibit_shortcuts(true).discard(), widget::text_input::select_all(shortcut.id.clone()) ]); - } else if self.editing == Some(id) { - self.editing = None; - return iced_winit::platform_specific::commands::keyboard_shortcuts_inhibit::inhibit_shortcuts(false).discard(); - } - } + } else if self.editing == Some(id) { + self.editing = None; + return iced_winit::platform_specific::commands::keyboard_shortcuts_inhibit::inhibit_shortcuts(false).discard(); } } } @@ -491,12 +481,12 @@ impl Model { crate::app::Message::OpenContextDrawer(self.entity), )]; - if let Some(model) = self.shortcut_models.get(0) { - if let Some(shortcut) = model.bindings.get(0) { - self.editing = Some(0); - tasks.push(widget::text_input::focus(shortcut.id.clone())); - tasks.push(widget::text_input::select_all(shortcut.id.clone())); - } + if let Some(model) = self.shortcut_models.get(0) + && let Some(shortcut) = model.bindings.get(0) + { + self.editing = Some(0); + tasks.push(widget::text_input::focus(shortcut.id.clone())); + tasks.push(widget::text_input::select_all(shortcut.id.clone())); } return Task::batch(tasks); @@ -509,112 +499,106 @@ impl Model { panic!("{}", v); } ShortcutMessage::ModifiersChanged(modifiers) => { - if let Some((short_id, id)) = self.shortcut_context.zip(self.editing) { - if let Some(model) = self.shortcut_models.get_mut(short_id) { - if let Some(shortcut) = model.bindings.get_mut(id) { - let mut cfg_modifiers = - cosmic_settings_config::shortcuts::Modifiers::new(); - if modifiers.alt() { - cfg_modifiers = cfg_modifiers.alt() - } - if modifiers.control() { - cfg_modifiers = cfg_modifiers.ctrl() - } - if modifiers.shift() { - cfg_modifiers = cfg_modifiers.shift() - } - if modifiers.logo() { - cfg_modifiers = cfg_modifiers.logo() - } - let old = - std::mem::replace(&mut shortcut.pending.modifiers, cfg_modifiers); + if let Some((short_id, id)) = self.shortcut_context.zip(self.editing) + && let Some(model) = self.shortcut_models.get_mut(short_id) + && let Some(shortcut) = model.bindings.get_mut(id) + { + let mut cfg_modifiers = cosmic_settings_config::shortcuts::Modifiers::new(); + if modifiers.alt() { + cfg_modifiers = cfg_modifiers.alt() + } + if modifiers.control() { + cfg_modifiers = cfg_modifiers.ctrl() + } + if modifiers.shift() { + cfg_modifiers = cfg_modifiers.shift() + } + if modifiers.logo() { + cfg_modifiers = cfg_modifiers.logo() + } + let old = std::mem::replace(&mut shortcut.pending.modifiers, cfg_modifiers); - if shortcut.pending.keycode.is_none() && modifiers.is_empty() { - if old.logo { - shortcut.pending.modifiers = old; - shortcut.input = shortcut.pending.to_string(); - // XX for now avoid applying the keycode - shortcut.binding.keycode = None; - return Task::batch(vec![ + if shortcut.pending.keycode.is_none() && modifiers.is_empty() { + if old.logo { + shortcut.pending.modifiers = old; + shortcut.input = shortcut.pending.to_string(); + // XX for now avoid applying the keycode + shortcut.binding.keycode = None; + return Task::batch(vec![ iced_winit::platform_specific::commands::keyboard_shortcuts_inhibit::inhibit_shortcuts(false).discard(), self.submit_binding(id), cosmic::widget::text_input::focus(self.add_keybindings_button_id.clone()), ]); - } else if old.alt || old.ctrl || old.shift { - self.editing = None; - shortcut.reset(); - return Task::batch(vec![ + } else if old.alt || old.ctrl || old.shift { + self.editing = None; + shortcut.reset(); + return Task::batch(vec![ cosmic::widget::text_input::focus(self.add_keybindings_button_id.clone()), iced_winit::platform_specific::commands::keyboard_shortcuts_inhibit::inhibit_shortcuts(false).discard() ]); - } - } - shortcut.input = shortcut.pending.to_string(); } } + shortcut.input = shortcut.pending.to_string(); } } ShortcutMessage::KeyReleased(keycode, _, _) => { - if let Some((short_id, id)) = self.shortcut_context.zip(self.editing) { - if let Some(model) = self.shortcut_models.get_mut(short_id) { - if let Some(shortcut) = model.bindings.get_mut(id) { - // if the currently selected shortcut matches, finish selecting shortcut - if shortcut.pending.key.is_some() - && shortcut.pending.keycode.is_some_and(|k| k == keycode) - { - if shortcut.pending.modifiers - != cosmic_settings_config::shortcuts::Modifiers::new() - || shortcut.pending.key.is_some_and(|key| { - key.is_misc_function_key() - || matches!(key.raw(), 0x10080001..=0x1008FFFF) - }) - { - shortcut.input = shortcut.pending.to_string(); - // XX for now avoid applying the keycode - shortcut.binding.keycode = None; - return Task::batch(vec![ + if let Some((short_id, id)) = self.shortcut_context.zip(self.editing) + && let Some(model) = self.shortcut_models.get_mut(short_id) + && let Some(shortcut) = model.bindings.get_mut(id) + { + // if the currently selected shortcut matches, finish selecting shortcut + if shortcut.pending.key.is_some() + && shortcut.pending.keycode.is_some_and(|k| k == keycode) + { + if shortcut.pending.modifiers + != cosmic_settings_config::shortcuts::Modifiers::new() + || shortcut.pending.key.is_some_and(|key| { + key.is_misc_function_key() + || matches!(key.raw(), 0x10080001..=0x1008FFFF) + }) + { + shortcut.input = shortcut.pending.to_string(); + // XX for now avoid applying the keycode + shortcut.binding.keycode = None; + return Task::batch(vec![ iced_winit::platform_specific::commands::keyboard_shortcuts_inhibit::inhibit_shortcuts(false).discard(), self.submit_binding(id), cosmic::widget::text_input::focus(self.add_keybindings_button_id.clone()), ]); - } + } - return Task::batch(vec![ + return Task::batch(vec![ cosmic::widget::text_input::focus(self.add_keybindings_button_id.clone()), iced_winit::platform_specific::commands::keyboard_shortcuts_inhibit::inhibit_shortcuts(false).discard() ]); - } - } } } } ShortcutMessage::KeyPressed(keycode, unmodified_keysym, location, modifiers) => { if unmodified_keysym == Key::Named(Named::Escape) && modifiers.is_empty() { - if let Some((short_id, id)) = self.shortcut_context.zip(self.editing) { - if let Some(model) = self.shortcut_models.get_mut(short_id) { - if let Some(binding) = model.bindings.get_mut(id) { - binding.reset(); - self.editing = None; - return Task::batch(vec![ + if let Some((short_id, id)) = self.shortcut_context.zip(self.editing) + && let Some(model) = self.shortcut_models.get_mut(short_id) + && let Some(binding) = model.bindings.get_mut(id) + { + binding.reset(); + self.editing = None; + return Task::batch(vec![ cosmic::widget::text_input::focus(self.add_keybindings_button_id.clone()), iced_winit::platform_specific::commands::keyboard_shortcuts_inhibit::inhibit_shortcuts(false).discard() ]); - } - } } return Task::none(); } - if let Some((short_id, id)) = self.shortcut_context.zip(self.editing) { - if let Some(model) = self.shortcut_models.get_mut(short_id) { - if let Some(shortcut) = model.bindings.get_mut(id) { - shortcut.pending.keycode = Some(keycode); - shortcut.pending.key = - iced_winit::platform_specific::wayland::keymap::key_to_keysym( - unmodified_keysym, - location, - ); - } - } + if let Some((short_id, id)) = self.shortcut_context.zip(self.editing) + && let Some(model) = self.shortcut_models.get_mut(short_id) + && let Some(shortcut) = model.bindings.get_mut(id) + { + shortcut.pending.keycode = Some(keycode); + shortcut.pending.key = + iced_winit::platform_specific::wayland::keymap::key_to_keysym( + unmodified_keysym, + location, + ); } } } @@ -689,68 +673,65 @@ impl Model { let mut apply_binding = None; // Check for conflicts with the new binding. - if let Some(model) = self.shortcut_models.get_mut(short_id) { - if let Some(shortcut) = model.bindings.get_mut(id) { - if shortcut.input.is_empty() { - return Task::none(); + if let Some(model) = self.shortcut_models.get_mut(short_id) + && let Some(shortcut) = model.bindings.get_mut(id) + { + if shortcut.input.is_empty() { + return Task::none(); + } + + match Binding::from_str(&shortcut.input) { + Ok(new_binding) => { + if shortcut.binding == new_binding { + return Task::none(); + } + + if !new_binding.is_set() { + shortcut.reset(); + return Task::none(); + } + + if let Some(action) = self.config_contains(&new_binding) { + let action_str = if let Action::Spawn(_) = &action { + super::localize_custom_action(&action, &new_binding) + } else { + super::localize_action(&action) + }; + self.replace_dialog = Some((id, new_binding, action, action_str)); + return Task::none(); + } + + apply_binding = Some(new_binding); } - match Binding::from_str(&shortcut.input) { - Ok(new_binding) => { - if shortcut.binding == new_binding { - return Task::none(); - } - - if !new_binding.is_set() { - shortcut.reset(); - return Task::none(); - } - - if let Some(action) = self.config_contains(&new_binding) { - let action_str = if let Action::Spawn(_) = &action { - super::localize_custom_action(&action, &new_binding) - } else { - super::localize_action(&action) - }; - self.replace_dialog = Some((id, new_binding, action, action_str)); - return Task::none(); - } - - apply_binding = Some(new_binding); - } - - Err(why) => { - tracing::error!(why, "keybinding input invalid"); - shortcut.reset(); - } + Err(why) => { + tracing::error!(why, "keybinding input invalid"); + shortcut.reset(); } } } // Apply if no conflict was found. - if let Some(new_binding) = apply_binding { - if let Some(model) = self.shortcut_models.get_mut(short_id) { - if let Some(shortcut) = model.bindings.get_mut(id) { - let prev_binding = mem::replace(&mut shortcut.binding, new_binding.clone()); - shortcut.is_saved = true; - shortcut.reset(); + if let Some(new_binding) = apply_binding + && let Some(model) = self.shortcut_models.get_mut(short_id) + && let Some(shortcut) = model.bindings.get_mut(id) + { + let prev_binding = mem::replace(&mut shortcut.binding, new_binding.clone()); + shortcut.is_saved = true; + shortcut.reset(); - if self.editing == Some(id) { - self.editing = None; - } - - let action = model.action.clone(); - if shortcut.is_default { - self.config_add(Action::Disable, prev_binding); - } else { - self.config_remove(&prev_binding); - } - self.config_add(action, new_binding); - return cosmic::widget::text_input::focus( - self.add_keybindings_button_id.clone(), - ); - } + if self.editing == Some(id) { + self.editing = None; } + + let action = model.action.clone(); + if shortcut.is_default { + self.config_add(Action::Disable, prev_binding); + } else { + self.config_remove(&prev_binding); + } + self.config_add(action, new_binding); + return cosmic::widget::text_input::focus(self.add_keybindings_button_id.clone()); } } diff --git a/cosmic-settings/src/pages/networking/wifi.rs b/cosmic-settings/src/pages/networking/wifi.rs index d4060ec..84f1780 100644 --- a/cosmic-settings/src/pages/networking/wifi.rs +++ b/cosmic-settings/src/pages/networking/wifi.rs @@ -431,18 +431,17 @@ impl Page { hw_address, .. } = dialog + && let Some(nm) = self.nm_state.as_mut() { - if let Some(nm) = self.nm_state.as_mut() { - self.connecting.insert(ssid.clone()); - _ = nm - .sender - .unbounded_send(network_manager::Request::Authenticate { - ssid: ssid.to_string(), - identity, - hw_address, - password, - }); - } + self.connecting.insert(ssid.clone()); + _ = nm + .sender + .unbounded_send(network_manager::Request::Authenticate { + ssid: ssid.to_string(), + identity, + hw_address, + password, + }); } } @@ -848,19 +847,16 @@ fn connection_settings(conn: zbus::Connection) -> Task { }) // Reduce the settings list into a SSID->UUID map. .fold(BTreeMap::new(), |mut set, settings| async move { - if let Some(ref wifi) = settings.wifi { - if let Some(ssid) = wifi + if let Some(ref wifi) = settings.wifi + && let Some(ssid) = wifi .ssid .clone() .and_then(|ssid| String::from_utf8(ssid).ok()) - { - if let Some(ref connection) = settings.connection { - if let Some(uuid) = connection.uuid.clone() { - set.insert(ssid.into(), uuid.into()); - return set; - } - } - } + && let Some(ref connection) = settings.connection + && let Some(uuid) = connection.uuid.clone() + { + set.insert(ssid.into(), uuid.into()); + return set; } set diff --git a/cosmic-settings/src/pages/power/backend/mod.rs b/cosmic-settings/src/pages/power/backend/mod.rs index d10e629..02b843d 100644 --- a/cosmic-settings/src/pages/power/backend/mod.rs +++ b/cosmic-settings/src/pages/power/backend/mod.rs @@ -319,15 +319,15 @@ impl Battery { && (percent - 100.0_f64).abs() < f64::EPSILON; if !is_charging { - if let Ok(time) = proxy.time_to_empty().await { - if let Ok(dur) = Duration::from_std(std::time::Duration::from_secs(time as u64)) { - remaining_duration = dur; - } - } - } else if let Ok(time) = proxy.time_to_full().await { - if let Ok(dur) = Duration::from_std(std::time::Duration::from_secs(time as u64)) { + if let Ok(time) = proxy.time_to_empty().await + && let Ok(dur) = Duration::from_std(std::time::Duration::from_secs(time as u64)) + { remaining_duration = dur; } + } else if let Ok(time) = proxy.time_to_full().await + && let Ok(dur) = Duration::from_std(std::time::Duration::from_secs(time as u64)) + { + remaining_duration = dur; } let battery_percent = if percent > 95.0 { diff --git a/cosmic-settings/src/pages/power/mod.rs b/cosmic-settings/src/pages/power/mod.rs index 75b81f7..a4e5965 100644 --- a/cosmic-settings/src/pages/power/mod.rs +++ b/cosmic-settings/src/pages/power/mod.rs @@ -43,7 +43,7 @@ static SUSPEND_TIMES: &[Duration] = &[ fn format_time(duration: Duration) -> String { let m = duration.as_secs() / 60; - if m % 60 == 0 { + if m.is_multiple_of(60) { fl!("x-hours", number = (m / 60)) } else { fl!("x-minutes", number = m) diff --git a/cosmic-settings/src/pages/system/users/getent.rs b/cosmic-settings/src/pages/system/users/getent.rs index 60c8a53..4078567 100644 --- a/cosmic-settings/src/pages/system/users/getent.rs +++ b/cosmic-settings/src/pages/system/users/getent.rs @@ -27,10 +27,11 @@ pub fn passwd(range: (u64, u64)) -> Vec { _ => (), } - if let Ok(user) = line.trim().parse::() { - if user.uid >= range.0 && user.uid <= range.1 { - users.push(user); - } + if let Ok(user) = line.trim().parse::() + && user.uid >= range.0 + && user.uid <= range.1 + { + users.push(user); } } } diff --git a/cosmic-settings/src/pages/system/users/mod.rs b/cosmic-settings/src/pages/system/users/mod.rs index 049be48..5eb6b3b 100644 --- a/cosmic-settings/src/pages/system/users/mod.rs +++ b/cosmic-settings/src/pages/system/users/mod.rs @@ -965,12 +965,12 @@ fn get_encrypt_method() -> String { let reader = BufReader::new(login_defs); for line in reader.lines().map_while(Result::ok) { - if !line.trim().is_empty() { - if let Some(index) = line.find(|c: char| c.is_whitespace()) { - let key = line[0..index].trim(); - if key == "ENCRYPT_METHOD" { - value = line[(index + 1)..].trim().to_string(); - } + if !line.trim().is_empty() + && let Some(index) = line.find(|c: char| c.is_whitespace()) + { + let key = line[0..index].trim(); + if key == "ENCRYPT_METHOD" { + value = line[(index + 1)..].trim().to_string(); } } } diff --git a/cosmic-settings/src/pages/time/region.rs b/cosmic-settings/src/pages/time/region.rs index db265dd..4163265 100644 --- a/cosmic-settings/src/pages/time/region.rs +++ b/cosmic-settings/src/pages/time/region.rs @@ -204,24 +204,22 @@ impl Page { pub fn update(&mut self, message: Message) -> cosmic::Task { match message { Message::AddLanguage(id) => { - if let Some(language) = self.available_languages.get(id) { - if let Some((config, locales)) = self.config.as_mut() { - if !locales.contains(&language.lang_code) { - locales.push(language.lang_code.clone()); - _ = config.set("system_locales", &locales); - } - } + if let Some(language) = self.available_languages.get(id) + && let Some((config, locales)) = self.config.as_mut() + && !locales.contains(&language.lang_code) + { + locales.push(language.lang_code.clone()); + _ = config.set("system_locales", &locales); } } Message::RemoveLanguage(id) => { - if let Some(language) = self.available_languages.remove(id) { - if let Some((config, locales)) = self.config.as_mut() { - if let Some(pos) = locales.iter().position(|l| l == &language.lang_code) { - locales.remove(pos); - _ = config.set("system_locales", &locales); - } - } + if let Some(language) = self.available_languages.remove(id) + && let Some((config, locales)) = self.config.as_mut() + && let Some(pos) = locales.iter().position(|l| l == &language.lang_code) + { + locales.remove(pos); + _ = config.set("system_locales", &locales); } } @@ -235,10 +233,10 @@ impl Page { let region = region.lang_code.clone(); return cosmic::task::future(async move { - if let Ok(exit_status) = set_locale(lang, region.clone()).await { - if exit_status.success() { - update_time_settings_after_region_change(region); - } + if let Ok(exit_status) = set_locale(lang, region.clone()).await + && exit_status.success() + { + update_time_settings_after_region_change(region); } Message::Refresh(Arc::new(page_reload().await)) @@ -315,24 +313,23 @@ impl Page { _ = config.set("system_locales", &locales); - if let Some(language_code) = locales.first() { - if let Some(language) = self + if let Some(language_code) = locales.first() + && let Some(language) = self .available_languages .values() .find(|lang| &lang.lang_code == language_code) - { - let language = language.clone(); - self.language = Some(language.clone()); - let region = self.region.clone(); + { + let language = language.clone(); + self.language = Some(language.clone()); + let region = self.region.clone(); - tokio::spawn(async move { - _ = set_locale( - language.lang_code.clone(), - region.unwrap_or(language).lang_code.clone(), - ) - .await; - }); - } + tokio::spawn(async move { + _ = set_locale( + language.lang_code.clone(), + region.unwrap_or(language).lang_code.clone(), + ) + .await; + }); } } } diff --git a/subscriptions/a11y-manager/src/lib.rs b/subscriptions/a11y-manager/src/lib.rs index ffdb971..0bcd849 100644 --- a/subscriptions/a11y-manager/src/lib.rs +++ b/subscriptions/a11y-manager/src/lib.rs @@ -25,21 +25,16 @@ pub enum AccessibilityEvent { Closed, } -#[derive(Debug, Clone, Copy, PartialEq, Eq, FromPrimitive, ToPrimitive)] +#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, FromPrimitive, ToPrimitive)] pub enum ColorFilter { Greyscale, Deuteranopia, Protanopia, Tritanopia, + #[default] Unknown, } -impl Default for ColorFilter { - fn default() -> Self { - ColorFilter::Unknown - } -} - #[derive(Debug, Clone, Copy)] pub enum AccessibilityRequest { Magnifier(bool), diff --git a/subscriptions/network-manager/src/available_wifi.rs b/subscriptions/network-manager/src/available_wifi.rs index 5b3058a..409580c 100644 --- a/subscriptions/network-manager/src/available_wifi.rs +++ b/subscriptions/network-manager/src/available_wifi.rs @@ -22,13 +22,9 @@ pub async fn handle_wireless_device( let mut scan_changed = device.receive_last_scan_changed().await; if let Some(t) = scan_changed.next().await { - match t.get().await { - Ok(-1) => { - tracing::error!("wireless device scan errored"); - return Ok(Default::default()); - } - - _ => (), + if let Ok(-1) = t.get().await { + tracing::error!("wireless device scan errored"); + return Ok(Default::default()); } } diff --git a/subscriptions/network-manager/src/devices.rs b/subscriptions/network-manager/src/devices.rs index 6fa4372..d111f2d 100644 --- a/subscriptions/network-manager/src/devices.rs +++ b/subscriptions/network-manager/src/devices.rs @@ -36,8 +36,8 @@ pub struct KnownDeviceConnection { pub uuid: Arc, } -pub async fn list<'a>( - conn: &'a zbus::Connection, +pub async fn list( + conn: &zbus::Connection, device_type_filter: fn(DeviceType) -> bool, ) -> zbus::Result> { let nm = NetworkManager::new(conn).await?; diff --git a/subscriptions/network-manager/src/hw_address.rs b/subscriptions/network-manager/src/hw_address.rs index 2e3636d..9803780 100644 --- a/subscriptions/network-manager/src/hw_address.rs +++ b/subscriptions/network-manager/src/hw_address.rs @@ -16,19 +16,23 @@ impl HwAddress { } u64::from_str_radix(columnless_vec.join("").as_str(), 16) .ok() - .and_then(|address| Some(HwAddress { address })) + .map(|address| HwAddress { address }) } pub fn from_string(arg: &String) -> Option { HwAddress::from_str(arg.as_str()) } - pub fn to_string(&self) -> String { - format!("{:#x}", self.address) +} + +impl std::fmt::Display for HwAddress { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let hex = format!("{:#x}", self.address) .trim_start_matches("0x") .chars() .collect::>() .chunks(2) .map(|chunk| chunk.iter().cloned().collect::()) .collect::>() - .join(":") + .join(":"); + write!(f, "{}", hex) } } diff --git a/subscriptions/network-manager/src/lib.rs b/subscriptions/network-manager/src/lib.rs index 880b09e..c5ecb3a 100644 --- a/subscriptions/network-manager/src/lib.rs +++ b/subscriptions/network-manager/src/lib.rs @@ -620,7 +620,7 @@ impl NetworkManagerState { std::str::from_utf8(&output.stdout) .ok() - .map_or(false, |stdout| stdout.contains("Soft blocked: yes")) + .is_some_and(|stdout| stdout.contains("Soft blocked: yes")) }), network_manager .wireless_enabled() @@ -714,7 +714,7 @@ impl NetworkManagerState { .iter() .filter(|a| { known_ssid.contains(&a.ssid) - && !active_conns.iter().any(|ac| &ac.name() == a.ssid.as_ref()) + && !active_conns.iter().any(|ac| ac.name() == a.ssid.as_ref()) }) .cloned() .collect(); @@ -733,7 +733,7 @@ impl NetworkManagerState { self.wireless_access_points = Vec::new(); } - async fn connect_wifi<'a>( + async fn connect_wifi( &self, conn: &zbus::Connection, ssid: &str, @@ -844,8 +844,8 @@ impl NetworkManagerState { let (_, active_conn) = nm .add_and_activate_connection(conn_settings, device.inner().path(), &ap.path) .await?; - let dummy = ActiveConnectionProxy::new(&conn, active_conn).await?; - let active = ActiveConnectionProxy::builder(&conn) + let dummy = ActiveConnectionProxy::new(conn, active_conn).await?; + let active = ActiveConnectionProxy::builder(conn) .destination(dummy.inner().destination().to_owned()) .unwrap() .interface(dummy.inner().interface().to_owned()) @@ -867,14 +867,13 @@ impl NetworkManagerState { } else if let Ok(enums::ActiveConnectionState::Deactivated) = state { return Err(Error::ConnectionActivate); } - match tokio::time::timeout(Duration::from_secs(20), changes.next()).await { - Ok(Some(s)) => { - let state = s.get().await.unwrap_or_default().into(); - if matches!(state, enums::ActiveConnectionState::Activated) { - return Ok(()); - } + if let Ok(Some(s)) = + tokio::time::timeout(Duration::from_secs(20), changes.next()).await + { + let state = s.get().await.unwrap_or_default().into(); + if matches!(state, enums::ActiveConnectionState::Activated) { + return Ok(()); } - _ => {} }; count -= 1;