patches
from
matplotlib.typing
import
@dataclass
class
ItemProperties
:
fontsize
:
float
=
14
labelcolor
:
=
'black'
bgcolor
:
=
'yellow'
alpha
:
float
=
1.0
class
MenuItem
(
):
padx
=
0.05
pady
=
0.05
def
__init__
(
self
,
,
labelstr
,
props
=
None
,
hoverprops
=
None
,
on_select
=
None
):
super
()
.
__init__
()
self
.
set_figure
(
)
self
.
labelstr
=
labelstr
self
.
props
=
props
if
props
is
not
None
else
ItemProperties
()
self
.
hoverprops
=
(
hoverprops
if
hoverprops
is
not
None
else
ItemProperties
())
if
self
.
!=
self
.
hoverprops
.
fontsize
:
raise
NotImplementedError
(
'support for different font sizes not implemented'
)
self
.
on_select
=
on_select
self
.
=
(
0
,
0
,
labelstr
,
transform
=
,
size
=
)
self
.
text_bbox
=
self
.
.
get_window_extent
(
())
self
.
text_bbox
=
()
.
transform_bbox
(
self
.
text_bbox
)
self
.
rect
=
(
(
0
,
0
),
1
,
1
,
transform
=
)
self
.
set_hover_props
(
False
)
(
'button_release_event'
,
self
.
check_select
)
def
check_select
(
self
,
event
):
over
,
_
=
self
.
rect
.
contains
(
event
)
if
not
over
:
return
if
self
.
on_select
is
not
None
:
self
.
on_select
(
self
)
def
set_extent
(
self
,
x
,
y
,
w
,
h
,
depth
):
self
.
rect
.
set
(
x
=
x
,
y
=
y
,
width
=
w
,
height
=
h
)
self
.
.
set
(
position
=
(
x
+
self
.
padx
,
y
+
depth
+
self
.
pady
/
2
))
self
.
hover
=
False
def
draw
(
self
,
renderer
):
self
.
rect
.
draw
(
renderer
)
self
.
.
draw
(
renderer
)
def
set_hover_props
(
self
,
b
):
props
=
self
.
hoverprops
if
b
else
self
.
props
self
.
.
set
(
color
=
)
self
.
rect
.
set
(
facecolor
=
,
alpha
=
)
def
set_hover
(
self
,
event
):
"""
Update the hover status of event and return whether it was changed.
"""
b
,
_
=
self
.
rect
.
contains
(
event
)
changed
=
(
b
!=
self
.
hover
)
if
changed
:
self
.
set_hover_props
(
b
)
self
.
hover
=
b
return
changed
class
Menu
:
def
__init__
(
self
,
,
):
self
.
figure
=
self
.
=
maxw
=
max
(
for
item
in
)
maxh
=
max
(
for
item
in
)
depth
=
max
(
-
for
item
in
)
x0
=
1
y0
=
4
width
=
maxw
+
2
*
height
=
maxh
+
for
item
in
:
left
=
x0
bottom
=
y0
-
maxh
-
item
.
set_extent
(
left
,
bottom
,
width
,
height
,
depth
)
.
artists
.
append
(
item
)
y0
-=
maxh
+
(
'motion_notify_event'
,
self
.
on_move
)
def
on_move
(
self
,
event
):
if
any
(
item
.
set_hover
(
event
)
for
item
in
self
.
):
self
.
figure
.
canvas
.
draw
()
=
()
(
left
=
0.3
)
props
=
ItemProperties
(
labelcolor
=
'black'
,
bgcolor
=
'yellow'
,
fontsize
=
15
,
alpha
=
0.2
)
hoverprops
=
ItemProperties
(
labelcolor
=
'white'
,
bgcolor
=
'blue'
,
fontsize
=
15
,
alpha
=
0.2
)
=
[]
for
in
(
'open'
,
'close'
,
'save'
,
'save as'
,
'quit'
):
def
on_select
(
item
):
print
(
f
'you selected
{
}
'
)
item
=
(
,
,
props
=
props
,
hoverprops
=
hoverprops
,
on_select
=
on_select
)
.
append
(
item
)
menu
=
Menu
(
,
)
()