Skip to content

Commit

Permalink
updated Context Menu to newest Iced master branch
Browse files Browse the repository at this point in the history
  • Loading branch information
genusistimelord committed Jun 27, 2024
1 parent bb2e27c commit 961c9a9
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 82 deletions.
8 changes: 8 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ default = [
"slide_bar",
"wrap",
"selection_list",
#"quad",
#"context_menu",
"quad",
"context_menu",
"spinner",
"drop_down",
#"menu",
Expand Down Expand Up @@ -89,7 +89,7 @@ members = [
"examples/sliderbar",
"examples/wrap",
"examples/selection_list",
#"examples/context_menu",
"examples/context_menu",
"examples/spinner",
"examples/WidgetIDReturn",
"examples/drop_down",
Expand Down
25 changes: 10 additions & 15 deletions examples/context_menu/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
use iced::{
widget::{column, Button, Container, Row, Text},
Alignment, Element, Sandbox, Settings,
Alignment, Element,
};

use iced_aw::ContextMenu;

fn main() -> iced::Result {
ContextMenuExample::run(Settings::default())
iced::application(
"ContextMenu example",
ContextMenuExample::update,
ContextMenuExample::view,
)
.run()
}

#[derive(Clone, Debug)]
Expand All @@ -23,22 +28,12 @@ struct ContextMenuExample {
last_message: Option<Message>,
}

impl Sandbox for ContextMenuExample {
type Message = Message;

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

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

fn update(&mut self, message: Self::Message) {
impl ContextMenuExample {
fn update(&mut self, message: Message) {
self.last_message = Some(message);
}

fn view(&self) -> Element<'_, Self::Message> {
fn view(&self) -> Element<'_, Message> {
let underlay = Container::new(
Row::new()
.spacing(10)
Expand Down
4 changes: 1 addition & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,7 @@ mod platform {

#[doc(no_inline)]
#[cfg(feature = "context_menu")]
pub use {
crate::style::ContextMenuStyle, crate::widgets::context_menu, context_menu::ContextMenu,
};
pub use {crate::widgets::context_menu, context_menu::ContextMenu};

#[doc(no_inline)]
#[cfg(feature = "drop_down")]
Expand Down
2 changes: 0 additions & 2 deletions src/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,3 @@ pub use menu_bar::MenuBarStyle;

#[cfg(feature = "context_menu")]
pub mod context_menu;
#[cfg(feature = "context_menu")]
pub use context_menu::ContextMenuStyle;
64 changes: 30 additions & 34 deletions src/style/context_menu.rs
Original file line number Diff line number Diff line change
@@ -1,63 +1,59 @@
//! Use a badge for color highlighting important information.
//!
//! *This API requires the following crate features to be activated: badge*
use std::rc::Rc;

use super::{Status, StyleFn};
use iced::{Background, Color, Theme};

/// The appearance of a [`ContextMenu`](crate::widgets::ContextMenu).
/// The style of a [`ContextMenu`](crate::widgets::ContextMenu).
#[derive(Clone, Copy, Debug)]
pub struct Appearance {
pub struct Style {
/// The background of the [`ContextMenu`](crate::widgets::ContextMenu).
///
/// This is used to color the backdrop of the modal.
pub background: Background,
}

impl Default for Appearance {
impl Default for Style {
fn default() -> Self {
Self {
background: Background::Color([0.87, 0.87, 0.87, 0.30].into()),
}
}
}

/// The appearance of a [`ContextMenu`](crate::widgets::ContextMenu).
pub trait StyleSheet {
/// The Catalog of a [`ContextMenu`](crate::widgets::ContextMenu).
pub trait Catalog {
///Style for the trait to use.
type Style: Default + Clone;
/// The normal appearance of a [`ContextMenu`](crate::widgets::ContextMenu).
fn active(&self, style: &Self::Style) -> Appearance;
}
type Class<'a>;

/// The default appearance of a [`ContextMenu`](crate::widgets::ContextMenu).
#[derive(Clone, Default)]
#[allow(missing_docs, clippy::missing_docs_in_private_items)]
pub enum ContextMenuStyle {
#[default]
Default,
Custom(Rc<dyn StyleSheet<Style = Theme>>),
/// The default class produced by the [`Catalog`].
fn default<'a>() -> Self::Class<'a>;

/// The [`Style`] of a class with the given status.
fn style(&self, class: &Self::Class<'_>, status: Status) -> Style;
}

impl ContextMenuStyle {
/// Creates a custom [`ContextMenuStyle`] style variant.
pub fn custom(style_sheet: impl StyleSheet<Style = Theme> + 'static) -> Self {
Self::Custom(Rc::new(style_sheet))
impl Catalog for Theme {
type Class<'a> = StyleFn<'a, Self, Style>;

fn default<'a>() -> Self::Class<'a> {
Box::new(primary)
}
}

impl StyleSheet for Theme {
type Style = ContextMenuStyle;
fn style(&self, class: &Self::Class<'_>, status: Status) -> Style {
class(self, status)
}
}

fn active(&self, _style: &Self::Style) -> Appearance {
let palette = self.extended_palette();
/// The primary theme of a [`ContextMenu`](crate::widgets::ContextMenu).
#[must_use]
pub fn primary(theme: &Theme, _status: Status) -> Style {
let palette = theme.extended_palette();

Appearance {
background: Color {
a: 0f32,
..palette.background.base.color
}
.into(),
}
Style {
background: Background::Color(Color {
a: 0f32,
..palette.background.base.color
}),
}
}
38 changes: 26 additions & 12 deletions src/widgets/context_menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ use iced::{
Element, Event, Length, Point, Rectangle, Vector,
};

pub use crate::style::context_menu::StyleSheet;
pub use crate::style::{
context_menu::{Catalog, Style},
status::{Status, StyleFn},
};

use crate::widgets::overlay::ContextMenuOverlay;

/// A context menu
Expand Down Expand Up @@ -41,22 +45,22 @@ where
Overlay: Fn() -> Element<'a, Message, Theme, Renderer>,
Message: Clone,
Renderer: renderer::Renderer,
Theme: StyleSheet,
Theme: Catalog,
{
/// The underlying element.
underlay: Element<'a, Message, Theme, Renderer>,
/// The content of [`ContextMenuOverlay`].
overlay: Overlay,
/// The style of the [`ContextMenu`].
style: <Theme as StyleSheet>::Style,
class: Theme::Class<'a>,
}

impl<'a, Overlay, Message, Theme, Renderer> ContextMenu<'a, Overlay, Message, Theme, Renderer>
where
Overlay: Fn() -> Element<'a, Message, Theme, Renderer>,
Message: Clone,
Renderer: renderer::Renderer,
Theme: StyleSheet,
Theme: Catalog,
{
/// Creates a new [`ContextMenu`]
///
Expand All @@ -70,14 +74,24 @@ where
ContextMenu {
underlay: underlay.into(),
overlay,
style: <Theme as StyleSheet>::Style::default(),
class: Theme::default(),
}
}

/// Sets the style of the [`ContextMenu`].
#[must_use]
pub fn style(mut self, style: <Theme as StyleSheet>::Style) -> Self {
self.style = style;
pub fn style(mut self, style: impl Fn(&Theme, Status) -> Style + 'a) -> Self
where
Theme::Class<'a>: From<StyleFn<'a, Theme, Style>>,
{
self.class = (Box::new(style) as StyleFn<'a, Theme, Style>).into();
self
}

/// Sets the class of the input of the [`Badge`].
#[must_use]
pub fn class(mut self, class: impl Into<Theme::Class<'a>>) -> Self {
self.class = class.into();
self
}
}
Expand All @@ -88,7 +102,7 @@ where
Content: 'a + Fn() -> Element<'a, Message, Theme, Renderer>,
Message: 'a + Clone,
Renderer: 'a + renderer::Renderer,
Theme: StyleSheet,
Theme: Catalog,
{
fn size(&self) -> iced::Size<Length> {
self.underlay.as_widget().size()
Expand Down Expand Up @@ -130,7 +144,7 @@ where
}

fn children(&self) -> Vec<Tree> {
vec![Tree::new(&self.underlay), Tree::new(&(self.overlay)())]
vec![Tree::new(&self.underlay), Tree::new((self.overlay)())]
}

fn diff(&self, tree: &mut Tree) {
Expand All @@ -142,7 +156,7 @@ where
state: &'b mut Tree,
layout: Layout<'_>,
renderer: &Renderer,
operation: &mut dyn Operation<Message>,
operation: &mut dyn Operation<()>,
) {
let s: &mut State = state.state.downcast_mut();

Expand Down Expand Up @@ -237,7 +251,7 @@ where
position + translation,
&mut state.children[1],
content,
self.style.clone(),
&self.class,
s,
)
.overlay(),
Expand All @@ -251,7 +265,7 @@ where
Content: 'a + Fn() -> Self,
Message: 'a + Clone,
Renderer: 'a + renderer::Renderer,
Theme: 'a + StyleSheet,
Theme: 'a + Catalog,
{
fn from(modal: ContextMenu<'a, Content, Message, Theme, Renderer>) -> Self {
Element::new(modal)
Expand Down
Loading

0 comments on commit 961c9a9

Please sign in to comment.