Move struct Options into Memory so Style persists

This commit is contained in:
Emil Ernerfeldt 2021-01-02 23:28:44 +01:00
parent a905c884e8
commit d2d9bf4bdd
3 changed files with 34 additions and 25 deletions

View file

@ -14,20 +14,6 @@ use crate::{
// ----------------------------------------------------------------------------
#[derive(Clone, Debug, Default)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[cfg_attr(feature = "serde", serde(default))]
struct Options {
/// The default style for new `Ui`:s.
style: Arc<Style>,
/// Controls the tessellator.
tessellation_options: paint::TessellationOptions,
/// Font sizes etc.
font_definitions: FontDefinitions,
}
// ----------------------------------------------------------------------------
/// State that is collected during a frame and then cleared.
/// Short-term (single frame) memory.
#[derive(Clone)]
@ -389,11 +375,10 @@ impl CtxRef {
/// This is the first thing you need when working with Egui. Create using [`CtxRef`].
///
/// Contains the [`InputState`], [`Memory`], [`Output`], options and more.
/// Contains the [`InputState`], [`Memory`], [`Output`], and more.
// TODO: too many mutexes. Maybe put it all behind one Mutex instead.
#[derive(Default)]
pub struct Context {
options: Mutex<Options>,
/// None until first call to `begin_frame`.
fonts: Option<Arc<Fonts>>,
memory: Arc<Mutex<Memory>>,
@ -417,7 +402,6 @@ pub struct Context {
impl Clone for Context {
fn clone(&self) -> Self {
Context {
options: self.options.clone(),
fonts: self.fonts.clone(),
memory: self.memory.clone(),
animation_manager: self.animation_manager.clone(),
@ -493,17 +477,17 @@ impl Context {
/// Will become active at the start of the next frame.
/// `pixels_per_point` will be ignored (overwritten at start of each frame with the contents of input)
pub fn set_fonts(&self, font_definitions: FontDefinitions) {
self.options.lock().font_definitions = font_definitions;
self.memory().options.font_definitions = font_definitions;
}
/// The [`Style`] used by all new windows, panels etc.
pub fn style(&self) -> Arc<Style> {
self.options.lock().style.clone()
self.memory().options.style.clone()
}
/// The [`Style`] used by all new windows, panels etc.
pub fn set_style(&self, style: impl Into<Arc<Style>>) {
self.options.lock().style = style.into();
self.memory().options.style = style.into();
}
/// The number of physical pixels for each logical point.
@ -578,7 +562,7 @@ impl Context {
self.input = std::mem::take(&mut self.input).begin_frame(new_raw_input);
self.frame_state.lock().begin_frame(&self.input);
let font_definitions = self.options.lock().font_definitions.clone();
let font_definitions = self.memory().options.font_definitions.clone();
let pixels_per_point = self.input.pixels_per_point();
let same_as_current = match &self.fonts {
None => false,
@ -635,7 +619,7 @@ impl Context {
/// Tessellate the given paint commands into triangle meshes.
pub fn tessellate(&self, paint_commands: Vec<(Rect, PaintCmd)>) -> PaintJobs {
let mut tessellation_options = self.options.lock().tessellation_options;
let mut tessellation_options = self.memory().options.tessellation_options;
tessellation_options.aa_size = 1.0 / self.pixels_per_point();
let paint_stats = PaintStats::from_paint_commands(&paint_commands); // TODO: internal allocations
let paint_jobs = tessellator::tessellate_paint_commands(
@ -775,9 +759,9 @@ impl Context {
CollapsingHeader::new("✒ Painting")
.default_open(true)
.show(ui, |ui| {
let mut tessellation_options = self.options.lock().tessellation_options;
let mut tessellation_options = self.memory().options.tessellation_options;
tessellation_options.ui(ui);
self.options.lock().tessellation_options = tessellation_options;
self.memory().options.tessellation_options = tessellation_options;
});
}

View file

@ -6,9 +6,11 @@ use crate::{
resize, scroll_area,
util::Cache,
widgets::text_edit,
window, Id, LayerId, Pos2, Rect,
window, Id, LayerId, Pos2, Rect, Style,
};
// ----------------------------------------------------------------------------
/// The data that Egui persists between frames.
///
/// This includes window positions and sizes,
@ -19,6 +21,8 @@ use crate::{
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[cfg_attr(feature = "serde", serde(default))]
pub struct Memory {
pub(crate) options: Options,
#[cfg_attr(feature = "serde", serde(skip))]
pub(crate) interaction: Interaction,
@ -53,6 +57,22 @@ pub struct Memory {
everything_is_visible: bool,
}
// ----------------------------------------------------------------------------
#[derive(Clone, Debug, Default)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[cfg_attr(feature = "serde", serde(default))]
pub(crate) struct Options {
/// The default style for new `Ui`:s.
pub(crate) style: std::sync::Arc<Style>,
/// Controls the tessellator.
pub(crate) tessellation_options: crate::paint::TessellationOptions,
/// Font sizes etc.
pub(crate) font_definitions: crate::paint::FontDefinitions,
}
// ----------------------------------------------------------------------------
/// Say there is a button in a scroll area.
/// If the user clicks the button, the button should click.
/// If the user drags the button we should scroll the scroll area.

View file

@ -40,6 +40,7 @@ impl Style {
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[cfg_attr(feature = "serde", serde(default))]
pub struct Spacing {
/// Horizontal and vertical spacing between widgets
pub item_spacing: Vec2,
@ -95,6 +96,7 @@ impl Spacing {
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[cfg_attr(feature = "serde", serde(default))]
pub struct Interaction {
/// Mouse must be the close to the side of a window to resize
pub resize_grab_radius_side: f32,
@ -105,6 +107,7 @@ pub struct Interaction {
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[cfg_attr(feature = "serde", serde(default))]
pub struct Visuals {
/// Override default text color for all text.
///
@ -166,6 +169,7 @@ impl Visuals {
/// Selected text, selected elements etc
#[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[cfg_attr(feature = "serde", serde(default))]
pub struct Selection {
pub bg_fill: Color32,
pub stroke: Stroke,
@ -173,6 +177,7 @@ pub struct Selection {
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[cfg_attr(feature = "serde", serde(default))]
pub struct Widgets {
/// For an interactive widget that is being interacted with
pub active: WidgetVisuals,