chore: apply cargo fmt

This commit is contained in:
Victoria Brekenfeld 2023-01-03 21:35:35 +01:00 committed by Michael Murphy
parent cb2b0f7b9a
commit 75687acf2f
22 changed files with 373 additions and 285 deletions

View file

@ -1,5 +1,5 @@
use iced::Size;
use iced::widget::Container;
use iced::Size;
use iced_native::alignment;
use iced_native::event::{self, Event};
use iced_native::layout;
@ -13,7 +13,7 @@ pub use iced_style::container::{Appearance, StyleSheet};
pub fn aspect_ratio_container<'a, Message: 'static, T>(
content: T,
ratio: f32
ratio: f32,
) -> AspectRatio<'a, Message, crate::Renderer>
where
T: Into<Element<'a, Message, crate::Renderer>>,
@ -40,13 +40,16 @@ where
Renderer::Theme: StyleSheet,
{
fn constrain_limits(&self, size: Size) -> Size {
let Size { mut width, mut height } = size;
let Size {
mut width,
mut height,
} = size;
if size.width / size.height > self.ratio {
width = self.ratio * height;
} else {
height = width / self.ratio;
}
Size { width, height }
Size { width, height }
}
}
@ -137,8 +140,7 @@ where
}
}
impl<'a, Message, Renderer> Widget<Message, Renderer>
for AspectRatio<'a, Message, Renderer>
impl<'a, Message, Renderer> Widget<Message, Renderer> for AspectRatio<'a, Message, Renderer>
where
Renderer: iced_native::Renderer,
Renderer::Theme: StyleSheet,
@ -160,7 +162,10 @@ where
}
fn layout(&self, renderer: &Renderer, limits: &layout::Limits) -> layout::Node {
let custom_limits = layout::Limits::new(self.constrain_limits(limits.min()), self.constrain_limits(limits.max()));
let custom_limits = layout::Limits::new(
self.constrain_limits(limits.min()),
self.constrain_limits(limits.max()),
);
self.container.layout(renderer, &custom_limits)
}
@ -239,9 +244,7 @@ where
Renderer: 'a + iced_native::Renderer,
Renderer::Theme: StyleSheet,
{
fn from(
column: AspectRatio<'a, Message, Renderer>,
) -> Element<'a, Message, Renderer> {
fn from(column: AspectRatio<'a, Message, Renderer>) -> Element<'a, Message, Renderer> {
Element::new(column)
}
}

View file

@ -7,7 +7,10 @@ use iced::widget;
/// A button widget with COSMIC styling
#[must_use]
pub const fn button<Message>(style: theme::Button) -> Button<Message> {
Button { style, message: None }
Button {
style,
message: None,
}
}
/// A button widget with COSMIC styling
@ -25,7 +28,12 @@ impl<Message: 'static> Button<Message> {
}
/// A button with an icon.
pub fn icon(self, style: theme::Svg, icon: &str, size: u16) -> widget::Button<Message, Renderer> {
pub fn icon(
self,
style: theme::Svg,
icon: &str,
size: u16,
) -> widget::Button<Message, Renderer> {
self.custom(vec![super::icon(icon, size).style(style).into()])
}
@ -47,4 +55,3 @@ impl<Message: 'static> Button<Message> {
}
}
}

View file

@ -1,10 +1,10 @@
// Copyright 2022 System76 <info@system76.com>
// SPDX-License-Identifier: MPL-2.0
use crate::{theme, Element};
use apply::Apply;
use derive_setters::Setters;
use iced::{self, widget, Length};
use crate::{theme, Element};
#[must_use]
pub fn header_bar<'a, Message>() -> HeaderBar<'a, Message> {
@ -36,7 +36,7 @@ pub struct HeaderBar<'a, Message> {
#[setters(strip_option)]
center: Option<Element<'a, Message>>,
#[setters(strip_option)]
end: Option<Element<'a, Message>>
end: Option<Element<'a, Message>>,
}
impl<'a, Message: Clone + 'static> HeaderBar<'a, Message> {
@ -45,11 +45,17 @@ impl<'a, Message: Clone + 'static> HeaderBar<'a, Message> {
let mut packed: Vec<Element<Message>> = Vec::with_capacity(4);
if let Some(start) = self.start.take() {
packed.push(widget::container(start).align_x(iced::alignment::Horizontal::Left).into());
packed.push(
widget::container(start)
.align_x(iced::alignment::Horizontal::Left)
.into(),
);
}
packed.push(if let Some(center) = self.center.take() {
widget::container(center).align_x(iced::alignment::Horizontal::Center).into()
widget::container(center)
.align_x(iced::alignment::Horizontal::Center)
.into()
} else {
self.title_widget()
});

View file

@ -3,14 +3,16 @@
//! Lazily-generated SVG icon widget for Iced.
use crate::{Element, Renderer};
use derive_setters::Setters;
use iced::{
widget::{svg, Image},
Length, ContentFit,
ContentFit, Length,
};
use std::{borrow::Cow, collections::hash_map::DefaultHasher, ffi::OsStr, hash::Hasher, path::Path};
use std::hash::Hash;
use derive_setters::Setters;
use crate::{Element, Renderer};
use std::{
borrow::Cow, collections::hash_map::DefaultHasher, ffi::OsStr, hash::Hasher, path::Path,
};
#[derive(Debug, Hash)]
pub enum IconSource<'a> {
@ -65,7 +67,6 @@ pub struct Icon<'a> {
#[setters(strip_option)]
height: Option<Length>,
force_svg: bool,
}
/// A lazily-generated icon.
@ -79,7 +80,7 @@ pub fn icon<'a>(name: impl Into<IconSource<'a>>, size: u16) -> Icon<'a> {
style: crate::theme::Svg::default(),
theme: Cow::Borrowed("Pop"),
width: None,
force_svg: false
force_svg: false,
}
}
@ -88,8 +89,8 @@ impl<'a> Icon<'a> {
fn into_element<Message: 'static>(self) -> Element<'a, Message> {
if let IconSource::Embedded(mut image) = self.name {
image = image
.width(self.width.unwrap_or(Length::Units(self.size)))
.height(self.height.unwrap_or(Length::Units(self.size)));
.width(self.width.unwrap_or(Length::Units(self.size)))
.height(self.height.unwrap_or(Length::Units(self.size)));
if let Some(content_fit) = self.content_fit {
image = image.content_fit(content_fit);
}
@ -115,19 +116,23 @@ impl<'a> Icon<'a> {
.find()
} else {
icon
}.map(Cow::from)
},
}
.map(Cow::from)
}
IconSource::Embedded(_) => unimplemented!(),
};
let is_svg = self.force_svg || icon.as_ref().map_or(true, |path| path.extension() == Some(OsStr::new("svg")));
let is_svg = self.force_svg
|| icon
.as_ref()
.map_or(true, |path| path.extension() == Some(OsStr::new("svg")));
if is_svg {
let handle = if let Some(path) = icon {
svg::Handle::from_path(path)
} else {
eprintln!("icon '{:?}' size {} not found", &self.name, self.size);
svg::Handle::from_memory(Vec::new())
svg::Handle::from_memory(Vec::new())
};
let mut widget = svg::Svg::<Renderer>::new(handle)
@ -150,7 +155,8 @@ impl<'a> Icon<'a> {
}
image.into()
}
}).into()
})
.into()
}
}

View file

@ -1,9 +1,9 @@
// Copyright 2022 System76 <info@system76.com>
// SPDX-License-Identifier: MPL-2.0
use apply::Apply;
use crate::{Element, theme};
use crate::widget::horizontal_rule;
use crate::{theme, Element};
use apply::Apply;
use iced::{Background, Color};
#[must_use]
@ -17,7 +17,9 @@ pub struct ListColumn<'a, Message> {
impl<'a, Message: 'static> Default for ListColumn<'a, Message> {
fn default() -> Self {
Self { children: Vec::with_capacity(4) }
Self {
children: Vec::with_capacity(4),
}
}
}

View file

@ -4,5 +4,5 @@
pub mod column;
// mod item;
pub use self::column::{ListColumn, list_column};
pub use self::column::{list_column, ListColumn};
// pub use self::item::{ListItem, list_item};

View file

@ -5,16 +5,16 @@ mod button;
pub use button::*;
mod header_bar;
pub use header_bar::{HeaderBar, header_bar};
pub use header_bar::{header_bar, HeaderBar};
mod icon;
pub use self::icon::{Icon, icon, IconSource};
pub use self::icon::{icon, Icon, IconSource};
pub mod list;
pub use self::list::*;
pub mod nav_button;
pub use self::nav_button::{NavButton, nav_button};
pub use self::nav_button::{nav_button, NavButton};
pub mod navigation;
pub use navigation::*;
@ -24,10 +24,8 @@ pub use toggler::toggler;
pub mod segmented_button;
pub use segmented_button::{
HorizontalSegmentedButton,
horizontal_segmented_button, vertical_segmented_button, HorizontalSegmentedButton,
VerticalSegmentedButton,
horizontal_segmented_button,
vertical_segmented_button
};
pub mod settings;
@ -39,8 +37,8 @@ pub mod separator;
pub use separator::{horizontal_rule, vertical_rule};
pub mod spin_button;
pub use spin_button::{SpinButton, spin_button};
pub use spin_button::{spin_button, SpinButton};
pub mod rectangle_tracker;
pub mod aspect_ratio;
pub mod aspect_ratio;

View file

@ -1,10 +1,10 @@
// Copyright 2022 System76 <info@system76.com>
// SPDX-License-Identifier: MPL-2.0
use crate::{theme, Element};
use apply::Apply;
use derive_setters::Setters;
use iced::{alignment::Vertical, Length};
use crate::{Element, theme};
#[derive(Setters)]
pub struct NavButton<'a, Message> {
@ -54,9 +54,10 @@ impl<'a, Message: 'static + Clone> From<NavButton<'a, Message>> for Element<'a,
widget = widget.on_press(message);
}
widget.apply(iced::widget::container)
widget
.apply(iced::widget::container)
.center_y()
.height(Length::Fill)
.into()
}
}
}

View file

@ -174,35 +174,43 @@ impl<'a, Message> Component<Message, Renderer> for NavBar<'a, Message> {
let nav_bar: Element<Self::Event, Renderer> =
container(if self.condensed && state.selected_page.is_some() {
row![container(scrollable(column(pages)
row![container(scrollable(
column(pages)
.spacing(10)
.padding(10)
.max_width(200)
.width(Length::Units(200))
.height(Length::Shrink)))
.height(Length::Fill)
.style(theme::Container::Custom(nav_bar_pages_style))]
.height(Length::Shrink)
))
.height(Length::Fill)
.style(theme::Container::Custom(nav_bar_pages_style))]
} else if !state.section_active || self.condensed && state.selected_page.is_none() {
row![scrollable(column(sections)
.spacing(10)
.padding(10)
.max_width(100)
.align_items(Alignment::Center)
.height(Length::Shrink))]
} else {
row![
scrollable(column(sections)
row![scrollable(
column(sections)
.spacing(10)
.padding(10)
.max_width(100)
.align_items(Alignment::Center)
.height(Length::Shrink)),
container(scrollable(column(pages)
.spacing(10)
.padding(10)
.max_width(200)
.width(Length::Units(200))
.height(Length::Shrink)))
.height(Length::Shrink)
)]
} else {
row![
scrollable(
column(sections)
.spacing(10)
.padding(10)
.max_width(100)
.align_items(Alignment::Center)
.height(Length::Shrink)
),
container(scrollable(
column(pages)
.spacing(10)
.padding(10)
.max_width(200)
.width(Length::Units(200))
.height(Length::Shrink)
))
.height(Length::Fill)
.style(theme::Container::Custom(nav_bar_pages_style)),
]
@ -217,9 +225,7 @@ impl<'a, Message> Component<Message, Renderer> for NavBar<'a, Message> {
}
}
impl<'a, Message: 'static> From<NavBar<'a, Message>>
for Element<'a, Message, Renderer>
{
impl<'a, Message: 'static> From<NavBar<'a, Message>> for Element<'a, Message, Renderer> {
fn from(nav_bar: NavBar<'a, Message>) -> Self {
iced_lazy::component(nav_bar)
}

View file

@ -5,7 +5,7 @@ use iced::{
},
subscription, Rectangle,
};
use std::{fmt::Debug, hash::Hash, collections::HashMap};
use std::{collections::HashMap, fmt::Debug, hash::Hash};
use super::RectangleTracker;
@ -38,11 +38,14 @@ async fn start_listening<I: Copy, R: 'static + Hash + Copy + Send + Sync + Debug
)
}
State::Waiting(mut rx, mut map) => match rx.next().await {
Some(u) =>
{
Some(u) => {
if let Some(prev) = map.get(&u.0) {
let new = u.1;
if prev.width != new.width || prev.height != new.height || prev.x != new.x || prev.y != new.y {
if prev.width != new.width
|| prev.height != new.height
|| prev.x != new.x
|| prev.y != new.y
{
map.insert(u.0, new);
return (
Some((id, RectangleUpdate::Rectangle(u))),
@ -57,8 +60,7 @@ async fn start_listening<I: Copy, R: 'static + Hash + Copy + Send + Sync + Debug
);
}
(None, State::Waiting(rx, map))
},
}
None => (None, State::Finished),
},
State::Finished => iced::futures::future::pending().await,

View file

@ -4,8 +4,10 @@
use crate::{Element, Renderer};
use iced::widget;
pub fn scrollable<'a, Message>(element: impl Into<Element<'a, Message>>) -> widget::Scrollable<'a, Message, Renderer> {
pub fn scrollable<'a, Message>(
element: impl Into<Element<'a, Message>>,
) -> widget::Scrollable<'a, Message, Renderer> {
widget::scrollable(element)
.scrollbar_width(8)
.scroller_width(8)
}
}

View file

@ -23,4 +23,4 @@ fn separator_style(theme: &Theme) -> widget::rule::Appearance {
radius: 0.0,
fill_mode: widget::rule::FillMode::Padded(10),
}
}
}

View file

@ -13,8 +13,5 @@ use iced::widget::{column, Column};
/// A column with a predefined style for creating a settings panel
#[must_use]
pub fn view_column<Message: 'static>(children: Vec<Element<Message>>) -> Column<Message, Renderer> {
column(children)
.spacing(24)
.padding([0, 24])
.max_width(678)
column(children).spacing(24).padding([0, 24]).max_width(678)
}