Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replacing f32 by iced::Padding and iced::Pixels when possible #281

Merged
merged 1 commit into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/time_picker/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ edition = "2021"
iced_aw = { workspace = true, features = [
"time_picker",
] }
iced.workspace = true
iced.workspace = true
8 changes: 4 additions & 4 deletions src/widgets/card.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub use crate::style::{
};

/// The default padding of a [`Card`].
const DEFAULT_PADDING: f32 = 10.0;
const DEFAULT_PADDING: Padding = Padding::new(10.0);

/// A card consisting of a head, body and optional foot.
///
Expand Down Expand Up @@ -101,9 +101,9 @@ where
height: Length::Shrink,
max_width: u32::MAX as f32,
max_height: u32::MAX as f32,
padding_head: DEFAULT_PADDING.into(),
padding_body: DEFAULT_PADDING.into(),
padding_foot: DEFAULT_PADDING.into(),
padding_head: DEFAULT_PADDING,
padding_body: DEFAULT_PADDING,
padding_foot: DEFAULT_PADDING,
close_size: None,
on_close: None,
head: head.into(),
Expand Down
2 changes: 1 addition & 1 deletion src/widgets/grid/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ where
/// Sets the spacing between rows and columns. To set row and column spacing separately, use
/// [`Self::column_spacing()`] and [`Self::row_spacing()`].
#[must_use]
pub fn spacing(mut self, spacing: f32) -> Self {
pub fn spacing(mut self, spacing: impl Into<Pixels>) -> Self {
let spacing: Pixels = spacing.into();
self.row_spacing = spacing;
self.column_spacing = spacing;
Expand Down
4 changes: 2 additions & 2 deletions src/widgets/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#[cfg(feature = "selection_list")]
use crate::style::{Status, StyleFn};
#[allow(unused_imports)]
use iced::{self, advanced::renderer, Color, Element};
use iced::{self, advanced::renderer, Color, Element, Padding};

#[cfg(feature = "number_input")]
use num_traits::bounds::Bounded;
Expand Down Expand Up @@ -357,7 +357,7 @@ pub fn selection_list_with<'a, T, Message, Theme, Renderer>(
options: &'a [T],
on_selected: impl Fn(usize, T) -> Message + 'static,
text_size: f32,
padding: f32,
padding: impl Into<Padding>,
style: impl Fn(&Theme, Status) -> crate::style::selection_list::Style + 'a + Clone,
selected: Option<usize>,
font: iced::Font,
Expand Down
8 changes: 4 additions & 4 deletions src/widgets/menu/flex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use iced::{
layout::{Limits, Node},
renderer, widget,
},
Alignment, Element, Length, Padding, Point, Size,
Alignment, Element, Length, Padding, Pixels, Point, Size,
};

/// The main axis of a flex layout.
Expand Down Expand Up @@ -73,7 +73,7 @@ pub fn resolve<'a, E, T, Message, Theme, Renderer>(
width: Length,
height: Length,
padding: Padding,
spacing: f32,
spacing: Pixels,
align_items: Alignment,
items: &[E],
trees: &mut [T],
Expand All @@ -100,7 +100,7 @@ where
},
};

let mut available = axis.main(limits.max()) - total_spacing;
let mut available = axis.main(limits.max()) - total_spacing.0;

let mut nodes: Vec<Node> = Vec::with_capacity(items.len());
nodes.resize(items.len(), Node::default());
Expand Down Expand Up @@ -218,7 +218,7 @@ where

for (i, node) in nodes.iter_mut().enumerate() {
if i > 0 {
main += spacing;
main += spacing.0;
}

let (x, y) = axis.pack(main, pad.1);
Expand Down
10 changes: 5 additions & 5 deletions src/widgets/menu/menu_bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use iced::{
widget::{tree, Operation, Tree},
Clipboard, Layout, Shell, Widget,
},
alignment, event, Element, Event, Length, Padding, Rectangle, Size,
alignment, event, Element, Event, Length, Padding, Pixels, Rectangle, Size,
};

use super::{common::*, flex, menu_bar_overlay::MenuBarOverlay, menu_tree::*};
Expand All @@ -34,7 +34,7 @@ where
Renderer: renderer::Renderer,
{
roots: Vec<Item<'a, Message, Theme, Renderer>>,
spacing: f32,
spacing: Pixels,
padding: Padding,
width: Length,
height: Length,
Expand All @@ -58,7 +58,7 @@ where

Self {
roots,
spacing: 0.0,
spacing: Pixels::ZERO,
padding: Padding::ZERO,
width: Length::Shrink,
height: Length::Shrink,
Expand All @@ -85,8 +85,8 @@ where
}

/// Sets the spacing of the [`MenuBar`].
pub fn spacing(mut self, spacing: f32) -> Self {
self.spacing = spacing;
pub fn spacing(mut self, spacing: impl Into<Pixels>) -> Self {
self.spacing = spacing.into();
self
}

Expand Down
9 changes: 5 additions & 4 deletions src/widgets/menu/menu_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use super::common::*;
use super::flex;
use iced::advanced::overlay::Group;
use iced::advanced::widget::Operation;
use iced::Pixels;
use iced::{
advanced::{
layout::{Layout, Limits, Node},
Expand Down Expand Up @@ -89,7 +90,7 @@ where
Renderer: renderer::Renderer,
{
pub(super) items: Vec<Item<'a, Message, Theme, Renderer>>,
pub(super) spacing: f32,
pub(super) spacing: Pixels,
pub(super) max_width: f32,
pub(super) width: Length,
pub(super) height: Length,
Expand All @@ -105,7 +106,7 @@ where
pub fn new(items: Vec<Item<'a, Message, Theme, Renderer>>) -> Self {
Self {
items,
spacing: 0.0,
spacing: Pixels::ZERO,
max_width: f32::MAX,
width: Length::Fill,
height: Length::Shrink,
Expand All @@ -127,8 +128,8 @@ where
}

/// Sets the spacing of the [`Menu`].
pub fn spacing(mut self, spacing: f32) -> Self {
self.spacing = spacing;
pub fn spacing(mut self, spacing: impl Into<Pixels>) -> Self {
self.spacing = spacing.into();
self
}

Expand Down
6 changes: 3 additions & 3 deletions src/widgets/number_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub use crate::{
};

/// The default padding
const DEFAULT_PADDING: f32 = 5.0;
const DEFAULT_PADDING: Padding = Padding::new(5.0);

/// A field that can only be filled with numeric type.
///
Expand Down Expand Up @@ -116,7 +116,7 @@ where
F: 'static + Fn(T) -> Message + Copy,
T: 'static,
{
let padding = DEFAULT_PADDING.into();
let padding = DEFAULT_PADDING;

Self {
value,
Expand Down Expand Up @@ -356,7 +356,7 @@ where
.center_x(Length::Shrink)
};

let default_padding = Padding::from(DEFAULT_PADDING);
let default_padding = DEFAULT_PADDING;

let element = if self.padding.top < default_padding.top
|| self.padding.bottom < default_padding.bottom
Expand Down
67 changes: 30 additions & 37 deletions src/widgets/overlay/color_picker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,36 +22,24 @@ use iced::{
Clipboard, Layout, Overlay, Renderer as _, Shell, Text, Widget,
},
alignment::{Horizontal, Vertical},
event,
keyboard,
event, keyboard,
mouse::{self, Cursor},
touch,
widget::{
canvas::{self, LineCap, Path, Stroke},
text, Button, Column, Row,
},
Alignment,
Border,
Color,
Element,
Event,
Length,
Padding,
Point,
Rectangle,
Renderer, // the actual type
Shadow,
Size,
Vector,
Alignment, Border, Color, Element, Event, Length, Padding, Pixels, Point, Rectangle, Renderer,
Shadow, Size, Vector,
};
use std::collections::HashMap;

/// The padding around the elements.
const PADDING: f32 = 10.0;
const PADDING: Padding = Padding::new(10.0);
/// The spacing between the element.
const SPACING: f32 = 15.0;
const SPACING: Pixels = Pixels(15.0);
/// The spacing between the buttons.
const BUTTON_SPACING: f32 = 5.0;
const BUTTON_SPACING: Pixels = Pixels(5.0);

/// The step value of the keyboard change of the sat/value color values.
const SAT_VALUE_STEP: f32 = 0.005;
Expand Down Expand Up @@ -564,7 +552,7 @@ where
};

let limits = Limits::new(Size::ZERO, bounds)
.shrink(Padding::from(PADDING))
.shrink(PADDING)
.width(Length::Fill)
.height(Length::Fill)
.max_width(max_width)
Expand Down Expand Up @@ -603,13 +591,13 @@ where

let (width, height) = if bounds.width > bounds.height {
(
block1_node.size().width + block2_node.size().width + SPACING, // + (2.0 * PADDING as f32),
block1_node.size().width + block2_node.size().width + SPACING.0, // + (2.0 * PADDING as f32),
block2_node.size().height,
)
} else {
(
block2_node.size().width,
block1_node.size().height + block2_node.size().height + SPACING,
block1_node.size().height + block2_node.size().height + SPACING.0,
)
};

Expand Down Expand Up @@ -912,7 +900,7 @@ where
.height(Length::Fill);

let block1_node = Column::<(), Theme, Renderer>::new()
.spacing(PADDING)
.spacing(PADDING.vertical() / 2.) // Average vertical padding
.push(
Row::new()
.width(Length::Fill)
Expand All @@ -925,7 +913,7 @@ where
)
.layout(color_picker.tree, renderer, &block1_limits);

block1_node.move_to(Point::new(bounds.x + PADDING, bounds.y + PADDING))
block1_node.move_to(Point::new(bounds.x + PADDING.left, bounds.y + PADDING.top))
}

/// Defines the layout of the 2. block of the color picker containing the RGBA part, Hex and buttons.
Expand Down Expand Up @@ -957,12 +945,14 @@ where

let mut hex_text_layout = Row::<Message, Theme, Renderer>::new()
.width(Length::Fill)
.height(Length::Fixed(renderer.default_size().0 + 2.0 * PADDING))
.height(Length::Fixed(
renderer.default_size().0 + PADDING.vertical(),
))
.layout(color_picker.tree, renderer, &hex_text_limits);

let block2_limits = block2_limits.shrink(Size::new(
0.0,
cancel_button.bounds().height + hex_text_layout.bounds().height + 2.0 * SPACING,
cancel_button.bounds().height + hex_text_layout.bounds().height + 2.0 * SPACING.0,
));

// RGBA Colors
Expand Down Expand Up @@ -1008,20 +998,23 @@ where
.layout(rgba_tree, renderer, &block2_limits);

let rgba_bounds = rgba_colors.bounds();
rgba_colors = rgba_colors.move_to(Point::new(rgba_bounds.x + PADDING, rgba_bounds.y + PADDING));
rgba_colors = rgba_colors.move_to(Point::new(
rgba_bounds.x + PADDING.left,
rgba_bounds.y + PADDING.top,
));
let rgba_bounds = rgba_colors.bounds();

// Hex text
let hex_bounds = hex_text_layout.bounds();
hex_text_layout = hex_text_layout.move_to(Point::new(
hex_bounds.x + PADDING,
hex_bounds.y + rgba_bounds.height + PADDING + SPACING,
hex_bounds.x + PADDING.left,
hex_bounds.y + rgba_bounds.height + PADDING.top + SPACING.0,
));
let hex_bounds = hex_text_layout.bounds();

// Buttons
let cancel_limits =
block2_limits.max_width(((rgba_bounds.width / 2.0) - BUTTON_SPACING).max(0.0));
block2_limits.max_width(((rgba_bounds.width / 2.0) - BUTTON_SPACING.0).max(0.0));

let mut cancel_button = color_picker.cancel_button.layout(
&mut color_picker.tree.children[0],
Expand All @@ -1030,7 +1023,7 @@ where
);

let submit_limits =
block2_limits.max_width(((rgba_bounds.width / 2.0) - BUTTON_SPACING).max(0.0));
block2_limits.max_width(((rgba_bounds.width / 2.0) - BUTTON_SPACING.0).max(0.0));

let mut submit_button = color_picker.submit_button.layout(
&mut color_picker.tree.children[1],
Expand All @@ -1040,25 +1033,25 @@ where

let cancel_bounds = cancel_button.bounds();
cancel_button = cancel_button.move_to(Point::new(
cancel_bounds.x + PADDING,
cancel_bounds.y + rgba_bounds.height + hex_bounds.height + PADDING + 2.0 * SPACING,
cancel_bounds.x + PADDING.left,
cancel_bounds.y + rgba_bounds.height + hex_bounds.height + PADDING.top + 2.0 * SPACING.0,
));
let cancel_bounds = cancel_button.bounds();

let submit_bounds = submit_button.bounds();
submit_button = submit_button.move_to(Point::new(
submit_bounds.x + rgba_colors.bounds().width - submit_bounds.width + PADDING,
submit_bounds.y + rgba_bounds.height + hex_bounds.height + PADDING + 2.0 * SPACING,
submit_bounds.x + rgba_colors.bounds().width - submit_bounds.width + PADDING.left,
submit_bounds.y + rgba_bounds.height + hex_bounds.height + PADDING.top + 2.0 * SPACING.0,
));

Node::with_children(
Size::new(
rgba_bounds.width + (2.0 * PADDING),
rgba_bounds.width + PADDING.horizontal(),
rgba_bounds.height
+ hex_bounds.height
+ cancel_bounds.height
+ (2.0 * PADDING)
+ (2.0 * SPACING),
+ PADDING.vertical()
+ (2.0 * SPACING.0),
),
vec![rgba_colors, hex_text_layout, cancel_button, submit_button],
)
Expand Down
Loading