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

Added segmented button,segmented button style, and example #193

Merged
merged 3 commits into from
Oct 23, 2023
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
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 Down Expand Up @@ -105,6 +107,7 @@ members = [
"examples/spinner",
"examples/context_menu",
"examples/WidgetIDReturn",
"examples/segmented_button",
]

[workspace.dependencies.iced]
Expand Down
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
103 changes: 103 additions & 0 deletions examples/segmented_button/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
use iced::widget::container;
use iced::widget::{column, row, text};
use iced::{Element, Length, Sandbox, Settings};

use iced_aw::native::segmented_button;
use segmented_button::SegmentedButton;

pub fn main() -> iced::Result {
Example::run(Settings::default())
}

#[derive(Default)]
struct Example {
selected_radio: Option<Choice>,
}

#[derive(Debug, Clone, Copy)]
enum Message {
RadioSelected(Choice),
}

impl Sandbox for Example {
type Message = Message;

fn new() -> Self {
Self {
selected_radio: Some(Choice::A),
}
}

fn title(&self) -> String {
String::from("Radio - Iced")
}

fn update(&mut self, message: Message) {
match message {
Message::RadioSelected(value) => {
self.selected_radio = Some(value);
}
}
}

fn view(&self) -> Element<Message> {
// let selected_radio = Some(Choice::A);

// i added a row just to demonstrate that anything can be used as a child,
//in this case instead of A B C you might add icons
let a = SegmentedButton::new(
row!(text("HEAVY "), "A"),
Choice::A,
self.selected_radio,
Message::RadioSelected,
);

let b = SegmentedButton::new(
row!(text("MEDIUM "), "B"),
Choice::B,
self.selected_radio,
Message::RadioSelected,
);

let c = SegmentedButton::new(
row!(text("LIGHT "), "C"),
Choice::C,
self.selected_radio,
Message::RadioSelected,
);
let content = column![
row![a, b, c],
text(self.selected_radio.unwrap().to_string())
]
.align_items(iced::Alignment::Center);

container(content)
.width(Length::Fill)
.height(Length::Fill)
.center_x()
.center_y()
.into()
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Choice {
#[default]
A,
B,
C,
}

impl std::fmt::Display for Choice {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}",
match self {
Choice::A => "A",
Choice::B => "B",
Choice::C => "C",
}
)
}
}
7 changes: 7 additions & 0 deletions src/native/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,10 @@ pub mod context_menu;
/// A context menu
pub type ContextMenu<'a, Overlay, Message, Renderer> =
context_menu::ContextMenu<'a, Overlay, Message, Renderer>;

#[cfg(feature = "segmented_button")]
pub mod segmented_button;
#[cfg(feature = "segmented_button")]
/// A badge for color highlighting small information.
pub type SegmentedButton<'a, Message, Renderer> =
segmented_button::SegmentedButton<'a, Message, Renderer>;
Loading