Skip to content

Refactor deprecated winit#101

Open
seungje0612 wants to merge 3 commits into
utilForever:mainfrom
seungje0612:issue-30-refactor-deprecated-winit
Open

Refactor deprecated winit#101
seungje0612 wants to merge 3 commits into
utilForever:mainfrom
seungje0612:issue-30-refactor-deprecated-winit

Conversation

@seungje0612
Copy link
Copy Markdown

@seungje0612 seungje0612 commented May 23, 2026

What

Refactor the native winit main loop to use ApplicationHandler and run_app instead of the deprecated closure-based EventLoop::run.

Why

  • winit 0.30 deprecates the older closure-based model in favor of the ApplicationHandler lifecycle model with EventLoop::run_app.
  • Follow that model by storing only init state in the context wrapper, creating a "NativeApp" state struct and initializing
    the native runtime during resumed

Closes #30

Checklist

Required

  • cargo check --all passes
  • cargo fmt --all -- --check passes
  • cargo clippy --workspace --all-targets -- -D warnings -A clippy::multiple-crate-versions passes
  • cargo test --all passes
  • I linked the related issue (for example: Closes #123)

Functional Validation

  • Behavior related to this change was verified locally (if applicable)
  • Rendering/backend behavior was verified when runtime code changed (if applicable)
  • Algorithm behavior (pathfinding/FOV/noise/random) was verified when affected (if applicable)
  • I added or updated tests for changed behavior (if applicable)

Configuration & Docs

  • User-facing docs were updated (README.md, ARCHITECTURE.md, or relevant manual pages, if applicable)
  • New dependencies/configuration are documented (if applicable)
  • No sensitive values or credentials were introduced

If Applicable

  • Security impact considered (run cargo audit locally if needed)
  • Breaking behavior changes are clearly described in this PR

Summary by CodeRabbit

  • Refactor
    • Reorganized native initialization process for improved control flow separation.
    • Restructured main event loop with enhanced error handling to prevent runtime panics.
    • Improved error propagation throughout the rendering pipeline.

Review Change Stack

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 23, 2026

📝 Walkthrough

Walkthrough

This PR refactors the native backend's initialization and event loop from a monolithic single-phase design into a two-phase architecture that separates EventLoop creation from window/OpenGL setup, and transitions the event loop from closure-based to struct-based ApplicationHandler. Error handling improves throughout the render path via BResult propagation.

Changes

Two-Phase Init and Event Loop Refactoring

Layer / File(s) Summary
Data structure reorganization for split initialization
bracket-terminal/src/hal/native/mod.rs
NativeInitSettings struct introduced to hold window sizing, title, and platform hints. WrappedContext refactored to store EventLoop and deferred NativeInitSettings instead of pre-created window/context fields.
Phase 1: EventLoop creation and deferred initialization storage
bracket-terminal/src/hal/native/init.rs
init_raw now creates winit EventLoop and stores NativeInitSettings in global BACKEND via WrappedContext, then returns BTerm with default state. New NativeRuntime struct introduced. OpenGL/window creation deferred to phase 2.
Phase 2: Window and OpenGL context creation via init_runtime
bracket-terminal/src/hal/native/init.rs
New init_runtime function takes stored NativeInitSettings and ActiveEventLoop to create window, configure OpenGL context/surface using destructured hint fields, load shaders, build FBO/VAO, update global BACKEND state, and return NativeRuntime.
Main loop ApplicationHandler-based refactoring
bracket-terminal/src/hal/native/mainloop.rs
Event loop replaced with NativeApp struct implementing ApplicationHandler that owns BTerm and GameState. Initialization moved to resumed, window events centralized in window_event, rendering in redraw. Error handling changed from unwrap/expect to BResult propagation throughout.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

🐰 Two phases bloom where once was one,
EventLoop awakens, initialization done,
Handler embraces the windowed dance,
With errors now caught in Result's stance,
Backend reborn in structured stance! 🎉

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 5.88% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Refactor deprecated winit' directly reflects the main objective: replacing deprecated closure-based EventLoop::run with winit 0.30's ApplicationHandler lifecycle model. It clearly summarizes the primary refactoring effort across all changed files.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (2)
bracket-terminal/src/hal/native/init.rs (1)

235-236: 💤 Low value

Redundant re-assignment of frame_sleep_time and resize_scaling.

These fields are already set in init_raw (lines 51-52) from the same platform_hints. Since init_runtime receives the same InitHints via NativeInitSettings, this duplicates the assignment. Consider removing these lines or consolidating the initialization.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@bracket-terminal/src/hal/native/init.rs` around lines 235 - 236, The
assignments to be.frame_sleep_time and be.resize_scaling in init_runtime are
redundant because init_raw already sets these from the same platform_hints;
remove the duplicate lines in init_runtime (references: be.frame_sleep_time,
be.resize_scaling inside init_runtime) or consolidate by passing the
already-populated InitHints/NativeInitSettings through so init_runtime relies on
init_raw's initialization (references: init_raw, init_runtime,
NativeInitSettings, InitHints, platform_hints).
bracket-terminal/src/hal/native/mainloop.rs (1)

359-370: 💤 Low value

resize_surface called twice per resize event.

queue_resize calls resize_surface immediately (line 364), then stores the event. When redraw processes the queued event (line 306), it calls resize_surface again with the same size. Consider removing the call in queue_resize since the actual resize happens during redraw anyway.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@bracket-terminal/src/hal/native/mainloop.rs` around lines 359 - 370, Remove
the eager call to resize_surface from queue_resize: instead of calling
resize_surface(physical_size) inside queue_resize, only set
self.queued_resize_event = Some(ResizeEvent { physical_size, dpi_scale_factor:
scale_factor, send_event }); rely on the existing redraw method (which inspects
queued_resize_event and calls resize_surface) to perform the actual surface
resize; keep the ResizeEvent struct, queued_resize_event field, and the
send_event handling unchanged so redraw still triggers any events after
resizing.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@bracket-terminal/src/hal/native/init.rs`:
- Around line 235-236: The assignments to be.frame_sleep_time and
be.resize_scaling in init_runtime are redundant because init_raw already sets
these from the same platform_hints; remove the duplicate lines in init_runtime
(references: be.frame_sleep_time, be.resize_scaling inside init_runtime) or
consolidate by passing the already-populated InitHints/NativeInitSettings
through so init_runtime relies on init_raw's initialization (references:
init_raw, init_runtime, NativeInitSettings, InitHints, platform_hints).

In `@bracket-terminal/src/hal/native/mainloop.rs`:
- Around line 359-370: Remove the eager call to resize_surface from
queue_resize: instead of calling resize_surface(physical_size) inside
queue_resize, only set self.queued_resize_event = Some(ResizeEvent {
physical_size, dpi_scale_factor: scale_factor, send_event }); rely on the
existing redraw method (which inspects queued_resize_event and calls
resize_surface) to perform the actual surface resize; keep the ResizeEvent
struct, queued_resize_event field, and the send_event handling unchanged so
redraw still triggers any events after resizing.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 16db8ffb-3b20-40f0-9ae5-7dd160d38bab

📥 Commits

Reviewing files that changed from the base of the PR and between fe7bf77 and 7d37eaf.

📒 Files selected for processing (3)
  • bracket-terminal/src/hal/native/init.rs
  • bracket-terminal/src/hal/native/mainloop.rs
  • bracket-terminal/src/hal/native/mod.rs
📜 Review details
🧰 Additional context used
📓 Path-based instructions (1)
**/*.rs

📄 CodeRabbit inference engine (AGENTS.md)

**/*.rs: Format all Rust code using cargo fmt --all
Fix all cargo clippy warnings — the CI enforces -D warnings
Add tests for new functionality in the relevant module; for split domains, prefer colocated tests.rs

Files:

  • bracket-terminal/src/hal/native/mod.rs
  • bracket-terminal/src/hal/native/mainloop.rs
  • bracket-terminal/src/hal/native/init.rs
🔇 Additional comments (13)
bracket-terminal/src/hal/native/mod.rs (1)

52-62: LGTM!

bracket-terminal/src/hal/native/init.rs (3)

24-28: LGTM!


30-78: LGTM!


103-174: LGTM!

Also applies to: 176-246

bracket-terminal/src/hal/native/mainloop.rs (9)

118-130: LGTM!


132-145: LGTM!


147-164: LGTM!


166-194: LGTM!


196-288: LGTM!


290-349: LGTM!


379-417: LGTM!


419-434: LGTM!


584-592: LGTM!

Also applies to: 642-642, 734-740

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Deprecated 코드 리팩토링하기

1 participant