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,
/// 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>,
/// 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.
///
/// 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,
/// Events that may be useful to e.g. a screen reader.

View file

@ -20,7 +20,7 @@ use super::{
Sense, TextStyle, Ui, Vec2,
};
use crate::{widgets::*, *};
use epaint::{mutex::RwLock, Stroke};
use epaint::mutex::RwLock;
use std::sync::Arc;
/// 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`],
/// but can also be placed in a [`Window`].
/// 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> {
ui.horizontal(|ui| {
let mut style = (**ui.style()).clone();
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);
set_menu_style(ui.style_mut());
// Take full width and fixed height:
let height = ui.spacing().interact_size.y;
@ -130,27 +130,17 @@ pub(crate) fn menu_ui<'c, R>(
.interactable(true)
.drag_bounds(Rect::EVERYTHING);
let inner_response = area.show(ctx, |ui| {
ui.scope(|ui| {
let style = ui.style_mut();
style.spacing.item_spacing = Vec2::ZERO;
style.spacing.button_padding = crate::vec2(2.0, 0.0);
set_menu_style(ui.style_mut());
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(emilk): 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
Frame::menu(ui.style())
.show(ui, |ui| {
const DEFAULT_MENU_WIDTH: f32 = 150.0; // TODO(emilk): 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
});
menu_state_arc.write().rect = inner_response.response.rect;
inner_response
@ -437,11 +427,13 @@ impl SubMenuButton {
.align_size_within_rect(icon_galley.size(), rect.shrink2(button_padding))
.min;
ui.painter().rect_filled(
rect.expand(visuals.expansion),
visuals.rounding,
visuals.bg_fill,
);
if ui.visuals().button_frame {
ui.painter().rect_filled(
rect.expand(visuals.expansion),
visuals.rounding,
visuals.bg_fill,
);
}
let text_color = visuals.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() { /* … */ }
/// # });
/// ```
#[must_use]
pub fn interact(&self, sense: Sense) -> Self {
self.ctx.interact_with_hovered(
self.layer_id,

View file

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

View file

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

View file

@ -58,7 +58,7 @@ cargo deny check
#
# 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
# what compiles slowly?