Code cleanup and improved docs

This commit is contained in:
Emil Ernerfeldt 2022-07-30 18:39:38 +02:00
parent 5fb4efa768
commit 2500a60062
7 changed files with 48 additions and 36 deletions

View file

@ -195,6 +195,9 @@ pub struct NativeOptions {
pub drag_and_drop_support: bool, pub drag_and_drop_support: bool,
/// The application icon, e.g. in the Windows task bar etc. /// The application icon, e.g. in the Windows task bar etc.
///
/// This doesn't work on Mac and on Wayland.
/// See <https://docs.rs/winit/latest/winit/window/struct.Window.html#method.set_window_icon> for more.
pub icon_data: Option<IconData>, pub icon_data: Option<IconData>,
/// The initial (inner) position of the native window in points (logical pixels). /// The initial (inner) position of the native window in points (logical pixels).

View file

@ -66,6 +66,14 @@ pub struct PlatformOutput {
/// If set, put this text in the system clipboard. Ignore if empty. /// If set, put this text in the system clipboard. Ignore if empty.
/// ///
/// This is often a response to [`crate::Event::Copy`] or [`crate::Event::Cut`]. /// This is often a response to [`crate::Event::Copy`] or [`crate::Event::Cut`].
///
/// ```
/// # egui::__run_test_ui(|ui| {
/// if ui.button("📋").clicked() {
/// ui.output().copied_text = "some_text".to_string();
/// }
/// # });
/// ```
pub copied_text: String, pub copied_text: String,
/// Events that may be useful to e.g. a screen reader. /// Events that may be useful to e.g. a screen reader.

View file

@ -20,7 +20,7 @@ use super::{
Sense, TextStyle, Ui, Vec2, Sense, TextStyle, Ui, Vec2,
}; };
use crate::{widgets::*, *}; use crate::{widgets::*, *};
use epaint::{mutex::RwLock, Stroke}; use epaint::mutex::RwLock;
use std::sync::Arc; use std::sync::Arc;
/// What is saved between frames. /// What is saved between frames.
@ -61,20 +61,20 @@ impl std::ops::DerefMut for BarState {
} }
} }
fn set_menu_style(style: &mut Style) {
style.spacing.button_padding = vec2(2.0, 0.0);
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();
}
/// The menu bar goes well in a [`TopBottomPanel::top`], /// The menu bar goes well in a [`TopBottomPanel::top`],
/// but can also be placed in a [`Window`]. /// but can also be placed in a [`Window`].
/// In the latter case you may want to wrap it in [`Frame`]. /// In the latter case you may want to wrap it in [`Frame`].
pub fn bar<R>(ui: &mut Ui, add_contents: impl FnOnce(&mut Ui) -> R) -> InnerResponse<R> { pub fn bar<R>(ui: &mut Ui, add_contents: impl FnOnce(&mut Ui) -> R) -> InnerResponse<R> {
ui.horizontal(|ui| { ui.horizontal(|ui| {
let mut style = (**ui.style()).clone(); set_menu_style(ui.style_mut());
style.spacing.button_padding = vec2(2.0, 0.0);
// 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();
ui.set_style(style);
// Take full width and fixed height: // Take full width and fixed height:
let height = ui.spacing().interact_size.y; let height = ui.spacing().interact_size.y;
@ -130,27 +130,17 @@ pub(crate) fn menu_ui<'c, R>(
.interactable(true) .interactable(true)
.drag_bounds(Rect::EVERYTHING); .drag_bounds(Rect::EVERYTHING);
let inner_response = area.show(ctx, |ui| { let inner_response = area.show(ctx, |ui| {
ui.scope(|ui| { set_menu_style(ui.style_mut());
let style = ui.style_mut();
style.spacing.item_spacing = Vec2::ZERO;
style.spacing.button_padding = crate::vec2(2.0, 0.0);
style.visuals.widgets.active.bg_stroke = Stroke::none(); Frame::menu(ui.style())
style.visuals.widgets.hovered.bg_stroke = Stroke::none(); .show(ui, |ui| {
style.visuals.widgets.inactive.bg_fill = Color32::TRANSPARENT; const DEFAULT_MENU_WIDTH: f32 = 150.0; // TODO(emilk): add to ui.spacing
style.visuals.widgets.inactive.bg_stroke = Stroke::none(); ui.set_max_width(DEFAULT_MENU_WIDTH);
ui.set_menu_state(Some(menu_state_arc.clone()));
Frame::menu(style) ui.with_layout(Layout::top_down_justified(Align::LEFT), add_contents)
.show(ui, |ui| { .inner
const DEFAULT_MENU_WIDTH: f32 = 150.0; // TODO(emilk): add to ui.spacing })
ui.set_max_width(DEFAULT_MENU_WIDTH); .inner
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
@ -437,11 +427,13 @@ impl SubMenuButton {
.align_size_within_rect(icon_galley.size(), rect.shrink2(button_padding)) .align_size_within_rect(icon_galley.size(), rect.shrink2(button_padding))
.min; .min;
ui.painter().rect_filled( if ui.visuals().button_frame {
rect.expand(visuals.expansion), ui.painter().rect_filled(
visuals.rounding, rect.expand(visuals.expansion),
visuals.bg_fill, visuals.rounding,
); visuals.bg_fill,
);
}
let text_color = visuals.text_color(); let text_color = visuals.text_color();
text_galley.paint_with_fallback_color(ui.painter(), text_pos, text_color); text_galley.paint_with_fallback_color(ui.painter(), text_pos, text_color);

View file

@ -463,6 +463,7 @@ impl Response {
/// if response.clicked() { /* … */ } /// if response.clicked() { /* … */ }
/// # }); /// # });
/// ``` /// ```
#[must_use]
pub fn interact(&self, sense: Sense) -> Self { pub fn interact(&self, sense: Sense) -> Self {
self.ctx.interact_with_hovered( self.ctx.interact_with_hovered(
self.layer_id, self.layer_id,

View file

@ -370,6 +370,13 @@ impl Ui {
/// The [`PlatformOutput`] of the [`Context`] associated with this ui. /// The [`PlatformOutput`] of the [`Context`] associated with this ui.
/// Equivalent to `.ctx().output()`. /// Equivalent to `.ctx().output()`.
///
/// ```
/// # egui::__run_test_ui(|ui| {
/// if ui.button("📋").clicked() {
/// ui.output().copied_text = "some_text".to_string();
/// }
/// # });
#[inline] #[inline]
pub fn output(&self) -> RwLockWriteGuard<'_, PlatformOutput> { pub fn output(&self) -> RwLockWriteGuard<'_, PlatformOutput> {
self.ctx().output() self.ctx().output()

View file

@ -6,6 +6,7 @@ cd "$script_path/.."
./sh/setup_web.sh ./sh/setup_web.sh
CRATE_NAME="egui_demo_app" CRATE_NAME="egui_demo_app"
# NOTE: persistence use up about 400kB (10%) of the WASM!
FEATURES="glow,http,persistence,screen_reader" FEATURES="glow,http,persistence,screen_reader"
OPEN=false OPEN=false

View file

@ -58,7 +58,7 @@ cargo deny check
# #
# For finding bloat: # For finding bloat:
# cargo bloat --release --bin demo_glium -n 200 | rg egui # cargo bloat --release --bin egui_demo_app -n 200 | rg egui
# Also try https://github.com/google/bloaty # Also try https://github.com/google/bloaty
# what compiles slowly? # what compiles slowly?