Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
danieleades committed Nov 21, 2023
1 parent 001fd74 commit 7bed1dd
Show file tree
Hide file tree
Showing 18 changed files with 78 additions and 76 deletions.
4 changes: 2 additions & 2 deletions cosmic-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ impl Config {
//TODO: handle errors
}
},
Err(err) => {
Err(_err) => {
//TODO: handle errors
}
}
Expand All @@ -224,7 +224,7 @@ impl Config {
f(&watch_config, &keys);
}
}
Err(err) => {
Err(_err) => {
//TODO: handle errors
}
}
Expand Down
1 change: 0 additions & 1 deletion examples/cosmic/src/window/demo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion examples/cosmic/src/window/editor.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down
2 changes: 1 addition & 1 deletion src/app/cosmic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ where
fn new((core, flags): Self::Flags) -> (Self, iced::Command<Self::Message>) {
let (model, command) = T::init(core, flags);

(Cosmic::new(model), command)
(Self::new(model), command)
}

#[cfg(feature = "wayland")]
Expand Down
1 change: 0 additions & 1 deletion src/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 4 additions & 4 deletions src/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
}
}
2 changes: 1 addition & 1 deletion src/theme/style/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
24 changes: 12 additions & 12 deletions src/theme/style/iced.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
}
Expand Down Expand Up @@ -950,7 +950,7 @@ pub enum Text {

impl From<Color> for Text {
fn from(color: Color) -> Self {
Text::Color(color)
Self::Color(color)
}
}

Expand Down
15 changes: 8 additions & 7 deletions src/widget/button/widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -315,7 +315,8 @@ where
clipboard,
shell,
viewport,
) {
) == event::Status::Captured
{
return event::Status::Captured;
}

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -806,15 +807,15 @@ pub fn focus<Message: 'static>(id: Id) -> Command<Message> {

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);
}
}

Expand Down
21 changes: 13 additions & 8 deletions src/widget/color_picker/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -687,7 +691,8 @@ where
clipboard,
shell,
viewport,
) {
) == event::Status::Captured
{
return event::Status::Captured;
}

Expand Down
1 change: 0 additions & 1 deletion src/widget/context_drawer/widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down
6 changes: 3 additions & 3 deletions src/widget/dropdown/menu/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,14 +423,14 @@ impl<'a, S: AsRef<str>, Message> Widget<Message, crate::Renderer> 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 {
Expand Down Expand Up @@ -463,7 +463,7 @@ impl<'a, S: AsRef<str>, Message> Widget<Message, crate::Renderer> 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 {
Expand Down
2 changes: 0 additions & 2 deletions src/widget/dropdown/widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,6 @@ impl<'a, S: AsRef<str>, Message: 'a> Widget<Message, crate::Renderer> for Dropdo
self.gap,
self.padding,
self.text_size.unwrap_or(14.0),
self.font,
self.selections,
self.selected,
&self.on_selected,
Expand Down Expand Up @@ -359,7 +358,6 @@ pub fn overlay<'a, S: AsRef<str>, Message: 'a>(
gap: f32,
padding: Padding,
text_size: f32,
font: Option<crate::font::Font>,
selections: &'a [S],
selected_option: Option<usize>,
on_selected: &'a dyn Fn(usize) -> Message,
Expand Down
20 changes: 16 additions & 4 deletions src/widget/icon/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,29 @@ impl Icon {
fn into_element<Message: 'static>(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()
};

let from_svg = |handle| {
Svg::<Renderer>::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()
Expand Down
6 changes: 3 additions & 3 deletions src/widget/segmented_button/horizontal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/widget/segmented_button/model/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ where
self.storage
.0
.entry(TypeId::of::<Data>())
.or_insert_with(SecondaryMap::new)
.or_default()
.insert(id, Box::new(data));
}
}
Expand Down Expand Up @@ -297,7 +297,7 @@ where
}

pub fn indent(&self, id: Entity) -> Option<u16> {
self.indents.get(id).map(|indent| *indent)
self.indents.get(id).copied()
}

pub fn indent_set(&mut self, id: Entity, indent: u16) -> Option<u16> {
Expand Down
2 changes: 1 addition & 1 deletion src/widget/text_input/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub enum State {

impl Default for Cursor {
fn default() -> Self {
Cursor {
Self {
state: State::Index(0),
}
}
Expand Down
Loading

0 comments on commit 7bed1dd

Please sign in to comment.