From 5c6f9c4172b49690ee554dd4ff19f1347f15479b Mon Sep 17 00:00:00 2001 From: lampsitter <96946613+lampsitter@users.noreply.github.com> Date: Thu, 6 Jan 2022 00:21:44 +0100 Subject: [PATCH] Fix context menu styling --- CHANGELOG.md | 2 ++ egui/src/menu.rs | 57 ++++++++++++++++++++++++------------------------ 2 files changed, 30 insertions(+), 29 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ab5175d82162..834958558a14 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,8 @@ NOTE: [`epaint`](epaint/CHANGELOG.md), [`eframe`](eframe/CHANGELOG.md), [`egui_w ### Changed 🔧 * 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` diff --git a/egui/src/menu.rs b/egui/src/menu.rs index 986d695e7494..d17491effafa 100644 --- a/egui/src/menu.rs +++ b/egui/src/menu.rs @@ -17,8 +17,8 @@ use super::{ style::{Spacing, WidgetVisuals}, - Align, CtxRef, Id, InnerResponse, PointerState, Pos2, Rect, Response, Sense, Style, TextStyle, - Ui, Vec2, + Align, CtxRef, Id, InnerResponse, PointerState, Pos2, Rect, Response, Sense, TextStyle, Ui, + Vec2, }; use crate::{widgets::*, *}; use epaint::{mutex::RwLock, Stroke}; @@ -120,7 +120,6 @@ pub(crate) fn menu_ui<'c, R>( ctx: &CtxRef, menu_id: impl std::hash::Hash, menu_state_arc: &Arc>, - mut style: Style, add_contents: impl FnOnce(&mut Ui) -> R + 'c, ) -> InnerResponse { let pos = { @@ -128,29 +127,37 @@ pub(crate) fn menu_ui<'c, R>( menu_state.entry_count = 0; 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) .order(Order::Foreground) .fixed_pos(pos) .interactable(false) .drag_bounds(Rect::EVERYTHING); - let frame = Frame::menu(&style); let inner_response = area.show(ctx, |ui| { - frame - .show(ui, |ui| { - const DEFAULT_MENU_WIDTH: f32 = 150.0; // TODO: add to ui.spacing - ui.set_max_width(DEFAULT_MENU_WIDTH); - ui.set_style(style); - ui.set_menu_state(Some(menu_state_arc.clone())); - ui.with_layout(Layout::top_down_justified(Align::LEFT), add_contents) - .inner - }) - .inner + ui.scope(|ui| { + let style = ui.style_mut(); + style.spacing = Spacing { + item_spacing: Vec2::ZERO, + button_padding: crate::vec2(2.0, 0.0), + ..Default::default() + }; + + style.visuals.widgets.active.bg_stroke = Stroke::none(); + style.visuals.widgets.hovered.bg_stroke = Stroke::none(); + style.visuals.widgets.inactive.bg_fill = Color32::TRANSPARENT; + style.visuals.widgets.inactive.bg_stroke = Stroke::none(); + + 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; inner_response @@ -522,15 +529,7 @@ impl MenuState { id: Id, add_contents: impl FnOnce(&mut Ui) -> R, ) -> InnerResponse { - let style = Style { - 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) + crate::menu::menu_ui(ctx, id, menu_state, add_contents) } fn show_submenu( &mut self,