summaryrefslogtreecommitdiffstats
path: root/core
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2021-07-12 21:44:01 +0200
committerLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2021-07-12 21:44:01 +0200
commit735cfb790813c44852612400e31c0190b9c641a6 (patch)
tree4b33019dc01fd8afbe3d79431660788961262dcd /core
parent1428e9180ae9f4edbf22514bb74c5c7e9df9c712 (diff)
downloadiced-735cfb790813c44852612400e31c0190b9c641a6.tar.gz
iced-735cfb790813c44852612400e31c0190b9c641a6.tar.bz2
iced-735cfb790813c44852612400e31c0190b9c641a6.zip
Move `menu` module from `iced_native` to `iced_core`
Diffstat (limited to 'core')
-rw-r--r--core/src/lib.rs2
-rw-r--r--core/src/menu.rs84
2 files changed, 86 insertions, 0 deletions
diff --git a/core/src/lib.rs b/core/src/lib.rs
index 6453d599..c4288158 100644
--- a/core/src/lib.rs
+++ b/core/src/lib.rs
@@ -15,6 +15,7 @@
#![forbid(unsafe_code)]
#![forbid(rust_2018_idioms)]
pub mod keyboard;
+pub mod menu;
pub mod mouse;
mod align;
@@ -33,6 +34,7 @@ pub use background::Background;
pub use color::Color;
pub use font::Font;
pub use length::Length;
+pub use menu::Menu;
pub use padding::Padding;
pub use point::Point;
pub use rectangle::Rectangle;
diff --git a/core/src/menu.rs b/core/src/menu.rs
new file mode 100644
index 00000000..fe770216
--- /dev/null
+++ b/core/src/menu.rs
@@ -0,0 +1,84 @@
+//! Build menus for your application.
+use crate::keyboard::Hotkey;
+
+/// Menu representation.
+///
+/// This can be used by `shell` implementations to create a menu.
+#[derive(Debug, Clone, PartialEq)]
+pub struct Menu<Message> {
+ entries: Vec<Entry<Message>>,
+}
+
+impl<Message> Menu<Message> {
+ /// Creates an empty [`Menu`].
+ pub fn new() -> Self {
+ Self::with_entries(Vec::new())
+ }
+
+ /// Creates a new [`Menu`] with the given entries.
+ pub fn with_entries(entries: Vec<Entry<Message>>) -> Self {
+ Self { entries }
+ }
+
+ /// Adds an [`Entry`] to the [`Menu`].
+ pub fn push(mut self, entry: Entry<Message>) -> Self {
+ self.entries.push(entry);
+ self
+ }
+
+ /// Returns a [`MenuEntry`] iterator.
+ pub fn iter(self) -> impl Iterator<Item = Entry<Message>> {
+ self.entries.into_iter()
+ }
+}
+
+/// Represents one of the possible entries used to build a [`Menu`].
+#[derive(Debug, Clone, PartialEq)]
+pub enum Entry<Message> {
+ /// Item for a [`Menu`]
+ Item {
+ /// The title of the item
+ content: String,
+ /// The [`Hotkey`] to activate the item, if any
+ hotkey: Option<Hotkey>,
+ /// The message generated when the item is activated
+ on_activation: Message,
+ },
+ /// Dropdown for a [`Menu`]
+ Dropdown {
+ /// Title of the dropdown
+ content: String,
+ /// The submenu of the dropdown
+ submenu: Menu<Message>,
+ },
+ /// Separator for a [`Menu`]
+ Separator,
+}
+
+impl<Message> Entry<Message> {
+ /// Creates an [`Entry::Item`].
+ pub fn item<S: Into<String>>(
+ content: S,
+ hotkey: impl Into<Option<Hotkey>>,
+ on_activation: Message,
+ ) -> Self {
+ let content = content.into();
+ let hotkey = hotkey.into();
+
+ Entry::Item {
+ content,
+ hotkey,
+ on_activation,
+ }
+ }
+
+ /// Creates an [`Entry::Dropdown`].
+ pub fn dropdown<S: Into<String>>(
+ content: S,
+ submenu: Menu<Message>,
+ ) -> Self {
+ let content = content.into();
+
+ Entry::Dropdown { content, submenu }
+ }
+}