egui_macview/controls/segmented/
item.rs1use crate::symbols::MacSymbol;
4use egui::{Color32, Painter, Rect};
5use std::borrow::Cow;
6use std::sync::Arc;
7
8pub type SegmentIconPainter<'a> = Arc<dyn Fn(&Painter, Rect, Color32) + 'a>;
10
11pub type SegmentIconFn = fn(&Painter, Rect, Color32);
13
14#[derive(Clone)]
16pub enum SegmentContent<'a> {
17 Text(Cow<'a, str>),
19 Symbol(MacSymbol),
21 IconFn(SegmentIconFn),
23 Custom(SegmentIconPainter<'a>),
25}
26
27#[derive(Clone)]
29pub struct SegmentItem<'a> {
30 pub(crate) content: SegmentContent<'a>,
31 pub(crate) width: Option<f32>,
32 pub(crate) enabled: bool,
33 pub(crate) has_menu: bool,
34 pub(crate) shows_menu_indicator: bool,
35 pub(crate) tooltip: Option<Cow<'a, str>>,
36}
37
38impl<'a> SegmentItem<'a> {
39 const fn with_content(content: SegmentContent<'a>) -> Self {
40 Self {
41 content,
42 width: None,
43 enabled: true,
44 has_menu: false,
45 shows_menu_indicator: false,
46 tooltip: None,
47 }
48 }
49
50 pub(crate) fn accessible_cow(&self) -> Option<Cow<'a, str>> {
51 match &self.content {
52 SegmentContent::Text(s) if !s.is_empty() => Some(s.clone()),
53 _ => self.tooltip.clone(),
54 }
55 }
56
57 pub fn new(label: impl Into<Cow<'a, str>>) -> Self {
59 Self::with_content(SegmentContent::Text(label.into()))
60 }
61
62 pub const fn from_symbol(symbol: MacSymbol) -> Self {
64 Self::with_content(SegmentContent::Symbol(symbol))
65 }
66
67 pub const fn from_icon_fn(painter: SegmentIconFn) -> Self {
69 Self::with_content(SegmentContent::IconFn(painter))
70 }
71
72 pub fn from_icon(painter: impl Fn(&Painter, Rect, Color32) + 'a) -> Self {
74 Self::with_content(SegmentContent::Custom(Arc::new(painter)))
75 }
76
77 pub const fn with_width(mut self, width: f32) -> Self {
79 self.width = Some(width);
80 self
81 }
82
83 pub const fn enabled(mut self, enabled: bool) -> Self {
85 self.enabled = enabled;
86 self
87 }
88
89 pub const fn with_menu(mut self, has_menu: bool) -> Self {
91 self.has_menu = has_menu;
92 self
93 }
94
95 pub const fn with_shows_menu_indicator(mut self, shows: bool) -> Self {
97 self.shows_menu_indicator = shows;
98 self
99 }
100
101 pub const fn with_submenu(mut self, enabled: bool) -> Self {
103 self.has_menu = enabled;
104 self.shows_menu_indicator = enabled;
105 self
106 }
107
108 pub fn with_tooltip(mut self, tooltip: impl Into<Cow<'a, str>>) -> Self {
110 self.tooltip = Some(tooltip.into());
111 self
112 }
113}
114
115impl<'a> From<&'a str> for SegmentItem<'a> {
116 fn from(s: &'a str) -> Self {
117 Self::new(s)
118 }
119}
120
121impl<'a> From<MacSymbol> for SegmentItem<'a> {
122 fn from(sym: MacSymbol) -> Self {
123 Self::from_symbol(sym)
124 }
125}
126
127#[derive(Debug, Clone, Copy, PartialEq, Eq)]
129pub enum SegmentMenuTrigger {
130 PressAndHold,
132 PullDown,
134 SecondaryClick,
136 Keyboard,
138}
139
140#[derive(Debug, Clone, Copy, PartialEq)]
142pub struct SegmentEvent {
143 pub index: usize,
145 pub menu_trigger: Option<SegmentMenuTrigger>,
147 pub rect: Rect,
149}
150
151impl SegmentEvent {
152 #[inline]
154 pub const fn opens_menu(&self) -> bool {
155 self.menu_trigger.is_some()
156 }
157}
158
159pub type MacSegmentItem<'a> = SegmentItem<'a>;
161
162pub type MacSegmentEvent = SegmentEvent;