From 7bed1ddef1df137dfd9afd0fee855901b331dc90 Mon Sep 17 00:00:00 2001 From: Daniel Eades Date: Sun, 19 Nov 2023 14:54:47 +0000 Subject: [PATCH 1/2] refactor --- cosmic-config/src/lib.rs | 4 +-- examples/cosmic/src/window/demo.rs | 1 - examples/cosmic/src/window/editor.rs | 2 +- src/app/cosmic.rs | 2 +- src/app/mod.rs | 1 - src/ext.rs | 8 +++--- src/theme/style/button.rs | 2 +- src/theme/style/iced.rs | 24 ++++++++--------- src/widget/button/widget.rs | 15 ++++++----- src/widget/color_picker/mod.rs | 21 +++++++++------ src/widget/context_drawer/widget.rs | 1 - src/widget/dropdown/menu/mod.rs | 6 ++--- src/widget/dropdown/widget.rs | 2 -- src/widget/icon/mod.rs | 20 +++++++++++--- src/widget/segmented_button/horizontal.rs | 6 ++--- src/widget/segmented_button/model/mod.rs | 4 +-- src/widget/text_input/cursor.rs | 2 +- src/widget/text_input/input.rs | 33 ++++++++--------------- 18 files changed, 78 insertions(+), 76 deletions(-) diff --git a/cosmic-config/src/lib.rs b/cosmic-config/src/lib.rs index 374bf29d3a8..73fb4a751d3 100644 --- a/cosmic-config/src/lib.rs +++ b/cosmic-config/src/lib.rs @@ -215,7 +215,7 @@ impl Config { //TODO: handle errors } }, - Err(err) => { + Err(_err) => { //TODO: handle errors } } @@ -224,7 +224,7 @@ impl Config { f(&watch_config, &keys); } } - Err(err) => { + Err(_err) => { //TODO: handle errors } } diff --git a/examples/cosmic/src/window/demo.rs b/examples/cosmic/src/window/demo.rs index 5b42b7d2f8c..5a484f09a71 100644 --- a/examples/cosmic/src/window/demo.rs +++ b/examples/cosmic/src/window/demo.rs @@ -5,7 +5,6 @@ use cosmic::{ cosmic_theme, iced::widget::{checkbox, column, progress_bar, radio, slider, text, text_input}, iced::{id, Alignment, Length}, - iced_core::Color, theme::ThemeType, widget::{ button, color_picker::ColorPickerUpdate, cosmic_container::container, dropdown, icon, diff --git a/examples/cosmic/src/window/editor.rs b/examples/cosmic/src/window/editor.rs index 8dc8be2f0ea..764a4cfe0ba 100644 --- a/examples/cosmic/src/window/editor.rs +++ b/examples/cosmic/src/window/editor.rs @@ -1,7 +1,7 @@ use cosmic::iced::widget::{horizontal_space, row}; use cosmic::iced::{Alignment, Length}; use cosmic::widget::{button, icon, segmented_button, view_switcher}; -use cosmic::{theme, Apply, Element}; +use cosmic::{Apply, Element}; use slotmap::Key; #[derive(Clone, Copy, Debug)] diff --git a/src/app/cosmic.rs b/src/app/cosmic.rs index f820649f6c2..fd97746e89e 100644 --- a/src/app/cosmic.rs +++ b/src/app/cosmic.rs @@ -78,7 +78,7 @@ where fn new((core, flags): Self::Flags) -> (Self, iced::Command) { let (model, command) = T::init(core, flags); - (Cosmic::new(model), command) + (Self::new(model), command) } #[cfg(feature = "wayland")] diff --git a/src/app/mod.rs b/src/app/mod.rs index 91fb8d0b129..82a7967e8be 100644 --- a/src/app/mod.rs +++ b/src/app/mod.rs @@ -259,7 +259,6 @@ impl DbusActivation { } #[cfg(feature = "single-instance")] - /// Launch a COSMIC application with the given [`Settings`]. /// If the application is already running, the arguments will be passed to the /// running instance. diff --git a/src/ext.rs b/src/ext.rs index f44669a864b..f182371f35b 100644 --- a/src/ext.rs +++ b/src/ext.rs @@ -92,11 +92,11 @@ pub trait ColorExt { impl ColorExt for iced::Color { fn blend_alpha(self, background: Self, alpha: f32) -> Self { - Color { + Self { a: 1.0, - r: background.r + (self.r - background.r) * alpha, - g: background.g + (self.g - background.g) * alpha, - b: background.b + (self.b - background.b) * alpha, + r: (self.r - background.r).mul_add(alpha, background.r), + g: (self.g - background.g).mul_add(alpha, background.g), + b: (self.b - background.b).mul_add(alpha, background.b), } } } diff --git a/src/theme/style/button.rs b/src/theme/style/button.rs index e98ec0d0501..ec263eb5176 100644 --- a/src/theme/style/button.rs +++ b/src/theme/style/button.rs @@ -68,7 +68,7 @@ pub fn appearance( } Button::Icon | Button::IconVertical => { - if let Button::IconVertical = style { + if matches!(style, Button::IconVertical) { corner_radii = &cosmic.corner_radii.radius_m; } diff --git a/src/theme/style/iced.rs b/src/theme/style/iced.rs index 93b2a00be21..d4868618f06 100644 --- a/src/theme/style/iced.rs +++ b/src/theme/style/iced.rs @@ -84,17 +84,17 @@ impl Button { fn cosmic<'a>(&'a self, theme: &'a Theme) -> &CosmicComponent { let cosmic = theme.cosmic(); match self { - Button::Primary => &cosmic.accent_button, - Button::Secondary => &theme.current_container().component, - Button::Positive => &cosmic.success_button, - Button::Destructive => &cosmic.destructive_button, - Button::Text => &cosmic.text_button, - Button::Link => &cosmic.accent_button, - Button::LinkActive => &cosmic.accent_button, - Button::Transparent => &TRANSPARENT_COMPONENT, - Button::Deactivated => &theme.current_container().component, - Button::Card => &theme.current_container().component, - Button::Custom { .. } => &TRANSPARENT_COMPONENT, + Self::Primary => &cosmic.accent_button, + Self::Secondary => &theme.current_container().component, + Self::Positive => &cosmic.success_button, + Self::Destructive => &cosmic.destructive_button, + Self::Text => &cosmic.text_button, + Self::Link => &cosmic.accent_button, + Self::LinkActive => &cosmic.accent_button, + Self::Transparent => &TRANSPARENT_COMPONENT, + Self::Deactivated => &theme.current_container().component, + Self::Card => &theme.current_container().component, + Self::Custom { .. } => &TRANSPARENT_COMPONENT, } } } @@ -950,7 +950,7 @@ pub enum Text { impl From for Text { fn from(color: Color) -> Self { - Text::Color(color) + Self::Color(color) } } diff --git a/src/widget/button/widget.rs b/src/widget/button/widget.rs index 1a98536f086..624003e4960 100644 --- a/src/widget/button/widget.rs +++ b/src/widget/button/widget.rs @@ -306,7 +306,7 @@ where } } - if let event::Status::Captured = self.content.as_widget_mut().on_event( + if self.content.as_widget_mut().on_event( &mut tree.children[0], event.clone(), layout.children().next().unwrap(), @@ -315,7 +315,8 @@ where clipboard, shell, viewport, - ) { + ) == event::Status::Captured + { return event::Status::Captured; } @@ -557,8 +558,8 @@ pub struct State { impl State { /// Creates a new [`State`]. - pub fn new() -> State { - State::default() + pub fn new() -> Self { + Self::default() } /// Returns whether the [`Button`] is currently focused or not. @@ -806,15 +807,15 @@ pub fn focus(id: Id) -> Command { impl operation::Focusable for State { fn is_focused(&self) -> bool { - State::is_focused(*self) + Self::is_focused(*self) } fn focus(&mut self) { - State::focus(self); + Self::focus(self); } fn unfocus(&mut self) { - State::unfocus(self); + Self::unfocus(self); } } diff --git a/src/widget/color_picker/mod.rs b/src/widget/color_picker/mod.rs index 54c6eab1f52..327b0b7f412 100644 --- a/src/widget/color_picker/mod.rs +++ b/src/widget/color_picker/mod.rs @@ -589,16 +589,20 @@ where let t = t.cosmic(); let handle_radius = f32::from(t.space_xs()) / 2.0; let (x, y) = ( - (self.active_color.saturation * bounds.width) + bounds.position().x - handle_radius, - ((1.0 - self.active_color.value) * bounds.height) + bounds.position().y - handle_radius, + self.active_color + .saturation + .mul_add(bounds.width, bounds.position().x) + - handle_radius, + (1.0 - self.active_color.value).mul_add(bounds.height, bounds.position().y) + - handle_radius, ); renderer.fill_quad( Quad { bounds: Rectangle { x, y, - width: 1.0 + handle_radius * 2.0, - height: 1.0 + handle_radius * 2.0, + width: handle_radius.mul_add(2.0, 1.0), + height: handle_radius.mul_add(2.0, 1.0), }, border_radius: (1.0 + handle_radius).into(), border_width: 1.0, @@ -654,8 +658,8 @@ where match event { Event::Mouse(mouse::Event::CursorMoved { .. } | mouse::Event::CursorEntered) => { if let Some(mut clamped) = cursor.position() { - clamped.x = clamped.x.min(bounds.x + bounds.width).max(bounds.x); - clamped.y = clamped.y.min(bounds.y + bounds.height).max(bounds.y); + clamped.x = clamped.x.clamp(bounds.x, bounds.x + bounds.width); + clamped.y = clamped.y.clamp(bounds.y, bounds.y + bounds.height); let relative_pos = clamped - bounds.position(); let (s, v) = ( relative_pos.x / bounds.width, @@ -678,7 +682,7 @@ where } let column_tree = &mut tree.children[0]; - if let event::Status::Captured = self.inner.as_widget_mut().on_event( + if self.inner.as_widget_mut().on_event( column_tree, event.clone(), column_layout, @@ -687,7 +691,8 @@ where clipboard, shell, viewport, - ) { + ) == event::Status::Captured + { return event::Status::Captured; } diff --git a/src/widget/context_drawer/widget.rs b/src/widget/context_drawer/widget.rs index 3be9b977362..c0cd941b15a 100644 --- a/src/widget/context_drawer/widget.rs +++ b/src/widget/context_drawer/widget.rs @@ -17,7 +17,6 @@ use iced_core::{ }; use iced_renderer::core::widget::OperationOutputWrapper; -pub use iced_style::container::{Appearance, StyleSheet}; #[must_use] pub struct ContextDrawer<'a, Message> { diff --git a/src/widget/dropdown/menu/mod.rs b/src/widget/dropdown/menu/mod.rs index 1c3bffd3c73..4797adb1f6d 100644 --- a/src/widget/dropdown/menu/mod.rs +++ b/src/widget/dropdown/menu/mod.rs @@ -423,14 +423,14 @@ impl<'a, S: AsRef, Message> Widget for List<'a, S let bounds = Rectangle { x: bounds.x, - y: bounds.y + (option_height * i as f32), + y: option_height.mul_add(i as f32, bounds.y), width: bounds.width, height: option_height, }; let (color, font) = if self.selected_option == Some(i) { let item_x = bounds.x + appearance.border_width; - let item_width = bounds.width - appearance.border_width * 2.0; + let item_width = appearance.border_width.mul_add(-2.0, bounds.width); renderer.fill_quad( renderer::Quad { @@ -463,7 +463,7 @@ impl<'a, S: AsRef, Message> Widget for List<'a, S (appearance.selected_text_color, crate::font::FONT_SEMIBOLD) } else if *self.hovered_option == Some(i) { let item_x = bounds.x + appearance.border_width; - let item_width = bounds.width - appearance.border_width * 2.0; + let item_width = appearance.border_width.mul_add(-2.0, bounds.width); renderer.fill_quad( renderer::Quad { diff --git a/src/widget/dropdown/widget.rs b/src/widget/dropdown/widget.rs index 720ba6c0ed4..b722f9561a5 100644 --- a/src/widget/dropdown/widget.rs +++ b/src/widget/dropdown/widget.rs @@ -173,7 +173,6 @@ impl<'a, S: AsRef, Message: 'a> Widget for Dropdo self.gap, self.padding, self.text_size.unwrap_or(14.0), - self.font, self.selections, self.selected, &self.on_selected, @@ -359,7 +358,6 @@ pub fn overlay<'a, S: AsRef, Message: 'a>( gap: f32, padding: Padding, text_size: f32, - font: Option, selections: &'a [S], selected_option: Option, on_selected: &'a dyn Fn(usize) -> Message, diff --git a/src/widget/icon/mod.rs b/src/widget/icon/mod.rs index 844b07ae62b..93442d42ac7 100644 --- a/src/widget/icon/mod.rs +++ b/src/widget/icon/mod.rs @@ -72,8 +72,14 @@ impl Icon { fn into_element(self) -> Element<'static, Message> { let from_image = |handle| { Image::new(handle) - .width(self.width.unwrap_or(Length::Fixed(f32::from(self.size)))) - .height(self.height.unwrap_or(Length::Fixed(f32::from(self.size)))) + .width( + self.width + .unwrap_or_else(|| Length::Fixed(f32::from(self.size))), + ) + .height( + self.height + .unwrap_or_else(|| Length::Fixed(f32::from(self.size))), + ) .content_fit(self.content_fit) .into() }; @@ -81,8 +87,14 @@ impl Icon { let from_svg = |handle| { Svg::::new(handle) .style(self.style.clone()) - .width(self.width.unwrap_or(Length::Fixed(f32::from(self.size)))) - .height(self.height.unwrap_or(Length::Fixed(f32::from(self.size)))) + .width( + self.width + .unwrap_or_else(|| Length::Fixed(f32::from(self.size))), + ) + .height( + self.height + .unwrap_or_else(|| Length::Fixed(f32::from(self.size))), + ) .content_fit(self.content_fit) .symbolic(self.handle.symbolic) .into() diff --git a/src/widget/segmented_button/horizontal.rs b/src/widget/segmented_button/horizontal.rs index 83bc50b9fb3..03d696b2a28 100644 --- a/src/widget/segmented_button/horizontal.rs +++ b/src/widget/segmented_button/horizontal.rs @@ -48,10 +48,10 @@ where let num = self.model.items.len(); if num != 0 { let spacing = f32::from(self.spacing); - bounds.width = (bounds.width - (num as f32 * spacing) + spacing) / num as f32; + bounds.width = ((num as f32).mul_add(-spacing, bounds.width) + spacing) / num as f32; if nth != 0 { - bounds.x += (nth as f32 * bounds.width) + (nth as f32 * spacing); + bounds.x += (nth as f32).mul_add(bounds.width, nth as f32 * spacing); } } @@ -69,7 +69,7 @@ where let spacing = f32::from(self.spacing); if num != 0 { - width = (num as f32 * width) + (num as f32 * spacing) - spacing; + width = (num as f32).mul_add(width, num as f32 * spacing) - spacing; } let size = limits diff --git a/src/widget/segmented_button/model/mod.rs b/src/widget/segmented_button/model/mod.rs index 5f2e72eb1f1..49003239c31 100644 --- a/src/widget/segmented_button/model/mod.rs +++ b/src/widget/segmented_button/model/mod.rs @@ -183,7 +183,7 @@ where self.storage .0 .entry(TypeId::of::()) - .or_insert_with(SecondaryMap::new) + .or_default() .insert(id, Box::new(data)); } } @@ -297,7 +297,7 @@ where } pub fn indent(&self, id: Entity) -> Option { - self.indents.get(id).map(|indent| *indent) + self.indents.get(id).copied() } pub fn indent_set(&mut self, id: Entity, indent: u16) -> Option { diff --git a/src/widget/text_input/cursor.rs b/src/widget/text_input/cursor.rs index b185cf6c986..a42596e1380 100644 --- a/src/widget/text_input/cursor.rs +++ b/src/widget/text_input/cursor.rs @@ -28,7 +28,7 @@ pub enum State { impl Default for Cursor { fn default() -> Self { - Cursor { + Self { state: State::Index(0), } } diff --git a/src/widget/text_input/input.rs b/src/widget/text_input/input.rs index bbff1e254fc..8930a55dde4 100644 --- a/src/widget/text_input/input.rs +++ b/src/widget/text_input/input.rs @@ -915,7 +915,7 @@ pub fn layout( text_node.move_to(Point::new( padding.left + leading_icon_width, - padding.top + ((text_input_height - text_size * 1.2) / 2.0).max(0.0), + padding.top + (text_size.mul_add(-1.2, text_input_height) / 2.0).max(0.0), )); let mut node_list: Vec<_> = Vec::with_capacity(3); @@ -1515,18 +1515,6 @@ where } } #[cfg(feature = "wayland")] - Event::PlatformSpecific(PlatformSpecific::Wayland(wayland::Event::DataSource( - wayland::DataSourceEvent::Cancelled - | wayland::DataSourceEvent::DndFinished - | wayland::DataSourceEvent::Cancelled, - ))) => { - let state = state(); - if matches!(state.dragging_state, Some(DraggingState::Dnd(..))) { - state.dragging_state = None; - return event::Status::Captured; - } - } - #[cfg(feature = "wayland")] Event::PlatformSpecific(PlatformSpecific::Wayland(wayland::Event::DataSource( wayland::DataSourceEvent::DndActionAccepted(action), ))) => { @@ -1872,8 +1860,8 @@ pub fn draw<'a, Message>( let offset_bounds = Rectangle { x: bounds.x - border_offset, y: bounds.y - border_offset, - width: bounds.width + border_offset * 2.0, - height: bounds.height + border_offset * 2.0, + width: border_offset.mul_add(2.0, bounds.width), + height: border_offset.mul_add(2.0, bounds.height), }; renderer.fill_quad( renderer::Quad { @@ -2228,6 +2216,7 @@ impl State { Self { is_focused: None, dragging_state: None, + #[allow(clippy::default_constructed_unit_structs)] dnd_offer: DndOfferState::default(), is_pasting: None, last_click: None, @@ -2288,33 +2277,33 @@ impl State { impl operation::Focusable for State { fn is_focused(&self) -> bool { - State::is_focused(self) + Self::is_focused(self) } fn focus(&mut self) { - State::focus(self); + Self::focus(self); } fn unfocus(&mut self) { - State::unfocus(self); + Self::unfocus(self); } } impl operation::TextInput for State { fn move_cursor_to_front(&mut self) { - State::move_cursor_to_front(self); + Self::move_cursor_to_front(self); } fn move_cursor_to_end(&mut self) { - State::move_cursor_to_end(self); + Self::move_cursor_to_end(self); } fn move_cursor_to(&mut self, position: usize) { - State::move_cursor_to(self, position); + Self::move_cursor_to(self, position); } fn select_all(&mut self) { - State::select_all(self); + Self::select_all(self); } } From d9a2bc9b4512b14000c3ffa1b097ea9014220fad Mon Sep 17 00:00:00 2001 From: Daniel Eades Date: Tue, 21 Nov 2023 20:07:43 +0000 Subject: [PATCH 2/2] fixup --- src/widget/button/widget.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/widget/button/widget.rs b/src/widget/button/widget.rs index 624003e4960..597993999d7 100644 --- a/src/widget/button/widget.rs +++ b/src/widget/button/widget.rs @@ -550,6 +550,7 @@ where /// The local state of a [`Button`]. #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] +#[allow(clippy::struct_field_names)] pub struct State { is_hovered: bool, is_pressed: bool,