Skip to content

Commit

Permalink
Merge branch 'iced-rs:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
Redhawk18 authored Nov 20, 2023
2 parents 78cc6fc + 99e5bc1 commit 61adb22
Show file tree
Hide file tree
Showing 20 changed files with 1,405 additions and 430 deletions.
6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ date_picker = ["chrono", "once_cell", "icon_text"]
color_picker = ["icon_text", "iced_widget/canvas"]
cupertino = ["iced_widget/canvas", "time"]
floating_element = []
grid = []
grid = ["itertools"]
glow = [] # TODO
icon_text = ["icons"]
icons = []
Expand All @@ -37,6 +37,7 @@ menu = []
quad = []
spinner = []
context_menu = []
segmented_button = []

default = [
"badge",
Expand All @@ -59,6 +60,7 @@ default = [
"context_menu",
"spinner",
"cupertino",
"segmented_button",
]

[dependencies]
Expand All @@ -67,6 +69,7 @@ num-traits = { version = "0.2.16", optional = true }
time = { version = "0.3.23", features = ["local-offset"], optional = true }
chrono = { version = "0.4.26", optional = true }
once_cell = { version = "1.18.0", optional = true }
itertools = { version = "0.11.0", optional = true }


[dependencies.iced_widget]
Expand Down Expand Up @@ -105,6 +108,7 @@ members = [
"examples/spinner",
"examples/context_menu",
"examples/WidgetIDReturn",
"examples/segmented_button",
]

[workspace.dependencies.iced]
Expand Down
6 changes: 2 additions & 4 deletions examples/grid/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
[package]
name = "grid"
version = "0.1.0"
authors = ["daxpedda <[email protected]>", "Luni-4 <[email protected]>"]
authors = ["Alexander van Saase <avsaase [at] gmail.com>"]
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
iced_aw = { workspace = true, features = ["grid"] }
iced.workspace = true
iced.workspace = true
191 changes: 145 additions & 46 deletions examples/grid/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,79 +1,178 @@
use iced::widget::{checkbox, container, pick_list, row, slider};
use iced::Padding;
use iced::{
theme,
widget::{Button, Column, Container, Scrollable, Text},
Alignment, Color, Element, Length, Sandbox, Settings,
alignment::{Horizontal, Vertical},
Color, Element, Length, Sandbox, Settings,
};
use iced_aw::{grid, grid_row};

use iced_aw::grid;

// Number of columns for the grid
const COLUMNS: usize = 2;

fn main() -> iced::Result {
GridExample::run(Settings::default())
struct App {
horizontal_alignment: Horizontal,
vertical_alignment: Vertical,
column_spacing: f32,
row_spacing: f32,
fill_width: bool,
fill_height: bool,
padding: f32,
debug_layout: bool,
}

#[derive(Debug, Clone)]
enum Message {
AddElement,
}

struct GridExample {
element_index: usize,
HorizontalAlignment(Horizontal),
VerticalAlignment(Vertical),
ColumnSpacing(f32),
RowSpacing(f32),
FillWidth(bool),
FillHeight(bool),
Padding(f32),
DebugToggled(bool),
}

impl Sandbox for GridExample {
impl Sandbox for App {
type Message = Message;

fn new() -> Self {
GridExample { element_index: 0 }
Self {
horizontal_alignment: Horizontal::Left,
vertical_alignment: Vertical::Center,
column_spacing: 5.0,
row_spacing: 5.0,
fill_width: false,
fill_height: false,
padding: 0.0,
debug_layout: false,
}
}

fn title(&self) -> String {
String::from("Grid example")
"Iced Grid widget example".into()
}

fn update(&mut self, message: self::Message) {
fn update(&mut self, message: Self::Message) {
match message {
Message::AddElement => {
self.element_index += 1;
}
Message::HorizontalAlignment(align) => self.horizontal_alignment = align,
Message::VerticalAlignment(align) => self.vertical_alignment = align,
Message::ColumnSpacing(spacing) => self.column_spacing = spacing,
Message::RowSpacing(spacing) => self.row_spacing = spacing,
Message::FillWidth(fill) => self.fill_width = fill,
Message::FillHeight(fill) => self.fill_height = fill,
Message::Padding(value) => self.padding = value,
Message::DebugToggled(enabled) => self.debug_layout = enabled,
}
}

fn view(&self) -> Element<'_, self::Message> {
// Creates a grid with two columns
let mut grid = grid!(
Text::new("Column 1").style(theme::Text::Color(Color::from_rgb8(255, 0, 0))),
Text::new("Column 2").style(theme::Text::Color(Color::from_rgb8(255, 0, 0))),
)
.strategy(iced_aw::Strategy::Columns(2));
fn view(&self) -> iced::Element<'_, Self::Message> {
let horizontal_align_pick = pick_list(
HORIZONTAL_ALIGNMENTS
.iter()
.map(horizontal_align_to_string)
.collect::<Vec<_>>(),
Some(horizontal_align_to_string(&self.horizontal_alignment)),
|selected| Message::HorizontalAlignment(string_to_horizontal_align(&selected)),
);

// Add elements to the grid
for i in 0..self.element_index {
grid.insert(Text::new(format!("Row {} Element {}", (i / COLUMNS), i)));
}
let vertical_align_pick = pick_list(
VERTICAL_ALIGNMENTS
.iter()
.map(vertical_alignment_to_string)
.collect::<Vec<_>>(),
Some(vertical_alignment_to_string(&self.vertical_alignment)),
|selected| Message::VerticalAlignment(string_to_vertical_align(&selected)),
);

let add_button: Element<'_, Message> = Button::new(Text::new("Add element"))
.on_press(Message::AddElement)
.into();
let row_spacing_slider =
slider(0.0..=100.0, self.row_spacing, Message::RowSpacing).width(Length::Fill);
let col_spacing_slider =
slider(0.0..=100.0, self.column_spacing, Message::ColumnSpacing).width(Length::Fill);

let column: Element<'_, Message> = Column::new()
.spacing(15)
.max_width(600)
.align_items(Alignment::Center)
.push(grid)
.push(add_button)
.into();
let debug_mode_check = checkbox("", self.debug_layout, Message::DebugToggled);

let content = Scrollable::new(column);
let fill_checkboxes = row![
checkbox("Width", self.fill_width, Message::FillWidth),
checkbox("Height", self.fill_height, Message::FillHeight)
]
.spacing(10);

Container::new(content)
let padding_slider =
slider(0.0..=100.0, self.padding, Message::Padding).width(Length::Fixed(400.0));

let mut grid = grid!(
grid_row!("Horizontal alignment", horizontal_align_pick,),
grid_row!("Vertical alignment", vertical_align_pick),
grid_row!("Row spacing", row_spacing_slider),
grid_row!("Column spacing", col_spacing_slider),
grid_row!("Fill space", fill_checkboxes),
grid_row!("Padding", padding_slider),
grid_row!("Debug mode", debug_mode_check)
)
.horizontal_alignment(self.horizontal_alignment)
.vertical_alignment(self.vertical_alignment)
.row_spacing(self.row_spacing)
.column_spacing(self.column_spacing)
.padding(Padding::new(self.padding));

if self.fill_width {
grid = grid.width(Length::Fill);
}
if self.fill_height {
grid = grid.height(Length::Fill);
}

let mut contents = Element::from(grid);
if self.debug_layout {
contents = contents.explain(Color::BLACK);
}
container(contents)
.width(Length::Fill)
.height(Length::Fill)
.padding(10)
.center_x()
.center_y()
.into()
}
}

const HORIZONTAL_ALIGNMENTS: [Horizontal; 3] =
[Horizontal::Left, Horizontal::Center, Horizontal::Right];

const VERTICAL_ALIGNMENTS: [Vertical; 3] = [Vertical::Top, Vertical::Center, Vertical::Bottom];

fn horizontal_align_to_string(alignment: &Horizontal) -> String {
match alignment {
Horizontal::Left => "Left",
Horizontal::Center => "Center",
Horizontal::Right => "Right",
}
.to_string()
}

fn string_to_horizontal_align(input: &str) -> Horizontal {
match input {
"Left" => Horizontal::Left,
"Center" => Horizontal::Center,
"Right" => Horizontal::Right,
_ => panic!(),
}
}

fn vertical_alignment_to_string(alignment: &Vertical) -> String {
match alignment {
Vertical::Top => "Top",
Vertical::Center => "Center",
Vertical::Bottom => "Bottom",
}
.to_string()
}

fn string_to_vertical_align(input: &str) -> Vertical {
match input {
"Top" => Vertical::Top,
"Center" => Vertical::Center,
"Bottom" => Vertical::Bottom,
_ => panic!(),
}
}

fn main() -> iced::Result {
App::run(Settings::default())
}
14 changes: 14 additions & 0 deletions examples/segmented_button/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "segmented_button"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html


[dependencies]
iced_aw = { workspace = true, features = [
"segmented_button",
"icons",
] }
iced.workspace = true
Loading

0 comments on commit 61adb22

Please sign in to comment.