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

Drop down widget #205

Merged
merged 4 commits into from
Feb 17, 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ pkg/
.idea
*.lock
.DS_Store
.vscode/settings.json

Empty file removed .vscode/settings.json
Empty file.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ spinner = []
context_menu = []
segmented_button = []
slide_bar = []
drop_down = []

default = [
"badge",
Expand All @@ -60,6 +61,7 @@ default = [
"spinner",
"cupertino",
"segmented_button",
"drop_down",
#"menu",
]

Expand Down Expand Up @@ -108,6 +110,7 @@ members = [
"examples/cupertino/cupertino_switch",
"examples/WidgetIDReturn",
"examples/segmented_button",
"examples/drop_down",
#"examples/menu",
]

Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,13 @@ Please take a look into our examples on how to use slidebars.

Enable this widget with the feature `slide_bar`.

### Context Menu

See the example [here](./examples/context_menu/src/main.rs)

### Drop Down Menu

See the example [here](./examples/drop_down/src/main.rs)

## Quickstart features

Expand Down
3 changes: 1 addition & 2 deletions examples/context_menu/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
[package]
name = "context_menu"
version = "0.1.0"
authors = ["wiiznokes <wiiznokes@gmail.com>"]
authors = ["wiiznokes <wiiznokes2@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 = [
Expand Down
12 changes: 12 additions & 0 deletions examples/drop_down/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "drop_down"
version = "0.1.0"
authors = ["wiiznokes <[email protected]>"]
edition = "2021"


[dependencies]
iced_aw = { workspace = true, features = [
"drop_down",
] }
iced.workspace = true
101 changes: 101 additions & 0 deletions examples/drop_down/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
use std::fmt::Display;

use iced::{
widget::{Button, Column, Row, Text},
Element, Length, Sandbox, Settings,
};

use iced_aw::{drop_down, DropDown};

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

#[derive(Clone, Debug, Default)]
enum Choice {
#[default]
Choice1,
Choice2,
Choice3,
Choice4,
}

const CHOICES: [Choice; 4] = [
Choice::Choice1,
Choice::Choice2,
Choice::Choice3,
Choice::Choice4,
];

#[derive(Clone, Debug)]
enum Message {
Select(Choice),
Dismiss,
Expand,
}

#[derive(Default)]
struct DropDownExample {
selected: Choice,
expanded: bool,
}

impl Sandbox for DropDownExample {
type Message = Message;

fn new() -> Self {
Self::default()
}

fn title(&self) -> String {
String::from("ContextMenu example")
}

fn update(&mut self, message: Self::Message) {
match message {
Message::Select(choice) => {
self.selected = choice;
self.expanded = false;
}
Message::Dismiss => self.expanded = false,
Message::Expand => self.expanded = !self.expanded,
}
}

fn view(&self) -> Element<'_, Self::Message> {
let underlay = Row::new()
.push(Text::new(format!("Selected: {}", self.selected)))
.push(Button::new(Text::new("expand")).on_press(Message::Expand));

let overlay = Column::with_children(CHOICES.map(|choice| {
Row::new()
.push(Text::new(choice.to_string()))
.push(Button::new(Text::new("choose")).on_press(Message::Select(choice)))
.into()
}));

let drop_down = DropDown::new(underlay, overlay, self.expanded)
.width(Length::Fill)
.on_dismiss(Message::Dismiss)
.alignment(drop_down::Alignment::Bottom);

Column::new()
.padding(20)
.width(Length::Fill)
.height(Length::Fill)
.align_items(iced::Alignment::Center)
.push(drop_down)
.into()
}
}

impl Display for Choice {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Choice::Choice1 => write!(f, "1"),
Choice::Choice2 => write!(f, "2"),
Choice::Choice3 => write!(f, "3"),
Choice::Choice4 => write!(f, "4"),
}
}
}
26 changes: 26 additions & 0 deletions src/core/alignment.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//! Alignment enum, similar to an anchor

/// ```text
/// +-----------+-----------+-----------+
/// | TopStart | Top | TopEnd |
/// +-----------+-----------+-----------+
/// | Start | | End |
/// +-----------+-----------+-----------+
/// |BottomStart| Bottom | BottomEnd |
/// +-----------+-----------+-----------+
/// ```
#[allow(missing_docs)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Alignment {
TopStart,
Top,
TopEnd,

End,

BottomEnd,
Bottom,
BottomStart,

Start,
}
6 changes: 6 additions & 0 deletions src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,9 @@ pub mod renderer;

#[cfg(feature = "time_picker")]
pub mod time;

#[cfg(feature = "drop_down")]
pub mod offset;

#[cfg(feature = "drop_down")]
pub mod alignment;
47 changes: 47 additions & 0 deletions src/core/offset.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//! Offset struct

use iced::Point;

/// Represents an offset in a two-dimensional space.
#[derive(Copy, Clone, Debug)]
pub struct Offset {
/// Offset on the x-axis
pub x: f32,
/// Offset on the y-axis
pub y: f32,
}

impl Offset {
/// Construct a new [`Offset`]
#[must_use]
pub fn new(x: f32, y: f32) -> Self {
Self { x, y }
}
}

impl From<f32> for Offset {
fn from(float: f32) -> Self {
Self { x: float, y: float }
}
}

impl From<[f32; 2]> for Offset {
fn from(array: [f32; 2]) -> Self {
Self {
x: array[0],
y: array[1],
}
}
}

impl From<Offset> for Point {
fn from(offset: Offset) -> Self {
Self::new(offset.x, offset.y)
}
}

impl From<&Offset> for Point {
fn from(offset: &Offset) -> Self {
Self::new(offset.x, offset.y)
}
}
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@ mod platform {
pub use {
crate::native::context_menu, crate::style::ContextMenuStyle, context_menu::ContextMenu,
};

#[doc(no_inline)]
#[cfg(feature = "drop_down")]
pub use {crate::native::drop_down, drop_down::DropDown};
}

#[doc(no_inline)]
Expand Down
Loading
Loading