Skip to content

Commit

Permalink
Move all interior mutability from Context to CtxRef and make it a handle
Browse files Browse the repository at this point in the history
  • Loading branch information
danielkeller committed Dec 29, 2021
1 parent d775eb3 commit 875efe1
Show file tree
Hide file tree
Showing 36 changed files with 283 additions and 297 deletions.
4 changes: 2 additions & 2 deletions egui-winit/src/epi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ impl Persistence {
pub fn save(
&mut self,
_app: &mut dyn epi::App,
_egui_ctx: &egui::Context,
_egui_ctx: &egui::CtxRef,
_window: &winit::window::Window,
) {
#[cfg(feature = "persistence")]
Expand All @@ -174,7 +174,7 @@ impl Persistence {
pub fn maybe_autosave(
&mut self,
app: &mut dyn epi::App,
egui_ctx: &egui::Context,
egui_ctx: &egui::CtxRef,
window: &winit::window::Window,
) {
let now = std::time::Instant::now();
Expand Down
4 changes: 2 additions & 2 deletions egui-winit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ impl State {
/// Note that egui uses `tab` to move focus between elements, so this will always return `true` for tabs.
pub fn on_event(
&mut self,
egui_ctx: &egui::Context,
egui_ctx: &egui::CtxRef,
event: &winit::event::WindowEvent<'_>,
) -> bool {
use winit::event::WindowEvent;
Expand Down Expand Up @@ -512,7 +512,7 @@ impl State {
pub fn handle_output(
&mut self,
window: &winit::window::Window,
egui_ctx: &egui::Context,
egui_ctx: &egui::CtxRef,
output: egui::Output,
) {
self.current_pixels_per_point = egui_ctx.pixels_per_point(); // someone can have changed it to scale the UI
Expand Down
9 changes: 5 additions & 4 deletions egui/src/containers/area.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,15 +369,16 @@ impl Prepared {
}
}

fn pointer_pressed_on_area(ctx: &Context, layer_id: LayerId) -> bool {
if let Some(pointer_pos) = ctx.input().pointer.interact_pos() {
ctx.input().pointer.any_pressed() && ctx.layer_id_at(pointer_pos) == Some(layer_id)
fn pointer_pressed_on_area(ctx: &CtxRef, layer_id: LayerId) -> bool {
if let Some(pointer_pos) = ctx.interact_pos() {
let any_pressed = ctx.input().pointer.any_pressed();
return any_pressed && ctx.layer_id_at(pointer_pos) == Some(layer_id);
} else {
false
}
}

fn automatic_area_position(ctx: &Context) -> Pos2 {
fn automatic_area_position(ctx: &CtxRef) -> Pos2 {
let mut existing: Vec<Rect> = ctx
.memory()
.areas
Expand Down
10 changes: 5 additions & 5 deletions egui/src/containers/collapsing_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,23 @@ impl Default for State {
}

impl State {
pub fn load(ctx: &Context, id: Id) -> Option<Self> {
pub fn load(ctx: &CtxRef, id: Id) -> Option<Self> {
ctx.memory().data.get_persisted(id)
}

pub fn store(self, ctx: &Context, id: Id) {
pub fn store(self, ctx: &CtxRef, id: Id) {
ctx.memory().data.insert_persisted(id, self);
}

pub fn from_memory_with_default_open(ctx: &Context, id: Id, default_open: bool) -> Self {
pub fn from_memory_with_default_open(ctx: &CtxRef, id: Id, default_open: bool) -> Self {
Self::load(ctx, id).unwrap_or_else(|| State {
open: default_open,
..Default::default()
})
}

// Helper
pub fn is_open(ctx: &Context, id: Id) -> Option<bool> {
pub fn is_open(ctx: &CtxRef, id: Id) -> Option<bool> {
if ctx.memory().everything_is_visible() {
Some(true)
} else {
Expand All @@ -53,7 +53,7 @@ impl State {
}

/// 0 for closed, 1 for open, with tweening
pub fn openness(&self, ctx: &Context, id: Id) -> f32 {
pub fn openness(&self, ctx: &CtxRef, id: Id) -> f32 {
if ctx.memory().everything_is_visible() {
1.0
} else {
Expand Down
9 changes: 5 additions & 4 deletions egui/src/containers/panel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ struct PanelState {
}

impl PanelState {
fn load(ctx: &Context, bar_id: Id) -> Option<Self> {
fn load(ctx: &CtxRef, bar_id: Id) -> Option<Self> {
ctx.memory().data.get_persisted(bar_id)
}

fn store(self, ctx: &Context, bar_id: Id) {
fn store(self, ctx: &CtxRef, bar_id: Id) {
ctx.memory().data.insert_persisted(bar_id, self);
}
}
Expand Down Expand Up @@ -191,7 +191,7 @@ impl SidePanel {
let mut is_resizing = false;
if resizable {
let resize_id = id.with("__resize");
if let Some(pointer) = ui.input().pointer.latest_pos() {
if let Some(pointer) = ui.ctx().latest_pos() {
let we_are_on_top = ui
.ctx()
.layer_id_at(pointer)
Expand Down Expand Up @@ -469,7 +469,8 @@ impl TopBottomPanel {
let mut is_resizing = false;
if resizable {
let resize_id = id.with("__resize");
if let Some(pointer) = ui.input().pointer.latest_pos() {
let latest_pos = ui.input().pointer.latest_pos();
if let Some(pointer) = latest_pos {
let we_are_on_top = ui
.ctx()
.layer_id_at(pointer)
Expand Down
4 changes: 2 additions & 2 deletions egui/src/containers/popup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ pub(crate) struct MonoState {
}

impl MonoState {
fn load(ctx: &Context) -> Option<Self> {
fn load(ctx: &CtxRef) -> Option<Self> {
ctx.memory().data.get_temp(Id::null())
}

fn store(self, ctx: &Context) {
fn store(self, ctx: &CtxRef) {
ctx.memory().data.insert_temp(Id::null(), self);
}

Expand Down
4 changes: 2 additions & 2 deletions egui/src/containers/resize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ pub(crate) struct State {
}

impl State {
pub fn load(ctx: &Context, id: Id) -> Option<Self> {
pub fn load(ctx: &CtxRef, id: Id) -> Option<Self> {
ctx.memory().data.get_persisted(id)
}

pub fn store(self, ctx: &Context, id: Id) {
pub fn store(self, ctx: &CtxRef, id: Id) {
ctx.memory().data.insert_persisted(id, self);
}
}
Expand Down
11 changes: 5 additions & 6 deletions egui/src/containers/scroll_area.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ impl Default for State {
}

impl State {
pub fn load(ctx: &Context, id: Id) -> Option<Self> {
pub fn load(ctx: &CtxRef, id: Id) -> Option<Self> {
ctx.memory().data.get_persisted(id)
}

pub fn store(self, ctx: &Context, id: Id) {
pub fn store(self, ctx: &CtxRef, id: Id) {
ctx.memory().data.insert_persisted(id, self);
}
}
Expand Down Expand Up @@ -523,12 +523,11 @@ impl Prepared {
};
let content_response = ui.interact(inner_rect, id.with("area"), sense);

let input = ui.input();
if content_response.dragged() {
for d in 0..2 {
if has_bar[d] {
state.offset[d] -= input.pointer.delta()[d];
state.vel[d] = input.pointer.velocity()[d];
state.offset[d] -= ui.input().pointer.delta()[d];
state.vel[d] = ui.input().pointer.velocity()[d];
state.scroll_stuck_to_end[d] = false;
} else {
state.vel[d] = 0.0;
Expand All @@ -537,7 +536,7 @@ impl Prepared {
} else {
let stop_speed = 20.0; // Pixels per second.
let friction_coeff = 1000.0; // Pixels per second squared.
let dt = input.unstable_dt;
let dt = ui.input().unstable_dt;

let friction = friction_coeff * dt;
if friction > state.vel.length() || state.vel.length() < stop_speed {
Expand Down
14 changes: 7 additions & 7 deletions egui/src/containers/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ impl<'open> Window<'open> {
.and_then(|window_interaction| {
// Calculate roughly how much larger the window size is compared to the inner rect
let title_bar_height = if with_title_bar {
title.font_height(ctx.fonts(), &ctx.style()) + title_content_spacing
title.font_height(&ctx) + title_content_spacing
} else {
0.0
};
Expand Down Expand Up @@ -480,7 +480,7 @@ pub(crate) struct WindowInteraction {
}

impl WindowInteraction {
pub fn set_cursor(&self, ctx: &Context) {
pub fn set_cursor(&self, ctx: &CtxRef) {
if (self.left && self.top) || (self.right && self.bottom) {
ctx.output().cursor_icon = CursorIcon::ResizeNwSe;
} else if (self.right && self.top) || (self.left && self.bottom) {
Expand All @@ -499,7 +499,7 @@ impl WindowInteraction {

fn interact(
window_interaction: WindowInteraction,
ctx: &Context,
ctx: &CtxRef,
margins: Vec2,
area_layer_id: LayerId,
area: &mut area::Prepared,
Expand All @@ -524,7 +524,7 @@ fn interact(
Some(window_interaction)
}

fn move_and_resize_window(ctx: &Context, window_interaction: &WindowInteraction) -> Option<Rect> {
fn move_and_resize_window(ctx: &CtxRef, window_interaction: &WindowInteraction) -> Option<Rect> {
window_interaction.set_cursor(ctx);

// Only move/resize windows with primary mouse button:
Expand Down Expand Up @@ -566,7 +566,7 @@ fn move_and_resize_window(ctx: &Context, window_interaction: &WindowInteraction)

/// Returns `Some` if there is a move or resize
fn window_interaction(
ctx: &Context,
ctx: &CtxRef,
possible: PossibleInteractions,
area_layer_id: LayerId,
id: Id,
Expand Down Expand Up @@ -606,7 +606,7 @@ fn window_interaction(
}

fn resize_hover(
ctx: &Context,
ctx: &CtxRef,
possible: PossibleInteractions,
area_layer_id: LayerId,
rect: Rect,
Expand Down Expand Up @@ -757,7 +757,7 @@ fn show_title_bar(
) -> TitleBar {
let inner_response = ui.horizontal(|ui| {
let height = title
.font_height(ui.fonts(), ui.style())
.font_height(ui.ctx())
.max(ui.spacing().interact_size.y);
ui.set_min_height(height);

Expand Down
Loading

0 comments on commit 875efe1

Please sign in to comment.