UI Code Quality: bContext Management #74429

Open
opened 2020-03-04 13:08:15 +01:00 by Julian Eisel · 1 comment
Member

UI Code Quality: bContext Management

NOTE: This is a proposal, not an agreed on plan.

Reducing bContext dependencies

bContext provides a handy way of passing around state information. The UI code probably relies too much on it though:

  • It's become a real hassle to ensure it's valid as needed
  • There are multiple cases where we know it's not set correctly, but fixing it is difficult. E.g. see #73565.
  • Often passes around redundant data. E.g. some parameter lists include both wmWindow * and bContext * (itself also including a wmWindow *) - which window should be used? Can we rely on them being the same, or when do they differ?
  • Testing the UI code is difficult either way, but removing dependencies on context, and only passing around actually needed data, should make units more isolated, thus more testable.

Ideally, I think context should only be passed to high-level operator, drawing or handler functions and the like. These should then read out needed data from context and use that to perform actions on this data as input. In-between these callbacks there should not be any access to the context.

Plan of action:

  • Get rid of bContext * usage in lower level functions or we it can be easily avoided (see P1250).
  • Add utility structures to wrap common UI data to operate on (wmWindow, bScreen, ScrArea, etc.).
  • Add more sanity checks for context state.

Maybe we should also look into only actually creating a context when it's needed for callbacks. Making that easy and reliable isn't a low-hanging fruit though.

Push/pop bContext semantics

There are more and more places where we temporarily override context members, which always follows the same pattern. It's a good idea to generalize this, for esthetical reasons but also to prevent errors (e.g. single variables not reset in certain execution paths). We could introduce a simple and lightweight static context stack with push and pop calls to wrap any temorary overrides.

## UI Code Quality: `bContext` Management NOTE: This is a proposal, not an agreed on plan. **Reducing `bContext` dependencies** `bContext` provides a handy way of passing around state information. The UI code probably relies too much on it though: * It's become a real hassle to ensure it's valid as needed * There are multiple cases where we know it's not set correctly, but fixing it is difficult. E.g. see #73565. * Often passes around redundant data. E.g. some parameter lists include both `wmWindow *` and `bContext *` (itself also including a `wmWindow *`) - which window should be used? Can we rely on them being the same, or when do they differ? * Testing the UI code is difficult either way, but removing dependencies on context, and only passing around actually needed data, should make units more isolated, thus more testable. Ideally, I think context should only be passed to high-level operator, drawing or handler functions and the like. These should then read out needed data from context and use that to perform actions on this data as input. In-between these callbacks there should not be any access to the context. Plan of action: * Get rid of `bContext *` usage in lower level functions or we it can be easily avoided (see [P1250](https://archive.blender.org/developer/P1250.txt)). * Add utility structures to wrap common UI data to operate on (`wmWindow`, `bScreen`, `ScrArea`, etc.). * Add more sanity checks for context state. Maybe we should also look into only actually creating a context when it's needed for callbacks. Making that easy and reliable isn't a low-hanging fruit though. **Push/pop `bContext` semantics** There are more and more places where we temporarily override context members, which always follows the same pattern. It's a good idea to generalize this, for esthetical reasons but also to prevent errors (e.g. single variables not reset in certain execution paths). We could introduce a simple and lightweight static context stack with push and pop calls to wrap any temorary overrides.
Author
Member

Added subscriber: @JulianEisel

Added subscriber: @JulianEisel
Julian Eisel changed title from UI Code Quality: `bContext` Management to UI Code Quality: bContext Management 2020-03-04 13:14:29 +01:00
Philipp Oeser removed the
Interest
User Interface
label 2023-02-10 09:24:46 +01:00
Iliya Katushenock removed the
Status
Needs Triage
label 2024-02-06 12:08:30 +01:00
Sign in to join this conversation.
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#74429
No description provided.