-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #205 from wiiznokes/drop_down
Drop down widget looks good thank you
- Loading branch information
Showing
13 changed files
with
627 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,5 @@ pkg/ | |
.idea | ||
*.lock | ||
.DS_Store | ||
.vscode/settings.json | ||
|
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
[package] | ||
name = "drop_down" | ||
version = "0.1.0" | ||
authors = ["wiiznokes <wiiznokes2@gmail.com>"] | ||
edition = "2021" | ||
|
||
|
||
[dependencies] | ||
iced_aw = { workspace = true, features = [ | ||
"drop_down", | ||
] } | ||
iced.workspace = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.