Skip to content

Commit

Permalink
Replace Arc<Context> with struct CtxRef
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk committed Dec 19, 2020
1 parent ce0ea74 commit 01c65b0
Show file tree
Hide file tree
Showing 24 changed files with 286 additions and 300 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

### Changed 🔧

* `Arc<Context>` has been replaced with `CtxRef` everywhere.
* Slight tweak of the default `Style` and font sizes.
* `SidePanel::left` and `TopPanel::top` now takes `impl Hash` as first argument.
* A `Window` may now cover an existing `CentralPanel`.
Expand Down
8 changes: 4 additions & 4 deletions egui/benches/benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pub fn criterion_benchmark(c: &mut Criterion) {
let raw_input = egui::RawInput::default();

{
let mut ctx = egui::Context::new();
let mut ctx = egui::CtxRef::default();
let mut demo_windows = egui::demos::DemoWindows::default();

c.bench_function("demo_windows_minimal", |b| {
Expand All @@ -17,7 +17,7 @@ pub fn criterion_benchmark(c: &mut Criterion) {
}

{
let mut ctx = egui::Context::new();
let mut ctx = egui::CtxRef::default();
ctx.memory().all_collpasing_are_open = true; // expand the demo window with everything
let mut demo_windows = egui::demos::DemoWindows::default();

Expand All @@ -31,7 +31,7 @@ pub fn criterion_benchmark(c: &mut Criterion) {
}

{
let mut ctx = egui::Context::new();
let mut ctx = egui::CtxRef::default();
ctx.memory().all_collpasing_are_open = true; // expand the demo window with everything
let mut demo_windows = egui::demos::DemoWindows::default();
ctx.begin_frame(raw_input.clone());
Expand All @@ -44,7 +44,7 @@ pub fn criterion_benchmark(c: &mut Criterion) {
}

{
let mut ctx = egui::Context::new();
let mut ctx = egui::CtxRef::default();
ctx.begin_frame(raw_input);
egui::CentralPanel::default().show(&ctx, |ui| {
c.bench_function("label", |b| {
Expand Down
10 changes: 2 additions & 8 deletions egui/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
// TODO: move egui/src/app.rs to own crate, e.g. egui_framework ?

use crate::Context;

/// Implement this trait to write apps that can be compiled both natively using the [`egui_glium`](https://crates.io/crates/egui_glium) crate,
/// and deployed as a web site using the [`egui_web`](https://crates.io/crates/egui_web) crate.
pub trait App {
Expand All @@ -24,15 +22,11 @@ pub trait App {
/// Called once before the first frame.
/// Allows you to do setup code and to call `ctx.set_fonts()`.
/// Optional.
fn setup(&mut self, _ctx: &std::sync::Arc<Context>) {}
fn setup(&mut self, _ctx: &crate::CtxRef) {}

/// Called each time the UI needs repainting, which may be many times per second.
/// Put your widgets into a `SidePanel`, `TopPanel`, `CentralPanel`, `Window` or `Area`.
fn ui(
&mut self,
ctx: &std::sync::Arc<Context>,
integration_context: &mut IntegrationContext<'_>,
);
fn ui(&mut self, ctx: &crate::CtxRef, integration_context: &mut IntegrationContext<'_>);

/// Called once on shutdown. Allows you to save state.
fn on_exit(&mut self, _storage: &mut dyn Storage) {}
Expand Down
10 changes: 5 additions & 5 deletions egui/src/containers/area.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//! It has no frame or own size. It is potentially movable.
//! It is the foundation for windows and popups.
use std::{fmt::Debug, hash::Hash, sync::Arc};
use std::{fmt::Debug, hash::Hash};

use crate::*;

Expand Down Expand Up @@ -103,7 +103,7 @@ pub(crate) struct Prepared {
}

impl Area {
pub(crate) fn begin(self, ctx: &Arc<Context>) -> Prepared {
pub(crate) fn begin(self, ctx: &CtxRef) -> Prepared {
let Area {
id,
movable,
Expand Down Expand Up @@ -131,7 +131,7 @@ impl Area {
}
}

pub fn show(self, ctx: &Arc<Context>, add_contents: impl FnOnce(&mut Ui)) -> Response {
pub fn show(self, ctx: &CtxRef, add_contents: impl FnOnce(&mut Ui)) -> Response {
let prepared = self.begin(ctx);
let mut content_ui = prepared.content_ui(ctx);
add_contents(&mut content_ui);
Expand All @@ -148,7 +148,7 @@ impl Prepared {
&mut self.state
}

pub(crate) fn content_ui(&self, ctx: &Arc<Context>) -> Ui {
pub(crate) fn content_ui(&self, ctx: &CtxRef) -> Ui {
let max_rect = Rect::from_min_size(self.state.pos, Vec2::infinity());
let clip_rect = max_rect
.expand(ctx.style().visuals.clip_rect_margin)
Expand All @@ -163,7 +163,7 @@ impl Prepared {
}

#[allow(clippy::needless_pass_by_value)] // intentional to swallow up `content_ui`.
pub(crate) fn end(self, ctx: &Arc<Context>, content_ui: Ui) -> Response {
pub(crate) fn end(self, ctx: &CtxRef, content_ui: Ui) -> Response {
let Prepared {
layer_id,
mut state,
Expand Down
19 changes: 3 additions & 16 deletions egui/src/containers/panel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
//! the only places where you can put you widgets.
use crate::*;
use std::sync::Arc;

// ----------------------------------------------------------------------------

Expand All @@ -27,11 +26,7 @@ impl SidePanel {
}

impl SidePanel {
pub fn show<R>(
self,
ctx: &Arc<Context>,
add_contents: impl FnOnce(&mut Ui) -> R,
) -> (R, Response) {
pub fn show<R>(self, ctx: &CtxRef, add_contents: impl FnOnce(&mut Ui) -> R) -> (R, Response) {
let Self { id, max_width } = self;

let mut panel_rect = ctx.available_rect();
Expand Down Expand Up @@ -80,11 +75,7 @@ impl TopPanel {
}

impl TopPanel {
pub fn show<R>(
self,
ctx: &Arc<Context>,
add_contents: impl FnOnce(&mut Ui) -> R,
) -> (R, Response) {
pub fn show<R>(self, ctx: &CtxRef, add_contents: impl FnOnce(&mut Ui) -> R) -> (R, Response) {
let Self { id, max_height } = self;
let max_height = max_height.unwrap_or_else(|| ctx.style().spacing.interact_size.y);

Expand Down Expand Up @@ -122,11 +113,7 @@ impl TopPanel {
pub struct CentralPanel {}

impl CentralPanel {
pub fn show<R>(
self,
ctx: &Arc<Context>,
add_contents: impl FnOnce(&mut Ui) -> R,
) -> (R, Response) {
pub fn show<R>(self, ctx: &CtxRef, add_contents: impl FnOnce(&mut Ui) -> R) -> (R, Response) {
let Self {} = self;

let panel_rect = ctx.available_rect();
Expand Down
8 changes: 3 additions & 5 deletions egui/src/containers/popup.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use std::sync::Arc;

use crate::*;

/// Show a tooltip at the current mouse position (if any).
pub fn show_tooltip(ctx: &Arc<Context>, add_contents: impl FnOnce(&mut Ui)) {
pub fn show_tooltip(ctx: &CtxRef, add_contents: impl FnOnce(&mut Ui)) {
let tooltip_rect = ctx.memory().tooltip_rect;

let window_pos = if let Some(tooltip_rect) = tooltip_rect {
Expand All @@ -23,15 +21,15 @@ pub fn show_tooltip(ctx: &Arc<Context>, add_contents: impl FnOnce(&mut Ui)) {
}

/// Show a tooltip at the current mouse position (if any).
pub fn show_tooltip_text(ctx: &Arc<Context>, text: impl Into<String>) {
pub fn show_tooltip_text(ctx: &CtxRef, text: impl Into<String>) {
show_tooltip(ctx, |ui| {
ui.add(crate::widgets::Label::new(text));
})
}

/// Show a pop-over window.
fn show_popup(
ctx: &Arc<Context>,
ctx: &CtxRef,
id: Id,
window_pos: Pos2,
add_contents: impl FnOnce(&mut Ui),
Expand Down
6 changes: 2 additions & 4 deletions egui/src/containers/window.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// WARNING: the code in here is horrible. It is a behemoth that needs breaking up into simpler parts.

use std::sync::Arc;

use crate::{paint::*, widgets::*, *};

use super::*;
Expand Down Expand Up @@ -183,13 +181,13 @@ impl<'open> Window<'open> {
}

impl<'open> Window<'open> {
pub fn show(self, ctx: &Arc<Context>, add_contents: impl FnOnce(&mut Ui)) -> Option<Response> {
pub fn show(self, ctx: &CtxRef, add_contents: impl FnOnce(&mut Ui)) -> Option<Response> {
self.show_impl(ctx, Box::new(add_contents))
}

fn show_impl<'c>(
self,
ctx: &Arc<Context>,
ctx: &CtxRef,
add_contents: Box<dyn FnOnce(&mut Ui) + 'c>,
) -> Option<Response> {
let Window {
Expand Down
Loading

0 comments on commit 01c65b0

Please sign in to comment.