@@ -144,14 +144,16 @@ pub struct MenuEntry {
144
144
text : String ,
145
145
icon : String ,
146
146
state : EntryState ,
147
+ index : usize ,
147
148
}
148
149
impl MenuEntry {
149
150
#[ allow( clippy:: needless_pass_by_value) ]
150
- fn new ( text : impl ToString , icon : impl ToString , state : EntryState ) -> Self {
151
+ fn new ( text : impl ToString , icon : impl ToString , state : EntryState , index : usize ) -> Self {
151
152
Self {
152
153
text : text. to_string ( ) ,
153
154
icon : icon. to_string ( ) ,
154
155
state,
156
+ index,
155
157
}
156
158
}
157
159
#[ allow( clippy:: needless_pass_by_value) ]
@@ -167,7 +169,8 @@ impl MenuEntry {
167
169
let MenuEntry {
168
170
text,
169
171
icon,
170
- state
172
+ state,
173
+ ..
171
174
} = self ;
172
175
173
176
let text_style = TextStyle :: Button ;
@@ -223,14 +226,14 @@ pub struct SubMenu<'a> {
223
226
}
224
227
impl < ' a > SubMenu < ' a > {
225
228
#[ allow( clippy:: needless_pass_by_value) ]
226
- fn new ( text : impl ToString , parent_state : & ' a mut MenuState ) -> Self {
229
+ fn new ( text : impl ToString , parent_state : & ' a mut MenuState , index : usize ) -> Self {
227
230
Self {
228
- entry : MenuEntry :: new ( text, "⏵" , EntryState :: Active ) ,
231
+ entry : MenuEntry :: new ( text, "⏵" , EntryState :: Active , index ) ,
229
232
parent_state,
230
233
}
231
234
}
232
235
pub fn show ( self , ui : & mut Ui , add_contents : impl FnOnce ( & mut Ui , & mut MenuState ) ) -> Response {
233
- let sub_id = ui. id ( ) . with ( format ! ( "{:?}" , ui . placer . cursor ( ) . min ) ) ;
236
+ let sub_id = ui. id ( ) . with ( self . entry . index ) ;
234
237
let button = self . entry . show_with_state ( ui, EntryState :: submenu ( self . parent_state , sub_id) ) ;
235
238
self . parent_state
236
239
. submenu_button_interaction ( ui, sub_id, & button) ;
@@ -244,29 +247,36 @@ pub struct MenuState {
244
247
sub_menu : Option < ( Id , Box < MenuState > ) > ,
245
248
rect : Rect ,
246
249
response : MenuResponse ,
250
+ entry_count : usize ,
247
251
}
248
252
impl MenuState {
249
253
/// close menu hierarchy
250
254
pub fn close ( & mut self ) {
251
255
self . response = MenuResponse :: Close ;
252
256
}
253
257
/// create a menu item
254
- pub fn item ( & self , text : impl ToString ) -> MenuEntry {
255
- MenuEntry :: new ( text, "" , EntryState :: entry ( self ) )
258
+ pub fn item ( & mut self , text : impl ToString ) -> MenuEntry {
259
+ MenuEntry :: new ( text, "" , EntryState :: entry ( self ) , self . next_entry_index ( ) )
256
260
}
257
261
/// create a menu item with an icon
258
- pub fn item_with_icon ( & self , text : impl ToString , icon : impl ToString ) -> MenuEntry {
259
- MenuEntry :: new ( text, icon, EntryState :: entry ( self ) )
262
+ pub fn item_with_icon ( & mut self , text : impl ToString , icon : impl ToString ) -> MenuEntry {
263
+ MenuEntry :: new ( text, icon, EntryState :: entry ( self ) , self . next_entry_index ( ) )
264
+ }
265
+ fn next_entry_index ( & mut self ) -> usize {
266
+ self . entry_count += 1 ;
267
+ self . entry_count -1
260
268
}
261
269
/// create a sub-menu
262
270
pub fn submenu ( & ' _ mut self , text : impl ToString ) -> SubMenu < ' _ > {
263
- SubMenu :: new ( text, self )
271
+ let index = self . next_entry_index ( ) ;
272
+ SubMenu :: new ( text, self , index)
264
273
}
265
274
fn new ( position : Pos2 ) -> Self {
266
275
Self {
267
276
rect : Rect :: from_min_size ( position, Vec2 :: ZERO ) ,
268
277
sub_menu : None ,
269
278
response : MenuResponse :: Stay ,
279
+ entry_count : 0 ,
270
280
}
271
281
}
272
282
/// sense button interaction opening and closing submenu
@@ -303,6 +313,7 @@ impl MenuState {
303
313
id : Id ,
304
314
add_contents : impl FnOnce ( & mut Ui , & mut MenuState ) ,
305
315
) -> Response {
316
+ self . entry_count = 0 ;
306
317
crate :: menu:: menu_ui (
307
318
ctx,
308
319
id,
0 commit comments