Fix context menu styling (#1043)

This commit is contained in:
Lampsitter 2022-01-06 12:17:12 +01:00 committed by GitHub
parent 7863f44111
commit b0ea4dc0b5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 30 deletions

View file

@ -13,6 +13,8 @@ NOTE: [`epaint`](epaint/CHANGELOG.md), [`eframe`](eframe/CHANGELOG.md), [`egui_w
### Changed 🔧 ### Changed 🔧
* Renamed `Ui::visible` to `Ui::is_visible`. * Renamed `Ui::visible` to `Ui::is_visible`.
### Fixed 🐛
* Context menu now respects the theme ([#1043](https://github.com/emilk/egui/pull/1043))
## 0.16.1 - 2021-12-31 - Add back `CtxRef::begin_frame,end_frame` ## 0.16.1 - 2021-12-31 - Add back `CtxRef::begin_frame,end_frame`

View file

@ -16,9 +16,8 @@
//! ``` //! ```
use super::{ use super::{
style::{Spacing, WidgetVisuals}, style::WidgetVisuals, Align, CtxRef, Id, InnerResponse, PointerState, Pos2, Rect, Response,
Align, CtxRef, Id, InnerResponse, PointerState, Pos2, Rect, Response, Sense, Style, TextStyle, Sense, TextStyle, Ui, Vec2,
Ui, Vec2,
}; };
use crate::{widgets::*, *}; use crate::{widgets::*, *};
use epaint::{mutex::RwLock, Stroke}; use epaint::{mutex::RwLock, Stroke};
@ -120,7 +119,6 @@ pub(crate) fn menu_ui<'c, R>(
ctx: &CtxRef, ctx: &CtxRef,
menu_id: impl std::hash::Hash, menu_id: impl std::hash::Hash,
menu_state_arc: &Arc<RwLock<MenuState>>, menu_state_arc: &Arc<RwLock<MenuState>>,
mut style: Style,
add_contents: impl FnOnce(&mut Ui) -> R + 'c, add_contents: impl FnOnce(&mut Ui) -> R + 'c,
) -> InnerResponse<R> { ) -> InnerResponse<R> {
let pos = { let pos = {
@ -128,29 +126,34 @@ pub(crate) fn menu_ui<'c, R>(
menu_state.entry_count = 0; menu_state.entry_count = 0;
menu_state.rect.min menu_state.rect.min
}; };
// style.visuals.widgets.active.bg_fill = Color32::TRANSPARENT;
style.visuals.widgets.active.bg_stroke = Stroke::none();
// style.visuals.widgets.hovered.bg_fill = Color32::TRANSPARENT;
style.visuals.widgets.hovered.bg_stroke = Stroke::none();
style.visuals.widgets.inactive.bg_fill = Color32::TRANSPARENT;
style.visuals.widgets.inactive.bg_stroke = Stroke::none();
let area = Area::new(menu_id) let area = Area::new(menu_id)
.order(Order::Foreground) .order(Order::Foreground)
.fixed_pos(pos) .fixed_pos(pos)
.interactable(false) .interactable(false)
.drag_bounds(Rect::EVERYTHING); .drag_bounds(Rect::EVERYTHING);
let frame = Frame::menu(&style);
let inner_response = area.show(ctx, |ui| { let inner_response = area.show(ctx, |ui| {
frame ui.scope(|ui| {
.show(ui, |ui| { let style = ui.style_mut();
const DEFAULT_MENU_WIDTH: f32 = 150.0; // TODO: add to ui.spacing style.spacing.item_spacing = Vec2::ZERO;
ui.set_max_width(DEFAULT_MENU_WIDTH); style.spacing.button_padding = crate::vec2(2.0, 0.0);
ui.set_style(style);
ui.set_menu_state(Some(menu_state_arc.clone())); style.visuals.widgets.active.bg_stroke = Stroke::none();
ui.with_layout(Layout::top_down_justified(Align::LEFT), add_contents) style.visuals.widgets.hovered.bg_stroke = Stroke::none();
.inner style.visuals.widgets.inactive.bg_fill = Color32::TRANSPARENT;
}) style.visuals.widgets.inactive.bg_stroke = Stroke::none();
.inner
Frame::menu(style)
.show(ui, |ui| {
const DEFAULT_MENU_WIDTH: f32 = 150.0; // TODO: add to ui.spacing
ui.set_max_width(DEFAULT_MENU_WIDTH);
ui.set_menu_state(Some(menu_state_arc.clone()));
ui.with_layout(Layout::top_down_justified(Align::LEFT), add_contents)
.inner
})
.inner
})
.inner
}); });
menu_state_arc.write().rect = inner_response.response.rect; menu_state_arc.write().rect = inner_response.response.rect;
inner_response inner_response
@ -522,15 +525,7 @@ impl MenuState {
id: Id, id: Id,
add_contents: impl FnOnce(&mut Ui) -> R, add_contents: impl FnOnce(&mut Ui) -> R,
) -> InnerResponse<R> { ) -> InnerResponse<R> {
let style = Style { crate::menu::menu_ui(ctx, id, menu_state, add_contents)
spacing: Spacing {
item_spacing: Vec2::ZERO,
button_padding: crate::vec2(2.0, 0.0),
..Default::default()
},
..Default::default()
};
crate::menu::menu_ui(ctx, id, menu_state, style, add_contents)
} }
fn show_submenu<R>( fn show_submenu<R>(
&mut self, &mut self,